Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2d3b986

Browse files
committed
Disambiguate the grammar for backtick.
The old syntax suggested that a trailing comma was OK inside backticks, but in fact (due to ideosyncrasies of pgen) it was not. Fix the grammar to avoid the ambiguity. Fred: you may want to update the refman.
1 parent a0a6c5a commit 2d3b986

5 files changed

Lines changed: 93 additions & 59 deletions

File tree

Grammar/Grammar

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ arith_expr: term (('+'|'-') term)*
8080
term: factor (('*'|'/'|'%'|'//') factor)*
8181
factor: ('+'|'-'|'~') factor | power
8282
power: atom trailer* ['**' factor]
83-
atom: '(' [testlist] ')' | '[' [listmaker] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
83+
atom: '(' [testlist] ')' | '[' [listmaker] ']' | '{' [dictmaker] '}' | '`' testlist1 '`' | NAME | NUMBER | STRING+
8484
listmaker: test ( list_for | (',' test)* [','] )
8585
lambdef: 'lambda' [varargslist] ':' test
8686
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
@@ -100,3 +100,5 @@ argument: [test '='] test # Really [keyword '='] test
100100
list_iter: list_for | list_if
101101
list_for: 'for' exprlist 'in' testlist_safe [list_iter]
102102
list_if: 'if' test [list_iter]
103+
104+
testlist1: test (',' test)*

Include/graminit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@
6464
#define list_iter 319
6565
#define list_for 320
6666
#define list_if 321
67+
#define testlist1 322

Modules/parsermodule.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,7 @@ VALIDATER(subscriptlist); VALIDATER(sliceop);
844844
VALIDATER(exprlist); VALIDATER(dictmaker);
845845
VALIDATER(arglist); VALIDATER(argument);
846846
VALIDATER(listmaker); VALIDATER(yield_stmt);
847+
VALIDATER(testlist1);
847848

848849
#undef VALIDATER
849850

@@ -1056,6 +1057,14 @@ validate_testlist(node *tree)
10561057
}
10571058

10581059

1060+
static int
1061+
validate_testlist1(node *tree)
1062+
{
1063+
return (validate_repeating_list(tree, testlist1,
1064+
validate_test, "testlist1"));
1065+
}
1066+
1067+
10591068
static int
10601069
validate_testlist_safe(node *tree)
10611070
{
@@ -2185,7 +2194,7 @@ validate_atom(node *tree)
21852194
break;
21862195
case BACKQUOTE:
21872196
res = ((nch == 3)
2188-
&& validate_testlist(CHILD(tree, 1))
2197+
&& validate_testlist1(CHILD(tree, 1))
21892198
&& validate_ntype(CHILD(tree, 2), BACKQUOTE));
21902199
break;
21912200
case NAME:
@@ -2671,6 +2680,9 @@ validate_node(node *tree)
26712680
case testlist:
26722681
res = validate_testlist(tree);
26732682
break;
2683+
case testlist1:
2684+
res = validate_testlist1(tree);
2685+
break;
26742686
case test:
26752687
res = validate_test(tree);
26762688
break;

Python/compile.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2474,6 +2474,7 @@ com_assign(struct compiling *c, node *n, int assigning, node *augn)
24742474

24752475
case exprlist:
24762476
case testlist:
2477+
case testlist1:
24772478
if (NCH(n) > 1) {
24782479
if (assigning > OP_APPLY) {
24792480
com_error(c, PyExc_SyntaxError,
@@ -2955,6 +2956,7 @@ is_constant_false(struct compiling *c, node *n)
29552956

29562957
case expr_stmt:
29572958
case testlist:
2959+
case testlist1:
29582960
case test:
29592961
case and_test:
29602962
case not_test:
@@ -3356,6 +3358,7 @@ get_rawdocstring(node *n)
33563358

33573359
case expr_stmt:
33583360
case testlist:
3361+
case testlist1:
33593362
case test:
33603363
case and_test:
33613364
case not_test:
@@ -3704,6 +3707,7 @@ com_node(struct compiling *c, node *n)
37043707
/* Expression nodes */
37053708

37063709
case testlist:
3710+
case testlist1:
37073711
case testlist_safe:
37083712
com_list(c, n, 0);
37093713
break;
@@ -5447,6 +5451,7 @@ symtable_assign(struct symtable *st, node *n, int def_flag)
54475451
return;
54485452
case exprlist:
54495453
case testlist:
5454+
case testlist1:
54505455
if (NCH(n) == 1) {
54515456
n = CHILD(n, 0);
54525457
goto loop;

0 commit comments

Comments
 (0)