Overview
TheLuaSyntaxWalker class automatically traverses an entire Lua syntax tree in depth-first order, visiting each node, token, and optionally trivia. Unlike LuaSyntaxVisitor<TResult>, it automatically visits child nodes, making it ideal for operations that need to process the entire tree.
Namespace
Syntax
When to Use
UseLuaSyntaxWalker when you need to:
- Traverse the entire syntax tree automatically
- Collect information from multiple nodes
- Perform operations at different tree depths (nodes, tokens, trivia)
- Avoid manually implementing child node traversal
- Need to return values from visits
- Want manual control over traversal
- Only need to visit specific node types
Constructor
depth: The depth to which the walker should descend
SyntaxWalkerDepth Options
TheSyntaxWalkerDepth enum controls how deep the walker descends into the syntax tree:
Node (Default)
Token
Trivia
StructuredTrivia
Key Properties
Depth
Key Methods
Visit
DefaultVisit
VisitToken
VisitTrivia
VisitLeadingTrivia
VisitTrailingTrivia
Visit Methods
The walker inherits all Visit methods fromLuaSyntaxVisitor (see LuaSyntaxVisitor for the complete list). The key difference is that you typically don’t need to manually visit children - the walker does it automatically.
Common methods to override:
VisitFunctionCallExpressionVisitIdentifierNameVisitLocalVariableDeclarationStatementVisitAssignmentStatementVisitBinaryExpression- And any other node type you’re interested in
Examples
Example 1: Collect All Identifiers
Example 2: Find All Function Declarations
Example 3: Count Tokens and Comments
Example 4: Validate Variable Usage
Important Notes
Always Call Base Methods
When overriding Visit methods, always call the base method to ensure child nodes are visited:Stack Overflow Protection
The walker includes automatic stack overflow protection for deeply nested trees. The framework monitors recursion depth and throws an exception if it becomes too deep.Differences from LuaSyntaxVisitor
| Feature | LuaSyntaxWalker | LuaSyntaxVisitor (generic) |
|---|---|---|
| Auto-traverses children | Yes | No |
| Returns values | No | Yes |
| Depth control | Yes | No |
| Override pattern | Override Visit methods + call base | Override Visit methods + manually visit children |
| Default behavior | Visits all children | Returns default(TResult) |
| Common use case | Collect/analyze tree data | Extract/compute values |
| Visit tokens/trivia | Yes (with depth setting) | No (nodes only) |
See Also
- LuaSyntaxVisitor - For manual traversal with return values
- LuaSyntaxRewriter - For transforming syntax trees
- SyntaxWalkerDepth - Depth enumeration