Skip to content

Commit

Permalink
Merge pull request #382 from sunng87/fix/null-grammar
Browse files Browse the repository at this point in the history
Parser issue for references starts with null/true/false
  • Loading branch information
sunng87 committed Sep 21, 2020
2 parents 25b450f + 567eea6 commit e9d28f6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
13 changes: 6 additions & 7 deletions src/grammar.pest
@@ -1,5 +1,5 @@
WHITESPACE = _{ " "|"\t"|"\n"|"\r" }
keywords = @{ "as" | "else" }
keywords = { "as" | "else" }

escape = @{ ("\\" ~ "{{" ~ "{{"?) | ("\\" ~ "\\"+ ~ &"{{") }
raw_text = ${ ( escape | (!"{{" ~ ANY) )+ }
Expand All @@ -12,17 +12,16 @@ literal = { string_literal |
null_literal |
boolean_literal }

null_literal = { "null" }
boolean_literal = { "true"|"false" }
number_literal = @{ "-"? ~ ASCII_DIGIT+ ~ "."? ~ ASCII_DIGIT*
~ ("E" ~ "-"? ~ ASCII_DIGIT+)? }
null_literal = @{ "null" ~ !symbol_char }
boolean_literal = @{ ("true"|"false") ~ !symbol_char }
number_literal = @{ "-"? ~ ASCII_DIGIT+ ~ "."? ~ ASCII_DIGIT* ~ ("E" ~ "-"? ~ ASCII_DIGIT+)? }
string_literal = @{ ("\"" ~ (!"\"" ~ ("\\\"" | ANY))* ~ "\"") | ("'" ~ (!"'" ~ ("\\'" | ANY))* ~ "'") }
array_literal = { "[" ~ literal? ~ ("," ~ literal)* ~ "]" }
object_literal = { "{" ~ (string_literal ~ ":" ~ literal)?
~ ("," ~ string_literal ~ ":" ~ literal)* ~ "}" }

symbol_char = _{'a'..'z'|'A'..'Z'|ASCII_DIGIT|"-"|"_"|"$"|'\u{80}'..'\u{7ff}'|'\u{800}'..'\u{ffff}'|'\u{10000}'..'\u{10ffff}'}
partial_symbol_char = _{'a'..'z'|'A'..'Z'|ASCII_DIGIT|"-"|"_"|'\u{80}'..'\u{7ff}'|'\u{800}'..'\u{ffff}'|'\u{10000}'..'\u{10ffff}'|"/"|"."}
symbol_char = _{ASCII_ALPHANUMERIC|"-"|"_"|"$"|'\u{80}'..'\u{7ff}'|'\u{800}'..'\u{ffff}'|'\u{10000}'..'\u{10ffff}'}
partial_symbol_char = _{ASCII_ALPHANUMERIC|"-"|"_"|'\u{80}'..'\u{7ff}'|'\u{800}'..'\u{ffff}'|'\u{10000}'..'\u{10ffff}'|"/"|"."}
path_char = _{ "/" }

identifier = @{ symbol_char+ }
Expand Down
5 changes: 4 additions & 1 deletion src/grammar.rs
Expand Up @@ -84,6 +84,7 @@ mod test {
"this.[$id]",
"[$id]",
"$id",
"this.[null]",
];
for i in s.iter() {
assert_rule!(Rule::reference, i);
Expand All @@ -100,7 +101,7 @@ mod test {

#[test]
fn test_param() {
let s = vec!["hello", "\"json literal\""];
let s = vec!["hello", "\"json literal\"", "nullable", "truestory"];
for i in s.iter() {
assert_rule!(Rule::param, i);
}
Expand Down Expand Up @@ -132,6 +133,7 @@ mod test {
"{\"hello\": \"world\"}",
"{}",
"{\"a\":1, \"b\":2 }",
"\"nullable\"",
];
for i in s.iter() {
assert_rule!(Rule::literal, i);
Expand Down Expand Up @@ -289,6 +291,7 @@ mod test {
"./[/foo]",
"[foo]",
"@root/a/b",
"nullable",
];
for i in s.iter() {
assert_rule_match!(Rule::path, i);
Expand Down
61 changes: 61 additions & 0 deletions src/registry.rs
Expand Up @@ -895,4 +895,65 @@ mod test {
.unwrap()
);
}

#[test]
fn test_keys_starts_with_null() {
env_logger::init();
let reg = Registry::new();
let data = json!({
"optional": true,
"is_null": true,
"nullable": true,
"null": true,
"falsevalue": true,
});
assert_eq!(
"optional: true --> true",
reg.render_template(
"optional: {{optional}} --> {{#if optional }}true{{else}}false{{/if}}",
&data
)
.unwrap()
);
assert_eq!(
"is_null: true --> true",
reg.render_template(
"is_null: {{is_null}} --> {{#if is_null }}true{{else}}false{{/if}}",
&data
)
.unwrap()
);
assert_eq!(
"nullable: true --> true",
reg.render_template(
"nullable: {{nullable}} --> {{#if nullable }}true{{else}}false{{/if}}",
&data
)
.unwrap()
);
assert_eq!(
"falsevalue: true --> true",
reg.render_template(
"falsevalue: {{falsevalue}} --> {{#if falsevalue }}true{{else}}false{{/if}}",
&data
)
.unwrap()
);
assert_eq!(
"null: true --> false",
reg.render_template(
"null: {{null}} --> {{#if null }}true{{else}}false{{/if}}",
&data
)
.unwrap()
);
assert_eq!(
"null: true --> true",
reg.render_template(
"null: {{null}} --> {{#if this.[null]}}true{{else}}false{{/if}}",
&data
)
.unwrap()
);
}
}

0 comments on commit e9d28f6

Please sign in to comment.