summaryrefslogtreecommitdiff
path: root/Userland/Shell/Parser.cpp
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 /Userland/Shell/Parser.cpp
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.
Diffstat (limited to 'Userland/Shell/Parser.cpp')
-rw-r--r--Userland/Shell/Parser.cpp14
1 files changed, 9 insertions, 5 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) {