From f97d8cd562bde11e3d37a13dff60866d39bdd630 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Sun, 17 Jan 2021 16:52:38 +0900 Subject: [PATCH] Fix a bad indentation handling for if/try. --- compiler.c | 16 +++++++++------- test/testWeirdIndentCase.krk | 17 +++++++++++++++++ test/testWeirdIndentCase.krk.expect | 5 +++++ 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 test/testWeirdIndentCase.krk create mode 100644 test/testWeirdIndentCase.krk.expect diff --git a/compiler.c b/compiler.c index 0bc259c..90ae783 100644 --- a/compiler.c +++ b/compiler.c @@ -1282,14 +1282,14 @@ static void ifStatement() { block(blockWidth,"else"); endScope(); } - } else { - if (!check(TOKEN_EOF) && !check(TOKEN_EOL)) { - krk_ungetToken(parser.current); - parser.current = parser.previous; - if (blockWidth) { - parser.previous = previous; - } + } else if (!check(TOKEN_EOF) && !check(TOKEN_EOL)) { + krk_ungetToken(parser.current); + parser.current = parser.previous; + if (blockWidth) { + parser.previous = previous; } + } else { + advance(); /* Ignore this blank indentation line */ } } @@ -1519,6 +1519,8 @@ static void tryStatement() { if (blockWidth) { parser.previous = previous; } + } else { + advance(); /* Ignore this blank indentation line */ } } diff --git a/test/testWeirdIndentCase.krk b/test/testWeirdIndentCase.krk new file mode 100644 index 0000000..03e1612 --- /dev/null +++ b/test/testWeirdIndentCase.krk @@ -0,0 +1,17 @@ +def test(val): + if val: + print("In if") + # Comment + # val -= int(val / max) * max + return val + +print("Result:", test(True)) +print("result:", test(False)) + +def test2(val): + try: + print("This has a comment on the next line.", val) + # That could have been an else. + return val + +print("Result:", test2(True)) diff --git a/test/testWeirdIndentCase.krk.expect b/test/testWeirdIndentCase.krk.expect new file mode 100644 index 0000000..5fb93d5 --- /dev/null +++ b/test/testWeirdIndentCase.krk.expect @@ -0,0 +1,5 @@ +In if +Result: True +result: False +This has a comment on the next line. True +Result: True