add helper function Value::from_vec

This commit is contained in:
ConnorSkees 2020-04-06 00:11:18 -04:00
parent 81e84536da
commit 44ff1c99b3
7 changed files with 39 additions and 90 deletions

View File

@ -68,11 +68,7 @@ impl CallArgs {
super_selector: &Selector, super_selector: &Selector,
) -> Option<SassResult<Value>> { ) -> Option<SassResult<Value>> {
match self.0.remove(&CallArg::Named(val)) { match self.0.remove(&CallArg::Named(val)) {
Some(v) => Some(Value::from_tokens( Some(v) => Some(Value::from_vec(v, scope, super_selector)),
&mut v.into_iter().peekable(),
scope,
super_selector,
)),
None => None, None => None,
} }
} }
@ -87,11 +83,7 @@ impl CallArgs {
super_selector: &Selector, super_selector: &Selector,
) -> Option<SassResult<Value>> { ) -> Option<SassResult<Value>> {
match self.0.remove(&CallArg::Positional(val)) { match self.0.remove(&CallArg::Positional(val)) {
Some(v) => Some(Value::from_tokens( Some(v) => Some(Value::from_vec(v, scope, super_selector)),
&mut v.into_iter().peekable(),
scope,
super_selector,
)),
None => None, None => None,
} }
} }
@ -105,11 +97,7 @@ impl CallArgs {
.collect::<SassResult<Vec<(usize, Vec<Token>)>>>()?; .collect::<SassResult<Vec<(usize, Vec<Token>)>>>()?;
args.sort_by(|(a1, _), (a2, _)| a1.cmp(a2)); args.sort_by(|(a1, _), (a2, _)| a1.cmp(a2));
for arg in args { for arg in args {
vals.push(Value::from_tokens( vals.push(Value::from_vec(arg.1, scope, super_selector)?);
&mut arg.1.into_iter().peekable(),
scope,
super_selector,
)?);
} }
Ok(vals) Ok(vals)
} }

View File

