Skip to content

Commit

Permalink
Shell: Do not treat the ending newline as part of a comment
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alimpfard authored and awesomekling committed Jul 6, 2020
1 parent 6d17fe3 commit f9d3055
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
22 changes: 21 additions & 1 deletion Shell/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions Shell/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ class Sequence final : public Node {
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;
Expand Down
3 changes: 0 additions & 3 deletions Shell/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 2 additions & 3 deletions Shell/Shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f9d3055

Please sign in to comment.