Skip to content

Commit e9709af

Browse files
Merge pull request #1141 from Thirumalai-Shaktivel/fix_parser
2 parents c34ca67 + 64ef2fa commit e9709af

14 files changed

+39
-34
lines changed

src/lpython/parser/parser.yy

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ assignment_statement
442442
;
443443

444444
augassign_statement
445-
: expr augassign_op expr { $$ = AUGASSIGN_01($1, $2, $3, @$); }
445+
: expr augassign_op tuple_list { $$ = AUGASSIGN_01($1, $2, $3, @$); }
446446
;
447447

448448
augassign_op
@@ -879,6 +879,8 @@ primary
879879

880880
function_call
881881
: primary "(" call_arguement_list ")" { $$ = CALL_01($1, $3, @$); }
882+
| primary "(" TK_TYPE_IGNORE call_arguement_list ")" {
883+
$$ = CALL_01($1, $4, @$); extract_type_comment(p, @$, $3); }
882884
| primary "(" expr comp_for_items ")" {
883885
$$ = CALL_02($1, A2LIST(p.m_a, GENERATOR_EXPR($3, $4, @$)), @$); }
884886
| function_call "(" call_arguement_list ")" { $$ = CALL_01($1, $3, @$); }
@@ -912,7 +914,8 @@ slice_items
912914
;
913915

914916
slice_item
915-
: slice_item_list comma_opt { $$ = TUPLE($1, @$); }
917+
: slice_item_list { $$ = TUPLE_01($1, @$); }
918+
| slice_item_list "," { $$ = TUPLE_03($1, @$); }
916919
;
917920

918921
subscript

