SwiftSyntax
A library for working with Swift code.
import SwiftSyntaxModule information
- Declarations
- 11688
- Symbols
- 51141
Overview
SwiftSyntax is a source accurate tree representation of Swift source code. It allows Swift tools to parse, inspect, generate, and transform Swift source code.
Articles
Working with SwiftSyntax
Learn the basics of working with Swift source code with the SwiftSyntax library.
Read MoreUpdating a Macro to a New Major swift-syntax Version
Guidance of how to version macros when a new major swift-syntax version is released.
Read MoreRaw Syntax Validation for Macro Testing
A guide to using
Read MoreSWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATIONin third-party Swift macro packages.Glossary
Glossary of terms and abbreviations used in SwiftSyntax
Read More
Tutorials
Introduction to SwiftSyntax
SwiftSyntax provides the foundation for tools that inspect, manipulate, and transform Swift source code.
Read More
Syntax
struct SyntaxA Syntax node represents a tree of nodes with tokens at the leaves. Each node has accessors for its known children, and allows efficient iteration over the children through its
childrenproperty.protocol SyntaxCollection
Trivia
struct TriviaTrivia represent pieces of the source code that are not relevant to represent its semantic structure.
enum TriviaPieceA contiguous stretch of a single kind of trivia. The constituent part of a
Triviacollection.enum TriviaPosition
Tokens
struct TokenSyntaxA Syntax node representing a single token.
enum TokenKindEnumerates the kinds of tokens in the Swift language.
struct TokenSequenceSequence of tokens that are part of the provided Syntax node.
enum SourcePresenceAn indicator of whether a Syntax node was found or written in the source.
Syntax Visitors
class SyntaxVisitorclass SyntaxAnyVisitorA
SyntaxVisitorthat can visit the nodes as genericSyntaxvalues.class SyntaxRewriter/ Automatically generated by generate-swift-syntax / Do not edit directly!
enum SyntaxTreeViewModeSpecifies how missing and unexpected nodes should be handled when traversing a syntax tree.
enum SyntaxVisitorContinueKindThe enum describes how the
SyntaxVisitorshould continue after visiting the current node.struct ReversedTokenSequenceReverse sequence of tokens that are part of the provided Syntax node.
Declarations
struct DeclSyntaxprotocol DeclSyntaxProtocolProtocol to which all
DeclSyntaxnodes conform.struct MissingDeclSyntaxIn case the source code is missing a declaration, this node stands in place of the missing declaration.
struct AccessorDeclSyntaxstruct ActorDeclSyntaxstruct AssociatedTypeDeclSyntaxAn
associatedtypedeclarationstruct ClassDeclSyntaxA
classdeclarationstruct DeinitializerDeclSyntaxA
deinitdeclarationstruct EditorPlaceholderDeclSyntaxAn editor placeholder, e.g.
<#declaration#>that is used in a position that expects a declaration.struct EnumCaseDeclSyntaxA
casedeclaration of a Swiftenum. It can have 1 or moreEnumCaseElements inside, each declaring a different case of the enum.struct EnumDeclSyntaxA Swift
enumdeclaration.struct ExtensionDeclSyntaxAn
extensiondeclaration.struct FunctionDeclSyntaxA Swift
funcdeclaration.struct IfConfigDeclSyntaxstruct ImportDeclSyntaxAn
importdeclarationstruct InitializerDeclSyntaxAn
initdeclarationstruct MacroDeclSyntaxstruct MacroExpansionDeclSyntaxThe expansion of a freestanding macro in a position that expects a declaration.
struct OperatorDeclSyntaxA Swift
operatordeclaration.struct PoundSourceLocationSyntaxstruct PrecedenceGroupDeclSyntaxA Swift
precedencegroupdeclaration.struct ProtocolDeclSyntaxA
protocoldeclarationstruct StructDeclSyntaxA
structdeclarationstruct SubscriptDeclSyntaxstruct TypeAliasDeclSyntaxstruct VariableDeclSyntaxDeclaration of one or more variables
Expressions
struct ExprSyntaxprotocol ExprSyntaxProtocolProtocol to which all
ExprSyntaxnodes conform.struct MissingExprSyntaxIn case the source code is missing an expression, this node stands in place of the missing expression.
struct ArrayExprSyntaxAn array literal.
struct ArrowExprSyntaxThe arrow when a type is used at a position that syntactically expectes an expression.
struct AsExprSyntaxThe cast of an expressison to a different type.
struct AssignmentExprSyntaxstruct AwaitExprSyntaxstruct BinaryOperatorExprSyntaxAn operator like
+or-.struct BooleanLiteralExprSyntaxstruct BorrowExprSyntaxstruct ClosureExprSyntaxstruct ConsumeExprSyntaxstruct CopyExprSyntaxstruct DeclReferenceExprSyntaxstruct DictionaryExprSyntaxA dictionary literal
struct DiscardAssignmentExprSyntaxA
_that discards a value inside an assignment.struct EditorPlaceholderExprSyntaxstruct FloatLiteralExprSyntaxstruct ForceUnwrapExprSyntaxstruct FunctionCallExprSyntaxstruct GenericSpecializationExprSyntaxstruct IfExprSyntaxstruct InOutExprSyntaxAn expression prefixed with
&to pass an argument to aninoutparameter.struct InfixOperatorExprSyntaxAn infix operator call like
1 + 2.struct IntegerLiteralExprSyntaxstruct IsExprSyntaxChecks if an expression is of a given type.
struct KeyPathExprSyntaxA key path.
struct MacroExpansionExprSyntaxThe expansion of a freestanding macro in a position that expects an expression.
struct MemberAccessExprSyntaxAn expression that access a member like a function or a property.
struct NilLiteralExprSyntaxstruct OptionalChainingExprSyntaxstruct PackElementExprSyntaxA pack element expression spelled with
each.struct PackExpansionExprSyntaxA pack expansion expression spelled with
repeat.struct PatternExprSyntaxstruct PostfixIfConfigExprSyntaxstruct PostfixOperatorExprSyntaxstruct PrefixOperatorExprSyntaxA prefix operator applied to a value.
struct RegexLiteralExprSyntaxstruct SequenceExprSyntaxA flat list of expressions before operator folding using the
SwiftOperatorslibrary.struct SimpleStringLiteralExprSyntaxA simple string that can’t contain string interpolation and cannot have raw string delimiters.
struct StringLiteralExprSyntaxA string literal.
struct SubscriptCallExprSyntaxstruct SuperExprSyntaxstruct SwitchExprSyntaxA
switchexpression.struct TernaryExprSyntaxThe ternary operator with operator precedences resolved.
struct TryExprSyntaxAn expression prefixed with
try.struct TupleExprSyntaxstruct TypeExprSyntaxstruct UnresolvedAsExprSyntaxThe
askeyword without any operands.struct UnresolvedIsExprSyntaxThe
iskeyword without any operands.struct UnresolvedTernaryExprSyntaxThe middle section of a ternary operator between
?and:.
Patterns
struct PatternSyntaxprotocol PatternSyntaxProtocolProtocol to which all
PatternSyntaxnodes conform.struct MissingPatternSyntaxIn case the source code is missing a pattern, this node stands in place of the missing pattern.
struct ExpressionPatternSyntaxA pattern that contains an
ExprSyntaxProtocol.struct IdentifierPatternSyntaxA pattern that contains a
TokenSyntax.struct IsTypePatternSyntaxstruct TuplePatternSyntaxA pattern that contains a list of other pattern.
struct ValueBindingPatternSyntaxstruct WildcardPatternSyntaxA pattern that represent a wildcard.
Statements
struct StmtSyntaxprotocol StmtSyntaxProtocolProtocol to which all
StmtSyntaxnodes conform.struct MissingStmtSyntaxIn case the source code is missing a statement, this node stands in place of the missing statement.
struct BreakStmtSyntaxstruct ContinueStmtSyntaxstruct DeferStmtSyntaxstruct DiscardStmtSyntaxstruct DoStmtSyntaxstruct ExpressionStmtSyntaxstruct FallThroughStmtSyntaxstruct ForStmtSyntaxstruct GuardStmtSyntaxstruct LabeledStmtSyntaxstruct RepeatStmtSyntaxstruct ReturnStmtSyntaxstruct ThrowStmtSyntaxstruct WhileStmtSyntaxstruct YieldStmtSyntax
Types
struct TypeSyntaxprotocol TypeSyntaxProtocolProtocol to which all
TypeSyntaxnodes conform.struct MissingTypeSyntaxIn case the source code is missing a type, this node stands in place of the missing type.
struct ArrayTypeSyntaxstruct AttributedTypeSyntaxstruct ClassRestrictionTypeSyntaxstruct CompositionTypeSyntaxstruct DictionaryTypeSyntaxstruct FunctionTypeSyntaxstruct IdentifierTypeSyntaxstruct ImplicitlyUnwrappedOptionalTypeSyntaxstruct MemberTypeSyntaxstruct MetatypeTypeSyntaxstruct NamedOpaqueReturnTypeSyntaxstruct OptionalTypeSyntaxstruct PackElementTypeSyntaxstruct PackExpansionTypeSyntaxstruct SomeOrAnyTypeSyntaxstruct SuppressedTypeSyntaxstruct TupleTypeSyntax
Collections
struct SyntaxChildrenCollection that contains the present child
Syntaxnodes of the given node.struct SyntaxChildrenIndexAn index in a syntax children collection.
struct AccessorDeclListSyntaxstruct ArrayElementListSyntaxstruct ArrayElementSyntaxAn element inside an array literal.
struct AttributeListSyntaxA list of attributes that can be attached to a declaration.
struct AttributeSyntaxAn
@attribute.struct AvailabilityArgumentListSyntaxstruct AvailabilityArgumentSyntaxA single argument to an
@availableargument like*,iOS 10.1, ormessage: "This has been deprecated".struct CatchClauseListSyntaxstruct CatchClauseSyntaxstruct CatchItemListSyntaxstruct CatchItemSyntaxstruct ClosureCaptureListSyntaxstruct ClosureCaptureSyntaxstruct ClosureParameterListSyntaxstruct ClosureParameterSyntaxstruct ClosureShorthandParameterListSyntaxA list of closure parameters that are not parenthesized and don’t have type annotations.
struct ClosureShorthandParameterSyntaxstruct CodeBlockItemListSyntaxstruct CodeBlockItemSyntaxA CodeBlockItem is any Syntax node that appears on its own line inside a CodeBlock.
struct CompositionTypeElementListSyntaxstruct CompositionTypeElementSyntaxstruct ConditionElementListSyntaxstruct ConditionElementSyntaxstruct DeclModifierListSyntaxstruct DeclModifierSyntaxstruct DeclNameArgumentListSyntaxstruct DeclNameArgumentSyntaxstruct DesignatedTypeListSyntaxstruct DesignatedTypeSyntaxstruct DictionaryElementListSyntaxstruct DictionaryElementSyntaxAn element inside a dictionary literal.
struct DifferentiabilityArgumentListSyntaxstruct DifferentiabilityArgumentSyntaxA differentiability argument: either the “self” identifier, a function parameter name, or a function parameter index.
struct DocumentationAttributeArgumentListSyntaxThe arguments of the ‘@_documentation’ attribute
struct DocumentationAttributeArgumentSyntaxstruct EffectsAttributeArgumentListSyntaxThe arguments of the ‘@_effects’ attribute. These will be parsed during the SIL stage.
struct EnumCaseElementListSyntaxA collection of 0 or more
EnumCaseElementSyntaxs.struct EnumCaseElementSyntaxAn element of an enum case, containing the name of the case and, optionally, either associated values or an assignment to a raw value.
struct EnumCaseParameterListSyntaxstruct EnumCaseParameterSyntaxstruct ExprListSyntaxA list of expressions connected by operators. This list is contained by a
SequenceExprSyntax.struct FunctionParameterListSyntaxA list of function parameters that are type annotated and a label. The function parameters are represented by
FunctionParameterListSyntax.struct FunctionParameterSyntaxA function parameter
struct GenericArgumentListSyntaxstruct GenericArgumentSyntaxstruct GenericParameterListSyntaxstruct GenericParameterSyntaxstruct GenericRequirementListSyntaxstruct GenericRequirementSyntaxstruct IfConfigClauseListSyntaxstruct IfConfigClauseSyntaxstruct ImportPathComponentListSyntaxstruct ImportPathComponentSyntaxstruct InheritedTypeListSyntaxstruct InheritedTypeSyntaxstruct KeyPathComponentListSyntaxThe components of a key path
struct KeyPathComponentSyntaxA single key path component
struct LabeledExprListSyntaxstruct LabeledExprSyntaxAn expression that is prefixed by a label.
struct MemberBlockItemListSyntaxstruct MemberBlockItemSyntaxA member declaration of a type consisting of a declaration and an optional semicolon;
struct MultipleTrailingClosureElementListSyntaxstruct MultipleTrailingClosureElementSyntaxstruct ObjCSelectorPieceListSyntaxstruct ObjCSelectorPieceSyntaxA piece of an Objective-C selector. Either consisting of just an identifier for a nullary selector, an identifier and a colon for a labeled argument or just a colon for an unlabeled argument
struct PatternBindingListSyntaxstruct PatternBindingSyntaxDefines variables inside a variable declaration.
struct PlatformVersionItemListSyntaxstruct PlatformVersionItemSyntaxA single platform/version pair in an attribute, e.g.
iOS 10.1.struct PrecedenceGroupAttributeListSyntaxstruct PrecedenceGroupRelationSyntaxSpecify the new precedence group’s relation to existing precedence groups.
struct PrecedenceGroupAssignmentSyntaxSpecifies the precedence of an operator when used in an operation that includes optional chaining.
struct PrecedenceGroupAssociativitySyntaxSpecifies how a sequence of operators with the same precedence level are grouped together in the absence of grouping parentheses.
struct PrecedenceGroupNameListSyntaxstruct PrecedenceGroupNameSyntaxstruct PrimaryAssociatedTypeListSyntaxstruct PrimaryAssociatedTypeSyntaxstruct SimpleStringLiteralSegmentListSyntaxString literal segments that only can contain non string interpolated or extended escaped strings
struct StringSegmentSyntaxA literal segment inside a string segment.
struct SpecializeAttributeArgumentListSyntaxA collection of arguments for the
@_specializeattributestruct LabeledSpecializeArgumentSyntaxA labeled argument for the
@_specializeattribute likeexported: truestruct SpecializeAvailabilityArgumentSyntaxThe availability argument for the _specialize attribute
struct SpecializeTargetFunctionArgumentSyntaxA labeled argument for the
@_specializeattribute with a function decl value liketarget: myFunc(_:)struct GenericWhereClauseSyntaxA
whereclause that places additional constraints on generic parameters likewhere Element: Hashable.struct StringLiteralSegmentListSyntaxstruct StringSegmentSyntaxA literal segment inside a string segment.
struct ExpressionSegmentSyntaxAn interpolated expression inside a string literal.
struct SwitchCaseItemListSyntaxstruct SwitchCaseItemSyntaxstruct SwitchCaseListSyntaxstruct SwitchCaseSyntaxstruct TuplePatternElementListSyntaxA list of
TuplePatternElementSyntax.struct TuplePatternElementSyntaxAn element that represents a single tuple value in
TuplePatternElementListSyntax.struct TupleTypeElementListSyntaxstruct TupleTypeElementSyntaxstruct TypeSpecifierListSyntaxstruct SimpleTypeSpecifierSyntaxA specifier that can be attached to a type to eg. mark a parameter as
inoutorconsumingstruct UnexpectedNodesSyntaxA collection of syntax nodes that occurred in the source code but could not be used to form a valid syntax tree.
struct VersionComponentListSyntaxstruct VersionComponentSyntaxAn element to represent a single component in a version, like
.1.struct YieldedExpressionListSyntaxstruct YieldedExpressionSyntax
Attributes
struct AttributeListSyntaxA list of attributes that can be attached to a declaration.
struct AttributeSyntaxAn
@attribute.struct BackDeployedAttributeArgumentsSyntaxA collection of arguments for the
@backDeployedattributestruct ConventionAttributeArgumentsSyntaxThe arguments for the ‘@convention(…)’.
struct ConventionWitnessMethodAttributeArgumentsSyntaxThe arguments for the ‘@convention(witness_method: …)’.
struct DerivativeAttributeArgumentsSyntaxThe arguments for the ‘@derivative(of:)’ and ‘@transpose(of:)’ attributes: the ‘of:’ label, the original declaration name, and an optional differentiability parameter list.
struct DifferentiabilityArgumentListSyntaxstruct DifferentiabilityArgumentSyntaxA differentiability argument: either the “self” identifier, a function parameter name, or a function parameter index.
struct DifferentiabilityArgumentsSyntaxThe differentiability arguments.
struct DifferentiabilityWithRespectToArgumentSyntaxA clause containing differentiability parameters.
struct DifferentiableAttributeArgumentsSyntaxThe arguments for the
@differentiableattribute: an optional differentiability kind, an optional differentiability parameter clause, and an optional ‘where’ clause.struct DocumentationAttributeArgumentListSyntaxThe arguments of the ‘@_documentation’ attribute
struct DocumentationAttributeArgumentSyntaxstruct DynamicReplacementAttributeArgumentsSyntaxThe arguments for the ‘@_dynamicReplacement’ attribute
struct EffectsAttributeArgumentListSyntaxThe arguments of the ‘@_effects’ attribute. These will be parsed during the SIL stage.
struct ExposeAttributeArgumentsSyntaxThe arguments for the ‘@_expose’ attribute
struct ImplementsAttributeArgumentsSyntaxThe arguments for the
@_implementsattribute of the formType, methodName(arg1Label:arg2Label:)struct LabeledSpecializeArgumentSyntaxA labeled argument for the
@_specializeattribute likeexported: truestruct ObjCSelectorPieceListSyntaxstruct ObjCSelectorPieceSyntaxA piece of an Objective-C selector. Either consisting of just an identifier for a nullary selector, an identifier and a colon for a labeled argument or just a colon for an unlabeled argument
struct OpaqueReturnTypeOfAttributeArgumentsSyntaxThe arguments for the ‘@_opaqueReturnTypeOf()’.
struct OriginallyDefinedInAttributeArgumentsSyntaxThe arguments for the ‘@_originallyDefinedIn’ attribute
struct PlatformVersionItemListSyntaxstruct PlatformVersionItemSyntaxA single platform/version pair in an attribute, e.g.
iOS 10.1.struct SpecializeAttributeArgumentListSyntaxA collection of arguments for the
@_specializeattributestruct SpecializeAvailabilityArgumentSyntaxThe availability argument for the _specialize attribute
struct SpecializeTargetFunctionArgumentSyntaxA labeled argument for the
@_specializeattribute with a function decl value liketarget: myFunc(_:)struct UnavailableFromAsyncAttributeArgumentsSyntaxThe arguments for the ‘@_unavailableFromAsync’ attribute
struct UnderscorePrivateAttributeArgumentsSyntaxThe arguments for the ‘@_private’ attribute
Miscellaneous Syntax
struct AccessorBlockSyntaxstruct AccessorEffectSpecifiersSyntaxstruct AccessorParametersSyntaxstruct AvailabilityConditionSyntaxstruct AvailabilityLabeledArgumentSyntaxAn argument to an
@availableattribute that consists of a label and a value, e.g.message: "This has been deprecated".struct ClosureCaptureClauseSyntaxstruct ClosureCaptureSpecifierSyntaxstruct ClosureParameterClauseSyntaxstruct ClosureSignatureSyntaxstruct CodeBlockSyntaxstruct ConformanceRequirementSyntaxstruct DeclModifierDetailSyntaxstruct DeclNameArgumentsSyntaxstruct DeinitializerEffectSpecifiersSyntaxstruct EnumCaseParameterClauseSyntaxstruct FunctionEffectSpecifiersSyntaxstruct FunctionParameterClauseSyntaxstruct FunctionSignatureSyntaxA function signature that defines the interface of the function.
struct GenericArgumentClauseSyntaxstruct GenericParameterClauseSyntaxThe parameter clause that defines the generic parameters.
struct InheritanceClauseSyntaxstruct InitializerClauseSyntaxstruct KeyPathOptionalComponentSyntaxA key path component like
?or!.struct KeyPathPropertyComponentSyntaxA key path component like
.propertyor.1.struct KeyPathSubscriptComponentSyntaxA key path component like
.[17]struct LayoutRequirementSyntaxstruct MatchingPatternConditionSyntaxstruct MemberBlockSyntaxstruct MissingSyntaxIn case the source code is missing a syntax node, this node stands in place of the missing node.
struct OperatorPrecedenceAndTypesSyntaxA clause to specify precedence group in infix operator declarations, and designated types in any operator declaration.
struct OptionalBindingConditionSyntaxstruct PlatformVersionSyntaxAn argument to
@availablethat restricts the availability on a certain platform to a version, e.g.iOS 10orswift 3.4.struct PoundSourceLocationArgumentsSyntaxstruct PrimaryAssociatedTypeClauseSyntaxstruct ReturnClauseSyntaxstruct SameTypeRequirementSyntaxstruct SourceFileSyntaxstruct SwitchCaseLabelSyntaxstruct SwitchDefaultLabelSyntaxstruct ThrowsClauseSyntaxstruct TypeAnnotationSyntaxstruct TypeEffectSpecifiersSyntaxstruct TypeInitializerClauseSyntaxstruct VersionTupleSyntaxA version number like
1.2.0. Only the first version component is required. There might be an arbitrary number of following components.struct WhereClauseSyntaxstruct YieldedExpressionsClauseSyntax
Traits
protocol BracedSyntax/ Automatically generated by generate-swift-syntax / Do not edit directly!
protocol DeclGroupSyntaxprotocol EffectSpecifiersSyntaxprotocol FreestandingMacroExpansionSyntaxprotocol NamedDeclSyntaxprotocol MissingNodeSyntaxRepresents a layout node that is missing in the source file.
protocol ParenthesizedSyntaxprotocol WithAttributesSyntaxprotocol WithCodeBlockSyntaxprotocol WithGenericParametersSyntaxSyntax nodes that have generic parameters.
protocol WithModifiersSyntaxprotocol WithOptionalCodeBlockSyntaxprotocol WithStatementsSyntaxprotocol WithTrailingCommaSyntax
Syntax Position
struct AbsolutePositionAn absolute position in a source file as text - the absolute utf8Offset from the start of the file.
typealias ByteSourceRangestruct SourceLocationRepresents a source location in a Swift file.
class SourceLocationConverterConverts
AbsolutePositions of syntax nodes toSourceLocations, and vice-versa. TheAbsolutePositions must be originating from nodes that are part of the same tree that was used to initialize this class.struct SourceRangeRepresents a half-open range in a Swift file.
struct SourceLengthThe length a syntax node spans in the source code. From any AbsolutePosition you reach a node’s end location by adding its UTF-8 length.
Internals
protocol SyntaxProtocolProvide common functionality for specialized syntax nodes. Extend this protocol to provide common functionality for all syntax nodes.
class SyntaxArenaA syntax arena owns the memory for all syntax nodes within it.
enum SyntaxEnumEnum to exhaustively switch over all different syntax nodes.
protocol SyntaxHashableProtocol that provides a common Hashable implementation for all syntax nodes
struct SyntaxIdentifierProvides a stable and unique identity for
Syntaxnodes.