add helper function Value::from_vec
This commit is contained in:
parent
81e84536da
commit
44ff1c99b3
18
src/args.rs
18
src/args.rs
@ -68,11 +68,7 @@ impl CallArgs {
|
||||
super_selector: &Selector,
|
||||
) -> Option<SassResult<Value>> {
|
||||
match self.0.remove(&CallArg::Named(val)) {
|
||||
Some(v) => Some(Value::from_tokens(
|
||||
&mut v.into_iter().peekable(),
|
||||
scope,
|
||||
super_selector,
|
||||
)),
|
||||
Some(v) => Some(Value::from_vec(v, scope, super_selector)),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
@ -87,11 +83,7 @@ impl CallArgs {
|
||||
super_selector: &Selector,
|
||||
) -> Option<SassResult<Value>> {
|
||||
match self.0.remove(&CallArg::Positional(val)) {
|
||||
Some(v) => Some(Value::from_tokens(
|
||||
&mut v.into_iter().peekable(),
|
||||
scope,
|
||||
super_selector,
|
||||
)),
|
||||
Some(v) => Some(Value::from_vec(v, scope, super_selector)),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
@ -105,11 +97,7 @@ impl CallArgs {
|
||||
.collect::<SassResult<Vec<(usize, Vec<Token>)>>>()?;
|
||||
args.sort_by(|(a1, _), (a2, _)| a1.cmp(a2));
|
||||
for arg in args {
|
||||
vals.push(Value::from_tokens(
|
||||
&mut arg.1.into_iter().peekable(),
|
||||
scope,
|
||||
super_selector,
|
||||
)?);
|
||||
vals.push(Value::from_vec(arg.1, scope, super_selector)?);
|
||||
}
|
||||
Ok(vals)
|
||||
}
|
||||
|
@ -98,18 +98,17 @@ pub(crate) fn parse_for<I: Iterator<Item = Token>>(
|
||||
_ => from_toks.extend(these_toks),
|
||||
}
|
||||
}
|
||||
let from =
|
||||
match Value::from_tokens(&mut from_toks.into_iter().peekable(), scope, super_selector)? {
|
||||
Value::Dimension(n, _) => match n.to_integer().to_usize() {
|
||||
Some(v) => v,
|
||||
None => return Err(format!("{} is not a int.", n).into()),
|
||||
},
|
||||
v => return Err(format!("{} is not an integer.", v).into()),
|
||||
};
|
||||
let from = match Value::from_vec(from_toks, scope, super_selector)? {
|
||||
Value::Dimension(n, _) => match n.to_integer().to_usize() {
|
||||
Some(v) => v,
|
||||
None => return Err(format!("{} is not a int.", n).into()),
|
||||
},
|
||||
v => return Err(format!("{} is not an integer.", v).into()),
|
||||
};
|
||||
devour_whitespace(toks);
|
||||
let to_toks = read_until_open_curly_brace(toks);
|
||||
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() {
|
||||
Some(v) => v,
|
||||
None => return Err(format!("{} is not a int.", n).into()),
|
||||
|
@ -95,13 +95,7 @@ impl If {
|
||||
let mut toks = Vec::new();
|
||||
let mut found_true = false;
|
||||
for branch in self.branches {
|
||||
if Value::from_tokens(
|
||||
&mut branch.cond.into_iter().peekable(),
|
||||
scope,
|
||||
super_selector,
|
||||
)?
|
||||
.is_true()?
|
||||
{
|
||||
if Value::from_vec(branch.cond, scope, super_selector)?.is_true()? {
|
||||
toks = branch.toks;
|
||||
found_true = true;
|
||||
break;
|
||||
|
@ -128,8 +128,8 @@ impl AtRule {
|
||||
return Err("Expected \"in\".".into());
|
||||
}
|
||||
devour_whitespace(toks);
|
||||
let iterator = match Value::from_tokens(
|
||||
&mut read_until_open_curly_brace(toks).into_iter().peekable(),
|
||||
let iterator = match Value::from_vec(
|
||||
read_until_open_curly_brace(toks),
|
||||
scope,
|
||||
super_selector,
|
||||
)? {
|
||||
@ -201,13 +201,7 @@ impl AtRule {
|
||||
|
||||
devour_whitespace(toks);
|
||||
|
||||
while Value::from_tokens(
|
||||
&mut cond.clone().into_iter().peekable(),
|
||||
scope,
|
||||
super_selector,
|
||||
)?
|
||||
.is_true()?
|
||||
{
|
||||
while Value::from_vec(cond.clone(), scope, super_selector)?.is_true()? {
|
||||
stmts.extend(eat_stmts(
|
||||
&mut body.clone().into_iter().peekable(),
|
||||
scope,
|
||||
|
@ -71,10 +71,8 @@ impl<'a> StyleParser<'a> {
|
||||
scope: &Scope,
|
||||
) -> SassResult<Value> {
|
||||
devour_whitespace(toks);
|
||||
Value::from_tokens(
|
||||
&mut read_until_semicolon_or_open_or_closing_curly_brace(toks)
|
||||
.into_iter()
|
||||
.peekable(),
|
||||
Value::from_vec(
|
||||
read_until_semicolon_or_open_or_closing_curly_brace(toks),
|
||||
scope,
|
||||
self.super_selector,
|
||||
)
|
||||
|
12
src/utils.rs
12
src/utils.rs
@ -65,11 +65,7 @@ pub(crate) fn parse_interpolation<I: Iterator<Item = Token>>(
|
||||
scope: &Scope,
|
||||
super_selector: &Selector,
|
||||
) -> SassResult<Value> {
|
||||
let val = Value::from_tokens(
|
||||
&mut read_until_closing_curly_brace(toks).into_iter().peekable(),
|
||||
scope,
|
||||
super_selector,
|
||||
)?;
|
||||
let val = Value::from_vec(read_until_closing_curly_brace(toks), scope, super_selector)?;
|
||||
toks.next();
|
||||
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 == ';' {
|
||||
toks.next();
|
||||
}
|
||||
let mut x = Vec::new();
|
||||
let mut val_toks = Vec::new();
|
||||
while let Some(tok) = raw.next() {
|
||||
match tok.kind {
|
||||
'!' => {
|
||||
@ -355,12 +351,12 @@ pub(crate) fn eat_variable_value<I: Iterator<Item = Token>>(
|
||||
_ => return Err("Invalid flag name.".into()),
|
||||
}
|
||||
}
|
||||
_ => x.push(tok),
|
||||
_ => val_toks.push(tok),
|
||||
}
|
||||
}
|
||||
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))
|
||||
}
|
||||
|
||||
|
@ -159,22 +159,14 @@ fn parse_paren(
|
||||
let paren_toks = &mut t.into_iter().peekable();
|
||||
|
||||
let mut map = SassMap::new();
|
||||
let key = Value::from_tokens(
|
||||
&mut read_until_char(paren_toks, ':').into_iter().peekable(),
|
||||
scope,
|
||||
super_selector,
|
||||
)?;
|
||||
let key = Value::from_vec(read_until_char(paren_toks, ':'), scope, super_selector)?;
|
||||
|
||||
if paren_toks.peek().is_none() {
|
||||
space_separated.push(Value::Paren(Box::new(key)));
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let val = Value::from_tokens(
|
||||
&mut read_until_char(paren_toks, ',').into_iter().peekable(),
|
||||
scope,
|
||||
super_selector,
|
||||
)?;
|
||||
let val = Value::from_vec(read_until_char(paren_toks, ','), scope, super_selector)?;
|
||||
|
||||
map.insert(key, val);
|
||||
|
||||
@ -184,17 +176,9 @@ fn parse_paren(
|
||||
}
|
||||
|
||||
loop {
|
||||
let key = Value::from_tokens(
|
||||
&mut read_until_char(paren_toks, ':').into_iter().peekable(),
|
||||
scope,
|
||||
super_selector,
|
||||
)?;
|
||||
let key = Value::from_vec(read_until_char(paren_toks, ':'), scope, super_selector)?;
|
||||
devour_whitespace(paren_toks);
|
||||
let val = Value::from_tokens(
|
||||
&mut read_until_char(paren_toks, ',').into_iter().peekable(),
|
||||
scope,
|
||||
super_selector,
|
||||
)?;
|
||||
let val = Value::from_vec(read_until_char(paren_toks, ','), scope, super_selector)?;
|
||||
devour_whitespace(paren_toks);
|
||||
if map.insert(key, val) {
|
||||
return Err("Duplicate key.".into());
|
||||
@ -289,17 +273,13 @@ fn single_value<I: Iterator<Item = IntermediateValue>>(
|
||||
},
|
||||
IntermediateValue::Whitespace => unreachable!(),
|
||||
IntermediateValue::Comma => return Err("Expected expression.".into()),
|
||||
IntermediateValue::Bracketed(t) => {
|
||||
match Value::from_tokens(&mut t.into_iter().peekable(), scope, super_selector)? {
|
||||
Value::List(v, sep, Brackets::None) => Value::List(v, sep, Brackets::Bracketed),
|
||||
v => Value::List(vec![v], ListSeparator::Space, Brackets::Bracketed),
|
||||
}
|
||||
IntermediateValue::Bracketed(t) => match Value::from_vec(t, scope, super_selector)? {
|
||||
Value::List(v, sep, Brackets::None) => Value::List(v, sep, 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(
|
||||
&mut t.into_iter().peekable(),
|
||||
scope,
|
||||
super_selector,
|
||||
)? {
|
||||
Value::List(v, sep, Brackets::None) => Value::List(v, sep, Brackets::Bracketed),
|
||||
v => Value::List(vec![v], ListSeparator::Space, Brackets::Bracketed),
|
||||
}),
|
||||
IntermediateValue::Bracketed(t) => {
|
||||
space_separated.push(match Value::from_vec(t, scope, super_selector)? {
|
||||
Value::List(v, sep, Brackets::None) => {
|
||||
Value::List(v, sep, Brackets::Bracketed)
|
||||
}
|
||||
v => Value::List(vec![v], ListSeparator::Space, Brackets::Bracketed),
|
||||
})
|
||||
}
|
||||
IntermediateValue::Paren(t) => {
|
||||
parse_paren(t, scope, super_selector, &mut space_separated)?;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user