Commits

tomo cocoa committed 0d20f34

update

Comments (0)

Files changed (3)

 
   <dependencies>
     <dependency>
+      <groupId>com.google.guava</groupId>
+      <artifactId>guava</artifactId>
+      <version>11.0.2</version>
+    </dependency>
+    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>3.8.1</version>

src/main/java/net/elliptium/jinjava2/parser/Parser.java

+package net.elliptium.jinjava2.parser;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Stack;
+import java.util.List;
+import java.util.ArrayList;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSet.Builder;
+import com.google.common.base.Joiner;
+
+
+public class Parser {
+
+    // may be enum
+    private static final ImmutableSet statement_keywords =
+        new ImmutableSet.Builder<String>().add("for", "if", "block", "extends", "print",
+                                           "macro", "include", "from", "import", "set").build();
+
+    private static final ImmutableSet compare_operators =
+        new ImmutableSet.Builder<String>().add("eq", "ne", "lt", "lteq", "gt", "gteq").build();
+
+    public Object environment;
+    public Object stream;
+    public Object name;
+    public boolean closed;
+    public Map extensions;
+    private Integer last_identifier;
+    private Stack tag_stack;
+    private Stack end_token_stack;
+
+    public Parser(Object environment, Object source) {
+        this(environment, source, null, null, null);
+    }
+
+    public Parser(Object environment, Object source, Object name) {
+        this(environment, source, name, null, null);   
+    }
+
+    public Parser(Object environment, Object source, Object name,
+                  Object filename) {
+        this(environment, source, name, filename, null);
+    }
+
+    public Parser(Object environment, Object source, Object name,
+                  Object filename, Object state) {
+        this.environment = environment;
+        this.stream = environment._tokenize(source, name, filename, state); /* now working */
+        this.name = name;
+        this.closed = false;
+        this.extensions = new HashMap();
+        for (Object extension: environment.iter_extensions()) {
+            for (Object tag: extension.tags) {
+                this.extensions.put(tag, extension.parse);
+            }
+        }
+        this.last_identifier = 0;
+        this.tag_stack = new Stack();
+        this.end_token_stack = new Stack();
+    }
+
+    public void fail(Object msg, Object lineno, Object exc) {
+        if (lineno == null) {
+            lineno = this.stream.current.lineno;
+        }
+        Class<?>[] paramTypes = {msg.getClass(), lineno.getClass(),
+                                 this.name.getClass(), this.filename.getClass()};
+        Constructor<exc> constructor = exc.getConstructor(paramTypes);
+        Object[] params = {msg, lineno, this.name, this.filename};
+        throw constructor.newInstance(params);
+    }
+
+    private void fail_ut_eof(Object name, Object end_token_stack, Object lineno) {
+        List expected = new ArrayList();
+        for (Object exprs: end_token_stack) {
+            expected.addAll(/* implement map function with Guava */);
+        }
+        String currentry_looking;
+        if (end_token_stack != null && end_token_stack.size() != 0 /* type check! */) {
+            currentry_looking = Joiner.on(" or ").join(/* ??? */);
+        } else {
+            currentry_looking = "";
+        }
+
+        /* TBI */
+    }
+
+    public void fail_unknown_tag(Object name, Object lineno) {
+    }
+
+    public void fail_eof(Object end_tokens, Object lineno) {}
+
+    public Boolean is_tuple_end(Object extra_end_rules) {}
+
+    public Object free_identifier(Object lineno) {}
+
+    public void parse_statement() {}
+
+    public Object parse_statements(Object end_tokens, Object drop_needle) {}
+
+    public Object parse_set() {}
+
+    public Object parse_for() {}
+
+    public Object parse_if() {}
+
+    public Object parse_block() {}
+
+    public Object parse_extends() {}
+
+    public Object parse_import_context() {}
+
+    public Object parse_include() {}
+
+    public Object parse_import() {}
+
+    public Object parse_from() {}
+
+    private Object parse_context() {}
+
+    public Object parse_signature() {}
+
+    public Object parse_call_block() {}
+
+    public Object parse_filter_block() {}
+
+    public Object parse_macro() {}
+
+    public Object parse_print() {}
+
+    public Object parse_assign_target(Boolean with_tuple, Boolean name_only,
+                                      Object extra_end_rules) {}
+
+    public Object parse_expression(Boolean with_condexpr) {}
+
+    public Object parse_or() {}
+
+    public Object parse_and() {}
+
+    public Object parse_not() {}
+
+    public Object parse_compare() {}
+
+    public Object parse_add() {}
+
+    public Object parse_sub() {}
+
+    public Object parse_concat() {}
+
+    public Object parse_mul() {}
+
+    public Object parse_div() {}
+
+    public Object parse_floordiv() {}
+
+    public Object parse_mod() {}
+
+    public Object parse_pow() {}
+
+    public Object parse_unary() {}
+
+    public Object parse_primary() {}
+
+    public Object parse_tuple(Boolean simplified, Boolean with_condexpr,
+                              Object extra_end_rules, Boolean explicit_parentheses) {}
+
+    public Object parse_list() {}
+
+    public Object parse_dict() {}
+
+    public Object parse_postfix(Object node) {}
+
+    public Object parse_filter_expr(Object node) {}
+
+    public Object parse_subscript(Object node) {}
+
+    public Object parse_subscribed() {}
+
+    public Object parse_call(Object node) {}
+
+    public Object parse_filter(Object node, Boolean start_inline) {}
+
+    public Object parse_test(Object node) {}
+
+    public Object subparse(Object end_tokens) {}
+
+    public Object flush_data() {}
+
+    public Object parse() {}
+
+}

