Skip to content

Commit

Permalink
feat(path): show location in parse error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
zaneduffield committed Dec 19, 2024
1 parent 683aea7 commit a416fda
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl FromStr for Expression {
struct ParseError(String);

impl ParseError {
fn new(inner: winnow::error::ContextError) -> Self {
fn new(inner: winnow::error::ParseError<&str, winnow::error::ContextError>) -> Self {
Self(inner.to_string())
}
}
Expand Down
44 changes: 36 additions & 8 deletions src/path/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use winnow::combinator::opt;
use winnow::combinator::repeat;
use winnow::combinator::seq;
use winnow::error::ContextError;
use winnow::error::ParseError;
use winnow::error::StrContext;
use winnow::error::StrContextValue;
use winnow::prelude::*;
Expand All @@ -17,9 +18,8 @@ use winnow::token::take_while;

use crate::path::Expression;

pub(crate) fn from_str(mut input: &str) -> Result<Expression, ContextError> {
let input = &mut input;
path.parse(input).map_err(|e| e.into_inner())
pub(crate) fn from_str(input: &str) -> Result<Expression, ParseError<&str, ContextError>> {
path.parse(input)
}

fn path(i: &mut &str) -> PResult<Expression> {
Expand Down Expand Up @@ -143,7 +143,11 @@ mod test {
fn test_invalid_identifier() {
let err = from_str("!").unwrap_err();
assert_eq!(
"invalid identifier\nexpected ASCII alphanumeric, underscore, hyphen",
"\
!
^
invalid identifier
expected ASCII alphanumeric, underscore, hyphen",
err.to_string()
);
}
Expand All @@ -152,26 +156,50 @@ mod test {
fn test_invalid_child() {
let err = from_str("a..").unwrap_err();
assert_eq!(
"invalid identifier\nexpected ASCII alphanumeric, underscore, hyphen",
"\
a..
^
invalid identifier
expected ASCII alphanumeric, underscore, hyphen",
err.to_string()
);
}

#[test]
fn test_invalid_subscript() {
let err = from_str("a[b]").unwrap_err();
assert_eq!("invalid subscript\nexpected integer", err.to_string());
assert_eq!(
"\
a[b]
^
invalid subscript
expected integer",
err.to_string()
);
}

#[test]
fn test_incomplete_subscript() {
let err = from_str("a[0").unwrap_err();
assert_eq!("invalid subscript", err.to_string());
assert_eq!(
"\
a[0
^
invalid subscript",
err.to_string()
);
}

#[test]
fn test_invalid_postfix() {
let err = from_str("a!b").unwrap_err();
assert_eq!("invalid postfix\nexpected `[`, `.`", err.to_string());
assert_eq!(
"\
a!b
^
invalid postfix
expected `[`, `.`",
err.to_string()
);
}
}

0 comments on commit a416fda

Please sign in to comment.