Introduction
Syntax nodes are the building blocks of Loretta’s syntax trees. They represent the structural elements of Lua code, from entire files down to individual expressions and statements.Syntax Node Hierarchy
All syntax nodes in Loretta inherit from the base classLuaSyntaxNode, which provides common functionality for working with the syntax tree.
CompilationUnitSyntax - The Root Node
Every syntax tree starts with aCompilationUnitSyntax node, which represents the entire Lua source file:
Statements- AStatementListSyntaxcontaining all top-level statementsEndOfFileToken- The end-of-file token marking the end of the file
Common Properties and Methods
All syntax nodes inherit these members fromLuaSyntaxNode:
Navigation Properties
Parent- Gets the parent node in the treeChildNodes()- Gets all child nodesAncestors()- Gets all ancestor nodesDescendants()- Gets all descendant nodes
Position and Location
Span- The text span this node coversFullSpan- The span including leading and trailing triviaGetLocation()- Gets the location information for this node
Trivia (Whitespace and Comments)
GetLeadingTrivia()- Gets whitespace/comments before this nodeGetTrailingTrivia()- Gets whitespace/comments after this node
Token Access
GetFirstToken()- Gets the first token in this nodeGetLastToken()- Gets the last token in this nodeFindToken(position)- Finds a token at a specific position
Kind and Type
Kind()- Returns theSyntaxKindenum value for this nodeIsKind(kind)- Tests if the node is of a specific kind
Tokens vs. Nodes vs. Trivia
Loretta’s syntax tree consists of three fundamental elements:Syntax Nodes
Represent structural elements like statements and expressions. Nodes contain other nodes and tokens.Syntax Tokens
Represent individual keywords, identifiers, operators, and literals.Syntax Trivia
Represent whitespace, comments, and other non-semantic content.Traversing the Syntax Tree
You can walk the syntax tree in several ways:Using Visitors
Using Walkers
Manual Traversal
Using LINQ
Immutability
Syntax nodes are immutable. To modify a tree, you must create new nodes:Working with Positions
Every node knows its position in the source text:See Also
- Statements - All statement node types
- Expressions - All expression node types
- Types - Type annotation node types
- Syntax Trees - Working with syntax trees
- Visitors and Walkers - Advanced tree traversal