use consume_char_if_exists in more places

This commit is contained in:
Connor Skees 2021-07-20 21:50:22 -04:00
parent 7979158465
commit b4c346f51f
9 changed files with 28 additions and 37 deletions

View File

@ -170,8 +170,7 @@ impl<'a> Parser<'a> {
self.whitespace_or_comment();
if let Some(Token { kind: ':', .. }) = self.toks.peek() {
self.toks.next();
if self.consume_char_if_exists(':') {
name = v.node;
} else {
self.toks.set_cursor(start);

View File

@ -7,7 +7,7 @@ use crate::{
error::SassResult,
lexer::Lexer,
scope::Scopes,
utils::{read_until_closing_curly_brace},
utils::read_until_closing_curly_brace,
value::{SassFunction, Value},
};
@ -125,7 +125,10 @@ impl<'a> Parser<'a> {
self.scopes.exit_scope();
}
debug_assert!(return_value.len() <= 1);
debug_assert!(
return_value.len() <= 1,
"we expect there to be only one return value"
);
match return_value
.pop()
.ok_or(("Function finished without @return.", self.span_before))?

View File

@ -88,8 +88,7 @@ impl<'a> Parser<'a> {
while let Some(tok) = self.toks.next() {
match tok.kind {
'#' => {
if let Some(Token { kind: '{', .. }) = self.toks.peek() {
self.toks.next();
if self.consume_char_if_exists('{') {
name.push_str(&self.parse_interpolation_as_string()?);
} else {
name.push('#');
@ -126,8 +125,7 @@ impl<'a> Parser<'a> {
span = span.merge(tok.pos());
match tok.kind {
'#' => {
if let Some(Token { kind: '{', .. }) = self.toks.peek() {
self.toks.next();
if self.consume_char_if_exists('{') {
string.push_str(&self.parse_interpolation()?.to_css_string(span)?);
} else {
string.push('#');

View File

@ -69,11 +69,11 @@ impl<'a> Parser<'a> {
}
fn parse_media_feature(&mut self) -> SassResult<String> {
if let Some(Token { kind: '#', .. }) = self.toks.peek() {
self.toks.next();
if self.consume_char_if_exists('#') {
self.expect_char('{')?;
return Ok(self.parse_interpolation_as_string()?.into_owned());
}
let mut buf = String::with_capacity(2);
self.expect_char('(')?;
buf.push('(');
@ -81,8 +81,7 @@ impl<'a> Parser<'a> {
buf.push_str(&self.expression_until_comparison()?);
if let Some(Token { kind: ':', .. }) = self.toks.peek() {
self.toks.next();
if self.consume_char_if_exists(':') {
self.whitespace_or_comment();
buf.push(':');

View File

@ -73,9 +73,7 @@ impl<'a> Parser<'a> {
self.whitespace_or_comment();
let name = self.parse_identifier()?.map_node(Into::into);
let mixin = if let Some(Token { kind: '.', .. }) = self.toks.peek() {
self.toks.next();
let mixin = if self.consume_char_if_exists('.') {
let module = name;
let name = self.parse_identifier()?.map_node(Into::into);
@ -88,8 +86,7 @@ impl<'a> Parser<'a> {
self.whitespace_or_comment();
let args = if let Some(Token { kind: '(', .. }) = self.toks.peek() {
self.toks.next();
let args = if self.consume_char_if_exists('(') {
self.parse_call_args()?
} else {
CallArgs::new(name.span)
@ -132,7 +129,7 @@ impl<'a> Parser<'a> {
};
self.consume_char_if_exists(';');
let UserDefinedMixin {
body,
args: fn_args,

View File

@ -131,6 +131,7 @@ impl<'a> Parser<'a> {
return true;
}
}
false
}
@ -208,10 +209,9 @@ impl<'a> Parser<'a> {
span,
} = self.parse_value(false, &|_| false)?;
span.merge(kind_string.span);
if let Some(Token { kind: ';', pos }) = self.toks.peek() {
kind_string.span.merge(pos);
self.toks.next();
}
self.consume_char_if_exists(';');
self.warn(&Spanned {
node: message.to_css_string(span)?,
span,
@ -223,10 +223,9 @@ impl<'a> Parser<'a> {
span,
} = self.parse_value(false, &|_| false)?;
span.merge(kind_string.span);
if let Some(Token { kind: ';', pos }) = self.toks.peek() {
kind_string.span.merge(pos);
self.toks.next();
}
self.consume_char_if_exists(';');
self.debug(&Spanned {
node: message.inspect(span)?,
span,
@ -248,7 +247,7 @@ impl<'a> Parser<'a> {
let val = self.parse_value(false, &|_| false)?;
self.consume_char_if_exists(';');
if !val.node.is_quoted_string() {
return Err(("Expected string.", val.span).into());
}
@ -409,8 +408,7 @@ impl<'a> Parser<'a> {
span = span.merge(pos);
match kind {
'#' => {
if let Some(Token { kind: '{', .. }) = self.toks.peek() {
self.toks.next();
if self.consume_char_if_exists('{') {
string.push_str(&self.parse_interpolation()?.to_css_string(span)?);
} else {
string.push('#');
@ -594,8 +592,7 @@ impl<'a> Parser<'a> {
#[allow(clippy::while_let_on_iterator)]
while let Some(tok) = self.toks.next() {
if tok.kind == '*' {
if let Some(Token { kind: '/', .. }) = self.toks.peek() {
self.toks.next();
if self.consume_char_if_exists('/') {
break;
}
}

View File

@ -35,8 +35,7 @@ impl<'a> Parser<'a> {
self.whitespace_or_comment();
if let Some(Token { kind: '*', .. }) = self.toks.peek() {
self.toks.next();
if self.consume_char_if_exists('*') {
return Ok(Some('*'.to_string()));
}

View File

@ -174,8 +174,8 @@ impl<'a> Parser<'a> {
fn parse_property(&mut self, mut super_property: String) -> SassResult<String> {
let property = self.parse_identifier()?;
self.whitespace_or_comment();
if let Some(Token { kind: ':', .. }) = self.toks.peek() {
self.toks.next();
// todo: expect_char(':')?;
if self.consume_char_if_exists(':') {
self.whitespace_or_comment();
} else {
return Err(("Expected \":\".", property.span).into());

View File

@ -146,8 +146,7 @@ impl<'a> Parser<'a> {
}
'#' => {
self.toks.next();
if let Some(Token { kind: '{', .. }) = self.toks.peek() {
self.toks.next();
if self.consume_char_if_exists('{') {
let interpolation = self.parse_interpolation_as_string()?;
buf.push_str(&interpolation);