BoxAST
Generates the Abstract Syntax Tree (AST) for BoxLang source code or a file.
The AST represents the syntactic structure of the code and can be used for code analysis, transformation, or generation.
Usage Examples:
// Parse source code and return as struct (default)
ast = boxAST( source = "x = 1 + 2" );
println( ast.ASTType ); // Outputs: BoxAssignment
// Parse source code and return as JSON
json = boxAST( source = "function add(a, b) { return a + b; }", returnType = "json" );
println( json ); // Outputs: JSON representation of the AST
// Parse source code and return as text
text = boxAST( source = "if (x > 5) { println('yes'); }", returnType = "text" );
println( text ); // Outputs: Human-readable text representation
// Parse a file
ast = boxAST( filepath = "src/MyClass.bx" );
// Use as a member function on a string
source = "a = [1, 2, 3]";
ast = source.toAST(); // Returns AST as struct
ast = source.toAST( returnType = "json" ); // Returns AST as JSON string
// Parse CFML/ColdFusion syntax
ast = boxAST( source = "cfset x = 1", sourceType = "cfscript" );
// Parse template syntax
ast = boxAST( source = "#now()#", sourceType = "template" );
The returned AST structure contains nodes with the following key properties:
ASTType - The type of AST node (e.g., BoxAssignment, BoxFunctionDeclaration, BoxClass)
ASTPackage - The package name of the AST node class
Additional properties specific to each node type (e.g., name, value, children, etc.)
Method Signature
Arguments
source
string
false
The BoxLang source code to parse. Either source or filepath must be provided. When used as a member function, this is automatically set to the string value.
filepath
string
false
The path to a BoxLang file to parse. Either source or filepath must be provided. Can be relative (to the current working directory) or absolute.
returnType
string
false
The format of the returned AST. Valid values are "struct" (default), "json", or "text".
struct - Returns a nested structure (Map) representing the AST hierarchy
json - Returns a JSON string representation of the AST
text - Returns a human-readable text representation of the AST
struct
sourceType
string
false
The type of source code being parsed. Valid values are "script" (default), "template", "cfscript", or "cftemplate".
script - BoxLang script syntax (BOXSCRIPT)
template - BoxLang template syntax (BOXTEMPLATE)
cfscript - ColdFusion script syntax (CFSCRIPT)
cftemplate - ColdFusion template syntax (CFTEMPLATE)
script
Examples
Related
Last updated
Was this helpful?
