From 11f5bc0c5a8bcfb6ac8fef8abc33297c4addc81b Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 5 Jun 2026 11:28:07 -0700 Subject: Started on the parser. Thinking about some tweaks. - Creating a LookaheadIterator as a general class that could be used. - Wrap symbols in a symbol struct that tracks symbol start and end position for error reporting. - Make a struct for position in a file as well. --- src/lexer.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/lexer.rs') diff --git a/src/lexer.rs b/src/lexer.rs index e80e461..94825b2 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -2,7 +2,12 @@ use std::iter::Iterator; //use core::error::Error; use std::str::CharIndices; -#[derive(Debug)] +#[derive(Debug,Copy,Clone)] +pub enum ErrorType { + UnexpectedChar(char), +} + +#[derive(Debug,Copy,Clone)] pub enum Symbol<'a> { StartFlat, StartPoint, @@ -15,7 +20,7 @@ pub enum Symbol<'a> { Token(&'a str), Literal(&'a str), Text(&'a str), - Error{ line: u32, row: u32, what: String }, + Error{ line: u32, row: u32, what: ErrorType }, EOS, } @@ -104,7 +109,7 @@ impl<'a> Lexer<'a> { } } - fn error( &self, what: String ) -> Option> { + fn error( &self, what: ErrorType ) -> Option> { Some(Symbol::Error { line: self.line, row: self.row, @@ -162,6 +167,7 @@ impl<'a> Lexer<'a> { return self.parse_literal_str(); } Some('=') => { + self.next(); return Some(Symbol::Equals); } _ => {} @@ -186,7 +192,7 @@ impl<'a> Lexer<'a> { fn parse_literal_str(&mut self) -> Option> { if let Some(chr) = self.cur() && chr != '"' { - return self.error(format!("Expected '\"' but found '{}'", chr)); + return self.error( ErrorType::UnexpectedChar(chr) ); } let start = self.peek_index(); while self.next().is_some_and(|chr| chr != '"') { } -- cgit v1.2.3