]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Initial version of the ANTLR grammar
authorKevin <github@kevin-brown.com>
Tue, 21 May 2024 16:09:27 +0000 (12:09 -0400)
committerKevin <github@kevin-brown.com>
Tue, 21 May 2024 16:09:27 +0000 (12:09 -0400)
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.

.gitignore
grammar/JinjaGrammar.g4 [new file with mode: 0644]
grammar/JinjaLexer.g4 [new file with mode: 0644]

index 62c1b887d73cc6325731f0c9c97927df23a42d1d..00a1e2a9845cb9ee82e99a145411025e2b35dd05 100644 (file)
@@ -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 (file)
index 0000000..e82202d
--- /dev/null
@@ -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 (file)
index 0000000..3bfdabe
--- /dev/null
@@ -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