more liberally throw away comments alongside whitespace

This commit is contained in:
Connor Skees 2020-07-25 15:03:45 -04:00
parent 3757c39c6f
commit 7f25d526f5
3 changed files with 48 additions and 10 deletions

View File

@ -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<Token> = 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());

View File

@ -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<IntoIter<Token>>) -> bool
found_whitespace
}
pub(crate) fn peek_whitespace_or_comment(s: &mut PeekMoreIterator<IntoIter<Token>>) -> 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, "//"

View File

@ -97,7 +97,7 @@ fn peek_until_closing_quote(
Ok(t)
}
fn peek_until_newline(toks: &mut PeekMoreIterator<IntoIter<Token>>) {
pub(crate) fn peek_until_newline(toks: &mut PeekMoreIterator<IntoIter<Token>>) {
while let Some(tok) = toks.peek() {
if tok.kind == '\n' {
break;