From b5fa1c0efefdbd79b0a0293e98f0576772bc02e3 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 21 Jul 2024 15:01:15 +0200 Subject: [PATCH] Add declarations --- src/grammar.pest | 9 +++++---- src/test/mod.rs | 1 + src/test/statements.rs | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 src/test/statements.rs 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) + ]), + ]) + ]) + ]) + ]); +}