refactor lexing of at-rules
This commit is contained in:
parent
5914a07d22
commit
c5f3936eb2
19
src/lexer.rs
19
src/lexer.rs
@ -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 {
|
||||
|
@ -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() },
|
||||
|
@ -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."
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user