Parser.cpp 68 KB

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