Skip to content

Commit

Permalink
Update rules for with and async-with and add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
akshanshbhatt committed Aug 4, 2022
1 parent 945a81a commit 548bb4a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/lpython/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,8 @@ with_as_items
;

with_statement
: KW_WITH expr_list ":" sep statements { $$ = WITH($2, $5, @$); }
| KW_WITH with_as_items ":" sep statements { $$ = WITH_02($2, $5, @$); }
: KW_WITH expr_list ":" body_stmts { $$ = WITH($2, $4, @$); }
| KW_WITH with_as_items ":" body_stmts { $$ = WITH_02($2, $4, @$); }
| KW_WITH expr_list ":" TK_TYPE_COMMENT TK_NEWLINE
statements { $$ = WITH_01($2, $6, $4, @$); }
| KW_WITH with_as_items ":" TK_TYPE_COMMENT TK_NEWLINE
Expand Down Expand Up @@ -727,10 +727,10 @@ async_for_stmt
;

async_with_stmt
: KW_ASYNC KW_WITH expr_list ":" sep statements {
$$ = ASYNC_WITH($3, $6, @$); }
| KW_ASYNC KW_WITH with_as_items ":" sep statements {
$$ = ASYNC_WITH_02($3, $6, @$); }
: KW_ASYNC KW_WITH expr_list ":" body_stmts {
$$ = ASYNC_WITH($3, $5, @$); }
| KW_ASYNC KW_WITH with_as_items ":" body_stmts {
$$ = ASYNC_WITH_02($3, $5, @$); }
| KW_ASYNC KW_WITH expr_list ":" TK_TYPE_COMMENT
TK_NEWLINE statements { $$ = ASYNC_WITH_01($3, $7, $5, @$); }
| KW_ASYNC KW_WITH with_as_items ":" TK_TYPE_COMMENT
Expand Down
4 changes: 4 additions & 0 deletions tests/parser/with1.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@

with open('examples/expr2.py', 'r') as file:
x = file.read()

with open(...) as f: ...

with open('examples/expr2.py', 'r') as file: x = file.read()
4 changes: 2 additions & 2 deletions tests/reference/ast_new-with1-6c88c0f.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "ast_new-with1-6c88c0f",
"cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}",
"infile": "tests/parser/with1.py",
"infile_hash": "41a20b49149c9f91f1e64c9db70568231a8a35b8884e70c0349170cc",
"infile_hash": "8d46f1e1f87c55c04e4043db4a7a64c14a316187b0df625ee53d28b8",
"outfile": null,
"outfile_hash": null,
"stdout": "ast_new-with1-6c88c0f.stdout",
"stdout_hash": "de72f87d201bfe9ce6ff615405c240ecf343bd2ea718a43b39effd07",
"stdout_hash": "e4d8a8dda24748e0d8e3386310e13391ac5a72c4e9568542920d6da5",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/ast_new-with1-6c88c0f.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(Module [(With [((Call (Name A Load) [] []) (Name a Store)) ((Call (Name B Load) [] []) (Name b Store))] [(Expr (Call (Name f Load) [(Name a Load) (Name b Load)] []))] ()) (With [((Call (Name A Load) [] []) (Name a Store)) ((Call (Name B Load) [(Name a Load)] []) (Name b Store)) ((Call (Name C Load) [(Name a Load) (Name b Load)] []) (Name c Store))] [(Expr (Call (Name f Load) [(Name a Load) (Name c Load)] []))] ()) (With [((Call (Name open Load) [(ConstantStr "examples/expr2.py" ()) (ConstantStr "r" ())] []) (Name file Store))] [(Assign [(Name x Store)] (Call (Attribute (Name file Load) read Load) [] []) ())] ())] [])
(Module [(With [((Call (Name A Load) [] []) (Name a Store)) ((Call (Name B Load) [] []) (Name b Store))] [(Expr (Call (Name f Load) [(Name a Load) (Name b Load)] []))] ()) (With [((Call (Name A Load) [] []) (Name a Store)) ((Call (Name B Load) [(Name a Load)] []) (Name b Store)) ((Call (Name C Load) [(Name a Load) (Name b Load)] []) (Name c Store))] [(Expr (Call (Name f Load) [(Name a Load) (Name c Load)] []))] ()) (With [((Call (Name open Load) [(ConstantStr "examples/expr2.py" ()) (ConstantStr "r" ())] []) (Name file Store))] [(Assign [(Name x Store)] (Call (Attribute (Name file Load) read Load) [] []) ())] ()) (With [((Call (Name open Load) [(ConstantEllipsis ())] []) (Name f Store))] [(Expr (ConstantEllipsis ()))] ()) (With [((Call (Name open Load) [(ConstantStr "examples/expr2.py" ()) (ConstantStr "r" ())] []) (Name file Store))] [(Assign [(Name x Store)] (Call (Attribute (Name file Load) read Load) [] []) ())] ())] [])

0 comments on commit 548bb4a

Please sign in to comment.