parser: tighten error reporting

This commit is contained in:
Guillem L. Jara
2026-05-07 18:00:56 +02:00
parent 432afad97b
commit 746f3d5eef
4 changed files with 19 additions and 6 deletions
+1 -1
View File
@@ -149,7 +149,7 @@ pub fn report_error<'a>(
name: &'a str,
source: &'a [u8],
) -> super::AriadneErr<'a> {
let span = error.span().unwrap();
let span = error.span().unwrap_or(source.len()..source.len());
let source = str::from_utf8(source).unwrap();
let mut report = Report::build(ReportKind::Error, (name, span.clone()))
.with_message("Syntax error")
+2 -2
View File
@@ -53,7 +53,7 @@ impl<'a> Lexer<'a> {
Some(Ok(tok)) if expected == &tok => Ok(tok),
Some(Ok(_)) => Err(err(self.span())),
Some(err @ Err(_)) => err.map_err(Into::into),
None => todo!(),
None => Err(ParsingError::LexingError(LexingError::UnexpectedEof)),
}
}
@@ -66,7 +66,7 @@ impl<'a> Lexer<'a> {
Some(Ok(tok)) if expected(&tok) => Ok(tok),
Some(Ok(_)) => Err(ParsingError::UnexpectedToken(self.span(), msg)),
Some(err @ Err(_)) => err.map_err(Into::into),
None => todo!(),
None => Err(ParsingError::LexingError(LexingError::UnexpectedEof)),
}
}
+12 -2
View File
@@ -157,7 +157,12 @@ impl<'a> Parser<'a> {
));
}
Token::Newline | Token::Semicolon => {}
x => unimplemented!("{x:?}"),
_ => {
return Err(ParsingError::UnexpectedToken(
lex.span(),
"invalid rule beginning.".into(),
));
}
}
}
}
@@ -362,7 +367,12 @@ impl<'a> Parser<'a> {
.then(|| self.parse_expression(lex))
.transpose()?,
),
a => todo!("{a:?}"),
_ => {
return Err(ParsingError::UnexpectedToken(
lex.span(),
"invalid statement start.".into(),
));
}
}
};
+4 -1
View File
@@ -163,7 +163,10 @@ impl<'a, 'b> Pratt<'a, 'b> {
self.parser
.parse_function_call(lex, name.qualify(self.parser.namespace), lex.span())
} else {
Ok(Expr::leaf(self.parser.parse_atom(lex, next)?))
match self.parser.parse_atom(lex, next) {
Ok(atom) => Ok(Expr::leaf(atom)),
Err(_) => Err(ParsingError::InvalidExpression(lex.span())),
}
}
}