From 524835a5bdf2cefe5bfcac5da23925fa962dcc2d Mon Sep 17 00:00:00 2001 From: Veera Date: Sun, 28 May 2023 11:18:52 -0400 Subject: [PATCH] Fix index in error messages --- cli/src/main.rs | 16 +++++++++++----- core/src/error.rs | 8 ++++---- core/src/lexer.rs | 15 ++++++++++++--- core/src/lib.rs | 10 +++++++--- core/src/parser.rs | 2 +- test/hello.lox | 3 ++- 6 files changed, 37 insertions(+), 17 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index c712d02..62c0ebf 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -12,10 +12,16 @@ fn get_input() -> String { } fn main() { - loop { - let input = get_input(); - core::evaluate(&input); + // loop { + // let input = get_input(); + // match core::evaluate(&input) { + // Ok(()) => continue, + // Err(msg) => eprintln!("{msg}"), + // } + // } + let input = include_str!("../../test/hello.lox"); + match core::evaluate(input) { + Ok(()) => {}, + Err(msg) => eprintln!("{msg}"), } - // let input = include_str!("../../test/hello.lox"); - // core::evaluate(&input); } diff --git a/core/src/error.rs b/core/src/error.rs index 85e039c..0e9e701 100644 --- a/core/src/error.rs +++ b/core/src/error.rs @@ -16,8 +16,8 @@ impl From<(u16, u16)> for Index { } impl fmt::Display for Index { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "at line {}, column {}", self.row, self.column) + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "line {}, column {}", self.row, self.column) } } @@ -28,11 +28,11 @@ pub enum BessyError { } impl fmt::Display for BessyError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use BessyError::*; match self { UnterminatedString(span) => { - write!(f, "Lex error: Unterminated String Literal at {span}.") + write!(f, "Syntax Error: Unterminated string literal at {span}.") } Unexpected { msg, span } => write!(f, "Parse error: {msg} at {span}."), } diff --git a/core/src/lexer.rs b/core/src/lexer.rs index 2352e14..ffbffee 100644 --- a/core/src/lexer.rs +++ b/core/src/lexer.rs @@ -79,6 +79,7 @@ pub struct Lexer<'src> { cursor: Peekable>, tokens: Vec, line: u16, + consumed: u16, } impl<'src> Lexer<'src> { @@ -87,6 +88,7 @@ impl<'src> Lexer<'src> { cursor: text.char_indices().peekable(), tokens: vec![], line: 1, + consumed: 0, } } @@ -101,7 +103,11 @@ impl<'src> Lexer<'src> { ' ' | '\r' | '\t' => { self.cursor.next(); } - '\n' => self.line += 1, + '\n' => { + let (index, _) = self.cursor.next().unwrap(); + self.line += 1; + self.consumed = index as u16; + } '"' => { let token = self.scan_string()?; self.tokens.push(token); @@ -149,8 +155,10 @@ impl<'src> Lexer<'src> { } fn scan_comment(&mut self) { - for (_, ch) in self.cursor.by_ref() { + for (index, ch) in self.cursor.by_ref() { if ch == '\n' { + self.line += 1; + self.consumed = index as u16; break; } } @@ -221,8 +229,9 @@ impl<'src> Lexer<'src> { self.line, )) } else { + let column = start as u16 - self.consumed; Err(BessyError::UnterminatedString( - (start_pos as u16, self.line).into(), + (self.line, column - 2).into(), )) } } diff --git a/core/src/lib.rs b/core/src/lib.rs index 889d64b..4bb0a5c 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -4,9 +4,13 @@ mod lexer; mod parser; mod stmt; -pub fn evaluate(text: &str) { +use crate::error::BessyError; + +pub fn evaluate(text: &str) -> Result<(), BessyError> { let mut lex = lexer::Lexer::new(text); - let tokens = lex.scan().unwrap(); + let tokens = lex.scan()?; println!("Tokens: {tokens:?}"); - let mut parser = parser::Parser::new(tokens.into_iter()); + Ok(()) + // let mut parser = parser::Parser::new(tokens.into_iter()); + // parser.parse(); } diff --git a/core/src/parser.rs b/core/src/parser.rs index 56a4065..4d90d0f 100644 --- a/core/src/parser.rs +++ b/core/src/parser.rs @@ -23,7 +23,7 @@ impl> Parser { } } - pub fn parse() -> Result, BessyError> { + pub fn parse(&mut self) -> Result, BessyError> { todo!() } } diff --git a/test/hello.lox b/test/hello.lox index 2850b61..32005b4 100644 --- a/test/hello.lox +++ b/test/hello.lox @@ -1,4 +1,5 @@ ~ comment comment fun hello() { - print("hello); + print("hello" + "world" + "wat); + var a = 10; } \ No newline at end of file