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 {
|
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 {
|
||||||
|
@ -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() },
|
||||||
|
@ -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."
|
||||||
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user