Rename jsonpath::parser::error::Error to jsonpath::parser::error::ParseError

This commit is contained in:
Jean-Christophe Amiel 2024-06-28 13:40:24 +02:00
parent da4792b258
commit d026280b97
No known key found for this signature in database
GPG Key ID: 07FF11CFD55356CC
5 changed files with 107 additions and 250 deletions

View File

@ -55,7 +55,7 @@ pub fn choice<T>(fs: &[ParseFunc<T>], reader: &mut Reader) -> ParseResult<T> {
return f(reader);
}
match f(reader) {
Err(Error {
Err(ParseError {
recoverable: true, ..
}) => {
reader.state = start;

View File

@ -18,13 +18,23 @@
use super::Pos;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Error {
pub struct ParseError {
pub pos: Pos,
pub recoverable: bool,
pub inner: ParseError,
pub kind: ParseErrorKind,
}
impl ParseError {
pub fn new(pos: Pos, recoverable: bool, kind: ParseErrorKind) -> Self {
ParseError {
pos,
recoverable,
kind,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ParseError {
Expecting { value: String },
pub enum ParseErrorKind {
Expecting(String),
}

View File

@ -16,7 +16,7 @@
*
*/
use error::Error;
use error::ParseError;
use reader::Reader;
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
@ -25,7 +25,7 @@ pub struct Pos {
pub column: usize,
}
pub type ParseResult<T> = Result<T, Error>;
pub type ParseResult<T> = Result<T, ParseError>;
pub type ParseFunc<T> = fn(&mut Reader) -> ParseResult<T>;
pub use self::parse::parse;

View File

@ -17,12 +17,12 @@
*/
use super::super::ast::*;
use super::combinators::*;
use super::error::{Error, ParseError};
use super::error::{ParseError, ParseErrorKind};
use super::primitives::*;
use super::reader::Reader;
use super::ParseResult;
pub fn parse(s: &str) -> Result<Query, Error> {
pub fn parse(s: &str) -> Result<Query, ParseError> {
let mut reader = Reader::new(s);
query(&mut reader)
}
@ -32,13 +32,9 @@ fn query(reader: &mut Reader) -> ParseResult<Query> {
let selectors = zero_or_more(selector, reader)?;
if !reader.is_eof() {
return Err(Error {
pos: reader.state.pos,
recoverable: false,
inner: ParseError::Expecting {
value: "eof".to_string(),
},
});
let kind = ParseErrorKind::Expecting("eof".to_string());
let error = ParseError::new(reader.state.pos, false, kind);
return Err(error);
}
Ok(Query { selectors })
}
@ -60,17 +56,14 @@ fn selector(reader: &mut Reader) -> ParseResult<Selector> {
)
}
fn selector_array_index_or_array_indices(reader: &mut Reader) -> Result<Selector, Error> {
fn selector_array_index_or_array_indices(reader: &mut Reader) -> Result<Selector, ParseError> {
let initial_state = reader.state;
try_left_bracket(reader)?;
let mut indexes = vec![];
let i = match natural(reader) {
Err(e) => {
return Err(Error {
pos: e.pos,
recoverable: true,
inner: e.inner,
})
let error = ParseError::new(e.pos, true, e.kind);
return Err(error);
}
Ok(v) => v,
};
@ -80,11 +73,7 @@ fn selector_array_index_or_array_indices(reader: &mut Reader) -> Result<Selector
if try_literal(",", reader).is_ok() {
let i = match natural(reader) {
Err(e) => {
return Err(Error {
pos: e.pos,
recoverable: true,
inner: e.inner,
})
return Err(ParseError::new(e.pos, true, e.kind));
}
Ok(v) => v,
};
@ -98,11 +87,7 @@ fn selector_array_index_or_array_indices(reader: &mut Reader) -> Result<Selector
// TODO: combine array index, indices and slice in the same function
if let Err(e) = try_literal("]", reader) {
reader.state = initial_state;
return Err(Error {
pos: reader.state.pos,
recoverable: true,
inner: e.inner,
});
return Err(ParseError::new(reader.state.pos, true, e.kind));
}
let selector = if indexes.len() == 1 {
Selector::ArrayIndex(*indexes.first().unwrap())
@ -112,14 +97,14 @@ fn selector_array_index_or_array_indices(reader: &mut Reader) -> Result<Selector
Ok(selector)
}
fn selector_array_wildcard(reader: &mut Reader) -> Result<Selector, Error> {
fn selector_array_wildcard(reader: &mut Reader) -> Result<Selector, ParseError> {
try_left_bracket(reader)?;
try_literal("*", reader)?;
literal("]", reader)?;
Ok(Selector::ArrayWildcard)
}
fn selector_array_slice(reader: &mut Reader) -> Result<Selector, Error> {
fn selector_array_slice(reader: &mut Reader) -> Result<Selector, ParseError> {
try_left_bracket(reader)?;
let state = reader.state;
let start = match integer(reader) {
@ -130,13 +115,9 @@ fn selector_array_slice(reader: &mut Reader) -> Result<Selector, Error> {
Ok(v) => Some(v),
};
if try_literal(":", reader).is_err() {
return Err(Error {
pos: state.pos,
recoverable: true,
inner: ParseError::Expecting {
value: ":".to_string(),
},
});
let kind = ParseErrorKind::Expecting(":".to_string());
let error = ParseError::new(state.pos, true, kind);
return Err(error);
};
let state = reader.state;
let end = match integer(reader) {
@ -150,7 +131,7 @@ fn selector_array_slice(reader: &mut Reader) -> Result<Selector, Error> {
Ok(Selector::ArraySlice(Slice { start, end }))
}
fn selector_filter(reader: &mut Reader) -> Result<Selector, Error> {
fn selector_filter(reader: &mut Reader) -> Result<Selector, ParseError> {
try_left_bracket(reader)?;
try_literal("?(", reader)?;
let pred = predicate(reader)?;
@ -158,16 +139,14 @@ fn selector_filter(reader: &mut Reader) -> Result<Selector, Error> {
Ok(Selector::Filter(pred))
}
fn selector_object_key_bracket(reader: &mut Reader) -> Result<Selector, Error> {
fn selector_object_key_bracket(reader: &mut Reader) -> Result<Selector, ParseError> {
try_left_bracket(reader)?;
match string_value(reader) {
Err(_) => Err(Error {
pos: reader.state.pos,
recoverable: true,
inner: ParseError::Expecting {
value: "value string".to_string(),
},
}),
Err(_) => {
let kind = ParseErrorKind::Expecting("value string".to_string());
let error = ParseError::new(reader.state.pos, true, kind);
Err(error)
}
Ok(v) => {
literal("]", reader)?;
Ok(Selector::NameChild(v))
@ -175,48 +154,40 @@ fn selector_object_key_bracket(reader: &mut Reader) -> Result<Selector, Error> {
}
}
fn selector_object_key(reader: &mut Reader) -> Result<Selector, Error> {
fn selector_object_key(reader: &mut Reader) -> Result<Selector, ParseError> {
if reader.peek() != Some('.') {
return Err(Error {
pos: reader.state.pos,
recoverable: true,
inner: ParseError::Expecting {
value: "[ or .".to_string(),
},
});
let kind = ParseErrorKind::Expecting("[ or .".to_string());
let error = ParseError::new(reader.state.pos, true, kind);
return Err(error);
};
_ = reader.read();
let s = reader.read_while(|c| c.is_alphanumeric() || *c == '_' || *c == '-');
if s.is_empty() {
return Err(Error {
pos: reader.state.pos,
recoverable: false,
inner: ParseError::Expecting {
value: "empty value".to_string(),
},
});
let kind = ParseErrorKind::Expecting("empty value".to_string());
let error = ParseError::new(reader.state.pos, false, kind);
return Err(error);
}
Ok(Selector::NameChild(s))
}
fn selector_wildcard(reader: &mut Reader) -> Result<Selector, Error> {
fn selector_wildcard(reader: &mut Reader) -> Result<Selector, ParseError> {
try_literal(".*", reader)?;
Ok(Selector::Wildcard)
}
fn selector_recursive_wildcard(reader: &mut Reader) -> Result<Selector, Error> {
fn selector_recursive_wildcard(reader: &mut Reader) -> Result<Selector, ParseError> {
try_literal("..*", reader)?;
Ok(Selector::RecursiveWildcard)
}
fn selector_recursive_key(reader: &mut Reader) -> Result<Selector, Error> {
fn selector_recursive_key(reader: &mut Reader) -> Result<Selector, ParseError> {
try_literal("..", reader)?;
let k = key_name(reader)?;
Ok(Selector::RecursiveKey(k))
}
fn try_left_bracket(reader: &mut Reader) -> Result<(), Error> {
fn try_left_bracket(reader: &mut Reader) -> Result<(), ParseError> {
let start = reader.state;
if literal(".[", reader).is_err() {
reader.state = start;

View File

@ -16,30 +16,22 @@
*
*/
use super::super::ast::*;
use super::error::{Error, ParseError};
use super::error::{ParseError, ParseErrorKind};
use super::{ParseResult, Reader};
pub fn natural(reader: &mut Reader) -> ParseResult<usize> {
let start = reader.state;
if reader.is_eof() {
return Err(Error {
pos: start.pos,
recoverable: true,
inner: ParseError::Expecting {
value: String::from("natural"),
},
});
let kind = ParseErrorKind::Expecting("natural".to_string());
let error = ParseError::new(start.pos, true, kind);
return Err(error);
}
let first_digit = reader.read().unwrap();
if !first_digit.is_ascii_digit() {
return Err(Error {
pos: start.pos,
recoverable: true,
inner: ParseError::Expecting {
value: String::from("natural"),
},
});
let kind = ParseErrorKind::Expecting("natural".to_string());
let error = ParseError::new(start.pos, true, kind);
return Err(error);
}
let save = reader.state;
@ -47,13 +39,9 @@ pub fn natural(reader: &mut Reader) -> ParseResult<usize> {
// if the first digit is zero, you should not have any more digits
if first_digit == '0' && !s.is_empty() {
return Err(Error {
pos: save.pos,
recoverable: false,
inner: ParseError::Expecting {
value: String::from("natural"),
},
});
let kind = ParseErrorKind::Expecting("natural".to_string());
let error = ParseError::new(save.pos, false, kind);
return Err(error);
}
Ok(format!("{first_digit}{s}").parse().unwrap())
}
@ -75,24 +63,16 @@ pub fn number(reader: &mut Reader) -> ParseResult<Number> {
let decimal = if reader.peek() == Some('.') {
_ = reader.read();
if reader.is_eof() {
return Err(Error {
pos: reader.state.pos,
recoverable: false,
inner: ParseError::Expecting {
value: String::from("natural"),
},
});
let kind = ParseErrorKind::Expecting("natural".to_string());
let error = ParseError::new(reader.state.pos, false, kind);
return Err(error);
}
let s = reader.read_while(|c| c.is_ascii_digit());
if s.is_empty() {
return Err(Error {
pos: reader.state.pos,
recoverable: false,
inner: ParseError::Expecting {
value: String::from("natural"),
},
});
let kind = ParseErrorKind::Expecting("natural".to_string());
let error = ParseError::new(reader.state.pos, false, kind);
return Err(error);
}
format!("{s:0<18}").parse().unwrap()
} else {
@ -102,19 +82,15 @@ pub fn number(reader: &mut Reader) -> ParseResult<Number> {
Ok(Number { int, decimal })
}
pub fn string_value(reader: &mut Reader) -> Result<String, Error> {
pub fn string_value(reader: &mut Reader) -> Result<String, ParseError> {
try_literal("'", reader)?;
let mut s = String::new();
loop {
match reader.read() {
None => {
return Err(Error {
pos: reader.state.pos,
recoverable: false,
inner: ParseError::Expecting {
value: String::from("'"),
},
})
let kind = ParseErrorKind::Expecting("'".to_string());
let error = ParseError::new(reader.state.pos, false, kind);
return Err(error);
}
Some('\'') => break,
Some('\\') => {
@ -124,13 +100,9 @@ pub fn string_value(reader: &mut Reader) -> Result<String, Error> {
s.push('\'');
}
_ => {
return Err(Error {
pos: reader.state.pos,
recoverable: false,
inner: ParseError::Expecting {
value: String::from("'"),
},
})
let kind = ParseErrorKind::Expecting("'".to_string());
let error = ParseError::new(reader.state.pos, false, kind);
return Err(error);
}
}
}
@ -143,7 +115,7 @@ pub fn string_value(reader: &mut Reader) -> Result<String, Error> {
Ok(s)
}
pub fn key_name(reader: &mut Reader) -> Result<String, Error> {
pub fn key_name(reader: &mut Reader) -> Result<String, ParseError> {
// test python or javascript
// subset that can used for dot notation
// The key must not be empty and must not start with a digit
@ -153,23 +125,15 @@ pub fn key_name(reader: &mut Reader) -> Result<String, Error> {
if c.is_alphabetic() || c == '_' {
c
} else {
return Err(Error {
pos: reader.state.pos,
recoverable: false,
inner: ParseError::Expecting {
value: "key".to_string(),
},
});
let kind = ParseErrorKind::Expecting("key".to_string());
let error = ParseError::new(reader.state.pos, false, kind);
return Err(error);
}
}
None => {
return Err(Error {
pos: reader.state.pos,
recoverable: false,
inner: ParseError::Expecting {
value: "key".to_string(),
},
})
let kind = ParseErrorKind::Expecting("key".to_string());
let error = ParseError::new(reader.state.pos, false, kind);
return Err(error);
}
};
let s = reader.read_while(|c| c.is_alphanumeric() || *c == '_');
@ -178,7 +142,7 @@ pub fn key_name(reader: &mut Reader) -> Result<String, Error> {
}
// key1.key2.key3
pub fn key_path(reader: &mut Reader) -> Result<Vec<String>, Error> {
pub fn key_path(reader: &mut Reader) -> Result<Vec<String>, ParseError> {
let root = key_name(reader)?;
let mut path = vec![root];
while let Some('.') = reader.peek() {
@ -195,35 +159,22 @@ pub fn literal(s: &str, reader: &mut Reader) -> ParseResult<()> {
// => use combinator recover to make it recoverable
let start = reader.state;
if reader.is_eof() {
return Err(Error {
pos: start.pos,
recoverable: false,
inner: ParseError::Expecting {
value: s.to_string(),
},
});
let kind = ParseErrorKind::Expecting(s.to_string());
let error = ParseError::new(start.pos, false, kind);
return Err(error);
}
for c in s.chars() {
let _state = reader.state;
match reader.read() {
None => {
return Err(Error {
pos: start.pos,
recoverable: false,
inner: ParseError::Expecting {
value: s.to_string(),
},
});
let kind = ParseErrorKind::Expecting(s.to_string());
let error = ParseError::new(start.pos, false, kind);
return Err(error);
}
Some(x) => {
if x != c {
return Err(Error {
pos: start.pos,
recoverable: false,
inner: ParseError::Expecting {
value: s.to_string(),
},
});
let kind = ParseErrorKind::Expecting(s.to_string());
let error = ParseError::new(start.pos, false, kind);
return Err(error);
} else {
continue;
}
@ -237,10 +188,10 @@ pub fn literal(s: &str, reader: &mut Reader) -> ParseResult<()> {
pub fn try_literal(s: &str, p: &mut Reader) -> ParseResult<()> {
match literal(s, p) {
Ok(_) => Ok(()),
Err(Error { pos, inner, .. }) => Err(Error {
Err(ParseError { pos, kind, .. }) => Err(ParseError {
pos,
recoverable: true,
inner,
kind,
}),
}
}
@ -276,34 +227,19 @@ mod tests {
let mut reader = Reader::new("");
let error = natural(&mut reader).err().unwrap();
assert_eq!(error.pos, Pos { line: 1, column: 1 });
assert_eq!(
error.inner,
ParseError::Expecting {
value: String::from("natural")
}
);
assert_eq!(error.kind, ParseErrorKind::Expecting("natural".to_string()));
assert!(error.recoverable);
let mut reader = Reader::new("01");
let error = natural(&mut reader).err().unwrap();
assert_eq!(error.pos, Pos { line: 1, column: 2 });
assert_eq!(
error.inner,
ParseError::Expecting {
value: String::from("natural")
}
);
assert_eq!(error.kind, ParseErrorKind::Expecting("natural".to_string()));
assert!(!error.recoverable);
let mut reader = Reader::new("x");
let error = natural(&mut reader).err().unwrap();
assert_eq!(error.pos, Pos { line: 1, column: 1 });
assert_eq!(
error.inner,
ParseError::Expecting {
value: String::from("natural")
}
);
assert_eq!(error.kind, ParseErrorKind::Expecting("natural".to_string()));
assert!(error.recoverable);
}
@ -321,12 +257,7 @@ mod tests {
let mut reader = Reader::new("x");
let error = integer(&mut reader).err().unwrap();
assert_eq!(error.pos, Pos { line: 1, column: 1 });
assert_eq!(
error.inner,
ParseError::Expecting {
value: String::from("natural")
}
);
assert_eq!(error.kind, ParseErrorKind::Expecting("natural".to_string()));
assert!(error.recoverable);
}
@ -405,45 +336,25 @@ mod tests {
fn test_number_error() {
let mut reader = Reader::new("");
let error = number(&mut reader).err().unwrap();
assert_eq!(
error.inner,
ParseError::Expecting {
value: String::from("natural")
}
);
assert_eq!(error.kind, ParseErrorKind::Expecting("natural".to_string()));
assert_eq!(error.pos, Pos { line: 1, column: 1 });
assert!(error.recoverable);
let mut reader = Reader::new("-");
let error = number(&mut reader).err().unwrap();
assert_eq!(
error.inner,
ParseError::Expecting {
value: String::from("natural")
}
);
assert_eq!(error.kind, ParseErrorKind::Expecting("natural".to_string()));
assert_eq!(error.pos, Pos { line: 1, column: 2 });
assert!(error.recoverable);
let mut reader = Reader::new("1.");
let error = number(&mut reader).err().unwrap();
assert_eq!(
error.inner,
ParseError::Expecting {
value: String::from("natural")
}
);
assert_eq!(error.kind, ParseErrorKind::Expecting("natural".to_string()));
assert_eq!(error.pos, Pos { line: 1, column: 3 });
assert!(!error.recoverable);
let mut reader = Reader::new("1.x");
let error = number(&mut reader).err().unwrap();
assert_eq!(
error.inner,
ParseError::Expecting {
value: String::from("natural")
}
);
assert_eq!(error.kind, ParseErrorKind::Expecting("natural".to_string()));
assert_eq!(error.pos, Pos { line: 1, column: 3 });
assert!(!error.recoverable);
}
@ -458,23 +369,13 @@ mod tests {
let mut reader = Reader::new("1");
let error = string_value(&mut reader).err().unwrap();
assert_eq!(
error.inner,
ParseError::Expecting {
value: String::from("'")
}
);
assert_eq!(error.kind, ParseErrorKind::Expecting("'".to_string()));
assert_eq!(error.pos, Pos { line: 1, column: 1 });
assert!(error.recoverable);
let mut reader = Reader::new("'hi");
let error = string_value(&mut reader).err().unwrap();
assert_eq!(
error.inner,
ParseError::Expecting {
value: String::from("'")
}
);
assert_eq!(error.kind, ParseErrorKind::Expecting("'".to_string()));
assert_eq!(error.pos, Pos { line: 1, column: 4 });
assert!(!error.recoverable);
}
@ -490,22 +391,12 @@ mod tests {
let mut reader = Reader::new(".");
let error = key_name(&mut reader).err().unwrap();
assert!(!error.recoverable);
assert_eq!(
error.inner,
ParseError::Expecting {
value: "key".to_string()
}
);
assert_eq!(error.kind, ParseErrorKind::Expecting("key".to_string()));
let mut reader = Reader::new("1id");
let error = key_name(&mut reader).err().unwrap();
assert!(!error.recoverable);
assert_eq!(
error.inner,
ParseError::Expecting {
value: "key".to_string()
}
);
assert_eq!(error.kind, ParseErrorKind::Expecting("key".to_string()));
}
#[test]
@ -533,34 +424,19 @@ mod tests {
let mut reader = Reader::new("");
let error = literal("hello", &mut reader).err().unwrap();
assert_eq!(error.pos, Pos { line: 1, column: 1 });
assert_eq!(
error.inner,
ParseError::Expecting {
value: String::from("hello")
}
);
assert_eq!(error.kind, ParseErrorKind::Expecting("hello".to_string()));
assert_eq!(reader.state.cursor, 0);
let mut reader = Reader::new("hi");
let error = literal("hello", &mut reader).err().unwrap();
assert_eq!(error.pos, Pos { line: 1, column: 1 });
assert_eq!(
error.inner,
ParseError::Expecting {
value: String::from("hello")
}
);
assert_eq!(error.kind, ParseErrorKind::Expecting("hello".to_string()));
assert_eq!(reader.state.cursor, 2);
let mut reader = Reader::new("he");
let error = literal("hello", &mut reader).err().unwrap();
assert_eq!(error.pos, Pos { line: 1, column: 1 });
assert_eq!(
error.inner,
ParseError::Expecting {
value: String::from("hello")
}
);
assert_eq!(error.kind, ParseErrorKind::Expecting("hello".to_string()));
assert_eq!(reader.state.cursor, 2);
}
}