Parser.cpp 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  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 "Shell.h"
  28. #include <AK/AllOf.h>
  29. #include <AK/ScopedValueRollback.h>
  30. #include <AK/TemporaryChange.h>
  31. #include <ctype.h>
  32. #include <stdio.h>
  33. #include <unistd.h>
  34. namespace Shell {
  35. Parser::SavedOffset Parser::save_offset() const
  36. {
  37. return { m_offset, m_line };
  38. }
  39. char Parser::peek()
  40. {
  41. if (at_end())
  42. return 0;
  43. VERIFY(m_offset < m_input.length());
  44. auto ch = m_input[m_offset];
  45. if (ch == '\\' && m_input.length() > m_offset + 1 && m_input[m_offset + 1] == '\n') {
  46. m_offset += 2;
  47. ++m_line.line_number;
  48. m_line.line_column = 0;
  49. return peek();
  50. }
  51. return ch;
  52. }
  53. char Parser::consume()
  54. {
  55. if (at_end())
  56. return 0;
  57. auto ch = peek();
  58. ++m_offset;
  59. if (ch == '\n') {
  60. ++m_line.line_number;
  61. m_line.line_column = 0;
  62. } else {
  63. ++m_line.line_column;
  64. }
  65. return ch;
  66. }
  67. bool Parser::expect(char ch)
  68. {
  69. return expect(StringView { &ch, 1 });
  70. }
  71. bool Parser::expect(const StringView& expected)
  72. {
  73. auto offset_at_start = m_offset;
  74. auto line_at_start = line();
  75. if (expected.length() + m_offset > m_input.length())
  76. return false;
  77. for (auto& c : expected) {
  78. if (peek() != c) {
  79. restore_to(offset_at_start, line_at_start);
  80. return false;
  81. }
  82. consume();
  83. }
  84. return true;
  85. }
  86. template<typename A, typename... Args>
  87. NonnullRefPtr<A> Parser::create(Args... args)
  88. {
  89. return adopt(*new A(AST::Position { m_rule_start_offsets.last(), m_offset, m_rule_start_lines.last(), line() }, args...));
  90. }
  91. [[nodiscard]] OwnPtr<Parser::ScopedOffset> Parser::push_start()
  92. {
  93. return make<ScopedOffset>(m_rule_start_offsets, m_rule_start_lines, m_offset, m_line.line_number, m_line.line_column);
  94. }
  95. Parser::Offset Parser::current_position()
  96. {
  97. return Offset { m_offset, { m_line.line_number, m_line.line_column } };
  98. }
  99. static constexpr bool is_whitespace(char c)
  100. {
  101. return c == ' ' || c == '\t';
  102. }
  103. static constexpr bool is_digit(char c)
  104. {
  105. return c <= '9' && c >= '0';
  106. }
  107. static constexpr auto is_not(char c)
  108. {
  109. return [c](char ch) { return ch != c; };
  110. }
  111. static inline char to_byte(char a, char b)
  112. {
  113. char buf[3] { a, b, 0 };
  114. return strtol(buf, nullptr, 16);
  115. }
  116. RefPtr<AST::Node> Parser::parse()
  117. {
  118. m_offset = 0;
  119. m_line = { 0, 0 };
  120. auto toplevel = parse_toplevel();
  121. if (m_offset < m_input.length()) {
  122. // Parsing stopped midway, this is a syntax error.
  123. auto error_start = push_start();
  124. while (!at_end())
  125. consume();
  126. auto syntax_error_node = create<AST::SyntaxError>("Unexpected tokens past the end");
  127. if (!toplevel)
  128. toplevel = move(syntax_error_node);
  129. else if (!toplevel->is_syntax_error())
  130. toplevel->set_is_syntax_error(*syntax_error_node);
  131. }
  132. return toplevel;
  133. }
  134. RefPtr<AST::Node> Parser::parse_as_single_expression()
  135. {
  136. auto input = Shell::escape_token_for_double_quotes(m_input);
  137. Parser parser { input };
  138. return parser.parse_expression();
  139. }
  140. NonnullRefPtrVector<AST::Node> Parser::parse_as_multiple_expressions()
  141. {
  142. NonnullRefPtrVector<AST::Node> nodes;
  143. for (;;) {
  144. consume_while(is_whitespace);
  145. auto node = parse_expression();
  146. if (!node)
  147. node = parse_redirection();
  148. if (!node)
  149. return nodes;
  150. nodes.append(node.release_nonnull());
  151. }
  152. return nodes;
  153. }
  154. RefPtr<AST::Node> Parser::parse_toplevel()
  155. {
  156. auto rule_start = push_start();
  157. SequenceParseResult result;
  158. NonnullRefPtrVector<AST::Node> sequence;
  159. Vector<AST::Position> positions;
  160. do {
  161. result = parse_sequence();
  162. if (result.entries.is_empty())
  163. break;
  164. sequence.append(move(result.entries));
  165. positions.append(move(result.separator_positions));
  166. } while (result.decision == ShouldReadMoreSequences::Yes);
  167. if (sequence.is_empty())
  168. return nullptr;
  169. return create<AST::Execute>(
  170. create<AST::Sequence>(move(sequence), move(positions)));
  171. }
  172. Parser::SequenceParseResult Parser::parse_sequence()
  173. {
  174. consume_while(is_any_of(" \t\n;")); // ignore whitespaces or terminators without effect.
  175. NonnullRefPtrVector<AST::Node> left;
  176. auto rule_start = push_start();
  177. {
  178. auto var_decls = parse_variable_decls();
  179. if (var_decls)
  180. left.append(var_decls.release_nonnull());
  181. }
  182. auto pos_before_seps = save_offset();
  183. switch (peek()) {
  184. case '}':
  185. return { move(left), {}, ShouldReadMoreSequences::No };
  186. case ';':
  187. case '\n': {
  188. if (left.is_empty())
  189. break;
  190. consume_while(is_any_of("\n;"));
  191. auto pos_after_seps = save_offset();
  192. AST::Position separator_position { pos_before_seps.offset, pos_after_seps.offset, pos_before_seps.line, pos_after_seps.line };
  193. return { move(left), { move(separator_position) }, ShouldReadMoreSequences::Yes };
  194. }
  195. default:
  196. break;
  197. }
  198. auto first_entry = parse_function_decl();
  199. Vector<AST::Position> separator_positions;
  200. if (!first_entry)
  201. first_entry = parse_or_logical_sequence();
  202. if (!first_entry)
  203. return { move(left), {}, ShouldReadMoreSequences::No };
  204. left.append(first_entry.release_nonnull());
  205. separator_positions.empend(pos_before_seps.offset, pos_before_seps.offset, pos_before_seps.line, pos_before_seps.line);
  206. consume_while(is_whitespace);
  207. pos_before_seps = save_offset();
  208. switch (peek()) {
  209. case ';':
  210. case '\n': {
  211. consume_while(is_any_of("\n;"));
  212. auto pos_after_seps = save_offset();
  213. separator_positions.empend(pos_before_seps.offset, pos_after_seps.offset, pos_before_seps.line, pos_after_seps.line);
  214. return { move(left), move(separator_positions), ShouldReadMoreSequences::Yes };
  215. }
  216. case '&': {
  217. consume();
  218. auto pos_after_seps = save_offset();
  219. auto bg = create<AST::Background>(left.take_last()); // Execute Background
  220. left.append(move(bg));
  221. separator_positions.empend(pos_before_seps.offset, pos_after_seps.offset, pos_before_seps.line, pos_after_seps.line);
  222. return { move(left), move(separator_positions), ShouldReadMoreSequences::Yes };
  223. }
  224. default:
  225. return { move(left), move(separator_positions), ShouldReadMoreSequences::No };
  226. }
  227. }
  228. RefPtr<AST::Node> Parser::parse_variable_decls()
  229. {
  230. auto rule_start = push_start();
  231. consume_while(is_whitespace);
  232. auto pos_before_name = save_offset();
  233. auto var_name = consume_while(is_word_character);
  234. if (var_name.is_empty())
  235. return nullptr;
  236. if (!expect('=')) {
  237. restore_to(pos_before_name.offset, pos_before_name.line);
  238. return nullptr;
  239. }
  240. auto name_expr = create<AST::BarewordLiteral>(move(var_name));
  241. auto start = push_start();
  242. auto expression = parse_expression();
  243. if (!expression || expression->is_syntax_error()) {
  244. restore_to(*start);
  245. if (peek() == '(') {
  246. consume();
  247. auto command = parse_pipe_sequence();
  248. if (!command)
  249. restore_to(*start);
  250. else if (!expect(')'))
  251. command->set_is_syntax_error(*create<AST::SyntaxError>("Expected a terminating close paren", true));
  252. expression = command;
  253. }
  254. }
  255. if (!expression) {
  256. if (is_whitespace(peek())) {
  257. auto string_start = push_start();
  258. expression = create<AST::StringLiteral>("");
  259. } else {
  260. restore_to(pos_before_name.offset, pos_before_name.line);
  261. return nullptr;
  262. }
  263. }
  264. Vector<AST::VariableDeclarations::Variable> variables;
  265. variables.append({ move(name_expr), expression.release_nonnull() });
  266. if (consume_while(is_whitespace).is_empty())
  267. return create<AST::VariableDeclarations>(move(variables));
  268. auto rest = parse_variable_decls();
  269. if (!rest)
  270. return create<AST::VariableDeclarations>(move(variables));
  271. VERIFY(rest->is_variable_decls());
  272. auto* rest_decl = static_cast<AST::VariableDeclarations*>(rest.ptr());
  273. variables.append(rest_decl->variables());
  274. return create<AST::VariableDeclarations>(move(variables));
  275. }
  276. RefPtr<AST::Node> Parser::parse_function_decl()
  277. {
  278. auto rule_start = push_start();
  279. auto restore = [&] {
  280. restore_to(*rule_start);
  281. return nullptr;
  282. };
  283. consume_while(is_whitespace);
  284. auto pos_before_name = save_offset();
  285. auto function_name = consume_while(is_word_character);
  286. auto pos_after_name = save_offset();
  287. if (function_name.is_empty())
  288. return restore();
  289. if (!expect('('))
  290. return restore();
  291. Vector<AST::NameWithPosition> arguments;
  292. for (;;) {
  293. consume_while(is_whitespace);
  294. if (expect(')'))
  295. break;
  296. auto name_offset = m_offset;
  297. auto start_line = line();
  298. auto arg_name = consume_while(is_word_character);
  299. if (arg_name.is_empty()) {
  300. // FIXME: Should this be a syntax error, or just return?
  301. return restore();
  302. }
  303. arguments.append({ arg_name, { name_offset, m_offset, start_line, line() } });
  304. }
  305. consume_while(is_whitespace);
  306. {
  307. RefPtr<AST::Node> syntax_error;
  308. {
  309. auto obrace_error_start = push_start();
  310. syntax_error = create<AST::SyntaxError>("Expected an open brace '{' to start a function body", true);
  311. }
  312. if (!expect('{')) {
  313. return create<AST::FunctionDeclaration>(
  314. AST::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(syntax_error));
  319. }
  320. }
  321. TemporaryChange controls { m_continuation_controls_allowed, false };
  322. auto body = parse_toplevel();
  323. {
  324. RefPtr<AST::SyntaxError> syntax_error;
  325. {
  326. auto cbrace_error_start = push_start();
  327. syntax_error = create<AST::SyntaxError>("Expected a close brace '}' to end a function body", true);
  328. }
  329. if (!expect('}')) {
  330. if (body)
  331. body->set_is_syntax_error(*syntax_error);
  332. else
  333. body = move(syntax_error);
  334. return create<AST::FunctionDeclaration>(
  335. AST::NameWithPosition {
  336. move(function_name),
  337. { pos_before_name.offset, pos_after_name.offset, pos_before_name.line, pos_after_name.line } },
  338. move(arguments),
  339. move(body));
  340. }
  341. }
  342. return create<AST::FunctionDeclaration>(
  343. AST::NameWithPosition {
  344. move(function_name),
  345. { pos_before_name.offset, pos_after_name.offset, pos_before_name.line, pos_after_name.line } },
  346. move(arguments),
  347. move(body));
  348. }
  349. RefPtr<AST::Node> Parser::parse_or_logical_sequence()
  350. {
  351. consume_while(is_whitespace);
  352. auto rule_start = push_start();
  353. auto and_sequence = parse_and_logical_sequence();
  354. if (!and_sequence)
  355. return nullptr;
  356. consume_while(is_whitespace);
  357. auto pos_before_or = save_offset();
  358. if (!expect("||"))
  359. return and_sequence;
  360. auto pos_after_or = save_offset();
  361. auto right_and_sequence = parse_and_logical_sequence();
  362. if (!right_and_sequence)
  363. right_and_sequence = create<AST::SyntaxError>("Expected an expression after '||'", true);
  364. return create<AST::Or>(
  365. and_sequence.release_nonnull(),
  366. right_and_sequence.release_nonnull(),
  367. AST::Position { pos_before_or.offset, pos_after_or.offset, pos_before_or.line, pos_after_or.line });
  368. }
  369. RefPtr<AST::Node> Parser::parse_and_logical_sequence()
  370. {
  371. consume_while(is_whitespace);
  372. auto rule_start = push_start();
  373. auto pipe_sequence = parse_pipe_sequence();
  374. if (!pipe_sequence)
  375. return nullptr;
  376. consume_while(is_whitespace);
  377. auto pos_before_and = save_offset();
  378. if (!expect("&&"))
  379. return pipe_sequence;
  380. auto pos_after_end = save_offset();
  381. auto right_and_sequence = parse_and_logical_sequence();
  382. if (!right_and_sequence)
  383. right_and_sequence = create<AST::SyntaxError>("Expected an expression after '&&'", true);
  384. return create<AST::And>(
  385. pipe_sequence.release_nonnull(),
  386. right_and_sequence.release_nonnull(),
  387. AST::Position { pos_before_and.offset, pos_after_end.offset, pos_before_and.line, pos_after_end.line });
  388. }
  389. RefPtr<AST::Node> Parser::parse_pipe_sequence()
  390. {
  391. auto rule_start = push_start();
  392. auto left = parse_control_structure();
  393. if (!left) {
  394. if (auto cmd = parse_command())
  395. left = cmd;
  396. else
  397. return nullptr;
  398. }
  399. consume_while(is_whitespace);
  400. if (peek() != '|')
  401. return left;
  402. auto before_pipe = save_offset();
  403. consume();
  404. if (auto pipe_seq = parse_pipe_sequence()) {
  405. return create<AST::Pipe>(left.release_nonnull(), pipe_seq.release_nonnull()); // Pipe
  406. }
  407. restore_to(before_pipe.offset, before_pipe.line);
  408. return left;
  409. }
  410. RefPtr<AST::Node> Parser::parse_command()
  411. {
  412. auto rule_start = push_start();
  413. consume_while(is_whitespace);
  414. auto redir = parse_redirection();
  415. if (!redir) {
  416. auto list_expr = parse_list_expression();
  417. if (!list_expr)
  418. return nullptr;
  419. auto cast = create<AST::CastToCommand>(list_expr.release_nonnull()); // Cast List Command
  420. auto next_command = parse_command();
  421. if (!next_command)
  422. return cast;
  423. return create<AST::Join>(move(cast), next_command.release_nonnull()); // Join List Command
  424. }
  425. auto command = parse_command();
  426. if (!command)
  427. return redir;
  428. return create<AST::Join>(redir.release_nonnull(), command.release_nonnull()); // Join Command Command
  429. }
  430. RefPtr<AST::Node> Parser::parse_control_structure()
  431. {
  432. auto rule_start = push_start();
  433. consume_while(is_whitespace);
  434. if (auto control = parse_continuation_control())
  435. return control;
  436. if (auto for_loop = parse_for_loop())
  437. return for_loop;
  438. if (auto loop = parse_loop_loop())
  439. return loop;
  440. if (auto if_expr = parse_if_expr())
  441. return if_expr;
  442. if (auto subshell = parse_subshell())
  443. return subshell;
  444. if (auto match = parse_match_expr())
  445. return match;
  446. return nullptr;
  447. }
  448. RefPtr<AST::Node> Parser::parse_continuation_control()
  449. {
  450. if (!m_continuation_controls_allowed)
  451. return nullptr;
  452. auto rule_start = push_start();
  453. if (expect("break")) {
  454. {
  455. auto break_end = push_start();
  456. if (consume_while(is_any_of(" \t\n;")).is_empty()) {
  457. restore_to(*rule_start);
  458. return nullptr;
  459. }
  460. restore_to(*break_end);
  461. }
  462. return create<AST::ContinuationControl>(AST::ContinuationControl::Break);
  463. }
  464. if (expect("continue")) {
  465. {
  466. auto continue_end = push_start();
  467. if (consume_while(is_any_of(" \t\n;")).is_empty()) {
  468. restore_to(*rule_start);
  469. return nullptr;
  470. }
  471. restore_to(*continue_end);
  472. }
  473. return create<AST::ContinuationControl>(AST::ContinuationControl::Continue);
  474. }
  475. return nullptr;
  476. }
  477. RefPtr<AST::Node> Parser::parse_for_loop()
  478. {
  479. auto rule_start = push_start();
  480. if (!expect("for"))
  481. return nullptr;
  482. if (consume_while(is_any_of(" \t\n")).is_empty()) {
  483. restore_to(*rule_start);
  484. return nullptr;
  485. }
  486. Optional<AST::NameWithPosition> index_variable_name, variable_name;
  487. Optional<AST::Position> in_start_position, index_start_position;
  488. auto offset_before_index = current_position();
  489. if (expect("index")) {
  490. auto offset = current_position();
  491. if (!consume_while(is_whitespace).is_empty()) {
  492. auto offset_before_variable = current_position();
  493. auto variable = consume_while(is_word_character);
  494. if (!variable.is_empty()) {
  495. index_start_position = AST::Position { offset_before_index.offset, offset.offset, offset_before_index.line, offset.line };
  496. auto offset_after_variable = current_position();
  497. index_variable_name = AST::NameWithPosition {
  498. variable,
  499. { offset_before_variable.offset, offset_after_variable.offset, offset_before_variable.line, offset_after_variable.line },
  500. };
  501. consume_while(is_whitespace);
  502. } else {
  503. restore_to(offset_before_index.offset, offset_before_index.line);
  504. }
  505. } else {
  506. restore_to(offset_before_index.offset, offset_before_index.line);
  507. }
  508. }
  509. auto variable_name_start_offset = current_position();
  510. auto name = consume_while(is_word_character);
  511. auto variable_name_end_offset = current_position();
  512. if (!name.is_empty()) {
  513. variable_name = AST::NameWithPosition {
  514. name,
  515. { variable_name_start_offset.offset, variable_name_end_offset.offset, variable_name_start_offset.line, variable_name_end_offset.line }
  516. };
  517. consume_while(is_whitespace);
  518. auto in_error_start = push_start();
  519. if (!expect("in")) {
  520. auto syntax_error = create<AST::SyntaxError>("Expected 'in' after a variable name in a 'for' loop", true);
  521. return create<AST::ForLoop>(move(variable_name), move(index_variable_name), move(syntax_error), nullptr); // ForLoop Var Iterated Block
  522. }
  523. in_start_position = AST::Position { in_error_start->offset, m_offset, in_error_start->line, line() };
  524. }
  525. consume_while(is_whitespace);
  526. RefPtr<AST::Node> iterated_expression;
  527. {
  528. auto iter_error_start = push_start();
  529. iterated_expression = parse_expression();
  530. if (!iterated_expression)
  531. iterated_expression = create<AST::SyntaxError>("Expected an expression in 'for' loop", true);
  532. }
  533. consume_while(is_any_of(" \t\n"));
  534. {
  535. auto obrace_error_start = push_start();
  536. if (!expect('{')) {
  537. auto syntax_error = create<AST::SyntaxError>("Expected an open brace '{' to start a 'for' loop body", true);
  538. return create<AST::ForLoop>(move(variable_name), move(index_variable_name), move(iterated_expression), move(syntax_error), move(in_start_position), move(index_start_position)); // ForLoop Var Iterated Block
  539. }
  540. }
  541. TemporaryChange controls { m_continuation_controls_allowed, true };
  542. auto body = parse_toplevel();
  543. {
  544. auto cbrace_error_start = push_start();
  545. if (!expect('}')) {
  546. auto error_start = push_start();
  547. auto syntax_error = create<AST::SyntaxError>("Expected a close brace '}' to end a 'for' loop body", true);
  548. if (body)
  549. body->set_is_syntax_error(*syntax_error);
  550. else
  551. body = syntax_error;
  552. }
  553. }
  554. return create<AST::ForLoop>(move(variable_name), move(index_variable_name), move(iterated_expression), move(body), move(in_start_position), move(index_start_position)); // ForLoop Var Iterated Block
  555. }
  556. RefPtr<AST::Node> Parser::parse_loop_loop()
  557. {
  558. auto rule_start = push_start();
  559. if (!expect("loop"))
  560. return nullptr;
  561. if (consume_while(is_any_of(" \t\n")).is_empty()) {
  562. restore_to(*rule_start);
  563. return nullptr;
  564. }
  565. {
  566. auto obrace_error_start = push_start();
  567. if (!expect('{')) {
  568. auto syntax_error = create<AST::SyntaxError>("Expected an open brace '{' to start a 'loop' loop body", true);
  569. return create<AST::ForLoop>(AST::NameWithPosition {}, AST::NameWithPosition {}, nullptr, move(syntax_error)); // ForLoop null null Block
  570. }
  571. }
  572. TemporaryChange controls { m_continuation_controls_allowed, true };
  573. auto body = parse_toplevel();
  574. {
  575. auto cbrace_error_start = push_start();
  576. if (!expect('}')) {
  577. auto error_start = push_start();
  578. auto syntax_error = create<AST::SyntaxError>("Expected a close brace '}' to end a 'loop' loop body", true);
  579. if (body)
  580. body->set_is_syntax_error(*syntax_error);
  581. else
  582. body = syntax_error;
  583. }
  584. }
  585. return create<AST::ForLoop>(AST::NameWithPosition {}, AST::NameWithPosition {}, nullptr, move(body)); // ForLoop null null Block
  586. }
  587. RefPtr<AST::Node> Parser::parse_if_expr()
  588. {
  589. auto rule_start = push_start();
  590. if (!expect("if"))
  591. return nullptr;
  592. if (consume_while(is_any_of(" \t\n")).is_empty()) {
  593. restore_to(*rule_start);
  594. return nullptr;
  595. }
  596. RefPtr<AST::Node> condition;
  597. {
  598. auto cond_error_start = push_start();
  599. condition = parse_or_logical_sequence();
  600. if (!condition)
  601. condition = create<AST::SyntaxError>("Expected a logical sequence after 'if'", true);
  602. }
  603. auto parse_braced_toplevel = [&]() -> RefPtr<AST::Node> {
  604. RefPtr<AST::Node> body;
  605. {
  606. auto obrace_error_start = push_start();
  607. if (!expect('{')) {
  608. body = create<AST::SyntaxError>("Expected an open brace '{' to start an 'if' true branch", true);
  609. }
  610. }
  611. if (!body)
  612. body = parse_toplevel();
  613. {
  614. auto cbrace_error_start = push_start();
  615. if (!expect('}')) {
  616. auto error_start = push_start();
  617. RefPtr<AST::SyntaxError> syntax_error = create<AST::SyntaxError>("Expected a close brace '}' to end an 'if' true branch", true);
  618. if (body)
  619. body->set_is_syntax_error(*syntax_error);
  620. else
  621. body = syntax_error;
  622. }
  623. }
  624. return body;
  625. };
  626. consume_while(is_any_of(" \t\n"));
  627. auto true_branch = parse_braced_toplevel();
  628. auto end_before_else = m_offset;
  629. auto line_before_else = line();
  630. consume_while(is_any_of(" \t\n"));
  631. Optional<AST::Position> else_position;
  632. {
  633. auto else_start = push_start();
  634. if (expect("else"))
  635. else_position = AST::Position { else_start->offset, m_offset, else_start->line, line() };
  636. else
  637. restore_to(end_before_else, line_before_else);
  638. }
  639. if (else_position.has_value()) {
  640. consume_while(is_any_of(" \t\n"));
  641. if (peek() == '{') {
  642. auto false_branch = parse_braced_toplevel();
  643. return create<AST::IfCond>(else_position, condition.release_nonnull(), move(true_branch), move(false_branch)); // If expr true_branch Else false_branch
  644. }
  645. auto else_if_branch = parse_if_expr();
  646. return create<AST::IfCond>(else_position, condition.release_nonnull(), move(true_branch), move(else_if_branch)); // If expr true_branch Else If ...
  647. }
  648. return create<AST::IfCond>(else_position, condition.release_nonnull(), move(true_branch), nullptr); // If expr true_branch
  649. }
  650. RefPtr<AST::Node> Parser::parse_subshell()
  651. {
  652. auto rule_start = push_start();
  653. if (!expect('{'))
  654. return nullptr;
  655. auto body = parse_toplevel();
  656. {
  657. auto cbrace_error_start = push_start();
  658. if (!expect('}')) {
  659. auto error_start = push_start();
  660. RefPtr<AST::SyntaxError> syntax_error = create<AST::SyntaxError>("Expected a close brace '}' to end a subshell", true);
  661. if (body)
  662. body->set_is_syntax_error(*syntax_error);
  663. else
  664. body = syntax_error;
  665. }
  666. }
  667. return create<AST::Subshell>(move(body));
  668. }
  669. RefPtr<AST::Node> Parser::parse_match_expr()
  670. {
  671. auto rule_start = push_start();
  672. if (!expect("match"))
  673. return nullptr;
  674. if (consume_while(is_whitespace).is_empty()) {
  675. restore_to(*rule_start);
  676. return nullptr;
  677. }
  678. auto match_expression = parse_expression();
  679. if (!match_expression) {
  680. return create<AST::MatchExpr>(
  681. create<AST::SyntaxError>("Expected an expression after 'match'", true),
  682. String {}, Optional<AST::Position> {}, Vector<AST::MatchEntry> {});
  683. }
  684. consume_while(is_any_of(" \t\n"));
  685. String match_name;
  686. Optional<AST::Position> as_position;
  687. auto as_start = m_offset;
  688. auto as_line = line();
  689. if (expect("as")) {
  690. as_position = AST::Position { as_start, m_offset, as_line, line() };
  691. if (consume_while(is_any_of(" \t\n")).is_empty()) {
  692. auto node = create<AST::MatchExpr>(
  693. match_expression.release_nonnull(),
  694. String {}, move(as_position), Vector<AST::MatchEntry> {});
  695. node->set_is_syntax_error(create<AST::SyntaxError>("Expected whitespace after 'as' in 'match'", true));
  696. return node;
  697. }
  698. match_name = consume_while(is_word_character);
  699. if (match_name.is_empty()) {
  700. auto node = create<AST::MatchExpr>(
  701. match_expression.release_nonnull(),
  702. String {}, move(as_position), Vector<AST::MatchEntry> {});
  703. node->set_is_syntax_error(create<AST::SyntaxError>("Expected an identifier after 'as' in 'match'", true));
  704. return node;
  705. }
  706. }
  707. consume_while(is_any_of(" \t\n"));
  708. if (!expect('{')) {
  709. auto node = create<AST::MatchExpr>(
  710. match_expression.release_nonnull(),
  711. move(match_name), move(as_position), Vector<AST::MatchEntry> {});
  712. node->set_is_syntax_error(create<AST::SyntaxError>("Expected an open brace '{' to start a 'match' entry list", true));
  713. return node;
  714. }
  715. consume_while(is_any_of(" \t\n"));
  716. Vector<AST::MatchEntry> entries;
  717. for (;;) {
  718. auto entry = parse_match_entry();
  719. consume_while(is_any_of(" \t\n"));
  720. if (entry.options.is_empty())
  721. break;
  722. entries.append(entry);
  723. }
  724. consume_while(is_any_of(" \t\n"));
  725. if (!expect('}')) {
  726. auto node = create<AST::MatchExpr>(
  727. match_expression.release_nonnull(),
  728. move(match_name), move(as_position), move(entries));
  729. node->set_is_syntax_error(create<AST::SyntaxError>("Expected a close brace '}' to end a 'match' entry list", true));
  730. return node;
  731. }
  732. return create<AST::MatchExpr>(match_expression.release_nonnull(), move(match_name), move(as_position), move(entries));
  733. }
  734. AST::MatchEntry Parser::parse_match_entry()
  735. {
  736. auto rule_start = push_start();
  737. NonnullRefPtrVector<AST::Node> patterns;
  738. Vector<AST::Position> pipe_positions;
  739. Optional<Vector<String>> match_names;
  740. Optional<AST::Position> match_as_position;
  741. auto pattern = parse_match_pattern();
  742. if (!pattern)
  743. return { {}, {}, {}, {}, create<AST::SyntaxError>("Expected a pattern in 'match' body", true) };
  744. patterns.append(pattern.release_nonnull());
  745. consume_while(is_any_of(" \t\n"));
  746. auto previous_pipe_start_position = m_offset;
  747. auto previous_pipe_start_line = line();
  748. RefPtr<AST::SyntaxError> error;
  749. while (expect('|')) {
  750. pipe_positions.append({ previous_pipe_start_position, m_offset, previous_pipe_start_line, line() });
  751. consume_while(is_any_of(" \t\n"));
  752. auto pattern = parse_match_pattern();
  753. if (!pattern) {
  754. error = create<AST::SyntaxError>("Expected a pattern to follow '|' in 'match' body", true);
  755. break;
  756. }
  757. consume_while(is_any_of(" \t\n"));
  758. patterns.append(pattern.release_nonnull());
  759. previous_pipe_start_line = line();
  760. previous_pipe_start_position = m_offset;
  761. }
  762. consume_while(is_any_of(" \t\n"));
  763. auto as_start_position = m_offset;
  764. auto as_start_line = line();
  765. if (expect("as")) {
  766. match_as_position = AST::Position { as_start_position, m_offset, as_start_line, line() };
  767. consume_while(is_any_of(" \t\n"));
  768. if (!expect('(')) {
  769. if (!error)
  770. error = create<AST::SyntaxError>("Expected an explicit list of identifiers after a pattern 'as'");
  771. } else {
  772. match_names = Vector<String>();
  773. for (;;) {
  774. consume_while(is_whitespace);
  775. auto name = consume_while(is_word_character);
  776. if (name.is_empty())
  777. break;
  778. match_names.value().append(move(name));
  779. }
  780. if (!expect(')')) {
  781. if (!error)
  782. error = create<AST::SyntaxError>("Expected a close paren ')' to end the identifier list of pattern 'as'", true);
  783. }
  784. }
  785. consume_while(is_any_of(" \t\n"));
  786. }
  787. if (!expect('{')) {
  788. if (!error)
  789. error = create<AST::SyntaxError>("Expected an open brace '{' to start a match entry body", true);
  790. }
  791. auto body = parse_toplevel();
  792. if (!expect('}')) {
  793. if (!error)
  794. error = create<AST::SyntaxError>("Expected a close brace '}' to end a match entry body", true);
  795. }
  796. if (body && error)
  797. body->set_is_syntax_error(*error);
  798. else if (error)
  799. body = error;
  800. return { move(patterns), move(match_names), move(match_as_position), move(pipe_positions), move(body) };
  801. }
  802. RefPtr<AST::Node> Parser::parse_match_pattern()
  803. {
  804. return parse_expression();
  805. }
  806. RefPtr<AST::Node> Parser::parse_redirection()
  807. {
  808. auto rule_start = push_start();
  809. auto pipe_fd = 0;
  810. auto number = consume_while(is_digit);
  811. if (number.is_empty()) {
  812. pipe_fd = -1;
  813. } else {
  814. auto fd = number.to_int();
  815. pipe_fd = fd.value_or(-1);
  816. }
  817. switch (peek()) {
  818. case '>': {
  819. consume();
  820. if (peek() == '>') {
  821. consume();
  822. consume_while(is_whitespace);
  823. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDOUT_FILENO;
  824. auto path = parse_expression();
  825. if (!path) {
  826. if (!at_end()) {
  827. // Eat a character and hope the problem goes away
  828. consume();
  829. }
  830. path = create<AST::SyntaxError>("Expected a path after redirection", true);
  831. }
  832. return create<AST::WriteAppendRedirection>(pipe_fd, path.release_nonnull()); // Redirection WriteAppend
  833. }
  834. if (peek() == '&') {
  835. consume();
  836. // FIXME: 'fd>&-' Syntax not the best. needs discussion.
  837. if (peek() == '-') {
  838. consume();
  839. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDOUT_FILENO;
  840. return create<AST::CloseFdRedirection>(pipe_fd); // Redirection CloseFd
  841. }
  842. int dest_pipe_fd = 0;
  843. auto number = consume_while(is_digit);
  844. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDOUT_FILENO;
  845. if (number.is_empty()) {
  846. dest_pipe_fd = -1;
  847. } else {
  848. auto fd = number.to_int();
  849. dest_pipe_fd = fd.value_or(-1);
  850. }
  851. auto redir = create<AST::Fd2FdRedirection>(pipe_fd, dest_pipe_fd); // Redirection Fd2Fd
  852. if (dest_pipe_fd == -1)
  853. redir->set_is_syntax_error(*create<AST::SyntaxError>("Expected a file descriptor"));
  854. return redir;
  855. }
  856. consume_while(is_whitespace);
  857. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDOUT_FILENO;
  858. auto path = parse_expression();
  859. if (!path) {
  860. if (!at_end()) {
  861. // Eat a character and hope the problem goes away
  862. consume();
  863. }
  864. path = create<AST::SyntaxError>("Expected a path after redirection", true);
  865. }
  866. return create<AST::WriteRedirection>(pipe_fd, path.release_nonnull()); // Redirection Write
  867. }
  868. case '<': {
  869. consume();
  870. enum {
  871. Read,
  872. ReadWrite,
  873. } mode { Read };
  874. if (peek() == '>') {
  875. mode = ReadWrite;
  876. consume();
  877. }
  878. consume_while(is_whitespace);
  879. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDIN_FILENO;
  880. auto path = parse_expression();
  881. if (!path) {
  882. if (!at_end()) {
  883. // Eat a character and hope the problem goes away
  884. consume();
  885. }
  886. path = create<AST::SyntaxError>("Expected a path after redirection", true);
  887. }
  888. if (mode == Read)
  889. return create<AST::ReadRedirection>(pipe_fd, path.release_nonnull()); // Redirection Read
  890. return create<AST::ReadWriteRedirection>(pipe_fd, path.release_nonnull()); // Redirection ReadWrite
  891. }
  892. default:
  893. restore_to(*rule_start);
  894. return nullptr;
  895. }
  896. }
  897. RefPtr<AST::Node> Parser::parse_list_expression()
  898. {
  899. consume_while(is_whitespace);
  900. auto rule_start = push_start();
  901. Vector<NonnullRefPtr<AST::Node>> nodes;
  902. do {
  903. auto expr = parse_expression();
  904. if (!expr)
  905. break;
  906. nodes.append(expr.release_nonnull());
  907. } while (!consume_while(is_whitespace).is_empty());
  908. if (nodes.is_empty())
  909. return nullptr;
  910. return create<AST::ListConcatenate>(move(nodes)); // Concatenate List
  911. }
  912. RefPtr<AST::Node> Parser::parse_expression()
  913. {
  914. auto rule_start = push_start();
  915. if (m_rule_start_offsets.size() > max_allowed_nested_rule_depth)
  916. return create<AST::SyntaxError>(String::formatted("Expression nested too deep (max allowed is {})", max_allowed_nested_rule_depth));
  917. auto starting_char = peek();
  918. auto read_concat = [&](auto&& expr) -> NonnullRefPtr<AST::Node> {
  919. if (is_whitespace(peek()))
  920. return move(expr);
  921. if (auto next_expr = parse_expression())
  922. return create<AST::Juxtaposition>(move(expr), next_expr.release_nonnull());
  923. return move(expr);
  924. };
  925. if (strchr("&|)} ;<>\n", starting_char) != nullptr)
  926. return nullptr;
  927. if (m_extra_chars_not_allowed_in_barewords.contains_slow(starting_char))
  928. return nullptr;
  929. if (m_is_in_brace_expansion_spec && next_is(".."))
  930. return nullptr;
  931. if (isdigit(starting_char)) {
  932. ScopedValueRollback offset_rollback { m_offset };
  933. auto redir = parse_redirection();
  934. if (redir)
  935. return nullptr;
  936. }
  937. if (starting_char == '$') {
  938. if (auto variable = parse_variable())
  939. return read_concat(variable.release_nonnull());
  940. if (auto immediate = parse_immediate_expression())
  941. return read_concat(immediate.release_nonnull());
  942. if (auto inline_exec = parse_evaluate())
  943. return read_concat(inline_exec.release_nonnull());
  944. }
  945. if (starting_char == '#')
  946. return parse_comment();
  947. if (starting_char == '(') {
  948. consume();
  949. auto list = parse_list_expression();
  950. if (!expect(')')) {
  951. restore_to(*rule_start);
  952. return nullptr;
  953. }
  954. return read_concat(create<AST::CastToList>(move(list))); // Cast To List
  955. }
  956. if (starting_char == '!' && m_in_interactive_mode) {
  957. if (auto designator = parse_history_designator())
  958. return designator;
  959. }
  960. if (auto composite = parse_string_composite())
  961. return read_concat(composite.release_nonnull());
  962. return nullptr;
  963. }
  964. RefPtr<AST::Node> Parser::parse_string_composite()
  965. {
  966. auto rule_start = push_start();
  967. if (auto string = parse_string()) {
  968. if (auto next_part = parse_string_composite())
  969. return create<AST::Juxtaposition>(string.release_nonnull(), next_part.release_nonnull()); // Concatenate String StringComposite
  970. return string;
  971. }
  972. if (auto variable = parse_variable()) {
  973. if (auto next_part = parse_string_composite())
  974. return create<AST::Juxtaposition>(variable.release_nonnull(), next_part.release_nonnull()); // Concatenate Variable StringComposite
  975. return variable;
  976. }
  977. if (auto glob = parse_glob()) {
  978. if (auto next_part = parse_string_composite())
  979. return create<AST::Juxtaposition>(glob.release_nonnull(), next_part.release_nonnull()); // Concatenate Glob StringComposite
  980. return glob;
  981. }
  982. if (auto expansion = parse_brace_expansion()) {
  983. if (auto next_part = parse_string_composite())
  984. return create<AST::Juxtaposition>(expansion.release_nonnull(), next_part.release_nonnull()); // Concatenate BraceExpansion StringComposite
  985. return expansion;
  986. }
  987. if (auto bareword = parse_bareword()) {
  988. if (auto next_part = parse_string_composite())
  989. return create<AST::Juxtaposition>(bareword.release_nonnull(), next_part.release_nonnull()); // Concatenate Bareword StringComposite
  990. return bareword;
  991. }
  992. if (auto inline_command = parse_evaluate()) {
  993. if (auto next_part = parse_string_composite())
  994. return create<AST::Juxtaposition>(inline_command.release_nonnull(), next_part.release_nonnull()); // Concatenate Execute StringComposite
  995. return inline_command;
  996. }
  997. return nullptr;
  998. }
  999. RefPtr<AST::Node> Parser::parse_string()
  1000. {
  1001. auto rule_start = push_start();
  1002. if (at_end())
  1003. return nullptr;
  1004. if (peek() == '"') {
  1005. consume();
  1006. auto inner = parse_doublequoted_string_inner();
  1007. if (!inner)
  1008. inner = create<AST::SyntaxError>("Unexpected EOF in string", true);
  1009. if (!expect('"')) {
  1010. inner = create<AST::DoubleQuotedString>(move(inner));
  1011. inner->set_is_syntax_error(*create<AST::SyntaxError>("Expected a terminating double quote", true));
  1012. return inner;
  1013. }
  1014. return create<AST::DoubleQuotedString>(move(inner)); // Double Quoted String
  1015. }
  1016. if (peek() == '\'') {
  1017. consume();
  1018. auto text = consume_while(is_not('\''));
  1019. bool is_error = false;
  1020. if (!expect('\''))
  1021. is_error = true;
  1022. auto result = create<AST::StringLiteral>(move(text)); // String Literal
  1023. if (is_error)
  1024. result->set_is_syntax_error(*create<AST::SyntaxError>("Expected a terminating single quote", true));
  1025. return move(result);
  1026. }
  1027. return nullptr;
  1028. }
  1029. RefPtr<AST::Node> Parser::parse_doublequoted_string_inner()
  1030. {
  1031. auto rule_start = push_start();
  1032. if (at_end())
  1033. return nullptr;
  1034. StringBuilder builder;
  1035. while (!at_end() && peek() != '"') {
  1036. if (peek() == '\\') {
  1037. consume();
  1038. if (at_end()) {
  1039. break;
  1040. }
  1041. auto ch = consume();
  1042. switch (ch) {
  1043. case '\\':
  1044. default:
  1045. builder.append(ch);
  1046. break;
  1047. case 'x': {
  1048. if (m_input.length() <= m_offset + 2)
  1049. break;
  1050. auto first_nibble = tolower(consume());
  1051. auto second_nibble = tolower(consume());
  1052. if (!isxdigit(first_nibble) || !isxdigit(second_nibble)) {
  1053. builder.append(first_nibble);
  1054. builder.append(second_nibble);
  1055. break;
  1056. }
  1057. builder.append(to_byte(first_nibble, second_nibble));
  1058. break;
  1059. }
  1060. case 'a':
  1061. builder.append('\a');
  1062. break;
  1063. case 'b':
  1064. builder.append('\b');
  1065. break;
  1066. case 'e':
  1067. builder.append('\x1b');
  1068. break;
  1069. case 'f':
  1070. builder.append('\f');
  1071. break;
  1072. case 'r':
  1073. builder.append('\r');
  1074. break;
  1075. case 'n':
  1076. builder.append('\n');
  1077. break;
  1078. }
  1079. continue;
  1080. }
  1081. if (peek() == '$') {
  1082. auto string_literal = create<AST::StringLiteral>(builder.to_string()); // String Literal
  1083. auto read_concat = [&](auto&& node) {
  1084. auto inner = create<AST::StringPartCompose>(
  1085. move(string_literal),
  1086. move(node)); // Compose String Node
  1087. if (auto string = parse_doublequoted_string_inner()) {
  1088. return create<AST::StringPartCompose>(move(inner), string.release_nonnull()); // Compose Composition Composition
  1089. }
  1090. return inner;
  1091. };
  1092. if (auto variable = parse_variable())
  1093. return read_concat(variable.release_nonnull());
  1094. if (auto immediate = parse_immediate_expression())
  1095. return read_concat(immediate.release_nonnull());
  1096. if (auto evaluate = parse_evaluate())
  1097. return read_concat(evaluate.release_nonnull());
  1098. }
  1099. builder.append(consume());
  1100. }
  1101. return create<AST::StringLiteral>(builder.to_string()); // String Literal
  1102. }
  1103. RefPtr<AST::Node> Parser::parse_variable()
  1104. {
  1105. auto rule_start = push_start();
  1106. if (at_end())
  1107. return nullptr;
  1108. if (peek() != '$')
  1109. return nullptr;
  1110. consume();
  1111. switch (peek()) {
  1112. case '$':
  1113. case '?':
  1114. case '*':
  1115. case '#':
  1116. return create<AST::SpecialVariable>(consume()); // Variable Special
  1117. default:
  1118. break;
  1119. }
  1120. auto name = consume_while(is_word_character);
  1121. if (name.length() == 0) {
  1122. restore_to(rule_start->offset, rule_start->line);
  1123. return nullptr;
  1124. }
  1125. return create<AST::SimpleVariable>(move(name)); // Variable Simple
  1126. }
  1127. RefPtr<AST::Node> Parser::parse_evaluate()
  1128. {
  1129. auto rule_start = push_start();
  1130. if (at_end())
  1131. return nullptr;
  1132. if (peek() != '$')
  1133. return nullptr;
  1134. consume();
  1135. if (peek() == '(') {
  1136. consume();
  1137. auto inner = parse_pipe_sequence();
  1138. if (!inner)
  1139. inner = create<AST::SyntaxError>("Unexpected EOF in list", true);
  1140. if (!expect(')'))
  1141. inner->set_is_syntax_error(*create<AST::SyntaxError>("Expected a terminating close paren", true));
  1142. return create<AST::Execute>(inner.release_nonnull(), true);
  1143. }
  1144. auto inner = parse_expression();
  1145. if (!inner) {
  1146. inner = create<AST::SyntaxError>("Expected a command", true);
  1147. } else {
  1148. if (inner->is_list()) {
  1149. auto execute_inner = create<AST::Execute>(inner.release_nonnull(), true);
  1150. inner = move(execute_inner);
  1151. } else {
  1152. auto dyn_inner = create<AST::DynamicEvaluate>(inner.release_nonnull());
  1153. inner = move(dyn_inner);
  1154. }
  1155. }
  1156. return inner;
  1157. }
  1158. RefPtr<AST::Node> Parser::parse_immediate_expression()
  1159. {
  1160. auto rule_start = push_start();
  1161. if (at_end())
  1162. return nullptr;
  1163. if (peek() != '$')
  1164. return nullptr;
  1165. consume();
  1166. if (peek() != '{') {
  1167. restore_to(*rule_start);
  1168. return nullptr;
  1169. }
  1170. consume();
  1171. consume_while(is_whitespace);
  1172. auto function_name_start_offset = current_position();
  1173. auto function_name = consume_while(is_word_character);
  1174. auto function_name_end_offset = current_position();
  1175. AST::Position function_position {
  1176. function_name_start_offset.offset,
  1177. function_name_end_offset.offset,
  1178. function_name_start_offset.line,
  1179. function_name_end_offset.line,
  1180. };
  1181. consume_while(is_whitespace);
  1182. NonnullRefPtrVector<AST::Node> arguments;
  1183. do {
  1184. auto expr = parse_expression();
  1185. if (!expr)
  1186. break;
  1187. arguments.append(expr.release_nonnull());
  1188. } while (!consume_while(is_whitespace).is_empty());
  1189. auto ending_brace_start_offset = current_position();
  1190. if (peek() == '}')
  1191. consume();
  1192. auto ending_brace_end_offset = current_position();
  1193. auto ending_brace_position = ending_brace_start_offset.offset == ending_brace_end_offset.offset
  1194. ? Optional<AST::Position> {}
  1195. : Optional<AST::Position> {
  1196. AST::Position {
  1197. ending_brace_start_offset.offset,
  1198. ending_brace_end_offset.offset,
  1199. ending_brace_start_offset.line,
  1200. ending_brace_end_offset.line,
  1201. }
  1202. };
  1203. auto node = create<AST::ImmediateExpression>(
  1204. AST::NameWithPosition { function_name, move(function_position) },
  1205. move(arguments),
  1206. ending_brace_position);
  1207. if (!ending_brace_position.has_value())
  1208. node->set_is_syntax_error(create<AST::SyntaxError>("Expected a closing brace '}' to end an immediate expression", true));
  1209. else if (node->function_name().is_empty())
  1210. node->set_is_syntax_error(create<AST::SyntaxError>("Expected an immediate function name"));
  1211. return node;
  1212. }
  1213. RefPtr<AST::Node> Parser::parse_history_designator()
  1214. {
  1215. auto rule_start = push_start();
  1216. VERIFY(peek() == '!');
  1217. consume();
  1218. // Event selector
  1219. AST::HistorySelector selector;
  1220. RefPtr<AST::SyntaxError> syntax_error;
  1221. selector.event.kind = AST::HistorySelector::EventKind::StartingStringLookup;
  1222. selector.event.text_position = { m_offset, m_offset, m_line, m_line };
  1223. selector.word_selector_range = {
  1224. AST::HistorySelector::WordSelector {
  1225. AST::HistorySelector::WordSelectorKind::Index,
  1226. 0,
  1227. { m_offset, m_offset, m_line, m_line },
  1228. nullptr },
  1229. AST::HistorySelector::WordSelector {
  1230. AST::HistorySelector::WordSelectorKind::Last,
  1231. 0,
  1232. { m_offset, m_offset, m_line, m_line },
  1233. nullptr }
  1234. };
  1235. switch (peek()) {
  1236. case '!':
  1237. consume();
  1238. selector.event.kind = AST::HistorySelector::EventKind::IndexFromEnd;
  1239. selector.event.index = 0;
  1240. selector.event.text = "!";
  1241. break;
  1242. case '?':
  1243. consume();
  1244. selector.event.kind = AST::HistorySelector::EventKind::ContainingStringLookup;
  1245. [[fallthrough]];
  1246. default: {
  1247. TemporaryChange chars_change { m_extra_chars_not_allowed_in_barewords, { ':' } };
  1248. auto bareword = parse_bareword();
  1249. if (!bareword || !bareword->is_bareword()) {
  1250. restore_to(*rule_start);
  1251. return nullptr;
  1252. }
  1253. selector.event.text = static_ptr_cast<AST::BarewordLiteral>(bareword)->text();
  1254. selector.event.text_position = bareword->position();
  1255. auto it = selector.event.text.begin();
  1256. bool is_negative = false;
  1257. if (*it == '-') {
  1258. ++it;
  1259. is_negative = true;
  1260. }
  1261. if (it != selector.event.text.end() && all_of(it, selector.event.text.end(), is_digit)) {
  1262. if (is_negative)
  1263. selector.event.kind = AST::HistorySelector::EventKind::IndexFromEnd;
  1264. else
  1265. selector.event.kind = AST::HistorySelector::EventKind::IndexFromStart;
  1266. auto number = selector.event.text.to_int();
  1267. if (number.has_value())
  1268. selector.event.index = abs(number.value());
  1269. else
  1270. syntax_error = create<AST::SyntaxError>("History entry index value invalid or out of range");
  1271. }
  1272. break;
  1273. }
  1274. }
  1275. if (peek() != ':') {
  1276. auto node = create<AST::HistoryEvent>(move(selector));
  1277. if (syntax_error)
  1278. node->set_is_syntax_error(*syntax_error);
  1279. return node;
  1280. }
  1281. consume();
  1282. // Word selectors
  1283. auto parse_word_selector = [&]() -> Optional<AST::HistorySelector::WordSelector> {
  1284. auto rule_start = push_start();
  1285. auto c = peek();
  1286. if (isdigit(c)) {
  1287. auto num = consume_while(is_digit);
  1288. auto value = num.to_uint();
  1289. if (!value.has_value()) {
  1290. return AST::HistorySelector::WordSelector {
  1291. AST::HistorySelector::WordSelectorKind::Index,
  1292. 0,
  1293. { m_rule_start_offsets.last(), m_offset, m_rule_start_lines.last(), line() },
  1294. syntax_error ? NonnullRefPtr(*syntax_error) : create<AST::SyntaxError>("Word selector value invalid or out of range")
  1295. };
  1296. }
  1297. return AST::HistorySelector::WordSelector {
  1298. AST::HistorySelector::WordSelectorKind::Index,
  1299. value.value(),
  1300. { m_rule_start_offsets.last(), m_offset, m_rule_start_lines.last(), line() },
  1301. syntax_error
  1302. };
  1303. }
  1304. if (c == '^') {
  1305. consume();
  1306. return AST::HistorySelector::WordSelector {
  1307. AST::HistorySelector::WordSelectorKind::Index,
  1308. 0,
  1309. { m_rule_start_offsets.last(), m_offset, m_rule_start_lines.last(), line() },
  1310. syntax_error
  1311. };
  1312. }
  1313. if (c == '$') {
  1314. consume();
  1315. return AST::HistorySelector::WordSelector {
  1316. AST::HistorySelector::WordSelectorKind::Last,
  1317. 0,
  1318. { m_rule_start_offsets.last(), m_offset, m_rule_start_lines.last(), line() },
  1319. syntax_error
  1320. };
  1321. }
  1322. return {};
  1323. };
  1324. auto start = parse_word_selector();
  1325. if (!start.has_value()) {
  1326. if (!syntax_error)
  1327. syntax_error = create<AST::SyntaxError>("Expected a word selector after ':' in a history event designator", true);
  1328. auto node = create<AST::HistoryEvent>(move(selector));
  1329. node->set_is_syntax_error(*syntax_error);
  1330. return node;
  1331. }
  1332. selector.word_selector_range.start = start.release_value();
  1333. if (peek() == '-') {
  1334. consume();
  1335. auto end = parse_word_selector();
  1336. if (!end.has_value()) {
  1337. if (!syntax_error)
  1338. syntax_error = create<AST::SyntaxError>("Expected a word selector after '-' in a history event designator word selector", true);
  1339. auto node = create<AST::HistoryEvent>(move(selector));
  1340. node->set_is_syntax_error(*syntax_error);
  1341. return node;
  1342. }
  1343. selector.word_selector_range.end = move(end);
  1344. } else {
  1345. selector.word_selector_range.end.clear();
  1346. }
  1347. auto node = create<AST::HistoryEvent>(move(selector));
  1348. if (syntax_error)
  1349. node->set_is_syntax_error(*syntax_error);
  1350. return node;
  1351. }
  1352. RefPtr<AST::Node> Parser::parse_comment()
  1353. {
  1354. if (at_end())
  1355. return nullptr;
  1356. if (peek() != '#')
  1357. return nullptr;
  1358. consume();
  1359. auto text = consume_while(is_not('\n'));
  1360. return create<AST::Comment>(move(text)); // Comment
  1361. }
  1362. RefPtr<AST::Node> Parser::parse_bareword()
  1363. {
  1364. auto rule_start = push_start();
  1365. StringBuilder builder;
  1366. auto is_acceptable_bareword_character = [&](char c) {
  1367. return strchr("\\\"'*$&#|(){} ?;<>\n", c) == nullptr
  1368. && !m_extra_chars_not_allowed_in_barewords.contains_slow(c);
  1369. };
  1370. while (!at_end()) {
  1371. char ch = peek();
  1372. if (ch == '\\') {
  1373. consume();
  1374. if (!at_end()) {
  1375. ch = consume();
  1376. if (is_acceptable_bareword_character(ch))
  1377. builder.append('\\');
  1378. }
  1379. builder.append(ch);
  1380. continue;
  1381. }
  1382. if (m_is_in_brace_expansion_spec && next_is("..")) {
  1383. // Don't eat '..' in a brace expansion spec.
  1384. break;
  1385. }
  1386. if (is_acceptable_bareword_character(ch)) {
  1387. builder.append(consume());
  1388. continue;
  1389. }
  1390. break;
  1391. }
  1392. if (builder.is_empty())
  1393. return nullptr;
  1394. auto current_end = m_offset;
  1395. auto current_line = line();
  1396. auto string = builder.to_string();
  1397. if (string.starts_with('~')) {
  1398. String username;
  1399. RefPtr<AST::Node> tilde, text;
  1400. auto first_slash_index = string.index_of("/");
  1401. if (first_slash_index.has_value()) {
  1402. username = string.substring_view(1, first_slash_index.value() - 1);
  1403. string = string.substring_view(first_slash_index.value(), string.length() - first_slash_index.value());
  1404. } else {
  1405. username = string.substring_view(1, string.length() - 1);
  1406. string = "";
  1407. }
  1408. // Synthesize a Tilde Node with the correct positioning information.
  1409. {
  1410. restore_to(rule_start->offset, rule_start->line);
  1411. auto ch = consume();
  1412. VERIFY(ch == '~');
  1413. auto username_length = username.length();
  1414. tilde = create<AST::Tilde>(move(username));
  1415. // Consume the username (if any)
  1416. for (size_t i = 0; i < username_length; ++i)
  1417. consume();
  1418. }
  1419. if (string.is_empty())
  1420. return tilde;
  1421. // Synthesize a BarewordLiteral Node with the correct positioning information.
  1422. {
  1423. auto text_start = push_start();
  1424. restore_to(current_end, current_line);
  1425. text = create<AST::BarewordLiteral>(move(string));
  1426. }
  1427. return create<AST::Juxtaposition>(tilde.release_nonnull(), text.release_nonnull()); // Juxtaposition Variable Bareword
  1428. }
  1429. if (string.starts_with("\\~")) {
  1430. // Un-escape the tilde, but only at the start (where it would be an expansion)
  1431. string = string.substring(1, string.length() - 1);
  1432. }
  1433. return create<AST::BarewordLiteral>(move(string)); // Bareword Literal
  1434. }
  1435. RefPtr<AST::Node> Parser::parse_glob()
  1436. {
  1437. auto rule_start = push_start();
  1438. auto bareword_part = parse_bareword();
  1439. if (at_end())
  1440. return bareword_part;
  1441. char ch = peek();
  1442. if (ch == '*' || ch == '?') {
  1443. auto saved_offset = save_offset();
  1444. consume();
  1445. StringBuilder textbuilder;
  1446. if (bareword_part) {
  1447. StringView text;
  1448. if (bareword_part->is_bareword()) {
  1449. auto bareword = static_cast<AST::BarewordLiteral*>(bareword_part.ptr());
  1450. text = bareword->text();
  1451. } else {
  1452. // FIXME: Allow composition of tilde+bareword with globs: '~/foo/bar/baz*'
  1453. restore_to(saved_offset.offset, saved_offset.line);
  1454. bareword_part->set_is_syntax_error(*create<AST::SyntaxError>(String::format("Unexpected %s inside a glob", bareword_part->class_name().characters())));
  1455. return bareword_part;
  1456. }
  1457. textbuilder.append(text);
  1458. }
  1459. textbuilder.append(ch);
  1460. auto glob_after = parse_glob();
  1461. if (glob_after) {
  1462. if (glob_after->is_glob()) {
  1463. auto glob = static_cast<AST::Glob*>(glob_after.ptr());
  1464. textbuilder.append(glob->text());
  1465. } else if (glob_after->is_bareword()) {
  1466. auto bareword = static_cast<AST::BarewordLiteral*>(glob_after.ptr());
  1467. textbuilder.append(bareword->text());
  1468. } else if (glob_after->is_tilde()) {
  1469. auto bareword = static_cast<AST::Tilde*>(glob_after.ptr());
  1470. textbuilder.append("~");
  1471. textbuilder.append(bareword->text());
  1472. } else {
  1473. return create<AST::SyntaxError>(String::formatted("Invalid node '{}' in glob position, escape shell special characters", glob_after->class_name()));
  1474. }
  1475. }
  1476. return create<AST::Glob>(textbuilder.to_string()); // Glob
  1477. }
  1478. return bareword_part;
  1479. }
  1480. RefPtr<AST::Node> Parser::parse_brace_expansion()
  1481. {
  1482. auto rule_start = push_start();
  1483. if (!expect('{'))
  1484. return nullptr;
  1485. if (auto spec = parse_brace_expansion_spec()) {
  1486. if (!expect('}'))
  1487. spec->set_is_syntax_error(create<AST::SyntaxError>("Expected a close brace '}' to end a brace expansion", true));
  1488. return spec;
  1489. }
  1490. restore_to(*rule_start);
  1491. return nullptr;
  1492. }
  1493. RefPtr<AST::Node> Parser::parse_brace_expansion_spec()
  1494. {
  1495. TemporaryChange is_in_brace_expansion { m_is_in_brace_expansion_spec, true };
  1496. TemporaryChange chars_change { m_extra_chars_not_allowed_in_barewords, { ',' } };
  1497. auto rule_start = push_start();
  1498. auto start_expr = parse_expression();
  1499. if (start_expr) {
  1500. if (expect("..")) {
  1501. if (auto end_expr = parse_expression()) {
  1502. if (end_expr->position().start_offset != start_expr->position().end_offset + 2)
  1503. end_expr->set_is_syntax_error(create<AST::SyntaxError>("Expected no whitespace between '..' and the following expression in brace expansion"));
  1504. return create<AST::Range>(start_expr.release_nonnull(), end_expr.release_nonnull());
  1505. }
  1506. return create<AST::Range>(start_expr.release_nonnull(), create<AST::SyntaxError>("Expected an expression to end range brace expansion with", true));
  1507. }
  1508. }
  1509. NonnullRefPtrVector<AST::Node> subexpressions;
  1510. if (start_expr)
  1511. subexpressions.append(start_expr.release_nonnull());
  1512. while (expect(',')) {
  1513. auto expr = parse_expression();
  1514. if (expr) {
  1515. subexpressions.append(expr.release_nonnull());
  1516. } else {
  1517. subexpressions.append(create<AST::StringLiteral>(""));
  1518. }
  1519. }
  1520. if (subexpressions.is_empty())
  1521. return nullptr;
  1522. return create<AST::BraceExpansion>(move(subexpressions));
  1523. }
  1524. StringView Parser::consume_while(Function<bool(char)> condition)
  1525. {
  1526. if (at_end())
  1527. return {};
  1528. auto start_offset = m_offset;
  1529. while (!at_end() && condition(peek()))
  1530. consume();
  1531. return m_input.substring_view(start_offset, m_offset - start_offset);
  1532. }
  1533. bool Parser::next_is(const StringView& next)
  1534. {
  1535. auto start = push_start();
  1536. auto res = expect(next);
  1537. restore_to(*start);
  1538. return res;
  1539. }
  1540. }