From b4c346f51f29ab5ec2f2a1c305716b5005e7cd2e Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Tue, 20 Jul 2021 21:50:22 -0400 Subject: [PATCH] use consume_char_if_exists in more places --- src/parse/args.rs | 3 +-- src/parse/function.rs | 7 +++++-- src/parse/keyframes.rs | 6 ++---- src/parse/media.rs | 7 +++---- src/parse/mixin.rs | 9 +++------ src/parse/mod.rs | 23 ++++++++++------------- src/parse/module.rs | 3 +-- src/parse/style.rs | 4 ++-- src/parse/value/css_function.rs | 3 +-- 9 files changed, 28 insertions(+), 37 deletions(-) diff --git a/src/parse/args.rs b/src/parse/args.rs index b3fa2ac..37ec1e9 100644 --- a/src/parse/args.rs +++ b/src/parse/args.rs @@ -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); diff --git a/src/parse/function.rs b/src/parse/function.rs index 7add40d..9ad00a9 100644 --- a/src/parse/function.rs +++ b/src/parse/function.rs @@ -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))? diff --git a/src/parse/keyframes.rs b/src/parse/keyframes.rs index dbb4097..b2d6457 100644 --- a/src/parse/keyframes.rs +++ b/src/parse/keyframes.rs @@ -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('#'); diff --git a/src/parse/media.rs b/src/parse/media.rs index bd70340..ddb69fd 100644 --- a/src/parse/media.rs +++ b/src/parse/media.rs @@ -69,11 +69,11 @@ impl<'a> Parser<'a> { } fn parse_media_feature(&mut self) -> SassResult { - 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(':'); diff --git a/src/parse/mixin.rs b/src/parse/mixin.rs index 91673bd..f97ad68 100644 --- a/src/parse/mixin.rs +++ b/src/parse/mixin.rs @@ -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, diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 446f8e1..55951f2 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -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; } } diff --git a/src/parse/module.rs b/src/parse/module.rs index d980fa9..67fe53a 100644 --- a/src/parse/module.rs +++ b/src/parse/module.rs @@ -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())); } diff --git a/src/parse/style.rs b/src/parse/style.rs index 65beda7..b0a19ab 100644 --- a/src/parse/style.rs +++ b/src/parse/style.rs @@ -174,8 +174,8 @@ impl<'a> Parser<'a> { fn parse_property(&mut self, mut super_property: String) -> SassResult { 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()); diff --git a/src/parse/value/css_function.rs b/src/parse/value/css_function.rs index 207f9e3..30c4455 100644 --- a/src/parse/value/css_function.rs +++ b/src/parse/value/css_function.rs @@ -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);