Skip to content

Commit

Permalink
Fix index in error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
veera-sivarajan committed May 28, 2023
1 parent 19bc547 commit 524835a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 17 deletions.
16 changes: 11 additions & 5 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
8 changes: 4 additions & 4 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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}."),
}
Expand Down
15 changes: 12 additions & 3 deletions core/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub struct Lexer<'src> {
cursor: Peekable<CharIndices<'src>>,
tokens: Vec<Token>,
line: u16,
consumed: u16,
}

impl<'src> Lexer<'src> {
Expand All @@ -87,6 +88,7 @@ impl<'src> Lexer<'src> {
cursor: text.char_indices().peekable(),
tokens: vec![],
line: 1,
consumed: 0,
}
}

Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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(),
))
}
}
Expand Down
10 changes: 7 additions & 3 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<T: Iterator<Item = Token>> Parser<T> {
}
}

pub fn parse() -> Result<Vec<Stmt>, BessyError> {
pub fn parse(&mut self) -> Result<Vec<Stmt>, BessyError> {
todo!()
}
}
3 changes: 2 additions & 1 deletion test/hello.lox
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
~ comment comment
fun hello() {
print("hello);
print("hello" + "world" + "wat);
var a = 10;
}

0 comments on commit 524835a

Please sign in to comment.