1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:28:11 +00:00

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.
This commit is contained in:
sin-ack 2021-08-11 21:40:26 +00:00 committed by Ali Mohammad Pur
parent c419b1ade6
commit 4c6a97e757
2 changed files with 16 additions and 6 deletions

View file

@ -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) {