Skip to content

Commit 0002e42

Browse files
author
Andy C
committed
[ysh] Add parse error for splat expr ...x
Fix crash This is issue #2237
1 parent 1dfd7e3 commit 0002e42

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

test/ysh-parse-errors.sh

+7
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,13 @@ test-int-overflow() {
17031703
_ysh-parse-error '= 0x123456789_123456789_123456789'
17041704
}
17051705

1706+
test-splat-expr() {
1707+
_ysh-parse-error '= ...y'
1708+
_ysh-parse-error '= [42, ...y]'
1709+
_ysh-parse-error '= (...x for x in [1,2,3])'
1710+
_ysh-parse-error '= [...x for x in [1,2,3]]'
1711+
}
1712+
17061713
#
17071714
# Entry Points
17081715
#

ysh/expr_to_ast.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,11 @@ def _TestlistComp(self, parent, p_node, id0):
324324

325325
n = p_node.NumChildren()
326326
if n > 1 and p_node.GetChild(1).typ == grammar_nt.comp_for:
327-
elt = self.Expr(p_node.GetChild(0))
327+
child0 = p_node.GetChild(0)
328+
if child0.typ == grammar_nt.splat_expr:
329+
p_die('Splat not currently supported', child0.tok)
330+
elt = self.Expr(child0)
331+
328332
comp = self._CompFor(p_node.GetChild(1))
329333
if id0 == Id.Op_LParen: # (x+1 for x in y)
330334
return expr.GeneratorExp(elt, [comp])
@@ -346,7 +350,10 @@ def _TestlistComp(self, parent, p_node, id0):
346350
if id0 == Id.Op_LBracket: # List [1,2,3]
347351
elts = [] # type: List[expr_t]
348352
for i in xrange(0, n, 2): # skip commas
349-
elts.append(self.Expr(p_node.GetChild(i)))
353+
child = p_node.GetChild(i)
354+
if child.typ == grammar_nt.splat_expr:
355+
p_die('Splat not currently supported', child.tok)
356+
elts.append(self.Expr(child))
350357

351358
return expr.List(parent.tok, elts,
352359
expr_context_e.Store) # unused expr_context_e

ysh/grammar.pgen2

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ factor: ('+'|'-'|'~') factor | power
8080
# YSH: removed Python 3 'await'
8181
power: atom trailer* ['**' factor]
8282

83+
# Note: I think splat_expr is not for list comprehensions, it's only for
84+
# literals like [42, *x] in Python, or [42, ...x] in YSH. This is new in
85+
# Python 3.
86+
# I think splat_expr expressed awkwardly because of pgen limitations.
8387
testlist_comp: (test|splat_expr) ( comp_for | (',' (test|splat_expr))* [','] )
8488

8589
atom: (

0 commit comments

Comments
 (0)