diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2021-01-17 22:33:59 +0330 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-01-18 08:56:34 +0100 |
commit | 5b79d0d1a33c04000a8b2d74df3376d9f6967fc4 (patch) | |
tree | 9e4f060d75905c564b4a3afdaad082485500002c /Userland/Shell | |
parent | a6917465d775c69dd18a922834cafd156df2cf7f (diff) | |
download | serenity-5b79d0d1a33c04000a8b2d74df3376d9f6967fc4.zip |
Shell: Allow newlines between `else` and `if`'s closing brace
This is more flexible and intuitive. Fixes #4992.
Diffstat (limited to 'Userland/Shell')
-rw-r--r-- | Userland/Shell/Parser.cpp | 10 | ||||
-rw-r--r-- | Userland/Shell/Tests/if.sh | 8 |
2 files changed, 15 insertions, 3 deletions
diff --git a/Userland/Shell/Parser.cpp b/Userland/Shell/Parser.cpp index 468f43288f..f5b6f7a5e7 100644 --- a/Userland/Shell/Parser.cpp +++ b/Userland/Shell/Parser.cpp @@ -718,19 +718,23 @@ RefPtr<AST::Node> Parser::parse_if_expr() return body; }; - consume_while(is_whitespace); + consume_while(is_any_of(" \t\n")); auto true_branch = parse_braced_toplevel(); - consume_while(is_whitespace); + auto end_before_else = m_offset; + auto line_before_else = line(); + consume_while(is_any_of(" \t\n")); Optional<AST::Position> else_position; { auto else_start = push_start(); if (expect("else")) else_position = AST::Position { else_start->offset, m_offset, else_start->line, line() }; + else + restore_to(end_before_else, line_before_else); } if (else_position.has_value()) { - consume_while(is_whitespace); + consume_while(is_any_of(" \t\n")); if (peek() == '{') { auto false_branch = parse_braced_toplevel(); return create<AST::IfCond>(else_position, condition.release_nonnull(), move(true_branch), move(false_branch)); // If expr true_branch Else false_branch diff --git a/Userland/Shell/Tests/if.sh b/Userland/Shell/Tests/if.sh index 598a9471bc..68731805e2 100644 --- a/Userland/Shell/Tests/if.sh +++ b/Userland/Shell/Tests/if.sh @@ -16,6 +16,14 @@ if test 1 -eq 1 { } else { } + # Can we put `else` on a new line? + if false { + echo "if false runs true branch" + exit 2 + } + else { + } + # Basic 'if' structure, without 'else' if false { echo "Fail: 'if false' runs the branch" |