Discover Packages
github.com/wudi/php-parser
examples
ast-visitor
command
Version:
v0.1.0
Opens a new window with list of versions in this module.
Published: Sep 3, 2025
License: MIT
Opens a new window with license information.
Imports: 5
Opens a new window with list of imports.
Imported by: ? modules, 0 packages
The first number is the count of modules that contain a package that imports this one.
The module count is a better indicator of popularity than the package count.
A module with 100 importing packages is counted only once instead of 100 times,
reflecting the fact that the choice to use the package was likely made only once,
not 100 times.
A '?' means that the value hasn't been computed yet.
Opens a new window with list of known importers.
README
README
¶
AST Visitor Example
This example demonstrates how to use the visitor pattern to traverse and analyze the Abstract Syntax Tree (AST).
What it does
Shows how to implement custom visitors to collect specific information from the AST
Demonstrates the use of built-in visitor functions for common tasks
Provides examples of different analysis patterns:
Variable collection with duplicate removal
Function declaration analysis
AST depth calculation
Node type statistics
Key concepts
Custom Visitors
VariableCollector: Finds all variable usage in the code
FunctionCollector: Collects function declarations with parameter counts
DepthCounter: Calculates the maximum depth of the AST
Built-in Visitor Functions
ast.FindAllFunc(): Find all nodes matching a condition
ast.CountFunc(): Count nodes matching a condition
ast.WalkFunc(): Simple traversal with a function
Running the example
cd examples/ast-visitor
go run main.go
Expected output
The program will display:
List of all unique variables found in the code
Information about each function declaration
Maximum depth of the AST
Statistics about different node types
Count of various expression types
This pattern is useful for building code analysis tools, linters, and documentation generators.
Expand ▾
Collapse ▴
Documentation
¶
There is no documentation for this package.
Source Files
¶
Click to show internal directories.
Click to hide internal directories.