Formatter.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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 = m_root_node ? m_root_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 && node->kind() != AST::Node::Kind::Execute) {
  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. insert_separator();
  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. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  178. NodeVisitor::visit(node);
  179. visited(node);
  180. }
  181. void Formatter::visit(const AST::CastToList* node)
  182. {
  183. will_visit(node);
  184. test_and_update_output_cursor(node);
  185. current_builder().append('(');
  186. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  187. NodeVisitor::visit(node);
  188. current_builder().append(')');
  189. visited(node);
  190. }
  191. void Formatter::visit(const AST::CloseFdRedirection* node)
  192. {
  193. will_visit(node);
  194. test_and_update_output_cursor(node);
  195. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  196. current_builder().appendf("%d>&-", node->fd());
  197. visited(node);
  198. }
  199. void Formatter::visit(const AST::CommandLiteral*)
  200. {
  201. VERIFY_NOT_REACHED();
  202. }
  203. void Formatter::visit(const AST::Comment* node)
  204. {
  205. will_visit(node);
  206. test_and_update_output_cursor(node);
  207. current_builder().append("#");
  208. current_builder().append(node->text());
  209. visited(node);
  210. }
  211. void Formatter::visit(const AST::ContinuationControl* node)
  212. {
  213. will_visit(node);
  214. test_and_update_output_cursor(node);
  215. if (node->continuation_kind() == AST::ContinuationControl::Break)
  216. current_builder().append("break");
  217. else if (node->continuation_kind() == AST::ContinuationControl::Continue)
  218. current_builder().append("continue");
  219. else
  220. VERIFY_NOT_REACHED();
  221. visited(node);
  222. }
  223. void Formatter::visit(const AST::DynamicEvaluate* node)
  224. {
  225. will_visit(node);
  226. test_and_update_output_cursor(node);
  227. current_builder().append('$');
  228. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  229. NodeVisitor::visit(node);
  230. visited(node);
  231. }
  232. void Formatter::visit(const AST::DoubleQuotedString* node)
  233. {
  234. will_visit(node);
  235. test_and_update_output_cursor(node);
  236. current_builder().append("\"");
  237. TemporaryChange quotes { m_options.in_double_quotes, true };
  238. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  239. NodeVisitor::visit(node);
  240. current_builder().append("\"");
  241. visited(node);
  242. }
  243. void Formatter::visit(const AST::Fd2FdRedirection* node)
  244. {
  245. will_visit(node);
  246. test_and_update_output_cursor(node);
  247. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  248. current_builder().appendf("%d>&%d", node->source_fd(), node->dest_fd());
  249. if (m_hit_node == node)
  250. ++m_output_cursor;
  251. visited(node);
  252. }
  253. void Formatter::visit(const AST::FunctionDeclaration* node)
  254. {
  255. will_visit(node);
  256. test_and_update_output_cursor(node);
  257. current_builder().append(node->name().name);
  258. current_builder().append('(');
  259. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  260. auto first = true;
  261. for (auto& arg : node->arguments()) {
  262. if (!first)
  263. current_builder().append(' ');
  264. first = false;
  265. current_builder().append(arg.name);
  266. }
  267. current_builder().append(") ");
  268. in_new_block([&] {
  269. if (node->block())
  270. node->block()->visit(*this);
  271. });
  272. visited(node);
  273. }
  274. void Formatter::visit(const AST::ForLoop* node)
  275. {
  276. will_visit(node);
  277. test_and_update_output_cursor(node);
  278. auto is_loop = node->iterated_expression().is_null();
  279. current_builder().append(is_loop ? "loop" : "for ");
  280. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  281. if (!is_loop) {
  282. if (node->variable_name() != "it") {
  283. current_builder().append(node->variable_name());
  284. current_builder().append(" in ");
  285. }
  286. node->iterated_expression()->visit(*this);
  287. }
  288. current_builder().append(' ');
  289. in_new_block([&] {
  290. if (node->block())
  291. node->block()->visit(*this);
  292. });
  293. visited(node);
  294. }
  295. void Formatter::visit(const AST::Glob* node)
  296. {
  297. will_visit(node);
  298. test_and_update_output_cursor(node);
  299. current_builder().append(node->text());
  300. visited(node);
  301. }
  302. void Formatter::visit(const AST::HistoryEvent* node)
  303. {
  304. will_visit(node);
  305. test_and_update_output_cursor(node);
  306. current_builder().append('!');
  307. switch (node->selector().event.kind) {
  308. case AST::HistorySelector::EventKind::ContainingStringLookup:
  309. current_builder().append('?');
  310. current_builder().append(node->selector().event.text);
  311. break;
  312. case AST::HistorySelector::EventKind::StartingStringLookup:
  313. current_builder().append(node->selector().event.text);
  314. break;
  315. case AST::HistorySelector::EventKind::IndexFromStart:
  316. current_builder().append(node->selector().event.text);
  317. break;
  318. case AST::HistorySelector::EventKind::IndexFromEnd:
  319. if (node->selector().event.index == 0)
  320. current_builder().append('!');
  321. else
  322. current_builder().append(node->selector().event.text);
  323. break;
  324. }
  325. auto& range = node->selector().word_selector_range;
  326. if (!range.end.has_value()
  327. || range.end.value().kind != AST::HistorySelector::WordSelectorKind::Last
  328. || range.start.kind != AST::HistorySelector::WordSelectorKind::Index || range.start.selector != 0) {
  329. auto append_word = [this](auto& selector) {
  330. switch (selector.kind) {
  331. case AST::HistorySelector::WordSelectorKind::Index:
  332. if (selector.selector == 0)
  333. current_builder().append('^');
  334. else
  335. current_builder().appendff("{}", selector.selector);
  336. break;
  337. case AST::HistorySelector::WordSelectorKind::Last:
  338. current_builder().append('$');
  339. break;
  340. }
  341. };
  342. current_builder().append(':');
  343. append_word(range.start);
  344. if (range.end.has_value()) {
  345. current_builder().append('-');
  346. append_word(range.end.value());
  347. }
  348. }
  349. visited(node);
  350. }
  351. void Formatter::visit(const AST::Execute* node)
  352. {
  353. will_visit(node);
  354. test_and_update_output_cursor(node);
  355. auto& builder = current_builder();
  356. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  357. ScopedValueRollback options_rollback { m_options };
  358. if (node->does_capture_stdout())
  359. builder.append("$(");
  360. NodeVisitor::visit(node);
  361. if (node->does_capture_stdout())
  362. builder.append(")");
  363. visited(node);
  364. }
  365. void Formatter::visit(const AST::IfCond* node)
  366. {
  367. will_visit(node);
  368. test_and_update_output_cursor(node);
  369. current_builder().append("if ");
  370. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  371. node->condition()->visit(*this);
  372. current_builder().append(' ');
  373. in_new_block([&] {
  374. if (node->true_branch())
  375. node->true_branch()->visit(*this);
  376. });
  377. if (node->false_branch()) {
  378. current_builder().append(" else ");
  379. if (node->false_branch()->kind() != AST::Node::Kind::IfCond) {
  380. in_new_block([&] {
  381. node->false_branch()->visit(*this);
  382. });
  383. } else {
  384. node->false_branch()->visit(*this);
  385. }
  386. } else if (node->else_position().has_value()) {
  387. current_builder().append(" else ");
  388. }
  389. visited(node);
  390. }
  391. void Formatter::visit(const AST::ImmediateExpression* node)
  392. {
  393. will_visit(node);
  394. test_and_update_output_cursor(node);
  395. current_builder().append("${");
  396. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  397. current_builder().append(node->function_name());
  398. for (auto& node : node->arguments()) {
  399. current_builder().append(' ');
  400. node.visit(*this);
  401. }
  402. if (node->has_closing_brace())
  403. current_builder().append('}');
  404. visited(node);
  405. }
  406. void Formatter::visit(const AST::Join* node)
  407. {
  408. will_visit(node);
  409. test_and_update_output_cursor(node);
  410. auto should_parenthesise = m_options.explicit_parentheses;
  411. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  412. TemporaryChange parens { m_options.explicit_parentheses, false };
  413. if (should_parenthesise)
  414. current_builder().append('(');
  415. node->left()->visit(*this);
  416. current_builder().append(' ');
  417. node->right()->visit(*this);
  418. if (should_parenthesise)
  419. current_builder().append(')');
  420. visited(node);
  421. }
  422. void Formatter::visit(const AST::MatchExpr* node)
  423. {
  424. will_visit(node);
  425. test_and_update_output_cursor(node);
  426. current_builder().append("match ");
  427. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  428. node->matched_expr()->visit(*this);
  429. if (!node->expr_name().is_empty()) {
  430. current_builder().append(" as ");
  431. current_builder().append(node->expr_name());
  432. }
  433. current_builder().append(' ');
  434. in_new_block([&] {
  435. auto first_entry = true;
  436. for (auto& entry : node->entries()) {
  437. if (!first_entry)
  438. insert_separator();
  439. first_entry = false;
  440. auto first = true;
  441. for (auto& option : entry.options) {
  442. if (!first)
  443. current_builder().append(" | ");
  444. first = false;
  445. option.visit(*this);
  446. }
  447. current_builder().append(' ');
  448. if (entry.match_names.has_value() && !entry.match_names.value().is_empty()) {
  449. current_builder().append("as (");
  450. auto first = true;
  451. for (auto& name : entry.match_names.value()) {
  452. if (!first)
  453. current_builder().append(' ');
  454. first = false;
  455. current_builder().append(name);
  456. }
  457. current_builder().append(") ");
  458. }
  459. in_new_block([&] {
  460. if (entry.body)
  461. entry.body->visit(*this);
  462. });
  463. }
  464. });
  465. visited(node);
  466. }
  467. void Formatter::visit(const AST::Or* node)
  468. {
  469. will_visit(node);
  470. test_and_update_output_cursor(node);
  471. auto should_indent = m_parent_node && m_parent_node->kind() != AST::Node::Kind::Or;
  472. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  473. with_added_indent(should_indent ? 1 : 0, [&] {
  474. node->left()->visit(*this);
  475. current_builder().append(" \\");
  476. insert_separator();
  477. current_builder().append("|| ");
  478. node->right()->visit(*this);
  479. });
  480. visited(node);
  481. }
  482. void Formatter::visit(const AST::Pipe* node)
  483. {
  484. will_visit(node);
  485. test_and_update_output_cursor(node);
  486. auto should_indent = m_parent_node && m_parent_node->kind() != AST::Node::Kind::Pipe;
  487. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  488. node->left()->visit(*this);
  489. current_builder().append(" \\");
  490. with_added_indent(should_indent ? 1 : 0, [&] {
  491. insert_separator();
  492. current_builder().append("| ");
  493. node->right()->visit(*this);
  494. });
  495. visited(node);
  496. }
  497. void Formatter::visit(const AST::Range* node)
  498. {
  499. will_visit(node);
  500. test_and_update_output_cursor(node);
  501. current_builder().append('{');
  502. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  503. node->start()->visit(*this);
  504. current_builder().append("..");
  505. node->end()->visit(*this);
  506. current_builder().append('}');
  507. visited(node);
  508. }
  509. void Formatter::visit(const AST::ReadRedirection* node)
  510. {
  511. will_visit(node);
  512. test_and_update_output_cursor(node);
  513. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  514. if (node->fd() != 0)
  515. current_builder().appendf(" %d<", node->fd());
  516. else
  517. current_builder().append(" <");
  518. NodeVisitor::visit(node);
  519. visited(node);
  520. }
  521. void Formatter::visit(const AST::ReadWriteRedirection* node)
  522. {
  523. will_visit(node);
  524. test_and_update_output_cursor(node);
  525. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  526. if (node->fd() != 0)
  527. current_builder().appendf(" %d<>", node->fd());
  528. else
  529. current_builder().append(" <>");
  530. NodeVisitor::visit(node);
  531. visited(node);
  532. }
  533. void Formatter::visit(const AST::Sequence* node)
  534. {
  535. will_visit(node);
  536. test_and_update_output_cursor(node);
  537. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  538. bool first = true;
  539. for (auto& entry : node->entries()) {
  540. if (first)
  541. first = false;
  542. else
  543. insert_separator();
  544. entry.visit(*this);
  545. }
  546. visited(node);
  547. }
  548. void Formatter::visit(const AST::Subshell* node)
  549. {
  550. will_visit(node);
  551. test_and_update_output_cursor(node);
  552. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  553. in_new_block([&] {
  554. NodeVisitor::visit(node);
  555. });
  556. visited(node);
  557. }
  558. void Formatter::visit(const AST::SimpleVariable* node)
  559. {
  560. will_visit(node);
  561. test_and_update_output_cursor(node);
  562. current_builder().append('$');
  563. current_builder().append(node->name());
  564. visited(node);
  565. }
  566. void Formatter::visit(const AST::SpecialVariable* node)
  567. {
  568. will_visit(node);
  569. test_and_update_output_cursor(node);
  570. current_builder().append('$');
  571. current_builder().append(node->name());
  572. visited(node);
  573. }
  574. void Formatter::visit(const AST::Juxtaposition* node)
  575. {
  576. will_visit(node);
  577. test_and_update_output_cursor(node);
  578. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  579. NodeVisitor::visit(node);
  580. visited(node);
  581. }
  582. void Formatter::visit(const AST::StringLiteral* node)
  583. {
  584. will_visit(node);
  585. test_and_update_output_cursor(node);
  586. if (!m_options.in_double_quotes)
  587. current_builder().append("'");
  588. if (m_options.in_double_quotes) {
  589. for (auto ch : node->text()) {
  590. switch (ch) {
  591. case '"':
  592. case '\\':
  593. case '$':
  594. current_builder().append('\\');
  595. break;
  596. case '\n':
  597. current_builder().append("\\n");
  598. continue;
  599. case '\r':
  600. current_builder().append("\\r");
  601. continue;
  602. case '\t':
  603. current_builder().append("\\t");
  604. continue;
  605. case '\v':
  606. current_builder().append("\\v");
  607. continue;
  608. case '\f':
  609. current_builder().append("\\f");
  610. continue;
  611. case '\a':
  612. current_builder().append("\\a");
  613. continue;
  614. case '\e':
  615. current_builder().append("\\e");
  616. continue;
  617. default:
  618. break;
  619. }
  620. current_builder().append(ch);
  621. }
  622. } else {
  623. current_builder().append(node->text());
  624. }
  625. if (!m_options.in_double_quotes)
  626. current_builder().append("'");
  627. visited(node);
  628. }
  629. void Formatter::visit(const AST::StringPartCompose* node)
  630. {
  631. will_visit(node);
  632. test_and_update_output_cursor(node);
  633. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  634. NodeVisitor::visit(node);
  635. visited(node);
  636. }
  637. void Formatter::visit(const AST::SyntaxError* node)
  638. {
  639. will_visit(node);
  640. test_and_update_output_cursor(node);
  641. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  642. NodeVisitor::visit(node);
  643. visited(node);
  644. }
  645. void Formatter::visit(const AST::Tilde* node)
  646. {
  647. will_visit(node);
  648. test_and_update_output_cursor(node);
  649. current_builder().append(node->text());
  650. visited(node);
  651. }
  652. void Formatter::visit(const AST::VariableDeclarations* node)
  653. {
  654. will_visit(node);
  655. test_and_update_output_cursor(node);
  656. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  657. auto first = true;
  658. for (auto& entry : node->variables()) {
  659. if (!first)
  660. current_builder().append(' ');
  661. first = false;
  662. entry.name->visit(*this);
  663. current_builder().append('=');
  664. if (entry.value->is_command())
  665. current_builder().append('(');
  666. entry.value->visit(*this);
  667. if (entry.value->is_command())
  668. current_builder().append(')');
  669. }
  670. visited(node);
  671. }
  672. void Formatter::visit(const AST::WriteAppendRedirection* node)
  673. {
  674. will_visit(node);
  675. test_and_update_output_cursor(node);
  676. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  677. if (node->fd() != 1)
  678. current_builder().appendf(" %d>>", node->fd());
  679. else
  680. current_builder().append(" >>");
  681. NodeVisitor::visit(node);
  682. visited(node);
  683. }
  684. void Formatter::visit(const AST::WriteRedirection* node)
  685. {
  686. will_visit(node);
  687. test_and_update_output_cursor(node);
  688. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  689. if (node->fd() != 1)
  690. current_builder().appendf(" %d>", node->fd());
  691. else
  692. current_builder().append(" >");
  693. NodeVisitor::visit(node);
  694. visited(node);
  695. }
  696. }