From 0581e3d8bd193c20a025a73f7b21f93bd862bf6e Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Sat, 16 May 2020 17:09:24 -0400 Subject: [PATCH] Properly handle implicit tuples This adds support for implicit tuple literals and removes support for identifier tuples for now. --- grammar.ebnf | 31 +++++++++++++++++++++---------- src/jinja2/new_parser.py | 15 --------------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/grammar.ebnf b/grammar.ebnf index 18496d4a..3fef91a6 100644 --- a/grammar.ebnf +++ b/grammar.ebnf @@ -134,11 +134,6 @@ line_block_parameters @+:block_parameter { { !"\n" SP }+ @+:block_parameter }* ; -implicit_identifier_tuple - = - tuple+:IDENTIFIER { {SP}* "," {SP}* tuple+:IDENTIFIER }+ - ; - block_parameters = @+:block_parameter @@ -167,13 +162,11 @@ block_parameter_key_value block_parameter_key = - | implicit_identifier_tuple - | variable_accessor_call_parameter_key + variable_accessor_call_parameter_key ; block_parameter_value_only = - | value:implicit_identifier_tuple | value:variable_identifier_with_alias | value:variable_accessor_call_parameter_value | value:conditional_expression @@ -560,11 +553,29 @@ TUPLE_LITERAL = literal_type:`tuple` ( - | ( "(" {SP}* value+:LITERAL {SP}* { "," {SP}* value+:LITERAL {SP}* }+ ")" ) - | ( "(" {SP}* value+:LITERAL {SP}* "," {SP}* ")" ) + | value:EXPLICIT_TUPLE_LITERAL + | value:IMPLICIT_TUPLE_LITERAL + | EMPTY_TUPLE_LITERAL ) ; +EXPLICIT_TUPLE_LITERAL + = + | ( "(" {SP}* @+:LITERAL {SP}* { "," {SP}* @+:LITERAL {SP}* }+ ")" ) + | ( "(" {SP}* @+:LITERAL {SP}* "," {SP}* ")" ) + ; + +IMPLICIT_TUPLE_LITERAL + = + | ( @+:LITERAL {SP}* { "," {SP}* @+:LITERAL {SP}* }+ ) + | ( @+:LITERAL {SP}* "," {SP}* ) + ; + +EMPTY_TUPLE_LITERAL + = + "(" {SP}* ")" + ; + INTEGER_LITERAL = /[\d_]*\d+/ diff --git a/src/jinja2/new_parser.py b/src/jinja2/new_parser.py index 6d0fb901..844f9ae7 100644 --- a/src/jinja2/new_parser.py +++ b/src/jinja2/new_parser.py @@ -837,9 +837,6 @@ def parse_template(ast): return nodes.Template(parse(ast), lineno=1) def parse_variable(ast, variable_context='load'): - if 'tuple' in ast: - return parse_variable_tuple(ast, variable_context) - name = ast['variable'] if 'literal_type' in name: @@ -980,18 +977,6 @@ def parse_variable_filter(node, ast): return last_filter -def parse_variable_tuple(ast, variable_context): - identifiers = [] - - for name in ast['tuple']: - identifiers.append(nodes.Name(name, variable_context)) - - return nodes.Tuple( - identifiers, - variable_context, - lineno=lineno_from_parseinfo(ast['parseinfo']) - ) - def _parse_import_context(block_parameters): if block_parameters[-1]['value']['variable'] != 'context': return None -- 2.47.3