Avoid an unnecessary intermediate value in char_lit().

This makes the function more concise and easier to understand.
This commit is contained in:
Nicholas Nethercote 2016-09-12 13:03:21 +10:00
parent ea45edf0ee
commit 4c274b6aea
1 changed files with 9 additions and 14 deletions

View File

@ -287,26 +287,21 @@ pub fn char_lit(lit: &str) -> (char, isize) {
use std::char;
let mut chars = lit.chars();
let c = match (chars.next(), chars.next()) {
match (chars.next(), chars.next()) {
(Some(c), None) if c != '\\' => return (c, 1),
(Some('\\'), Some(c)) => match c {
'"' => Some('"'),
'n' => Some('\n'),
'r' => Some('\r'),
't' => Some('\t'),
'\\' => Some('\\'),
'\'' => Some('\''),
'0' => Some('\0'),
_ => { None }
'"' => return ('"', 2),
'n' => return ('\n', 2),
'r' => return ('\r', 2),
't' => return ('\t', 2),
'\\' => return ('\\', 2),
'\'' => return ('\'', 2),
'0' => return ('\0', 2),
_ => {}
},
_ => panic!("lexer accepted invalid char escape `{}`", lit)
};
match c {
Some(x) => return (x, 2),
None => { }
}
let msg = format!("lexer should have rejected a bad character escape {}", lit);
let msg2 = &msg[..];