Parser.cpp 66 KB

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