From 354fb8c2b321b13e244b01d03c4c811fb8ee24be Mon Sep 17 00:00:00 2001 From: thehiddenwaffle Date: Mon, 19 May 2025 15:37:43 -0400 Subject: [PATCH] Eliminate whitespace validation via compound-atomic($) and not-atomics(!) in pest --- rfc9535/test_suite/results.csv | 1 + src/parser.rs | 18 ++++++------------ src/parser/grammar/json_path_9535.pest | 4 ++-- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/rfc9535/test_suite/results.csv b/rfc9535/test_suite/results.csv index 661ecd2..7b686e0 100644 --- a/rfc9535/test_suite/results.csv +++ b/rfc9535/test_suite/results.csv @@ -9,3 +9,4 @@ Total; Passed; Failed; Date 687; 652; 0; 2025-03-18 22:56:39 687; 652; 0; 2025-03-18 22:57:01 687; 652; 0; 2025-05-13 21:21:59 +687; 652; 0; 2025-05-19 15:05:31 diff --git a/src/parser.rs b/src/parser.rs index 7f5adbb..6b8fa32 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -27,18 +27,12 @@ pub type Parsed = Result; /// /// Returns a variant of [crate::JsonPathParserError] if the parsing operation failed. pub fn parse_json_path(jp_str: &str) -> Parsed { - if jp_str != jp_str.trim() { - Err(JsonPathError::InvalidJsonPath( - "Leading or trailing whitespaces".to_string(), - )) - } else { - JSPathParser::parse(Rule::main, jp_str) - .map_err(Box::new)? - .next() - .ok_or(JsonPathError::UnexpectedPestOutput) - .and_then(next_down) - .and_then(jp_query) - } + JSPathParser::parse(Rule::main, jp_str) + .map_err(Box::new)? + .next() + .ok_or(JsonPathError::UnexpectedPestOutput) + .and_then(next_down) + .and_then(jp_query) } pub fn jp_query(rule: Pair) -> Parsed { diff --git a/src/parser/grammar/json_path_9535.pest b/src/parser/grammar/json_path_9535.pest index 77dcd0a..380fb59 100644 --- a/src/parser/grammar/json_path_9535.pest +++ b/src/parser/grammar/json_path_9535.pest @@ -1,6 +1,6 @@ -main = { SOI ~ jp_query ~ EOI } +main = ${ SOI ~ jp_query ~ EOI } jp_query = {root ~ segments} -segments = {(S ~ segment)*} +segments = !{(S ~ segment)*} segment = { child_segment | descendant_segment } child_segment = { bracketed_selection | ("." ~ (wildcard_selector | member_name_shorthand)) } bracketed_selection = { "[" ~ S ~ selector ~ (S ~ "," ~ S ~ selector)* ~ S ~ "]" }