From eb3048b4f4e10e8d86a8bbbead2b2d04bd0933a8 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 21 Jul 2024 14:45:29 +0200 Subject: [PATCH] Add ident expression --- src/grammar.pest | 18 ++++++++---------- src/test/infix_expressions.rs | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/grammar.pest b/src/grammar.pest index ead9bee..4f75643 100644 --- a/src/grammar.pest +++ b/src/grammar.pest @@ -2,7 +2,7 @@ file = { "hello world" } expr = { infix_expr | term } -term = _{ literal | "(" ~ expr ~ ")" } +term = _{ ident | literal | "(" ~ expr ~ ")" } infix_expr = { #lhs = term ~ operator ~ #rhs = expr @@ -10,20 +10,18 @@ infix_expr = { operator = { "+" | "-" | "*" | "/" | "&&" | "||" } -literal = { string | number | boolean } - -string = { "\"" ~ (!"\"" ~ ANY)* ~ "\"" } +ident = @{ ALPHABETIC ~ (ALPHABETIC | NUMBER | "_")* | quoted_indent } +quoted_indent = _{ "`" ~ (!"`" ~ ANY)+ ~ "`" } -number = _{ byte | float | integer } +literal = { string | number | boolean } +string = { "\"" ~ (!"\"" ~ ANY)* ~ "\"" } +number = _{ byte | float | integer } integer = @{ "-"? ~ ASCII_DIGIT+ } - -float = @{ +float = @{ "-"? ~ (ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT* | ASCII_DIGIT* ~ "." ~ ASCII_DIGIT+) } - -byte = @{ "0x" ~ ASCII_HEX_DIGIT{1, 2} | "0b" ~ ASCII_BIN_DIGIT{0, 8} } - +byte = @{ "0x" ~ ASCII_HEX_DIGIT{1, 2} | "0b" ~ ASCII_BIN_DIGIT{0, 8} } boolean = @{ "true" | "false" } WHITESPACE = _{ " " | "\t" | "\r" | "\n" } diff --git a/src/test/infix_expressions.rs b/src/test/infix_expressions.rs index c96e9dc..853d67c 100644 --- a/src/test/infix_expressions.rs +++ b/src/test/infix_expressions.rs @@ -21,6 +21,21 @@ fn it_parses_addition() { ]); } +#[test] +fn it_parses_addition_with_variables() { + parses_to!(parser: SnekParser, input: "first + second", rule: Rule::expr, tokens: [ + expr(0, 14, [ + infix_expr(0, 14, [ + ident(0, 5), + operator(6, 7), + expr(8, 14, [ + ident(8, 14) + ]) + ]) + ]) + ]); +} + #[test] fn it_parses_subtraction() { parses_to!(parser: SnekParser, input: "1.0 - 1.1", rule: Rule::expr, tokens: [