refactor(ast): Improve error type, remove unnecessary eof judge

This commit is contained in:
2026-06-12 11:11:31 +08:00
parent 4dabf32c2a
commit ef3dcb41aa
8 changed files with 62 additions and 231 deletions
+4 -14
View File
@@ -1,24 +1,21 @@
use crate::{
ast::err::ParseError,
ast::types::{
BlockStmt, BreakStmt, ContinueStmt, ForInit, ForStmt, IfElseBranch, IfStmt, ReturnStmt,
Statement, WhileStmt,
},
lexer::{
err::ParseError,
types::TokenValue,
},
lexer::types::TokenValue,
};
use super::{ParseProcessError, ParseType, Parser};
impl Parser {
pub(super) fn parse_block_stmt(&mut self, parse_type: ParseType) -> Result<BlockStmt, ParseProcessError> {
assert!(!self.is_eof());
if self.peek().value != TokenValue::LBrace {
if parse_type == ParseType::MustParse {
let token = self.next().clone();
self.diagnostics.add_from_frontend_error(
ParseError::UnexpectedToken(token.value, "`{`"),
ParseError::ExpectedBefore(token.value, "`{`"),
token.span,
);
return Err(ParseProcessError::ErrorInMatch);
@@ -32,7 +29,7 @@ impl Parser {
if self.is_eof() {
let span = self.last().span;
self.diagnostics
.add_from_frontend_error(ParseError::ExpectButEof("`}`"), span);
.add_from_frontend_error(ParseError::ExpectedBefore(TokenValue::Eof, "`}`"), span);
return Err(ParseProcessError::ErrorInMatch);
}
if matches!(self.peek().value, TokenValue::Semicolon) {
@@ -62,7 +59,6 @@ impl Parser {
}
fn parse_stmt(&mut self) -> Result<Statement, ParseProcessError> {
assert!(!self.is_eof());
match self.parse_var_decl_stmt(ParseType::TryParse) {
Ok(var_decl) => return Ok(Statement::VarDecl(var_decl)),
Err(ParseProcessError::ErrorInMatch) => return Err(ParseProcessError::ErrorInMatch),
@@ -116,7 +112,6 @@ impl Parser {
}
fn parse_return_stmt(&mut self) -> Result<ReturnStmt, ParseProcessError> {
assert!(!self.is_eof());
if self.peek().value != TokenValue::Return {
return Err(ParseProcessError::TryNext);
}
@@ -133,7 +128,6 @@ impl Parser {
})
}
fn parse_if_stmt(&mut self) -> Result<IfStmt, ParseProcessError> {
assert!(!self.is_eof());
if self.peek().value != TokenValue::If {
return Err(ParseProcessError::TryNext);
}
@@ -202,7 +196,6 @@ impl Parser {
}
fn parse_while_stmt(&mut self) -> Result<WhileStmt, ParseProcessError> {
assert!(!self.is_eof());
if self.peek().value != TokenValue::While {
return Err(ParseProcessError::TryNext);
}
@@ -225,7 +218,6 @@ impl Parser {
}
fn parse_for_stmt(&mut self) -> Result<ForStmt, ParseProcessError> {
assert!(!self.is_eof());
if self.peek().value != TokenValue::For {
return Err(ParseProcessError::TryNext);
}
@@ -267,7 +259,6 @@ impl Parser {
}
fn parse_break_stmt(&mut self) -> Result<BreakStmt, ParseProcessError> {
assert!(!self.is_eof());
let start_span = self.peek().span;
if self.peek().value == TokenValue::Break {
self.advance(1);
@@ -281,7 +272,6 @@ impl Parser {
}
fn parse_continue_stmt(&mut self) -> Result<ContinueStmt, ParseProcessError> {
assert!(!self.is_eof());
let start_span = self.peek().span;
if self.peek().value == TokenValue::Continue {
self.advance(1);