Skip to content

Commit dabd06a

Browse files
Merge pull request #1016 from Thirumalai-Shaktivel/fix_parser
2 parents dcd584b + 9d9e20d commit dabd06a

7 files changed

+23
-12
lines changed

src/lpython/parser/parser.yy

+10-6
Original file line numberDiff line numberDiff line change
@@ -707,12 +707,12 @@ class_def
707707
$$ = CLASS_01($1, $3, $5, @$); }
708708
| decorators_opt KW_CLASS id "(" expr_list_opt ")" ":" body_stmts {
709709
$$ = CLASS_02($1, $3, $5, $8, @$); }
710-
| decorators_opt KW_CLASS id "(" expr_list "," keyword_items ")"
711-
":" body_stmts { $$ = CLASS_03($1, $3, $5, $7, $10, @$); }
710+
| decorators_opt KW_CLASS id "(" expr_list "," keyword_items comma_opt ")"
711+
":" body_stmts { $$ = CLASS_03($1, $3, $5, $7, $11, @$); }
712712
| decorators_opt KW_CLASS id "(" keyword_items "," expr_list ")"
713713
":" body_stmts { $$ = CLASS_03($1, $3, $7, $5, $10, @$); }
714-
| decorators_opt KW_CLASS id "(" keyword_items ")" ":" body_stmts {
715-
$$ = CLASS_04($1, $3, $5, $8, @$); }
714+
| decorators_opt KW_CLASS id "(" keyword_items comma_opt ")" ":" body_stmts
715+
{ $$ = CLASS_04($1, $3, $5, $9, @$); }
716716
;
717717

718718
async_func_def
@@ -770,7 +770,7 @@ while_statement
770770
;
771771

772772
expr_list_opt
773-
: expr_list { $$ = $1; }
773+
: expr_list comma_opt { $$ = $1; }
774774
| %empty { LIST_NEW($$); }
775775
;
776776

@@ -997,13 +997,17 @@ expr
997997
| KW_NONE { $$ = NONE(@$); }
998998
| TK_ELLIPSIS { $$ = ELLIPSIS(@$); }
999999
| "(" expr ")" { $$ = $2; }
1000+
| "(" TK_TYPE_IGNORE expr ")" { $$ = $3; extract_type_comment(p, @$, $2); }
10001001
| "(" ")" { $$ = TUPLE_EMPTY(@$); }
10011002
| "(" expr_list "," ")" { $$ = TUPLE_03($2, @$); }
1003+
| "(" TK_TYPE_IGNORE expr_list "," ")" { $$ = TUPLE_03($3, @$);
1004+
extract_type_comment(p, @$, $2); }
10021005
| "(" expr_list "," expr ")" { $$ = TUPLE_01(TUPLE_($2, $4), @$); }
1006+
| "(" TK_TYPE_IGNORE expr_list "," expr ")" {
1007+
$$ = TUPLE_01(TUPLE_($3, $5), @$); extract_type_comment(p, @$, $2);}
10031008
| function_call { $$ = $1; }
10041009
| subscript { $$ = $1; }
10051010
| "[" expr_list_opt "]" { $$ = LIST($2, @$); }
1006-
| "[" expr_list "," "]" { $$ = LIST($2, @$); }
10071011
| "{" expr_list "}" { $$ = SET($2, @$); }
10081012
| "{" expr_list "," "}" { $$ = SET($2, @$); }
10091013
| expr "." id { $$ = ATTRIBUTE_REF($1, $3, @$); }

tests/parser/class_def2.py

+2
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ class test_08(x): # type: ignore[misc]
3131
class sub(np.ndarray): pass
3232

3333
class name: ...;
34+
35+
class MMatrix(MaskedArray, np.matrix,): ...

tests/parser/tuple1.py

+5
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,8 @@ def test():
3030

3131
sum([(1, -1)[i % 2] for i in x])
3232
sum([(1, -1,)[i % 2] for i in x])
33+
34+
a = ( # type: ignore
35+
x,
36+
y,
37+
)

