lexer: allow escaped characters in char-token

This commit is contained in:
Michael Sippel 2023-10-02 02:01:15 +02:00
parent 601327f602
commit 3fc691acf2
Signed by: senvas
GPG key ID: F96CF119C34B64A6
2 changed files with 24 additions and 2 deletions

View file

@ -90,8 +90,24 @@ where It: Iterator<Item = char>
}
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('\'') => {

View file

@ -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]