From 7b031138fce2b0fd468398094b7b074094650b90 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Tue, 18 Apr 2023 16:48:01 +0330 Subject: [PATCH] Shell: Correctly handle commands after heredoc contents Previously we did not emit a newline after the ending heredoc key, which wreaked havoc on the parser logic, leading to parse errors. --- Userland/Shell/PosixLexer.cpp | 6 +++++- Userland/Shell/PosixLexer.h | 2 ++ Userland/Shell/PosixParser.cpp | 3 +++ Userland/Shell/PosixParser.h | 1 + 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Userland/Shell/PosixLexer.cpp b/Userland/Shell/PosixLexer.cpp index fb63375a8a..beaf2694ac 100644 --- a/Userland/Shell/PosixLexer.cpp +++ b/Userland/Shell/PosixLexer.cpp @@ -581,9 +581,13 @@ ErrorOr Lexer::reduce_start() m_state.on_new_line = true; m_state.buffer.clear(); + m_state.position.start_offset = m_state.position.end_offset; + m_state.position.start_line = m_state.position.end_line; + + Vector tokens { move(token), Token::newline() }; return ReductionResult { - .tokens = { move(token) }, + .tokens = move(tokens), .next_reduction = Reduction::Start, }; } diff --git a/Userland/Shell/PosixLexer.h b/Userland/Shell/PosixLexer.h index 8aa9364b45..ba01d32eb8 100644 --- a/Userland/Shell/PosixLexer.h +++ b/Userland/Shell/PosixLexer.h @@ -321,6 +321,8 @@ struct Token { return Token::Type::Great; if (name == "<"sv) return Token::Type::Less; + if (name == "\n"sv) + return Token::Type::Newline; return {}; } diff --git a/Userland/Shell/PosixParser.cpp b/Userland/Shell/PosixParser.cpp index 68e4a51f59..711f23f9df 100644 --- a/Userland/Shell/PosixParser.cpp +++ b/Userland/Shell/PosixParser.cpp @@ -698,6 +698,9 @@ ErrorOr> Parser::parse_list() ErrorOr> Parser::parse_and_or() { + while (peek().type == Token::Type::Newline) + skip(); + auto node = TRY(parse_pipeline()); if (!node) return RefPtr {}; diff --git a/Userland/Shell/PosixParser.h b/Userland/Shell/PosixParser.h index a45a0cbc4a..462e3dc319 100644 --- a/Userland/Shell/PosixParser.h +++ b/Userland/Shell/PosixParser.h @@ -54,6 +54,7 @@ private: { if (eof()) return; + handle_heredoc_contents(); m_token_index++; } bool eof() const