Formatter.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. String Formatter::format()
  31. {
  32. auto node = Parser(m_source).parse();
  33. if (m_cursor >= 0)
  34. m_output_cursor = m_cursor;
  35. if (!node)
  36. return String();
  37. if (node->is_syntax_error())
  38. return m_source;
  39. if (m_cursor >= 0) {
  40. auto hit_test = node->hit_test_position(m_cursor);
  41. if (hit_test.matching_node)
  42. m_hit_node = hit_test.matching_node.ptr();
  43. else
  44. m_hit_node = nullptr;
  45. }
  46. m_parent_node = nullptr;
  47. node->visit(*this);
  48. auto string = m_builder.string_view();
  49. if (!string.ends_with(" "))
  50. m_builder.append(m_trivia);
  51. return m_builder.to_string();
  52. }
  53. void Formatter::with_added_indent(int indent, Function<void()> callback)
  54. {
  55. TemporaryChange indent_change { m_current_indent, m_current_indent + indent };
  56. callback();
  57. }
  58. void Formatter::in_new_block(Function<void()> callback)
  59. {
  60. current_builder().append('{');
  61. with_added_indent(1, [&] {
  62. insert_separator();
  63. callback();
  64. });
  65. insert_separator();
  66. current_builder().append('}');
  67. }
  68. void Formatter::test_and_update_output_cursor(const AST::Node* node)
  69. {
  70. if (!node)
  71. return;
  72. if (node != m_hit_node)
  73. return;
  74. m_output_cursor = current_builder().length() + m_cursor - node->position().start_offset;
  75. }
  76. void Formatter::insert_separator()
  77. {
  78. current_builder().append('\n');
  79. insert_indent();
  80. }
  81. void Formatter::insert_indent()
  82. {
  83. for (size_t i = 0; i < m_current_indent; ++i)
  84. current_builder().append(" ");
  85. }
  86. void Formatter::visit(const AST::PathRedirectionNode* node)
  87. {
  88. test_and_update_output_cursor(node);
  89. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  90. NodeVisitor::visit(node);
  91. }
  92. void Formatter::visit(const AST::And* node)
  93. {
  94. test_and_update_output_cursor(node);
  95. auto should_indent = m_parent_node && m_parent_node->class_name() != "And";
  96. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  97. with_added_indent(should_indent ? 1 : 0, [&] {
  98. node->left()->visit(*this);
  99. current_builder().append(" \\");
  100. insert_separator();
  101. current_builder().append("&& ");
  102. node->right()->visit(*this);
  103. });
  104. }
  105. void Formatter::visit(const AST::ListConcatenate* node)
  106. {
  107. test_and_update_output_cursor(node);
  108. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  109. auto first = true;
  110. for (auto& subnode : node->list()) {
  111. if (!first)
  112. current_builder().append(' ');
  113. first = false;
  114. subnode->visit(*this);
  115. }
  116. }
  117. void Formatter::visit(const AST::Background* node)
  118. {
  119. test_and_update_output_cursor(node);
  120. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  121. NodeVisitor::visit(node);
  122. current_builder().append(" &");
  123. }
  124. void Formatter::visit(const AST::BarewordLiteral* node)
  125. {
  126. test_and_update_output_cursor(node);
  127. current_builder().append(node->text());
  128. }
  129. void Formatter::visit(const AST::CastToCommand* node)
  130. {
  131. test_and_update_output_cursor(node);
  132. if (m_options.explicit_parentheses)
  133. current_builder().append('(');
  134. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  135. NodeVisitor::visit(node);
  136. if (m_options.explicit_parentheses)
  137. current_builder().append(')');
  138. }
  139. void Formatter::visit(const AST::CastToList* node)
  140. {
  141. test_and_update_output_cursor(node);
  142. current_builder().append('(');
  143. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  144. NodeVisitor::visit(node);
  145. current_builder().append(')');
  146. }
  147. void Formatter::visit(const AST::CloseFdRedirection* node)
  148. {
  149. test_and_update_output_cursor(node);
  150. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  151. current_builder().appendf(" %d>&-", node->fd());
  152. }
  153. void Formatter::visit(const AST::CommandLiteral*)
  154. {
  155. ASSERT_NOT_REACHED();
  156. }
  157. void Formatter::visit(const AST::Comment* node)
  158. {
  159. test_and_update_output_cursor(node);
  160. current_builder().append("#");
  161. current_builder().append(node->text());
  162. }
  163. void Formatter::visit(const AST::DynamicEvaluate* node)
  164. {
  165. test_and_update_output_cursor(node);
  166. current_builder().append('$');
  167. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  168. NodeVisitor::visit(node);
  169. }
  170. void Formatter::visit(const AST::DoubleQuotedString* node)
  171. {
  172. test_and_update_output_cursor(node);
  173. current_builder().append("\"");
  174. TemporaryChange quotes { m_options.in_double_quotes, true };
  175. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  176. NodeVisitor::visit(node);
  177. current_builder().append("\"");
  178. }
  179. void Formatter::visit(const AST::Fd2FdRedirection* node)
  180. {
  181. test_and_update_output_cursor(node);
  182. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  183. current_builder().appendf(" %d>&%d", node->source_fd(), node->dest_fd());
  184. if (m_hit_node == node)
  185. ++m_output_cursor;
  186. }
  187. void Formatter::visit(const AST::FunctionDeclaration* node)
  188. {
  189. test_and_update_output_cursor(node);
  190. current_builder().append(node->name().name);
  191. current_builder().append('(');
  192. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  193. auto first = true;
  194. for (auto& arg : node->arguments()) {
  195. if (!first)
  196. current_builder().append(' ');
  197. first = false;
  198. current_builder().append(arg.name);
  199. }
  200. current_builder().append(") ");
  201. in_new_block([&] {
  202. if (node->block())
  203. node->block()->visit(*this);
  204. });
  205. }
  206. void Formatter::visit(const AST::ForLoop* node)
  207. {
  208. test_and_update_output_cursor(node);
  209. current_builder().append("for ");
  210. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  211. if (node->variable_name() != "it") {
  212. current_builder().append(node->variable_name());
  213. current_builder().append(" in ");
  214. }
  215. node->iterated_expression()->visit(*this);
  216. current_builder().append(' ');
  217. in_new_block([&] {
  218. if (node->block())
  219. node->block()->visit(*this);
  220. });
  221. }
  222. void Formatter::visit(const AST::Glob* node)
  223. {
  224. test_and_update_output_cursor(node);
  225. current_builder().append(node->text());
  226. }
  227. void Formatter::visit(const AST::Execute* node)
  228. {
  229. test_and_update_output_cursor(node);
  230. auto& builder = current_builder();
  231. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  232. ScopedValueRollback options_rollback { m_options };
  233. if (node->does_capture_stdout()) {
  234. builder.append("$");
  235. m_options.explicit_parentheses = true;
  236. }
  237. NodeVisitor::visit(node);
  238. }
  239. void Formatter::visit(const AST::IfCond* node)
  240. {
  241. test_and_update_output_cursor(node);
  242. current_builder().append("if ");
  243. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  244. node->condition()->visit(*this);
  245. current_builder().append(' ');
  246. in_new_block([&] {
  247. if (node->true_branch())
  248. node->true_branch()->visit(*this);
  249. });
  250. if (node->false_branch()) {
  251. current_builder().append(" else ");
  252. if (node->false_branch()->class_name() != "IfCond") {
  253. in_new_block([&] {
  254. node->false_branch()->visit(*this);
  255. });
  256. } else {
  257. node->false_branch()->visit(*this);
  258. }
  259. } else if (node->else_position().has_value()) {
  260. current_builder().append(" else ");
  261. }
  262. }
  263. void Formatter::visit(const AST::Join* node)
  264. {
  265. test_and_update_output_cursor(node);
  266. auto should_parenthesise = m_options.explicit_parentheses;
  267. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  268. TemporaryChange parens { m_options.explicit_parentheses, false };
  269. if (should_parenthesise)
  270. current_builder().append('(');
  271. NodeVisitor::visit(node);
  272. if (should_parenthesise)
  273. current_builder().append(')');
  274. }
  275. void Formatter::visit(const AST::MatchExpr* node)
  276. {
  277. test_and_update_output_cursor(node);
  278. current_builder().append("match ");
  279. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  280. node->matched_expr()->visit(*this);
  281. if (!node->expr_name().is_empty()) {
  282. current_builder().append(" as ");
  283. current_builder().append(node->expr_name());
  284. }
  285. current_builder().append(' ');
  286. in_new_block([&] {
  287. auto first_entry = true;
  288. for (auto& entry : node->entries()) {
  289. if (!first_entry)
  290. insert_separator();
  291. first_entry = false;
  292. auto first = true;
  293. for (auto& option : entry.options) {
  294. if (!first)
  295. current_builder().append(" | ");
  296. first = false;
  297. option.visit(*this);
  298. }
  299. in_new_block([&] {
  300. if (entry.body)
  301. entry.body->visit(*this);
  302. });
  303. }
  304. });
  305. }
  306. void Formatter::visit(const AST::Or* node)
  307. {
  308. test_and_update_output_cursor(node);
  309. auto should_indent = m_parent_node && m_parent_node->class_name() != "Or";
  310. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  311. with_added_indent(should_indent ? 1 : 0, [&] {
  312. node->left()->visit(*this);
  313. current_builder().append(" \\");
  314. insert_separator();
  315. current_builder().append("|| ");
  316. node->right()->visit(*this);
  317. });
  318. }
  319. void Formatter::visit(const AST::Pipe* node)
  320. {
  321. test_and_update_output_cursor(node);
  322. auto should_indent = m_parent_node && m_parent_node->class_name() != "Pipe";
  323. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  324. node->left()->visit(*this);
  325. current_builder().append(" \\");
  326. with_added_indent(should_indent ? 1 : 0, [&] {
  327. insert_separator();
  328. current_builder().append("| ");
  329. node->right()->visit(*this);
  330. });
  331. }
  332. void Formatter::visit(const AST::ReadRedirection* node)
  333. {
  334. test_and_update_output_cursor(node);
  335. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  336. if (node->fd() != 0)
  337. current_builder().appendf(" %d<", node->fd());
  338. else
  339. current_builder().append(" <");
  340. NodeVisitor::visit(node);
  341. }
  342. void Formatter::visit(const AST::ReadWriteRedirection* node)
  343. {
  344. test_and_update_output_cursor(node);
  345. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  346. if (node->fd() != 0)
  347. current_builder().appendf(" %d<>", node->fd());
  348. else
  349. current_builder().append(" <>");
  350. NodeVisitor::visit(node);
  351. }
  352. void Formatter::visit(const AST::Sequence* node)
  353. {
  354. test_and_update_output_cursor(node);
  355. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  356. node->left()->visit(*this);
  357. insert_separator();
  358. node->right()->visit(*this);
  359. }
  360. void Formatter::visit(const AST::Subshell* node)
  361. {
  362. test_and_update_output_cursor(node);
  363. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  364. in_new_block([&] {
  365. insert_separator();
  366. NodeVisitor::visit(node);
  367. insert_separator();
  368. });
  369. }
  370. void Formatter::visit(const AST::SimpleVariable* node)
  371. {
  372. test_and_update_output_cursor(node);
  373. current_builder().append('$');
  374. current_builder().append(node->name());
  375. }
  376. void Formatter::visit(const AST::SpecialVariable* node)
  377. {
  378. test_and_update_output_cursor(node);
  379. current_builder().append('$');
  380. current_builder().append(node->name());
  381. }
  382. void Formatter::visit(const AST::Juxtaposition* node)
  383. {
  384. test_and_update_output_cursor(node);
  385. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  386. NodeVisitor::visit(node);
  387. }
  388. void Formatter::visit(const AST::StringLiteral* node)
  389. {
  390. test_and_update_output_cursor(node);
  391. if (!m_options.in_double_quotes)
  392. current_builder().append("'");
  393. if (m_options.in_double_quotes) {
  394. for (auto ch : node->text()) {
  395. switch (ch) {
  396. case '"':
  397. case '\\':
  398. case '$':
  399. current_builder().append('\\');
  400. break;
  401. case '\n':
  402. current_builder().append("\\n");
  403. continue;
  404. case '\r':
  405. current_builder().append("\\r");
  406. continue;
  407. case '\t':
  408. current_builder().append("\\t");
  409. continue;
  410. case '\v':
  411. current_builder().append("\\v");
  412. continue;
  413. case '\f':
  414. current_builder().append("\\f");
  415. continue;
  416. case '\a':
  417. current_builder().append("\\a");
  418. continue;
  419. case '\e':
  420. current_builder().append("\\e");
  421. continue;
  422. default:
  423. break;
  424. }
  425. current_builder().append(ch);
  426. }
  427. } else {
  428. current_builder().append(node->text());
  429. }
  430. if (!m_options.in_double_quotes)
  431. current_builder().append("'");
  432. }
  433. void Formatter::visit(const AST::StringPartCompose* node)
  434. {
  435. test_and_update_output_cursor(node);
  436. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  437. NodeVisitor::visit(node);
  438. }
  439. void Formatter::visit(const AST::SyntaxError* node)
  440. {
  441. test_and_update_output_cursor(node);
  442. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  443. NodeVisitor::visit(node);
  444. }
  445. void Formatter::visit(const AST::Tilde* node)
  446. {
  447. test_and_update_output_cursor(node);
  448. current_builder().append(node->text());
  449. }
  450. void Formatter::visit(const AST::VariableDeclarations* node)
  451. {
  452. test_and_update_output_cursor(node);
  453. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  454. auto first = true;
  455. for (auto& entry : node->variables()) {
  456. if (!first)
  457. current_builder().append(' ');
  458. first = false;
  459. entry.name->visit(*this);
  460. current_builder().append('=');
  461. if (entry.value->is_command())
  462. current_builder().append('(');
  463. entry.value->visit(*this);
  464. if (entry.value->is_command())
  465. current_builder().append(')');
  466. }
  467. }
  468. void Formatter::visit(const AST::WriteAppendRedirection* node)
  469. {
  470. test_and_update_output_cursor(node);
  471. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  472. if (node->fd() != 1)
  473. current_builder().appendf(" %d>>", node->fd());
  474. else
  475. current_builder().append(" >>");
  476. NodeVisitor::visit(node);
  477. }
  478. void Formatter::visit(const AST::WriteRedirection* node)
  479. {
  480. test_and_update_output_cursor(node);
  481. TemporaryChange<const AST::Node*> parent { m_parent_node, node };
  482. if (node->fd() != 1)
  483. current_builder().appendf(" %d>", node->fd());
  484. else
  485. current_builder().append(" >");
  486. NodeVisitor::visit(node);
  487. }