simplify lookahead in @if parsing

This commit is contained in:
Connor Skees 2021-07-12 03:17:17 -04:00
parent a4004dce4f
commit dceaea6d42
3 changed files with 15 additions and 18 deletions

View File

@ -60,17 +60,14 @@ impl<'a> Parser<'a> {
loop {
self.whitespace_or_comment();
if let Some(Token { kind: '@', pos }) = self.toks.peek() {
self.toks.peek_forward(1);
let ident = peek_ident_no_interpolation(self.toks, false, pos)?;
if ident.as_str() != "else" {
self.toks.reset_cursor();
break;
}
self.toks.truncate_iterator_to_cursor();
} else {
let start = self.toks.cursor();
if !self.consume_char_if_exists('@') || !self.scan_identifier("else", false) {
self.toks.set_cursor(start);
break;
}
self.whitespace_or_comment();
if let Some(tok) = self.toks.peek() {
match tok.kind {
@ -195,9 +192,9 @@ impl<'a> Parser<'a> {
Some(..) | None => false,
})?;
let through = if self.scan_identifier("through") {
let through = if self.scan_identifier("through", true) {
1
} else if self.scan_identifier("to") {
} else if self.scan_identifier("to", true) {
0
} else {
return Err(("Expected \"to\" or \"through\".", self.span_before).into());

View File

@ -9,16 +9,16 @@ use super::Parser;
impl<'a> Parser<'a> {
/// Peeks to see if the `ident` is at the current position. If it is,
/// consume the identifier
///
/// This method is case insensitive
pub fn scan_identifier(&mut self, ident: &'static str) -> bool {
pub fn scan_identifier(&mut self, ident: &'static str, case_insensitive: bool) -> bool {
let mut peeked_identifier =
match peek_ident_no_interpolation(self.toks, false, self.span_before) {
Ok(v) => v.node,
Err(..) => return false,
};
peeked_identifier.make_ascii_lowercase();
if case_insensitive {
peeked_identifier.make_ascii_lowercase();
}
if peeked_identifier == ident {
self.toks.truncate_iterator_to_cursor();
@ -144,7 +144,7 @@ impl<'a> Parser<'a> {
} else {
buf.push_str(&ident);
if self.scan_identifier("and") {
if self.scan_identifier("and", true) {
self.whitespace_or_comment();
buf.push_str(" and ");
} else {
@ -157,7 +157,7 @@ impl<'a> Parser<'a> {
self.whitespace_or_comment();
buf.push_str(&self.parse_media_feature()?);
self.whitespace_or_comment();
if !self.scan_identifier("and") {
if !self.scan_identifier("and", true) {
break;
}
buf.push_str(" and ");

View File

@ -420,7 +420,7 @@ impl<'a> Parser<'a> {
'u' | 'U' => {
let before_url = self.toks.cursor();
if !self.scan_identifier("url") {
if !self.scan_identifier("url", true) {
buffer.push(tok.kind);
self.toks.next();
wrote_newline = false;