This commit is contained in:
2026-04-12 17:26:39 -04:00
parent 2582e8c87e
commit f702f47714
9 changed files with 121 additions and 49 deletions
+5 -8
View File
@@ -1,3 +1,5 @@
use std::borrow::Borrow;
use crate::io::{CompilerMsg, Span, Spanned};
mod lit;
@@ -35,8 +37,8 @@ impl<'a> Cursor<'a> {
})
}
pub fn next_if(&mut self, token: &Token) -> bool {
if self.peek().is_some_and(|t| t == token) {
pub fn next_if(&mut self, token: impl Borrow<Token>) -> bool {
if self.peek().is_some_and(|t| t == token.borrow()) {
self.next();
true
} else {
@@ -52,11 +54,6 @@ impl<'a> Cursor<'a> {
self.next().ok_or_else(CompilerMsg::unexpected_eof)
}
pub fn expect_peek(&self) -> Result<&Token, CompilerMsg> {
self.peek().ok_or_else(CompilerMsg::unexpected_eof)
// Ok(self.peek().unwrap())
}
pub fn expect(&mut self, token: Token) -> Result<Token, CompilerMsg> {
let next = self.expect_next()?;
if next == token {
@@ -87,7 +84,7 @@ impl CompilerMsg {
pub fn unexpected_token(token: &Token, span: Span, expected: &str) -> Self {
Self {
spans: vec![span],
msg: format!("Unexpected token '{}'; expected {expected}", token),
msg: format!("Unexpected token '{}', expected {expected}", token),
}
}