tests/reference/ast_new-class_def2-c6db986.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "ast_new-class_def2-c6db986",
33
"cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}",
44
"infile": "tests/parser/class_def2.py",
5-
"infile_hash": "522d482d24adfe793e0f92aa25775c68cafafe6e4111c33a61595e3e",
5+
"infile_hash": "e7ad3c230a50dbe235499e6cc7ca8a93c59142bbc91b640edbd072af",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "ast_new-class_def2-c6db986.stdout",
9-
"stdout_hash": "df9bb5794f1973e3ffdaa16e1bb95210745ce1cfcf3aa69ffba5747f",
9+
"stdout_hash": "0258c4bd6a67c9603ac8761d7ed1cc27dcbe049a04dad480af7be41f",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(Module [(ClassDef _DTypeDict [(Name _DTypeDictBase Load)] [(total (ConstantBool .false. ()))] [(Pass)] []) (ClassDef test_01 [] [] [(Pass)] []) (ClassDef test_02 [(Name x Load) (Name y Load)] [] [(Pass)] []) (ClassDef test_03 [(Name x Load)] [(y (ConstantInt 1 ())) (z (ConstantStr "123" ()))] [(Pass)] []) (ClassDef test_04 [(ConstantInt 100 ())] [(() (Name x Load))] [(Pass)] []) (ClassDef test_05 [(Starred (Name x Load) Load)] [(() (Name y Load))] [(Pass)] []) (ClassDef test_06 [(Starred (Name x Load) Load) (Name y Load)] [] [(Pass)] []) (ClassDef test_07 [(Starred (Name x Load) Load) (Starred (Name y Load) Load)] [] [(Pass)] []) (ClassDef test_08 [] [(() (Name x Load)) (() (Name y Load))] [(Pass)] []) (ClassDef test_08 [(Name x Load)] [] [(Pass)] []) (ClassDef sub [(Attribute (Name np Load) ndarray Load)] [] [(Pass)] []) (ClassDef name [] [] [(Expr (ConstantEllipsis ()))] [])] [(TypeIgnore 0 "[misc]")])
1+
(Module [(ClassDef _DTypeDict [(Name _DTypeDictBase Load)] [(total (ConstantBool .false. ()))] [(Pass)] []) (ClassDef test_01 [] [] [(Pass)] []) (ClassDef test_02 [(Name x Load) (Name y Load)] [] [(Pass)] []) (ClassDef test_03 [(Name x Load)] [(y (ConstantInt 1 ())) (z (ConstantStr "123" ()))] [(Pass)] []) (ClassDef test_04 [(ConstantInt 100 ())] [(() (Name x Load))] [(Pass)] []) (ClassDef test_05 [(Starred (Name x Load) Load)] [(() (Name y Load))] [(Pass)] []) (ClassDef test_06 [(Starred (Name x Load) Load) (Name y Load)] [] [(Pass)] []) (ClassDef test_07 [(Starred (Name x Load) Load) (Starred (Name y Load) Load)] [] [(Pass)] []) (ClassDef test_08 [] [(() (Name x Load)) (() (Name y Load))] [(Pass)] []) (ClassDef test_08 [(Name x Load)] [] [(Pass)] []) (ClassDef sub [(Attribute (Name np Load) ndarray Load)] [] [(Pass)] []) (ClassDef name [] [] [(Expr (ConstantEllipsis ()))] []) (ClassDef MMatrix [(Name MaskedArray Load) (Attribute (Name np Load) matrix Load)] [] [(Expr (ConstantEllipsis ()))] [])] [(TypeIgnore 0 "[misc]")])

