Formatter.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Formatter.h"
  27. #include "AST.h"
  28. #include "Parser.h"
  29. #include <AK/TemporaryChange.h>
  30. namespace Shell {
  31. String Formatter::format()
  32. {
  33. auto node = Parser(m_source).parse();
  34. if (m_cursor >= 0)
  35. m_output_cursor = m_cursor;
  36. if (!node)
  37. return String();
  38. if (node->is_syntax_error())
  39. return m_source;
  40. if (m_cursor >= 0) {
  41. auto hit_test = node->hit_test_position(m_cursor);
  42. if (hit_test.matching_node)
  43. m_hit_node = hit_test.matching_node.ptr();
  44. else
  45. m_hit_node = nullptr;
  46. }
  47. m_parent_node = nullptr;
  48. node->visit(*this);
  49. auto string = m_builder.string_view();
  50. if (!string.ends_with(" "))
  51. m_builder.append(m_trivia);
  52. return m_builder.to_string();
  53. }
  54. void Formatter::with_added_indent(int indent, Function<void()> callback)
  55. {
  56. TemporaryChange indent_change { m_current_indent, m_current_indent + indent };
  57. callback();
  58. }
  59. void Formatter::in_new_block(Function<void()> callback)
  60. {
  61. current_builder().append('{');
  62. with_added_indent(1, [&] {
  63. insert_separator();
  64. callback();
  65. });
  66. insert_separator();
  67. current_builder().append('}');
  68. }
  69. void Formatter::test_and_update_output_cursor(const AST::Node* node)
  70. {
  71. if (!node)
  72. return;
  73. if (node != m_hit_node)
  74. return;
  75. m_output_cursor = current_builder().length() + m_cursor - node->position().start_offset;
  76. }
  77. void Formatter::visited(const AST::Node* node)
  78. {
  79. m_last_visited_node = node;
  80. }
  81. void Formatter::will_visit(const AST::Node* node)
  82. {
  83. if (!m_last_visited_node)
  84. return;
  85. if (!node)
  86. return;
  87. auto direct_sequence_child = !m_parent_node || m_parent_node->kind() == AST::Node::Kind::Sequence;
  88. if (direct_sequence_child && node->kind() != AST::Node::Kind::Sequence) {
  89. // Collapse more than one empty line to a single one.
  90. if (node->position().start_line.line_number - m_last_visited_node->position().end_line.line_number > 1)
  91. current_builder().append('\n');
  92. }
  93. }
  94. void Formatter::insert_separator()
  95. {
  96. current_builder().append('\n');
  97. insert_indent();
  98. }
  99. void Formatter::insert_indent()
  100. {
  101. for (size_t i = 0; i < m_current_indent; ++i)
  102. current_builder().append(" ");
  103. }
  104. void Formatter::visit(const AST::PathRedirectionNode* node)
  105. {
  106. will_visit(node);
  107. test_and_update_output_cursor(node);
  108. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  109. NodeVisitor::visit(node);
  110. visited(node);
  111. }
  112. void Formatter::visit(const AST::And* node)
  113. {
  114. will_visit(node);
  115. test_and_update_output_cursor(node);
  116. auto should_indent = m_parent_node && m_parent_node->kind() != AST::Node::Kind::And;
  117. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  118. with_added_indent(should_indent ? 1 : 0, [&] {
  119. node->left()->visit(*this);
  120. current_builder().append(" \\");
  121. insert_separator();
  122. current_builder().append("&& ");
  123. node->right()->visit(*this);
  124. });
  125. visited(node);
  126. }
  127. void Formatter::visit(const AST::ListConcatenate* node)
  128. {
  129. will_visit(node);
  130. test_and_update_output_cursor(node);
  131. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  132. auto first = true;
  133. for (auto& subnode : node->list()) {
  134. if (!first)
  135. current_builder().append(' ');
  136. first = false;
  137. subnode->visit(*this);
  138. }
  139. visited(node);
  140. }
  141. void Formatter::visit(const AST::Background* node)
  142. {
  143. will_visit(node);
  144. test_and_update_output_cursor(node);
  145. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  146. NodeVisitor::visit(node);
  147. current_builder().append(" &");
  148. visited(node);
  149. }
  150. void Formatter::visit(const AST::BarewordLiteral* node)
  151. {
  152. will_visit(node);
  153. test_and_update_output_cursor(node);
  154. current_builder().append(node->text());
  155. visited(node);
  156. }
  157. void Formatter::visit(const AST::BraceExpansion* node)
  158. {
  159. will_visit(node);
  160. test_and_update_output_cursor(node);
  161. current_builder().append('{');
  162. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  163. bool first = true;
  164. for (auto& entry : node->entries()) {
  165. if (!first)
  166. current_builder().append(',');
  167. first = false;
  168. entry.visit(*this);
  169. }
  170. current_builder().append('}');
  171. visited(node);
  172. }
  173. void Formatter::visit(const AST::CastToCommand* node)
  174. {
  175. will_visit(node);
  176. test_and_update_output_cursor(node);
  177. if (m_options.explicit_parentheses)
  178. current_builder().append('(');
  179. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  180. NodeVisitor::visit(node);
  181. if (m_options.explicit_parentheses)
  182. current_builder().append(')');
  183. visited(node);
  184. }
  185. void Formatter::visit(const AST::CastToList* node)
  186. {
  187. will_visit(node);
  188. test_and_update_output_cursor(node);
  189. current_builder().append('(');
  190. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  191. NodeVisitor::visit(node);
  192. current_builder().append(')');
  193. visited(node);
  194. }
  195. void Formatter::visit(const AST::CloseFdRedirection* node)
  196. {
  197. will_visit(node);
  198. test_and_update_output_cursor(node);
  199. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  200. current_builder().appendf(" %d>&-", node->fd());
  201. visited(node);
  202. }
  203. void Formatter::visit(const AST::CommandLiteral*)
  204. {
  205. ASSERT_NOT_REACHED();
  206. }
  207. void Formatter::visit(const AST::Comment* node)
  208. {
  209. will_visit(node);
  210. test_and_update_output_cursor(node);
  211. current_builder().append("#");
  212. current_builder().append(node->text());
  213. visited(node);
  214. }
  215. void Formatter::visit(const AST::ContinuationControl* node)
  216. {
  217. will_visit(node);
  218. test_and_update_output_cursor(node);
  219. if (node->continuation_kind() == AST::ContinuationControl::Break)
  220. current_builder().append("break");
  221. else if (node->continuation_kind() == AST::ContinuationControl::Continue)
  222. current_builder().append("continue");
  223. else
  224. ASSERT_NOT_REACHED();
  225. visited(node);
  226. }
  227. void Formatter::visit(const AST::DynamicEvaluate* node)
  228. {
  229. will_visit(node);
  230. test_and_update_output_cursor(node);
  231. current_builder().append('$');
  232. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  233. NodeVisitor::visit(node);
  234. visited(node);
  235. }
  236. void Formatter::visit(const AST::DoubleQuotedString* node)
  237. {
  238. will_visit(node);
  239. test_and_update_output_cursor(node);
  240. current_builder().append("\"");
  241. TemporaryChange quotes { m_options.in_double_quotes, true };
  242. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  243. NodeVisitor::visit(node);
  244. current_builder().append("\"");
  245. visited(node);
  246. }
  247. void Formatter::visit(const AST::Fd2FdRedirection* node)
  248. {
  249. will_visit(node);
  250. test_and_update_output_cursor(node);
  251. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  252. current_builder().appendf(" %d>&%d", node->source_fd(), node->dest_fd());
  253. if (m_hit_node == node)
  254. ++m_output_cursor;
  255. visited(node);
  256. }
  257. void Formatter::visit(const AST::FunctionDeclaration* node)
  258. {
  259. will_visit(node);
  260. test_and_update_output_cursor(node);
  261. current_builder().append(node->name().name);
  262. current_builder().append('(');
  263. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  264. auto first = true;
  265. for (auto& arg : node->arguments()) {
  266. if (!first)
  267. current_builder().append(' ');
  268. first = false;
  269. current_builder().append(arg.name);
  270. }
  271. current_builder().append(") ");
  272. in_new_block([&] {
  273. if (node->block())
  274. node->block()->visit(*this);
  275. });
  276. visited(node);
  277. }
  278. void Formatter::visit(const AST::ForLoop* node)
  279. {
  280. will_visit(node);
  281. test_and_update_output_cursor(node);
  282. auto is_loop = node->iterated_expression().is_null();
  283. current_builder().append(is_loop ? "loop" : "for ");
  284. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  285. if (!is_loop) {
  286. if (node->variable_name() != "it") {
  287. current_builder().append(node->variable_name());
  288. current_builder().append(" in ");
  289. }
  290. node->iterated_expression()->visit(*this);
  291. }
  292. current_builder().append(' ');
  293. in_new_block([&] {
  294. if (node->block())
  295. node->block()->visit(*this);
  296. });
  297. visited(node);
  298. }
  299. void Formatter::visit(const AST::Glob* node)
  300. {
  301. will_visit(node);
  302. test_and_update_output_cursor(node);
  303. current_builder().append(node->text());
  304. visited(node);
  305. }
  306. void Formatter::visit(const AST::HistoryEvent* node)
  307. {
  308. will_visit(node);
  309. test_and_update_output_cursor(node);
  310. current_builder().append('!');
  311. switch (node->selector().event.kind) {
  312. case AST::HistorySelector::EventKind::ContainingStringLookup:
  313. current_builder().append('?');
  314. current_builder().append(node->selector().event.text);
  315. break;
  316. case AST::HistorySelector::EventKind::StartingStringLookup:
  317. current_builder().append(node->selector().event.text);
  318. break;
  319. case AST::HistorySelector::EventKind::IndexFromStart:
  320. current_builder().append(node->selector().event.text);
  321. break;
  322. case AST::HistorySelector::EventKind::IndexFromEnd:
  323. if (node->selector().event.index == 0)
  324. current_builder().append('!');
  325. else
  326. current_builder().append(node->selector().event.text);
  327. break;
  328. }
  329. auto& range = node->selector().word_selector_range;
  330. if (!range.end.has_value()
  331. || range.end.value().kind != AST::HistorySelector::WordSelectorKind::Last
  332. || range.start.kind != AST::HistorySelector::WordSelectorKind::Index || range.start.selector != 0) {
  333. auto append_word = [this](auto& selector) {
  334. switch (selector.kind) {
  335. case AST::HistorySelector::WordSelectorKind::Index:
  336. if (selector.selector == 0)
  337. current_builder().append('^');
  338. else
  339. current_builder().appendff("{}", selector.selector);
  340. break;
  341. case AST::HistorySelector::WordSelectorKind::Last:
  342. current_builder().append('$');
  343. break;
  344. }
  345. };
  346. current_builder().append(':');
  347. append_word(range.start);
  348. if (range.end.has_value()) {
  349. current_builder().append('-');
  350. append_word(range.end.value());
  351. }
  352. }
  353. visited(node);
  354. }
  355. void Formatter::visit(const AST::Execute* node)
  356. {
  357. will_visit(node);
  358. test_and_update_output_cursor(node);
  359. auto& builder = current_builder();
  360. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  361. ScopedValueRollback options_rollback { m_options };
  362. if (node->does_capture_stdout()) {
  363. builder.append("$");
  364. m_options.explicit_parentheses = true;
  365. }
  366. NodeVisitor::visit(node);
  367. visited(node);
  368. }
  369. void Formatter::visit(const AST::IfCond* node)
  370. {
  371. will_visit(node);
  372. test_and_update_output_cursor(node);
  373. current_builder().append("if ");
  374. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  375. node->condition()->visit(*this);
  376. current_builder().append(' ');
  377. in_new_block([&] {
  378. if (node->true_branch())
  379. node->true_branch()->visit(*this);
  380. });
  381. if (node->false_branch()) {
  382. current_builder().append(" else ");
  383. if (node->false_branch()->kind() != AST::Node::Kind::IfCond) {
  384. in_new_block([&] {
  385. node->false_branch()->visit(*this);
  386. });
  387. } else {
  388. node->false_branch()->visit(*this);
  389. }
  390. } else if (node->else_position().has_value()) {
  391. current_builder().append(" else ");
  392. }
  393. visited(node);
  394. }
  395. void Formatter::visit(const AST::Join* node)
  396. {
  397. will_visit(node);
  398. test_and_update_output_cursor(node);
  399. auto should_parenthesise = m_options.explicit_parentheses;
  400. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  401. TemporaryChange parens { m_options.explicit_parentheses, false };
  402. if (should_parenthesise)
  403. current_builder().append('(');
  404. NodeVisitor::visit(node);
  405. if (should_parenthesise)
  406. current_builder().append(')');
  407. visited(node);
  408. }
  409. void Formatter::visit(const AST::MatchExpr* node)
  410. {
  411. will_visit(node);
  412. test_and_update_output_cursor(node);
  413. current_builder().append("match ");
  414. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  415. node->matched_expr()->visit(*this);
  416. if (!node->expr_name().is_empty()) {
  417. current_builder().append(" as ");
  418. current_builder().append(node->expr_name());
  419. }
  420. current_builder().append(' ');
  421. in_new_block([&] {
  422. auto first_entry = true;
  423. for (auto& entry : node->entries()) {
  424. if (!first_entry)
  425. insert_separator();
  426. first_entry = false;
  427. auto first = true;
  428. for (auto& option : entry.options) {
  429. if (!first)
  430. current_builder().append(" | ");
  431. first = false;
  432. option.visit(*this);
  433. }
  434. current_builder().append(' ');
  435. if (entry.match_names.has_value() && !entry.match_names.value().is_empty()) {
  436. current_builder().append("as (");
  437. auto first = true;
  438. for (auto& name : entry.match_names.value()) {
  439. if (!first)
  440. current_builder().append(' ');
  441. first = false;
  442. current_builder().append(name);
  443. }
  444. current_builder().append(") ");
  445. }
  446. in_new_block([&] {
  447. if (entry.body)
  448. entry.body->visit(*this);
  449. });
  450. }
  451. });
  452. visited(node);
  453. }
  454. void Formatter::visit(const AST::Or* node)
  455. {
  456. will_visit(node);
  457. test_and_update_output_cursor(node);
  458. auto should_indent = m_parent_node && m_parent_node->kind() != AST::Node::Kind::Or;
  459. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  460. with_added_indent(should_indent ? 1 : 0, [&] {
  461. node->left()->visit(*this);
  462. current_builder().append(" \\");
  463. insert_separator();
  464. current_builder().append("|| ");
  465. node->right()->visit(*this);
  466. });
  467. visited(node);
  468. }
  469. void Formatter::visit(const AST::Pipe* node)
  470. {
  471. will_visit(node);
  472. test_and_update_output_cursor(node);
  473. auto should_indent = m_parent_node && m_parent_node->kind() != AST::Node::Kind::Pipe;
  474. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  475. node->left()->visit(*this);
  476. current_builder().append(" \\");
  477. with_added_indent(should_indent ? 1 : 0, [&] {
  478. insert_separator();
  479. current_builder().append("| ");
  480. node->right()->visit(*this);
  481. });
  482. visited(node);
  483. }
  484. void Formatter::visit(const AST::Range* node)
  485. {
  486. will_visit(node);
  487. test_and_update_output_cursor(node);
  488. current_builder().append('{');
  489. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  490. node->start()->visit(*this);
  491. current_builder().append("..");
  492. node->end()->visit(*this);
  493. current_builder().append('}');
  494. visited(node);
  495. }
  496. void Formatter::visit(const AST::ReadRedirection* node)
  497. {
  498. will_visit(node);
  499. test_and_update_output_cursor(node);
  500. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  501. if (node->fd() != 0)
  502. current_builder().appendf(" %d<", node->fd());
  503. else
  504. current_builder().append(" <");
  505. NodeVisitor::visit(node);
  506. visited(node);
  507. }
  508. void Formatter::visit(const AST::ReadWriteRedirection* node)
  509. {
  510. will_visit(node);
  511. test_and_update_output_cursor(node);
  512. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  513. if (node->fd() != 0)
  514. current_builder().appendf(" %d<>", node->fd());
  515. else
  516. current_builder().append(" <>");
  517. NodeVisitor::visit(node);
  518. visited(node);
  519. }
  520. void Formatter::visit(const AST::Sequence* node)
  521. {
  522. will_visit(node);
  523. test_and_update_output_cursor(node);
  524. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  525. bool first = true;
  526. for (auto& entry : node->entries()) {
  527. if (first)
  528. first = false;
  529. else
  530. insert_separator();
  531. entry.visit(*this);
  532. }
  533. visited(node);
  534. }
  535. void Formatter::visit(const AST::Subshell* node)
  536. {
  537. will_visit(node);
  538. test_and_update_output_cursor(node);
  539. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  540. in_new_block([&] {
  541. insert_separator();
  542. NodeVisitor::visit(node);
  543. insert_separator();
  544. });
  545. visited(node);
  546. }
  547. void Formatter::visit(const AST::SimpleVariable* node)
  548. {
  549. will_visit(node);
  550. test_and_update_output_cursor(node);
  551. current_builder().append('$');
  552. current_builder().append(node->name());
  553. visited(node);
  554. }
  555. void Formatter::visit(const AST::SpecialVariable* node)
  556. {
  557. will_visit(node);
  558. test_and_update_output_cursor(node);
  559. current_builder().append('$');
  560. current_builder().append(node->name());
  561. visited(node);
  562. }
  563. void Formatter::visit(const AST::Juxtaposition* node)
  564. {
  565. will_visit(node);
  566. test_and_update_output_cursor(node);
  567. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  568. NodeVisitor::visit(node);
  569. visited(node);
  570. }
  571. void Formatter::visit(const AST::StringLiteral* node)
  572. {
  573. will_visit(node);
  574. test_and_update_output_cursor(node);
  575. if (!m_options.in_double_quotes)
  576. current_builder().append("'");
  577. if (m_options.in_double_quotes) {
  578. for (auto ch : node->text()) {
  579. switch (ch) {
  580. case '"':
  581. case '\\':
  582. case '$':
  583. current_builder().append('\\');
  584. break;
  585. case '\n':
  586. current_builder().append("\\n");
  587. continue;
  588. case '\r':
  589. current_builder().append("\\r");
  590. continue;
  591. case '\t':
  592. current_builder().append("\\t");
  593. continue;
  594. case '\v':
  595. current_builder().append("\\v");
  596. continue;
  597. case '\f':
  598. current_builder().append("\\f");
  599. continue;
  600. case '\a':
  601. current_builder().append("\\a");
  602. continue;
  603. case '\e':
  604. current_builder().append("\\e");
  605. continue;
  606. default:
  607. break;
  608. }
  609. current_builder().append(ch);
  610. }
  611. } else {
  612. current_builder().append(node->text());
  613. }
  614. if (!m_options.in_double_quotes)
  615. current_builder().append("'");
  616. visited(node);
  617. }
  618. void Formatter::visit(const AST::StringPartCompose* node)
  619. {
  620. will_visit(node);
  621. test_and_update_output_cursor(node);
  622. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  623. NodeVisitor::visit(node);
  624. visited(node);
  625. }
  626. void Formatter::visit(const AST::SyntaxError* node)
  627. {
  628. will_visit(node);
  629. test_and_update_output_cursor(node);
  630. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  631. NodeVisitor::visit(node);
  632. visited(node);
  633. }
  634. void Formatter::visit(const AST::Tilde* node)
  635. {
  636. will_visit(node);
  637. test_and_update_output_cursor(node);
  638. current_builder().append(node->text());
  639. visited(node);
  640. }
  641. void Formatter::visit(const AST::VariableDeclarations* node)
  642. {
  643. will_visit(node);
  644. test_and_update_output_cursor(node);
  645. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  646. auto first = true;
  647. for (auto& entry : node->variables()) {
  648. if (!first)
  649. current_builder().append(' ');
  650. first = false;
  651. entry.name->visit(*this);
  652. current_builder().append('=');
  653. if (entry.value->is_command())
  654. current_builder().append('(');
  655. entry.value->visit(*this);
  656. if (entry.value->is_command())
  657. current_builder().append(')');
  658. }
  659. visited(node);
  660. }
  661. void Formatter::visit(const AST::WriteAppendRedirection* node)
  662. {
  663. will_visit(node);
  664. test_and_update_output_cursor(node);
  665. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  666. if (node->fd() != 1)
  667. current_builder().appendf(" %d>>", node->fd());
  668. else
  669. current_builder().append(" >>");
  670. NodeVisitor::visit(node);
  671. visited(node);
  672. }
  673. void Formatter::visit(const AST::WriteRedirection* node)
  674. {
  675. will_visit(node);
  676. test_and_update_output_cursor(node);
  677. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  678. if (node->fd() != 1)
  679. current_builder().appendf(" %d>", node->fd());
  680. else
  681. current_builder().append(" >");
  682. NodeVisitor::visit(node);
  683. visited(node);
  684. }
  685. }