From 40451a211aabeead91ec5da6700cc356915135ff Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Mon, 20 Apr 2020 14:38:05 -0400 Subject: [PATCH] refactor variable utils to separate file --- src/utils/mod.rs | 69 ++----------------------------------- src/utils/variables.rs | 77 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 66 deletions(-) create mode 100644 src/utils/variables.rs diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 479e9fa..5d30491 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -2,7 +2,7 @@ use std::iter::Iterator; use codemap::Spanned; -use peekmore::{PeekMore, PeekMoreIterator}; +use peekmore::PeekMoreIterator; use crate::error::SassResult; use crate::selector::Selector; @@ -11,9 +11,11 @@ use crate::{Scope, Token}; pub(crate) use chars::*; pub(crate) use strings::*; +pub(crate) use variables::*; mod chars; mod strings; +mod variables; pub(crate) trait IsWhitespace { fn is_whitespace(&self) -> bool; @@ -82,22 +84,6 @@ pub(crate) fn parse_interpolation>( }) } -pub(crate) struct VariableDecl { - pub val: Spanned, - pub default: bool, - pub global: bool, -} - -impl VariableDecl { - pub const fn new(val: Spanned, default: bool, global: bool) -> VariableDecl { - VariableDecl { - val, - default, - global, - } - } -} - // Eat tokens until an open curly brace // // Does not consume the open curly brace @@ -305,55 +291,6 @@ pub(crate) fn read_until_semicolon_or_open_or_closing_curly_brace>( - toks: &mut PeekMoreIterator, - scope: &Scope, - super_selector: &Selector, -) -> SassResult { - devour_whitespace(toks); - let mut default = false; - let mut global = false; - let mut raw = read_until_semicolon_or_closing_curly_brace(toks) - .into_iter() - .peekmore(); - if toks.peek().is_some() && toks.peek().unwrap().kind == ';' { - toks.next(); - } - let mut val_toks = Vec::new(); - while let Some(tok) = raw.next() { - match tok.kind { - '!' => { - let next = raw.next().unwrap(); - match next.kind { - 'i' => todo!("!important"), - 'g' => { - let s = eat_ident(&mut raw, scope, super_selector)?; - if s.node.to_ascii_lowercase().as_str() == "lobal" { - global = true; - } else { - return Err(("Invalid flag name.", s.span).into()); - } - } - 'd' => { - let s = eat_ident(&mut raw, scope, super_selector)?; - if s.to_ascii_lowercase().as_str() == "efault" { - default = true; - } else { - return Err(("Invalid flag name.", s.span).into()); - } - } - _ => return Err(("Invalid flag name.", next.pos()).into()), - } - } - _ => val_toks.push(tok), - } - } - devour_whitespace(toks); - - let val = Value::from_vec(val_toks, scope, super_selector)?; - Ok(VariableDecl::new(val, default, global)) -} - pub(crate) fn eat_number>( toks: &mut PeekMoreIterator, ) -> SassResult> { diff --git a/src/utils/variables.rs b/src/utils/variables.rs new file mode 100644 index 0000000..d5020dc --- /dev/null +++ b/src/utils/variables.rs @@ -0,0 +1,77 @@ +use std::iter::Iterator; + +use codemap::Spanned; + +use peekmore::{PeekMore, PeekMoreIterator}; + +use crate::error::SassResult; +use crate::selector::Selector; +use crate::value::Value; +use crate::{Scope, Token}; + +use super::*; + +pub(crate) struct VariableDecl { + pub val: Spanned, + pub default: bool, + pub global: bool, +} + +impl VariableDecl { + pub const fn new(val: Spanned, default: bool, global: bool) -> VariableDecl { + VariableDecl { + val, + default, + global, + } + } +} + +pub(crate) fn eat_variable_value>( + toks: &mut PeekMoreIterator, + scope: &Scope, + super_selector: &Selector, +) -> SassResult { + devour_whitespace(toks); + let mut default = false; + let mut global = false; + let mut raw = read_until_semicolon_or_closing_curly_brace(toks) + .into_iter() + .peekmore(); + if toks.peek().is_some() && toks.peek().unwrap().kind == ';' { + toks.next(); + } + let mut val_toks = Vec::new(); + while let Some(tok) = raw.next() { + match tok.kind { + '!' => { + let next = raw.next().unwrap(); + match next.kind { + 'i' => todo!("!important"), + 'g' => { + let s = eat_ident(&mut raw, scope, super_selector)?; + if s.node.to_ascii_lowercase().as_str() == "lobal" { + global = true; + } else { + return Err(("Invalid flag name.", s.span).into()); + } + } + 'd' => { + let s = eat_ident(&mut raw, scope, super_selector)?; + if s.to_ascii_lowercase().as_str() == "efault" { + default = true; + } else { + return Err(("Invalid flag name.", s.span).into()); + } + } + _ => return Err(("Invalid flag name.", next.pos()).into()), + } + } + _ => val_toks.push(tok), + } + } + devour_whitespace(toks); + + let val = Value::from_vec(val_toks, scope, super_selector)?; + Ok(VariableDecl::new(val, default, global)) +}