diff --git a/src/parse/keyframes.rs b/src/parse/keyframes.rs index 38b18c7..dbb4097 100644 --- a/src/parse/keyframes.rs +++ b/src/parse/keyframes.rs @@ -5,7 +5,6 @@ use crate::{ error::SassResult, lexer::Lexer, parse::Stmt, - utils::eat_whole_number, Token, }; @@ -54,12 +53,12 @@ impl<'a, 'b> KeyframesSelectorParser<'a, 'b> { } } '0'..='9' => { - let mut num = eat_whole_number(self.parser.toks); + let mut num = self.parser.parse_whole_number(); if let Some(Token { kind: '.', .. }) = self.parser.toks.peek() { self.parser.toks.next(); num.push('.'); - num.push_str(&eat_whole_number(self.parser.toks)); + num.push_str(&self.parser.parse_whole_number()); } self.parser.expect_char('%')?; diff --git a/src/parse/value/parse.rs b/src/parse/value/parse.rs index 627886c..a2e0548 100644 --- a/src/parse/value/parse.rs +++ b/src/parse/value/parse.rs @@ -13,7 +13,7 @@ use crate::{ error::SassResult, lexer::Lexer, unit::Unit, - utils::{eat_whole_number, is_name, IsWhitespace, ParsedNumber}, + utils::{is_name, IsWhitespace, ParsedNumber}, value::{Number, SassFunction, SassMap, Value}, Token, }; @@ -363,9 +363,24 @@ impl<'a> Parser<'a> { } } + pub(crate) fn parse_whole_number(&mut self) -> String { + let mut buf = String::new(); + + while let Some(c) = self.toks.peek() { + if !c.kind.is_ascii_digit() { + break; + } + + let tok = self.toks.next().unwrap(); + buf.push(tok.kind); + } + + buf + } + fn parse_number(&mut self, predicate: Predicate<'_>) -> SassResult> { let mut span = self.toks.peek().unwrap().pos; - let mut whole = eat_whole_number(self.toks); + let mut whole = self.parse_whole_number(); if self.toks.peek().is_none() || predicate(self) { return Ok(Spanned { @@ -379,7 +394,7 @@ impl<'a> Parser<'a> { let dec_len = if next_tok.kind == '.' { self.toks.next(); - let dec = eat_whole_number(self.toks); + let dec = self.parse_whole_number(); if dec.is_empty() { return Err(("Expected digit.", next_tok.pos()).into()); } @@ -393,33 +408,42 @@ impl<'a> Parser<'a> { let mut times_ten = String::new(); let mut times_ten_is_postive = true; + if let Some(Token { kind: 'e', .. }) | Some(Token { kind: 'E', .. }) = self.toks.peek() { if let Some(tok) = self.toks.peek_next() { - if tok.kind == '-' { - self.toks.next(); - times_ten_is_postive = false; + match tok.kind { + '-' => { + self.toks.next(); + self.toks.next(); + times_ten_is_postive = false; - self.toks.next(); - times_ten = eat_whole_number(self.toks); + times_ten = self.parse_whole_number(); - if times_ten.is_empty() { - return Err(("Expected digit.", self.toks.peek().unwrap_or(tok).pos).into()); - } else if times_ten.len() > 2 { - return Err(( - "Exponent too negative.", - self.toks.peek().unwrap_or(tok).pos, - ) - .into()); + if times_ten.is_empty() { + return Err( + ("Expected digit.", self.toks.peek().unwrap_or(tok).pos).into() + ); + } else if times_ten.len() > 2 { + return Err(( + "Exponent too negative.", + self.toks.peek().unwrap_or(tok).pos, + ) + .into()); + } } - } else if matches!(tok.kind, '0'..='9') { - self.toks.next(); - times_ten = eat_whole_number(self.toks); + '0'..='9' => { + self.toks.next(); + times_ten = self.parse_whole_number(); - if times_ten.len() > 2 { - return Err( - ("Exponent too large.", self.toks.peek().unwrap_or(tok).pos).into() - ); + if times_ten.len() > 2 { + return Err(( + "Exponent too large.", + self.toks.peek().unwrap_or(tok).pos, + ) + .into()); + } } + _ => {} } } } diff --git a/src/utils/number.rs b/src/utils/number.rs index aecb6be..f3e8f7d 100644 --- a/src/utils/number.rs +++ b/src/utils/number.rs @@ -1,5 +1,3 @@ -use crate::lexer::Lexer; - #[derive(Debug)] pub(crate) struct ParsedNumber { /// The full number excluding the decimal @@ -41,15 +39,3 @@ impl ParsedNumber { } } } - -pub(crate) fn eat_whole_number(toks: &mut Lexer) -> String { - let mut buf = String::new(); - while let Some(c) = toks.peek() { - if !c.kind.is_ascii_digit() { - break; - } - let tok = toks.next().unwrap(); - buf.push(tok.kind); - } - buf -}