Skip to content

Commit

Permalink
Parse ellipsis operator and emit a customized error message
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Jul 8, 2024
1 parent 20e83a1 commit 439f077
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
29 changes: 28 additions & 1 deletion crates/metaslang/cst/src/query/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub(super) fn parse_matcher_alt_sequence<T: KindTypes>(
pub(super) fn parse_sequence_item<T: KindTypes>(
i: &str,
) -> IResult<&str, ASTNode<T>, VerboseError<&str>> {
alt((anchor::<T>, parse_quantified_matcher::<T>)).parse(i)
alt((ellipsis_token, anchor::<T>, parse_quantified_matcher::<T>)).parse(i)
}

pub(super) fn parse_quantified_matcher<T: KindTypes>(
Expand Down Expand Up @@ -335,3 +335,30 @@ fn anchor<T: KindTypes>(i: &str) -> IResult<&str, ASTNode<T>, VerboseError<&str>
.map(|_| ASTNode::Anchor)
.parse(i)
}

fn recognize_as_failure<I: Clone, O1, O2, E: nom::error::ParseError<I>, F>(
mut parser: F,
) -> impl FnMut(I) -> IResult<I, O2, E>
where
F: nom::Parser<I, O1, E>,
{
use nom::error::{make_error, ErrorKind};
use nom::Err::Failure;
move |input: I| {
let i = input.clone();
match parser.parse(i) {
Ok((_, _)) => Err(Failure(make_error(input, ErrorKind::Fail))),
Err(e) => Err(e),
}
}
}

fn ellipsis_token<O>(i: &str) -> IResult<&str, O, VerboseError<&str>> {
use nom::bytes::complete::tag;
use nom::error::context;
context(
"deprecated ellipsis operator",
recognize_as_failure(terminated(tag("..."), multispace0)),
)
.parse(i)
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ fn test_fails_parsing_ellipsis() {
let result = Query::parse(r#"[_ ...]"#);
match result {
Ok(_) => panic!("Expected parse failure"),
Err(e) => assert_eq!(e.message, "Parse error:\nNoneOf at: ..]\n"),
Err(e) => assert_eq!(
e.message,
"Parse error:\nFail at: ...]\nin section 'deprecated ellipsis operator', at: ...]\n"
),
}
}

Expand Down

0 comments on commit 439f077

Please sign in to comment.