tests/reference/ast_new-tuple1-29c08af.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "ast_new-tuple1-29c08af",
33
"cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}",
44
"infile": "tests/parser/tuple1.py",
5-
"infile_hash": "5ea7f8e0dc86056990f33f70f4998abdd71b97f89f1f174d20d3534f",
5+
"infile_hash": "921763bd0566e7c1d7afe5cbf4cd8718721598b557d26e26833e8d97",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "ast_new-tuple1-29c08af.stdout",
9-
"stdout_hash": "93105938daca5e518874dc85913b13f0d57210e82f2fdf35d1d843e8",
9+
"stdout_hash": "79318056f942a6bfd156eeb50a4b48c025adee99f3a26ba81a5ffef9",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(Module [(Assign [(Name a Store)] (BinOp (Tuple [(ConstantInt 1 ())] Load) Add (ConstantInt 4 ())) ()) (Assign [(Name a Store)] (BinOp (BinOp (ConstantInt 3 ()) Mult (Tuple [(ConstantInt 1 ()) (ConstantInt 2 ())] Load)) Add (ConstantInt 4 ())) ()) (Expr (Call (Name f Load) [(Tuple [(Name x Load) (Name y Load)] Load)] [])) (Expr (Call (Name f Load) [(Tuple [(Name x Load)] Load)] [])) (Assign [(Name a Store)] (Call (Name len Load) [(Tuple [(ConstantInt 1 ()) (ConstantInt 2 ()) (ConstantInt 3 ())] Load)] []) ()) (Assign [(Name a4 Store)] (Tuple [(Tuple [(ConstantInt 1 ()) (ConstantInt 2 ()) (ConstantInt 3 ())] Load) (Tuple [(ConstantInt 4 ()) (ConstantInt 5 ()) (ConstantInt 6 ())] Load)] Load) ()) (Expr (Tuple [(ConstantInt 1 ()) (ConstantInt 2 ()) (ConstantInt 3 ())] Load)) (Expr (Tuple [(ConstantInt 1 ()) (ConstantInt 2 ())] Load)) (Expr (BinOp (Tuple [(ConstantInt 1 ()) (ConstantInt 2 ())] Load) Mult (ConstantInt 3 ()))) (Expr (Call (Name print Load) [(BinOp (ConstantStr "%s%s%s" ()) Mod (Tuple [(Name a Load) (Name b Load) (Name c Load)] Load))] [])) (Expr (Call (Name print Load) [(BinOp (ConstantStr "%s%s%s" ()) Mod (Tuple [(BinOp (ConstantInt 10 ()) Add (Name x Load)) (Name y Load) (BinOp (Name z Load) Add (ConstantInt 20 ()))] Load))] [])) (If (Compare (Attribute (Name sys Load) version_info Load) GtE [(Tuple [(ConstantInt 3 ()) (ConstantInt 9 ())] Load)]) [(Pass)] []) (Assign [(Name NDArray Store)] (Call (Name _GenericAlias Load) [(Attribute (Name np Load) ndarray Load) (Tuple [(Name Any Load) (Name _DType Load)] Load)] []) ()) (FunctionDef test ([] [] [] [] [] [] []) [(Pass)] [(Call (Attribute (Attribute (Name pytest Load) mark Load) parametrize Load) [(ConstantStr "shape" ()) (List [(ConstantInt 5 ()) (Tuple [(ConstantInt 3 ()) (ConstantInt 3 ()) (ConstantInt 3 ())] Load)] Load)] [])] () ()) (Assign [(Name _promotion_table Store)] (Dict [(Tuple [(Name int8 Load) (Name int8 Load)] Load)] [(Name int8 Load)]) ()) (Expr (Tuple [(Name x Load) (Name y Load)] Load)) (Expr (Tuple [(Name x Load) (Name y Load)] Load)) (Expr (Tuple [(Call (Name assert_ Load) [(Call (Attribute (Name np Load) isnan Load) [(Name div Load)] [])] []) (BinOp (ConstantStr "dt: %s, rem: %s" ()) Mod (Tuple [(Name dt Load) (Name rem Load)] Load))] Load)) (Expr (Call (Name sum Load) [(ListComp (Subscript (Tuple [(ConstantInt 1 ()) (UnaryOp USub (ConstantInt 1 ()))] Load) (BinOp (Name i Load) Mod (ConstantInt 2 ())) Load) [((Name i Store) (Name x Load) [] 0)])] [])) (Expr (Call (Name sum Load) [(ListComp (Subscript (Tuple [(ConstantInt 1 ()) (UnaryOp USub (ConstantInt 1 ()))] Load) (BinOp (Name i Load) Mod (ConstantInt 2 ())) Load) [((Name i Store) (Name x Load) [] 0)])] []))] [])
1+
(Module [(Assign [(Name a Store)] (BinOp (Tuple [(ConstantInt 1 ())] Load) Add (ConstantInt 4 ())) ()) (Assign [(Name a Store)] (BinOp (BinOp (ConstantInt 3 ()) Mult (Tuple [(ConstantInt 1 ()) (ConstantInt 2 ())] Load)) Add (ConstantInt 4 ())) ()) (Expr (Call (Name f Load) [(Tuple [(Name x Load) (Name y Load)] Load)] [])) (Expr (Call (Name f Load) [(Tuple [(Name x Load)] Load)] [])) (Assign [(Name a Store)] (Call (Name len Load) [(Tuple [(ConstantInt 1 ()) (ConstantInt 2 ()) (ConstantInt 3 ())] Load)] []) ()) (Assign [(Name a4 Store)] (Tuple [(Tuple [(ConstantInt 1 ()) (ConstantInt 2 ()) (ConstantInt 3 ())] Load) (Tuple [(ConstantInt 4 ()) (ConstantInt 5 ()) (ConstantInt 6 ())] Load)] Load) ()) (Expr (Tuple [(ConstantInt 1 ()) (ConstantInt 2 ()) (ConstantInt 3 ())] Load)) (Expr (Tuple [(ConstantInt 1 ()) (ConstantInt 2 ())] Load)) (Expr (BinOp (Tuple [(ConstantInt 1 ()) (ConstantInt 2 ())] Load) Mult (ConstantInt 3 ()))) (Expr (Call (Name print Load) [(BinOp (ConstantStr "%s%s%s" ()) Mod (Tuple [(Name a Load) (Name b Load) (Name c Load)] Load))] [])) (Expr (Call (Name print Load) [(BinOp (ConstantStr "%s%s%s" ()) Mod (Tuple [(BinOp (ConstantInt 10 ()) Add (Name x Load)) (Name y Load) (BinOp (Name z Load) Add (ConstantInt 20 ()))] Load))] [])) (If (Compare (Attribute (Name sys Load) version_info Load) GtE [(Tuple [(ConstantInt 3 ()) (ConstantInt 9 ())] Load)]) [(Pass)] []) (Assign [(Name NDArray Store)] (Call (Name _GenericAlias Load) [(Attribute (Name np Load) ndarray Load) (Tuple [(Name Any Load) (Name _DType Load)] Load)] []) ()) (FunctionDef test ([] [] [] [] [] [] []) [(Pass)] [(Call (Attribute (Attribute (Name pytest Load) mark Load) parametrize Load) [(ConstantStr "shape" ()) (List [(ConstantInt 5 ()) (Tuple [(ConstantInt 3 ()) (ConstantInt 3 ()) (ConstantInt 3 ())] Load)] Load)] [])] () ()) (Assign [(Name _promotion_table Store)] (Dict [(Tuple [(Name int8 Load) (Name int8 Load)] Load)] [(Name int8 Load)]) ()) (Expr (Tuple [(Name x Load) (Name y Load)] Load)) (Expr (Tuple [(Name x Load) (Name y Load)] Load)) (Expr (Tuple [(Call (Name assert_ Load) [(Call (Attribute (Name np Load) isnan Load) [(Name div Load)] [])] []) (BinOp (ConstantStr "dt: %s, rem: %s" ()) Mod (Tuple [(Name dt Load) (Name rem Load)] Load))] Load)) (Expr (Call (Name sum Load) [(ListComp (Subscript (Tuple [(ConstantInt 1 ()) (UnaryOp USub (ConstantInt 1 ()))] Load) (BinOp (Name i Load) Mod (ConstantInt 2 ())) Load) [((Name i Store) (Name x Load) [] 0)])] [])) (Expr (Call (Name sum Load) [(ListComp (Subscript (Tuple [(ConstantInt 1 ()) (UnaryOp USub (ConstantInt 1 ()))] Load) (BinOp (Name i Load) Mod (ConstantInt 2 ())) Load) [((Name i Store) (Name x Load) [] 0)])] [])) (Assign [(Name a Store)] (Tuple [(Name x Load) (Name y Load)] Load) ())] [(TypeIgnore 0 "")])

0 commit comments

Comments
 (0)