2020-09-16 03:43:04 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-09-16 03:43:04 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Formatter.h"
|
|
|
|
#include "AST.h"
|
|
|
|
#include "Parser.h"
|
2021-04-29 14:35:25 +03:00
|
|
|
#include <AK/Hex.h>
|
2021-03-12 19:29:37 +03:00
|
|
|
#include <AK/ScopedValueRollback.h>
|
2020-09-16 03:43:04 +03:00
|
|
|
#include <AK/TemporaryChange.h>
|
|
|
|
|
2020-10-01 17:43:01 +03:00
|
|
|
namespace Shell {
|
|
|
|
|
2020-09-16 03:43:04 +03:00
|
|
|
String Formatter::format()
|
|
|
|
{
|
2021-03-05 16:03:23 +03:00
|
|
|
auto node = m_root_node ? m_root_node : Parser(m_source).parse();
|
2020-09-16 03:43:04 +03:00
|
|
|
if (m_cursor >= 0)
|
|
|
|
m_output_cursor = m_cursor;
|
|
|
|
|
|
|
|
if (!node)
|
|
|
|
return String();
|
|
|
|
|
|
|
|
if (node->is_syntax_error())
|
|
|
|
return m_source;
|
|
|
|
|
|
|
|
if (m_cursor >= 0) {
|
|
|
|
auto hit_test = node->hit_test_position(m_cursor);
|
|
|
|
if (hit_test.matching_node)
|
|
|
|
m_hit_node = hit_test.matching_node.ptr();
|
|
|
|
else
|
|
|
|
m_hit_node = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_parent_node = nullptr;
|
|
|
|
|
|
|
|
node->visit(*this);
|
|
|
|
|
2021-04-29 14:35:25 +03:00
|
|
|
VERIFY(m_builders.size() == 1);
|
|
|
|
|
|
|
|
auto string = current_builder().string_view();
|
2020-09-16 03:43:04 +03:00
|
|
|
|
2022-07-11 23:10:18 +03:00
|
|
|
if (!string.ends_with(' '))
|
2021-04-29 14:35:25 +03:00
|
|
|
current_builder().append(m_trivia);
|
2020-09-16 03:43:04 +03:00
|
|
|
|
2021-04-29 14:35:25 +03:00
|
|
|
return current_builder().to_string();
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::with_added_indent(int indent, Function<void()> callback)
|
|
|
|
{
|
|
|
|
TemporaryChange indent_change { m_current_indent, m_current_indent + indent };
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::in_new_block(Function<void()> callback)
|
|
|
|
{
|
|
|
|
current_builder().append('{');
|
|
|
|
|
|
|
|
with_added_indent(1, [&] {
|
|
|
|
insert_separator();
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
|
|
|
|
insert_separator();
|
|
|
|
current_builder().append('}');
|
|
|
|
}
|
|
|
|
|
2021-04-29 14:35:25 +03:00
|
|
|
String Formatter::in_new_builder(Function<void()> callback, StringBuilder new_builder)
|
|
|
|
{
|
|
|
|
m_builders.append(move(new_builder));
|
|
|
|
callback();
|
|
|
|
return m_builders.take_last().to_string();
|
|
|
|
}
|
|
|
|
|
2020-09-16 03:43:04 +03:00
|
|
|
void Formatter::test_and_update_output_cursor(const AST::Node* node)
|
|
|
|
{
|
|
|
|
if (!node)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (node != m_hit_node)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_output_cursor = current_builder().length() + m_cursor - node->position().start_offset;
|
|
|
|
}
|
|
|
|
|
2020-10-14 17:55:11 +03:00
|
|
|
void Formatter::visited(const AST::Node* node)
|
|
|
|
{
|
|
|
|
m_last_visited_node = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::will_visit(const AST::Node* node)
|
|
|
|
{
|
|
|
|
if (!m_last_visited_node)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!node)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto direct_sequence_child = !m_parent_node || m_parent_node->kind() == AST::Node::Kind::Sequence;
|
|
|
|
|
2021-03-07 09:15:40 +03:00
|
|
|
if (direct_sequence_child && node->kind() != AST::Node::Kind::Sequence && node->kind() != AST::Node::Kind::Execute) {
|
2020-10-14 17:55:11 +03:00
|
|
|
// Collapse more than one empty line to a single one.
|
|
|
|
if (node->position().start_line.line_number - m_last_visited_node->position().end_line.line_number > 1)
|
2021-03-07 09:15:40 +03:00
|
|
|
insert_separator();
|
2020-10-14 17:55:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 14:35:25 +03:00
|
|
|
void Formatter::insert_separator(bool escaped)
|
2020-09-16 03:43:04 +03:00
|
|
|
{
|
2021-04-29 14:35:25 +03:00
|
|
|
if (escaped)
|
|
|
|
current_builder().append('\\');
|
2020-09-16 03:43:04 +03:00
|
|
|
current_builder().append('\n');
|
2021-04-29 14:35:25 +03:00
|
|
|
if (!escaped && !m_heredocs_to_append_after_sequence.is_empty()) {
|
|
|
|
for (auto& entry : m_heredocs_to_append_after_sequence) {
|
|
|
|
current_builder().append(entry);
|
|
|
|
}
|
|
|
|
m_heredocs_to_append_after_sequence.clear();
|
|
|
|
}
|
2020-09-16 03:43:04 +03:00
|
|
|
insert_indent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::insert_indent()
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_current_indent; ++i)
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append(" "sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::PathRedirectionNode* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
NodeVisitor::visit(node);
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::And* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
2020-10-14 18:55:40 +03:00
|
|
|
auto should_indent = m_parent_node && m_parent_node->kind() != AST::Node::Kind::And;
|
2020-09-16 03:43:04 +03:00
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
|
|
|
with_added_indent(should_indent ? 1 : 0, [&] {
|
|
|
|
node->left()->visit(*this);
|
|
|
|
|
2021-04-29 14:35:25 +03:00
|
|
|
current_builder().append(' ');
|
|
|
|
insert_separator(true);
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("&& "sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
|
|
|
|
node->right()->visit(*this);
|
|
|
|
});
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::ListConcatenate* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
|
|
|
auto first = true;
|
|
|
|
for (auto& subnode : node->list()) {
|
|
|
|
if (!first)
|
|
|
|
current_builder().append(' ');
|
|
|
|
first = false;
|
|
|
|
subnode->visit(*this);
|
|
|
|
}
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::Background* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
NodeVisitor::visit(node);
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append(" &"sv);
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::BarewordLiteral* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
current_builder().append(node->text());
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
2020-10-24 17:43:02 +03:00
|
|
|
void Formatter::visit(const AST::BraceExpansion* node)
|
|
|
|
{
|
|
|
|
will_visit(node);
|
|
|
|
test_and_update_output_cursor(node);
|
2021-03-13 02:40:18 +03:00
|
|
|
if (!m_parent_node || m_parent_node->kind() != AST::Node::Kind::Slice)
|
|
|
|
current_builder().append('{');
|
2020-10-24 17:43:02 +03:00
|
|
|
|
2021-03-13 02:40:18 +03:00
|
|
|
{
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
bool first = true;
|
|
|
|
for (auto& entry : node->entries()) {
|
|
|
|
if (!first)
|
|
|
|
current_builder().append(',');
|
|
|
|
first = false;
|
|
|
|
entry.visit(*this);
|
|
|
|
}
|
2020-10-24 17:43:02 +03:00
|
|
|
}
|
|
|
|
|
2021-03-13 02:40:18 +03:00
|
|
|
if (!m_parent_node || m_parent_node->kind() != AST::Node::Kind::Slice)
|
|
|
|
current_builder().append('}');
|
2020-10-24 17:43:02 +03:00
|
|
|
visited(node);
|
|
|
|
}
|
|
|
|
|
2020-09-16 03:43:04 +03:00
|
|
|
void Formatter::visit(const AST::CastToCommand* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
NodeVisitor::visit(node);
|
|
|
|
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::CastToList* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
current_builder().append('(');
|
|
|
|
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
NodeVisitor::visit(node);
|
|
|
|
|
|
|
|
current_builder().append(')');
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::CloseFdRedirection* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
2021-05-07 21:45:21 +03:00
|
|
|
current_builder().appendff("{}>&-", node->fd());
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::CommandLiteral*)
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::Comment* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("#"sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
current_builder().append(node->text());
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
2020-12-10 17:55:13 +03:00
|
|
|
void Formatter::visit(const AST::ContinuationControl* node)
|
|
|
|
{
|
|
|
|
will_visit(node);
|
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
if (node->continuation_kind() == AST::ContinuationControl::Break)
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("break"sv);
|
2020-12-10 17:55:13 +03:00
|
|
|
else if (node->continuation_kind() == AST::ContinuationControl::Continue)
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("continue"sv);
|
2020-12-10 17:55:13 +03:00
|
|
|
else
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-12-10 17:55:13 +03:00
|
|
|
visited(node);
|
|
|
|
}
|
|
|
|
|
2020-09-16 03:43:04 +03:00
|
|
|
void Formatter::visit(const AST::DynamicEvaluate* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
current_builder().append('$');
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
NodeVisitor::visit(node);
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::DoubleQuotedString* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
2021-04-29 14:35:25 +03:00
|
|
|
auto not_in_heredoc = m_parent_node->kind() != AST::Node::Kind::Heredoc;
|
|
|
|
if (not_in_heredoc)
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("\""sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
|
|
|
|
TemporaryChange quotes { m_options.in_double_quotes, true };
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
|
|
|
NodeVisitor::visit(node);
|
|
|
|
|
2021-04-29 14:35:25 +03:00
|
|
|
if (not_in_heredoc)
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("\""sv);
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::Fd2FdRedirection* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
2021-05-07 21:45:21 +03:00
|
|
|
current_builder().appendff("{}>&{}", node->source_fd(), node->dest_fd());
|
2020-09-16 03:43:04 +03:00
|
|
|
if (m_hit_node == node)
|
|
|
|
++m_output_cursor;
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::FunctionDeclaration* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
current_builder().append(node->name().name);
|
|
|
|
current_builder().append('(');
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
|
|
|
auto first = true;
|
|
|
|
for (auto& arg : node->arguments()) {
|
|
|
|
if (!first)
|
|
|
|
current_builder().append(' ');
|
|
|
|
first = false;
|
|
|
|
current_builder().append(arg.name);
|
|
|
|
}
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append(") "sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
|
|
|
|
in_new_block([&] {
|
|
|
|
if (node->block())
|
|
|
|
node->block()->visit(*this);
|
|
|
|
});
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::ForLoop* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
2020-12-10 17:55:13 +03:00
|
|
|
auto is_loop = node->iterated_expression().is_null();
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append(is_loop ? "loop"sv : "for "sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
2020-12-10 17:55:13 +03:00
|
|
|
if (!is_loop) {
|
2021-03-05 17:55:09 +03:00
|
|
|
if (node->index_variable().has_value()) {
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("index "sv);
|
2021-03-05 17:55:09 +03:00
|
|
|
current_builder().append(node->index_variable()->name);
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append(" "sv);
|
2021-03-05 17:55:09 +03:00
|
|
|
}
|
|
|
|
if (node->variable().has_value() && node->variable()->name != "it") {
|
|
|
|
current_builder().append(node->variable()->name);
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append(" in "sv);
|
2020-12-10 17:55:13 +03:00
|
|
|
}
|
2020-09-16 03:43:04 +03:00
|
|
|
|
2020-12-10 17:55:13 +03:00
|
|
|
node->iterated_expression()->visit(*this);
|
|
|
|
}
|
2020-09-16 03:43:04 +03:00
|
|
|
|
|
|
|
current_builder().append(' ');
|
|
|
|
in_new_block([&] {
|
|
|
|
if (node->block())
|
|
|
|
node->block()->visit(*this);
|
|
|
|
});
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::Glob* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
current_builder().append(node->text());
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
2021-04-29 14:35:25 +03:00
|
|
|
void Formatter::visit(const AST::Heredoc* node)
|
|
|
|
{
|
|
|
|
will_visit(node);
|
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("<<"sv);
|
2021-04-29 14:35:25 +03:00
|
|
|
if (node->deindent())
|
|
|
|
current_builder().append('~');
|
|
|
|
else
|
|
|
|
current_builder().append('-');
|
|
|
|
|
|
|
|
if (node->allow_interpolation())
|
|
|
|
current_builder().appendff("{}", node->end());
|
|
|
|
else
|
|
|
|
current_builder().appendff("'{}'", node->end());
|
|
|
|
|
|
|
|
auto content = in_new_builder([&] {
|
|
|
|
if (!node->contents())
|
|
|
|
return;
|
|
|
|
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
TemporaryChange heredoc { m_options.in_heredoc, true };
|
|
|
|
|
|
|
|
auto& contents = *node->contents();
|
|
|
|
contents.visit(*this);
|
|
|
|
current_builder().appendff("\n{}\n", node->end());
|
|
|
|
});
|
|
|
|
|
|
|
|
m_heredocs_to_append_after_sequence.append(move(content));
|
|
|
|
|
|
|
|
visited(node);
|
|
|
|
}
|
|
|
|
|
2021-01-14 15:31:07 +03:00
|
|
|
void Formatter::visit(const AST::HistoryEvent* node)
|
|
|
|
{
|
|
|
|
will_visit(node);
|
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
|
|
|
|
current_builder().append('!');
|
|
|
|
switch (node->selector().event.kind) {
|
|
|
|
case AST::HistorySelector::EventKind::ContainingStringLookup:
|
|
|
|
current_builder().append('?');
|
|
|
|
current_builder().append(node->selector().event.text);
|
|
|
|
break;
|
|
|
|
case AST::HistorySelector::EventKind::StartingStringLookup:
|
|
|
|
current_builder().append(node->selector().event.text);
|
|
|
|
break;
|
|
|
|
case AST::HistorySelector::EventKind::IndexFromStart:
|
|
|
|
current_builder().append(node->selector().event.text);
|
|
|
|
break;
|
|
|
|
case AST::HistorySelector::EventKind::IndexFromEnd:
|
|
|
|
if (node->selector().event.index == 0)
|
|
|
|
current_builder().append('!');
|
|
|
|
else
|
|
|
|
current_builder().append(node->selector().event.text);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& range = node->selector().word_selector_range;
|
|
|
|
if (!range.end.has_value()
|
|
|
|
|| range.end.value().kind != AST::HistorySelector::WordSelectorKind::Last
|
|
|
|
|| range.start.kind != AST::HistorySelector::WordSelectorKind::Index || range.start.selector != 0) {
|
|
|
|
|
|
|
|
auto append_word = [this](auto& selector) {
|
|
|
|
switch (selector.kind) {
|
|
|
|
case AST::HistorySelector::WordSelectorKind::Index:
|
|
|
|
if (selector.selector == 0)
|
|
|
|
current_builder().append('^');
|
|
|
|
else
|
|
|
|
current_builder().appendff("{}", selector.selector);
|
|
|
|
break;
|
|
|
|
case AST::HistorySelector::WordSelectorKind::Last:
|
|
|
|
current_builder().append('$');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
current_builder().append(':');
|
|
|
|
append_word(range.start);
|
|
|
|
|
|
|
|
if (range.end.has_value()) {
|
|
|
|
current_builder().append('-');
|
|
|
|
append_word(range.end.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
visited(node);
|
|
|
|
}
|
|
|
|
|
2020-09-16 03:43:04 +03:00
|
|
|
void Formatter::visit(const AST::Execute* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
auto& builder = current_builder();
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
ScopedValueRollback options_rollback { m_options };
|
|
|
|
|
2021-03-07 09:15:40 +03:00
|
|
|
if (node->does_capture_stdout())
|
2022-07-11 20:32:29 +03:00
|
|
|
builder.append("$("sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
|
|
|
|
NodeVisitor::visit(node);
|
2021-03-07 09:15:40 +03:00
|
|
|
|
|
|
|
if (node->does_capture_stdout())
|
2022-07-11 20:32:29 +03:00
|
|
|
builder.append(")"sv);
|
2021-03-07 09:15:40 +03:00
|
|
|
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::IfCond* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("if "sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
|
|
|
node->condition()->visit(*this);
|
|
|
|
|
|
|
|
current_builder().append(' ');
|
|
|
|
|
|
|
|
in_new_block([&] {
|
|
|
|
if (node->true_branch())
|
|
|
|
node->true_branch()->visit(*this);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (node->false_branch()) {
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append(" else "sv);
|
2020-10-14 18:55:40 +03:00
|
|
|
if (node->false_branch()->kind() != AST::Node::Kind::IfCond) {
|
2020-09-16 03:43:04 +03:00
|
|
|
in_new_block([&] {
|
|
|
|
node->false_branch()->visit(*this);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
node->false_branch()->visit(*this);
|
|
|
|
}
|
|
|
|
} else if (node->else_position().has_value()) {
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append(" else "sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:03:23 +03:00
|
|
|
void Formatter::visit(const AST::ImmediateExpression* node)
|
|
|
|
{
|
|
|
|
will_visit(node);
|
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("${"sv);
|
2021-03-05 16:03:23 +03:00
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
|
|
|
current_builder().append(node->function_name());
|
|
|
|
|
|
|
|
for (auto& node : node->arguments()) {
|
|
|
|
current_builder().append(' ');
|
|
|
|
node.visit(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->has_closing_brace())
|
|
|
|
current_builder().append('}');
|
|
|
|
|
|
|
|
visited(node);
|
|
|
|
}
|
|
|
|
|
2020-09-16 03:43:04 +03:00
|
|
|
void Formatter::visit(const AST::Join* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
auto should_parenthesise = m_options.explicit_parentheses;
|
|
|
|
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
TemporaryChange parens { m_options.explicit_parentheses, false };
|
|
|
|
|
|
|
|
if (should_parenthesise)
|
|
|
|
current_builder().append('(');
|
|
|
|
|
2021-03-07 09:15:40 +03:00
|
|
|
node->left()->visit(*this);
|
|
|
|
|
|
|
|
current_builder().append(' ');
|
|
|
|
|
|
|
|
node->right()->visit(*this);
|
2020-09-16 03:43:04 +03:00
|
|
|
|
|
|
|
if (should_parenthesise)
|
|
|
|
current_builder().append(')');
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::MatchExpr* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("match "sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
|
|
|
node->matched_expr()->visit(*this);
|
|
|
|
|
|
|
|
if (!node->expr_name().is_empty()) {
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append(" as "sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
current_builder().append(node->expr_name());
|
|
|
|
}
|
|
|
|
|
|
|
|
current_builder().append(' ');
|
|
|
|
in_new_block([&] {
|
|
|
|
auto first_entry = true;
|
|
|
|
for (auto& entry : node->entries()) {
|
|
|
|
if (!first_entry)
|
|
|
|
insert_separator();
|
|
|
|
first_entry = false;
|
|
|
|
auto first = true;
|
2022-04-15 00:20:36 +03:00
|
|
|
entry.options.visit(
|
|
|
|
[&](NonnullRefPtrVector<AST::Node> const& patterns) {
|
|
|
|
for (auto& option : patterns) {
|
|
|
|
if (!first)
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append(" | "sv);
|
2022-04-15 00:20:36 +03:00
|
|
|
first = false;
|
|
|
|
option.visit(*this);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[&](Vector<Regex<ECMA262>> const& patterns) {
|
|
|
|
for (auto& option : patterns) {
|
|
|
|
if (!first)
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append(" | "sv);
|
2022-04-15 00:20:36 +03:00
|
|
|
first = false;
|
|
|
|
auto node = make_ref_counted<AST::BarewordLiteral>(AST::Position {}, option.pattern_value);
|
|
|
|
node->visit(*this);
|
|
|
|
}
|
|
|
|
});
|
2020-09-16 03:43:04 +03:00
|
|
|
|
2020-10-14 17:54:39 +03:00
|
|
|
current_builder().append(' ');
|
2020-10-25 10:12:03 +03:00
|
|
|
if (entry.match_names.has_value() && !entry.match_names.value().is_empty()) {
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("as ("sv);
|
2020-10-25 10:12:03 +03:00
|
|
|
auto first = true;
|
|
|
|
for (auto& name : entry.match_names.value()) {
|
|
|
|
if (!first)
|
|
|
|
current_builder().append(' ');
|
|
|
|
first = false;
|
|
|
|
current_builder().append(name);
|
|
|
|
}
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append(") "sv);
|
2020-10-25 10:12:03 +03:00
|
|
|
}
|
2020-09-16 03:43:04 +03:00
|
|
|
in_new_block([&] {
|
|
|
|
if (entry.body)
|
|
|
|
entry.body->visit(*this);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::Or* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
2020-10-14 18:55:40 +03:00
|
|
|
auto should_indent = m_parent_node && m_parent_node->kind() != AST::Node::Kind::Or;
|
2020-09-16 03:43:04 +03:00
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
|
|
|
with_added_indent(should_indent ? 1 : 0, [&] {
|
|
|
|
node->left()->visit(*this);
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append(" "sv);
|
2021-04-29 14:35:25 +03:00
|
|
|
insert_separator(true);
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("|| "sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
|
|
|
|
node->right()->visit(*this);
|
|
|
|
});
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::Pipe* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
2020-10-14 18:55:40 +03:00
|
|
|
auto should_indent = m_parent_node && m_parent_node->kind() != AST::Node::Kind::Pipe;
|
2020-09-16 03:43:04 +03:00
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
|
|
|
node->left()->visit(*this);
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append(" "sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
|
|
|
|
with_added_indent(should_indent ? 1 : 0, [&] {
|
2021-04-29 14:35:25 +03:00
|
|
|
insert_separator(true);
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("| "sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
|
|
|
|
node->right()->visit(*this);
|
|
|
|
});
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
2020-10-24 17:43:02 +03:00
|
|
|
void Formatter::visit(const AST::Range* node)
|
|
|
|
{
|
|
|
|
will_visit(node);
|
|
|
|
test_and_update_output_cursor(node);
|
2021-03-13 02:40:18 +03:00
|
|
|
if (!m_parent_node || m_parent_node->kind() != AST::Node::Kind::Slice)
|
|
|
|
current_builder().append('{');
|
2020-10-24 17:43:02 +03:00
|
|
|
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
node->start()->visit(*this);
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append(".."sv);
|
2020-10-24 17:43:02 +03:00
|
|
|
node->end()->visit(*this);
|
|
|
|
|
2021-03-13 02:40:18 +03:00
|
|
|
if (!m_parent_node || m_parent_node->kind() != AST::Node::Kind::Slice)
|
|
|
|
current_builder().append('}');
|
2020-10-24 17:43:02 +03:00
|
|
|
visited(node);
|
|
|
|
}
|
|
|
|
|
2020-09-16 03:43:04 +03:00
|
|
|
void Formatter::visit(const AST::ReadRedirection* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
|
|
|
if (node->fd() != 0)
|
2021-05-07 21:45:21 +03:00
|
|
|
current_builder().appendff(" {}<", node->fd());
|
2020-09-16 03:43:04 +03:00
|
|
|
else
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append(" <"sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
NodeVisitor::visit(node);
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::ReadWriteRedirection* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
|
|
|
if (node->fd() != 0)
|
2021-05-07 21:45:21 +03:00
|
|
|
current_builder().appendff(" {}<>", node->fd());
|
2020-09-16 03:43:04 +03:00
|
|
|
else
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append(" <>"sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
NodeVisitor::visit(node);
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::Sequence* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
2021-01-16 22:50:52 +03:00
|
|
|
bool first = true;
|
|
|
|
for (auto& entry : node->entries()) {
|
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
else
|
|
|
|
insert_separator();
|
|
|
|
|
|
|
|
entry.visit(*this);
|
|
|
|
}
|
|
|
|
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::Subshell* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
|
|
|
in_new_block([&] {
|
|
|
|
NodeVisitor::visit(node);
|
|
|
|
});
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
2021-03-13 02:40:18 +03:00
|
|
|
void Formatter::visit(const AST::Slice* node)
|
|
|
|
{
|
|
|
|
will_visit(node);
|
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
|
|
|
current_builder().append('[');
|
|
|
|
node->selector()->visit(*this);
|
|
|
|
current_builder().append(']');
|
|
|
|
|
|
|
|
visited(node);
|
|
|
|
}
|
|
|
|
|
2020-09-16 03:43:04 +03:00
|
|
|
void Formatter::visit(const AST::SimpleVariable* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
current_builder().append('$');
|
|
|
|
current_builder().append(node->name());
|
2021-03-13 02:40:18 +03:00
|
|
|
if (const AST::Node* slice = node->slice())
|
|
|
|
slice->visit(*this);
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::SpecialVariable* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
current_builder().append('$');
|
|
|
|
current_builder().append(node->name());
|
2021-03-13 02:40:18 +03:00
|
|
|
if (const AST::Node* slice = node->slice())
|
|
|
|
slice->visit(*this);
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::Juxtaposition* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
NodeVisitor::visit(node);
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::StringLiteral* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
2021-04-29 14:35:25 +03:00
|
|
|
if (!m_options.in_double_quotes && !m_options.in_heredoc)
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("'"sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
|
2021-04-29 14:35:25 +03:00
|
|
|
if (m_options.in_double_quotes && !m_options.in_heredoc) {
|
2020-09-16 03:43:04 +03:00
|
|
|
for (auto ch : node->text()) {
|
|
|
|
switch (ch) {
|
|
|
|
case '"':
|
|
|
|
case '\\':
|
|
|
|
case '$':
|
|
|
|
current_builder().append('\\');
|
|
|
|
break;
|
|
|
|
case '\n':
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("\\n"sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
continue;
|
|
|
|
case '\r':
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("\\r"sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
continue;
|
|
|
|
case '\t':
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("\\t"sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
continue;
|
|
|
|
case '\v':
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("\\v"sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
continue;
|
|
|
|
case '\f':
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("\\f"sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
continue;
|
|
|
|
case '\a':
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("\\a"sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
continue;
|
|
|
|
case '\e':
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("\\e"sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
current_builder().append(ch);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
current_builder().append(node->text());
|
|
|
|
}
|
|
|
|
|
2021-04-29 14:35:25 +03:00
|
|
|
if (!m_options.in_double_quotes && !m_options.in_heredoc)
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append("'"sv);
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::StringPartCompose* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
NodeVisitor::visit(node);
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::SyntaxError* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
NodeVisitor::visit(node);
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::Tilde* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
current_builder().append(node->text());
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::VariableDeclarations* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
|
|
|
auto first = true;
|
|
|
|
for (auto& entry : node->variables()) {
|
|
|
|
if (!first)
|
|
|
|
current_builder().append(' ');
|
|
|
|
first = false;
|
|
|
|
entry.name->visit(*this);
|
|
|
|
current_builder().append('=');
|
|
|
|
|
|
|
|
if (entry.value->is_command())
|
|
|
|
current_builder().append('(');
|
|
|
|
|
|
|
|
entry.value->visit(*this);
|
|
|
|
|
|
|
|
if (entry.value->is_command())
|
|
|
|
current_builder().append(')');
|
|
|
|
}
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::WriteAppendRedirection* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
|
|
|
if (node->fd() != 1)
|
2021-05-07 21:45:21 +03:00
|
|
|
current_builder().appendff(" {}>>", node->fd());
|
2020-09-16 03:43:04 +03:00
|
|
|
else
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append(" >>"sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
NodeVisitor::visit(node);
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Formatter::visit(const AST::WriteRedirection* node)
|
|
|
|
{
|
2020-10-14 17:55:11 +03:00
|
|
|
will_visit(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
test_and_update_output_cursor(node);
|
|
|
|
TemporaryChange<const AST::Node*> parent { m_parent_node, node };
|
|
|
|
|
|
|
|
if (node->fd() != 1)
|
2021-05-07 21:45:21 +03:00
|
|
|
current_builder().appendff(" {}>", node->fd());
|
2020-09-16 03:43:04 +03:00
|
|
|
else
|
2022-07-11 20:32:29 +03:00
|
|
|
current_builder().append(" >"sv);
|
2020-09-16 03:43:04 +03:00
|
|
|
NodeVisitor::visit(node);
|
2020-10-14 17:55:11 +03:00
|
|
|
visited(node);
|
2020-09-16 03:43:04 +03:00
|
|
|
}
|
2020-10-01 17:43:01 +03:00
|
|
|
|
|
|
|
}
|