From 6be28381d6081dfb3a1dc9d1ec15062b67ba1ef9 Mon Sep 17 00:00:00 2001 From: Tolmachev Igor Date: Sat, 9 May 2026 19:04:27 +0300 Subject: Implement fmt::Display for Error Replaces the todo!() stub. InvalidFloatLiteral and InvalidIntegerLiteral now also take the original string. --- compiler/src/ast/error.rs | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'compiler/src/ast/error.rs') diff --git a/compiler/src/ast/error.rs b/compiler/src/ast/error.rs index c3283f4..8117513 100644 --- a/compiler/src/ast/error.rs +++ b/compiler/src/ast/error.rs @@ -7,8 +7,8 @@ use std::{ #[derive(Clone, Debug, PartialEq, Eq)] pub enum Error { // Number - InvalidFloatLiteral(ParseFloatError), - InvalidIntegerLiteral(ParseIntError), + InvalidFloatLiteral(Rc, ParseFloatError), + InvalidIntegerLiteral(Rc, ParseIntError), // String UnclosedString(Rc), @@ -23,21 +23,22 @@ pub enum Error { RecursionLimit, } -impl From for Error { - fn from(value: ParseFloatError) -> Self { - Self::InvalidFloatLiteral(value) - } -} - -impl From for Error { - fn from(value: ParseIntError) -> Self { - Self::InvalidIntegerLiteral(value) - } -} - impl fmt::Display for Error { - fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result { - todo!() + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Error::InvalidFloatLiteral(number, err) => { + write!(f, "invalid float literal {number}: {err}") + } + Error::InvalidIntegerLiteral(number, err) => { + write!(f, "invalid integer literal {number}: {err}") + } + Error::UnclosedString(string) => write!(f, "unclosed string {string:?}"), + Error::UnexpectedEscapeChar(ch) => write!(f, "unexpected escape char {ch:?}"), + Error::UnexpectedRightPar => write!(f, "unexpected right par"), + Error::UnclosedLeftPar => write!(f, "unclosed left par"), + Error::UnexpectedEof => write!(f, "unexpected eof"), + Error::RecursionLimit => write!(f, "recursion limit"), + } } } -- cgit v1.3