diff --git a/src/lex/mod.rs b/src/lex/mod.rs index 3f8f208..f96ab89 100644 --- a/src/lex/mod.rs +++ b/src/lex/mod.rs @@ -34,7 +34,7 @@ impl Iterator for Lexer<'_> { type Item = Result; fn next(&mut self) -> Option> { - self.read_white(); + self.read_whitespace(); Some(match self.cursor.next()? { ';' => Ok(self.make_token(TokenKind::Semi)), @@ -100,10 +100,14 @@ impl <'a> Lexer<'a> { let mut s = String::new(); loop { - // TODO: respect escapes match self.cursor.next() { None | Some('"') => break, + Some('\\') => match self.read_escape_seq() { + Ok(c) => s.push(c), + Err(e) => return Err(e), + }, + Some('\n') => return Err(SyntaxError { msg: String::from("Unexpected EOL"), line: self.cursor.line(), @@ -117,6 +121,19 @@ impl <'a> Lexer<'a> { Ok(self.make_token(TokenKind::StringLiteral(s))) } + fn read_escape_seq(&mut self) -> Result { + match self.cursor.next() { + Some('\\') => Ok('\\'), + Some('n') => Ok('\n'), + Some('"') => Ok('"'), + _ => Err(SyntaxError { + msg: String::from("Illegal escape sequence"), + line: self.token_line, + col: self.token_col, + }) + } + } + fn read_int_rest(&mut self, first: char) -> Token { let mut s = String::from(first); @@ -135,7 +152,7 @@ impl <'a> Lexer<'a> { )) } - fn read_white(&mut self) -> Option { + fn read_whitespace(&mut self) -> Option { let ws = self.cursor.next_until(|c| !is_whitespace(c)); if ws.len() > 0 {