From 58b937521f3e459089c0d475551bf9a49f930657 Mon Sep 17 00:00:00 2001 From: Tolmachev Igor Date: Fri, 8 May 2026 15:25:55 +0300 Subject: Fold unclosed string error into Token variant UnclosedString was the only error variant, making lexer::Error redundant. Also removes Result from the Iterator impl. --- compiler/src/lexer/mod.rs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'compiler/src/lexer/mod.rs') diff --git a/compiler/src/lexer/mod.rs b/compiler/src/lexer/mod.rs index 2ef4922..ff7d51d 100644 --- a/compiler/src/lexer/mod.rs +++ b/compiler/src/lexer/mod.rs @@ -1,7 +1,4 @@ -mod error; - use crate::span::{Pos, Span}; -pub use error::{Error, Result}; #[cfg(test)] mod tests; @@ -17,6 +14,7 @@ pub enum Token<'a> { Quote, Number(&'a str), String(&'a str), + UnclosedString(&'a str), Symbol(&'a str), } @@ -82,7 +80,7 @@ impl<'a> Lexer<'a> { self.next_while(|ch| !is_terminator(ch)) } - fn next_string(&mut self) -> Result<&'a str> { + fn next_string(&mut self) -> Result<&'a str, &'a str> { debug_assert_eq!(self.peek(), Some('"')); self.consume(); @@ -95,7 +93,11 @@ impl<'a> Lexer<'a> { self.consume(); return Ok(string); } - '\n' => return Err(Error::UnclosedString), + '\n' => { + let string = &self.input[start..self.cursor]; + self.consume(); + return Err(string); + } '\\' => { self.consume(); self.consume(); @@ -106,12 +108,12 @@ impl<'a> Lexer<'a> { } } - Err(Error::UnclosedString) + Err(&self.input[start..self.cursor]) } } impl<'a> Iterator for Lexer<'a> { - type Item = Span>>; + type Item = Span>; fn next(&mut self) -> Option { loop { @@ -131,15 +133,15 @@ impl<'a> Iterator for Lexer<'a> { let token = match self.peek()? { '(' => { self.consume(); - Ok(Token::LeftPar) + Token::LeftPar } ')' => { self.consume(); - Ok(Token::RightPar) + Token::RightPar } '\'' => { self.consume(); - Ok(Token::Quote) + Token::Quote } // Number @@ -147,14 +149,17 @@ impl<'a> Iterator for Lexer<'a> { || matches!(ch, '+' | '-' | '.') && self.peek_nth(1).is_some_and(|ch| ch.is_ascii_digit()) => { - Ok(Token::Number(self.next_atom())) + Token::Number(self.next_atom()) } // String - '"' => self.next_string().map(Token::String), + '"' => match self.next_string() { + Ok(string) => Token::String(string), + Err(string) => Token::UnclosedString(string), + }, // Symbol - _ => Ok(Token::Symbol(self.next_atom())), + _ => Token::Symbol(self.next_atom()), }; let end = Pos::new(self.line, self.column, self.cursor); -- cgit v1.3