string
String Methods
toAST(filepath=[string], returnType=[string], sourceType=[string])
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 = ",<,bx:output>#now()#,<,/bx:output>", 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.),
, ,
Arguments:
filepath
string
false
null
returnType
string
false
struct
sourceType
string
false
script
jSONDeserialize(strictMapping=[boolean], useCustomSerializer=[string])
Converts a JSON (JavaScript Object Notation) string data representation into data, such as a structure or array.
Arguments:
strictMapping
boolean
false
true
useCustomSerializer
string
false
null
fromJSON(strictMapping=[boolean], useCustomSerializer=[string])
Converts a JSON (JavaScript Object Notation) string data representation into data, such as a structure or array.
Arguments:
strictMapping
boolean
false
true
useCustomSerializer
string
false
null
Examples
Last updated
Was this helpful?
