Skip to content

Commit

Permalink
refactor(ir): errors for parser
Browse files Browse the repository at this point in the history
  • Loading branch information
JuniMay committed Feb 23, 2024
1 parent 1d31433 commit 0f45361
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/ir/frontend/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ where
}
}

pub(super) fn curr_pos(&self) -> Pos {
self.curr_pos
}

fn next_char(&mut self) -> Option<char> {
let mut buf = [0; 1];
match self.buf.read(&mut buf) {
Expand Down
18 changes: 14 additions & 4 deletions src/ir/frontend/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::io::{self};

use thiserror::Error;

use crate::ir::types::Type;

use super::{
Expand Down Expand Up @@ -31,12 +33,14 @@ where
peeked: bool,
}

#[derive(Debug)]
#[derive(Debug, Error)]
pub enum ParseError {
/// Lexer error
LexerError,
#[error("lexer error at {0}")]
LexerError(Pos),

/// Unexpected token
#[error("unexpected token at {0}")]
UnexpectedToken(Span),
}

Expand All @@ -58,7 +62,10 @@ where
self.peeked = false;
return Ok(&self.curr_token);
}
let token = self.lexer.next_token().ok_or(ParseError::LexerError)?;
let token = self
.lexer
.next_token()
.ok_or(ParseError::LexerError(self.lexer.curr_pos()))?;
self.curr_token = token;
Ok(&self.curr_token)
}
Expand All @@ -70,7 +77,10 @@ where
if self.peeked {
return Ok(&self.curr_token);
}
let token = self.lexer.next_token().ok_or(ParseError::LexerError)?;
let token = self
.lexer
.next_token()
.ok_or(ParseError::LexerError(self.lexer.curr_pos()))?;
self.curr_token = token;
self.peeked = true;
Ok(&self.curr_token)
Expand Down

0 comments on commit 0f45361

Please sign in to comment.