2020-04-23 15:22:35 -04:00
|
|
|
use codemap::{Span, Spanned};
|
|
|
|
|
|
|
|
use peekmore::{PeekMore, PeekMoreIterator};
|
|
|
|
|
2020-04-24 22:57:39 -04:00
|
|
|
use super::{ruleset_eval, AtRule};
|
2020-04-23 15:22:35 -04:00
|
|
|
|
|
|
|
use crate::error::SassResult;
|
|
|
|
use crate::scope::Scope;
|
|
|
|
use crate::selector::Selector;
|
|
|
|
use crate::utils::{
|
|
|
|
devour_whitespace, read_until_closing_curly_brace, read_until_open_curly_brace,
|
|
|
|
};
|
|
|
|
use crate::value::Value;
|
|
|
|
use crate::{Stmt, Token};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub(crate) struct While {
|
|
|
|
pub cond: Vec<Token>,
|
|
|
|
pub body: Vec<Token>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl While {
|
|
|
|
pub fn ruleset_eval(
|
|
|
|
self,
|
|
|
|
scope: &mut Scope,
|
|
|
|
super_selector: &Selector,
|
2020-04-23 18:14:42 -04:00
|
|
|
at_root: bool,
|
2020-04-23 15:22:35 -04:00
|
|
|
) -> SassResult<Vec<Spanned<Stmt>>> {
|
|
|
|
let mut stmts = Vec::new();
|
|
|
|
let mut val = Value::from_vec(self.cond.clone(), scope, super_selector)?;
|
|
|
|
let scope = &mut scope.clone();
|
|
|
|
while val.node.is_true(val.span)? {
|
2020-04-24 22:57:39 -04:00
|
|
|
ruleset_eval(
|
2020-04-23 15:22:35 -04:00
|
|
|
&mut self.body.clone().into_iter().peekmore(),
|
|
|
|
scope,
|
|
|
|
super_selector,
|
2020-04-23 18:14:42 -04:00
|
|
|
at_root,
|
2020-04-24 22:57:39 -04:00
|
|
|
&mut stmts,
|
|
|
|
)?;
|
2020-04-23 15:22:35 -04:00
|
|
|
val = Value::from_vec(self.cond.clone(), scope, super_selector)?;
|
|
|
|
}
|
|
|
|
Ok(stmts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn parse_while<I: Iterator<Item = Token>>(
|
|
|
|
toks: &mut PeekMoreIterator<I>,
|
|
|
|
span: Span,
|
|
|
|
) -> SassResult<Spanned<AtRule>> {
|
|
|
|
devour_whitespace(toks);
|
|
|
|
let cond = read_until_open_curly_brace(toks);
|
|
|
|
|
|
|
|
if cond.is_empty() {
|
|
|
|
return Err(("Expected expression.", span).into());
|
|
|
|
}
|
|
|
|
|
|
|
|
toks.next();
|
|
|
|
|
2020-04-23 18:14:42 -04:00
|
|
|
let mut body = read_until_closing_curly_brace(toks);
|
2020-04-23 15:22:35 -04:00
|
|
|
|
2020-04-23 18:14:42 -04:00
|
|
|
body.push(toks.next().unwrap());
|
2020-04-23 15:22:35 -04:00
|
|
|
|
|
|
|
devour_whitespace(toks);
|
|
|
|
Ok(Spanned {
|
|
|
|
node: AtRule::While(While { cond, body }),
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|