diff --git a/ablescript/src/error.rs b/ablescript/src/error.rs index 195a271..46f9d86 100644 --- a/ablescript/src/error.rs +++ b/ablescript/src/error.rs @@ -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), diff --git a/ablescript/src/parser.rs b/ablescript/src/parser.rs index 5cd89ab..5b1cc53 100644 --- a/ablescript/src/parser.rs +++ b/ablescript/src/parser.rs @@ -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,