refactor variable utils to separate file

This commit is contained in:
ConnorSkees 2020-04-20 14:38:05 -04:00
parent c121bbc1e3
commit 40451a211a
2 changed files with 80 additions and 66 deletions

View File

@ -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<I: Iterator<Item = Token>>(
})
}
pub(crate) struct VariableDecl {
pub val: Spanned<Value>,
pub default: bool,
pub global: bool,
}
impl VariableDecl {
pub const fn new(val: Spanned<Value>, 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<I: Iterator<It
t
}
pub(crate) fn eat_variable_value<I: Iterator<Item = Token>>(
toks: &mut PeekMoreIterator<I>,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<VariableDecl> {
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<I: Iterator<Item = Token>>(
toks: &mut PeekMoreIterator<I>,
) -> SassResult<Spanned<String>> {

77
src/utils/variables.rs Normal file
View File

@ -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<Value>,
pub default: bool,
pub global: bool,
}
impl VariableDecl {
pub const fn new(val: Spanned<Value>, default: bool, global: bool) -> VariableDecl {
VariableDecl {
val,
default,
global,
}
}
}
pub(crate) fn eat_variable_value<I: Iterator<Item = Token>>(
toks: &mut PeekMoreIterator<I>,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<VariableDecl> {
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))
}