Skip to main content

Module parser

Module parser 

Source
Expand description

The Swan parser: turns tokens into an ast::Node tree.

Parser is a Pratt (top-down operator precedence) parser. It reads the whole source as one expression and never fails: syntax errors become Node::Unclosed and Node::UnexpectedToken leaves, which evaluate to Undefined values. Comments are skipped.

§Precedence

A higher binding power binds tighter. Prefix operators (+, -, !, $) bind tighter than everything else, including . and application.

PowerOperatorsAssociativity
30.left
28application (f x)left
27^right
26* / %left
25+ -left
24== != < <= > >=left
23& |left
22?left
21;left
15->right
14>> <<left
13=>right
13~>left
12= :left
10,left

§Example

use swan_core::ast::Node;
use swan_core::lexer::Lexer;
use swan_core::parser::Parser;

let ast = Parser::new(Lexer::new("1 + 2 * 3")).parse();
assert!(matches!(ast, Node::SumOp(_)));

Structs§

Parser
A Pratt parser for Swan expressions.