From e07470c32601fda181c8c655e2f5599a1bbcbf5c Mon Sep 17 00:00:00 2001 From: Jean-Christophe Amiel Date: Sun, 30 Jun 2024 15:37:37 +0200 Subject: [PATCH] Use combinator in jsonpath parser. --- .../hurl/src/jsonpath/parser/combinators.rs | 68 ------------------- packages/hurl/src/jsonpath/parser/error.rs | 22 ++++++ packages/hurl/src/jsonpath/parser/mod.rs | 8 --- packages/hurl/src/jsonpath/parser/parse.rs | 12 ++-- .../hurl/src/jsonpath/parser/primitives.rs | 5 +- 5 files changed, 31 insertions(+), 84 deletions(-) delete mode 100644 packages/hurl/src/jsonpath/parser/combinators.rs diff --git a/packages/hurl/src/jsonpath/parser/combinators.rs b/packages/hurl/src/jsonpath/parser/combinators.rs deleted file mode 100644 index 033813794..000000000 --- a/packages/hurl/src/jsonpath/parser/combinators.rs +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Hurl (https://hurl.dev) - * Copyright (C) 2024 Orange - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -use super::error::*; -use super::{ParseFunc, ParseResult}; -use hurl_core::reader::Reader; - -pub fn zero_or_more(f: ParseFunc, p: &mut Reader) -> ParseResult> { - let _start = p.state; - - let mut v: Vec = Vec::new(); - loop { - let initial_state = p.state; - if p.is_eof() { - return Ok(v); - } - - match f(p) { - Ok(r) => { - v.push(r); - } - Err(e) => { - return if e.recoverable { - p.state.pos = initial_state.pos; - p.state.cursor = initial_state.cursor; - Ok(v) - } else { - Err(e) - }; - } - } - } -} - -/// Tries to apply the list of parser functions `fs` until one of them succeeds. -/// Typically this should be recoverable -pub fn choice(fs: &[ParseFunc], reader: &mut Reader) -> ParseResult { - for (pos, f) in fs.iter().enumerate() { - let start = reader.state; - if pos == fs.len() - 1 { - return f(reader); - } - match f(reader) { - Err(ParseError { - recoverable: true, .. - }) => { - reader.state = start; - continue; - } - x => return x, - } - } - panic!("You can't call choice with an empty vector of choice") -} diff --git a/packages/hurl/src/jsonpath/parser/error.rs b/packages/hurl/src/jsonpath/parser/error.rs index 63bbe8126..e10bf6278 100644 --- a/packages/hurl/src/jsonpath/parser/error.rs +++ b/packages/hurl/src/jsonpath/parser/error.rs @@ -18,6 +18,8 @@ use hurl_core::reader::Pos; +pub type ParseResult = Result; + #[derive(Clone, Debug, PartialEq, Eq)] pub struct ParseError { pub pos: Pos, @@ -39,3 +41,23 @@ impl ParseError { pub enum ParseErrorKind { Expecting(String), } + +impl hurl_core::combinator::ParseError for ParseError { + fn is_recoverable(&self) -> bool { + self.recoverable + } + + fn to_recoverable(self) -> Self { + ParseError { + recoverable: true, + ..self + } + } + + fn to_non_recoverable(self) -> Self { + ParseError { + recoverable: false, + ..self + } + } +} diff --git a/packages/hurl/src/jsonpath/parser/mod.rs b/packages/hurl/src/jsonpath/parser/mod.rs index b6e89532e..e3b873477 100644 --- a/packages/hurl/src/jsonpath/parser/mod.rs +++ b/packages/hurl/src/jsonpath/parser/mod.rs @@ -15,16 +15,8 @@ * limitations under the License. * */ - -use error::ParseError; -use hurl_core::reader::Reader; - -pub type ParseResult = Result; -pub type ParseFunc = fn(&mut Reader) -> ParseResult; - pub use self::parse::parse; -mod combinators; mod error; mod parse; mod primitives; diff --git a/packages/hurl/src/jsonpath/parser/parse.rs b/packages/hurl/src/jsonpath/parser/parse.rs index c05dda039..9c07324cc 100644 --- a/packages/hurl/src/jsonpath/parser/parse.rs +++ b/packages/hurl/src/jsonpath/parser/parse.rs @@ -15,11 +15,12 @@ * limitations under the License. * */ -use super::super::ast::*; -use super::combinators::*; -use super::error::{ParseError, ParseErrorKind}; -use super::primitives::*; -use super::ParseResult; +use crate::jsonpath::ast::{Predicate, PredicateFunc, Query, Selector, Slice}; +use crate::jsonpath::parser::error::{ParseError, ParseErrorKind, ParseResult}; +use crate::jsonpath::parser::primitives::{ + integer, key_name, key_path, literal, natural, number, string_value, try_literal, whitespace, +}; +use hurl_core::combinator::{choice, zero_or_more}; use hurl_core::reader::Reader; pub fn parse(s: &str) -> Result { @@ -277,6 +278,7 @@ fn equal_string_predicate_func(reader: &mut Reader) -> ParseResult ParseResult {