diff --git a/src/grammar.pest b/src/grammar.pest index 6f428fc..21ceb0f 100644 --- a/src/grammar.pest +++ b/src/grammar.pest @@ -1,10 +1,11 @@ file = { "hello world" } -expr = { infix_expr | term } +statement = { decl } +decl = { "let" ~ #name = ident ~ #args = (ident)* ~ "=" ~ expr } -term = _{ ident | literal | "(" ~ expr ~ ")" } - -infix_expr = { +expr = { infix_expr | term } +term = _{ ident | literal | "(" ~ expr ~ ")" } +infix_expr = { #lhs = term ~ operator ~ #rhs = expr } diff --git a/src/test/mod.rs b/src/test/mod.rs index 8c1e65b..2d409a1 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -1,5 +1,6 @@ mod infix_expressions; mod literals; +mod statements; use crate::parse; diff --git a/src/test/statements.rs b/src/test/statements.rs new file mode 100644 index 0000000..6b9dde1 --- /dev/null +++ b/src/test/statements.rs @@ -0,0 +1,19 @@ +use pest::{consumes_to, parses_to}; + +use crate::{Rule, SnekParser}; + +#[test] +fn it_parses_assignments() { + parses_to!(parser: SnekParser, input: "let a = 1", rule: Rule::statement, tokens: [ + statement(0, 9, [ + decl(0, 9, [ + ident(4, 5), + expr(8, 9, [ + literal(8, 9, [ + integer(8, 9) + ]), + ]) + ]) + ]) + ]); +}