Skip to content

Commit

Permalink
Allow all specific characters based on go parser and some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBger committed May 17, 2024
1 parent fc876b2 commit a078d3b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
34 changes: 18 additions & 16 deletions substreams/src/expr_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fn parsing(input: &str) -> Result<Pair<Rule>, Error> {
let pairs = EParser::parse(Rule::expression, input)
.context("parsing input based on the expression rule")?;

println!("{:?}", pairs.clone().collect::<Vec<_>>());
match pairs.into_iter().next() {
Some(pair) => Ok(pair),
None => Err(anyhow::Error::msg("no pairs found in input"))
Expand Down Expand Up @@ -118,16 +119,28 @@ fn expression_to_string(parsing: Pair<Rule>) -> String {
mod tests {
use rstest::rstest;
use super::*;
static TEST_KEYS: &[&str] = &["test", "test1", "test2", "test3", "test4", "test5", "test 6"];
static TEST_KEYS: &[&str] = &["test", "test1", "test2", "test3", "test4", "test5", "test 6", "test.7", "test:8", "test_9", "test*19z_|"];

#[rstest]
#[case(TEST_KEYS, "test", true)]
#[case(TEST_KEYS, "'test'", true)]
#[case(TEST_KEYS, "'test 6' || test7", true)]
#[case(TEST_KEYS, "'test_6' && test3", false)]
#[case(TEST_KEYS, "\"test 6\" || test7", true)]
#[case(TEST_KEYS, "\"test 6\"|| test7", true)]
#[case(TEST_KEYS, "\"test 6\" && test3", true)]

#[case(TEST_KEYS, "test.7", true)]
#[case(TEST_KEYS, "test.8", false)]
#[case(TEST_KEYS, "test:8", true)]
#[case(TEST_KEYS, "test*19z_|", true)]
#[case(TEST_KEYS, "test:9", false)]
#[case(TEST_KEYS, "test_9", true)]
#[case(TEST_KEYS, "test_10", false)]
#[case(TEST_KEYS, "test10 ||test.7", true)]
#[case(TEST_KEYS, "test10 && test:8", false)]
#[case(TEST_KEYS, "(test10 && test_9) || (test.7 && test:8)", true)]
#[case(TEST_KEYS, "(test10 && test_9) || (test.7 && test*19z_|)", true)]

#[case(TEST_KEYS, "test1 || test", true)]
#[case(TEST_KEYS, "test1 || test6", true)]
#[case(TEST_KEYS, "test6 || test7", false)]
Expand All @@ -141,8 +154,8 @@ mod tests {
#[case(TEST_KEYS, "test6 && test7", false)]

#[case(TEST_KEYS, "test1 && test && test2", true)]
#[case(TEST_KEYS, "test1 && test2 && test7", false)]
#[case(TEST_KEYS, "test6 && test7 && test8", false)]
#[case(TEST_KEYS, "test1&& test2 &&test7", false)]
#[case(TEST_KEYS, "test6 &&test7 && test8", false)]

#[case(TEST_KEYS, "test1 test", true)]
#[case(TEST_KEYS, "test1 test6", false)]
Expand All @@ -167,16 +180,5 @@ mod tests {
let result = matches_keys_in_parsed_expr(keys, input).expect("matching keys in parsed expression");

assert_eq!(result, expected, "This expression ast is {expr_as_string}");
}

#[rstest]
#[case(TEST_KEYS, "test1 *213 ", "parsing expression")]
#[case(TEST_KEYS, "|213 test", "parsing expression")]
#[case(TEST_KEYS, "", "parsing expression")]

fn test_matches_keys_in_parsed_expr_error(#[case] keys: &[&str], #[case] input: &str, #[case] expected_error: &str) {
let result = matches_keys_in_parsed_expr(keys, input).expect_err("parsing is not failing");
assert_eq!(result.to_string(), expected_error);
}

}
}
6 changes: 2 additions & 4 deletions substreams/src/expr_parser_rule.pest
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ expression = { or ~ space? ~ EOI }
or = { (and ~ (space? ~ ("||") ~ space? ~ and )* ) }
and = { (value ~ ((space ~ value) | (space? ~ ("&&") ~ space? ~ value ~ space?))* ) }
value = { (space? ~ singleQuoteKeyTerm ~ space?) | (space? ~ doubleQuoteKeyTerm ~ space?) | keyterm | (space? ~ "(" ~ space? ~ or ~ space? ~ ")" ~ space?) }
keyterm = { (letters | digits | symbols)+ }
keyterm = { (ctr_accepted)+ }
singleQuoteKeyTerm = { ("'") ~ (!"'" ~ ANY)+ ~ ("'")}
doubleQuoteKeyTerm = { ("\"") ~ (!"\"" ~ ANY)+ ~ ("\"")}

digits = _{ '0' .. '9' }
letters = _{ 'a' .. 'z' | 'A' .. 'Z' }
symbols = _{ "_" | ":" | "." }
ctr_accepted = _{(!(space | "'" | "\"" | "(" | ")" | "-" | ("||") | ("&&")) ~ ANY)}
space = _{ (" " | "\t" | "\n" )+ }

0 comments on commit a078d3b

Please sign in to comment.