work
This commit is contained in:
+16
-15
@@ -1,8 +1,10 @@
|
||||
use crate::io::{CompilerMsg, Span, Spanned};
|
||||
use std::iter::Peekable;
|
||||
pub use token::*;
|
||||
|
||||
mod lit;
|
||||
mod token;
|
||||
pub use lit::*;
|
||||
pub use token::*;
|
||||
|
||||
pub struct Cursor<'a> {
|
||||
pub span: Span,
|
||||
@@ -33,10 +35,11 @@ impl<'a> Cursor<'a> {
|
||||
}
|
||||
|
||||
pub fn expect_next(&mut self) -> Result<Token, CompilerMsg> {
|
||||
self.next().ok_or_else(|| CompilerMsg {
|
||||
spans: Vec::new(),
|
||||
msg: "unexpected end of file".to_string(),
|
||||
})
|
||||
self.next().ok_or_else(CompilerMsg::unexpected_eof)
|
||||
}
|
||||
|
||||
pub fn expect_peek(&mut self) -> Result<&Token, CompilerMsg> {
|
||||
self.peek().ok_or_else(CompilerMsg::unexpected_eof)
|
||||
}
|
||||
|
||||
pub fn expect(&mut self, token: Token) -> Result<Token, CompilerMsg> {
|
||||
@@ -44,16 +47,7 @@ impl<'a> Cursor<'a> {
|
||||
if next == token {
|
||||
Ok(next)
|
||||
} else {
|
||||
self.unexpected(next, &format!("{token:?}"))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expect_ident(&mut self) -> Result<String, CompilerMsg> {
|
||||
let next = self.expect_next()?;
|
||||
if let Token::Ident(s) = next {
|
||||
Ok(s)
|
||||
} else {
|
||||
self.unexpected(next, "identifier")
|
||||
self.unexpected(next, &format!("'{token}'"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,4 +81,11 @@ impl CompilerMsg {
|
||||
msg: format!("Unexpected token '{}'; expected {expected}", inst.inner),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unexpected_eof() -> Self {
|
||||
Self {
|
||||
spans: Vec::new(),
|
||||
msg: "unexpected end of file".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user