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 {
self.buf.next();
self.pos.next_char();
let mut string = String::with_capacity(99);
while let Some(c) = self.buf.peek() {
if !c.is_alphabetic() && c != &'-' && c != &'_' {
break;
if let TokenKind::Ident(s) = self.lex_ident() {
if !s.is_empty() {
TokenKind::AtRule(AtRuleKind::from(s.as_ref()))
} else {
TokenKind::Error("Expected identifier.".into())
}
let tok = self
.buf
.next()
.expect("this is impossible because we have already peeked");
self.pos.next_char();
string.push(tok);
} else {
TokenKind::Error("Expected identifier.".into())
}
TokenKind::AtRule(AtRuleKind::from(string.as_ref()))
}
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(),
)
}
TokenKind::Error(e) => return Err(e.clone()),
_ => match dbg!(self.lexer.next()) {
Some(Token { pos, .. }) => self.error(pos, "unexpected toplevel token"),
_ => unsafe { std::hint::unreachable_unchecked() },

View File

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