lexer: allow escaped characters in char-token
This commit is contained in:
parent
601327f602
commit
3fc691acf2
2 changed files with 24 additions and 2 deletions
20
src/lexer.rs
20
src/lexer.rs
|
@ -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('\'') => {
|
||||
|
|
|
@ -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]
|
||||
|
|
Loading…
Reference in a new issue