diff --git a/src/atrule/mod.rs b/src/atrule/mod.rs index a8e9841..572186e 100644 --- a/src/atrule/mod.rs +++ b/src/atrule/mod.rs @@ -37,7 +37,7 @@ impl AtRule { rule: &AtRuleKind, pos: Pos, toks: &mut Peekable, - scope: &Scope, + scope: &mut Scope, super_selector: &Selector, ) -> SassResult { devour_whitespace(toks); @@ -71,7 +71,7 @@ impl AtRule { AtRule::Mixin(name, Box::new(mixin)) } AtRuleKind::Function => { - let (name, func) = Function::decl_from_tokens(toks, scope)?; + let (name, func) = Function::decl_from_tokens(toks, scope.clone())?; AtRule::Function(name, Box::new(func)) } AtRuleKind::Return => { @@ -176,13 +176,12 @@ impl AtRule { devour_whitespace_or_comment(toks); - let mut scope = scope.clone(); if from < to { for i in from..(to + through) { scope.insert_var(&var, Value::Dimension(Number::from(i), Unit::None))?; stmts.extend(eat_stmts( &mut body.clone().into_iter().peekable(), - &scope, + scope, super_selector, )?); } @@ -191,7 +190,7 @@ impl AtRule { scope.insert_var(&var, Value::Dimension(Number::from(i), Unit::None))?; stmts.extend(eat_stmts( &mut body.clone().into_iter().peekable(), - &scope, + scope, super_selector, )?); } diff --git a/src/atrule/parse.rs b/src/atrule/parse.rs index 3328c5a..1fd03d4 100644 --- a/src/atrule/parse.rs +++ b/src/atrule/parse.rs @@ -7,12 +7,11 @@ use crate::{eat_expr, Expr, RuleSet, Stmt, Token}; pub(crate) fn eat_stmts>( toks: &mut Peekable, - scope: &Scope, + scope: &mut Scope, super_selector: &Selector, ) -> SassResult> { let mut stmts = Vec::new(); - let mut scope = scope.clone(); - while let Some(expr) = eat_expr(toks, &scope, super_selector)? { + while let Some(expr) = eat_expr(toks, scope, super_selector)? { match expr { Expr::AtRule(a) => stmts.push(Stmt::AtRule(a)), Expr::Style(s) => stmts.push(Stmt::Style(s)), @@ -22,7 +21,7 @@ pub(crate) fn eat_stmts>( todo!() } Expr::Selector(selector) => { - let rules = eat_stmts(toks, &scope, &super_selector.zip(&selector))?; + let rules = eat_stmts(toks, scope, &super_selector.zip(&selector))?; stmts.push(Stmt::RuleSet(RuleSet { super_selector: super_selector.clone(), selector, diff --git a/src/atrule/unknown.rs b/src/atrule/unknown.rs index 997dcf7..f542ec6 100644 --- a/src/atrule/unknown.rs +++ b/src/atrule/unknown.rs @@ -19,7 +19,7 @@ impl UnknownAtRule { pub fn from_tokens>( toks: &mut Peekable, name: &str, - scope: &Scope, + scope: &mut Scope, super_selector: &Selector, ) -> SassResult { let mut params = String::new(); diff --git a/src/function.rs b/src/function.rs index cf1f325..8b1a86e 100644 --- a/src/function.rs +++ b/src/function.rs @@ -23,7 +23,7 @@ impl Function { pub fn decl_from_tokens>( toks: &mut Peekable, - scope: &Scope, + mut scope: Scope, ) -> SassResult<(String, Function)> { let Token { kind, .. } = toks .next() @@ -38,7 +38,7 @@ impl Function { Some(Token { kind: TokenKind::Symbol(Symbol::OpenParen), .. - }) => eat_func_args(toks, scope)?, + }) => eat_func_args(toks, &scope)?, _ => return Err("expected \"(\".".into()), }; @@ -52,7 +52,7 @@ impl Function { rule, tok.pos, toks, - scope, + &mut scope, &Selector::new(), )?), TokenKind::Symbol(Symbol::CloseCurlyBrace) => nesting -= 1, diff --git a/src/lib.rs b/src/lib.rs index 2e7c686..f88c05d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -460,7 +460,7 @@ impl<'a> StyleSheetParser<'a> { pos, }) = self.lexer.next() { - match AtRule::from_tokens(rule, pos, &mut self.lexer, &self.global_scope, &Selector::new())? { + match AtRule::from_tokens(rule, pos, &mut self.lexer, &mut self.global_scope, &Selector::new())? { AtRule::Mixin(name, mixin) => { self.global_scope.insert_mixin(&name, *mixin); } @@ -542,7 +542,7 @@ impl<'a> StyleSheetParser<'a> { pub(crate) fn eat_expr>( toks: &mut Peekable, - scope: &Scope, + scope: &mut Scope, super_selector: &Selector, ) -> SassResult> { let mut values = Vec::with_capacity(5); diff --git a/src/mixin.rs b/src/mixin.rs index abcc0ce..ab89a0f 100644 --- a/src/mixin.rs +++ b/src/mixin.rs @@ -92,7 +92,7 @@ impl Mixin { fn eval(&mut self, super_selector: &Selector) -> SassResult> { let mut stmts = Vec::new(); - while let Some(expr) = eat_expr(&mut self.body, &self.scope, super_selector)? { + while let Some(expr) = eat_expr(&mut self.body, &mut self.scope, super_selector)? { match expr { Expr::AtRule(a) => stmts.push(Stmt::AtRule(a)), Expr::Style(s) => stmts.push(Stmt::Style(s)), diff --git a/tests/for.rs b/tests/for.rs index 8e4041d..f2ad251 100644 --- a/tests/for.rs +++ b/tests/for.rs @@ -38,3 +38,8 @@ test!( "a {\n @for $i from 1 to 3 {\n color: $i;\n }\n}\n", "a {\n color: 1;\n color: 2;\n}\n" ); +test!( + scope, + "a {\n $a: red;\n @for $i from 1 to 3 {\n $a: blue;\n }\n color: $a;\n}\n", + "a {\n color: blue;\n}\n" +);