From 4c6a97e757ab32e750b94d7c7880c44624fa310a Mon Sep 17 00:00:00 2001 From: sin-ack Date: Wed, 11 Aug 2021 21:40:26 +0000 Subject: Shell: Make caller specify the string parsing end condition Heredocs have a different parse end condition than double-quoted strings. parse_doublequoted_string_inner would assume that a string would always end in a double quote, so let's generalize it to parse_string_inner and have it take a StringEndCondition enum which specifies how the string terminates. --- Userland/Shell/Parser.cpp | 14 +++++++++----- Userland/Shell/Parser.h | 8 +++++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Userland/Shell/Parser.cpp b/Userland/Shell/Parser.cpp index e5cf0f496a..0f3bd2b5b6 100644 --- a/Userland/Shell/Parser.cpp +++ b/Userland/Shell/Parser.cpp @@ -1257,7 +1257,7 @@ RefPtr Parser::parse_string() if (peek() == '"') { consume(); - auto inner = parse_doublequoted_string_inner(); + auto inner = parse_string_inner(StringEndCondition::DoubleQuote); if (!inner) inner = create("Unexpected EOF in string", true); if (!expect('"')) { @@ -1283,14 +1283,18 @@ RefPtr Parser::parse_string() return nullptr; } -RefPtr Parser::parse_doublequoted_string_inner() +RefPtr Parser::parse_string_inner(StringEndCondition condition) { auto rule_start = push_start(); if (at_end()) return nullptr; StringBuilder builder; - while (!at_end() && peek() != '"') { + while (!at_end()) { + if (condition == StringEndCondition::DoubleQuote && peek() == '"') { + break; + } + if (peek() == '\\') { consume(); if (at_end()) { @@ -1358,7 +1362,7 @@ RefPtr Parser::parse_doublequoted_string_inner() move(string_literal), move(node)); // Compose String Node - if (auto string = parse_doublequoted_string_inner()) { + if (auto string = parse_string_inner(condition)) { return create(move(inner), string.release_nonnull()); // Compose Composition Composition } @@ -2083,7 +2087,7 @@ bool Parser::parse_heredoc_entries() return false; })); - auto expr = parse_doublequoted_string_inner(); + auto expr = parse_string_inner(StringEndCondition::Heredoc); set_end_condition(move(end_condition)); if (found_key) { diff --git a/Userland/Shell/Parser.h b/Userland/Shell/Parser.h index de230c5f2c..2c11d23a92 100644 --- a/Userland/Shell/Parser.h +++ b/Userland/Shell/Parser.h @@ -40,6 +40,12 @@ private: Yes, No, }; + + enum class StringEndCondition { + DoubleQuote, + Heredoc, + }; + struct SequenceParseResult { NonnullRefPtrVector entries; Vector separator_positions; @@ -76,7 +82,7 @@ private: RefPtr parse_expression(); RefPtr parse_string_composite(); RefPtr parse_string(); - RefPtr parse_doublequoted_string_inner(); + RefPtr parse_string_inner(StringEndCondition); RefPtr parse_variable(); RefPtr parse_variable_ref(); RefPtr parse_slice(); -- cgit v1.2.3