Skip to content

Commit

Permalink
Correct the order of elif clauses in the AST.
Browse files Browse the repository at this point in the history
Fixes #7.
trotterdylan authored and whitequark committed Jan 25, 2017
1 parent 585fcae commit a82d05d
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pythonparser/parser.py
Original file line number Diff line number Diff line change
@@ -1148,7 +1148,7 @@ def if_stmt(self, if_loc, test, if_colon_loc, body, elifs, else_opt):
if else_opt:
stmt.else_loc, stmt.else_colon_loc, stmt.orelse = else_opt

for elif_ in elifs:
for elif_ in reversed(elifs):
stmt.keyword_loc, stmt.test, stmt.if_colon_loc, stmt.body = elif_
stmt.loc = stmt.keyword_loc.join(stmt.body[-1].loc)
if stmt.orelse:
11 changes: 11 additions & 0 deletions pythonparser/test/test_parser.py
Original file line number Diff line number Diff line change
@@ -201,6 +201,7 @@ def assertDiagnosesUnexpected(self, code, err_token, loc_matcher="",
ast_expr_1 = {"ty": "Expr", "value": {"ty": "Num", "n": 1}}
ast_expr_2 = {"ty": "Expr", "value": {"ty": "Num", "n": 2}}
ast_expr_3 = {"ty": "Expr", "value": {"ty": "Num", "n": 3}}
ast_expr_4 = {"ty": "Expr", "value": {"ty": "Num", "n": 4}}

ast_x = {"ty": "Name", "id": "x", "ctx": None}
ast_y = {"ty": "Name", "id": "y", "ctx": None}
@@ -1410,6 +1411,16 @@ def test_if(self):
" ~~~~~~~~~~~~~~~~~~~~~ 0.orelse.0.loc"
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 0.loc")

# Ensure elif nodes are in the correct order.
# See: https://github.com/m-labs/pythonparser/issues/7
self.assertParsesSuite(
[{"ty": "If", "test": self.ast_x, "body": [self.ast_expr_1], "orelse": [
{"ty": "If", "test": self.ast_y, "body": [self.ast_expr_2], "orelse": [
{"ty": "If", "test": self.ast_z, "body": [self.ast_expr_3],
"orelse": [self.ast_expr_4]}
]}]}],
"if x:· 1·elif y:· 2·elif z:· 3·else:· 4")

def test_while(self):
self.assertParsesSuite(
[{"ty": "While", "test": self.ast_x, "body": [self.ast_expr_1], "orelse": []}],

0 comments on commit a82d05d

Please sign in to comment.