From: Kevin Brown Date: Fri, 8 May 2020 09:12:26 +0000 (-0400) Subject: Initial commit X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=db4ad51426a9b6f4f93c80113baf2e6e179aa865;p=thirdparty%2Fjinja.git Initial commit --- diff --git a/tatsu_grammar.txt b/tatsu_grammar.txt new file mode 100644 index 00000000..56703ea1 --- /dev/null +++ b/tatsu_grammar.txt @@ -0,0 +1,115 @@ +start + = + expression + $ + ; + +expression + = + { !matching_expression CHAR }* &matching_expression + ; + +matching_expression + = + raw_block_expression | block_expression | variable_expression | comment_expression + ; + +raw_block_expression + = + "{% raw %}" + { !raw_block_end CHAR }* + raw_block_end + ; + +raw_block_end + = + "{% endraw %}" + ; + +block_expression + = + block_start {SP}* expression {SP}* block_end + #| block_start + ; + +block_start + = + block_open !("end") IDENTIFIER {block_parameter}* block_close + ; + +block_end + = + block_open "end" IDENTIFIER block_close + ; + +block_open + = + @:"{%" {SP}* + ; +block_close + = + {SP}* @:"%}" + ; + +block_parameter + = + {SP}+ @:(IDENTIFIER "=" block_parameter_value) + | {SP}+ @:block_parameter_value + ; +block_parameter_value + = + {ALPHA}+ + ; + +variable_expression + = + variable_open IDENTIFIER { !variable_close variable_filter }* variable_close + ; +variable_open + = + @:"{{" {SP}* + ; +variable_close + = + {SP}* @:"}}" + ; + +variable_filter + = + {SP}* "|" {SP}* @:filter + ; +filter = @+:IDENTIFIER [@+:filter_parameters]; +filter_parameters = "(" [@+:filter_parameter {"," {SP}* @+:filter_parameter}*] ")"; +filter_parameter = {CHAR}* ; + +comment_expression = comment_open comment comment_close ; +comment_open = "{#" ; +comment_close = "#}" ; +comment = {!comment_close CHAR}* ; + +content = {CHAR}* ; + +IDENTIFIER + = + /[a-zA-Z]+/ + ; + +ALPHA + = + /[a-zA-Z]/ + ; + +DIGIT + = + /[0-9]/ + ; + +SP + = + /\s/ + ; + +CHAR + = + ?'.' | ?'\s' + ; \ No newline at end of file diff --git a/test_tatsu.py b/test_tatsu.py new file mode 100644 index 00000000..411ce60f --- /dev/null +++ b/test_tatsu.py @@ -0,0 +1,10 @@ +from tatsu.util import asjson +import json +import tatsu + + +with open('tatsu_grammar.txt', 'r') as tatsu_grammar: + with open('test_template.jinja', 'r') as test_template: + ast = tatsu.parse(tatsu_grammar.read(), test_template.read(), whitespace='') + + print(json.dumps(asjson(ast), indent=4)) \ No newline at end of file diff --git a/test_template.jinja b/test_template.jinja new file mode 100644 index 00000000..d7858877 --- /dev/null +++ b/test_template.jinja @@ -0,0 +1,8 @@ +{%block single key=val param=value %} +test {{var}} +{%endblock%} +{# comment contents +across lines #} +{% raw %} +{% block %}fake content{{ fake vars }} +{% endraw %} \ No newline at end of file