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 x

  • A binary operation node for addition

  • Literal nodes for values 1 and 2

📋 Table of Contents

📋 BoxAST() BIF

The BoxAST() function parses BoxLang or CFML source code and returns its AST representation.

Syntax

Parameters

Parameter
Type
Required
Default
Description

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

Performance Tips:

  • Use filepath parameter for large files instead of reading into memory first

  • Cache AST results for files that don't change frequently

  • Use returnType: "json" when passing to external tools

  • Consider using returnType: "text" only for debugging and development

  • Parse incrementally for large codebases rather than all at once

🛠️ 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?