summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsin-ack <sin-ack@users.noreply.github.com>2021-08-11 21:40:26 +0000
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2021-08-13 01:20:35 +0430
commit4c6a97e757ab32e750b94d7c7880c44624fa310a (patch)
tree98039945f5ae70386b2c97d1b55326fa9f818e18
parentc419b1ade6f4fb1e58e3eba814823433eee0448f (diff)
downloadserenity-4c6a97e757ab32e750b94d7c7880c44624fa310a.zip
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.
-rw-r--r--Userland/Shell/Parser.cpp14
-rw-r--r--Userland/Shell/Parser.h8
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<AST::Node> Parser::parse_string()
if (peek() == '"') {
consume();
- auto inner = parse_doublequoted_string_inner();
+ auto inner = parse_string_inner(StringEndCondition::DoubleQuote);
if (!inner)
inner = create<AST::SyntaxError>("Unexpected EOF in string", true);
if (!expect('"')) {
@@ -1283,14 +1283,18 @@ RefPtr<AST::Node> Parser::parse_string()
return nullptr;
}
-RefPtr<AST::Node> Parser::parse_doublequoted_string_inner()
+RefPtr<AST::Node> 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<AST::Node> 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<AST::StringPartCompose>(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<AST::Node> entries;
Vector<AST::Position, 1> separator_positions;
@@ -76,7 +82,7 @@ private:
RefPtr<AST::Node> parse_expression();
RefPtr<AST::Node> parse_string_composite();
RefPtr<AST::Node> parse_string();
- RefPtr<AST::Node> parse_doublequoted_string_inner();
+ RefPtr<AST::Node> parse_string_inner(StringEndCondition);
RefPtr<AST::Node> parse_variable();
RefPtr<AST::Node> parse_variable_ref();
RefPtr<AST::Node> parse_slice();