Skip to content

Commit

Permalink
Shell: Allow Syntax errors to be mutated while parsing
Browse files Browse the repository at this point in the history
Some nodes (such as heredocs) cannot be validated immediately, so the
entire tree will need to be revalidated if we don't allow mutating
syntax errors.
  • Loading branch information
alimpfard authored and awesomekling committed Apr 29, 2021
1 parent 39369f1 commit 0d74255
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
22 changes: 20 additions & 2 deletions Userland/Shell/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,25 @@ static Vector<String> resolve_slices(RefPtr<Shell> shell, Vector<String>&& value
return move(values);
}

void Node::clear_syntax_error()
{
m_syntax_error_node->clear_syntax_error();
}

void Node::set_is_syntax_error(const SyntaxError& error_node)
{
if (!m_syntax_error_node) {
m_syntax_error_node = error_node;
} else {
m_syntax_error_node->set_is_syntax_error(error_node);
}
}

bool Node::is_syntax_error() const
{
return m_syntax_error_node && m_syntax_error_node->is_syntax_error();
}

void Node::for_each_entry(RefPtr<Shell> shell, Function<IterationDecision(NonnullRefPtr<Value>)> callback)
{
auto value = run(shell)->resolve_without_cast(shell);
Expand Down Expand Up @@ -1884,7 +1903,7 @@ ImmediateExpression::ImmediateExpression(Position position, NameWithPosition fun
, m_function(move(function))
, m_closing_brace_position(move(closing_brace_position))
{
if (m_is_syntax_error)
if (is_syntax_error())
return;

for (auto& argument : m_arguments) {
Expand Down Expand Up @@ -3021,7 +3040,6 @@ SyntaxError::SyntaxError(Position position, String error, bool is_continuable)
, m_syntax_error_text(move(error))
, m_is_continuable(is_continuable)
{
m_is_syntax_error = true;
}

const SyntaxError& SyntaxError::syntax_error_node() const
Expand Down
32 changes: 21 additions & 11 deletions Userland/Shell/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,9 @@ class TildeValue final : public Value {
};

class Node : public RefCounted<Node> {
AK_MAKE_NONCOPYABLE(Node);
AK_MAKE_NONMOVABLE(Node);

public:
virtual void dump(int level) const = 0;
virtual void for_each_entry(RefPtr<Shell> shell, Function<IterationDecision(NonnullRefPtr<Value>)> callback);
Expand All @@ -431,20 +434,15 @@ class Node : public RefCounted<Node> {
virtual bool is_tilde() const { return false; }
virtual bool is_variable_decls() const { return false; }
virtual bool is_simple_variable() const { return false; }
virtual bool is_syntax_error() const { return m_is_syntax_error; }
virtual bool is_syntax_error() const;

virtual bool is_list() const { return false; }
virtual bool would_execute() const { return false; }
virtual bool should_override_execution_in_current_process() const { return false; }

const Position& position() const { return m_position; }
void set_is_syntax_error(const SyntaxError& error_node)
{
if (!m_is_syntax_error) {
m_is_syntax_error = true;
m_syntax_error_node = error_node;
}
}
virtual void clear_syntax_error();
virtual void set_is_syntax_error(const SyntaxError& error_node);
virtual const SyntaxError& syntax_error_node() const
{
VERIFY(is_syntax_error());
Expand Down Expand Up @@ -508,8 +506,7 @@ class Node : public RefCounted<Node> {

protected:
Position m_position;
bool m_is_syntax_error { false };
RefPtr<const SyntaxError> m_syntax_error_node;
RefPtr<SyntaxError> m_syntax_error_node;
};

#define NODE(name) \
Expand Down Expand Up @@ -1363,17 +1360,30 @@ class SyntaxError final : public Node {
const String& error_text() const { return m_syntax_error_text; }
bool is_continuable() const { return m_is_continuable; }

virtual void clear_syntax_error() override
{
m_is_cleared = true;
}
virtual void set_is_syntax_error(const SyntaxError& error) override
{
m_is_cleared = error.m_is_cleared;
m_is_continuable = error.m_is_continuable;
m_syntax_error_text = error.error_text();
}

virtual bool is_syntax_error() const override { return !m_is_cleared; }

private:
NODE(SyntaxError);
virtual void dump(int level) const override;
virtual RefPtr<Value> run(RefPtr<Shell>) override;
virtual void highlight_in_editor(Line::Editor&, Shell&, HighlightMetadata = {}) override;
virtual HitTestResult hit_test_position(size_t) const override { return { nullptr, nullptr, nullptr }; }
virtual bool is_syntax_error() const override { return true; }
virtual const SyntaxError& syntax_error_node() const override;

String m_syntax_error_text;
bool m_is_continuable { false };
bool m_is_cleared { false };
};

class SyntheticNode final : public Node {
Expand Down

0 comments on commit 0d74255

Please sign in to comment.