From: Kevin Date: Tue, 21 May 2024 16:09:27 +0000 (-0400) Subject: Initial version of the ANTLR grammar X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=04588e2e1c11d4a100206807d19289957ac6a0a7;p=thirdparty%2Fjinja.git Initial version of the ANTLR grammar Specifically using ANTLR because it aligns very well with the standard grammars out there and has a lot better tooling for validating the grammars and lexers themselves. --- diff --git a/.gitignore b/.gitignore index 62c1b887..00a1e2a9 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ dist/ htmlcov/ .tox/ docs/_build/ + +.antlr/ \ No newline at end of file diff --git a/grammar/JinjaGrammar.g4 b/grammar/JinjaGrammar.g4 new file mode 100644 index 00000000..e82202d6 --- /dev/null +++ b/grammar/JinjaGrammar.g4 @@ -0,0 +1,44 @@ +parser grammar JinjaGrammar; + +options { + tokenVocab=JinjaLexer; +} + +expression + : block_statement + | inline_statement + ; + +expressions : expression*; + +block_statement_id + : STATEMENT_ID_BLOCK + | STATEMENT_ID_SET + ; + +block_statement_with_parameters + : block_statement_id + | block_statement_id + ; + +block_statement_without_parameters + : block_statement_id + ; + +block_statement_start_content + : block_statement_without_parameters + | block_statement_with_parameters + ; + +inline_statement_id + : STATEMENT_ID_IMPORT + | STATEMENT_ID_INCLUDE + | STATEMENT_ID_SET + ; + +inline_statement : STATEMENT_OPEN inline_statement_id STATEMENT_CLOSE; + +block_statement_start : STATEMENT_OPEN block_statement_id STATEMENT_CLOSE; +block_statement_end : STATEMENT_OPEN END_STATEMENT_ID_PREFIX block_statement_id STATEMENT_CLOSE; + +block_statement : block_statement_start expressions block_statement_end; \ No newline at end of file diff --git a/grammar/JinjaLexer.g4 b/grammar/JinjaLexer.g4 new file mode 100644 index 00000000..3bfdabe9 --- /dev/null +++ b/grammar/JinjaLexer.g4 @@ -0,0 +1,85 @@ +lexer grammar JinjaLexer; + +TRUE_LOWER : 'true'; +TRUE_PY : 'True'; +TRUE + : TRUE_LOWER + | TRUE_PY + ; + +FALSE_LOWER : 'false'; +FALSE_PY : 'False'; +FALSE + : FALSE_LOWER + | FALSE_PY + ; + +BOOLEAN + : TRUE + | FALSE + ; + +NONE_LOWER : 'none'; +NONE_PY : 'None'; +NONE + : NONE_LOWER + | NONE_PY + ; + +LPAR : '('; +LSQB : '['; +LBRACE : '{'; +RPAR : ')'; +RSQB : ']'; +RBRACE : '}'; +DOT : '.'; +COLON : ':'; +COMMA : ','; +SEMI : ';'; +PLUS : '+'; +MINUS : '-'; +STAR : '*'; +SLASH : '/'; +VBAR : '|'; +AMPER : '&'; +LESS : '<'; +GREATER : '>'; +EQUAL : '='; +PERCENT : '%'; +EQEQUAL : '=='; +NOTEQUAL : '!='; +LESSEQUAL : '<='; +GREATEREQUAL : '>='; +TILDE : '~'; +CIRCUMFLEX : '^'; +LEFTSHIFT : '<<'; +RIGHTSHIFT : '>>'; +DOUBLESTAR : '**'; +DOUBLESLASH : '//'; +AT : '@'; +RARROW : '->'; +ELLIPSIS : '...'; +EXCLAMATION : '!'; + +STATEMENT_OPEN : '{%'; +STATEMENT_CLOSE : '%}'; + +EXPRESSION_OPEN : '{{'; +EXPRESSION_CLOSE : '}}'; + +COMMENT_OPEN : '{#'; +COMMENT_CLOSE : '#}'; + +SP : [ \t\f]+; + +IDENTIFIER : [a-zA-Z_][a-zA-Z0-9_]*; + +// Statement identifiers for built-in statements + +STATEMENT_ID_BLOCK : 'block'; +STATEMENT_ID_IMPORT : 'import'; +STATEMENT_ID_INCLUDE : 'include'; +STATEMENT_ID_RAW : 'raw'; +STATEMENT_ID_SET : 'set'; + +END_STATEMENT_ID_PREFIX : 'end'; \ No newline at end of file