src/main/java/net/elliptium/jinjava2/parser/environment/Environment.java

+package net.elliptium.jinjava2.environment;
+
+public class Environment {
+    public static Boolean sandboxed = false;
+    public static Object linked_to = null;
+    public static Boolean shared = false;
+    public static Object exception_handler = null;
+    public static Object exception_formatter = null;
+
+    public Environment(String block_start_string,
+                       String block_end_string,
+                       String variable_start_string,
+                       String variable_end_string,
+                       String comment_start_string,
+                       String comment_end_string,
+                       Object line_statement_prefix,
+                       Object line_comment_prefix,
+                       Boolean trim_blocks,
+                       String newline_sequence,
+                       List extensions,
+                       Boolean optimized,
+                       Object undifined,
+                       Object finalize,
+                       Boolean autoescape,
+                       Object loader,
+                       Integer cache_size,
+                       Boolean auto_reload,
+                       Object bytecode_cache) {
+        // lexer / parser information
+        this.block_start_string = block_start_string;
+        this.block_end_string = block_end_string;
+        this.variable_start_string = variable_start_string;
+        this.variable_end_string = variable_end_string;
+        this.comment_start_string = comment_start_string;
+        this.comment_end_string = comment_end_string;
+        this.line_statement_prefix = line_statement_prefix;
+        this.line_comment_prefix = line_comment_prefix;
+        this.trim_blocks = trim_blocks;
+        this.newline_sequence = newline_sequence;
+
+        // runtime information
+        this.undifined = undifined;
+        this.optimized = optimized;
+        this.finalize = finalize;
+        this.autoescape = autoescape;
+
+        // defaults
+        this.filters = DEFAULT_FILTERS; // TODO copy()
+        this.tests = DEFAULT_TESTS; // TODO copy()
+        this.globals = DEFAULT_NAMESPACE; // TODO copy()
+
+        // set the loader provided
+        this.loader = loader;
+        this.bytecode_cache = null;
+        this.cache = create_cache(cache_size);
+        this.bytecode_cache = bytecode_cache;
+        this.auto_reload = auto_reload;
+
+        // load extensions
+        this.extensions = load_extensions(this, extensions);
+
+        environment_sanity_check(this);
+    }
+
+    public void add_extension(extension) {
+        this.extensions.put(load_extensions(this, Lists.newArrayList(extension)));
+    }
+
+    public void extend(Object... attributes) {
+        for (Map.Entry entry: attributes.entrySet()) {
+            // ... __dict__ property needed?
+        }
+    }
+
+    public Object overlay() {
+    }
+}