From 7f25d526f5466c1005a9099822d4f3776f90b823 Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Sat, 25 Jul 2020 15:03:45 -0400 Subject: [PATCH] more liberally throw away comments alongside whitespace --- src/parse/args.rs | 20 +++++++++--------- src/utils/comment_whitespace.rs | 36 +++++++++++++++++++++++++++++++++ src/utils/peek_until.rs | 2 +- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/parse/args.rs b/src/parse/args.rs index 14451db..053e160 100644 --- a/src/parse/args.rs +++ b/src/parse/args.rs @@ -7,8 +7,8 @@ use crate::{ error::SassResult, scope::Scope, utils::{ - peek_ident_no_interpolation, read_until_closing_paren, read_until_closing_quote, - read_until_closing_square_brace, + peek_ident_no_interpolation, peek_whitespace_or_comment, read_until_closing_paren, + read_until_closing_quote, read_until_closing_square_brace, }, value::Value, Token, @@ -24,7 +24,7 @@ impl<'a> Parser<'a> { None => return Err(("expected \")\".", self.span_before).into()), }; - self.whitespace(); + self.whitespace_or_comment(); while let Some(Token { kind, pos }) = self.toks.next() { let name = match kind { '$' => self.parse_identifier_no_interpolation(false)?, @@ -36,14 +36,14 @@ impl<'a> Parser<'a> { }; let mut default: Vec = Vec::new(); let mut is_variadic = false; - self.whitespace(); + self.whitespace_or_comment(); let (kind, span) = match self.toks.next() { Some(Token { kind, pos }) => (kind, pos), None => return Err(("expected \")\".", pos).into()), }; match kind { ':' => { - self.whitespace(); + self.whitespace_or_comment(); while let Some(tok) = self.toks.peek() { match &tok.kind { ',' => { @@ -81,7 +81,7 @@ impl<'a> Parser<'a> { if next.kind != '.' { return Err(("expected \".\".", next.pos()).into()); } - self.whitespace(); + self.whitespace_or_comment(); let next = self.toks.next().ok_or(("expected \")\".", next.pos()))?; if next.kind != ')' { return Err(("expected \")\".", next.pos()).into()); @@ -116,9 +116,9 @@ impl<'a> Parser<'a> { }), _ => {} } - self.whitespace(); + self.whitespace_or_comment(); } - self.whitespace(); + self.whitespace_or_comment(); // TODO: this should NOT eat the opening curly brace match self.toks.next() { Some(v) if v.kind == '{' => {} @@ -144,6 +144,8 @@ impl<'a> Parser<'a> { self.toks.next(); let v = peek_ident_no_interpolation(self.toks, false, self.span_before)?; + peek_whitespace_or_comment(self.toks); + if let Some(Token { kind: ':', .. }) = self.toks.peek() { self.toks.truncate_iterator_to_cursor(); self.toks.next(); @@ -262,7 +264,7 @@ impl<'a> Parser<'a> { ); } - self.whitespace(); + self.whitespace_or_comment(); if self.toks.peek().is_none() { return Err(("expected \")\".", span).into()); diff --git a/src/utils/comment_whitespace.rs b/src/utils/comment_whitespace.rs index fe3d9fe..f3b1a18 100644 --- a/src/utils/comment_whitespace.rs +++ b/src/utils/comment_whitespace.rs @@ -4,6 +4,8 @@ use peekmore::PeekMoreIterator; use crate::Token; +use super::peek_until_newline; + pub(crate) trait IsWhitespace { fn is_whitespace(&self) -> bool; } @@ -40,6 +42,40 @@ pub(crate) fn peek_whitespace(s: &mut PeekMoreIterator>) -> bool found_whitespace } +pub(crate) fn peek_whitespace_or_comment(s: &mut PeekMoreIterator>) -> 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_forward(1) { + 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_forward(1), Some(Token { kind: '/', .. })) { + 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 59f65b0..e81ffa3 100644 --- a/src/utils/peek_until.rs +++ b/src/utils/peek_until.rs @@ -97,7 +97,7 @@ fn peek_until_closing_quote( Ok(t) } -fn peek_until_newline(toks: &mut PeekMoreIterator>) { +pub(crate) fn peek_until_newline(toks: &mut PeekMoreIterator>) { while let Some(tok) = toks.peek() { if tok.kind == '\n' { break;