Skip to content

Commit

Permalink
Fail parsing ellipsis or consecutive anchors in queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Jul 2, 2024
1 parent 020e23c commit 908e1a8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
18 changes: 11 additions & 7 deletions crates/metaslang/cst/src/query/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use std::rc::Rc;

use nom::branch::alt;
use nom::bytes::complete::{is_not, take_while, take_while1, take_while_m_n};
use nom::character::complete::{char, multispace0, multispace1, satisfy};
use nom::combinator::{all_consuming, map_opt, map_res, opt, recognize, success, value, verify};
use nom::character::complete::{char, multispace0, multispace1, none_of, satisfy};
use nom::combinator::{
all_consuming, cut, map_opt, map_res, opt, peek, recognize, success, value, verify,
};
use nom::error::VerboseError;
use nom::multi::{fold_many0, many1, separated_list1};
use nom::sequence::{delimited, pair, preceded, terminated};
Expand Down Expand Up @@ -73,11 +75,13 @@ pub(super) fn parse_matcher_sequence<T: KindTypes>(
pub(super) fn parse_sequence_item<T: KindTypes>(
i: &str,
) -> IResult<&str, ASTNode<T>, VerboseError<&str>> {
alt((
token('.').map(|_| ASTNode::Anchor),
parse_quantified_matcher::<T>,
))
.parse(i)
alt((parse_anchor::<T>, parse_quantified_matcher::<T>)).parse(i)
}

pub(super) fn parse_anchor<T: KindTypes>(i: &str) -> IResult<&str, ASTNode<T>, VerboseError<&str>> {
pair(token('.'), cut(peek(none_of(". \t\r\n"))))
.map(|_| ASTNode::Anchor)
.parse(i)
}

pub(super) fn parse_quantified_matcher<T: KindTypes>(
Expand Down
22 changes: 20 additions & 2 deletions crates/testlang/outputs/cargo/tests/src/query/parser_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,27 @@ fn test_zero_or_more_canonicalisation() {
// Test the error message on parse failure
#[test]
fn test_parsing_error() {
let result = Query::parse(r#"@root [_ ."#);
let result = Query::parse(r#"@root [_"#);
match result {
Ok(_) => panic!("Expected error"),
Err(e) => assert_eq!(e.message, "Parse error:\nexpected ']' at: \nAlt at: [_ .\n"),
Err(e) => assert_eq!(e.message, "Parse error:\nexpected ']' at: \nAlt at: [_\n"),
}
}

#[test]
fn test_fails_parsing_ellipsis() {
let result = Query::parse(r#"[_ ...]"#);
match result {
Ok(_) => panic!("Expected error"),
Err(e) => assert_eq!(e.message, "Parse error:\nNoneOf at: ..]\n"),
}
}

#[test]
fn test_fails_consecutive_anchors() {
let result = Query::parse(r#"[_ . .]"#);
match result {
Ok(_) => panic!("Expected error"),
Err(e) => assert_eq!(e.message, "Parse error:\nNoneOf at: .]\n"),
}
}

0 comments on commit 908e1a8

Please sign in to comment.