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

View File

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

View File

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