bjpop / language-python

A parser for Python 2.x and 3.x written in Haskell
157 stars 46 forks source link

iterable unpacking in comprehension shall not be parsed #66

Open JackAllTrades-MoN opened 3 years ago

JackAllTrades-MoN commented 3 years ago

Summary

In python3 (at least on 3.8.10), star_expression in comprehension is handled as a parse error. However, version 3 parser can parse the expression.

Although this issue may not be a big deal to parse valid python programs, I'm sure that it may be better to fix this to enhance usability of the library.

For example (AS IS)

>>> [*b for i in range(1,2)]
  File "<stdin>", line 1
SyntaxError: iterable unpacking cannot be used in comprehension
>>> 
*Language.Python.Common Language.Python.Version3.Parser> parseExpr "[*b for i in range(1,2)]" ""
Right (ListComp {list_comprehension = Comprehension {comprehension_expr = ComprehensionExpr (Starred {starred_expr = Var {var_ident = Ident {ident_string = "b", ident_annot = SpanPoint {span_filename = "", span_row = 1, span_column = 3}}, expr_annot = SpanPoint {span_filename = "", span_row = 1, span_column = 3}}, expr_annot = SpanCoLinear {span_filename = "", span_row = 1, span_start_column = 2, span_end_column = 3}}), comprehension_for = CompFor {comp_for_async = False, comp_for_exprs = [Var {var_ident = Ident {ident_string = "i", ident_annot = SpanPoint {span_filename = "", span_row = 1, span_column = 9}}, expr_annot = SpanPoint {span_filename = "", span_row = 1, span_column = 9}}], comp_in_expr = Call {call_fun = Var {var_ident = Ident {ident_string = "range", ident_annot = SpanCoLinear {span_filename = "", span_row = 1, span_start_column = 14, span_end_column = 18}}, expr_annot = SpanCoLinear {span_filename = "", span_row = 1, span_start_column = 14, span_end_column = 18}}, call_args = [ArgExpr {arg_expr = Int {int_value = 1, expr_literal = "1", expr_annot = SpanPoint {span_filename = "", span_row = 1, span_column = 20}}, arg_annot = SpanPoint {span_filename = "", span_row = 1, span_column = 20}},ArgExpr {arg_expr = Int {int_value = 2, expr_literal = "2", expr_annot = SpanPoint {span_filename = "", span_row = 1, span_column = 22}}, arg_annot = SpanPoint {span_filename = "", span_row = 1, span_column = 22}}], expr_annot = SpanCoLinear {span_filename = "", span_row = 1, span_start_column = 14, span_end_column = 23}}, comp_for_iter = Nothing, comp_for_annot = SpanCoLinear {span_filename = "", span_row = 1, span_start_column = 5, span_end_column = 23}}, comprehension_annot = SpanCoLinear {span_filename = "", span_row = 1, span_start_column = 2, span_end_column = 23}}, expr_annot = SpanCoLinear {span_filename = "", span_row = 1, span_start_column = 1, span_end_column = 24}},[])
JackAllTrades-MoN commented 3 years ago

I'm going to deal this problem on #65 too.