src/lpython/parser/semantics.h

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,17 @@ Arg** ARG2LIST(Allocator &al, Arg *x) {
367367
if(n_##x > 0) { \
368368
for(size_t i = 0; i < n_##x; i++) { \
369369
_m_##x.push_back(al, m_##x[i]->_arg); \
370-
if(m_##x[i]->default_value && !kw) { \
371-
defaults.push_back(al, m_##x[i]->defaults); \
372-
} else if (m_##x[i]->default_value){ \
373-
kw_defaults.push_back(al, m_##x[i]->defaults); \
370+
if(m_##x[i]->default_value) { \
371+
if (kw == 0) { \
372+
defaults.push_back(al, m_##x[i]->defaults); \
373+
} else { \
374+
kw_defaults.push_back(al, m_##x[i]->defaults); \
375+
} \
376+
} else { \
377+
if (kw == 1) { \
378+
kw_defaults.push_back(al, \
379+
(expr_t*)make_ConstantNone_t(al, l, nullptr)); \
380+
} \
374381
} \
375382
} \
376383
r->arguments.m_##x = _m_##x.p; \
@@ -394,25 +401,25 @@ static inline Args *FUNC_ARGS_01(Allocator &al, Location &l, Fn_Arg *parameters)
394401
if(parameters != nullptr) {
395402
Arg** m_posonlyargs = parameters->posonlyargs.p;
396403
size_t n_posonlyargs = parameters->posonlyargs.n;
397-
FUNC_ARGS_(posonlyargs, false);
404+
FUNC_ARGS_(posonlyargs, 0);
398405
}
399406
if(parameters != nullptr && parameters->args_val) {
400407
Arg** m_args = parameters->args->args.p;
401408
size_t n_args = parameters->args->args.n;
402-
FUNC_ARGS_(args, false);
409+
FUNC_ARGS_(args, 0);
403410

404411
if(parameters->args->var_kw_val) {
405412
Arg** m_vararg = parameters->args->var_kw->vararg.p;
406413
size_t n_vararg = parameters->args->var_kw->vararg.n;
407-
FUNC_ARGS_(vararg, false);
414+
FUNC_ARGS_(vararg, 0);
408415

409416
Arg** m_kwonlyargs = parameters->args->var_kw->kwonlyargs.p;
410417
size_t n_kwonlyargs = parameters->args->var_kw->kwonlyargs.n;
411-
FUNC_ARGS_(kwonlyargs, true);
418+
FUNC_ARGS_(kwonlyargs, 1);
412419

413420
Arg** m_kwarg = parameters->args->var_kw->kwarg.p;
414421
size_t n_kwarg = parameters->args->var_kw->kwarg.n;
415-
FUNC_ARGS_(kwarg, true);
422+
FUNC_ARGS_(kwarg, 2);
416423
}
417424
}
418425
r->arguments.m_kw_defaults = kw_defaults.p;
@@ -905,22 +912,12 @@ static inline ast_t* ID_TUPLE_02(Allocator &al, Location &l, Vec<ast_t*> elts) {
905912
#define COMP_EXPR_1(expr, generators, l) make_GeneratorExp_t(p.m_a, l, \
906913
EXPR(expr), generators.p, generators.n)
907914

908-
expr_t* CHECK_TUPLE(expr_t *x) {
909-
if(is_a<Tuple_t>(*x) && down_cast<Tuple_t>(x)->n_elts == 1) {
910-
return down_cast<Tuple_t>(x)->m_elts[0];
911-
} else {
912-
return x;
913-
}
914-
}
915-
916915
#define ELLIPSIS(l) make_ConstantEllipsis_t(p.m_a, l, nullptr)
917916

918917
#define NONE(l) make_ConstantNone_t(p.m_a, l, nullptr)
919918

920-
#define TUPLE(elts, l) make_Tuple_t(p.m_a, l, \
921-
EXPRS(elts), elts.size(), expr_contextType::Load)
922919
#define SUBSCRIPT_01(value, slice, l) make_Subscript_t(p.m_a, l, \
923-
EXPR(value), CHECK_TUPLE(EXPR(slice)), expr_contextType::Load)
920+
EXPR(value), EXPR(slice), expr_contextType::Load)
924921

925922
static inline ast_t* SLICE(Allocator &al, Location &l,
926923
ast_t *lower, ast_t *upper, ast_t *_step) {

tests/parser/statements1.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
obj = obj,
2424

2525
x += 1
26+
x += y, z
27+
x += (y, z),
2628

2729
x: i64
2830
y: i32 = 1

tests/parser/type_comment1.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ def main():
3232

3333
from sympy.simplify import (collect, powsimp, # type: ignore
3434
separatevars, simplify)
35+
36+
await test( # type: ignore
37+
x, y, z)

tests/reference/ast_new-function_def2-52c4587.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "ast_new-function_def2-52c4587.stdout",
9-
"stdout_hash": "f94b525c239bede5185c86dbf285f82e9a4ec3708e307c191f6270eb",
9+
"stdout_hash": "487f9f6f7ee1f07bfceae82a4dcff7b35d1861d587cd1f1f5d156912",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(Module [(FunctionDef test_01 ([] [] [] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_02 ([] [] [(x (Name i32 Load) ())] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_03 ([] [] [(x () ())] [(y () ()) (z () ())] [] [] []) [(Pass)] [] () ()) (FunctionDef test_04 ([] [] [(x () ())] [(y () ()) (z () ())] [] [(args () ())] []) [(Pass)] [] () ()) (FunctionDef test_05 ([] [] [(x () ())] [] [] [(args () ())] []) [(Pass)] [] () ()) (FunctionDef test_06 ([] [] [] [] [] [(args () ())] []) [(Pass)] [] () ()) (FunctionDef test_07 ([] [(x (Name i32 Load) ()) (y () ())] [] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_08 ([] [(x () ())] [(y () ())] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_09 ([] [(x () ())] [(y () ())] [(z () ())] [] [] []) [(Pass)] [] () ()) (FunctionDef test_10 ([] [(x () ())] [(y () ())] [(z () ())] [] [(args (Name i32 Load) ())] []) [(Pass)] [] () ()) (FunctionDef test_11 ([] [(x () ())] [(y () ())] [] [] [(args () ())] []) [(Pass)] [] () ()) (FunctionDef test_12 ([] [(x () ())] [] [] [] [(args () ())] []) [(Pass)] [] () ()) (FunctionDef test_13 ([] [(x (Name i32 Load) ()) (y (Name i32 Load) ())] [] [] [] [] [(ConstantInt 1 ()) (ConstantInt 1 ())]) [(Pass)] [] () ()) (FunctionDef test_14 ([] [(x () ())] [(y () ())] [(z () ())] [] [(args (Name i32 Load) ())] []) [(Pass)] [] (Name i32 Load) ()) (FunctionDef test_15 ([(a () ())] [] [] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_16 ([(a () ())] [(b () ()) (c () ())] [] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_17 ([(a () ())] [] [(b () ())] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_18 ([(a (Name i32 Load) ())] [] [(b (Name i64 Load) ())] [(c (Name i32 Load) ()) (d (Name i32 Load) ())] [] [] []) [(Pass)] [] () ()) (FunctionDef test_19 ([(a () ())] [] [(b () ())] [(c () ())] [] [(d () ())] []) [(Pass)] [] () ()) (FunctionDef test_20 ([(a () ())] [] [] [] [] [(b () ())] []) [(Pass)] [] () ()) (FunctionDef test_21 ([(a () ())] [] [(b () ())] [] [] [(c () ())] []) [(Pass)] [] () ()) (FunctionDef test_22 ([(a () ())] [(b () ()) (c () ())] [(d () ())] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_23 ([(a () ())] [(b () ()) (c () ())] [] [] [] [(d () ())] []) [(Pass)] [] () ()) (FunctionDef test_24 ([(a () ())] [(b () ()) (c () ())] [(d () ())] [] [] [(e () ())] []) [(Pass)] [] () ()) (FunctionDef test_25 ([(a () ())] [(b () ()) (c () ())] [(d () ())] [(e () ())] [] [(f () ())] []) [(Pass)] [] () ()) (FunctionDef test_26 ([] [] [] [(a () ())] [] [] []) [(Pass)] [] () ()) (FunctionDef test_27 ([] [] [] [(a () ())] [] [(b () ())] []) [(Pass)] [] () ()) (FunctionDef test_28 ([] [(a () ())] [] [(b () ())] [] [] []) [(Pass)] [] () ()) (FunctionDef test_29 ([] [(a () ())] [] [(b () ())] [] [(c () ())] []) [(Pass)] [] () ()) (FunctionDef test_30 ([(a () ())] [] [] [(b () ())] [] [] []) [(Pass)] [] () ()) (FunctionDef test_31 ([(a () ())] [(b () ())] [] [(c () ())] [] [] []) [(Pass)] [] () ()) (FunctionDef test_32 ([(a () ())] [] [] [(c () ())] [] [(d () ())] []) [(Pass)] [] () ()) (FunctionDef test_33 ([(a () ())] [(b () ())] [] [(c () ())] [] [(d () ())] []) [(Pass)] [] () ()) (Expr (Call (Name test Load) [] [])) (Expr (Call (Name test Load) [(Name x Load) (Name y Load)] [])) (Expr (Call (Name test Load) [(Name x Load)] [(y (ConstantInt 1 ())) (z (ConstantStr "123" ()))])) (Expr (Call (Name test Load) [(ConstantInt 100 ())] [(() (Name x Load))])) (Expr (Call (Name test Load) [(Starred (Name x Load) Load)] [(() (Name y Load))])) (Expr (Call (Name test Load) [(Starred (Name x Load) Load) (Name y Load)] [])) (Expr (Call (Name test Load) [(Starred (Name x Load) Load) (Starred (Name y Load) Load)] [])) (Expr (Call (Name test Load) [] [(() (Name x Load)) (() (Name y Load))])) (Expr (Call (Name test Load) [(Starred (Name x Load) Load)] [(y (ConstantInt 1 ())) (z (ConstantInt 2 ())) (() (Name a Load)) (b (ConstantInt 1 ()))])) (Expr (Call (Name test Load) [(Starred (Name x Load) Load)] [(() (Name a Load)) (b (ConstantStr "1" ()))])) (Expr (Call (Name test Load) [(Starred (Name y Load) Load)] [(x (ConstantInt 1 ())) (z (ConstantInt 2 ())) (() (Name a Load)) (b (ConstantStr "1" ()))])) (Expr (Call (Attribute (Name lp Load) test Load) [] [])) (Expr (Call (Attribute (Name lp Load) test Load) [(Name x Load) (Name y Load)] [])) (Expr (Call (Attribute (Name lp Load) test Load) [(Name x Load)] [(y (ConstantInt 1 ())) (z (ConstantStr "123" ()))])) (Expr (Subscript (Call (Name test Load) [] []) (ConstantStr "version" ()) Load)) (Expr (Subscript (Call (Name test Load) [(Name x Load) (Name y Load)] []) (ConstantStr "version" ()) Load)) (Expr (Subscript (Call (Name test Load) [(Name x Load) (Starred (Name y Load) Load)] []) (Slice () (UnaryOp USub (ConstantInt 1 ())) ()) Load)) (Expr (Subscript (Subscript (Call (Name test Load) [] []) (ConstantInt 1 ()) Load) (ConstantInt 1 ()) Load)) (Expr (Call (Subscript (Name x Load) (ConstantStr "numpystr" ()) Load) [] [])) (Expr (Call (Subscript (Name x Load) (ConstantStr "int" ()) Load) [] [])) (Expr (Call (Subscript (Name x Load) (Name func Load) Load) [(Starred (Name args Load) Load)] [(() (Name kwargs Load))])) (Expr (Call (Subscript (Name test Load) (ConstantInt 0 ()) Load) [(Starred (Subscript (Name test Load) (Slice (ConstantInt 1 ()) () ()) Load) Load)] [])) (Expr (Call (BinOp (Name obj Load) Mult (Attribute (Attribute (Name self Load) _arr Load) ndim Load)) [(Starred (Attribute (Attribute (Name self Load) _arr Load) shape Load) Load)] [])) (Expr (Call (Name traverse Load) [(Tuple [(Name index Load) (Name value Load)] Load) (Name visit Load) (Starred (Name args Load) Load)] [(result (Name result Load)) (() (Name kwargs Load))]))] [])
1+
(Module [(FunctionDef test_01 ([] [] [] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_02 ([] [] [(x (Name i32 Load) ())] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_03 ([] [] [(x () ())] [(y () ()) (z () ())] [(ConstantNone ()) (ConstantNone ())] [] []) [(Pass)] [] () ()) (FunctionDef test_04 ([] [] [(x () ())] [(y () ()) (z () ())] [(ConstantNone ()) (ConstantNone ())] [(args () ())] []) [(Pass)] [] () ()) (FunctionDef test_05 ([] [] [(x () ())] [] [] [(args () ())] []) [(Pass)] [] () ()) (FunctionDef test_06 ([] [] [] [] [] [(args () ())] []) [(Pass)] [] () ()) (FunctionDef test_07 ([] [(x (Name i32 Load) ()) (y () ())] [] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_08 ([] [(x () ())] [(y () ())] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_09 ([] [(x () ())] [(y () ())] [(z () ())] [(ConstantNone ())] [] []) [(Pass)] [] () ()) (FunctionDef test_10 ([] [(x () ())] [(y () ())] [(z () ())] [(ConstantNone ())] [(args (Name i32 Load) ())] []) [(Pass)] [] () ()) (FunctionDef test_11 ([] [(x () ())] [(y () ())] [] [] [(args () ())] []) [(Pass)] [] () ()) (FunctionDef test_12 ([] [(x () ())] [] [] [] [(args () ())] []) [(Pass)] [] () ()) (FunctionDef test_13 ([] [(x (Name i32 Load) ()) (y (Name i32 Load) ())] [] [] [] [] [(ConstantInt 1 ()) (ConstantInt 1 ())]) [(Pass)] [] () ()) (FunctionDef test_14 ([] [(x () ())] [(y () ())] [(z () ())] [(ConstantNone ())] [(args (Name i32 Load) ())] []) [(Pass)] [] (Name i32 Load) ()) (FunctionDef test_15 ([(a () ())] [] [] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_16 ([(a () ())] [(b () ()) (c () ())] [] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_17 ([(a () ())] [] [(b () ())] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_18 ([(a (Name i32 Load) ())] [] [(b (Name i64 Load) ())] [(c (Name i32 Load) ()) (d (Name i32 Load) ())] [(ConstantNone ()) (ConstantNone ())] [] []) [(Pass)] [] () ()) (FunctionDef test_19 ([(a () ())] [] [(b () ())] [(c () ())] [(ConstantNone ())] [(d () ())] []) [(Pass)] [] () ()) (FunctionDef test_20 ([(a () ())] [] [] [] [] [(b () ())] []) [(Pass)] [] () ()) (FunctionDef test_21 ([(a () ())] [] [(b () ())] [] [] [(c () ())] []) [(Pass)] [] () ()) (FunctionDef test_22 ([(a () ())] [(b () ()) (c () ())] [(d () ())] [] [] [] []) [(Pass)] [] () ()) (FunctionDef test_23 ([(a () ())] [(b () ()) (c () ())] [] [] [] [(d () ())] []) [(Pass)] [] () ()) (FunctionDef test_24 ([(a () ())] [(b () ()) (c () ())] [(d () ())] [] [] [(e () ())] []) [(Pass)] [] () ()) (FunctionDef test_25 ([(a () ())] [(b () ()) (c () ())] [(d () ())] [(e () ())] [(ConstantNone ())] [(f () ())] []) [(Pass)] [] () ()) (FunctionDef test_26 ([] [] [] [(a () ())] [(ConstantNone ())] [] []) [(Pass)] [] () ()) (FunctionDef test_27 ([] [] [] [(a () ())] [(ConstantNone ())] [(b () ())] []) [(Pass)] [] () ()) (FunctionDef test_28 ([] [(a () ())] [] [(b () ())] [(ConstantNone ())] [] []) [(Pass)] [] () ()) (FunctionDef test_29 ([] [(a () ())] [] [(b () ())] [(ConstantNone ())] [(c () ())] []) [(Pass)] [] () ()) (FunctionDef test_30 ([(a () ())] [] [] [(b () ())] [(ConstantNone ())] [] []) [(Pass)] [] () ()) (FunctionDef test_31 ([(a () ())] [(b () ())] [] [(c () ())] [(ConstantNone ())] [] []) [(Pass)] [] () ()) (FunctionDef test_32 ([(a () ())] [] [] [(c () ())] [(ConstantNone ())] [(d () ())] []) [(Pass)] [] () ()) (FunctionDef test_33 ([(a () ())] [(b () ())] [] [(c () ())] [(ConstantNone ())] [(d () ())] []) [(Pass)] [] () ()) (Expr (Call (Name test Load) [] [])) (Expr (Call (Name test Load) [(Name x Load) (Name y Load)] [])) (Expr (Call (Name test Load) [(Name x Load)] [(y (ConstantInt 1 ())) (z (ConstantStr "123" ()))])) (Expr (Call (Name test Load) [(ConstantInt 100 ())] [(() (Name x Load))])) (Expr (Call (Name test Load) [(Starred (Name x Load) Load)] [(() (Name y Load))])) (Expr (Call (Name test Load) [(Starred (Name x Load) Load) (Name y Load)] [])) (Expr (Call (Name test Load) [(Starred (Name x Load) Load) (Starred (Name y Load) Load)] [])) (Expr (Call (Name test Load) [] [(() (Name x Load)) (() (Name y Load))])) (Expr (Call (Name test Load) [(Starred (Name x Load) Load)] [(y (ConstantInt 1 ())) (z (ConstantInt 2 ())) (() (Name a Load)) (b (ConstantInt 1 ()))])) (Expr (Call (Name test Load) [(Starred (Name x Load) Load)] [(() (Name a Load)) (b (ConstantStr "1" ()))])) (Expr (Call (Name test Load) [(Starred (Name y Load) Load)] [(x (ConstantInt 1 ())) (z (ConstantInt 2 ())) (() (Name a Load)) (b (ConstantStr "1" ()))])) (Expr (Call (Attribute (Name lp Load) test Load) [] [])) (Expr (Call (Attribute (Name lp Load) test Load) [(Name x Load) (Name y Load)] [])) (Expr (Call (Attribute (Name lp Load) test Load) [(Name x Load)] [(y (ConstantInt 1 ())) (z (ConstantStr "123" ()))])) (Expr (Subscript (Call (Name test Load) [] []) (ConstantStr "version" ()) Load)) (Expr (Subscript (Call (Name test Load) [(Name x Load) (Name y Load)] []) (ConstantStr "version" ()) Load)) (Expr (Subscript (Call (Name test Load) [(Name x Load) (Starred (Name y Load) Load)] []) (Slice () (UnaryOp USub (ConstantInt 1 ())) ()) Load)) (Expr (Subscript (Subscript (Call (Name test Load) [] []) (ConstantInt 1 ()) Load) (ConstantInt 1 ()) Load)) (Expr (Call (Subscript (Name x Load) (ConstantStr "numpystr" ()) Load) [] [])) (Expr (Call (Subscript (Name x Load) (ConstantStr "int" ()) Load) [] [])) (Expr (Call (Subscript (Name x Load) (Name func Load) Load) [(Starred (Name args Load) Load)] [(() (Name kwargs Load))])) (Expr (Call (Subscript (Name test Load) (ConstantInt 0 ()) Load) [(Starred (Subscript (Name test Load) (Slice (ConstantInt 1 ()) () ()) Load) Load)] [])) (Expr (Call (BinOp (Name obj Load) Mult (Attribute (Attribute (Name self Load) _arr Load) ndim Load)) [(Starred (Attribute (Attribute (Name self Load) _arr Load) shape Load) Load)] [])) (Expr (Call (Name traverse Load) [(Tuple [(Name index Load) (Name value Load)] Load) (Name visit Load) (Starred (Name args Load) Load)] [(result (Name result Load)) (() (Name kwargs Load))]))] [])

tests/reference/ast_new-function_def3-f66064a.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "ast_new-function_def3-f66064a.stdout",
9-
"stdout_hash": "6eadb50ca8a83c6fb88e758234d91cceab17ee0177d97fc2bc04d503",
9+
"stdout_hash": "926e7c2dba8ad4f0536cb81c654f3ace4d4edce1b719da3dccc35f51",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)