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(); self.whitespace_or_comment();
if let Some(Token { kind: ':', .. }) = self.toks.peek() { if self.consume_char_if_exists(':') {
self.toks.next();
name = v.node; name = v.node;
} else { } else {
self.toks.set_cursor(start); self.toks.set_cursor(start);

View File

@ -7,7 +7,7 @@ use crate::{
error::SassResult, error::SassResult,
lexer::Lexer, lexer::Lexer,
scope::Scopes, scope::Scopes,
utils::{read_until_closing_curly_brace}, utils::read_until_closing_curly_brace,
value::{SassFunction, Value}, value::{SassFunction, Value},
}; };
@ -125,7 +125,10 @@ impl<'a> Parser<'a> {
self.scopes.exit_scope(); 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 match return_value
.pop() .pop()
.ok_or(("Function finished without @return.", self.span_before))? .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() { while let Some(tok) = self.toks.next() {
match tok.kind { match tok.kind {
'#' => { '#' => {
if let Some(Token { kind: '{', .. }) = self.toks.peek() { if self.consume_char_if_exists('{') {
self.toks.next();
name.push_str(&self.parse_interpolation_as_string()?); name.push_str(&self.parse_interpolation_as_string()?);
} else { } else {
name.push('#'); name.push('#');
@ -126,8 +125,7 @@ impl<'a> Parser<'a> {
span = span.merge(tok.pos()); span = span.merge(tok.pos());
match tok.kind { match tok.kind {
'#' => { '#' => {
if let Some(Token { kind: '{', .. }) = self.toks.peek() { if self.consume_char_if_exists('{') {
self.toks.next();
string.push_str(&self.parse_interpolation()?.to_css_string(span)?); string.push_str(&self.parse_interpolation()?.to_css_string(span)?);
} else { } else {
string.push('#'); string.push('#');

View File

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

View File

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

View File

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

View File

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

View File

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