> For the complete documentation index, see [llms.txt](https://boxlang.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/boxast.md).

# 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

```
BoxAST(source=[string], filepath=[string], returnType=[string], sourceType=[string])
```

### Arguments

| Argument     | Type     | Required | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | Default  |
| ------------ | -------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------- |
| `source`     | `string` | `false`  | <p>The BoxLang source code to parse. Either source or filepath must be provided.<br>When used as a member function, this is automatically set to the string value.</p>                                                                                                                                                                                                                                                                                                                                       |          |
| `filepath`   | `string` | `false`  | <p>The path to a BoxLang file to parse. Either source or filepath must be provided.<br>Can be relative (to the current working directory) or absolute.</p>                                                                                                                                                                                                                                                                                                                                                   |          |
| `returnType` | `string` | `false`  | <p>The format of the returned AST. Valid values are "struct" (default), "json", or "text".<br></p><ul><li><br></li><li><strong>struct</strong> - Returns a nested structure (Map) representing the AST hierarchy</li><li><br></li><li><strong>json</strong> - Returns a JSON string representation of the AST</li><li><br></li><li><strong>text</strong> - Returns a human-readable text representation of the AST</li><li><br></li></ul>                                                                    | `struct` |
| `sourceType` | `string` | `false`  | <p>The type of source code being parsed. Valid values are "script" (default), "template", "cfscript", or "cftemplate".<br></p><ul><li><br></li><li><strong>script</strong> - BoxLang script syntax (BOXSCRIPT)</li><li><br></li><li><strong>template</strong> - BoxLang template syntax (BOXTEMPLATE)</li><li><br></li><li><strong>cfscript</strong> - ColdFusion script syntax (CFSCRIPT)</li><li><br></li><li><strong>cftemplate</strong> - ColdFusion template syntax (CFTEMPLATE)</li><li><br></li></ul> | `script` |

## Examples

### Get the AST for BoxLang source code

```java
ast = boxAST( "x = 1 + 2" );
writeOutput( ast.containsKey( "type" ) );

```

Result: true

## Related

* [ApplicationRestart](/boxlang-language/reference/built-in-functions/system/applicationrestart.md)
* [ApplicationStartTime](/boxlang-language/reference/built-in-functions/system/applicationstarttime.md)
* [ApplicationStop](/boxlang-language/reference/built-in-functions/system/applicationstop.md)
* [BoxAnnounce](/boxlang-language/reference/built-in-functions/system/boxannounce.md)
* [BoxAnnounceAsync](/boxlang-language/reference/built-in-functions/system/boxannounceasync.md)
* [BoxModuleReload](/boxlang-language/reference/built-in-functions/system/boxmodulereload.md)
* [BoxRegisterInterceptionPoints](/boxlang-language/reference/built-in-functions/system/boxregisterinterceptionpoints.md)
* [BoxRegisterInterceptor](/boxlang-language/reference/built-in-functions/system/boxregisterinterceptor.md)
* [BoxRegisterRequestInterceptor](/boxlang-language/reference/built-in-functions/system/boxregisterrequestinterceptor.md)
* [BoxUnregisterInterceptor](/boxlang-language/reference/built-in-functions/system/boxunregisterinterceptor.md)
* [BoxUnregisterRequestInterceptor](/boxlang-language/reference/built-in-functions/system/boxunregisterrequestinterceptor.md)
* [CallStackGet](/boxlang-language/reference/built-in-functions/system/callstackget.md)
* [CreateGUID](/boxlang-language/reference/built-in-functions/system/createguid.md)
* [CreateObject](/boxlang-language/reference/built-in-functions/system/createobject.md)
* [CreateUUID](/boxlang-language/reference/built-in-functions/system/createuuid.md)
* [DE](/boxlang-language/reference/built-in-functions/system/de.md)
* [DebugBoxContexts](/boxlang-language/reference/built-in-functions/system/debugboxcontexts.md)
* [Dump](/boxlang-language/reference/built-in-functions/system/dump.md)
* [Duplicate](/boxlang-language/reference/built-in-functions/system/duplicate.md)
* [echo](/boxlang-language/reference/built-in-functions/system/echo.md)
* [EncodeForHTML](/boxlang-language/reference/built-in-functions/system/encodeforhtml.md)
* [GetApplicationMetadata](/boxlang-language/reference/built-in-functions/system/getapplicationmetadata.md)
* [GetBaseTagData](/boxlang-language/reference/built-in-functions/system/getbasetagdata.md)
* [GetBaseTagList](/boxlang-language/reference/built-in-functions/system/getbasetaglist.md)
* [GetBaseTemplatePath](/boxlang-language/reference/built-in-functions/system/getbasetemplatepath.md)
* [GetBoxContext](/boxlang-language/reference/built-in-functions/system/getboxcontext.md)
* [GetBoxRuntime](/boxlang-language/reference/built-in-functions/system/getboxruntime.md)
* [GetBoxVersionInfo](/boxlang-language/reference/built-in-functions/system/getboxversioninfo.md)
* [GetClassMetadata](/boxlang-language/reference/built-in-functions/system/getclassmetadata.md)
* [GetComponentList](/boxlang-language/reference/built-in-functions/system/getcomponentlist.md)
* [GetContextRoot](/boxlang-language/reference/built-in-functions/system/getcontextroot.md)
* [GetCurrentTemplatePath](/boxlang-language/reference/built-in-functions/system/getcurrenttemplatepath.md)
* [GetFileFromPath](/boxlang-language/reference/built-in-functions/system/getfilefrompath.md)
* [GetFunctionCalledName](/boxlang-language/reference/built-in-functions/system/getfunctioncalledname.md)
* [GetFunctionList](/boxlang-language/reference/built-in-functions/system/getfunctionlist.md)
* [GetModuleInfo](/boxlang-language/reference/built-in-functions/system/getmoduleinfo.md)
* [GetModuleList](/boxlang-language/reference/built-in-functions/system/getmodulelist.md)
* [GetModuleTree](/boxlang-language/reference/built-in-functions/system/getmoduletree.md)
* [GetRequestClassLoader](/boxlang-language/reference/built-in-functions/system/getrequestclassloader.md)
* [GetSemver](/boxlang-language/reference/built-in-functions/system/getsemver.md)
* [GetSystemSetting](/boxlang-language/reference/built-in-functions/system/getsystemsetting.md)
* [GetTempDirectory](/boxlang-language/reference/built-in-functions/system/gettempdirectory.md)
* [GetTickCount](/boxlang-language/reference/built-in-functions/system/gettickcount.md)
* [htmlEditFormat](/boxlang-language/reference/built-in-functions/system/htmleditformat.md)
* [IIF](/boxlang-language/reference/built-in-functions/system/iif.md)
* [Invoke](/boxlang-language/reference/built-in-functions/system/invoke.md)
* [IsInstanceOf](/boxlang-language/reference/built-in-functions/system/isinstanceof.md)
* [JavaCast](/boxlang-language/reference/built-in-functions/system/javacast.md)
* [ObjectDeserialize](/boxlang-language/reference/built-in-functions/system/objectdeserialize.md)
* [ObjectSerialize](/boxlang-language/reference/built-in-functions/system/objectserialize.md)
* [PagePoolClear](/boxlang-language/reference/built-in-functions/system/pagepoolclear.md)
* [Print](/boxlang-language/reference/built-in-functions/system/print.md)
* [Println](/boxlang-language/reference/built-in-functions/system/println.md)
* [RunThreadInContext](/boxlang-language/reference/built-in-functions/system/runthreadincontext.md)
* [SessionInvalidate](/boxlang-language/reference/built-in-functions/system/sessioninvalidate.md)
* [SessionRotate](/boxlang-language/reference/built-in-functions/system/sessionrotate.md)
* [SessionStartTime](/boxlang-language/reference/built-in-functions/system/sessionstarttime.md)
* [Sleep](/boxlang-language/reference/built-in-functions/system/sleep.md)
* [SystemCacheClear](/boxlang-language/reference/built-in-functions/system/systemcacheclear.md)
* [SystemExecute](/boxlang-language/reference/built-in-functions/system/systemexecute.md)
* [SystemOutput](/boxlang-language/reference/built-in-functions/system/systemoutput.md)
* [Throw](/boxlang-language/reference/built-in-functions/system/throw.md)
* [Trace](/boxlang-language/reference/built-in-functions/system/trace.md)
* [URLDecode](/boxlang-language/reference/built-in-functions/system/urldecode.md)
* [URLEncodedFormat](/boxlang-language/reference/built-in-functions/system/urlencodedformat.md)
* [writeDump](/boxlang-language/reference/built-in-functions/system/writedump.md)
* [WriteLog](/boxlang-language/reference/built-in-functions/system/writelog.md)
* [WriteOutput](/boxlang-language/reference/built-in-functions/system/writeoutput.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/boxast.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
