Parser.cpp 34 KB

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