Parser.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  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 <AK/TemporaryChange.h>
  28. #include <ctype.h>
  29. #include <stdio.h>
  30. #include <unistd.h>
  31. namespace Shell {
  32. Parser::SavedOffset Parser::save_offset() const
  33. {
  34. return { m_offset, m_line };
  35. }
  36. char Parser::peek()
  37. {
  38. if (at_end())
  39. return 0;
  40. ASSERT(m_offset < m_input.length());
  41. auto ch = m_input[m_offset];
  42. if (ch == '\\' && m_input.length() > m_offset + 1 && m_input[m_offset + 1] == '\n') {
  43. m_offset += 2;
  44. ++m_line.line_number;
  45. m_line.line_column = 0;
  46. return peek();
  47. }
  48. return ch;
  49. }
  50. char Parser::consume()
  51. {
  52. if (at_end())
  53. return 0;
  54. auto ch = peek();
  55. ++m_offset;
  56. if (ch == '\n') {
  57. ++m_line.line_number;
  58. m_line.line_column = 0;
  59. } else {
  60. ++m_line.line_column;
  61. }
  62. return ch;
  63. }
  64. bool Parser::expect(char ch)
  65. {
  66. return expect(StringView { &ch, 1 });
  67. }
  68. bool Parser::expect(const StringView& expected)
  69. {
  70. auto offset_at_start = m_offset;
  71. auto line_at_start = line();
  72. if (expected.length() + m_offset > m_input.length())
  73. return false;
  74. for (size_t i = 0; i < expected.length(); ++i) {
  75. if (peek() != expected[i]) {
  76. restore_to(offset_at_start, line_at_start);
  77. return false;
  78. }
  79. consume();
  80. }
  81. return true;
  82. }
  83. template<typename A, typename... Args>
  84. NonnullRefPtr<A> Parser::create(Args... args)
  85. {
  86. return adopt(*new A(AST::Position { m_rule_start_offsets.last(), m_offset, m_rule_start_lines.last(), line() }, args...));
  87. }
  88. [[nodiscard]] OwnPtr<Parser::ScopedOffset> Parser::push_start()
  89. {
  90. return make<ScopedOffset>(m_rule_start_offsets, m_rule_start_lines, m_offset, m_line.line_number, m_line.line_column);
  91. }
  92. static constexpr bool is_whitespace(char c)
  93. {
  94. return c == ' ' || c == '\t';
  95. }
  96. static constexpr bool is_word_character(char c)
  97. {
  98. return (c <= '9' && c >= '0') || (c <= 'Z' && c >= 'A') || (c <= 'z' && c >= 'a') || c == '_';
  99. }
  100. static constexpr bool is_digit(char c)
  101. {
  102. return c <= '9' && c >= '0';
  103. }
  104. static constexpr auto is_not(char c)
  105. {
  106. return [c](char ch) { return ch != c; };
  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. while (!at_end())
  122. consume();
  123. auto syntax_error_node = create<AST::SyntaxError>("Unexpected tokens past the end");
  124. if (!toplevel)
  125. toplevel = move(syntax_error_node);
  126. else if (!toplevel->is_syntax_error())
  127. toplevel->set_is_syntax_error(*syntax_error_node);
  128. }
  129. return toplevel;
  130. }
  131. RefPtr<AST::Node> Parser::parse_toplevel()
  132. {
  133. auto rule_start = push_start();
  134. if (auto sequence = parse_sequence())
  135. return create<AST::Execute>(sequence.release_nonnull());
  136. return nullptr;
  137. }
  138. RefPtr<AST::Node> Parser::parse_sequence()
  139. {
  140. consume_while(is_any_of(" \t\n;")); // ignore whitespaces or terminators without effect.
  141. auto rule_start = push_start();
  142. auto var_decls = parse_variable_decls();
  143. auto pos_before_seps = save_offset();
  144. switch (peek()) {
  145. case '}':
  146. return var_decls;
  147. case ';':
  148. case '\n': {
  149. if (!var_decls)
  150. break;
  151. consume_while(is_any_of("\n;"));
  152. auto pos_after_seps = save_offset();
  153. auto rest = parse_sequence();
  154. if (rest)
  155. return create<AST::Sequence>(
  156. var_decls.release_nonnull(),
  157. rest.release_nonnull(),
  158. AST::Position { pos_before_seps.offset, pos_after_seps.offset, pos_before_seps.line, pos_after_seps.line });
  159. return var_decls;
  160. }
  161. default:
  162. break;
  163. }
  164. auto first = parse_function_decl();
  165. if (!first)
  166. first = parse_or_logical_sequence();
  167. if (!first)
  168. return var_decls;
  169. if (var_decls)
  170. first = create<AST::Sequence>(
  171. var_decls.release_nonnull(),
  172. first.release_nonnull(),
  173. AST::Position { pos_before_seps.offset, pos_before_seps.offset, pos_before_seps.line, pos_before_seps.line });
  174. consume_while(is_whitespace);
  175. pos_before_seps = save_offset();
  176. switch (peek()) {
  177. case ';':
  178. case '\n': {
  179. consume_while(is_any_of("\n;"));
  180. auto pos_after_seps = save_offset();
  181. if (auto expr = parse_sequence()) {
  182. return create<AST::Sequence>(
  183. first.release_nonnull(),
  184. expr.release_nonnull(),
  185. AST::Position { pos_before_seps.offset, pos_after_seps.offset, pos_before_seps.line, pos_after_seps.line }); // Sequence
  186. }
  187. return first;
  188. }
  189. case '&': {
  190. consume();
  191. auto pos_after_seps = save_offset();
  192. auto bg = create<AST::Background>(first.release_nonnull()); // 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", true));
  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", true);
  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. TemporaryChange controls { m_continuation_controls_allowed, false };
  298. auto body = parse_toplevel();
  299. {
  300. RefPtr<AST::SyntaxError> syntax_error;
  301. {
  302. auto cbrace_error_start = push_start();
  303. syntax_error = create<AST::SyntaxError>("Expected a close brace '}' to end a function body", true);
  304. }
  305. if (!expect('}')) {
  306. if (body)
  307. body->set_is_syntax_error(*syntax_error);
  308. else
  309. body = move(syntax_error);
  310. return create<AST::FunctionDeclaration>(
  311. AST::FunctionDeclaration::NameWithPosition {
  312. move(function_name),
  313. { pos_before_name.offset, pos_after_name.offset, pos_before_name.line, pos_after_name.line } },
  314. move(arguments),
  315. move(body));
  316. }
  317. }
  318. return create<AST::FunctionDeclaration>(
  319. AST::FunctionDeclaration::NameWithPosition {
  320. move(function_name),
  321. { pos_before_name.offset, pos_after_name.offset, pos_before_name.line, pos_after_name.line } },
  322. move(arguments),
  323. move(body));
  324. }
  325. RefPtr<AST::Node> Parser::parse_or_logical_sequence()
  326. {
  327. consume_while(is_whitespace);
  328. auto rule_start = push_start();
  329. auto and_sequence = parse_and_logical_sequence();
  330. if (!and_sequence)
  331. return nullptr;
  332. consume_while(is_whitespace);
  333. auto pos_before_or = save_offset();
  334. if (!expect("||"))
  335. return and_sequence;
  336. auto pos_after_or = save_offset();
  337. auto right_and_sequence = parse_and_logical_sequence();
  338. if (!right_and_sequence)
  339. right_and_sequence = create<AST::SyntaxError>("Expected an expression after '||'", true);
  340. return create<AST::Or>(
  341. and_sequence.release_nonnull(),
  342. right_and_sequence.release_nonnull(),
  343. AST::Position { pos_before_or.offset, pos_after_or.offset, pos_before_or.line, pos_after_or.line });
  344. }
  345. RefPtr<AST::Node> Parser::parse_and_logical_sequence()
  346. {
  347. consume_while(is_whitespace);
  348. auto rule_start = push_start();
  349. auto pipe_sequence = parse_pipe_sequence();
  350. if (!pipe_sequence)
  351. return nullptr;
  352. consume_while(is_whitespace);
  353. auto pos_before_and = save_offset();
  354. if (!expect("&&"))
  355. return pipe_sequence;
  356. auto pos_after_end = save_offset();
  357. auto right_and_sequence = parse_and_logical_sequence();
  358. if (!right_and_sequence)
  359. right_and_sequence = create<AST::SyntaxError>("Expected an expression after '&&'", true);
  360. return create<AST::And>(
  361. pipe_sequence.release_nonnull(),
  362. right_and_sequence.release_nonnull(),
  363. AST::Position { pos_before_and.offset, pos_after_end.offset, pos_before_and.line, pos_after_end.line });
  364. }
  365. RefPtr<AST::Node> Parser::parse_pipe_sequence()
  366. {
  367. auto rule_start = push_start();
  368. auto left = parse_control_structure();
  369. if (!left) {
  370. if (auto cmd = parse_command())
  371. left = cmd;
  372. else
  373. return nullptr;
  374. }
  375. consume_while(is_whitespace);
  376. if (peek() != '|')
  377. return left;
  378. auto before_pipe = save_offset();
  379. consume();
  380. if (auto pipe_seq = parse_pipe_sequence()) {
  381. return create<AST::Pipe>(left.release_nonnull(), pipe_seq.release_nonnull()); // Pipe
  382. }
  383. restore_to(before_pipe.offset, before_pipe.line);
  384. return left;
  385. }
  386. RefPtr<AST::Node> Parser::parse_command()
  387. {
  388. auto rule_start = push_start();
  389. consume_while(is_whitespace);
  390. auto redir = parse_redirection();
  391. if (!redir) {
  392. auto list_expr = parse_list_expression();
  393. if (!list_expr)
  394. return nullptr;
  395. auto cast = create<AST::CastToCommand>(list_expr.release_nonnull()); // Cast List Command
  396. auto next_command = parse_command();
  397. if (!next_command)
  398. return cast;
  399. return create<AST::Join>(move(cast), next_command.release_nonnull()); // Join List Command
  400. }
  401. auto command = parse_command();
  402. if (!command)
  403. return redir;
  404. return create<AST::Join>(redir.release_nonnull(), command.release_nonnull()); // Join Command Command
  405. }
  406. RefPtr<AST::Node> Parser::parse_control_structure()
  407. {
  408. auto rule_start = push_start();
  409. consume_while(is_whitespace);
  410. if (auto control = parse_continuation_control())
  411. return control;
  412. if (auto for_loop = parse_for_loop())
  413. return for_loop;
  414. if (auto loop = parse_loop_loop())
  415. return loop;
  416. if (auto if_expr = parse_if_expr())
  417. return if_expr;
  418. if (auto subshell = parse_subshell())
  419. return subshell;
  420. if (auto match = parse_match_expr())
  421. return match;
  422. return nullptr;
  423. }
  424. RefPtr<AST::Node> Parser::parse_continuation_control()
  425. {
  426. if (!m_continuation_controls_allowed)
  427. return nullptr;
  428. auto rule_start = push_start();
  429. if (expect("break")) {
  430. {
  431. auto break_end = push_start();
  432. if (consume_while(is_any_of(" \t\n;")).is_empty()) {
  433. restore_to(*rule_start);
  434. return nullptr;
  435. }
  436. restore_to(*break_end);
  437. }
  438. return create<AST::ContinuationControl>(AST::ContinuationControl::Break);
  439. }
  440. if (expect("continue")) {
  441. {
  442. auto continue_end = push_start();
  443. if (consume_while(is_any_of(" \t\n;")).is_empty()) {
  444. restore_to(*rule_start);
  445. return nullptr;
  446. }
  447. restore_to(*continue_end);
  448. }
  449. return create<AST::ContinuationControl>(AST::ContinuationControl::Continue);
  450. }
  451. return nullptr;
  452. }
  453. RefPtr<AST::Node> Parser::parse_for_loop()
  454. {
  455. auto rule_start = push_start();
  456. if (!expect("for"))
  457. return nullptr;
  458. if (consume_while(is_any_of(" \t\n")).is_empty()) {
  459. restore_to(*rule_start);
  460. return nullptr;
  461. }
  462. auto variable_name = consume_while(is_word_character);
  463. Optional<AST::Position> in_start_position;
  464. if (variable_name.is_empty()) {
  465. variable_name = "it";
  466. } else {
  467. consume_while(is_whitespace);
  468. auto in_error_start = push_start();
  469. if (!expect("in")) {
  470. auto syntax_error = create<AST::SyntaxError>("Expected 'in' after a variable name in a 'for' loop", true);
  471. return create<AST::ForLoop>(move(variable_name), move(syntax_error), nullptr); // ForLoop Var Iterated Block
  472. }
  473. in_start_position = AST::Position { in_error_start->offset, m_offset, in_error_start->line, line() };
  474. }
  475. consume_while(is_whitespace);
  476. RefPtr<AST::Node> iterated_expression;
  477. {
  478. auto iter_error_start = push_start();
  479. iterated_expression = parse_expression();
  480. if (!iterated_expression)
  481. iterated_expression = create<AST::SyntaxError>("Expected an expression in 'for' loop", true);
  482. }
  483. consume_while(is_any_of(" \t\n"));
  484. {
  485. auto obrace_error_start = push_start();
  486. if (!expect('{')) {
  487. auto syntax_error = create<AST::SyntaxError>("Expected an open brace '{' to start a 'for' loop body", true);
  488. return create<AST::ForLoop>(move(variable_name), move(iterated_expression), move(syntax_error), move(in_start_position)); // ForLoop Var Iterated Block
  489. }
  490. }
  491. TemporaryChange controls { m_continuation_controls_allowed, true };
  492. auto body = parse_toplevel();
  493. {
  494. auto cbrace_error_start = push_start();
  495. if (!expect('}')) {
  496. auto error_start = push_start();
  497. auto syntax_error = create<AST::SyntaxError>("Expected a close brace '}' to end a 'for' loop body", true);
  498. if (body)
  499. body->set_is_syntax_error(*syntax_error);
  500. else
  501. body = syntax_error;
  502. }
  503. }
  504. return create<AST::ForLoop>(move(variable_name), move(iterated_expression), move(body), move(in_start_position)); // ForLoop Var Iterated Block
  505. }
  506. RefPtr<AST::Node> Parser::parse_loop_loop()
  507. {
  508. auto rule_start = push_start();
  509. if (!expect("loop"))
  510. return nullptr;
  511. if (consume_while(is_any_of(" \t\n")).is_empty()) {
  512. restore_to(*rule_start);
  513. return nullptr;
  514. }
  515. {
  516. auto obrace_error_start = push_start();
  517. if (!expect('{')) {
  518. auto syntax_error = create<AST::SyntaxError>("Expected an open brace '{' to start a 'loop' loop body", true);
  519. return create<AST::ForLoop>(String::empty(), nullptr, move(syntax_error), Optional<AST::Position> {}); // ForLoop null null Block
  520. }
  521. }
  522. TemporaryChange controls { m_continuation_controls_allowed, true };
  523. auto body = parse_toplevel();
  524. {
  525. auto cbrace_error_start = push_start();
  526. if (!expect('}')) {
  527. auto error_start = push_start();
  528. auto syntax_error = create<AST::SyntaxError>("Expected a close brace '}' to end a 'loop' loop body", true);
  529. if (body)
  530. body->set_is_syntax_error(*syntax_error);
  531. else
  532. body = syntax_error;
  533. }
  534. }
  535. return create<AST::ForLoop>(String::empty(), nullptr, move(body), Optional<AST::Position> {}); // ForLoop null null Block
  536. }
  537. RefPtr<AST::Node> Parser::parse_if_expr()
  538. {
  539. auto rule_start = push_start();
  540. if (!expect("if"))
  541. return nullptr;
  542. if (consume_while(is_any_of(" \t\n")).is_empty()) {
  543. restore_to(*rule_start);
  544. return nullptr;
  545. }
  546. RefPtr<AST::Node> condition;
  547. {
  548. auto cond_error_start = push_start();
  549. condition = parse_or_logical_sequence();
  550. if (!condition)
  551. condition = create<AST::SyntaxError>("Expected a logical sequence after 'if'", true);
  552. }
  553. auto parse_braced_toplevel = [&]() -> RefPtr<AST::Node> {
  554. RefPtr<AST::Node> body;
  555. {
  556. auto obrace_error_start = push_start();
  557. if (!expect('{')) {
  558. body = create<AST::SyntaxError>("Expected an open brace '{' to start an 'if' true branch", true);
  559. }
  560. }
  561. if (!body)
  562. body = parse_toplevel();
  563. {
  564. auto cbrace_error_start = push_start();
  565. if (!expect('}')) {
  566. auto error_start = push_start();
  567. RefPtr<AST::SyntaxError> syntax_error = create<AST::SyntaxError>("Expected a close brace '}' to end an 'if' true branch", true);
  568. if (body)
  569. body->set_is_syntax_error(*syntax_error);
  570. else
  571. body = syntax_error;
  572. }
  573. }
  574. return body;
  575. };
  576. consume_while(is_whitespace);
  577. auto true_branch = parse_braced_toplevel();
  578. consume_while(is_whitespace);
  579. Optional<AST::Position> else_position;
  580. {
  581. auto else_start = push_start();
  582. if (expect("else"))
  583. else_position = AST::Position { else_start->offset, m_offset, else_start->line, line() };
  584. }
  585. if (else_position.has_value()) {
  586. consume_while(is_whitespace);
  587. if (peek() == '{') {
  588. auto false_branch = parse_braced_toplevel();
  589. return create<AST::IfCond>(else_position, condition.release_nonnull(), move(true_branch), move(false_branch)); // If expr true_branch Else false_branch
  590. }
  591. auto else_if_branch = parse_if_expr();
  592. return create<AST::IfCond>(else_position, condition.release_nonnull(), move(true_branch), move(else_if_branch)); // If expr true_branch Else If ...
  593. }
  594. return create<AST::IfCond>(else_position, condition.release_nonnull(), move(true_branch), nullptr); // If expr true_branch
  595. }
  596. RefPtr<AST::Node> Parser::parse_subshell()
  597. {
  598. auto rule_start = push_start();
  599. if (!expect('{'))
  600. return nullptr;
  601. auto body = parse_toplevel();
  602. {
  603. auto cbrace_error_start = push_start();
  604. if (!expect('}')) {
  605. auto error_start = push_start();
  606. RefPtr<AST::SyntaxError> syntax_error = create<AST::SyntaxError>("Expected a close brace '}' to end a subshell", true);
  607. if (body)
  608. body->set_is_syntax_error(*syntax_error);
  609. else
  610. body = syntax_error;
  611. }
  612. }
  613. return create<AST::Subshell>(move(body));
  614. }
  615. RefPtr<AST::Node> Parser::parse_match_expr()
  616. {
  617. auto rule_start = push_start();
  618. if (!expect("match"))
  619. return nullptr;
  620. if (consume_while(is_whitespace).is_empty()) {
  621. restore_to(*rule_start);
  622. return nullptr;
  623. }
  624. auto match_expression = parse_expression();
  625. if (!match_expression) {
  626. return create<AST::MatchExpr>(
  627. create<AST::SyntaxError>("Expected an expression after 'match'", true),
  628. String {}, Optional<AST::Position> {}, Vector<AST::MatchEntry> {});
  629. }
  630. consume_while(is_any_of(" \t\n"));
  631. String match_name;
  632. Optional<AST::Position> as_position;
  633. auto as_start = m_offset;
  634. auto as_line = line();
  635. if (expect("as")) {
  636. as_position = AST::Position { as_start, m_offset, as_line, line() };
  637. if (consume_while(is_any_of(" \t\n")).is_empty()) {
  638. auto node = create<AST::MatchExpr>(
  639. match_expression.release_nonnull(),
  640. String {}, move(as_position), Vector<AST::MatchEntry> {});
  641. node->set_is_syntax_error(create<AST::SyntaxError>("Expected whitespace after 'as' in 'match'", true));
  642. return node;
  643. }
  644. match_name = consume_while(is_word_character);
  645. if (match_name.is_empty()) {
  646. auto node = create<AST::MatchExpr>(
  647. match_expression.release_nonnull(),
  648. String {}, move(as_position), Vector<AST::MatchEntry> {});
  649. node->set_is_syntax_error(create<AST::SyntaxError>("Expected an identifier after 'as' in 'match'", true));
  650. return node;
  651. }
  652. }
  653. consume_while(is_any_of(" \t\n"));
  654. if (!expect('{')) {
  655. auto node = create<AST::MatchExpr>(
  656. match_expression.release_nonnull(),
  657. move(match_name), move(as_position), Vector<AST::MatchEntry> {});
  658. node->set_is_syntax_error(create<AST::SyntaxError>("Expected an open brace '{' to start a 'match' entry list", true));
  659. return node;
  660. }
  661. consume_while(is_any_of(" \t\n"));
  662. Vector<AST::MatchEntry> entries;
  663. for (;;) {
  664. auto entry = parse_match_entry();
  665. consume_while(is_any_of(" \t\n"));
  666. if (entry.options.is_empty())
  667. break;
  668. entries.append(entry);
  669. }
  670. consume_while(is_any_of(" \t\n"));
  671. if (!expect('}')) {
  672. auto node = create<AST::MatchExpr>(
  673. match_expression.release_nonnull(),
  674. move(match_name), move(as_position), move(entries));
  675. node->set_is_syntax_error(create<AST::SyntaxError>("Expected a close brace '}' to end a 'match' entry list", true));
  676. return node;
  677. }
  678. return create<AST::MatchExpr>(match_expression.release_nonnull(), move(match_name), move(as_position), move(entries));
  679. }
  680. AST::MatchEntry Parser::parse_match_entry()
  681. {
  682. auto rule_start = push_start();
  683. NonnullRefPtrVector<AST::Node> patterns;
  684. Vector<AST::Position> pipe_positions;
  685. Optional<Vector<String>> match_names;
  686. Optional<AST::Position> match_as_position;
  687. auto pattern = parse_match_pattern();
  688. if (!pattern)
  689. return { {}, {}, {}, {}, create<AST::SyntaxError>("Expected a pattern in 'match' body", true) };
  690. patterns.append(pattern.release_nonnull());
  691. consume_while(is_any_of(" \t\n"));
  692. auto previous_pipe_start_position = m_offset;
  693. auto previous_pipe_start_line = line();
  694. RefPtr<AST::SyntaxError> error;
  695. while (expect('|')) {
  696. pipe_positions.append({ previous_pipe_start_position, m_offset, previous_pipe_start_line, line() });
  697. consume_while(is_any_of(" \t\n"));
  698. auto pattern = parse_match_pattern();
  699. if (!pattern) {
  700. error = create<AST::SyntaxError>("Expected a pattern to follow '|' in 'match' body", true);
  701. break;
  702. }
  703. consume_while(is_any_of(" \t\n"));
  704. patterns.append(pattern.release_nonnull());
  705. previous_pipe_start_line = line();
  706. previous_pipe_start_position = m_offset;
  707. }
  708. consume_while(is_any_of(" \t\n"));
  709. auto as_start_position = m_offset;
  710. auto as_start_line = line();
  711. if (expect("as")) {
  712. match_as_position = AST::Position { as_start_position, m_offset, as_start_line, line() };
  713. consume_while(is_any_of(" \t\n"));
  714. if (!expect('(')) {
  715. if (!error)
  716. error = create<AST::SyntaxError>("Expected an explicit list of identifiers after a pattern 'as'");
  717. } else {
  718. match_names = Vector<String>();
  719. for (;;) {
  720. consume_while(is_whitespace);
  721. auto name = consume_while(is_word_character);
  722. if (name.is_empty())
  723. break;
  724. match_names.value().append(move(name));
  725. }
  726. if (!expect(')')) {
  727. if (!error)
  728. error = create<AST::SyntaxError>("Expected a close paren ')' to end the identifier list of pattern 'as'", true);
  729. }
  730. }
  731. consume_while(is_any_of(" \t\n"));
  732. }
  733. if (!expect('{')) {
  734. if (!error)
  735. error = create<AST::SyntaxError>("Expected an open brace '{' to start a match entry body", true);
  736. }
  737. auto body = parse_toplevel();
  738. if (!expect('}')) {
  739. if (!error)
  740. error = create<AST::SyntaxError>("Expected a close brace '}' to end a match entry body", true);
  741. }
  742. if (body && error)
  743. body->set_is_syntax_error(*error);
  744. else if (error)
  745. body = error;
  746. return { move(patterns), move(match_names), move(match_as_position), move(pipe_positions), move(body) };
  747. }
  748. RefPtr<AST::Node> Parser::parse_match_pattern()
  749. {
  750. return parse_expression();
  751. }
  752. RefPtr<AST::Node> Parser::parse_redirection()
  753. {
  754. auto rule_start = push_start();
  755. auto pipe_fd = 0;
  756. auto number = consume_while(is_digit);
  757. if (number.is_empty()) {
  758. pipe_fd = -1;
  759. } else {
  760. auto fd = number.to_int();
  761. pipe_fd = fd.value_or(-1);
  762. }
  763. switch (peek()) {
  764. case '>': {
  765. consume();
  766. if (peek() == '>') {
  767. consume();
  768. consume_while(is_whitespace);
  769. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDOUT_FILENO;
  770. auto path = parse_expression();
  771. if (!path) {
  772. if (!at_end()) {
  773. // Eat a character and hope the problem goes away
  774. consume();
  775. }
  776. path = create<AST::SyntaxError>("Expected a path after redirection", true);
  777. }
  778. return create<AST::WriteAppendRedirection>(pipe_fd, path.release_nonnull()); // Redirection WriteAppend
  779. }
  780. if (peek() == '&') {
  781. consume();
  782. // FIXME: 'fd>&-' Syntax not the best. needs discussion.
  783. if (peek() == '-') {
  784. consume();
  785. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDOUT_FILENO;
  786. return create<AST::CloseFdRedirection>(pipe_fd); // Redirection CloseFd
  787. }
  788. int dest_pipe_fd = 0;
  789. auto number = consume_while(is_digit);
  790. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDOUT_FILENO;
  791. if (number.is_empty()) {
  792. dest_pipe_fd = -1;
  793. } else {
  794. auto fd = number.to_int();
  795. dest_pipe_fd = fd.value_or(-1);
  796. }
  797. auto redir = create<AST::Fd2FdRedirection>(pipe_fd, dest_pipe_fd); // Redirection Fd2Fd
  798. if (dest_pipe_fd == -1)
  799. redir->set_is_syntax_error(*create<AST::SyntaxError>("Expected a file descriptor"));
  800. return redir;
  801. }
  802. consume_while(is_whitespace);
  803. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDOUT_FILENO;
  804. auto path = parse_expression();
  805. if (!path) {
  806. if (!at_end()) {
  807. // Eat a character and hope the problem goes away
  808. consume();
  809. }
  810. path = create<AST::SyntaxError>("Expected a path after redirection", true);
  811. }
  812. return create<AST::WriteRedirection>(pipe_fd, path.release_nonnull()); // Redirection Write
  813. }
  814. case '<': {
  815. consume();
  816. enum {
  817. Read,
  818. ReadWrite,
  819. } mode { Read };
  820. if (peek() == '>') {
  821. mode = ReadWrite;
  822. consume();
  823. }
  824. consume_while(is_whitespace);
  825. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDIN_FILENO;
  826. auto path = parse_expression();
  827. if (!path) {
  828. if (!at_end()) {
  829. // Eat a character and hope the problem goes away
  830. consume();
  831. }
  832. path = create<AST::SyntaxError>("Expected a path after redirection", true);
  833. }
  834. if (mode == Read)
  835. return create<AST::ReadRedirection>(pipe_fd, path.release_nonnull()); // Redirection Read
  836. return create<AST::ReadWriteRedirection>(pipe_fd, path.release_nonnull()); // Redirection ReadWrite
  837. }
  838. default:
  839. restore_to(*rule_start);
  840. return nullptr;
  841. }
  842. }
  843. RefPtr<AST::Node> Parser::parse_list_expression()
  844. {
  845. consume_while(is_whitespace);
  846. auto rule_start = push_start();
  847. Vector<NonnullRefPtr<AST::Node>> nodes;
  848. do {
  849. auto expr = parse_expression();
  850. if (!expr)
  851. break;
  852. nodes.append(expr.release_nonnull());
  853. } while (!consume_while(is_whitespace).is_empty());
  854. if (nodes.is_empty())
  855. return nullptr;
  856. return create<AST::ListConcatenate>(move(nodes)); // Concatenate List
  857. }
  858. RefPtr<AST::Node> Parser::parse_expression()
  859. {
  860. auto rule_start = push_start();
  861. if (m_rule_start_offsets.size() > max_allowed_nested_rule_depth)
  862. return create<AST::SyntaxError>(String::formatted("Expression nested too deep (max allowed is {})", max_allowed_nested_rule_depth));
  863. auto starting_char = peek();
  864. auto read_concat = [&](auto&& expr) -> NonnullRefPtr<AST::Node> {
  865. if (is_whitespace(peek()))
  866. return move(expr);
  867. if (auto next_expr = parse_expression())
  868. return create<AST::Juxtaposition>(move(expr), next_expr.release_nonnull());
  869. return move(expr);
  870. };
  871. if (strchr("&|)} ;<>\n", starting_char) != nullptr)
  872. return nullptr;
  873. if (m_is_in_brace_expansion_spec && starting_char == ',')
  874. return nullptr;
  875. if (m_is_in_brace_expansion_spec && next_is(".."))
  876. return nullptr;
  877. if (isdigit(starting_char)) {
  878. ScopedValueRollback offset_rollback { m_offset };
  879. auto redir = parse_redirection();
  880. if (redir)
  881. return nullptr;
  882. }
  883. if (starting_char == '$') {
  884. if (auto variable = parse_variable())
  885. return read_concat(variable.release_nonnull());
  886. if (auto inline_exec = parse_evaluate())
  887. return read_concat(inline_exec.release_nonnull());
  888. }
  889. if (starting_char == '#')
  890. return parse_comment();
  891. if (starting_char == '(') {
  892. consume();
  893. auto list = parse_list_expression();
  894. if (!expect(')')) {
  895. restore_to(*rule_start);
  896. return nullptr;
  897. }
  898. return read_concat(create<AST::CastToList>(move(list))); // Cast To List
  899. }
  900. if (auto composite = parse_string_composite())
  901. return read_concat(composite.release_nonnull());
  902. return nullptr;
  903. }
  904. RefPtr<AST::Node> Parser::parse_string_composite()
  905. {
  906. auto rule_start = push_start();
  907. if (auto string = parse_string()) {
  908. if (auto next_part = parse_string_composite())
  909. return create<AST::Juxtaposition>(string.release_nonnull(), next_part.release_nonnull()); // Concatenate String StringComposite
  910. return string;
  911. }
  912. if (auto variable = parse_variable()) {
  913. if (auto next_part = parse_string_composite())
  914. return create<AST::Juxtaposition>(variable.release_nonnull(), next_part.release_nonnull()); // Concatenate Variable StringComposite
  915. return variable;
  916. }
  917. if (auto glob = parse_glob()) {
  918. if (auto next_part = parse_string_composite())
  919. return create<AST::Juxtaposition>(glob.release_nonnull(), next_part.release_nonnull()); // Concatenate Glob StringComposite
  920. return glob;
  921. }
  922. if (auto expansion = parse_brace_expansion()) {
  923. if (auto next_part = parse_string_composite())
  924. return create<AST::Juxtaposition>(expansion.release_nonnull(), next_part.release_nonnull()); // Concatenate BraceExpansion StringComposite
  925. return expansion;
  926. }
  927. if (auto bareword = parse_bareword()) {
  928. if (auto next_part = parse_string_composite())
  929. return create<AST::Juxtaposition>(bareword.release_nonnull(), next_part.release_nonnull()); // Concatenate Bareword StringComposite
  930. return bareword;
  931. }
  932. if (auto inline_command = parse_evaluate()) {
  933. if (auto next_part = parse_string_composite())
  934. return create<AST::Juxtaposition>(inline_command.release_nonnull(), next_part.release_nonnull()); // Concatenate Execute StringComposite
  935. return inline_command;
  936. }
  937. return nullptr;
  938. }
  939. RefPtr<AST::Node> Parser::parse_string()
  940. {
  941. auto rule_start = push_start();
  942. if (at_end())
  943. return nullptr;
  944. if (peek() == '"') {
  945. consume();
  946. auto inner = parse_doublequoted_string_inner();
  947. if (!inner)
  948. inner = create<AST::SyntaxError>("Unexpected EOF in string", true);
  949. if (!expect('"')) {
  950. inner = create<AST::DoubleQuotedString>(move(inner));
  951. inner->set_is_syntax_error(*create<AST::SyntaxError>("Expected a terminating double quote", true));
  952. return inner;
  953. }
  954. return create<AST::DoubleQuotedString>(move(inner)); // Double Quoted String
  955. }
  956. if (peek() == '\'') {
  957. consume();
  958. auto text = consume_while(is_not('\''));
  959. bool is_error = false;
  960. if (!expect('\''))
  961. is_error = true;
  962. auto result = create<AST::StringLiteral>(move(text)); // String Literal
  963. if (is_error)
  964. result->set_is_syntax_error(*create<AST::SyntaxError>("Expected a terminating single quote", true));
  965. return move(result);
  966. }
  967. return nullptr;
  968. }
  969. RefPtr<AST::Node> Parser::parse_doublequoted_string_inner()
  970. {
  971. auto rule_start = push_start();
  972. if (at_end())
  973. return nullptr;
  974. StringBuilder builder;
  975. while (!at_end() && peek() != '"') {
  976. if (peek() == '\\') {
  977. consume();
  978. if (at_end()) {
  979. break;
  980. }
  981. auto ch = consume();
  982. switch (ch) {
  983. case '\\':
  984. default:
  985. builder.append(ch);
  986. break;
  987. case 'x': {
  988. if (m_input.length() <= m_offset + 2)
  989. break;
  990. auto first_nibble = tolower(consume());
  991. auto second_nibble = tolower(consume());
  992. if (!isxdigit(first_nibble) || !isxdigit(second_nibble)) {
  993. builder.append(first_nibble);
  994. builder.append(second_nibble);
  995. break;
  996. }
  997. builder.append(to_byte(first_nibble, second_nibble));
  998. break;
  999. }
  1000. case 'a':
  1001. builder.append('\a');
  1002. break;
  1003. case 'b':
  1004. builder.append('\b');
  1005. break;
  1006. case 'e':
  1007. builder.append('\x1b');
  1008. break;
  1009. case 'f':
  1010. builder.append('\f');
  1011. break;
  1012. case 'r':
  1013. builder.append('\r');
  1014. break;
  1015. case 'n':
  1016. builder.append('\n');
  1017. break;
  1018. }
  1019. continue;
  1020. }
  1021. if (peek() == '$') {
  1022. auto string_literal = create<AST::StringLiteral>(builder.to_string()); // String Literal
  1023. if (auto variable = parse_variable()) {
  1024. auto inner = create<AST::StringPartCompose>(
  1025. move(string_literal),
  1026. variable.release_nonnull()); // Compose String Variable
  1027. if (auto string = parse_doublequoted_string_inner()) {
  1028. return create<AST::StringPartCompose>(move(inner), string.release_nonnull()); // Compose Composition Composition
  1029. }
  1030. return inner;
  1031. }
  1032. if (auto evaluate = parse_evaluate()) {
  1033. auto composition = create<AST::StringPartCompose>(
  1034. move(string_literal),
  1035. evaluate.release_nonnull()); // Compose String Sequence
  1036. if (auto string = parse_doublequoted_string_inner()) {
  1037. return create<AST::StringPartCompose>(move(composition), string.release_nonnull()); // Compose Composition Composition
  1038. }
  1039. return composition;
  1040. }
  1041. }
  1042. builder.append(consume());
  1043. }
  1044. return create<AST::StringLiteral>(builder.to_string()); // String Literal
  1045. }
  1046. RefPtr<AST::Node> Parser::parse_variable()
  1047. {
  1048. auto rule_start = push_start();
  1049. if (at_end())
  1050. return nullptr;
  1051. if (peek() != '$')
  1052. return nullptr;
  1053. consume();
  1054. switch (peek()) {
  1055. case '$':
  1056. case '?':
  1057. case '*':
  1058. case '#':
  1059. return create<AST::SpecialVariable>(consume()); // Variable Special
  1060. default:
  1061. break;
  1062. }
  1063. auto name = consume_while(is_word_character);
  1064. if (name.length() == 0) {
  1065. restore_to(rule_start->offset, rule_start->line);
  1066. return nullptr;
  1067. }
  1068. return create<AST::SimpleVariable>(move(name)); // Variable Simple
  1069. }
  1070. RefPtr<AST::Node> Parser::parse_evaluate()
  1071. {
  1072. auto rule_start = push_start();
  1073. if (at_end())
  1074. return nullptr;
  1075. if (peek() != '$')
  1076. return nullptr;
  1077. consume();
  1078. if (peek() == '(') {
  1079. consume();
  1080. auto inner = parse_pipe_sequence();
  1081. if (!inner)
  1082. inner = create<AST::SyntaxError>("Unexpected EOF in list", true);
  1083. if (!expect(')'))
  1084. inner->set_is_syntax_error(*create<AST::SyntaxError>("Expected a terminating close paren", true));
  1085. return create<AST::Execute>(inner.release_nonnull(), true);
  1086. }
  1087. auto inner = parse_expression();
  1088. if (!inner) {
  1089. inner = create<AST::SyntaxError>("Expected a command", true);
  1090. } else {
  1091. if (inner->is_list()) {
  1092. auto execute_inner = create<AST::Execute>(inner.release_nonnull(), true);
  1093. inner = move(execute_inner);
  1094. } else {
  1095. auto dyn_inner = create<AST::DynamicEvaluate>(inner.release_nonnull());
  1096. inner = move(dyn_inner);
  1097. }
  1098. }
  1099. return inner;
  1100. }
  1101. RefPtr<AST::Node> Parser::parse_comment()
  1102. {
  1103. if (at_end())
  1104. return nullptr;
  1105. if (peek() != '#')
  1106. return nullptr;
  1107. consume();
  1108. auto text = consume_while(is_not('\n'));
  1109. return create<AST::Comment>(move(text)); // Comment
  1110. }
  1111. RefPtr<AST::Node> Parser::parse_bareword()
  1112. {
  1113. auto rule_start = push_start();
  1114. StringBuilder builder;
  1115. auto is_acceptable_bareword_character = [&](char c) {
  1116. return strchr("\\\"'*$&#|(){} ?;<>\n", c) == nullptr
  1117. && ((m_is_in_brace_expansion_spec && c != ',') || !m_is_in_brace_expansion_spec);
  1118. };
  1119. while (!at_end()) {
  1120. char ch = peek();
  1121. if (ch == '\\') {
  1122. consume();
  1123. if (!at_end()) {
  1124. ch = consume();
  1125. if (is_acceptable_bareword_character(ch))
  1126. builder.append('\\');
  1127. }
  1128. builder.append(ch);
  1129. continue;
  1130. }
  1131. if (m_is_in_brace_expansion_spec && next_is("..")) {
  1132. // Don't eat '..' in a brace expansion spec.
  1133. break;
  1134. }
  1135. if (is_acceptable_bareword_character(ch)) {
  1136. builder.append(consume());
  1137. continue;
  1138. }
  1139. break;
  1140. }
  1141. if (builder.is_empty())
  1142. return nullptr;
  1143. auto current_end = m_offset;
  1144. auto current_line = line();
  1145. auto string = builder.to_string();
  1146. if (string.starts_with('~')) {
  1147. String username;
  1148. RefPtr<AST::Node> tilde, text;
  1149. auto first_slash_index = string.index_of("/");
  1150. if (first_slash_index.has_value()) {
  1151. username = string.substring_view(1, first_slash_index.value() - 1);
  1152. string = string.substring_view(first_slash_index.value(), string.length() - first_slash_index.value());
  1153. } else {
  1154. username = string.substring_view(1, string.length() - 1);
  1155. string = "";
  1156. }
  1157. // Synthesize a Tilde Node with the correct positioning information.
  1158. {
  1159. restore_to(rule_start->offset, rule_start->line);
  1160. auto ch = consume();
  1161. ASSERT(ch == '~');
  1162. tilde = create<AST::Tilde>(move(username));
  1163. }
  1164. if (string.is_empty())
  1165. return tilde;
  1166. // Synthesize a BarewordLiteral Node with the correct positioning information.
  1167. {
  1168. auto text_start = push_start();
  1169. restore_to(current_end, current_line);
  1170. text = create<AST::BarewordLiteral>(move(string));
  1171. }
  1172. return create<AST::Juxtaposition>(tilde.release_nonnull(), text.release_nonnull()); // Juxtaposition Variable Bareword
  1173. }
  1174. if (string.starts_with("\\~")) {
  1175. // Un-escape the tilde, but only at the start (where it would be an expansion)
  1176. string = string.substring(1, string.length() - 1);
  1177. }
  1178. return create<AST::BarewordLiteral>(move(string)); // Bareword Literal
  1179. }
  1180. RefPtr<AST::Node> Parser::parse_glob()
  1181. {
  1182. auto rule_start = push_start();
  1183. auto bareword_part = parse_bareword();
  1184. if (at_end())
  1185. return bareword_part;
  1186. char ch = peek();
  1187. if (ch == '*' || ch == '?') {
  1188. auto saved_offset = save_offset();
  1189. consume();
  1190. StringBuilder textbuilder;
  1191. if (bareword_part) {
  1192. StringView text;
  1193. if (bareword_part->is_bareword()) {
  1194. auto bareword = static_cast<AST::BarewordLiteral*>(bareword_part.ptr());
  1195. text = bareword->text();
  1196. } else {
  1197. // FIXME: Allow composition of tilde+bareword with globs: '~/foo/bar/baz*'
  1198. restore_to(saved_offset.offset, saved_offset.line);
  1199. bareword_part->set_is_syntax_error(*create<AST::SyntaxError>(String::format("Unexpected %s inside a glob", bareword_part->class_name().characters())));
  1200. return bareword_part;
  1201. }
  1202. textbuilder.append(text);
  1203. }
  1204. textbuilder.append(ch);
  1205. auto glob_after = parse_glob();
  1206. if (glob_after) {
  1207. if (glob_after->is_glob()) {
  1208. auto glob = static_cast<AST::Glob*>(glob_after.ptr());
  1209. textbuilder.append(glob->text());
  1210. } else if (glob_after->is_bareword()) {
  1211. auto bareword = static_cast<AST::BarewordLiteral*>(glob_after.ptr());
  1212. textbuilder.append(bareword->text());
  1213. } else if (glob_after->is_tilde()) {
  1214. auto bareword = static_cast<AST::Tilde*>(glob_after.ptr());
  1215. textbuilder.append("~");
  1216. textbuilder.append(bareword->text());
  1217. } else {
  1218. return create<AST::SyntaxError>(String::formatted("Invalid node '{}' in glob position, escape shell special characters", glob_after->class_name()));
  1219. }
  1220. }
  1221. return create<AST::Glob>(textbuilder.to_string()); // Glob
  1222. }
  1223. return bareword_part;
  1224. }
  1225. RefPtr<AST::Node> Parser::parse_brace_expansion()
  1226. {
  1227. auto rule_start = push_start();
  1228. if (!expect('{'))
  1229. return nullptr;
  1230. if (auto spec = parse_brace_expansion_spec()) {
  1231. if (!expect('}'))
  1232. spec->set_is_syntax_error(create<AST::SyntaxError>("Expected a close brace '}' to end a brace expansion", true));
  1233. return spec;
  1234. }
  1235. restore_to(*rule_start);
  1236. return nullptr;
  1237. }
  1238. RefPtr<AST::Node> Parser::parse_brace_expansion_spec()
  1239. {
  1240. TemporaryChange is_in_brace_expansion { m_is_in_brace_expansion_spec, true };
  1241. auto rule_start = push_start();
  1242. auto start_expr = parse_expression();
  1243. if (start_expr) {
  1244. if (expect("..")) {
  1245. if (auto end_expr = parse_expression()) {
  1246. if (end_expr->position().start_offset != start_expr->position().end_offset + 2)
  1247. end_expr->set_is_syntax_error(create<AST::SyntaxError>("Expected no whitespace between '..' and the following expression in brace expansion"));
  1248. return create<AST::Range>(start_expr.release_nonnull(), end_expr.release_nonnull());
  1249. }
  1250. return create<AST::Range>(start_expr.release_nonnull(), create<AST::SyntaxError>("Expected an expression to end range brace expansion with", true));
  1251. }
  1252. }
  1253. NonnullRefPtrVector<AST::Node> subexpressions;
  1254. if (start_expr)
  1255. subexpressions.append(start_expr.release_nonnull());
  1256. while (expect(',')) {
  1257. auto expr = parse_expression();
  1258. if (expr) {
  1259. subexpressions.append(expr.release_nonnull());
  1260. } else {
  1261. subexpressions.append(create<AST::StringLiteral>(""));
  1262. }
  1263. }
  1264. if (subexpressions.is_empty())
  1265. return nullptr;
  1266. return create<AST::BraceExpansion>(move(subexpressions));
  1267. }
  1268. StringView Parser::consume_while(Function<bool(char)> condition)
  1269. {
  1270. if (at_end())
  1271. return {};
  1272. auto start_offset = m_offset;
  1273. while (!at_end() && condition(peek()))
  1274. consume();
  1275. return m_input.substring_view(start_offset, m_offset - start_offset);
  1276. }
  1277. bool Parser::next_is(const StringView& next)
  1278. {
  1279. auto start = push_start();
  1280. auto res = expect(next);
  1281. restore_to(*start);
  1282. return res;
  1283. }
  1284. }