Skip to content

Commit

Permalink
Inline scope tracking abstraction again
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Aug 10, 2017
1 parent 1a9a82a commit 8bda0d6
Showing 1 changed file with 6 additions and 24 deletions.
30 changes: 6 additions & 24 deletions askama_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,24 +181,6 @@ impl<'a> Generator<'a> {
self.prepare_ws(ws);
}

/* Helper methods for dealing with scope */

fn is_local(&self, var: &str) -> bool {
self.locals.contains(&var)
}

fn make_local<'s>(&mut self, var: &'a str) {
self.locals.insert(var);
}

fn push_scope(&mut self) {
self.locals.push();
}

fn pop_scope(&mut self) {
self.locals.pop();
}

/* Visitor methods for expression types */

fn visit_num_lit(&mut self, s: &str) {
Expand All @@ -210,7 +192,7 @@ impl<'a> Generator<'a> {
}

fn visit_var(&mut self, s: &str) {
if self.is_local(s) {
if self.locals.contains(s) {
self.write(s);
} else {
self.write(&format!("self.{}", s));
Expand Down Expand Up @@ -358,11 +340,11 @@ impl<'a> Generator<'a> {
fn write_loop(&mut self, ws1: &WS, var: &'a Target, iter: &Expr,
body: &'a [Node], ws2: &WS) {
self.handle_ws(ws1);
self.push_scope();
self.locals.push();
self.write("for (_loop_index, ");
let targets = self.visit_target(var);
for name in &targets {
self.make_local(name);
self.locals.insert(name);
self.write(name);
}
self.write(") in (&");
Expand All @@ -372,7 +354,7 @@ impl<'a> Generator<'a> {
self.handle(body);
self.handle_ws(ws2);
self.writeln("}");
self.pop_scope();
self.locals.pop();
}

fn write_block(&mut self, ws1: &WS, name: &str, ws2: &WS) {
Expand Down Expand Up @@ -592,8 +574,8 @@ impl<'a, T: 'a> SetChain<'a, T> where T: cmp::Eq + hash::Hash {
fn with_parent<'p>(parent: &'p SetChain<T>) -> SetChain<'p, T> {
SetChain { parent: Some(parent), scopes: vec![HashSet::new()] }
}
fn contains(&self, val: &T) -> bool {
self.scopes.iter().rev().any(|set| set.contains(val)) ||
fn contains(&self, val: T) -> bool {
self.scopes.iter().rev().any(|set| set.contains(&val)) ||
match self.parent {
Some(set) => set.contains(val),
None => false,
Expand Down

0 comments on commit 8bda0d6

Please sign in to comment.