From d3398e5f793f62de2dc32b7baa119696cf9275ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Tue, 3 Jan 2023 22:40:24 +0100 Subject: [PATCH] Errors - Added ErrorKind::Scan --- src/errors.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index 63683a9..72935d7 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -6,6 +6,8 @@ use crate::tokens::{Token, TokenType}; /// The type of an error. #[derive(Clone, Copy, Debug)] pub enum ErrorKind { + /// The error occurred while scanning the source code. + Scan, /// The error occurred while parsing the source code. Parse, /// The error occurred while trying to run the program. @@ -15,7 +17,7 @@ pub enum ErrorKind { impl fmt::Display for ErrorKind { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(match self { - ErrorKind::Parse => "Parse", + ErrorKind::Scan | ErrorKind::Parse => "Parse", ErrorKind::Runtime => "Runtime", }) } @@ -32,8 +34,21 @@ pub struct SloxError { } impl SloxError { - /// Initialize an error record. - pub fn new(kind: ErrorKind, token: &Token, message: String) -> Self { + /// Initialize a record for a scanner error. + pub fn scanner_error(line: usize, ch: Option, message: String) -> Self { + Self { + kind: ErrorKind::Scan, + line, + pos: match ch { + None => "at end of input".to_owned(), + Some(ch) => format!("near {:?}", ch) + }, + message, + } + } + + /// Initialize an error record using a token. + pub fn with_token(kind: ErrorKind, token: &Token, message: String) -> Self { Self { kind, line: token.line,