From 8d4b4bedbedca7428bb0e8828970c043eaa4ae30 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sun, 24 May 2020 15:53:51 -0400 Subject: [PATCH] remove unwrap in peek_ident --- src/atrule/for_rule.rs | 4 ++-- src/atrule/if_rule.rs | 13 ++++--------- src/lib.rs | 2 +- src/stylesheet.rs | 12 ++++++++---- src/utils/peek_until.rs | 7 +++++-- src/utils/variables.rs | 2 +- tests/error.rs | 4 ++++ 7 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/atrule/for_rule.rs b/src/atrule/for_rule.rs index 46160c2..4c20dd9 100644 --- a/src/atrule/for_rule.rs +++ b/src/atrule/for_rule.rs @@ -105,10 +105,10 @@ pub(crate) fn parse_for>( devour_whitespace(toks); let mut from_toks = Vec::new(); let mut through = 0; - while let Some(tok) = toks.peek() { + while let Some(tok) = toks.peek().cloned() { match tok.kind { 't' | 'T' | '\\' => { - let ident = peek_ident_no_interpolation(toks, false)?; + let ident = peek_ident_no_interpolation(toks, false, tok.pos)?; match ident.node.to_ascii_lowercase().as_str() { "through" => { through = 1; diff --git a/src/atrule/if_rule.rs b/src/atrule/if_rule.rs index b24ba5c..443d406 100644 --- a/src/atrule/if_rule.rs +++ b/src/atrule/if_rule.rs @@ -49,12 +49,7 @@ impl If { Some(t) => t.pos, None => return Err(("Expected expression.", span_before).into()), }; - let init_cond = Value::from_vec( - init_cond_toks, - scope, - super_selector, - span_before, - )?; + let init_cond = Value::from_vec(init_cond_toks, scope, super_selector, span_before)?; devour_whitespace_or_comment(toks)?; let mut init_toks = read_until_closing_curly_brace(toks)?; if let Some(tok) = toks.next() { @@ -69,10 +64,10 @@ impl If { let mut else_ = Vec::new(); loop { - match toks.peek() { - Some(Token { kind: '@', .. }) => { + match toks.peek().cloned() { + Some(Token { kind: '@', pos }) => { toks.peek_forward(1); - let mut ident = peek_ident_no_interpolation(toks, false)?; + let mut ident = peek_ident_no_interpolation(toks, false, pos)?; ident.node.make_ascii_lowercase(); if ident.as_str() != "else" { toks.reset_view(); diff --git a/src/lib.rs b/src/lib.rs index 9b51d48..50382e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -287,7 +287,7 @@ pub(crate) fn eat_expr>( continue; } - let name = peek_ident_no_interpolation(toks, false)?; + let name = peek_ident_no_interpolation(toks, false, tok.pos)?; let whitespace = peek_whitespace(toks); if toks.peek().ok_or(("expected \":\".", name.span))?.kind == ':' { diff --git a/src/stylesheet.rs b/src/stylesheet.rs index 48e2365..1c778b6 100644 --- a/src/stylesheet.rs +++ b/src/stylesheet.rs @@ -190,8 +190,8 @@ impl<'a> StyleSheetParser<'a> { continue; } '$' => { - self.lexer.next(); - let name = peek_ident_no_interpolation(self.lexer, false)?; + let span_before = self.lexer.next().unwrap().pos; + let name = peek_ident_no_interpolation(self.lexer, false, span_before)?; let whitespace = peek_whitespace(self.lexer); match self.lexer.peek() { @@ -202,8 +202,12 @@ impl<'a> StyleSheetParser<'a> { .for_each(drop); devour_whitespace(self.lexer); - let VariableDecl { val, default, .. } = - eat_variable_value(self.lexer, &Scope::new(), &Selector::new(), pos)?; + let VariableDecl { val, default, .. } = eat_variable_value( + self.lexer, + &Scope::new(), + &Selector::new(), + pos, + )?; if !(default && global_var_exists(&name.node)) { insert_global_var(&name.node, val)?; diff --git a/src/utils/peek_until.rs b/src/utils/peek_until.rs index 02c3eca..ffc654a 100644 --- a/src/utils/peek_until.rs +++ b/src/utils/peek_until.rs @@ -160,9 +160,12 @@ pub(crate) fn peek_escape>( pub(crate) fn peek_ident_no_interpolation>( toks: &mut PeekMoreIterator, unit: bool, + span_before: Span, ) -> SassResult> { - // todo: panics on "$" - let mut span = toks.peek().unwrap().pos(); + let mut span = toks + .peek() + .ok_or(("Expected identifier.", span_before))? + .pos(); let mut text = String::new(); if toks.peek().unwrap().kind == '-' { toks.peek_forward(1); diff --git a/src/utils/variables.rs b/src/utils/variables.rs index ce91819..cbc7483 100644 --- a/src/utils/variables.rs +++ b/src/utils/variables.rs @@ -102,7 +102,7 @@ pub(crate) fn eat_variable_value>( return Err(("Expected identifier.", pos).into()); } // todo: it should not be possible to declare the same flag more than once - let mut ident = peek_ident_no_interpolation(toks, false)?; + let mut ident = peek_ident_no_interpolation(toks, false, pos)?; ident.node.make_ascii_lowercase(); match ident.node.as_str() { "global" => { diff --git a/tests/error.rs b/tests/error.rs index 2ba9152..00251d8 100644 --- a/tests/error.rs +++ b/tests/error.rs @@ -224,3 +224,7 @@ error!( ident_colon_closing_brace, "r:}", "Error: Expected expression." ); +error!( + dollar_sign_alone, + "$", "Error: Expected identifier." +);