Parser.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  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 "Parser.h"
  27. #include <ctype.h>
  28. #include <stdio.h>
  29. #include <unistd.h>
  30. Parser::SavedOffset Parser::save_offset() const
  31. {
  32. return { m_offset, m_line };
  33. }
  34. char Parser::peek()
  35. {
  36. if (m_offset == m_input.length())
  37. return 0;
  38. ASSERT(m_offset < m_input.length());
  39. auto ch = m_input[m_offset];
  40. if (ch == '\\' && m_input.length() > m_offset + 1 && m_input[m_offset + 1] == '\n') {
  41. m_offset += 2;
  42. ++m_line.line_number;
  43. m_line.line_column = 0;
  44. return peek();
  45. }
  46. return ch;
  47. }
  48. char Parser::consume()
  49. {
  50. auto ch = peek();
  51. ++m_offset;
  52. if (ch == '\n') {
  53. ++m_line.line_number;
  54. m_line.line_column = 0;
  55. } else {
  56. ++m_line.line_column;
  57. }
  58. return ch;
  59. }
  60. bool Parser::expect(char ch)
  61. {
  62. return expect(StringView { &ch, 1 });
  63. }
  64. bool Parser::expect(const StringView& expected)
  65. {
  66. auto offset_at_start = m_offset;
  67. auto line_at_start = line();
  68. if (expected.length() + m_offset > m_input.length())
  69. return false;
  70. for (size_t i = 0; i < expected.length(); ++i) {
  71. if (peek() != expected[i]) {
  72. restore_to(offset_at_start, line_at_start);
  73. return false;
  74. }
  75. consume();
  76. }
  77. return true;
  78. }
  79. template<typename A, typename... Args>
  80. NonnullRefPtr<A> Parser::create(Args... args)
  81. {
  82. return adopt(*new A(AST::Position { m_rule_start_offsets.last(), m_offset, m_rule_start_lines.last(), line() }, args...));
  83. }
  84. [[nodiscard]] OwnPtr<Parser::ScopedOffset> Parser::push_start()
  85. {
  86. return make<ScopedOffset>(m_rule_start_offsets, m_rule_start_lines, m_offset, m_line.line_number, m_line.line_column);
  87. }
  88. static constexpr bool is_whitespace(char c)
  89. {
  90. return c == ' ' || c == '\t';
  91. }
  92. static constexpr bool is_word_character(char c)
  93. {
  94. return (c <= '9' && c >= '0') || (c <= 'Z' && c >= 'A') || (c <= 'z' && c >= 'a') || c == '_';
  95. }
  96. static constexpr bool is_digit(char c)
  97. {
  98. return c <= '9' && c >= '0';
  99. }
  100. static constexpr auto is_not(char c)
  101. {
  102. return [c](char ch) { return ch != c; };
  103. }
  104. static constexpr auto is_any_of(StringView s)
  105. {
  106. return [s](char ch) { return s.contains(ch); };
  107. }
  108. static inline char to_byte(char a, char b)
  109. {
  110. char buf[3] { a, b, 0 };
  111. return strtol(buf, nullptr, 16);
  112. }
  113. RefPtr<AST::Node> Parser::parse()
  114. {
  115. m_offset = 0;
  116. m_line = { 0, 0 };
  117. auto toplevel = parse_toplevel();
  118. if (m_offset < m_input.length()) {
  119. // Parsing stopped midway, this is a syntax error.
  120. auto error_start = push_start();
  121. consume_while([](auto) { return true; });
  122. auto syntax_error_node = create<AST::SyntaxError>("Unexpected tokens past the end");
  123. if (!toplevel)
  124. toplevel = move(syntax_error_node);
  125. else
  126. toplevel->set_is_syntax_error(*syntax_error_node);
  127. }
  128. return toplevel;
  129. }
  130. RefPtr<AST::Node> Parser::parse_toplevel()
  131. {
  132. auto rule_start = push_start();
  133. if (auto sequence = parse_sequence())
  134. return create<AST::Execute>(sequence.release_nonnull());
  135. return nullptr;
  136. }
  137. RefPtr<AST::Node> Parser::parse_sequence()
  138. {
  139. consume_while(is_any_of(" \t\n;")); // ignore whitespaces or terminators without effect.
  140. auto rule_start = push_start();
  141. auto var_decls = parse_variable_decls();
  142. auto pos_before_seps = save_offset();
  143. switch (peek()) {
  144. case '}':
  145. return var_decls;
  146. case ';':
  147. case '\n': {
  148. if (!var_decls)
  149. break;
  150. consume_while(is_any_of("\n;"));
  151. auto pos_after_seps = save_offset();
  152. auto rest = parse_sequence();
  153. if (rest)
  154. return create<AST::Sequence>(
  155. var_decls.release_nonnull(),
  156. rest.release_nonnull(),
  157. AST::Position { pos_before_seps.offset, pos_after_seps.offset, pos_before_seps.line, pos_after_seps.line });
  158. return var_decls;
  159. }
  160. default:
  161. break;
  162. }
  163. auto first = parse_function_decl();
  164. if (!first)
  165. first = parse_or_logical_sequence();
  166. if (!first)
  167. return var_decls;
  168. if (var_decls)
  169. first = create<AST::Sequence>(
  170. var_decls.release_nonnull(),
  171. first.release_nonnull(),
  172. AST::Position { pos_before_seps.offset, pos_before_seps.offset, pos_before_seps.line, pos_before_seps.line });
  173. consume_while(is_whitespace);
  174. pos_before_seps = save_offset();
  175. switch (peek()) {
  176. case ';':
  177. case '\n': {
  178. consume_while(is_any_of("\n;"));
  179. auto pos_after_seps = save_offset();
  180. if (auto expr = parse_sequence()) {
  181. return create<AST::Sequence>(
  182. first.release_nonnull(),
  183. expr.release_nonnull(),
  184. AST::Position { pos_before_seps.offset, pos_after_seps.offset, pos_before_seps.line, pos_after_seps.line }); // Sequence
  185. }
  186. return first;
  187. }
  188. case '&': {
  189. auto execute_pipe_seq = first->would_execute() ? first.release_nonnull() : static_cast<NonnullRefPtr<AST::Node>>(create<AST::Execute>(first.release_nonnull()));
  190. consume();
  191. auto pos_after_seps = save_offset();
  192. auto bg = create<AST::Background>(execute_pipe_seq); // Execute Background
  193. if (auto rest = parse_sequence())
  194. return create<AST::Sequence>(
  195. move(bg),
  196. rest.release_nonnull(),
  197. AST::Position { pos_before_seps.offset, pos_after_seps.offset, pos_before_seps.line, pos_before_seps.line }); // Sequence Background Sequence
  198. return bg;
  199. }
  200. default:
  201. return first;
  202. }
  203. }
  204. RefPtr<AST::Node> Parser::parse_variable_decls()
  205. {
  206. auto rule_start = push_start();
  207. consume_while(is_whitespace);
  208. auto pos_before_name = save_offset();
  209. auto var_name = consume_while(is_word_character);
  210. if (var_name.is_empty())
  211. return nullptr;
  212. if (!expect('=')) {
  213. restore_to(pos_before_name.offset, pos_before_name.line);
  214. return nullptr;
  215. }
  216. auto name_expr = create<AST::BarewordLiteral>(move(var_name));
  217. auto start = push_start();
  218. auto expression = parse_expression();
  219. if (!expression || expression->is_syntax_error()) {
  220. restore_to(*start);
  221. if (peek() == '(') {
  222. consume();
  223. auto command = parse_pipe_sequence();
  224. if (!command)
  225. restore_to(*start);
  226. else if (!expect(')'))
  227. command->set_is_syntax_error(*create<AST::SyntaxError>("Expected a terminating close paren"));
  228. expression = command;
  229. }
  230. }
  231. if (!expression) {
  232. if (is_whitespace(peek())) {
  233. auto string_start = push_start();
  234. expression = create<AST::StringLiteral>("");
  235. } else {
  236. restore_to(pos_before_name.offset, pos_before_name.line);
  237. return nullptr;
  238. }
  239. }
  240. Vector<AST::VariableDeclarations::Variable> variables;
  241. variables.append({ move(name_expr), expression.release_nonnull() });
  242. if (consume_while(is_whitespace).is_empty())
  243. return create<AST::VariableDeclarations>(move(variables));
  244. auto rest = parse_variable_decls();
  245. if (!rest)
  246. return create<AST::VariableDeclarations>(move(variables));
  247. ASSERT(rest->is_variable_decls());
  248. auto* rest_decl = static_cast<AST::VariableDeclarations*>(rest.ptr());
  249. variables.append(rest_decl->variables());
  250. return create<AST::VariableDeclarations>(move(variables));
  251. }
  252. RefPtr<AST::Node> Parser::parse_function_decl()
  253. {
  254. auto rule_start = push_start();
  255. auto restore = [&] {
  256. restore_to(*rule_start);
  257. return nullptr;
  258. };
  259. consume_while(is_whitespace);
  260. auto pos_before_name = save_offset();
  261. auto function_name = consume_while(is_word_character);
  262. auto pos_after_name = save_offset();
  263. if (function_name.is_empty())
  264. return restore();
  265. if (!expect('('))
  266. return restore();
  267. Vector<AST::FunctionDeclaration::NameWithPosition> arguments;
  268. for (;;) {
  269. consume_while(is_whitespace);
  270. if (expect(')'))
  271. break;
  272. auto name_offset = m_offset;
  273. auto start_line = line();
  274. auto arg_name = consume_while(is_word_character);
  275. if (arg_name.is_empty()) {
  276. // FIXME: Should this be a syntax error, or just return?
  277. return restore();
  278. }
  279. arguments.append({ arg_name, { name_offset, m_offset, start_line, line() } });
  280. }
  281. consume_while(is_whitespace);
  282. {
  283. RefPtr<AST::Node> syntax_error;
  284. {
  285. auto obrace_error_start = push_start();
  286. syntax_error = create<AST::SyntaxError>("Expected an open brace '{' to start a function body");
  287. }
  288. if (!expect('{')) {
  289. return create<AST::FunctionDeclaration>(
  290. AST::FunctionDeclaration::NameWithPosition {
  291. move(function_name),
  292. { pos_before_name.offset, pos_after_name.offset, pos_before_name.line, pos_after_name.line } },
  293. move(arguments),
  294. move(syntax_error));
  295. }
  296. }
  297. auto body = parse_toplevel();
  298. {
  299. RefPtr<AST::SyntaxError> syntax_error;
  300. {
  301. auto cbrace_error_start = push_start();
  302. syntax_error = create<AST::SyntaxError>("Expected a close brace '}' to end a function body");
  303. }
  304. if (!expect('}')) {
  305. if (body)
  306. body->set_is_syntax_error(*syntax_error);
  307. else
  308. body = move(syntax_error);
  309. return create<AST::FunctionDeclaration>(
  310. AST::FunctionDeclaration::NameWithPosition {
  311. move(function_name),
  312. { pos_before_name.offset, pos_after_name.offset, pos_before_name.line, pos_after_name.line } },
  313. move(arguments),
  314. move(body));
  315. }
  316. }
  317. return create<AST::FunctionDeclaration>(
  318. AST::FunctionDeclaration::NameWithPosition {
  319. move(function_name),
  320. { pos_before_name.offset, pos_after_name.offset, pos_before_name.line, pos_after_name.line } },
  321. move(arguments),
  322. move(body));
  323. }
  324. RefPtr<AST::Node> Parser::parse_or_logical_sequence()
  325. {
  326. consume_while(is_whitespace);
  327. auto rule_start = push_start();
  328. auto and_sequence = parse_and_logical_sequence();
  329. if (!and_sequence)
  330. return nullptr;
  331. consume_while(is_whitespace);
  332. auto pos_before_or = save_offset();
  333. if (!expect("||"))
  334. return and_sequence;
  335. auto pos_after_or = save_offset();
  336. auto right_and_sequence = parse_and_logical_sequence();
  337. if (!right_and_sequence)
  338. right_and_sequence = create<AST::SyntaxError>("Expected an expression after '||'");
  339. return create<AST::Or>(
  340. and_sequence.release_nonnull(),
  341. right_and_sequence.release_nonnull(),
  342. AST::Position { pos_before_or.offset, pos_after_or.offset, pos_before_or.line, pos_after_or.line });
  343. }
  344. RefPtr<AST::Node> Parser::parse_and_logical_sequence()
  345. {
  346. consume_while(is_whitespace);
  347. auto rule_start = push_start();
  348. auto pipe_sequence = parse_pipe_sequence();
  349. if (!pipe_sequence)
  350. return nullptr;
  351. consume_while(is_whitespace);
  352. auto pos_before_and = save_offset();
  353. if (!expect("&&"))
  354. return pipe_sequence;
  355. auto pos_after_end = save_offset();
  356. auto right_and_sequence = parse_and_logical_sequence();
  357. if (!right_and_sequence)
  358. right_and_sequence = create<AST::SyntaxError>("Expected an expression after '&&'");
  359. return create<AST::And>(
  360. pipe_sequence.release_nonnull(),
  361. right_and_sequence.release_nonnull(),
  362. AST::Position { pos_before_and.offset, pos_after_end.offset, pos_before_and.line, pos_after_end.line });
  363. }
  364. RefPtr<AST::Node> Parser::parse_pipe_sequence()
  365. {
  366. auto rule_start = push_start();
  367. auto left = parse_control_structure();
  368. if (!left) {
  369. if (auto cmd = parse_command())
  370. left = cmd;
  371. else
  372. return nullptr;
  373. }
  374. consume_while(is_whitespace);
  375. if (peek() != '|')
  376. return left;
  377. auto before_pipe = save_offset();
  378. consume();
  379. if (auto pipe_seq = parse_pipe_sequence()) {
  380. return create<AST::Pipe>(left.release_nonnull(), pipe_seq.release_nonnull()); // Pipe
  381. }
  382. restore_to(before_pipe.offset, before_pipe.line);
  383. return left;
  384. }
  385. RefPtr<AST::Node> Parser::parse_command()
  386. {
  387. auto rule_start = push_start();
  388. consume_while(is_whitespace);
  389. auto redir = parse_redirection();
  390. if (!redir) {
  391. auto list_expr = parse_list_expression();
  392. if (!list_expr)
  393. return nullptr;
  394. auto cast = create<AST::CastToCommand>(list_expr.release_nonnull()); // Cast List Command
  395. auto next_command = parse_command();
  396. if (!next_command)
  397. return cast;
  398. return create<AST::Join>(move(cast), next_command.release_nonnull()); // Join List Command
  399. }
  400. auto command = parse_command();
  401. if (!command)
  402. return redir;
  403. return create<AST::Join>(redir.release_nonnull(), command.release_nonnull()); // Join Command Command
  404. }
  405. RefPtr<AST::Node> Parser::parse_control_structure()
  406. {
  407. auto rule_start = push_start();
  408. consume_while(is_whitespace);
  409. if (auto for_loop = parse_for_loop())
  410. return for_loop;
  411. if (auto if_expr = parse_if_expr())
  412. return if_expr;
  413. if (auto subshell = parse_subshell())
  414. return subshell;
  415. if (auto match = parse_match_expr())
  416. return match;
  417. return nullptr;
  418. }
  419. RefPtr<AST::Node> Parser::parse_for_loop()
  420. {
  421. auto rule_start = push_start();
  422. if (!expect("for"))
  423. return nullptr;
  424. if (consume_while(is_any_of(" \t\n")).is_empty()) {
  425. restore_to(*rule_start);
  426. return nullptr;
  427. }
  428. auto variable_name = consume_while(is_word_character);
  429. Optional<AST::Position> in_start_position;
  430. if (variable_name.is_empty()) {
  431. variable_name = "it";
  432. } else {
  433. consume_while(is_whitespace);
  434. auto in_error_start = push_start();
  435. if (!expect("in")) {
  436. auto syntax_error = create<AST::SyntaxError>("Expected 'in' after a variable name in a 'for' loop");
  437. return create<AST::ForLoop>(move(variable_name), move(syntax_error), nullptr); // ForLoop Var Iterated Block
  438. }
  439. in_start_position = AST::Position { in_error_start->offset, m_offset, in_error_start->line, line() };
  440. }
  441. consume_while(is_whitespace);
  442. RefPtr<AST::Node> iterated_expression;
  443. {
  444. auto iter_error_start = push_start();
  445. iterated_expression = parse_expression();
  446. if (!iterated_expression) {
  447. auto syntax_error = create<AST::SyntaxError>("Expected an expression in 'for' loop");
  448. return create<AST::ForLoop>(move(variable_name), move(syntax_error), nullptr, move(in_start_position)); // ForLoop Var Iterated Block
  449. }
  450. }
  451. consume_while(is_any_of(" \t\n"));
  452. {
  453. auto obrace_error_start = push_start();
  454. if (!expect('{')) {
  455. auto syntax_error = create<AST::SyntaxError>("Expected an open brace '{' to start a 'for' loop body");
  456. return create<AST::ForLoop>(move(variable_name), iterated_expression.release_nonnull(), move(syntax_error), move(in_start_position)); // ForLoop Var Iterated Block
  457. }
  458. }
  459. auto body = parse_toplevel();
  460. {
  461. auto cbrace_error_start = push_start();
  462. if (!expect('}')) {
  463. auto error_start = push_start();
  464. auto syntax_error = create<AST::SyntaxError>("Expected a close brace '}' to end a 'for' loop body");
  465. if (body)
  466. body->set_is_syntax_error(*syntax_error);
  467. else
  468. body = syntax_error;
  469. }
  470. }
  471. return create<AST::ForLoop>(move(variable_name), iterated_expression.release_nonnull(), move(body), move(in_start_position)); // ForLoop Var Iterated Block
  472. }
  473. RefPtr<AST::Node> Parser::parse_if_expr()
  474. {
  475. auto rule_start = push_start();
  476. if (!expect("if"))
  477. return nullptr;
  478. if (consume_while(is_any_of(" \t\n")).is_empty()) {
  479. restore_to(*rule_start);
  480. return nullptr;
  481. }
  482. RefPtr<AST::Node> condition;
  483. {
  484. auto cond_error_start = push_start();
  485. condition = parse_or_logical_sequence();
  486. if (!condition)
  487. condition = create<AST::SyntaxError>("Expected a logical sequence after 'if'");
  488. }
  489. auto parse_braced_toplevel = [&]() -> RefPtr<AST::Node> {
  490. RefPtr<AST::Node> body;
  491. {
  492. auto obrace_error_start = push_start();
  493. if (!expect('{')) {
  494. body = create<AST::SyntaxError>("Expected an open brace '{' to start an 'if' true branch");
  495. }
  496. }
  497. if (!body)
  498. body = parse_toplevel();
  499. {
  500. auto cbrace_error_start = push_start();
  501. if (!expect('}')) {
  502. auto error_start = push_start();
  503. RefPtr<AST::SyntaxError> syntax_error = create<AST::SyntaxError>("Expected a close brace '}' to end an 'if' true branch");
  504. if (body)
  505. body->set_is_syntax_error(*syntax_error);
  506. else
  507. body = syntax_error;
  508. }
  509. }
  510. return body;
  511. };
  512. consume_while(is_whitespace);
  513. auto true_branch = parse_braced_toplevel();
  514. consume_while(is_whitespace);
  515. Optional<AST::Position> else_position;
  516. {
  517. auto else_start = push_start();
  518. if (expect("else"))
  519. else_position = AST::Position { else_start->offset, m_offset, else_start->line, line() };
  520. }
  521. if (else_position.has_value()) {
  522. consume_while(is_whitespace);
  523. if (peek() == '{') {
  524. auto false_branch = parse_braced_toplevel();
  525. return create<AST::IfCond>(else_position, condition.release_nonnull(), move(true_branch), move(false_branch)); // If expr true_branch Else false_branch
  526. }
  527. auto else_if_branch = parse_if_expr();
  528. return create<AST::IfCond>(else_position, condition.release_nonnull(), move(true_branch), move(else_if_branch)); // If expr true_branch Else If ...
  529. }
  530. return create<AST::IfCond>(else_position, condition.release_nonnull(), move(true_branch), nullptr); // If expr true_branch
  531. }
  532. RefPtr<AST::Node> Parser::parse_subshell()
  533. {
  534. auto rule_start = push_start();
  535. if (!expect('{'))
  536. return nullptr;
  537. auto body = parse_toplevel();
  538. {
  539. auto cbrace_error_start = push_start();
  540. if (!expect('}')) {
  541. auto error_start = push_start();
  542. RefPtr<AST::SyntaxError> syntax_error = create<AST::SyntaxError>("Expected a close brace '}' to end a subshell");
  543. if (body)
  544. body->set_is_syntax_error(*syntax_error);
  545. else
  546. body = syntax_error;
  547. }
  548. }
  549. return create<AST::Subshell>(move(body));
  550. }
  551. RefPtr<AST::Node> Parser::parse_match_expr()
  552. {
  553. auto rule_start = push_start();
  554. if (!expect("match"))
  555. return nullptr;
  556. if (consume_while(is_whitespace).is_empty()) {
  557. restore_to(*rule_start);
  558. return nullptr;
  559. }
  560. auto match_expression = parse_expression();
  561. if (!match_expression) {
  562. return create<AST::MatchExpr>(
  563. create<AST::SyntaxError>("Expected an expression after 'match'"),
  564. String {}, Optional<AST::Position> {}, Vector<AST::MatchEntry> {});
  565. }
  566. consume_while(is_any_of(" \t\n"));
  567. String match_name;
  568. Optional<AST::Position> as_position;
  569. auto as_start = m_offset;
  570. auto as_line = line();
  571. if (expect("as")) {
  572. as_position = AST::Position { as_start, m_offset, as_line, line() };
  573. if (consume_while(is_any_of(" \t\n")).is_empty()) {
  574. auto node = create<AST::MatchExpr>(
  575. match_expression.release_nonnull(),
  576. String {}, move(as_position), Vector<AST::MatchEntry> {});
  577. node->set_is_syntax_error(create<AST::SyntaxError>("Expected whitespace after 'as' in 'match'"));
  578. return node;
  579. }
  580. match_name = consume_while(is_word_character);
  581. if (match_name.is_empty()) {
  582. auto node = create<AST::MatchExpr>(
  583. match_expression.release_nonnull(),
  584. String {}, move(as_position), Vector<AST::MatchEntry> {});
  585. node->set_is_syntax_error(create<AST::SyntaxError>("Expected an identifier after 'as' in 'match'"));
  586. return node;
  587. }
  588. }
  589. consume_while(is_any_of(" \t\n"));
  590. if (!expect('{')) {
  591. auto node = create<AST::MatchExpr>(
  592. match_expression.release_nonnull(),
  593. move(match_name), move(as_position), Vector<AST::MatchEntry> {});
  594. node->set_is_syntax_error(create<AST::SyntaxError>("Expected an open brace '{' to start a 'match' entry list"));
  595. return node;
  596. }
  597. consume_while(is_any_of(" \t\n"));
  598. Vector<AST::MatchEntry> entries;
  599. for (;;) {
  600. auto entry = parse_match_entry();
  601. consume_while(is_any_of(" \t\n"));
  602. if (entry.options.is_empty())
  603. break;
  604. entries.append(entry);
  605. }
  606. consume_while(is_any_of(" \t\n"));
  607. if (!expect('}')) {
  608. auto node = create<AST::MatchExpr>(
  609. match_expression.release_nonnull(),
  610. move(match_name), move(as_position), move(entries));
  611. node->set_is_syntax_error(create<AST::SyntaxError>("Expected a close brace '}' to end a 'match' entry list"));
  612. return node;
  613. }
  614. return create<AST::MatchExpr>(match_expression.release_nonnull(), move(match_name), move(as_position), move(entries));
  615. }
  616. AST::MatchEntry Parser::parse_match_entry()
  617. {
  618. auto rule_start = push_start();
  619. NonnullRefPtrVector<AST::Node> patterns;
  620. Vector<AST::Position> pipe_positions;
  621. auto pattern = parse_match_pattern();
  622. if (!pattern)
  623. return { {}, {}, create<AST::SyntaxError>("Expected a pattern in 'match' body") };
  624. patterns.append(pattern.release_nonnull());
  625. consume_while(is_any_of(" \t\n"));
  626. auto previous_pipe_start_position = m_offset;
  627. auto previous_pipe_start_line = line();
  628. RefPtr<AST::SyntaxError> error;
  629. while (expect('|')) {
  630. pipe_positions.append({ previous_pipe_start_position, m_offset, previous_pipe_start_line, line() });
  631. consume_while(is_any_of(" \t\n"));
  632. auto pattern = parse_match_pattern();
  633. if (!pattern) {
  634. error = create<AST::SyntaxError>("Expected a pattern to follow '|' in 'match' body");
  635. break;
  636. }
  637. consume_while(is_any_of(" \t\n"));
  638. patterns.append(pattern.release_nonnull());
  639. previous_pipe_start_line = line();
  640. previous_pipe_start_position = m_offset;
  641. }
  642. consume_while(is_any_of(" \t\n"));
  643. if (!expect('{')) {
  644. if (!error)
  645. error = create<AST::SyntaxError>("Expected an open brace '{' to start a match entry body");
  646. }
  647. auto body = parse_toplevel();
  648. if (!expect('}')) {
  649. if (!error)
  650. error = create<AST::SyntaxError>("Expected a close brace '}' to end a match entry body");
  651. }
  652. if (body && error)
  653. body->set_is_syntax_error(*error);
  654. else if (error)
  655. body = error;
  656. return { move(patterns), move(pipe_positions), move(body) };
  657. }
  658. RefPtr<AST::Node> Parser::parse_match_pattern()
  659. {
  660. return parse_expression();
  661. }
  662. RefPtr<AST::Node> Parser::parse_redirection()
  663. {
  664. auto rule_start = push_start();
  665. auto pipe_fd = 0;
  666. auto number = consume_while(is_digit);
  667. if (number.is_empty()) {
  668. pipe_fd = -1;
  669. } else {
  670. auto fd = number.to_int();
  671. ASSERT(fd.has_value());
  672. pipe_fd = fd.value();
  673. }
  674. switch (peek()) {
  675. case '>': {
  676. consume();
  677. if (peek() == '>') {
  678. consume();
  679. consume_while(is_whitespace);
  680. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDOUT_FILENO;
  681. auto path = parse_expression();
  682. if (!path) {
  683. if (!at_end()) {
  684. // Eat a character and hope the problem goes away
  685. consume();
  686. }
  687. path = create<AST::SyntaxError>("Expected a path after redirection");
  688. }
  689. return create<AST::WriteAppendRedirection>(pipe_fd, path.release_nonnull()); // Redirection WriteAppend
  690. }
  691. if (peek() == '&') {
  692. consume();
  693. // FIXME: 'fd>&-' Syntax not the best. needs discussion.
  694. if (peek() == '-') {
  695. consume();
  696. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDOUT_FILENO;
  697. return create<AST::CloseFdRedirection>(pipe_fd); // Redirection CloseFd
  698. }
  699. int dest_pipe_fd = 0;
  700. auto number = consume_while(is_digit);
  701. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDOUT_FILENO;
  702. if (number.is_empty()) {
  703. dest_pipe_fd = -1;
  704. } else {
  705. auto fd = number.to_int();
  706. ASSERT(fd.has_value());
  707. dest_pipe_fd = fd.value();
  708. }
  709. auto redir = create<AST::Fd2FdRedirection>(pipe_fd, dest_pipe_fd); // Redirection Fd2Fd
  710. if (dest_pipe_fd == -1)
  711. redir->set_is_syntax_error(*create<AST::SyntaxError>("Expected a file descriptor"));
  712. return redir;
  713. }
  714. consume_while(is_whitespace);
  715. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDOUT_FILENO;
  716. auto path = parse_expression();
  717. if (!path) {
  718. if (!at_end()) {
  719. // Eat a character and hope the problem goes away
  720. consume();
  721. }
  722. path = create<AST::SyntaxError>("Expected a path after redirection");
  723. }
  724. return create<AST::WriteRedirection>(pipe_fd, path.release_nonnull()); // Redirection Write
  725. }
  726. case '<': {
  727. consume();
  728. enum {
  729. Read,
  730. ReadWrite,
  731. } mode { Read };
  732. if (peek() == '>') {
  733. mode = ReadWrite;
  734. consume();
  735. }
  736. consume_while(is_whitespace);
  737. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDIN_FILENO;
  738. auto path = parse_expression();
  739. if (!path) {
  740. if (!at_end()) {
  741. // Eat a character and hope the problem goes away
  742. consume();
  743. }
  744. path = create<AST::SyntaxError>("Expected a path after redirection");
  745. }
  746. if (mode == Read)
  747. return create<AST::ReadRedirection>(pipe_fd, path.release_nonnull()); // Redirection Read
  748. return create<AST::ReadWriteRedirection>(pipe_fd, path.release_nonnull()); // Redirection ReadWrite
  749. }
  750. default:
  751. restore_to(*rule_start);
  752. return nullptr;
  753. }
  754. }
  755. RefPtr<AST::Node> Parser::parse_list_expression()
  756. {
  757. consume_while(is_whitespace);
  758. auto rule_start = push_start();
  759. Vector<NonnullRefPtr<AST::Node>> nodes;
  760. do {
  761. auto expr = parse_expression();
  762. if (!expr)
  763. break;
  764. nodes.append(expr.release_nonnull());
  765. } while (!consume_while(is_whitespace).is_empty());
  766. if (nodes.is_empty())
  767. return nullptr;
  768. return create<AST::ListConcatenate>(move(nodes)); // Concatenate List
  769. }
  770. RefPtr<AST::Node> Parser::parse_expression()
  771. {
  772. auto rule_start = push_start();
  773. auto starting_char = peek();
  774. auto read_concat = [&](auto&& expr) -> NonnullRefPtr<AST::Node> {
  775. if (is_whitespace(peek()))
  776. return move(expr);
  777. if (auto next_expr = parse_expression())
  778. return create<AST::Juxtaposition>(move(expr), next_expr.release_nonnull());
  779. return move(expr);
  780. };
  781. if (strchr("&|){} ;<>\n", starting_char) != nullptr)
  782. return nullptr;
  783. if (isdigit(starting_char)) {
  784. ScopedValueRollback offset_rollback { m_offset };
  785. auto redir = parse_redirection();
  786. if (redir)
  787. return nullptr;
  788. }
  789. if (starting_char == '$') {
  790. if (auto variable = parse_variable())
  791. return read_concat(variable.release_nonnull());
  792. if (auto inline_exec = parse_evaluate())
  793. return read_concat(inline_exec.release_nonnull());
  794. }
  795. if (starting_char == '#')
  796. return parse_comment();
  797. if (starting_char == '(') {
  798. consume();
  799. auto list = parse_list_expression();
  800. if (!expect(')')) {
  801. restore_to(*rule_start);
  802. return nullptr;
  803. }
  804. return read_concat(create<AST::CastToList>(move(list))); // Cast To List
  805. }
  806. if (auto composite = parse_string_composite())
  807. return read_concat(composite.release_nonnull());
  808. return nullptr;
  809. }
  810. RefPtr<AST::Node> Parser::parse_string_composite()
  811. {
  812. auto rule_start = push_start();
  813. if (auto string = parse_string()) {
  814. if (auto next_part = parse_string_composite())
  815. return create<AST::Juxtaposition>(string.release_nonnull(), next_part.release_nonnull()); // Concatenate String StringComposite
  816. return string;
  817. }
  818. if (auto variable = parse_variable()) {
  819. if (auto next_part = parse_string_composite())
  820. return create<AST::Juxtaposition>(variable.release_nonnull(), next_part.release_nonnull()); // Concatenate Variable StringComposite
  821. return variable;
  822. }
  823. if (auto glob = parse_glob()) {
  824. if (auto next_part = parse_string_composite())
  825. return create<AST::Juxtaposition>(glob.release_nonnull(), next_part.release_nonnull()); // Concatenate Glob StringComposite
  826. return glob;
  827. }
  828. if (auto bareword = parse_bareword()) {
  829. if (auto next_part = parse_string_composite())
  830. return create<AST::Juxtaposition>(bareword.release_nonnull(), next_part.release_nonnull()); // Concatenate Bareword StringComposite
  831. return bareword;
  832. }
  833. if (auto inline_command = parse_evaluate()) {
  834. if (auto next_part = parse_string_composite())
  835. return create<AST::Juxtaposition>(inline_command.release_nonnull(), next_part.release_nonnull()); // Concatenate Execute StringComposite
  836. return inline_command;
  837. }
  838. return nullptr;
  839. }
  840. RefPtr<AST::Node> Parser::parse_string()
  841. {
  842. auto rule_start = push_start();
  843. if (at_end())
  844. return nullptr;
  845. if (peek() == '"') {
  846. consume();
  847. auto inner = parse_doublequoted_string_inner();
  848. if (!inner)
  849. inner = create<AST::SyntaxError>("Unexpected EOF in string");
  850. if (!expect('"')) {
  851. inner = create<AST::DoubleQuotedString>(move(inner));
  852. inner->set_is_syntax_error(*create<AST::SyntaxError>("Expected a terminating double quote"));
  853. return inner;
  854. }
  855. return create<AST::DoubleQuotedString>(move(inner)); // Double Quoted String
  856. }
  857. if (peek() == '\'') {
  858. consume();
  859. auto text = consume_while(is_not('\''));
  860. bool is_error = false;
  861. if (!expect('\''))
  862. is_error = true;
  863. auto result = create<AST::StringLiteral>(move(text)); // String Literal
  864. if (is_error)
  865. result->set_is_syntax_error(*create<AST::SyntaxError>("Expected a terminating single quote"));
  866. return move(result);
  867. }
  868. return nullptr;
  869. }
  870. RefPtr<AST::Node> Parser::parse_doublequoted_string_inner()
  871. {
  872. auto rule_start = push_start();
  873. if (at_end())
  874. return nullptr;
  875. StringBuilder builder;
  876. while (!at_end() && peek() != '"') {
  877. if (peek() == '\\') {
  878. consume();
  879. if (at_end()) {
  880. break;
  881. }
  882. auto ch = consume();
  883. switch (ch) {
  884. case '\\':
  885. default:
  886. builder.append(ch);
  887. break;
  888. case 'x': {
  889. if (m_input.length() <= m_offset + 2)
  890. break;
  891. auto first_nibble = tolower(consume());
  892. auto second_nibble = tolower(consume());
  893. if (!isxdigit(first_nibble) || !isxdigit(second_nibble)) {
  894. builder.append(first_nibble);
  895. builder.append(second_nibble);
  896. break;
  897. }
  898. builder.append(to_byte(first_nibble, second_nibble));
  899. break;
  900. }
  901. case 'a':
  902. builder.append('\a');
  903. break;
  904. case 'b':
  905. builder.append('\b');
  906. break;
  907. case 'e':
  908. builder.append('\x1b');
  909. break;
  910. case 'f':
  911. builder.append('\f');
  912. break;
  913. case 'r':
  914. builder.append('\r');
  915. break;
  916. case 'n':
  917. builder.append('\n');
  918. break;
  919. }
  920. continue;
  921. }
  922. if (peek() == '$') {
  923. auto string_literal = create<AST::StringLiteral>(builder.to_string()); // String Literal
  924. if (auto variable = parse_variable()) {
  925. auto inner = create<AST::StringPartCompose>(
  926. move(string_literal),
  927. variable.release_nonnull()); // Compose String Variable
  928. if (auto string = parse_doublequoted_string_inner()) {
  929. return create<AST::StringPartCompose>(move(inner), string.release_nonnull()); // Compose Composition Composition
  930. }
  931. return inner;
  932. }
  933. if (auto evaluate = parse_evaluate()) {
  934. auto composition = create<AST::StringPartCompose>(
  935. move(string_literal),
  936. evaluate.release_nonnull()); // Compose String Sequence
  937. if (auto string = parse_doublequoted_string_inner()) {
  938. return create<AST::StringPartCompose>(move(composition), string.release_nonnull()); // Compose Composition Composition
  939. }
  940. return composition;
  941. }
  942. }
  943. builder.append(consume());
  944. }
  945. return create<AST::StringLiteral>(builder.to_string()); // String Literal
  946. }
  947. RefPtr<AST::Node> Parser::parse_variable()
  948. {
  949. auto rule_start = push_start();
  950. if (at_end())
  951. return nullptr;
  952. if (peek() != '$')
  953. return nullptr;
  954. consume();
  955. switch (peek()) {
  956. case '$':
  957. case '?':
  958. case '*':
  959. case '#':
  960. return create<AST::SpecialVariable>(consume()); // Variable Special
  961. default:
  962. break;
  963. }
  964. auto name = consume_while(is_word_character);
  965. if (name.length() == 0) {
  966. restore_to(rule_start->offset, rule_start->line);
  967. return nullptr;
  968. }
  969. return create<AST::SimpleVariable>(move(name)); // Variable Simple
  970. }
  971. RefPtr<AST::Node> Parser::parse_evaluate()
  972. {
  973. auto rule_start = push_start();
  974. if (at_end())
  975. return nullptr;
  976. if (peek() != '$')
  977. return nullptr;
  978. consume();
  979. if (peek() == '(') {
  980. consume();
  981. auto inner = parse_pipe_sequence();
  982. if (!inner)
  983. inner = create<AST::SyntaxError>("Unexpected EOF in list");
  984. if (!expect(')'))
  985. inner->set_is_syntax_error(*create<AST::SyntaxError>("Expected a terminating close paren"));
  986. return create<AST::Execute>(inner.release_nonnull(), true);
  987. }
  988. auto inner = parse_expression();
  989. if (!inner) {
  990. inner = create<AST::SyntaxError>("Expected a command");
  991. } else {
  992. if (inner->is_list()) {
  993. auto execute_inner = create<AST::Execute>(inner.release_nonnull(), true);
  994. inner = move(execute_inner);
  995. } else {
  996. auto dyn_inner = create<AST::DynamicEvaluate>(inner.release_nonnull());
  997. inner = move(dyn_inner);
  998. }
  999. }
  1000. return inner;
  1001. }
  1002. RefPtr<AST::Node> Parser::parse_comment()
  1003. {
  1004. if (at_end())
  1005. return nullptr;
  1006. if (peek() != '#')
  1007. return nullptr;
  1008. consume();
  1009. auto text = consume_while(is_not('\n'));
  1010. return create<AST::Comment>(move(text)); // Comment
  1011. }
  1012. RefPtr<AST::Node> Parser::parse_bareword()
  1013. {
  1014. auto rule_start = push_start();
  1015. StringBuilder builder;
  1016. auto is_acceptable_bareword_character = [](char c) {
  1017. return strchr("\\\"'*$&#|(){} ?;<>\n", c) == nullptr;
  1018. };
  1019. while (!at_end()) {
  1020. char ch = peek();
  1021. if (ch == '\\') {
  1022. consume();
  1023. if (!at_end()) {
  1024. ch = consume();
  1025. if (is_acceptable_bareword_character(ch))
  1026. builder.append('\\');
  1027. }
  1028. builder.append(ch);
  1029. continue;
  1030. }
  1031. if (is_acceptable_bareword_character(ch)) {
  1032. builder.append(consume());
  1033. continue;
  1034. }
  1035. break;
  1036. }
  1037. if (builder.is_empty())
  1038. return nullptr;
  1039. auto current_end = m_offset;
  1040. auto current_line = line();
  1041. auto string = builder.to_string();
  1042. if (string.starts_with('~')) {
  1043. String username;
  1044. RefPtr<AST::Node> tilde, text;
  1045. auto first_slash_index = string.index_of("/");
  1046. if (first_slash_index.has_value()) {
  1047. username = string.substring_view(1, first_slash_index.value() - 1);
  1048. string = string.substring_view(first_slash_index.value(), string.length() - first_slash_index.value());
  1049. } else {
  1050. username = string.substring_view(1, string.length() - 1);
  1051. string = "";
  1052. }
  1053. // Synthesize a Tilde Node with the correct positioning information.
  1054. {
  1055. restore_to(rule_start->offset, rule_start->line);
  1056. auto ch = consume();
  1057. ASSERT(ch == '~');
  1058. tilde = create<AST::Tilde>(move(username));
  1059. }
  1060. if (string.is_empty())
  1061. return tilde;
  1062. // Synthesize a BarewordLiteral Node with the correct positioning information.
  1063. {
  1064. auto text_start = push_start();
  1065. restore_to(current_end, current_line);
  1066. text = create<AST::BarewordLiteral>(move(string));
  1067. }
  1068. return create<AST::Juxtaposition>(tilde.release_nonnull(), text.release_nonnull()); // Juxtaposition Varible Bareword
  1069. }
  1070. if (string.starts_with("\\~")) {
  1071. // Un-escape the tilde, but only at the start (where it would be an expansion)
  1072. string = string.substring(1, string.length() - 1);
  1073. }
  1074. return create<AST::BarewordLiteral>(move(string)); // Bareword Literal
  1075. }
  1076. RefPtr<AST::Node> Parser::parse_glob()
  1077. {
  1078. auto rule_start = push_start();
  1079. auto bareword_part = parse_bareword();
  1080. if (at_end())
  1081. return bareword_part;
  1082. char ch = peek();
  1083. if (ch == '*' || ch == '?') {
  1084. auto saved_offset = save_offset();
  1085. consume();
  1086. StringBuilder textbuilder;
  1087. if (bareword_part) {
  1088. StringView text;
  1089. if (bareword_part->is_bareword()) {
  1090. auto bareword = static_cast<AST::BarewordLiteral*>(bareword_part.ptr());
  1091. text = bareword->text();
  1092. } else {
  1093. // FIXME: Allow composition of tilde+bareword with globs: '~/foo/bar/baz*'
  1094. restore_to(saved_offset.offset, saved_offset.line);
  1095. bareword_part->set_is_syntax_error(*create<AST::SyntaxError>(String::format("Unexpected %s inside a glob", bareword_part->class_name().characters())));
  1096. return bareword_part;
  1097. }
  1098. textbuilder.append(text);
  1099. }
  1100. textbuilder.append(ch);
  1101. auto glob_after = parse_glob();
  1102. if (glob_after) {
  1103. if (glob_after->is_glob()) {
  1104. auto glob = static_cast<AST::BarewordLiteral*>(glob_after.ptr());
  1105. textbuilder.append(glob->text());
  1106. } else if (glob_after->is_bareword()) {
  1107. auto bareword = static_cast<AST::BarewordLiteral*>(glob_after.ptr());
  1108. textbuilder.append(bareword->text());
  1109. } else {
  1110. ASSERT_NOT_REACHED();
  1111. }
  1112. }
  1113. return create<AST::Glob>(textbuilder.to_string()); // Glob
  1114. }
  1115. return bareword_part;
  1116. }
  1117. StringView Parser::consume_while(Function<bool(char)> condition)
  1118. {
  1119. auto start_offset = m_offset;
  1120. while (!at_end() && condition(peek()))
  1121. consume();
  1122. return m_input.substring_view(start_offset, m_offset - start_offset);
  1123. }