Skip to content

Commit

Permalink
parser: Report errors for invalid patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Jan 19, 2025
1 parent cce864d commit 7b8efbf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 3 additions & 0 deletions dora-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,9 @@ impl Parser {
params,
}))
} else {
self.report_error(ParseError::ExpectedPattern);
self.advance();

Arc::new(Pattern::Error(PatternError {
id: self.new_node_id(),
span: self.finish_node(),
Expand Down
28 changes: 27 additions & 1 deletion dora-parser/src/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,22 @@ fn parse(code: &'static str) -> Arc<File> {

fn parse_with_error(code: &'static str, expected: Vec<(u32, u32, u32, ParseError)>) -> Arc<File> {
let (file, errors) = Parser::from_string(code).parse();
assert_eq!(expected.len(), errors.len());
let line_starts = compute_line_starts(code);

for error in &errors {
let (line, col) = compute_line_column(&line_starts, error.span.start());

println!(
"{} at {}:{} of length {}",
error.error.message(),
line,
col,
error.span.len()
);
}

assert_eq!(expected.len(), errors.len());

for ((exp_line, exp_col, exp_len, exp_error), actual_error) in expected.into_iter().zip(errors)
{
let (line, col) = compute_line_column(&line_starts, actual_error.span.start());
Expand Down Expand Up @@ -1484,6 +1497,19 @@ fn parse_match() {
parse_expr("match x { A(x, b) if foo => 1, B => 2 }");
parse_expr("match x { A(x, b) => 1, B | C => 2 }");
parse_expr("match x { A(x, b) => { 1 } B | C => { 2 } }");

parse_with_error(
"
fn main() {
match x { A(x, b) => 1, as }
}
",
vec![
(3, 41, 2, ParseError::ExpectedPattern),
(3, 44, 1, ParseError::ExpectedToken("=>".into())),
(3, 44, 1, ParseError::ExpectedExpression),
],
);
}

#[test]
Expand Down

0 comments on commit 7b8efbf

Please sign in to comment.