From a4004dce4f71b467aef93a44d9e48ecaa696ecee Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Mon, 12 Jul 2021 03:07:07 -0400 Subject: [PATCH] simplify lookahead in argument parsing --- src/parse/args.rs | 26 +++++++++------------- src/utils/comment_whitespace.rs | 39 +-------------------------------- src/utils/peek_until.rs | 9 -------- 3 files changed, 11 insertions(+), 63 deletions(-) diff --git a/src/parse/args.rs b/src/parse/args.rs index 277d70e..e6ad863 100644 --- a/src/parse/args.rs +++ b/src/parse/args.rs @@ -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 } diff --git a/src/utils/comment_whitespace.rs b/src/utils/comment_whitespace.rs index 671f739..f3bbfbb 100644 --- a/src/utils/comment_whitespace.rs +++ b/src/utils/comment_whitespace.rs @@ -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, "//" diff --git a/src/utils/peek_until.rs b/src/utils/peek_until.rs index 5f0bde5..8550970 100644 --- a/src/utils/peek_until.rs +++ b/src/utils/peek_until.rs @@ -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 { let mut value = 0; let first = match toks.peek() {