diff options
author | Kevin Brown <kevin@kevin-brown.com> | 2020-05-16 17:54:52 -0400 |
---|---|---|
committer | Kevin Brown <kevin@kevin-brown.com> | 2020-05-16 17:54:52 -0400 |
commit | 7e67623cbf1d1709a7cf8945d48d2b7b259387c8 (patch) | |
tree | 22b5aa89e357e7d4ce0ebbc5288e679d8043ebde | |
parent | 7151d778c45cfcd230dc949045e8bf7e3d21ec74 (diff) | |
download | jinja2-7e67623cbf1d1709a7cf8945d48d2b7b259387c8.tar.gz |
Only support implicit tuples in variable expressionsGH-1194-formal-grammar
They cause super inconsistent parsing behaviour if they show up
pretty much anywhere else because there is no difference between an
implicit tuple and a set of comma-separated parameters.
-rw-r--r-- | grammar.ebnf | 34 | ||||
-rw-r--r-- | src/jinja2/new_parser.py | 5 |
2 files changed, 26 insertions, 13 deletions
diff --git a/grammar.ebnf b/grammar.ebnf index 3fef91a..97ede84 100644 --- a/grammar.ebnf +++ b/grammar.ebnf @@ -174,7 +174,7 @@ block_parameter_value_only variable_expression
=
- variable_open type:`variable` name:conditional_expression variable_close
+ variable_open type:`variable` name:variable_expression_name variable_close
;
variable_open
@@ -199,6 +199,12 @@ variable_close_symbol "}}"
;
+variable_expression_name
+ =
+ | TUPLE_LITERAL
+ | conditional_expression
+ ;
+
variable_identifier
=
| variable_identifier_parentheses
@@ -523,7 +529,7 @@ LITERAL | BOOLEAN_LITERAL
| DICTIONARY_LITERAL
| LIST_LITERAL
- | TUPLE_LITERAL
+ | EXPLICIT_TUPLE_LITERAL
;
DICTIONARY_LITERAL
@@ -551,28 +557,32 @@ LIST_LITERAL TUPLE_LITERAL
=
- literal_type:`tuple`
- (
- | value:EXPLICIT_TUPLE_LITERAL
- | value:IMPLICIT_TUPLE_LITERAL
- | EMPTY_TUPLE_LITERAL
- )
+ | EXPLICIT_TUPLE_LITERAL
+ | IMPLICIT_TUPLE_LITERAL
+ | EMPTY_TUPLE_LITERAL
;
EXPLICIT_TUPLE_LITERAL
=
- | ( "(" {SP}* @+:LITERAL {SP}* { "," {SP}* @+:LITERAL {SP}* }+ ")" )
- | ( "(" {SP}* @+:LITERAL {SP}* "," {SP}* ")" )
+ literal_type:`tuple`
+ "(" value:TUPLE_LITERAL_CONTENTS ")"
;
IMPLICIT_TUPLE_LITERAL
=
- | ( @+:LITERAL {SP}* { "," {SP}* @+:LITERAL {SP}* }+ )
- | ( @+:LITERAL {SP}* "," {SP}* )
+ literal_type:`tuple`
+ value:TUPLE_LITERAL_CONTENTS
+ ;
+
+TUPLE_LITERAL_CONTENTS
+ =
+ | ( @+:variable_identifier {SP}* { "," {SP}* @+:variable_identifier {SP}* }+ )
+ | ( @+:variable_identifier {SP}* "," {SP}* )
;
EMPTY_TUPLE_LITERAL
=
+ literal_type:`tuple`
"(" {SP}* ")"
;
diff --git a/src/jinja2/new_parser.py b/src/jinja2/new_parser.py index 844f9ae..76051f5 100644 --- a/src/jinja2/new_parser.py +++ b/src/jinja2/new_parser.py @@ -564,6 +564,9 @@ def parse_conditional_expression(ast): if 'variable' in ast:
return parse_variable(ast)
+ if 'literal_type' in ast:
+ return parse_literal(ast)
+
if 'concatenate' in ast:
return parse_concatenate_expression(ast)
@@ -806,7 +809,7 @@ def parse_literal(ast): ast['value'] = []
items = [
- parse_literal(item) for item in ast['value']
+ parse_variable(item) for item in ast['value']
]
return nodes.Tuple(
|