BoxLang AST
Access BoxLang's Abstract Syntax Tree (AST) for building code analysis tools, linters, formatters, and migration utilities
New in BoxLang 1.7.0 - The BoxAST() BIF provides programmatic access to BoxLang's Abstract Syntax Tree (AST), enabling developers to build sophisticated code analysis tools, formatters, linters, and migration utilities. The AST represents the parsed structure of BoxLang code in a format that's easy to analyze and manipulate programmatically.
🌳 What is an AST?
An Abstract Syntax Tree (AST) is a tree representation of the syntactic structure of source code. Each node in the tree represents a construct occurring in the source code. The AST abstracts away concrete syntax details (like parentheses, semicolons, and whitespace) while preserving the semantic structure of the code.
For example, the code x = 1 + 2; would be represented as an AST with:
An assignment node with target
xA binary operation node for addition
Literal nodes for values
1and2
📋 Table of Contents
📋 BoxAST() BIF
The BoxAST() function parses BoxLang or CFML source code and returns its AST representation.
Syntax
Parameters
source
string
Yes*
-
BoxLang/CFML source code to parse
filepath
string
Yes*
-
Path to file to parse (alternative to source)
returnType
string
No
"struct"
Output format: "struct", "json", or "text"
sourceType
string
No
"script"
Syntax type: "script", "template", "cfscript", or "cftemplate"
* Either source or filepath must be provided, but not both.
Return Types
struct (default)
Returns the AST as a BoxLang struct with full node hierarchy and properties. This is the most useful format for programmatic analysis.
json
Returns the AST as a JSON string, perfect for passing to external tools or storing for later analysis.
text
Returns a human-readable text representation of the AST structure, useful for debugging and visualization.
Source Types
script (default)
Parse BoxLang script syntax (.bx, .bxs files).
template
Parse BoxLang template syntax (.bxm files with <bx:> tags).
cfscript
Parse CFML/ColdFusion script syntax for migration and compatibility tools.
cftemplate
Parse CFML/ColdFusion template syntax (.cfm files with <cf> tags) for migration tools.
💡 Usage Examples
Basic AST Generation
Using String Member Method
BoxLang strings have a convenient toAST() member method:
Parsing Files
JSON Export for External Tools
Text Visualization
🎯 Use Cases
Code Analysis Tools
Build custom linters and static analysis tools to enforce coding standards:
Code Formatters
Create custom code formatting utilities:
Documentation Generators
Extract function signatures, parameters, and documentation comments:
Migration Tools
Parse and analyze CFML code for BoxLang migration:
Refactoring Tools
Analyze and transform code structures:
IDE Tooling
Power syntax highlighting, code intelligence, and autocomplete features:
🔍 AST Structure
The AST returned by BoxAST() contains detailed information about the code structure:
Node Properties
Each AST node includes detailed metadata about the code structure:
ASTType - The kind of node (e.g., "BoxScript", "BoxAssignment", "BoxBinaryOperation", "BoxIntegerLiteral")
ASTPackage - The Java package containing the node class
sourceText - Original source code for this node
position - Source location with start/end line and column numbers
comments - Associated comments
Additional Properties - Node-specific data (name, value, operator, statements, etc.)
Example AST Structure
For code: x = 1 + 2
📊 Best Practices
When to Use BoxAST():
Building code analysis and quality tools
Creating custom formatters and linters
Developing migration utilities from CFML to BoxLang
Generating documentation from source code
Implementing refactoring tools
Powering IDE features and code intelligence
Important Considerations:
Parsing Errors: Invalid syntax will throw an exception - wrap in try/catch
Large Files: Parsing very large files can consume significant memory
Source Type: Ensure you specify the correct
sourceTypefor CFML vs BoxLangAST Changes: AST structure may evolve between BoxLang versions
Read-Only: The returned AST is for analysis - use code generation to create new code
🔗 Related Resources
🛠️ Building Tools with BoxAST()
The BoxAST() BIF opens up powerful possibilities for the BoxLang ecosystem:
Example: Simple Linter
Example: Function Extractor
The possibilities are endless - from simple code metrics to sophisticated refactoring tools, BoxAST() provides the foundation for building powerful development tools in the BoxLang ecosystem.
Last updated
Was this helpful?
