Skip to content

[Parser] Fix bug with comment in multiline statements #976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/lpython/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,11 @@ statements1

single_line_statements
: single_line_multi_statements TK_NEWLINE { $$ = $1; }
| single_line_multi_statements TK_COMMENT TK_NEWLINE { $$ = $1; }
| single_line_multi_statements TK_EOLCOMMENT { $$ = $1; }
| single_line_statement TK_NEWLINE { $$ = A2LIST(p.m_a, $1); }
| single_line_statement TK_SEMICOLON TK_NEWLINE { $$ = A2LIST(p.m_a, $1); }
| single_line_statement TK_SEMICOLON TK_COMMENT TK_NEWLINE { $$ = A2LIST(p.m_a, $1); }
| single_line_statement TK_COMMENT TK_NEWLINE { $$ = A2LIST(p.m_a, $1); }
| single_line_statement TK_SEMICOLON TK_EOLCOMMENT { $$ = A2LIST(p.m_a, $1); }
| single_line_statement TK_EOLCOMMENT { $$ = A2LIST(p.m_a, $1); }
;

single_line_multi_statements
Expand Down
5 changes: 5 additions & 0 deletions tests/parser/if1.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
pass
else: a = b

if a == b: pass # comment
if a == b: pass; # comment
if a == b: x = a + b; break # comment
if a == b: x = a + b; break; # comment

# TODO: Make this work (basically separated by multiple sep(s)).
# at present, it only works with a single newline separator.

Expand Down
4 changes: 2 additions & 2 deletions tests/reference/ast_new-if1-db43586.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "ast_new-if1-db43586",
"cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}",
"infile": "tests/parser/if1.py",
"infile_hash": "f6b4162b0a0f7d4040292c998d464fb0b8613ce2f1f696035a7243d4",
"infile_hash": "22ff1df9592cea862ebc5a2a03f872b87d7ef6cf7101670e8fc566da",
"outfile": null,
"outfile_hash": null,
"stdout": "ast_new-if1-db43586.stdout",
"stdout_hash": "08138f8420c2f2f5b8b8c072456203a6d5912fd7259984cc062b2cd3",
"stdout_hash": "267cff14e40641f5155167245af70ec048b692c5644aaf4d60d4334d",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/ast_new-if1-db43586.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(Module [(Assign [(Name x Store)] (ConstantInt 10 ()) ()) (Assign [(Name y Store)] (ConstantInt 14 ()) ()) (Assign [(Name z Store)] (ConstantInt 12 ()) ()) (If (BoolOp And [(Compare (Name x Load) GtE [(Name y Load)]) (Compare (Name x Load) GtE [(Name z Load)])]) [(Assign [(Name largest Store)] (Name x Load) ()) (Expr (Call (Name print Load) [(ConstantStr "Largest: " ()) (Name x Load)] []))] [(If (BoolOp And [(Compare (Name y Load) GtE [(Name x Load)]) (Compare (Name y Load) GtE [(Name z Load)])]) [(Assign [(Name largest Store)] (Name y Load) ()) (Expr (Call (Name print Load) [(ConstantStr "Largest: " ()) (Name y Load)] []))] [(Assign [(Name largest Store)] (Name z Load) ()) (Expr (Call (Name print Load) [(ConstantStr "Largest: " ()) (Name z Load)] []))])]) (If (Compare (Name y Load) Gt [(ConstantInt 10 ())]) [(Expr (Call (Name print Load) [(ConstantStr "Above ten," ())] [])) (If (Compare (Name y Load) Gt [(ConstantInt 20 ())]) [(Expr (Call (Name print Load) [(ConstantStr "and also above 20!" ())] []))] [(Expr (Call (Name print Load) [(ConstantStr "but not above 20." ())] []))])] []) (If (Compare (Name a Load) Eq [(Name b Load)]) [(Expr (ConstantEllipsis ())) (AugAssign (Name a Store) Add (Name b Load))] []) (If (Compare (Name a Load) Eq [(Name b Load)]) [(Expr (ConstantEllipsis ())) (AugAssign (Name a Store) Add (Name b Load))] []) (If (Name b Load) [(Expr (ConstantEllipsis ()))] [(Assign [(Name a Store)] (Name b Load) ())]) (If (Name b Load) [(Expr (ConstantEllipsis ()))] [(Assign [(Name a Store)] (Name b Load) ())]) (If (Name b Load) [(Pass)] [(Assign [(Name a Store)] (Name b Load) ())])] [])
(Module [(Assign [(Name x Store)] (ConstantInt 10 ()) ()) (Assign [(Name y Store)] (ConstantInt 14 ()) ()) (Assign [(Name z Store)] (ConstantInt 12 ()) ()) (If (BoolOp And [(Compare (Name x Load) GtE [(Name y Load)]) (Compare (Name x Load) GtE [(Name z Load)])]) [(Assign [(Name largest Store)] (Name x Load) ()) (Expr (Call (Name print Load) [(ConstantStr "Largest: " ()) (Name x Load)] []))] [(If (BoolOp And [(Compare (Name y Load) GtE [(Name x Load)]) (Compare (Name y Load) GtE [(Name z Load)])]) [(Assign [(Name largest Store)] (Name y Load) ()) (Expr (Call (Name print Load) [(ConstantStr "Largest: " ()) (Name y Load)] []))] [(Assign [(Name largest Store)] (Name z Load) ()) (Expr (Call (Name print Load) [(ConstantStr "Largest: " ()) (Name z Load)] []))])]) (If (Compare (Name y Load) Gt [(ConstantInt 10 ())]) [(Expr (Call (Name print Load) [(ConstantStr "Above ten," ())] [])) (If (Compare (Name y Load) Gt [(ConstantInt 20 ())]) [(Expr (Call (Name print Load) [(ConstantStr "and also above 20!" ())] []))] [(Expr (Call (Name print Load) [(ConstantStr "but not above 20." ())] []))])] []) (If (Compare (Name a Load) Eq [(Name b Load)]) [(Expr (ConstantEllipsis ())) (AugAssign (Name a Store) Add (Name b Load))] []) (If (Compare (Name a Load) Eq [(Name b Load)]) [(Expr (ConstantEllipsis ())) (AugAssign (Name a Store) Add (Name b Load))] []) (If (Name b Load) [(Expr (ConstantEllipsis ()))] [(Assign [(Name a Store)] (Name b Load) ())]) (If (Name b Load) [(Expr (ConstantEllipsis ()))] [(Assign [(Name a Store)] (Name b Load) ())]) (If (Name b Load) [(Pass)] [(Assign [(Name a Store)] (Name b Load) ())]) (If (Compare (Name a Load) Eq [(Name b Load)]) [(Pass)] []) (If (Compare (Name a Load) Eq [(Name b Load)]) [(Pass)] []) (If (Compare (Name a Load) Eq [(Name b Load)]) [(Assign [(Name x Store)] (BinOp (Name a Load) Add (Name b Load)) ()) (Break)] []) (If (Compare (Name a Load) Eq [(Name b Load)]) [(Assign [(Name x Store)] (BinOp (Name a Load) Add (Name b Load)) ()) (Break)] [])] [])