@ -98,18 +98,17 @@ pub(crate) fn parse_for<I: Iterator<Item = Token>>(
_ => from_toks.extend(these_toks), _ => from_toks.extend(these_toks),
} }
} }
let from = let from = match Value::from_vec(from_toks, scope, super_selector)? {
match Value::from_tokens(&mut from_toks.into_iter().peekable(), scope, super_selector)? { Value::Dimension(n, _) => match n.to_integer().to_usize() {
Value::Dimension(n, _) => match n.to_integer().to_usize() { Some(v) => v,
Some(v) => v, None => return Err(format!("{} is not a int.", n).into()),
None => return Err(format!("{} is not a int.", n).into()), },
}, v => return Err(format!("{} is not an integer.", v).into()),
v => return Err(format!("{} is not an integer.", v).into()), };
};
devour_whitespace(toks); devour_whitespace(toks);
let to_toks = read_until_open_curly_brace(toks); let to_toks = read_until_open_curly_brace(toks);
toks.next(); toks.next();
let to = match Value::from_tokens(&mut to_toks.into_iter().peekable(), scope, super_selector)? { let to = match Value::from_vec(to_toks, scope, super_selector)? {
Value::Dimension(n, _) => match n.to_integer().to_usize() { Value::Dimension(n, _) => match n.to_integer().to_usize() {
Some(v) => v, Some(v) => v,
None => return Err(format!("{} is not a int.", n).into()), None => return Err(format!("{} is not a int.", n).into()),

View File

@ -95,13 +95,7 @@ impl If {
let mut toks = Vec::new(); let mut toks = Vec::new();
let mut found_true = false; let mut found_true = false;
for branch in self.branches { for branch in self.branches {
if Value::from_tokens( if Value::from_vec(branch.cond, scope, super_selector)?.is_true()? {
&mut branch.cond.into_iter().peekable(),
scope,
super_selector,
)?
.is_true()?
{
toks = branch.toks; toks = branch.toks;
found_true = true; found_true = true;
break; break;

View File

@ -128,8 +128,8 @@ impl AtRule {
return Err("Expected \"in\".".into()); return Err("Expected \"in\".".into());
} }
devour_whitespace(toks); devour_whitespace(toks);
let iterator = match Value::from_tokens( let iterator = match Value::from_vec(
&mut read_until_open_curly_brace(toks).into_iter().peekable(), read_until_open_curly_brace(toks),
scope, scope,
super_selector, super_selector,
)? { )? {
@ -201,13 +201,7 @@ impl AtRule {
devour_whitespace(toks); devour_whitespace(toks);
while Value::from_tokens( while Value::from_vec(cond.clone(), scope, super_selector)?.is_true()? {
&mut cond.clone().into_iter().peekable(),
scope,
super_selector,
)?
.is_true()?
{
stmts.extend(eat_stmts( stmts.extend(eat_stmts(
&mut body.clone().into_iter().peekable(), &mut body.clone().into_iter().peekable(),
scope, scope,

View File

@ -71,10 +71,8 @@ impl<'a> StyleParser<'a> {
scope: &Scope, scope: &Scope,
) -> SassResult<Value> { ) -> SassResult<Value> {
devour_whitespace(toks); devour_whitespace(toks);
Value::from_tokens( Value::from_vec(
&mut read_until_semicolon_or_open_or_closing_curly_brace(toks) read_until_semicolon_or_open_or_closing_curly_brace(toks),
.into_iter()
.peekable(),
scope, scope,
self.super_selector, self.super_selector,
) )

View File

@ -65,11 +65,7 @@ pub(crate) fn parse_interpolation<I: Iterator<Item = Token>>(
scope: &Scope, scope: &Scope,
super_selector: &Selector, super_selector: &Selector,
) -> SassResult<Value> { ) -> SassResult<Value> {
let val = Value::from_tokens( let val = Value::from_vec(read_until_closing_curly_brace(toks), scope, super_selector)?;
&mut read_until_closing_curly_brace(toks).into_iter().peekable(),
scope,
super_selector,
)?;
toks.next(); toks.next();
Ok(val.eval()?.unquote()) Ok(val.eval()?.unquote())
} }
@ -323,7 +319,7 @@ pub(crate) fn eat_variable_value<I: Iterator<Item = Token>>(
if toks.peek().is_some() && toks.peek().unwrap().kind == ';' { if toks.peek().is_some() && toks.peek().unwrap().kind == ';' {
toks.next(); toks.next();
} }
let mut x = Vec::new(); let mut val_toks = Vec::new();
while let Some(tok) = raw.next() { while let Some(tok) = raw.next() {
match tok.kind { match tok.kind {
'!' => { '!' => {
@ -355,12 +351,12 @@ pub(crate) fn eat_variable_value<I: Iterator<Item = Token>>(
_ => return Err("Invalid flag name.".into()), _ => return Err("Invalid flag name.".into()),
} }
} }
_ => x.push(tok), _ => val_toks.push(tok),
} }
} }
devour_whitespace(toks); devour_whitespace(toks);
let val = Value::from_tokens(&mut x.into_iter().peekable(), scope, super_selector)?; let val = Value::from_vec(val_toks, scope, super_selector)?;
Ok(VariableDecl::new(val, default, global)) Ok(VariableDecl::new(val, default, global))
} }

View File

@ -159,22 +159,14 @@ fn parse_paren(
let paren_toks = &mut t.into_iter().peekable(); let paren_toks = &mut t.into_iter().peekable();
let mut map = SassMap::new(); let mut map = SassMap::new();
let key = Value::from_tokens( let key = Value::from_vec(read_until_char(paren_toks, ':'), scope, super_selector)?;
&mut read_until_char(paren_toks, ':').into_iter().peekable(),
scope,
super_selector,
)?;
if paren_toks.peek().is_none() { if paren_toks.peek().is_none() {
space_separated.push(Value::Paren(Box::new(key))); space_separated.push(Value::Paren(Box::new(key)));
return Ok(()); return Ok(());
} }
let val = Value::from_tokens( let val = Value::from_vec(read_until_char(paren_toks, ','), scope, super_selector)?;
&mut read_until_char(paren_toks, ',').into_iter().peekable(),
scope,
super_selector,
)?;
map.insert(key, val); map.insert(key, val);
@ -184,17 +176,9 @@ fn parse_paren(
} }
loop { loop {
let key = Value::from_tokens( let key = Value::from_vec(read_until_char(paren_toks, ':'), scope, super_selector)?;
&mut read_until_char(paren_toks, ':').into_iter().peekable(),
scope,
super_selector,
)?;
devour_whitespace(paren_toks); devour_whitespace(paren_toks);
let val = Value::from_tokens( let val = Value::from_vec(read_until_char(paren_toks, ','), scope, super_selector)?;
&mut read_until_char(paren_toks, ',').into_iter().peekable(),
scope,
super_selector,
)?;
devour_whitespace(paren_toks); devour_whitespace(paren_toks);
if map.insert(key, val) { if map.insert(key, val) {
return Err("Duplicate key.".into()); return Err("Duplicate key.".into());
@ -289,17 +273,13 @@ fn single_value<I: Iterator<Item = IntermediateValue>>(
}, },
IntermediateValue::Whitespace => unreachable!(), IntermediateValue::Whitespace => unreachable!(),
IntermediateValue::Comma => return Err("Expected expression.".into()), IntermediateValue::Comma => return Err("Expected expression.".into()),
IntermediateValue::Bracketed(t) => { IntermediateValue::Bracketed(t) => match Value::from_vec(t, scope, super_selector)? {
match Value::from_tokens(&mut t.into_iter().peekable(), scope, super_selector)? { Value::List(v, sep, Brackets::None) => Value::List(v, sep, Brackets::Bracketed),
Value::List(v, sep, Brackets::None) => Value::List(v, sep, Brackets::Bracketed), v => Value::List(vec![v], ListSeparator::Space, Brackets::Bracketed),
v => Value::List(vec![v], ListSeparator::Space, Brackets::Bracketed), },
} IntermediateValue::Paren(t) => {
Value::Paren(Box::new(Value::from_vec(t, scope, super_selector)?))
} }
IntermediateValue::Paren(t) => Value::Paren(Box::new(Value::from_tokens(
&mut t.into_iter().peekable(),
scope,
super_selector,
)?)),
}) })
} }
@ -334,14 +314,14 @@ impl Value {
)); ));
} }
} }
IntermediateValue::Bracketed(t) => space_separated.push(match Value::from_tokens( IntermediateValue::Bracketed(t) => {
&mut t.into_iter().peekable(), space_separated.push(match Value::from_vec(t, scope, super_selector)? {
scope, Value::List(v, sep, Brackets::None) => {
super_selector, Value::List(v, sep, Brackets::Bracketed)
)? { }
Value::List(v, sep, Brackets::None) => Value::List(v, sep, Brackets::Bracketed), v => Value::List(vec![v], ListSeparator::Space, Brackets::Bracketed),
v => Value::List(vec![v], ListSeparator::Space, Brackets::Bracketed), })
}), }
IntermediateValue::Paren(t) => { IntermediateValue::Paren(t) => {
parse_paren(t, scope, super_selector, &mut space_separated)?; parse_paren(t, scope, super_selector, &mut space_separated)?;
} }