lex: eat non special chars separately

let tight loop work as long as there no special characters
This commit is contained in:
Noah Hellman 2023-04-23 12:15:39 +02:00
parent 3701d282ac
commit bdab4f021b

View file

@ -163,6 +163,10 @@ impl<'s> Lexer<'s> {
' ' => Nbsp, ' ' => Nbsp,
_ => Text, _ => Text,
} }
} else {
self.eat_while(|c| !is_special(c));
if self.len > 0 {
Text
} else { } else {
match self.eat_char()? { match self.eat_char()? {
'\n' => Newline, '\n' => Newline,
@ -217,7 +221,8 @@ impl<'s> Lexer<'s> {
self.eat_char(); self.eat_char();
Close(BraceHyphen) Close(BraceHyphen)
} else { } else {
while self.peek_char() == Some('-') && self.peek_char_n(1) != Some('}') { while self.peek_char() == Some('-') && self.peek_char_n(1) != Some('}')
{
self.eat_char(); self.eat_char();
} }
Seq(Hyphen) Seq(Hyphen)
@ -251,6 +256,7 @@ impl<'s> Lexer<'s> {
_ => Text, _ => Text,
} }
}
}; };
Some(Token { Some(Token {
@ -282,6 +288,35 @@ impl<'s> Iterator for Lexer<'s> {
} }
} }
fn is_special(c: char) -> bool {
matches!(
c,
'\\' | '['
| ']'
| '('
| ')'
| '{'
| '}'
| '*'
| '^'
| '='
| '+'
| '~'
| '_'
| '\''
| '"'
| '-'
| '!'
| '<'
| '|'
| ':'
| '`'
| '.'
| '$'
| '\n'
)
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::Delimiter::*; use super::Delimiter::*;