refactor lexing of at-rules

This commit is contained in:
ConnorSkees 2020-03-21 23:57:36 -04:00
parent 5914a07d22
commit c5f3936eb2
3 changed files with 12 additions and 12 deletions

View File

@ -195,20 +195,15 @@ impl<'a> Lexer<'a> {
fn lex_at_rule(&mut self) -> TokenKind { fn lex_at_rule(&mut self) -> TokenKind {
self.buf.next(); self.buf.next();
self.pos.next_char(); self.pos.next_char();
let mut string = String::with_capacity(99); if let TokenKind::Ident(s) = self.lex_ident() {
while let Some(c) = self.buf.peek() { if !s.is_empty() {
if !c.is_alphabetic() && c != &'-' && c != &'_' { TokenKind::AtRule(AtRuleKind::from(s.as_ref()))
break; } else {
TokenKind::Error("Expected identifier.".into())
} }
let tok = self } else {
.buf TokenKind::Error("Expected identifier.".into())
.next()
.expect("this is impossible because we have already peeked");
self.pos.next_char();
string.push(tok);
} }
TokenKind::AtRule(AtRuleKind::from(string.as_ref()))
} }
fn lex_forward_slash(&mut self) -> TokenKind { fn lex_forward_slash(&mut self) -> TokenKind {

View File

@ -426,6 +426,7 @@ impl<'a> StyleSheetParser<'a> {
"Base-level rules cannot contain the parent-selector-referencing character '&'.".into(), "Base-level rules cannot contain the parent-selector-referencing character '&'.".into(),
) )
} }
TokenKind::Error(e) => return Err(e.clone()),
_ => match dbg!(self.lexer.next()) { _ => match dbg!(self.lexer.next()) {
Some(Token { pos, .. }) => self.error(pos, "unexpected toplevel token"), Some(Token { pos, .. }) => self.error(pos, "unexpected toplevel token"),
_ => unsafe { std::hint::unreachable_unchecked() }, _ => unsafe { std::hint::unreachable_unchecked() },

View File

@ -11,3 +11,7 @@ error!(
ascii_control_character, ascii_control_character,
"a {color: ;}", "Error: Expected expression." "a {color: ;}", "Error: Expected expression."
); );
error!(
toplevel_invalid_atrule_ident,
"@`or $i from 1 through 3 {}", "Error: Expected identifier."
);