1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 16:35:08 +00:00

Shell: Do not treat the ending newline as part of a comment

This allows the parser to finally parse the entire source into a single
AST.
As a result of allowing comments inside sequences, Sequence is also
marked as would_execute if its left or right node would.
This commit is contained in:
AnotherTest 2020-07-05 19:48:26 +04:30 committed by Andreas Kling
parent 6d17fe38a4
commit f9d3055691
4 changed files with 24 additions and 7 deletions

View file

@ -556,7 +556,7 @@ void Comment::dump(int level) const
RefPtr<Value> Comment::run(RefPtr<Shell>)
{
return create<StringValue>("");
return create<ListValue>({});
}
void Comment::highlight_in_editor(Line::Editor& editor, Shell&, HighlightMetadata)
@ -1229,7 +1229,27 @@ void Sequence::dump(int level) const
RefPtr<Value> Sequence::run(RefPtr<Shell> shell)
{
// If we are to return a job, block on the left one then return the right one.
if (would_execute()) {
RefPtr<AST::Node> execute_node = create<AST::Execute>(m_left->position(), m_left);
auto left_job = execute_node->run(shell);
ASSERT(left_job->is_job());
shell->block_on_job(static_cast<JobValue*>(left_job.ptr())->job());
if (m_right->would_execute())
return m_right->run(shell);
execute_node = create<AST::Execute>(m_right->position(), m_right);
return execute_node->run(shell);
}
auto left = m_left->run(shell)->resolve_as_commands(shell);
// This could happen if a comment is next to a command.
if (left.size() == 1) {
auto& command = left.first();
if (command.argv.is_empty() && command.redirections.is_empty())
return m_right->run(shell);
}
auto right = m_right->run(shell)->resolve_as_commands(shell);
Vector<Command> commands;

View file

@ -699,6 +699,7 @@ private:
virtual HitTestResult hit_test_position(size_t) override;
virtual String class_name() const override { return "Sequence"; }
virtual bool is_list() const override { return true; }
virtual bool would_execute() const override { return m_left->would_execute() || m_right->would_execute(); }
RefPtr<Node> m_left;
RefPtr<Node> m_right;

View file

@ -717,9 +717,6 @@ RefPtr<AST::Node> Parser::parse_comment()
consume();
auto text = consume_while(is_not('\n'));
if (peek() == '\n')
consume();
return create<AST::Comment>(move(text)); // Comment
}

View file

@ -316,9 +316,6 @@ int Shell::run_command(const StringView& cmd)
if (cmd.is_empty())
return 0;
if (cmd.starts_with("#"))
return 0;
auto command = Parser(cmd).parse();
if (!command)
@ -505,6 +502,8 @@ bool Shell::run_file(const String& filename, bool explicitly_invoked)
if (file_result.is_error()) {
if (explicitly_invoked)
fprintf(stderr, "Failed to open %s: %s\n", filename.characters(), file_result.error().characters());
else
dbg() << "open() failed for '" << filename << "' with " << file_result.error();
return false;
}
auto file = file_result.value();