Parser.cpp 41 KB

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