From ef3dcb41aa92a8801ce602d8c20b292514f261eb Mon Sep 17 00:00:00 2001 From: Hydrostic Date: Fri, 12 Jun 2026 11:11:31 +0800 Subject: [PATCH] refactor(ast): Improve error type, remove unnecessary eof judge --- src/ast/err.rs | 11 +++ src/ast/mod.rs | 1 + src/ast/parser/decl.rs | 53 +++---------- src/ast/parser/expr.rs | 173 +++++++---------------------------------- src/ast/parser/mod.rs | 15 +--- src/ast/parser/stmt.rs | 18 +---- src/lexer/err.rs | 20 +---- src/lexer/types.rs | 2 +- 8 files changed, 62 insertions(+), 231 deletions(-) create mode 100644 src/ast/err.rs diff --git a/src/ast/err.rs b/src/ast/err.rs new file mode 100644 index 0000000..c2e36f5 --- /dev/null +++ b/src/ast/err.rs @@ -0,0 +1,11 @@ +use thiserror::Error; + +use crate::lexer::types::TokenValue; + +#[derive(Debug, Clone, PartialEq, Eq, Error)] +pub enum ParseError { + #[error("expected {} before {}", .1, .0)] + ExpectedBefore(TokenValue, &'static str), + #[error("expected identifier after {}", .0)] + ExpectedIdentAfter(TokenValue), +} diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 8ba390b..4ba2b51 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -1,3 +1,4 @@ +pub mod err; pub mod graph; pub mod parser; pub mod types; diff --git a/src/ast/parser/decl.rs b/src/ast/parser/decl.rs index 71c5f7c..19edba5 100644 --- a/src/ast/parser/decl.rs +++ b/src/ast/parser/decl.rs @@ -1,12 +1,10 @@ use crate::{ + ast::err::ParseError, ast::types::{ ArrayDimension, CompileUnit, FuncDeclStmt, GlobalDeclStmt, Param, VarDeclStmt, VarDeclStmtValue, }, diagnostic::span::Span, - lexer::{ - err::ParseError, - types::{TokenValue, TypeIdent}, - }, + lexer::types::{TokenValue, TypeIdent}, }; use super::{ParseProcessError, ParseType, Parser}; @@ -25,7 +23,6 @@ impl Parser { } fn parse_global_decl_stmt(&mut self) -> Option { - assert!(!self.is_eof()); match self.parse_func_decl_stmt() { Ok(func_decl) => return Some(GlobalDeclStmt::FuncDecl(func_decl)), Err(ParseProcessError::ErrorInMatch) => { @@ -42,7 +39,7 @@ impl Parser { } let token = self.next().clone(); self.diagnostics.add_from_frontend_error( - ParseError::UnexpectedToken(token.value, "ident"), + ParseError::ExpectedBefore(token.value, "ident"), token.span, ); None @@ -58,7 +55,6 @@ impl Parser { // } // } fn parse_type_and_name(&mut self, parse_type: ParseType) -> Result<(TypeIdent, String, Span, Span), ParseProcessError> { - assert!(!self.is_eof()); let type_token = self.peek().clone(); let type_ident = match self.peek().value.as_type_ident() { Some(ti) => ti, @@ -66,7 +62,7 @@ impl Parser { if matches!(parse_type, ParseType::MustParse) { let token = self.next().clone(); self.diagnostics.add_from_frontend_error( - ParseError::UnexpectedToken(token.value, "type ident"), + ParseError::ExpectedBefore(token.value, "type ident"), token.span, ); return Err(ParseProcessError::ErrorInMatch); @@ -77,17 +73,10 @@ impl Parser { let type_span = type_token.span; self.advance(1); let name = match self.peek().value.as_ident() { - None if self.is_eof() => { - self.diagnostics.add_from_frontend_error( - ParseError::ExpectButEof("ident"), - type_span, - ); - return Err(ParseProcessError::ErrorInMatch); - }, None => { let next_span = self.peek().span; self.diagnostics.add_from_frontend_error( - ParseError::CantCombineWith(type_token.value), + ParseError::ExpectedIdentAfter(type_token.value), next_span ); return Err(ParseProcessError::ErrorInMatch); @@ -99,7 +88,6 @@ impl Parser { Ok((type_ident, name, type_span, name_span)) } fn parse_func_decl_stmt(&mut self) -> Result { - assert!(!self.is_eof()); let (return_type, name, ret_type_span, name_span) = self.parse_type_and_name(ParseType::MustParse)?; if matches!(self.peek().value, TokenValue::LParen) { } else { @@ -108,14 +96,7 @@ impl Parser { } // from here we can be sure it's a function declaration, so we can report error if the syntax is wrong let params = self.parse_param_list()?; - let body = if self.is_eof() { - let span = self.next().span; - self.diagnostics - .add_from_frontend_error(ParseError::ExpectButEof("function body"), span); - return Err(ParseProcessError::ErrorInMatch); - } else { - self.parse_block_stmt(ParseType::MustParse)? - }; + let body = self.parse_block_stmt(ParseType::MustParse)?; Ok(FuncDeclStmt { return_type: return_type.into(), name, @@ -126,11 +107,10 @@ impl Parser { }) } fn parse_param_list(&mut self) -> Result, ParseProcessError> { - assert!(!self.is_eof()); if self.peek().value != TokenValue::LParen { 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); @@ -163,7 +143,6 @@ impl Parser { Ok(params) } fn parse_param(&mut self) -> Result { - assert!(!self.is_eof()); let (param_type, name, type_span, name_span) = self.parse_type_and_name(ParseType::MustParse)?; Ok(Param { param_type: param_type.into(), @@ -183,7 +162,7 @@ impl Parser { // } else { // let token = self.next().clone(); // self.diagnostics - // .add_from_frontend_error(ParseError::UnexpectedToken(token.value, "`;`"), token.span); + // .add_from_frontend_error(ParseError::ExpectedBefore(token.value, "`;`"), token.span); // while let Some(t) = self.peek() { // if matches!(t.value, TokenValue::Semicolon) { // self.advance(1); @@ -202,7 +181,6 @@ impl Parser { } pub(super) fn parse_var_decl_stmt_inner(&mut self, parse_type: ParseType, consume_semicolon: bool) -> Result { - assert!(!self.is_eof()); let mut values = vec![]; let (var_type, name, type_span, name_span) = match self.parse_type_and_name(parse_type) { Ok(res) => res, @@ -231,13 +209,6 @@ impl Parser { self.must_match_token(&TokenValue::Comma, "`,` or `;`")?; last_name = false; } - // check eof again - if self.is_eof() { - let span = self.last().span; - self.diagnostics - .add_from_frontend_error(ParseError::ExpectButEof("`,` or `;`"), span); - break; - } if let Some(ident) = self.peek().value.as_ident() { let span = self.next().span; let dimensions = self.parse_array_dimensions(false)?; @@ -252,7 +223,7 @@ impl Parser { } else { let token = self.next().clone(); self.diagnostics.add_from_frontend_error( - ParseError::CantCombineWith(TokenValue::TypeIdent(var_type)), + ParseError::ExpectedIdentAfter(TokenValue::TypeIdent(var_type)), token.span, ); return Err(ParseProcessError::ErrorInMatch); @@ -272,7 +243,7 @@ impl Parser { if !allow_empty_first || !dimensions.is_empty() { let span = self.peek().span; self.diagnostics.add_from_frontend_error( - ParseError::UnexpectedToken(TokenValue::RBracket, "array dimension expression"), + ParseError::ExpectedBefore(TokenValue::RBracket, "array dimension expression"), span, ); return Err(ParseProcessError::ErrorInMatch); @@ -288,13 +259,13 @@ impl Parser { span } TokenValue::Eof => { - self.diagnostics.add_from_frontend_error(ParseError::ExpectButEof("`]`"), start_span); + self.diagnostics.add_from_frontend_error(ParseError::ExpectedBefore(TokenValue::Eof, "`]`"), start_span); return Err(ParseProcessError::ErrorInMatch); } _ => { let token = self.next(); self.diagnostics.add_from_frontend_error( - ParseError::UnexpectedToken(token.value, "`]`"), + ParseError::ExpectedBefore(token.value, "`]`"), token.span, ); return Err(ParseProcessError::ErrorInMatch); diff --git a/src/ast/parser/expr.rs b/src/ast/parser/expr.rs index 2adda7a..326b36f 100644 --- a/src/ast/parser/expr.rs +++ b/src/ast/parser/expr.rs @@ -1,32 +1,14 @@ use crate::{ + ast::err::ParseError, ast::types::{BinaryOp, Expr, ExprValue, IncDecOp, UnaryOp}, diagnostic::span::Span, - lexer::{ - err::ParseError, - types::TokenValue, - }, + lexer::types::TokenValue, }; use super::{ParseProcessError, Parser}; impl Parser { - // fn parse_expr_tail(&mut self, left: Expr) -> Option { - // match self.peek() { - // None => Some(left), - // Some(t1) => { - // // TODO: add delimiter judge to support better error recovery - // let op = BinaryOp::from_token_value(&t1.value)?; - // match op { - // BinaryOp::Add - // } - // let right = self.parse_term()?; - // let expr = Expr { value: ExprValue::BinaryOp { lhs: Box::new(left), op, rhs: Box::new(right) }, span: Span::from_two(left.span, right.span) }; - // self.parse_expr_tail(expr) - // } - // } - // } fn parse_primary_atom(&mut self) -> Result { - assert!(!self.is_eof()); let token = self.next().clone(); match token.value { TokenValue::Ident(name) => { @@ -45,7 +27,7 @@ impl Parser { } TokenValue::Eof => { self.diagnostics.add_from_frontend_error( - ParseError::ExpectButEof("`)`"), + ParseError::ExpectedBefore(TokenValue::Eof, "`)`"), token.span, ); return Err(ParseProcessError::ErrorInMatch); @@ -67,7 +49,7 @@ impl Parser { } TokenValue::Eof => { self.diagnostics.add_from_frontend_error( - ParseError::ExpectButEof("`)`"), + ParseError::ExpectedBefore(TokenValue::Eof, "`)`"), token.span, ); return Err(ParseProcessError::ErrorInMatch); @@ -75,7 +57,7 @@ impl Parser { _ => { let token = self.next(); self.diagnostics.add_from_frontend_error( - ParseError::UnexpectedToken(token.value, "`,` or `)`"), + ParseError::ExpectedBefore(token.value, "`,` or `)`"), token.span, ); return Err(ParseProcessError::ErrorInMatch); @@ -93,15 +75,7 @@ impl Parser { span: token.span, }), TokenValue::LParen => { - let expr = if self.is_eof() { - self.diagnostics.add_from_frontend_error( - ParseError::ExpectButEof("expression"), - token.span, - ); - return Err(ParseProcessError::ErrorInMatch); - } else { - self.parse_expr()? - }; + let expr = self.parse_expr()?; match &self.peek().value { TokenValue::RParen => { let end_span = self.peek().span; @@ -113,13 +87,13 @@ impl Parser { } TokenValue::Eof => { self.diagnostics - .add_from_frontend_error(ParseError::ExpectButEof("`)`"), expr.span); + .add_from_frontend_error(ParseError::ExpectedBefore(TokenValue::Eof, "`)`"), expr.span); Err(ParseProcessError::ErrorInMatch) } _ => { let token = self.next(); self.diagnostics.add_from_frontend_error( - ParseError::UnexpectedToken(token.value, "`)`"), + ParseError::ExpectedBefore(token.value, "`)`"), token.span, ); Err(ParseProcessError::ErrorInMatch) @@ -128,7 +102,7 @@ impl Parser { } _ => { self.diagnostics.add_from_frontend_error( - ParseError::UnexpectedToken(token.value, "expression"), + ParseError::ExpectedBefore(token.value, "expression"), token.span, ); Err(ParseProcessError::ErrorInMatch) @@ -141,12 +115,7 @@ impl Parser { if self.peek().value == TokenValue::LBracket { let start_span = expr.span; self.advance(1); - let index = if self.is_eof() { - self.diagnostics.add_from_frontend_error(ParseError::ExpectButEof("array index expression"), start_span); - return Err(ParseProcessError::ErrorInMatch); - } else { - self.parse_expr()? - }; + let index = self.parse_expr()?; let end_span = match &self.peek().value { TokenValue::RBracket => { let span = self.peek().span; @@ -154,13 +123,13 @@ impl Parser { span } TokenValue::Eof => { - self.diagnostics.add_from_frontend_error(ParseError::ExpectButEof("`]`"), index.span); + self.diagnostics.add_from_frontend_error(ParseError::ExpectedBefore(TokenValue::Eof, "`]`"), index.span); return Err(ParseProcessError::ErrorInMatch); } _ => { let token = self.next(); self.diagnostics.add_from_frontend_error( - ParseError::UnexpectedToken(token.value, "`]`"), + ParseError::ExpectedBefore(token.value, "`]`"), token.span, ); return Err(ParseProcessError::ErrorInMatch); @@ -198,21 +167,12 @@ impl Parser { Ok(expr) } fn parse_unary(&mut self) -> Result { - assert!(!self.is_eof()); let token = self.peek().clone(); match token.value { TokenValue::Plus => { if self.peek_n(1).value == TokenValue::Plus { self.advance(2); - let expr = if self.is_eof() { - self.diagnostics.add_from_frontend_error( - ParseError::ExpectButEof("expression"), - token.span, - ); - return Err(ParseProcessError::ErrorInMatch); - } else { - self.parse_unary()? - }; + let expr = self.parse_unary()?; let span = Span::from_two(token.span, expr.span); return Ok(Expr { value: ExprValue::IncDec { @@ -224,15 +184,7 @@ impl Parser { }); } self.advance(1); - let expr = if self.is_eof() { - self.diagnostics.add_from_frontend_error( - ParseError::ExpectButEof("expression"), - token.span, - ); - return Err(ParseProcessError::ErrorInMatch); - } else { - self.parse_unary()? - }; + let expr = self.parse_unary()?; let span = Span::from_two(token.span, expr.span); Ok(Expr { value: ExprValue::UnaryOp { @@ -245,15 +197,7 @@ impl Parser { TokenValue::Minus => { if self.peek_n(1).value == TokenValue::Minus { self.advance(2); - let rhs = if self.is_eof() { - self.diagnostics.add_from_frontend_error( - ParseError::ExpectButEof("expression"), - token.span, - ); - return Err(ParseProcessError::ErrorInMatch); - } else { - self.parse_unary()? - }; + let rhs = self.parse_unary()?; let span = Span::from_two(token.span, rhs.span); return Ok(Expr { value: ExprValue::IncDec { @@ -265,15 +209,7 @@ impl Parser { }); } self.advance(1); - let rhs = if self.is_eof() { - self.diagnostics.add_from_frontend_error( - ParseError::ExpectButEof("expression"), - token.span, - ); - return Err(ParseProcessError::ErrorInMatch); - } else { - self.parse_unary()? - }; + let rhs = self.parse_unary()?; let span = Span::from_two(token.span, rhs.span); Ok(Expr { value: ExprValue::UnaryOp { @@ -285,15 +221,7 @@ impl Parser { } TokenValue::Not => { self.advance(1); - let rhs = if self.is_eof() { - self.diagnostics.add_from_frontend_error( - ParseError::ExpectButEof("expression"), - token.span, - ); - return Err(ParseProcessError::ErrorInMatch); - } else { - self.parse_unary()? - }; + let rhs = self.parse_unary()?; let span = Span::from_two(token.span, rhs.span); Ok(Expr { value: ExprValue::UnaryOp { @@ -307,9 +235,8 @@ impl Parser { } } fn parse_multiplicative(&mut self) -> Result { - assert!(!self.is_eof()); let mut left = self.parse_unary()?; - while !self.is_eof() { + loop { let op = match self.peek().value { TokenValue::Star => BinaryOp::Mul, TokenValue::Slash => BinaryOp::Div, @@ -317,13 +244,7 @@ impl Parser { _ => break, }; self.advance(1); - let right = if self.is_eof() { - self.diagnostics - .add_from_frontend_error(ParseError::ExpectButEof("expression"), left.span); - return Err(ParseProcessError::ErrorInMatch); - } else { - self.parse_unary()? - }; + let right = self.parse_unary()?; let span = Span::from_two(left.span, right.span); left = Expr { value: ExprValue::BinaryOp { @@ -337,22 +258,15 @@ impl Parser { Ok(left) } fn parse_additive(&mut self) -> Result { - assert!(!self.is_eof()); let mut left = self.parse_multiplicative()?; - while !self.is_eof() { + loop { let op = match self.peek().value { TokenValue::Plus => BinaryOp::Add, TokenValue::Minus => BinaryOp::Sub, _ => break, }; self.advance(1); - let right = if self.is_eof() { - self.diagnostics - .add_from_frontend_error(ParseError::ExpectButEof("expression"), left.span); - return Err(ParseProcessError::ErrorInMatch); - } else { - self.parse_multiplicative()? - }; + let right = self.parse_multiplicative()?; let span = Span::from_two(left.span, right.span); left = Expr { value: ExprValue::BinaryOp { @@ -366,9 +280,8 @@ impl Parser { Ok(left) } fn parse_relational(&mut self) -> Result { - assert!(!self.is_eof()); let mut left = self.parse_additive()?; - while !self.is_eof() { + loop { let op = match self.peek().value { TokenValue::Less => BinaryOp::Less, TokenValue::Greater => BinaryOp::Greater, @@ -379,13 +292,7 @@ impl Parser { _ => break, }; self.advance(1); - let right = if self.is_eof() { - self.diagnostics - .add_from_frontend_error(ParseError::ExpectButEof("expression"), left.span); - return Err(ParseProcessError::ErrorInMatch); - } else { - self.parse_additive()? - }; + let right = self.parse_additive()?; let span = Span::from_two(left.span, right.span); left = Expr { value: ExprValue::BinaryOp { @@ -399,21 +306,14 @@ impl Parser { Ok(left) } fn parse_logical_and(&mut self) -> Result { - assert!(!self.is_eof()); let mut left = self.parse_relational()?; - while !self.is_eof() { + loop { let op = match self.peek().value { TokenValue::And => BinaryOp::And, _ => break, }; self.advance(1); - let right = if self.is_eof() { - self.diagnostics - .add_from_frontend_error(ParseError::ExpectButEof("expression"), left.span); - return Err(ParseProcessError::ErrorInMatch); - } else { - self.parse_relational()? - }; + let right = self.parse_relational()?; let span = Span::from_two(left.span, right.span); left = Expr { value: ExprValue::BinaryOp { @@ -427,21 +327,14 @@ impl Parser { Ok(left) } fn parse_logical_or(&mut self) -> Result { - assert!(!self.is_eof()); let mut left = self.parse_logical_and()?; - while !self.is_eof() { + loop { let op = match self.peek().value { TokenValue::Or => BinaryOp::Or, _ => break, }; self.advance(1); - let right = if self.is_eof() { - self.diagnostics - .add_from_frontend_error(ParseError::ExpectButEof("expression"), left.span); - return Err(ParseProcessError::ErrorInMatch); - } else { - self.parse_logical_and()? - }; + let right = self.parse_logical_and()?; let span = Span::from_two(left.span, right.span); left = Expr { value: ExprValue::BinaryOp { @@ -455,21 +348,12 @@ impl Parser { Ok(left) } fn parse_assign(&mut self) -> Result { - assert!(!self.is_eof()); let lvalue = self.parse_logical_or()?; if self.peek().value != TokenValue::Equal { return Ok(lvalue); } self.advance(1); - let rvalue = if self.is_eof() { - self.diagnostics.add_from_frontend_error( - ParseError::ExpectButEof("expression"), - lvalue.span, - ); - return Err(ParseProcessError::ErrorInMatch); - } else { - self.parse_assign()? - }; + let rvalue = self.parse_assign()?; let span = Span::from_two(lvalue.span, rvalue.span); Ok(Expr { value: ExprValue::Assign { @@ -480,7 +364,6 @@ impl Parser { }) } pub(super) fn parse_expr(&mut self) -> Result { - assert!(!self.is_eof()); self.parse_assign() } } diff --git a/src/ast/parser/mod.rs b/src/ast/parser/mod.rs index d09678d..bf5b995 100644 --- a/src/ast/parser/mod.rs +++ b/src/ast/parser/mod.rs @@ -1,10 +1,8 @@ use crate::{ + ast::err::ParseError, ast::types::CompileUnit, diagnostic::{Diagnositics, span::Span}, - lexer::{ - err::ParseError, - types::{Token, TokenValue}, - }, + lexer::types::{Token, TokenValue}, }; mod decl; @@ -83,15 +81,10 @@ impl Parser { if &self.peek().value == expected { self.advance(1); Ok(()) - } else if self.is_eof() { - let span = self.peek().span; - self.diagnostics - .add_from_frontend_error(ParseError::ExpectButEof(diagnostic_text), span); - Err(ParseProcessError::ErrorInMatch) } else { let token = self.next(); self.diagnostics.add_from_frontend_error( - ParseError::UnexpectedToken(token.value, diagnostic_text), + ParseError::ExpectedBefore(token.value, diagnostic_text), token.span, ); Err(ParseProcessError::ErrorInMatch) @@ -101,7 +94,7 @@ impl Parser { if self.is_eof() { let span = self.last().span; self.diagnostics - .add_from_frontend_error(ParseError::ExpectButEof(diagnostic_text), span); + .add_from_frontend_error(ParseError::ExpectedBefore(TokenValue::Eof, diagnostic_text), span); return Err(ParseProcessError::ErrorInMatch); } Ok(()) diff --git a/src/ast/parser/stmt.rs b/src/ast/parser/stmt.rs index a0ab55d..5a31022 100644 --- a/src/ast/parser/stmt.rs +++ b/src/ast/parser/stmt.rs @@ -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 { - 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 { - 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 { - 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 { - 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 { - 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 { - 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 { - 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 { - assert!(!self.is_eof()); let start_span = self.peek().span; if self.peek().value == TokenValue::Continue { self.advance(1); diff --git a/src/lexer/err.rs b/src/lexer/err.rs index c5fb39f..603d1bc 100644 --- a/src/lexer/err.rs +++ b/src/lexer/err.rs @@ -1,16 +1,7 @@ use thiserror::Error; -use crate::lexer::types::TokenValue; +use crate::ast::err::ParseError; -// #[derive(Debug, Clone, PartialEq, Eq, Error)] -// pub enum ParseError { -// BlockStmt(#[from] BlockStmtError) -// } -// #[derive(Debug, Clone, PartialEq, Eq, Error)] -// pub enum BlockStmtError { -// MissingLBrace, -// MissingRBrace, -// } #[derive(Debug, Clone, PartialEq, Eq, Error)] pub enum LexingError { #[error("invalid int literal")] @@ -23,15 +14,6 @@ pub enum LexingError { UnrecognizedToken(String), } #[derive(Debug, Clone, PartialEq, Eq, Error)] -pub enum ParseError { - #[error("unexpected token {}, expect {}", .0, .1)] - UnexpectedToken(TokenValue, &'static str), - #[error("cannot combine with previous {}", .0)] - CantCombineWith(TokenValue), - #[error("expect {0} after")] - ExpectButEof(&'static str), -} -#[derive(Debug, Clone, PartialEq, Eq, Error)] pub enum FrontendError { #[error(transparent)] Lexing(#[from] LexingError), diff --git a/src/lexer/types.rs b/src/lexer/types.rs index 532ae7e..3e58734 100644 --- a/src/lexer/types.rs +++ b/src/lexer/types.rs @@ -81,7 +81,7 @@ impl std::fmt::Display for TokenValue { TokenValue::Return => write!(f, "return"), TokenValue::Break => write!(f, "break"), TokenValue::Continue => write!(f, "continue"), - TokenValue::Eof => write!(f, ""), + TokenValue::Eof => write!(f, "end of input"), TokenValue::Unrecognized => write!(f, "unrecognized"), } }