AST Overview
Explore the Tiramisu AST node types, structure, and how to work with parsed documents.
The Tiramisu parser (@timeleap/tiramisu) takes source text and produces an Abstract Syntax Tree (AST). The AST is a tree of typed nodes that represents the structure of your document.
Node Types
The AST consists of the following node types:
| Node | Description |
|---|---|
Tiramisu | Root node of the document. Contains all top-level nodes. |
Paragraph | A paragraph of text. Created by blank line separation. |
MixedText | Text that contains both plain text and function calls. |
PureText | A plain text segment with no function calls. |
FunctionCall | A function call with a name and parameters. |
Parameters | The parameter list of a function call. |
Parameter | A single positional parameter. |
NamedParameter | A named parameter (key = value). |
ArrayValue | An array value ([a, b, c]). |
ArrayItem | A single item within an array. |
Example
Given this Tiramisu source:
bold { Hello } world The parser produces this AST:
Tiramisu
└── Paragraph
└── MixedText
├── FunctionCall (name: "bold")
│ └── Parameters
│ └── Parameter
│ └── PureText ("Hello")
└── PureText (" world") FunctionCall Structure
The FunctionCall node is the core of Tiramisu. It has:
functionName— the name of the function (text before the{)parameters— aParametersnode containing the function's arguments
Parameters contain a list of Parameter and NamedParameter nodes. Each parameter can contain text, nested function calls, or arrays.
Importing Node Types
All node types are available from the parser package:
import {
Tiramisu,
Paragraph,
MixedText,
PureText,
FunctionCall,
Parameters,
Parameter,
NamedParameter,
ArrayValue,
ArrayItem,
} from "@timeleap/tiramisu/src/types/nodes"; Node type checking uses instanceof:
if (node instanceof FunctionCall) {
console.log(node.functionName);
}