from datetime import datetime\r
-from tatsu.exceptions import FailedSemantics\r
-from tatsu.util import asjson\r
-import json\r
import pprint\r
-import tatsu\r
-import sys\r
-from new_parser import parse_template\r
from jinja2.environment import Environment\r
+from jinja2.parser import Parser\r
\r
\r
with open('grammar.ebnf', 'r') as tatsu_grammar:\r
with open('test_template.jinja', 'r') as test_template:\r
template_string = test_template.read()\r
\r
- grammar_start = datetime.now()\r
-\r
- grammar = tatsu.compile(tatsu_grammar.read())\r
-\r
- grammar_end = datetime.now()\r
-\r
- parse_start = datetime.now()\r
-\r
- ast = grammar.parse(template_string, whitespace='', parseinfo=True, semantics=JinjaSemantics())\r
-\r
- parse_end = datetime.now()\r
-\r
- with open('tatsu_jinja.json', 'w') as tatsu_ast_file:\r
- json.dump(asjson(ast), tatsu_ast_file, indent=2)\r
+ env = Environment(line_statement_prefix='#', line_comment_prefix='##')\r
+ parser = Parser(env, template_string)\r
\r
new_parse_start = datetime.now()\r
\r
- new_ast = parse_template(ast)\r
+ new_ast = parser.parse()\r
\r
new_parse_end = datetime.now()\r
\r
with open('tatsu_jinja.py', 'w') as new_ast_file:\r
pprint.pprint(new_ast, indent=2, stream=new_ast_file)\r
\r
- env = Environment(line_statement_prefix='#', line_comment_prefix='##')\r
-\r
jinja_parse_start = datetime.now()\r
\r
- jinja_ast = env.parse(template_string)\r
+ jinja_ast = parser.parse_old()\r
\r
jinja_parse_end = datetime.now()\r
\r
with open('parsed_jinja.py', 'w') as jinja_ast_file:\r
pprint.pprint(jinja_ast, indent=2, stream=jinja_ast_file)\r
\r
- print("Grammar", grammar_end - grammar_start, file=sys.stderr)\r
- print("Parser", parse_end - parse_start, file=sys.stderr)\r
print("New Parser", new_parse_end - new_parse_start, file=sys.stderr)\r
print("Jinja Parser", jinja_parse_end - jinja_parse_start, file=sys.stderr)\r
+++ /dev/null
-{% from 'forms.html' import input as input_field, textarea %}\r
-{{ dict_var['single']["double"].dot |test("first" ,2_000, named=3.14)|filter | lastFilter}}\r
-{% with a='', b=a.attribute %}...{% endwith %}\r
-{% for item in dict_var.values() %}\r
- <li>{% block loop_item %}{{ item }}{% endblock %}</li>\r
-{% endfor %}\r
-{# comment contents\r
-across lines #}\r
-{% raw %}\r
- {% block %}fake content{{ fake vars }}\r
-{% endraw %}\r
-{% if False %}\r
-{{ '{{' }}\r
-{% endif %}\r
-{% for href, caption in [('index.html', 'Index'), ('about.html', 'About'),\r
- ('downloads.html', 'Downloads')] %}\r
- <li><a href="{{ href }}">{{ caption }}</a></li>\r
-{% endfor %}\r
-{% set navigation = [('index.html', 'Index'), ('about.html', 'About')] %}\r
-{% set key, value = call_something() %}\r
-{% for value in values %}\r
- {% if loop.previtem is defined and value > loop.previtem %}\r
- The value just increased!\r
- {% endif %}\r
- {{ value }}\r
- {% if loop.nextitem is defined and loop.nextitem > value %}\r
- The value will increase even more!\r
- {% endif %}\r
-{% endfor %}\r
-{%- macro textarea(name, value='', rows=10, cols=40) -%}\r
- <textarea name="{{ name }}" rows="{{ rows }}" cols="{{ cols\r
- }}">{{ value|e }}</textarea>\r
-{%- endmacro -%}\r
-{# comment contents\r
-across lines #}\r
-<ul>\r
- # for item in seq:\r
- <li>{{ item }}</li> ## this comment is ignored\r
- # endfor\r
-</ul>\r
-{% set reply | wordwrap %}\r
- You wrote:\r
- {{ message }}\r
-{% endset %}\r
-<ul class="sitemap">\r
-{%- for item in sitemap recursive %}\r
- <li><a href="{{ item.href|e }}">{{ item.title }}</a>\r
- {%- if item.children -%}\r
- <ul class="submenu">{{ loop(item.children) }}</ul>\r
- {%- endif %}</li>\r
-{%- endfor %}\r
-</ul>\r
-{% autoescape true %}\r
- Autoescaping is active within this block\r
-{% endautoescape %}\r
-\r
-{% autoescape false %}\r
- Autoescaping is inactive within this block\r
-{% endautoescape %}\r
-{% if foo.attribute is sameas false %}\r
- the foo attribute really is the `False` singleton\r
-{% endif %}\r
-<ul{{ {'class': 'my_list', 'missing': none,\r
- 'id': 'list-%d'|format(variable)}|xmlattr }}>\r
-...\r
-</ul>\r
-{{ "[{}]".format(page.title) if page.title }}\r
-{% extends layout_template if layout_template is defined else 'master.html' %}\r
-{{ "Hello " ~ name ~ "!" }}\r
-{{ 1 in [1, 2, 3] }}\r
-{{ foo is not bar }}\r
-{{ not (foo and bar) }}\r
-{{ foo not in bar }}\r
-{% from 'forms.html' import input with context %}\r
-{% include 'header.html' without context %}\r
-{% import 'forms.html' as forms %}\r
-{% include ['special_sidebar.html', 'sidebar.html'] ignore missing %}\r
-{% include "sidebar.html" ignore missing %}\r
-{% include "sidebar.html" ignore missing with context %}\r
-{% include "sidebar.html" ignore missing without context %}\r
-{% for item in seq %}\r
- {{ item }}\r
-{% else %}\r
- did not iterate\r
-{% endfor %}\r
-{% call(user) dump_users(list_of_user) %}\r
- <dl>\r
- <dl>Realname</dl>\r
- <dd>{{ user.realname|e }}</dd>\r
- <dl>Description</dl>\r
- <dd>{{ user.description }}</dd>\r
- </dl>\r
-{% endcall %}\r
-{% if kenny.sick %}\r
- Kenny is sick.\r
-{% elif kenny.dead %}\r
- You killed Kenny! You bastard!!!\r
-{% else %}\r
- Kenny looks okay --- so far\r
-{% endif %}\r
-{% for user in users if not user.hidden %}\r
- <li>{{ user.username|e }}</li>\r
-{% endfor %}\r
-{% for item in seq %}\r
- <li>{% block loop_item scoped %}{{ item }}{% endblock %}</li>\r
-{% endfor %}\r
-{% if loop.index is divisibleby 3 %}{% endif %}\r
-{% if loop.index is divisibleby(3) %}{% endif %}
\ No newline at end of file