Fix key-string parser

This commit is contained in:
Fabrice Reix 2022-12-10 17:58:50 +01:00
parent 6b7753e174
commit bb7cf25a57
No known key found for this signature in database
GPG Key ID: BF5213154B2E7155
2 changed files with 29 additions and 7 deletions

View File

@ -319,7 +319,7 @@ key-string: (key-string-content | template)+
key-string-content: (key-string-text | key-string-escaped-char)*
key-string-text: ~[#: \n\r\t\\]+
key-string-text: (alphanum | "_" | "-" | "." | "[" | "]" | "@" | "$") +
key-string-escaped-char: "\\" ("#" | ":" | "\\" | "\b" | "\f" | "\n" | "\r" | "\t" | "\u" unicode-char )
@ -418,7 +418,9 @@ filter:
| count-filter
| url-encode-filter
| url-decode-filter
| to-int
| html-encode-filter
| html-decode-filter
| to-int-filter
regex-filter: "regex" sp (quoted-string | regex)
@ -432,7 +434,7 @@ html-encode-filter: "htmlEscape"
html-decode-filter: "htmlUnescape"
to-int: "toInt"
to-int-filter: "toInt"
# Lexical Grammar
@ -440,6 +442,8 @@ boolean: "true" | "false"
null: "null"
alphanum: [A-Za-z0-9]
integer: digit+
float: integer fraction

View File

@ -96,12 +96,20 @@ pub fn unquoted_string_key(reader: &mut Reader) -> ParseResult<'static, EncodedS
match reader.read() {
None => break,
Some(c) => {
if ['#', ':', ' ', '\n', '\r', '\n', '\t', '\\'].contains(&c) {
reader.state = save;
break;
} else {
if c.is_alphanumeric()
|| c == '_'
|| c == '-'
|| c == '.'
|| c == '['
|| c == ']'
|| c == '@'
|| c == '$'
{
value.push(c);
encoded.push_str(reader.from(save.cursor).as_str())
} else {
reader.state = save;
break;
}
}
}
@ -554,6 +562,16 @@ mod tests {
let error = unquoted_string_key(&mut reader).err().unwrap();
assert_eq!(error.pos, Pos { line: 1, column: 2 });
assert_eq!(error.inner, ParseError::EscapeChar {});
let mut reader = Reader::init(r#"{"id":1}"#);
let error = unquoted_string_key(&mut reader).err().unwrap();
assert_eq!(error.pos, Pos { line: 1, column: 1 });
assert_eq!(
error.inner,
ParseError::Expecting {
value: "key string".to_string()
}
);
}
#[test]