Overview
TheLuaSyntaxVisitor<TResult> class provides a generic visitor pattern implementation for traversing Lua syntax trees. It visits a single node and produces a value of type TResult. This class is ideal when you need to extract or compute information from syntax nodes.
Namespace
Syntax
Type Parameters
- TResult: The type of value returned by the Visit methods.
When to Use
UseLuaSyntaxVisitor<TResult> when you need to:
- Extract specific information from syntax nodes
- Compute values based on the syntax tree structure
- Implement pattern matching over syntax nodes
- Return a result from visiting each node
- Need to automatically traverse child nodes
- Don’t need to return values from visits
- Want to perform operations at specific tree depths (nodes, tokens, trivia)
Key Methods
Visit
default(TResult) if the node is null.
Parameters:
node: The syntax node to visit
DefaultVisit
default(TResult) by default. Override this to provide common behavior for all node types.
Visit Methods
The visitor provides specializedVisit methods for each syntax node type. Override the appropriate method to handle specific node types:
Expression Visitors
VisitAnonymousFunctionExpression- Function expressionsVisitBinaryExpression- Binary operations (+, -, *, etc.)VisitUnaryExpression- Unary operations (-, not, #, etc.)VisitLiteralExpression- Literals (numbers, strings, true, false, nil)VisitIdentifierName- Variable namesVisitMemberAccessExpression- Member access (table.field)VisitElementAccessExpression- Element access (table[key])VisitFunctionCallExpression- Function callsVisitMethodCallExpression- Method calls (object:method())VisitTableConstructorExpression- Table constructorsVisitParenthesizedExpression- Parenthesized expressionsVisitIfExpression- If expressions (ternary-like)VisitVarArgExpression- Variadic arguments (…)VisitInterpolatedStringExpression- Interpolated stringsVisitTypeCastExpression- Type casts
Statement Visitors
VisitAssignmentStatement- Variable assignmentsVisitCompoundAssignmentStatement- Compound assignments (+=, -=, etc.)VisitLocalVariableDeclarationStatement- Local variable declarationsVisitFunctionDeclarationStatement- Function declarationsVisitLocalFunctionDeclarationStatement- Local function declarationsVisitIfStatement- If statementsVisitWhileStatement- While loopsVisitRepeatUntilStatement- Repeat-until loopsVisitNumericForStatement- Numeric for loopsVisitGenericForStatement- Generic for loopsVisitDoStatement- Do blocksVisitReturnStatement- Return statementsVisitBreakStatement- Break statementsVisitContinueStatement- Continue statementsVisitGotoStatement- Goto statementsVisitGotoLabelStatement- Goto labelsVisitExpressionStatement- Expression statementsVisitEmptyStatement- Empty statements (lone semicolons)
Type System Visitors (Typed Lua)
VisitSimpleTypeName- Simple type namesVisitCompositeTypeName- Composite type namesVisitFunctionType- Function typesVisitTableType- Table typesVisitArrayType- Array typesVisitUnionType- Union typesVisitIntersectionType- Intersection typesVisitNilableType- Nilable typesVisitLiteralType- Literal typesVisitTypeofType- Typeof typesVisitParenthesizedType- Parenthesized types
Other Visitors
VisitCompilationUnit- Root node of a syntax treeVisitStatementList- List of statementsVisitParameterList- Function parametersVisitEqualsValuesClause- The= valuespart of assignmentsVisitElseIfClause- Elseif clauses in if statementsVisitElseClause- Else clauses in if statements- And many more…
Examples
Example 1: Count Specific Node Types
Example 2: Extract String Literals
Example 3: Find All Global Variables
Differences from LuaSyntaxWalker
| Feature | LuaSyntaxVisitor (generic) | LuaSyntaxWalker |
|---|---|---|
| Returns values | Yes (TResult) | No (void) |
| Auto-traverses children | No (manual) | Yes (automatic) |
| Depth control | No | Yes (Node, Token, Trivia, StructuredTrivia) |
| Use case | Extract/compute information | Perform operations during traversal |
| Override pattern | Override specific Visit methods | Override specific Visit methods |
| Default behavior | Returns default(TResult) | Visits all children |
See Also
- LuaSyntaxWalker - For automatic tree traversal
- LuaSyntaxRewriter - For transforming syntax trees
- LuaSyntaxNode - Base class for all syntax nodes
- SyntaxNode - Base Roslyn syntax node