simplify lookahead in argument parsing

This commit is contained in:
Connor Skees 2021-07-12 03:07:07 -04:00
parent 91aaa70446
commit a4004dce4f
3 changed files with 11 additions and 63 deletions

View File

@ -7,10 +7,7 @@ use crate::{
common::QuoteKind,
error::SassResult,
scope::Scope,
utils::{
peek_ident_no_interpolation, peek_whitespace_or_comment, read_until_closing_paren,
read_until_closing_quote, read_until_newline,
},
utils::{read_until_closing_paren, read_until_closing_quote, read_until_newline},
value::Value,
Token,
};
@ -164,19 +161,20 @@ impl<'a> Parser<'a> {
}
if let Some(Token { kind: '$', pos }) = self.toks.peek() {
let start = self.toks.cursor();
span = span.merge(pos);
self.toks.advance_cursor();
self.toks.next();
let v = peek_ident_no_interpolation(self.toks, false, self.span_before)?;
let v = self.parse_identifier_no_interpolation(false)?;
peek_whitespace_or_comment(self.toks);
self.whitespace_or_comment();
if let Some(Token { kind: ':', .. }) = self.toks.peek() {
self.toks.truncate_iterator_to_cursor();
self.toks.next();
name = v.node;
} else {
self.toks.reset_cursor();
self.toks.set_cursor(start);
name.clear();
}
} else {
@ -188,14 +186,12 @@ impl<'a> Parser<'a> {
let value = self.parse_value(true, &|c| match c.peek() {
Some(Token { kind: ')', .. }) | Some(Token { kind: ',', .. }) => true,
Some(Token { kind: '.', .. }) => {
let next_is_dot = matches!(c.peek_next(), Some(Token { kind: '.', .. }));
c.reset_cursor();
let next_is_dot = matches!(c.peek_n(1), Some(Token { kind: '.', .. }));
next_is_dot
}
Some(Token { kind: '=', .. }) => {
let next_is_eq = matches!(c.peek_next(), Some(Token { kind: '=', .. }));
c.reset_cursor();
let next_is_eq = matches!(c.peek_n(1), Some(Token { kind: '=', .. }));
!next_is_eq
}
@ -290,9 +286,7 @@ impl<'a> Parser<'a> {
let right = self.parse_value(true, &|c| match c.peek() {
Some(Token { kind: ')', .. }) | Some(Token { kind: ',', .. }) => true,
Some(Token { kind: '.', .. }) => {
let next_is_dot =
matches!(c.peek_next(), Some(Token { kind: '.', .. }));
c.reset_cursor();
let next_is_dot = matches!(c.peek_n(1), Some(Token { kind: '.', .. }));
next_is_dot
}

View File

@ -1,6 +1,4 @@
use crate::{lexer::Lexer, Token};
use super::peek_until_newline;
use crate::lexer::Lexer;
pub(crate) trait IsWhitespace {
fn is_whitespace(&self) -> bool;
@ -24,41 +22,6 @@ pub(crate) fn devour_whitespace(s: &mut Lexer) -> bool {
found_whitespace
}
pub(crate) fn peek_whitespace_or_comment(s: &mut Lexer) -> bool {
let mut found_whitespace = false;
while let Some(w) = s.peek() {
match w.kind {
' ' | '\t' | '\n' => {
found_whitespace = true;
s.advance_cursor();
}
'/' => match s.peek_next() {
Some(Token { kind: '/', .. }) => {
peek_until_newline(s);
found_whitespace = true;
}
Some(Token { kind: '*', .. }) => {
found_whitespace = true;
while let Some(tok) = s.peek_next() {
match tok.kind {
'*' => {
if matches!(s.peek_next(), Some(Token { kind: '/', .. })) {
s.advance_cursor();
break;
}
}
_ => continue,
}
}
}
_ => return found_whitespace,
},
_ => return found_whitespace,
}
}
found_whitespace
}
/// Eat tokens until a newline
///
/// This exists largely to eat silent comments, "//"

View File

@ -4,15 +4,6 @@ use crate::{error::SassResult, lexer::Lexer, Token};
use super::{as_hex, hex_char_for, is_name, is_name_start};
pub(crate) fn peek_until_newline(toks: &mut Lexer) {
while let Some(tok) = toks.peek() {
if tok.kind == '\n' {
break;
}
toks.advance_cursor();
}
}
pub(crate) fn peek_escape(toks: &mut Lexer) -> SassResult<String> {
let mut value = 0;
let first = match toks.peek() {