Expand description
Converts a sequence of tokens into an AST (abstract syntax tree).
The AST defines how tokens are interpreted and how they relate to others.
For example consider the following tokens: 1 + 2 * 3
.
The parser will use its hardcoded rules of order of operations to create
the following tree:
+
/
1 *
/
2 3
The same principles apply to more complicated examples as well.
Keep in mind that the AST isn’t necessarily a binary tree!
§Node
The nodes of the AST are defined using the Node struct. There are three types of nodes:
- Source - The source node holds a string slice to the source text (the text that was used to create the tokens).
- Expression - The expression node holds an expression - a sequence of operations that return a Value.
- Control Flow - Nodes that decide which nodes are executed and which are not.
§Example
use mini_builder_rs::{
tokenizer::{Tokenizer, TokenizerOptions},
parser::Parser,
};
let source = "is 1 bigger than 2? {{ 1 > 2 ? 'yes' : 'no' }}";
let tokens = Tokenizer::new(source, TokenizerOptions::default())
.tokenize()
.unwrap();
let tokens = tokens.as_slice();
let nodes = Parser::new(tokens).parse().unwrap();
// nodes is the root of the parsed AST
Modules§
Structs§
- Parser
- Creates an AST from a sequence of tokens