Invalid Token error + fixed forgotten refactor

This commit is contained in:
Erin 2022-07-02 00:30:51 +02:00 committed by ondra05
parent 5c0889a0ae
commit 2c04ebbadc
2 changed files with 9 additions and 4 deletions

View file

@ -12,6 +12,9 @@ pub enum ErrorKind {
/// Parser expected token, but none was available
UnexpectedEoi,
/// Parser encountered unknown token
InvalidToken,
/// Parser expected certain token, but other one appeared
UnexpectedToken(Token),
@ -65,6 +68,7 @@ impl Display for ErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ErrorKind::UnexpectedEoi => write!(f, "unexpected end of input"),
ErrorKind::InvalidToken => write!(f, "invalid token"),
ErrorKind::UnexpectedToken(Token::Melo) => write!(f, "unexpected marten"),
ErrorKind::UnexpectedToken(token) => write!(f, "unexpected token {:?}", token),
ErrorKind::UnknownVariable(name) => write!(f, "unknown identifier \"{}\"", name),

View file

@ -34,6 +34,9 @@ impl<'source> Parser<'source> {
// Ignore comments
Token::Comment => continue,
// Invalid token
Token::Error => return Err(Error::new(ErrorKind::InvalidToken, self.lexer.span())),
// T-Dark block (replace `lang` with `script`)
Token::TDark => ast.extend(self.tdark_flow()?),
token => ast.push(self.parse_stmt(token)?),
@ -53,6 +56,7 @@ impl<'source> Parser<'source> {
.ok_or_else(|| Error::unexpected_eoi(self.lexer.span().start))?
{
Token::Comment => (),
Token::Error => break Err(Error::new(ErrorKind::InvalidToken, self.lexer.span())),
token => break Ok(token),
}
}
@ -256,10 +260,7 @@ impl<'source> Parser<'source> {
.ok_or_else(|| Error::new(ErrorKind::MissingLhs, self.lexer.span()))?,
),
rhs: {
let next = self
.lexer
.next()
.ok_or_else(|| Error::unexpected_eoi(self.lexer.span().start))?;
let next = self.checked_next()?;
Box::new(self.parse_expr(next, &mut None)?)
},
kind,