From 3fc691acf28ceb2eeb5c2a63d9b16159fe6dbeb2 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Mon, 2 Oct 2023 02:01:15 +0200 Subject: [PATCH] lexer: allow escaped characters in char-token --- src/lexer.rs | 20 ++++++++++++++++++-- src/test/lexer.rs | 6 ++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/lexer.rs b/src/lexer.rs index d03b2f5..4644ae6 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -90,8 +90,24 @@ where It: Iterator } LexerState::Char(val) => { - // todo escape characters - *val = self.chars.next(); + *val = Some( + match self.chars.next() { + Some('\\') => { + match self.chars.next() { + Some('0') => '\0', + Some('n') => '\n', + Some('t') => '\t', + Some(c) => c, + None => { + return Some(Err(LexError::InvalidChar)); + } + } + } + Some(c) => c, + None => { + return Some(Err(LexError::InvalidChar)); + } + }); match self.chars.next() { Some('\'') => { diff --git a/src/test/lexer.rs b/src/test/lexer.rs index 7cba4db..a7ce90b 100644 --- a/src/test/lexer.rs +++ b/src/test/lexer.rs @@ -30,6 +30,12 @@ fn test_lexer_char() { assert_eq!( lex.next(), Some(Ok(LadderTypeToken::Char('x'))) ); assert_eq!( lex.next(), None ); + + assert_eq!( LadderTypeLexer::from("'\\0'".chars()).next(), Some(Ok(LadderTypeToken::Char('\0'))) ); + assert_eq!( LadderTypeLexer::from("'\\n'".chars()).next(), Some(Ok(LadderTypeToken::Char('\n'))) ); + assert_eq!( LadderTypeLexer::from("'\\t'".chars()).next(), Some(Ok(LadderTypeToken::Char('\t'))) ); + assert_eq!( LadderTypeLexer::from("'\\\''".chars()).next(), Some(Ok(LadderTypeToken::Char('\''))) ); + assert_eq!( LadderTypeLexer::from("'\\\\'".chars()).next(), Some(Ok(LadderTypeToken::Char('\\'))) ); } #[test]