lexer: use Err type for unrecognised tokens instead of unimplemented

This commit is contained in:
Ryan Chandler 2022-12-05 19:28:19 +00:00
parent ae2f617700
commit 010fe7be26
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
2 changed files with 9 additions and 6 deletions

View File

@ -16,6 +16,7 @@ pub enum SyntaxError {
UnpredictableState(Span),
InvalidDocIndentation(Span),
InvalidDocBodyIndentationLevel(usize, Span),
UnrecognisedToken(u8, Span),
}
impl Display for SyntaxError {
@ -72,6 +73,13 @@ impl Display for SyntaxError {
expected,
span.0
),
Self::UnrecognisedToken(token, span) => write!(
f,
"Syntax Error: Unrecognised token {} on line {} column {}",
token,
span.0,
span.1
)
}
}
}

View File

@ -1126,12 +1126,7 @@ impl Lexer {
let label = self.consume_identifier(state);
TokenKind::Identifier(label.into())
}
&[b, ..] => unimplemented!(
"<var offset> char: {}, line: {}, col: {}",
b as char,
state.span.0,
state.span.1
),
&[b, ..] => return Err(SyntaxError::UnrecognisedToken(b, state.span)),
[] => return Err(SyntaxError::UnexpectedEndOfFile(state.span)),
};
Ok(Token { kind, span })