Parser.cpp 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251
  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(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() }, forward<Args>(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"_string.release_value_but_fixme_should_propagate_errors());
  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. Vector<NonnullRefPtr<AST::Node>> Parser::parse_as_multiple_expressions()
  123. {
  124. Vector<NonnullRefPtr<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. }
  135. RefPtr<AST::Node> Parser::parse_toplevel()
  136. {
  137. auto rule_start = push_start();
  138. SequenceParseResult result;
  139. Vector<NonnullRefPtr<AST::Node>> sequence;
  140. Vector<AST::Position> positions;
  141. do {
  142. result = parse_sequence();
  143. if (result.entries.is_empty())
  144. break;
  145. sequence.extend(move(result.entries));
  146. positions.extend(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. Vector<NonnullRefPtr<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;"sv : "\n;"sv));
  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 "sv);
  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.to_string().release_value_but_fixme_should_propagate_errors(), 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;"sv));
  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;"sv));
  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>(String::from_utf8(var_name).release_value_but_fixme_should_propagate_errors());
  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"_string.release_value_but_fixme_should_propagate_errors(), 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>(String {}, AST::StringLiteral::EnclosureType::None);
  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.extend(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({ String::from_utf8(arg_name).release_value_but_fixme_should_propagate_errors(), { name_offset, m_offset, start_line, line() } });
  327. }
  328. consume_while(is_any_of("\n\t "sv));
  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"_string.release_value_but_fixme_should_propagate_errors(), true);
  334. }
  335. if (!expect('{')) {
  336. return create<AST::FunctionDeclaration>(
  337. AST::NameWithPosition {
  338. String::from_utf8(function_name).release_value_but_fixme_should_propagate_errors(),
  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"_string.release_value_but_fixme_should_propagate_errors(), 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. String::from_utf8(function_name).release_value_but_fixme_should_propagate_errors(),
  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. String::from_utf8(function_name).release_value_but_fixme_should_propagate_errors(),
  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("||"sv))
  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 '||'"_string.release_value_but_fixme_should_propagate_errors(), 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("&&"sv))
  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 '&&'"_string.release_value_but_fixme_should_propagate_errors(), 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. auto also_pipe_stderr = peek() == '&';
  428. if (also_pipe_stderr) {
  429. consume();
  430. RefPtr<AST::Node> redirection;
  431. {
  432. auto redirection_start = push_start();
  433. redirection = create<AST::Fd2FdRedirection>(STDERR_FILENO, STDOUT_FILENO);
  434. }
  435. left = create<AST::Join>(left.release_nonnull(), redirection.release_nonnull());
  436. }
  437. if (auto pipe_seq = parse_pipe_sequence()) {
  438. return create<AST::Pipe>(left.release_nonnull(), pipe_seq.release_nonnull()); // Pipe
  439. }
  440. restore_to(before_pipe.offset, before_pipe.line);
  441. return left;
  442. }
  443. RefPtr<AST::Node> Parser::parse_command()
  444. {
  445. auto rule_start = push_start();
  446. consume_while(is_whitespace);
  447. auto redir = parse_redirection();
  448. if (!redir) {
  449. auto list_expr = parse_list_expression();
  450. if (!list_expr)
  451. return nullptr;
  452. auto cast = create<AST::CastToCommand>(list_expr.release_nonnull()); // Cast List Command
  453. auto next_command = parse_command();
  454. if (!next_command)
  455. return cast;
  456. return create<AST::Join>(move(cast), next_command.release_nonnull()); // Join List Command
  457. }
  458. auto command = parse_command();
  459. if (!command)
  460. return redir;
  461. return create<AST::Join>(redir.release_nonnull(), command.release_nonnull()); // Join Command Command
  462. }
  463. RefPtr<AST::Node> Parser::parse_control_structure()
  464. {
  465. auto rule_start = push_start();
  466. consume_while(is_whitespace);
  467. if (auto control = parse_continuation_control())
  468. return control;
  469. if (auto for_loop = parse_for_loop())
  470. return for_loop;
  471. if (auto loop = parse_loop_loop())
  472. return loop;
  473. if (auto if_expr = parse_if_expr())
  474. return if_expr;
  475. if (auto subshell = parse_subshell())
  476. return subshell;
  477. if (auto match = parse_match_expr())
  478. return match;
  479. return nullptr;
  480. }
  481. RefPtr<AST::Node> Parser::parse_continuation_control()
  482. {
  483. if (!m_continuation_controls_allowed)
  484. return nullptr;
  485. auto rule_start = push_start();
  486. if (expect("break"sv)) {
  487. {
  488. auto break_end = push_start();
  489. if (consume_while(is_any_of(" \t\n;"sv)).is_empty()) {
  490. restore_to(*rule_start);
  491. return nullptr;
  492. }
  493. restore_to(*break_end);
  494. }
  495. return create<AST::ContinuationControl>(AST::ContinuationControl::Break);
  496. }
  497. if (expect("continue"sv)) {
  498. {
  499. auto continue_end = push_start();
  500. if (consume_while(is_any_of(" \t\n;"sv)).is_empty()) {
  501. restore_to(*rule_start);
  502. return nullptr;
  503. }
  504. restore_to(*continue_end);
  505. }
  506. return create<AST::ContinuationControl>(AST::ContinuationControl::Continue);
  507. }
  508. return nullptr;
  509. }
  510. RefPtr<AST::Node> Parser::parse_for_loop()
  511. {
  512. auto rule_start = push_start();
  513. if (!expect("for"sv))
  514. return nullptr;
  515. if (consume_while(is_any_of(" \t\n"sv)).is_empty()) {
  516. restore_to(*rule_start);
  517. return nullptr;
  518. }
  519. Optional<AST::NameWithPosition> index_variable_name, variable_name;
  520. Optional<AST::Position> in_start_position, index_start_position;
  521. auto offset_before_index = current_position();
  522. if (expect("index"sv)) {
  523. auto offset = current_position();
  524. if (!consume_while(is_whitespace).is_empty()) {
  525. auto offset_before_variable = current_position();
  526. auto variable = consume_while(is_word_character);
  527. if (!variable.is_empty()) {
  528. index_start_position = AST::Position { offset_before_index.offset, offset.offset, offset_before_index.line, offset.line };
  529. auto offset_after_variable = current_position();
  530. index_variable_name = AST::NameWithPosition {
  531. String::from_utf8(variable).release_value_but_fixme_should_propagate_errors(),
  532. { offset_before_variable.offset, offset_after_variable.offset, offset_before_variable.line, offset_after_variable.line },
  533. };
  534. consume_while(is_whitespace);
  535. } else {
  536. restore_to(offset_before_index.offset, offset_before_index.line);
  537. }
  538. } else {
  539. restore_to(offset_before_index.offset, offset_before_index.line);
  540. }
  541. }
  542. auto variable_name_start_offset = current_position();
  543. auto name = consume_while(is_word_character);
  544. auto variable_name_end_offset = current_position();
  545. if (!name.is_empty()) {
  546. variable_name = AST::NameWithPosition {
  547. String::from_utf8(name).release_value_but_fixme_should_propagate_errors(),
  548. { variable_name_start_offset.offset, variable_name_end_offset.offset, variable_name_start_offset.line, variable_name_end_offset.line }
  549. };
  550. consume_while(is_whitespace);
  551. auto in_error_start = push_start();
  552. if (!expect("in"sv)) {
  553. auto syntax_error = create<AST::SyntaxError>("Expected 'in' after a variable name in a 'for' loop"_string.release_value_but_fixme_should_propagate_errors(), true);
  554. return create<AST::ForLoop>(move(variable_name), move(index_variable_name), move(syntax_error), nullptr); // ForLoop Var Iterated Block
  555. }
  556. in_start_position = AST::Position { in_error_start->offset, m_offset, in_error_start->line, line() };
  557. }
  558. consume_while(is_whitespace);
  559. RefPtr<AST::Node> iterated_expression;
  560. {
  561. auto iter_error_start = push_start();
  562. iterated_expression = parse_expression();
  563. if (!iterated_expression)
  564. iterated_expression = create<AST::SyntaxError>("Expected an expression in 'for' loop"_string.release_value_but_fixme_should_propagate_errors(), true);
  565. }
  566. consume_while(is_any_of(" \t\n"sv));
  567. {
  568. auto obrace_error_start = push_start();
  569. if (!expect('{')) {
  570. auto syntax_error = create<AST::SyntaxError>("Expected an open brace '{' to start a 'for' loop body"_string.release_value_but_fixme_should_propagate_errors(), true);
  571. 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
  572. }
  573. }
  574. TemporaryChange controls { m_continuation_controls_allowed, true };
  575. auto body = parse_toplevel();
  576. {
  577. auto cbrace_error_start = push_start();
  578. if (!expect('}')) {
  579. auto error_start = push_start();
  580. auto syntax_error = create<AST::SyntaxError>("Expected a close brace '}' to end a 'for' loop body"_string.release_value_but_fixme_should_propagate_errors(), true);
  581. if (body)
  582. body->set_is_syntax_error(*syntax_error);
  583. else
  584. body = syntax_error;
  585. }
  586. }
  587. 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
  588. }
  589. RefPtr<AST::Node> Parser::parse_loop_loop()
  590. {
  591. auto rule_start = push_start();
  592. if (!expect("loop"sv))
  593. return nullptr;
  594. if (consume_while(is_any_of(" \t\n"sv)).is_empty()) {
  595. restore_to(*rule_start);
  596. return nullptr;
  597. }
  598. {
  599. auto obrace_error_start = push_start();
  600. if (!expect('{')) {
  601. auto syntax_error = create<AST::SyntaxError>("Expected an open brace '{' to start a 'loop' loop body"_string.release_value_but_fixme_should_propagate_errors(), true);
  602. return create<AST::ForLoop>(AST::NameWithPosition {}, AST::NameWithPosition {}, nullptr, move(syntax_error)); // ForLoop null null Block
  603. }
  604. }
  605. TemporaryChange controls { m_continuation_controls_allowed, true };
  606. auto body = parse_toplevel();
  607. {
  608. auto cbrace_error_start = push_start();
  609. if (!expect('}')) {
  610. auto error_start = push_start();
  611. auto syntax_error = create<AST::SyntaxError>("Expected a close brace '}' to end a 'loop' loop body"_string.release_value_but_fixme_should_propagate_errors(), true);
  612. if (body)
  613. body->set_is_syntax_error(*syntax_error);
  614. else
  615. body = syntax_error;
  616. }
  617. }
  618. return create<AST::ForLoop>(AST::NameWithPosition {}, AST::NameWithPosition {}, nullptr, move(body)); // ForLoop null null Block
  619. }
  620. RefPtr<AST::Node> Parser::parse_if_expr()
  621. {
  622. auto rule_start = push_start();
  623. if (!expect("if"sv))
  624. return nullptr;
  625. if (consume_while(is_any_of(" \t\n"sv)).is_empty()) {
  626. restore_to(*rule_start);
  627. return nullptr;
  628. }
  629. RefPtr<AST::Node> condition;
  630. {
  631. auto cond_error_start = push_start();
  632. condition = parse_or_logical_sequence();
  633. if (!condition)
  634. condition = create<AST::SyntaxError>("Expected a logical sequence after 'if'"_string.release_value_but_fixme_should_propagate_errors(), true);
  635. }
  636. auto parse_braced_toplevel = [&]() -> RefPtr<AST::Node> {
  637. RefPtr<AST::Node> body;
  638. {
  639. auto obrace_error_start = push_start();
  640. if (!expect('{')) {
  641. body = create<AST::SyntaxError>("Expected an open brace '{' to start an 'if' true branch"_string.release_value_but_fixme_should_propagate_errors(), true);
  642. }
  643. }
  644. if (!body)
  645. body = parse_toplevel();
  646. {
  647. auto cbrace_error_start = push_start();
  648. if (!expect('}')) {
  649. auto error_start = push_start();
  650. RefPtr<AST::SyntaxError> syntax_error = create<AST::SyntaxError>("Expected a close brace '}' to end an 'if' true branch"_string.release_value_but_fixme_should_propagate_errors(), true);
  651. if (body)
  652. body->set_is_syntax_error(*syntax_error);
  653. else
  654. body = syntax_error;
  655. }
  656. }
  657. return body;
  658. };
  659. consume_while(is_any_of(" \t\n"sv));
  660. auto true_branch = parse_braced_toplevel();
  661. auto end_before_else = m_offset;
  662. auto line_before_else = line();
  663. consume_while(is_any_of(" \t\n"sv));
  664. Optional<AST::Position> else_position;
  665. {
  666. auto else_start = push_start();
  667. if (expect("else"sv))
  668. else_position = AST::Position { else_start->offset, m_offset, else_start->line, line() };
  669. else
  670. restore_to(end_before_else, line_before_else);
  671. }
  672. if (else_position.has_value()) {
  673. consume_while(is_any_of(" \t\n"sv));
  674. if (peek() == '{') {
  675. auto false_branch = parse_braced_toplevel();
  676. return create<AST::IfCond>(else_position, condition.release_nonnull(), move(true_branch), move(false_branch)); // If expr true_branch Else false_branch
  677. }
  678. auto else_if_branch = parse_if_expr();
  679. return create<AST::IfCond>(else_position, condition.release_nonnull(), move(true_branch), move(else_if_branch)); // If expr true_branch Else If ...
  680. }
  681. return create<AST::IfCond>(else_position, condition.release_nonnull(), move(true_branch), nullptr); // If expr true_branch
  682. }
  683. RefPtr<AST::Node> Parser::parse_subshell()
  684. {
  685. auto rule_start = push_start();
  686. if (!expect('{'))
  687. return nullptr;
  688. auto body = parse_toplevel();
  689. {
  690. auto cbrace_error_start = push_start();
  691. if (!expect('}')) {
  692. auto error_start = push_start();
  693. RefPtr<AST::SyntaxError> syntax_error = create<AST::SyntaxError>("Expected a close brace '}' to end a subshell"_string.release_value_but_fixme_should_propagate_errors(), true);
  694. if (body)
  695. body->set_is_syntax_error(*syntax_error);
  696. else
  697. body = syntax_error;
  698. }
  699. }
  700. return create<AST::Subshell>(move(body));
  701. }
  702. RefPtr<AST::Node> Parser::parse_match_expr()
  703. {
  704. auto rule_start = push_start();
  705. if (!expect("match"sv))
  706. return nullptr;
  707. if (consume_while(is_whitespace).is_empty()) {
  708. restore_to(*rule_start);
  709. return nullptr;
  710. }
  711. auto match_expression = parse_expression();
  712. if (!match_expression) {
  713. return create<AST::MatchExpr>(
  714. create<AST::SyntaxError>("Expected an expression after 'match'"_string.release_value_but_fixme_should_propagate_errors(), true),
  715. String {}, Optional<AST::Position> {}, Vector<AST::MatchEntry> {});
  716. }
  717. consume_while(is_any_of(" \t\n"sv));
  718. String match_name;
  719. Optional<AST::Position> as_position;
  720. auto as_start = m_offset;
  721. auto as_line = line();
  722. if (expect("as"sv)) {
  723. as_position = AST::Position { as_start, m_offset, as_line, line() };
  724. if (consume_while(is_any_of(" \t\n"sv)).is_empty()) {
  725. auto node = create<AST::MatchExpr>(
  726. match_expression.release_nonnull(),
  727. String {}, move(as_position), Vector<AST::MatchEntry> {});
  728. node->set_is_syntax_error(create<AST::SyntaxError>("Expected whitespace after 'as' in 'match'"_string.release_value_but_fixme_should_propagate_errors(), true));
  729. return node;
  730. }
  731. match_name = String::from_utf8(consume_while(is_word_character)).release_value_but_fixme_should_propagate_errors();
  732. if (match_name.is_empty()) {
  733. auto node = create<AST::MatchExpr>(
  734. match_expression.release_nonnull(),
  735. String {}, move(as_position), Vector<AST::MatchEntry> {});
  736. node->set_is_syntax_error(create<AST::SyntaxError>("Expected an identifier after 'as' in 'match'"_string.release_value_but_fixme_should_propagate_errors(), true));
  737. return node;
  738. }
  739. }
  740. consume_while(is_any_of(" \t\n"sv));
  741. if (!expect('{')) {
  742. auto node = create<AST::MatchExpr>(
  743. match_expression.release_nonnull(),
  744. move(match_name), move(as_position), Vector<AST::MatchEntry> {});
  745. node->set_is_syntax_error(create<AST::SyntaxError>("Expected an open brace '{' to start a 'match' entry list"_string.release_value_but_fixme_should_propagate_errors(), true));
  746. return node;
  747. }
  748. consume_while(is_any_of(" \t\n"sv));
  749. Vector<AST::MatchEntry> entries;
  750. for (;;) {
  751. auto entry = parse_match_entry();
  752. consume_while(is_any_of(" \t\n"sv));
  753. if (entry.options.visit([](auto& x) { return x.is_empty(); }))
  754. break;
  755. entries.append(move(entry));
  756. }
  757. consume_while(is_any_of(" \t\n"sv));
  758. if (!expect('}')) {
  759. auto node = create<AST::MatchExpr>(
  760. match_expression.release_nonnull(),
  761. move(match_name), move(as_position), move(entries));
  762. node->set_is_syntax_error(create<AST::SyntaxError>("Expected a close brace '}' to end a 'match' entry list"_string.release_value_but_fixme_should_propagate_errors(), true));
  763. return node;
  764. }
  765. return create<AST::MatchExpr>(match_expression.release_nonnull(), move(match_name), move(as_position), move(entries));
  766. }
  767. AST::MatchEntry Parser::parse_match_entry()
  768. {
  769. auto rule_start = push_start();
  770. Vector<NonnullRefPtr<AST::Node>> patterns;
  771. Vector<Regex<ECMA262>> regexps;
  772. Vector<AST::Position> pipe_positions;
  773. Optional<Vector<String>> match_names;
  774. Optional<AST::Position> match_as_position;
  775. enum {
  776. Regex,
  777. Glob,
  778. } pattern_kind;
  779. consume_while(is_any_of(" \t\n"sv));
  780. auto regex_pattern = parse_regex_pattern();
  781. if (regex_pattern.has_value()) {
  782. if (auto error = regex_pattern.value().parser_result.error; error != regex::Error::NoError)
  783. return { Vector<NonnullRefPtr<AST::Node>> {}, {}, {}, {}, create<AST::SyntaxError>(String::from_utf8(regex::get_error_string(error)).release_value_but_fixme_should_propagate_errors(), false) };
  784. pattern_kind = Regex;
  785. regexps.append(regex_pattern.release_value());
  786. } else {
  787. auto glob_pattern = parse_match_pattern();
  788. if (!glob_pattern)
  789. return { Vector<NonnullRefPtr<AST::Node>> {}, {}, {}, {}, create<AST::SyntaxError>("Expected a pattern in 'match' body"_string.release_value_but_fixme_should_propagate_errors(), true) };
  790. pattern_kind = Glob;
  791. patterns.append(glob_pattern.release_nonnull());
  792. }
  793. consume_while(is_any_of(" \t\n"sv));
  794. auto previous_pipe_start_position = m_offset;
  795. auto previous_pipe_start_line = line();
  796. RefPtr<AST::SyntaxError> error;
  797. while (expect('|')) {
  798. pipe_positions.append({ previous_pipe_start_position, m_offset, previous_pipe_start_line, line() });
  799. consume_while(is_any_of(" \t\n"sv));
  800. switch (pattern_kind) {
  801. case Regex: {
  802. auto pattern = parse_regex_pattern();
  803. if (!pattern.has_value()) {
  804. error = create<AST::SyntaxError>("Expected a regex pattern to follow '|' in 'match' body"_string.release_value_but_fixme_should_propagate_errors(), true);
  805. break;
  806. }
  807. regexps.append(pattern.release_value());
  808. break;
  809. }
  810. case Glob: {
  811. auto pattern = parse_match_pattern();
  812. if (!pattern) {
  813. error = create<AST::SyntaxError>("Expected a pattern to follow '|' in 'match' body"_string.release_value_but_fixme_should_propagate_errors(), true);
  814. break;
  815. }
  816. patterns.append(pattern.release_nonnull());
  817. break;
  818. }
  819. }
  820. consume_while(is_any_of(" \t\n"sv));
  821. previous_pipe_start_line = line();
  822. previous_pipe_start_position = m_offset;
  823. }
  824. consume_while(is_any_of(" \t\n"sv));
  825. auto as_start_position = m_offset;
  826. auto as_start_line = line();
  827. if (pattern_kind == Glob && expect("as"sv)) {
  828. match_as_position = AST::Position { as_start_position, m_offset, as_start_line, line() };
  829. consume_while(is_any_of(" \t\n"sv));
  830. if (!expect('(')) {
  831. if (!error)
  832. error = create<AST::SyntaxError>("Expected an explicit list of identifiers after a pattern 'as'"_string.release_value_but_fixme_should_propagate_errors());
  833. } else {
  834. match_names = Vector<String>();
  835. for (;;) {
  836. consume_while(is_whitespace);
  837. auto name = consume_while(is_word_character);
  838. if (name.is_empty())
  839. break;
  840. match_names->append(String::from_utf8(name).release_value_but_fixme_should_propagate_errors());
  841. }
  842. if (!expect(')')) {
  843. if (!error)
  844. error = create<AST::SyntaxError>("Expected a close paren ')' to end the identifier list of pattern 'as'"_string.release_value_but_fixme_should_propagate_errors(), true);
  845. }
  846. }
  847. consume_while(is_any_of(" \t\n"sv));
  848. }
  849. if (pattern_kind == Regex) {
  850. Vector<String> names;
  851. for (auto& regex : regexps) {
  852. if (names.is_empty()) {
  853. for (auto& name : regex.parser_result.capture_groups)
  854. names.append(String::from_deprecated_string(name).release_value_but_fixme_should_propagate_errors());
  855. } else {
  856. size_t index = 0;
  857. for (auto& name : regex.parser_result.capture_groups) {
  858. if (names.size() <= index) {
  859. names.append(String::from_deprecated_string(name).release_value_but_fixme_should_propagate_errors());
  860. continue;
  861. }
  862. if (names[index] != name.view()) {
  863. if (!error)
  864. error = create<AST::SyntaxError>("Alternative regex patterns must have the same capture groups"_string.release_value_but_fixme_should_propagate_errors(), false);
  865. break;
  866. }
  867. }
  868. }
  869. }
  870. match_names = move(names);
  871. }
  872. if (!expect('{')) {
  873. if (!error)
  874. error = create<AST::SyntaxError>("Expected an open brace '{' to start a match entry body"_string.release_value_but_fixme_should_propagate_errors(), true);
  875. }
  876. auto body = parse_toplevel();
  877. if (!expect('}')) {
  878. if (!error)
  879. error = create<AST::SyntaxError>("Expected a close brace '}' to end a match entry body"_string.release_value_but_fixme_should_propagate_errors(), true);
  880. }
  881. if (body && error)
  882. body->set_is_syntax_error(*error);
  883. else if (error)
  884. body = error;
  885. if (pattern_kind == Glob)
  886. return { move(patterns), move(match_names), move(match_as_position), move(pipe_positions), move(body) };
  887. return { move(regexps), move(match_names), move(match_as_position), move(pipe_positions), move(body) };
  888. }
  889. RefPtr<AST::Node> Parser::parse_match_pattern()
  890. {
  891. return parse_expression();
  892. }
  893. Optional<Regex<ECMA262>> Parser::parse_regex_pattern()
  894. {
  895. auto rule_start = push_start();
  896. auto start = m_offset;
  897. if (!expect("(?:"sv) && !expect("(?<"sv))
  898. return {};
  899. size_t open_parens = 1;
  900. while (open_parens > 0) {
  901. if (at_end())
  902. break;
  903. if (next_is("("sv))
  904. ++open_parens;
  905. else if (next_is(")"sv))
  906. --open_parens;
  907. consume();
  908. }
  909. if (open_parens != 0) {
  910. restore_to(*rule_start);
  911. return {};
  912. }
  913. auto end = m_offset;
  914. auto pattern = m_input.substring_view(start, end - start);
  915. return Regex<ECMA262>(pattern);
  916. }
  917. RefPtr<AST::Node> Parser::parse_redirection()
  918. {
  919. auto rule_start = push_start();
  920. // heredoc entry
  921. if (next_is("<<-"sv) || next_is("<<~"sv))
  922. return nullptr;
  923. auto pipe_fd = 0;
  924. auto number = consume_while(is_digit);
  925. if (number.is_empty()) {
  926. pipe_fd = -1;
  927. } else {
  928. auto fd = number.to_int();
  929. pipe_fd = fd.value_or(-1);
  930. }
  931. switch (peek()) {
  932. case '>': {
  933. consume();
  934. if (peek() == '>') {
  935. consume();
  936. consume_while(is_whitespace);
  937. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDOUT_FILENO;
  938. auto path = parse_expression();
  939. if (!path) {
  940. if (!at_end()) {
  941. // Eat a character and hope the problem goes away
  942. consume();
  943. }
  944. path = create<AST::SyntaxError>("Expected a path after redirection"_string.release_value_but_fixme_should_propagate_errors(), true);
  945. }
  946. return create<AST::WriteAppendRedirection>(pipe_fd, path.release_nonnull()); // Redirection WriteAppend
  947. }
  948. if (peek() == '&') {
  949. consume();
  950. // FIXME: 'fd>&-' Syntax not the best. needs discussion.
  951. if (peek() == '-') {
  952. consume();
  953. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDOUT_FILENO;
  954. return create<AST::CloseFdRedirection>(pipe_fd); // Redirection CloseFd
  955. }
  956. int dest_pipe_fd = 0;
  957. auto number = consume_while(is_digit);
  958. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDOUT_FILENO;
  959. if (number.is_empty()) {
  960. dest_pipe_fd = -1;
  961. } else {
  962. auto fd = number.to_int();
  963. dest_pipe_fd = fd.value_or(-1);
  964. }
  965. auto redir = create<AST::Fd2FdRedirection>(pipe_fd, dest_pipe_fd); // Redirection Fd2Fd
  966. if (dest_pipe_fd == -1)
  967. redir->set_is_syntax_error(*create<AST::SyntaxError>("Expected a file descriptor"_string.release_value_but_fixme_should_propagate_errors()));
  968. return redir;
  969. }
  970. consume_while(is_whitespace);
  971. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDOUT_FILENO;
  972. auto path = parse_expression();
  973. if (!path) {
  974. if (!at_end()) {
  975. // Eat a character and hope the problem goes away
  976. consume();
  977. }
  978. path = create<AST::SyntaxError>("Expected a path after redirection"_string.release_value_but_fixme_should_propagate_errors(), true);
  979. }
  980. return create<AST::WriteRedirection>(pipe_fd, path.release_nonnull()); // Redirection Write
  981. }
  982. case '<': {
  983. consume();
  984. enum {
  985. Read,
  986. ReadWrite,
  987. } mode { Read };
  988. if (peek() == '>') {
  989. mode = ReadWrite;
  990. consume();
  991. }
  992. consume_while(is_whitespace);
  993. pipe_fd = pipe_fd >= 0 ? pipe_fd : STDIN_FILENO;
  994. auto path = parse_expression();
  995. if (!path) {
  996. if (!at_end()) {
  997. // Eat a character and hope the problem goes away
  998. consume();
  999. }
  1000. path = create<AST::SyntaxError>("Expected a path after redirection"_string.release_value_but_fixme_should_propagate_errors(), true);
  1001. }
  1002. if (mode == Read)
  1003. return create<AST::ReadRedirection>(pipe_fd, path.release_nonnull()); // Redirection Read
  1004. return create<AST::ReadWriteRedirection>(pipe_fd, path.release_nonnull()); // Redirection ReadWrite
  1005. }
  1006. default:
  1007. restore_to(*rule_start);
  1008. return nullptr;
  1009. }
  1010. }
  1011. RefPtr<AST::Node> Parser::parse_list_expression()
  1012. {
  1013. consume_while(is_whitespace);
  1014. auto rule_start = push_start();
  1015. Vector<NonnullRefPtr<AST::Node>> nodes;
  1016. do {
  1017. auto expr = parse_expression();
  1018. if (!expr)
  1019. break;
  1020. nodes.append(expr.release_nonnull());
  1021. } while (!consume_while(is_whitespace).is_empty());
  1022. if (nodes.is_empty())
  1023. return nullptr;
  1024. return create<AST::ListConcatenate>(move(nodes)); // Concatenate List
  1025. }
  1026. RefPtr<AST::Node> Parser::parse_expression()
  1027. {
  1028. auto rule_start = push_start();
  1029. if (m_rule_start_offsets.size() > max_allowed_nested_rule_depth)
  1030. return create<AST::SyntaxError>(String::formatted("Expression nested too deep (max allowed is {})", max_allowed_nested_rule_depth).release_value_but_fixme_should_propagate_errors());
  1031. auto starting_char = peek();
  1032. auto read_concat = [&](auto&& expr) -> NonnullRefPtr<AST::Node> {
  1033. if (is_whitespace(peek()))
  1034. return move(expr);
  1035. if (auto next_expr = parse_expression())
  1036. return create<AST::Juxtaposition>(move(expr), next_expr.release_nonnull());
  1037. return move(expr);
  1038. };
  1039. // Heredocs are expressions, so allow them
  1040. if (!(next_is("<<-"sv) || next_is("<<~"sv))) {
  1041. if (strchr("&|)} ;<>\n", starting_char) != nullptr)
  1042. return nullptr;
  1043. }
  1044. if (m_extra_chars_not_allowed_in_barewords.contains_slow(starting_char))
  1045. return nullptr;
  1046. if (m_is_in_brace_expansion_spec && next_is(".."sv))
  1047. return nullptr;
  1048. if (isdigit(starting_char)) {
  1049. ScopedValueRollback offset_rollback { m_offset };
  1050. auto redir = parse_redirection();
  1051. if (redir)
  1052. return nullptr;
  1053. }
  1054. if (starting_char == '$') {
  1055. if (auto variable = parse_variable())
  1056. return read_concat(variable.release_nonnull());
  1057. if (auto immediate = parse_immediate_expression())
  1058. return read_concat(immediate.release_nonnull());
  1059. auto inline_exec = parse_evaluate();
  1060. if (inline_exec && !inline_exec->is_syntax_error())
  1061. return read_concat(inline_exec.release_nonnull());
  1062. return inline_exec;
  1063. }
  1064. if (starting_char == '#')
  1065. return parse_comment();
  1066. if (starting_char == '(') {
  1067. consume();
  1068. auto list = parse_list_expression();
  1069. if (!expect(')')) {
  1070. restore_to(*rule_start);
  1071. return nullptr;
  1072. }
  1073. return read_concat(create<AST::CastToList>(move(list))); // Cast To List
  1074. }
  1075. if (starting_char == '!' && m_in_interactive_mode) {
  1076. if (auto designator = parse_history_designator())
  1077. return designator;
  1078. }
  1079. if (auto composite = parse_string_composite())
  1080. return read_concat(composite.release_nonnull());
  1081. return nullptr;
  1082. }
  1083. RefPtr<AST::Node> Parser::parse_string_composite()
  1084. {
  1085. auto rule_start = push_start();
  1086. if (auto string = parse_string()) {
  1087. if (auto next_part = parse_string_composite())
  1088. return create<AST::Juxtaposition>(string.release_nonnull(), next_part.release_nonnull()); // Concatenate String StringComposite
  1089. return string;
  1090. }
  1091. if (auto variable = parse_variable()) {
  1092. if (auto next_part = parse_string_composite())
  1093. return create<AST::Juxtaposition>(variable.release_nonnull(), next_part.release_nonnull()); // Concatenate Variable StringComposite
  1094. return variable;
  1095. }
  1096. if (auto glob = parse_glob()) {
  1097. if (auto next_part = parse_string_composite())
  1098. return create<AST::Juxtaposition>(glob.release_nonnull(), next_part.release_nonnull()); // Concatenate Glob StringComposite
  1099. return glob;
  1100. }
  1101. if (auto expansion = parse_brace_expansion()) {
  1102. if (auto next_part = parse_string_composite())
  1103. return create<AST::Juxtaposition>(expansion.release_nonnull(), next_part.release_nonnull()); // Concatenate BraceExpansion StringComposite
  1104. return expansion;
  1105. }
  1106. if (auto bareword = parse_bareword()) {
  1107. if (auto next_part = parse_string_composite())
  1108. return create<AST::Juxtaposition>(bareword.release_nonnull(), next_part.release_nonnull()); // Concatenate Bareword StringComposite
  1109. return bareword;
  1110. }
  1111. if (auto inline_command = parse_evaluate()) {
  1112. if (auto next_part = parse_string_composite())
  1113. return create<AST::Juxtaposition>(inline_command.release_nonnull(), next_part.release_nonnull()); // Concatenate Execute StringComposite
  1114. return inline_command;
  1115. }
  1116. if (auto heredoc = parse_heredoc_initiation_record()) {
  1117. if (auto next_part = parse_string_composite())
  1118. return create<AST::Juxtaposition>(heredoc.release_nonnull(), next_part.release_nonnull()); // Concatenate Heredoc StringComposite
  1119. return heredoc;
  1120. }
  1121. return nullptr;
  1122. }
  1123. RefPtr<AST::Node> Parser::parse_string()
  1124. {
  1125. auto rule_start = push_start();
  1126. if (at_end())
  1127. return nullptr;
  1128. if (peek() == '"') {
  1129. consume();
  1130. auto inner = parse_string_inner(StringEndCondition::DoubleQuote);
  1131. if (!inner)
  1132. inner = create<AST::SyntaxError>("Unexpected EOF in string"_string.release_value_but_fixme_should_propagate_errors(), true);
  1133. if (!expect('"')) {
  1134. inner = create<AST::DoubleQuotedString>(move(inner));
  1135. inner->set_is_syntax_error(*create<AST::SyntaxError>("Expected a terminating double quote"_string.release_value_but_fixme_should_propagate_errors(), true));
  1136. return inner;
  1137. }
  1138. return create<AST::DoubleQuotedString>(move(inner)); // Double Quoted String
  1139. }
  1140. if (peek() == '\'') {
  1141. consume();
  1142. auto text = consume_while(is_not('\''));
  1143. bool is_error = false;
  1144. if (!expect('\''))
  1145. is_error = true;
  1146. auto result = create<AST::StringLiteral>(String::from_utf8(text).release_value_but_fixme_should_propagate_errors(), AST::StringLiteral::EnclosureType::SingleQuotes); // String Literal
  1147. if (is_error)
  1148. result->set_is_syntax_error(*create<AST::SyntaxError>("Expected a terminating single quote"_string.release_value_but_fixme_should_propagate_errors(), true));
  1149. return result;
  1150. }
  1151. return nullptr;
  1152. }
  1153. RefPtr<AST::Node> Parser::parse_string_inner(StringEndCondition condition)
  1154. {
  1155. auto rule_start = push_start();
  1156. if (at_end())
  1157. return nullptr;
  1158. StringBuilder builder;
  1159. while (!at_end()) {
  1160. if (condition == StringEndCondition::DoubleQuote && peek() == '"') {
  1161. break;
  1162. }
  1163. if (peek() == '\\') {
  1164. consume();
  1165. if (at_end()) {
  1166. break;
  1167. }
  1168. auto ch = consume();
  1169. switch (ch) {
  1170. case '\\':
  1171. default:
  1172. builder.append(ch);
  1173. break;
  1174. case 'x': {
  1175. if (m_input.length() <= m_offset + 2)
  1176. break;
  1177. auto first_nibble = tolower(consume());
  1178. auto second_nibble = tolower(consume());
  1179. if (!isxdigit(first_nibble) || !isxdigit(second_nibble)) {
  1180. builder.append(first_nibble);
  1181. builder.append(second_nibble);
  1182. break;
  1183. }
  1184. builder.append(to_byte(first_nibble, second_nibble));
  1185. break;
  1186. }
  1187. case 'u': {
  1188. if (m_input.length() <= m_offset + 8)
  1189. break;
  1190. size_t counter = 8;
  1191. auto chars = consume_while([&](auto) { return counter-- > 0; });
  1192. if (auto number = AK::StringUtils::convert_to_uint_from_hex(chars); number.has_value())
  1193. builder.append(Utf32View { &number.value(), 1 });
  1194. else
  1195. builder.append(chars);
  1196. break;
  1197. }
  1198. case 'a':
  1199. builder.append('\a');
  1200. break;
  1201. case 'b':
  1202. builder.append('\b');
  1203. break;
  1204. case 'e':
  1205. builder.append('\x1b');
  1206. break;
  1207. case 'f':
  1208. builder.append('\f');
  1209. break;
  1210. case 'r':
  1211. builder.append('\r');
  1212. break;
  1213. case 'n':
  1214. builder.append('\n');
  1215. break;
  1216. case 't':
  1217. builder.append('\t');
  1218. break;
  1219. }
  1220. continue;
  1221. }
  1222. if (peek() == '$') {
  1223. auto string_literal = create<AST::StringLiteral>(builder.to_string().release_value_but_fixme_should_propagate_errors(), AST::StringLiteral::EnclosureType::DoubleQuotes); // String Literal
  1224. auto read_concat = [&](auto&& node) {
  1225. auto inner = create<AST::StringPartCompose>(
  1226. move(string_literal),
  1227. move(node)); // Compose String Node
  1228. if (auto string = parse_string_inner(condition)) {
  1229. return create<AST::StringPartCompose>(move(inner), string.release_nonnull()); // Compose Composition Composition
  1230. }
  1231. return inner;
  1232. };
  1233. if (auto variable = parse_variable())
  1234. return read_concat(variable.release_nonnull());
  1235. if (auto immediate = parse_immediate_expression())
  1236. return read_concat(immediate.release_nonnull());
  1237. if (auto evaluate = parse_evaluate())
  1238. return read_concat(evaluate.release_nonnull());
  1239. }
  1240. builder.append(consume());
  1241. }
  1242. return create<AST::StringLiteral>(builder.to_string().release_value_but_fixme_should_propagate_errors(), AST::StringLiteral::EnclosureType::DoubleQuotes); // String Literal
  1243. }
  1244. RefPtr<AST::Node> Parser::parse_variable()
  1245. {
  1246. auto rule_start = push_start();
  1247. auto ref = parse_variable_ref();
  1248. if (!ref)
  1249. return nullptr;
  1250. auto variable = static_ptr_cast<AST::VariableNode>(ref);
  1251. if (auto slice = parse_slice())
  1252. variable->set_slice(slice.release_nonnull());
  1253. return variable;
  1254. }
  1255. RefPtr<AST::Node> Parser::parse_variable_ref()
  1256. {
  1257. auto rule_start = push_start();
  1258. if (at_end())
  1259. return nullptr;
  1260. if (peek() != '$')
  1261. return nullptr;
  1262. consume();
  1263. switch (peek()) {
  1264. case '$':
  1265. case '?':
  1266. case '*':
  1267. case '#':
  1268. return create<AST::SpecialVariable>(consume()); // Variable Special
  1269. default:
  1270. break;
  1271. }
  1272. auto name = consume_while(is_word_character);
  1273. if (name.length() == 0) {
  1274. restore_to(rule_start->offset, rule_start->line);
  1275. return nullptr;
  1276. }
  1277. return create<AST::SimpleVariable>(String::from_utf8(name).release_value_but_fixme_should_propagate_errors()); // Variable Simple
  1278. }
  1279. RefPtr<AST::Slice> Parser::parse_slice()
  1280. {
  1281. auto rule_start = push_start();
  1282. if (!next_is("["sv))
  1283. return nullptr;
  1284. consume(); // [
  1285. ScopedValueRollback chars_change { m_extra_chars_not_allowed_in_barewords };
  1286. m_extra_chars_not_allowed_in_barewords.append(']');
  1287. auto spec = parse_brace_expansion_spec();
  1288. RefPtr<AST::SyntaxError> error;
  1289. if (peek() != ']')
  1290. error = create<AST::SyntaxError>("Expected a close bracket ']' to end a variable slice"_string.release_value_but_fixme_should_propagate_errors());
  1291. else
  1292. consume();
  1293. if (!spec) {
  1294. if (error)
  1295. spec = move(error);
  1296. else
  1297. spec = create<AST::SyntaxError>("Expected either a range, or a comma-seprated list of selectors"_string.release_value_but_fixme_should_propagate_errors());
  1298. }
  1299. auto node = create<AST::Slice>(spec.release_nonnull());
  1300. if (error)
  1301. node->set_is_syntax_error(*error);
  1302. return node;
  1303. }
  1304. RefPtr<AST::Node> Parser::parse_evaluate()
  1305. {
  1306. auto rule_start = push_start();
  1307. if (at_end())
  1308. return nullptr;
  1309. if (peek() != '$')
  1310. return nullptr;
  1311. consume();
  1312. if (peek() == '(') {
  1313. consume();
  1314. auto inner = parse_pipe_sequence();
  1315. if (!inner)
  1316. inner = create<AST::SyntaxError>("Unexpected EOF in list"_string.release_value_but_fixme_should_propagate_errors(), true);
  1317. if (!expect(')'))
  1318. inner->set_is_syntax_error(*create<AST::SyntaxError>("Expected a terminating close paren"_string.release_value_but_fixme_should_propagate_errors(), true));
  1319. return create<AST::Execute>(inner.release_nonnull(), true);
  1320. }
  1321. auto inner = parse_expression();
  1322. if (!inner) {
  1323. inner = create<AST::SyntaxError>("Expected a command"_string.release_value_but_fixme_should_propagate_errors(), true);
  1324. } else {
  1325. if (inner->is_list()) {
  1326. auto execute_inner = create<AST::Execute>(inner.release_nonnull(), true);
  1327. inner = move(execute_inner);
  1328. } else {
  1329. auto dyn_inner = create<AST::DynamicEvaluate>(inner.release_nonnull());
  1330. inner = move(dyn_inner);
  1331. }
  1332. }
  1333. return inner;
  1334. }
  1335. RefPtr<AST::Node> Parser::parse_immediate_expression()
  1336. {
  1337. auto rule_start = push_start();
  1338. if (at_end())
  1339. return nullptr;
  1340. if (peek() != '$')
  1341. return nullptr;
  1342. consume();
  1343. if (peek() != '{') {
  1344. restore_to(*rule_start);
  1345. return nullptr;
  1346. }
  1347. consume();
  1348. consume_while(is_whitespace);
  1349. auto function_name_start_offset = current_position();
  1350. auto function_name = consume_while(is_word_character);
  1351. auto function_name_end_offset = current_position();
  1352. AST::Position function_position {
  1353. function_name_start_offset.offset,
  1354. function_name_end_offset.offset,
  1355. function_name_start_offset.line,
  1356. function_name_end_offset.line,
  1357. };
  1358. consume_while(is_whitespace);
  1359. Vector<NonnullRefPtr<AST::Node>> arguments;
  1360. do {
  1361. auto expr = parse_expression();
  1362. if (!expr)
  1363. break;
  1364. arguments.append(expr.release_nonnull());
  1365. } while (!consume_while(is_whitespace).is_empty());
  1366. auto ending_brace_start_offset = current_position();
  1367. if (peek() == '}')
  1368. consume();
  1369. auto ending_brace_end_offset = current_position();
  1370. auto ending_brace_position = ending_brace_start_offset.offset == ending_brace_end_offset.offset
  1371. ? Optional<AST::Position> {}
  1372. : Optional<AST::Position> {
  1373. AST::Position {
  1374. ending_brace_start_offset.offset,
  1375. ending_brace_end_offset.offset,
  1376. ending_brace_start_offset.line,
  1377. ending_brace_end_offset.line,
  1378. }
  1379. };
  1380. auto node = create<AST::ImmediateExpression>(
  1381. AST::NameWithPosition { String::from_utf8(function_name).release_value_but_fixme_should_propagate_errors(), move(function_position) },
  1382. move(arguments),
  1383. ending_brace_position);
  1384. if (!ending_brace_position.has_value())
  1385. node->set_is_syntax_error(create<AST::SyntaxError>("Expected a closing brace '}' to end an immediate expression"_string.release_value_but_fixme_should_propagate_errors(), true));
  1386. else if (node->function_name().is_empty())
  1387. node->set_is_syntax_error(create<AST::SyntaxError>("Expected an immediate function name"_string.release_value_but_fixme_should_propagate_errors()));
  1388. return node;
  1389. }
  1390. RefPtr<AST::Node> Parser::parse_history_designator()
  1391. {
  1392. auto rule_start = push_start();
  1393. VERIFY(peek() == '!');
  1394. consume();
  1395. // Event selector
  1396. AST::HistorySelector selector;
  1397. RefPtr<AST::SyntaxError> syntax_error;
  1398. selector.event.kind = AST::HistorySelector::EventKind::StartingStringLookup;
  1399. selector.event.text_position = { m_offset, m_offset, m_line, m_line };
  1400. selector.word_selector_range = {
  1401. AST::HistorySelector::WordSelector {
  1402. AST::HistorySelector::WordSelectorKind::Index,
  1403. 0,
  1404. { m_offset, m_offset, m_line, m_line },
  1405. nullptr },
  1406. AST::HistorySelector::WordSelector {
  1407. AST::HistorySelector::WordSelectorKind::Last,
  1408. 0,
  1409. { m_offset, m_offset, m_line, m_line },
  1410. nullptr }
  1411. };
  1412. bool is_word_selector = false;
  1413. switch (peek()) {
  1414. case ':':
  1415. consume();
  1416. [[fallthrough]];
  1417. case '^':
  1418. case '$':
  1419. case '*':
  1420. is_word_selector = true;
  1421. break;
  1422. case '!':
  1423. consume();
  1424. selector.event.kind = AST::HistorySelector::EventKind::IndexFromEnd;
  1425. selector.event.index = 0;
  1426. selector.event.text = "!"_short_string;
  1427. break;
  1428. case '?':
  1429. consume();
  1430. selector.event.kind = AST::HistorySelector::EventKind::ContainingStringLookup;
  1431. [[fallthrough]];
  1432. default: {
  1433. TemporaryChange chars_change { m_extra_chars_not_allowed_in_barewords, { ':', '^', '$', '*' } };
  1434. auto bareword = parse_bareword();
  1435. if (!bareword || !bareword->is_bareword()) {
  1436. restore_to(*rule_start);
  1437. return nullptr;
  1438. }
  1439. selector.event.text = static_ptr_cast<AST::BarewordLiteral>(bareword)->text();
  1440. selector.event.text_position = bareword->position();
  1441. auto selector_bytes = selector.event.text.bytes();
  1442. auto it = selector_bytes.begin();
  1443. bool is_negative = false;
  1444. if (*it == '-') {
  1445. ++it;
  1446. is_negative = true;
  1447. }
  1448. if (it != selector_bytes.end() && all_of(it, selector_bytes.end(), is_digit)) {
  1449. if (is_negative)
  1450. selector.event.kind = AST::HistorySelector::EventKind::IndexFromEnd;
  1451. else
  1452. selector.event.kind = AST::HistorySelector::EventKind::IndexFromStart;
  1453. auto number = abs(selector.event.text.bytes_as_string_view().to_int().value_or(0));
  1454. if (number != 0)
  1455. selector.event.index = number - 1;
  1456. else
  1457. syntax_error = create<AST::SyntaxError>("History entry index value invalid or out of range"_string.release_value_but_fixme_should_propagate_errors());
  1458. }
  1459. if (":^$*"sv.contains(peek())) {
  1460. is_word_selector = true;
  1461. if (peek() == ':')
  1462. consume();
  1463. }
  1464. }
  1465. }
  1466. if (!is_word_selector) {
  1467. auto node = create<AST::HistoryEvent>(move(selector));
  1468. if (syntax_error)
  1469. node->set_is_syntax_error(*syntax_error);
  1470. return node;
  1471. }
  1472. // Word selectors
  1473. auto parse_word_selector = [&]() -> Optional<AST::HistorySelector::WordSelector> {
  1474. auto c = peek();
  1475. AST::HistorySelector::WordSelectorKind word_selector_kind;
  1476. ssize_t offset = -1;
  1477. if (isdigit(c)) {
  1478. auto num = consume_while(is_digit);
  1479. auto value = num.to_uint();
  1480. if (!value.has_value())
  1481. return {};
  1482. word_selector_kind = AST::HistorySelector::WordSelectorKind::Index;
  1483. offset = value.value();
  1484. } else if (c == '^') {
  1485. consume();
  1486. word_selector_kind = AST::HistorySelector::WordSelectorKind::Index;
  1487. offset = 1;
  1488. } else if (c == '$') {
  1489. consume();
  1490. word_selector_kind = AST::HistorySelector::WordSelectorKind::Last;
  1491. offset = 0;
  1492. }
  1493. if (offset == -1)
  1494. return {};
  1495. return AST::HistorySelector::WordSelector {
  1496. word_selector_kind,
  1497. static_cast<size_t>(offset),
  1498. { m_rule_start_offsets.last(), m_offset, m_rule_start_lines.last(), line() },
  1499. syntax_error
  1500. };
  1501. };
  1502. auto make_word_selector = [&](AST::HistorySelector::WordSelectorKind word_selector_kind, size_t offset) {
  1503. return AST::HistorySelector::WordSelector {
  1504. word_selector_kind,
  1505. offset,
  1506. { m_rule_start_offsets.last(), m_offset, m_rule_start_lines.last(), line() },
  1507. syntax_error
  1508. };
  1509. };
  1510. auto first_char = peek();
  1511. if (!(is_digit(first_char) || "^$-*"sv.contains(first_char))) {
  1512. if (!syntax_error)
  1513. syntax_error = create<AST::SyntaxError>("Expected a word selector after ':' in a history event designator"_string.release_value_but_fixme_should_propagate_errors(), true);
  1514. } else if (first_char == '*') {
  1515. consume();
  1516. selector.word_selector_range.start = make_word_selector(AST::HistorySelector::WordSelectorKind::Index, 1);
  1517. selector.word_selector_range.end = make_word_selector(AST::HistorySelector::WordSelectorKind::Last, 0);
  1518. } else if (first_char == '-') {
  1519. consume();
  1520. selector.word_selector_range.start = make_word_selector(AST::HistorySelector::WordSelectorKind::Index, 0);
  1521. auto last_selector = parse_word_selector();
  1522. if (!last_selector.has_value())
  1523. selector.word_selector_range.end = make_word_selector(AST::HistorySelector::WordSelectorKind::Last, 1);
  1524. else
  1525. selector.word_selector_range.end = last_selector.release_value();
  1526. } else {
  1527. auto first_selector = parse_word_selector();
  1528. // peek() should be a digit, ^, or $ here, so this should always have value.
  1529. VERIFY(first_selector.has_value());
  1530. selector.word_selector_range.start = first_selector.release_value();
  1531. if (peek() == '-') {
  1532. consume();
  1533. auto last_selector = parse_word_selector();
  1534. if (last_selector.has_value()) {
  1535. selector.word_selector_range.end = last_selector.release_value();
  1536. } else {
  1537. selector.word_selector_range.end = make_word_selector(AST::HistorySelector::WordSelectorKind::Last, 1);
  1538. }
  1539. } else if (peek() == '*') {
  1540. consume();
  1541. selector.word_selector_range.end = make_word_selector(AST::HistorySelector::WordSelectorKind::Last, 0);
  1542. } else {
  1543. selector.word_selector_range.end.clear();
  1544. }
  1545. }
  1546. auto node = create<AST::HistoryEvent>(move(selector));
  1547. if (syntax_error)
  1548. node->set_is_syntax_error(*syntax_error);
  1549. return node;
  1550. }
  1551. RefPtr<AST::Node> Parser::parse_comment()
  1552. {
  1553. if (at_end())
  1554. return nullptr;
  1555. if (peek() != '#')
  1556. return nullptr;
  1557. consume();
  1558. auto text = consume_while(is_not('\n'));
  1559. return create<AST::Comment>(String::from_utf8(text).release_value_but_fixme_should_propagate_errors()); // Comment
  1560. }
  1561. RefPtr<AST::Node> Parser::parse_bareword()
  1562. {
  1563. auto rule_start = push_start();
  1564. StringBuilder builder;
  1565. auto is_acceptable_bareword_character = [&](char c) {
  1566. return strchr("\\\"'*$&#|(){} ?;<>\n", c) == nullptr
  1567. && !m_extra_chars_not_allowed_in_barewords.contains_slow(c);
  1568. };
  1569. while (!at_end()) {
  1570. char ch = peek();
  1571. if (ch == '\\') {
  1572. consume();
  1573. if (!at_end()) {
  1574. ch = consume();
  1575. if (is_acceptable_bareword_character(ch))
  1576. builder.append('\\');
  1577. }
  1578. builder.append(ch);
  1579. continue;
  1580. }
  1581. if (m_is_in_brace_expansion_spec && next_is(".."sv)) {
  1582. // Don't eat '..' in a brace expansion spec.
  1583. break;
  1584. }
  1585. if (is_acceptable_bareword_character(ch)) {
  1586. builder.append(consume());
  1587. continue;
  1588. }
  1589. break;
  1590. }
  1591. if (builder.is_empty())
  1592. return nullptr;
  1593. auto current_end = m_offset;
  1594. auto current_line = line();
  1595. auto string = builder.to_string().release_value_but_fixme_should_propagate_errors();
  1596. if (string.starts_with('~')) {
  1597. String username;
  1598. RefPtr<AST::Node> tilde, text;
  1599. auto first_slash_index = string.find_byte_offset('/');
  1600. if (first_slash_index.has_value()) {
  1601. username = string.substring_from_byte_offset(1, *first_slash_index - 1).release_value_but_fixme_should_propagate_errors();
  1602. string = string.substring_from_byte_offset(*first_slash_index).release_value_but_fixme_should_propagate_errors();
  1603. } else {
  1604. username = string.substring_from_byte_offset(1).release_value_but_fixme_should_propagate_errors();
  1605. string = {};
  1606. }
  1607. // Synthesize a Tilde Node with the correct positioning information.
  1608. {
  1609. restore_to(rule_start->offset, rule_start->line);
  1610. auto ch = consume();
  1611. VERIFY(ch == '~');
  1612. auto username_length = username.bytes_as_string_view().length();
  1613. tilde = create<AST::Tilde>(move(username));
  1614. // Consume the username (if any)
  1615. for (size_t i = 0; i < username_length; ++i)
  1616. consume();
  1617. }
  1618. if (string.is_empty())
  1619. return tilde;
  1620. // Synthesize a BarewordLiteral Node with the correct positioning information.
  1621. {
  1622. auto text_start = push_start();
  1623. restore_to(current_end, current_line);
  1624. text = create<AST::BarewordLiteral>(move(string));
  1625. }
  1626. return create<AST::Juxtaposition>(tilde.release_nonnull(), text.release_nonnull()); // Juxtaposition Variable Bareword
  1627. }
  1628. if (string.starts_with_bytes("\\~"sv)) {
  1629. // Un-escape the tilde, but only at the start (where it would be an expansion)
  1630. string = string.substring_from_byte_offset(1).release_value_but_fixme_should_propagate_errors();
  1631. }
  1632. return create<AST::BarewordLiteral>(move(string)); // Bareword Literal
  1633. }
  1634. RefPtr<AST::Node> Parser::parse_glob()
  1635. {
  1636. auto rule_start = push_start();
  1637. auto bareword_part = parse_bareword();
  1638. if (at_end())
  1639. return bareword_part;
  1640. char ch = peek();
  1641. if (ch == '*' || ch == '?') {
  1642. auto saved_offset = save_offset();
  1643. consume();
  1644. StringBuilder textbuilder;
  1645. if (bareword_part) {
  1646. StringView text;
  1647. if (bareword_part->is_bareword()) {
  1648. auto bareword = static_cast<AST::BarewordLiteral*>(bareword_part.ptr());
  1649. text = bareword->text();
  1650. } else {
  1651. // FIXME: Allow composition of tilde+bareword with globs: '~/foo/bar/baz*'
  1652. restore_to(saved_offset.offset, saved_offset.line);
  1653. bareword_part->set_is_syntax_error(*create<AST::SyntaxError>(String::formatted("Unexpected {} inside a glob", bareword_part->class_name()).release_value_but_fixme_should_propagate_errors()));
  1654. return bareword_part;
  1655. }
  1656. textbuilder.append(text);
  1657. }
  1658. textbuilder.append(ch);
  1659. auto glob_after = parse_glob();
  1660. if (glob_after) {
  1661. if (glob_after->is_glob()) {
  1662. auto glob = static_cast<AST::Glob*>(glob_after.ptr());
  1663. textbuilder.append(glob->text());
  1664. } else if (glob_after->is_bareword()) {
  1665. auto bareword = static_cast<AST::BarewordLiteral*>(glob_after.ptr());
  1666. textbuilder.append(bareword->text());
  1667. } else if (glob_after->is_tilde()) {
  1668. auto bareword = static_cast<AST::Tilde*>(glob_after.ptr());
  1669. textbuilder.append('~');
  1670. textbuilder.append(bareword->text());
  1671. } else {
  1672. return create<AST::SyntaxError>(String::formatted("Invalid node '{}' in glob position, escape shell special characters", glob_after->class_name()).release_value_but_fixme_should_propagate_errors());
  1673. }
  1674. }
  1675. return create<AST::Glob>(textbuilder.to_string().release_value_but_fixme_should_propagate_errors()); // Glob
  1676. }
  1677. return bareword_part;
  1678. }
  1679. RefPtr<AST::Node> Parser::parse_brace_expansion()
  1680. {
  1681. auto rule_start = push_start();
  1682. if (!expect('{'))
  1683. return nullptr;
  1684. if (auto spec = parse_brace_expansion_spec()) {
  1685. if (!expect('}'))
  1686. spec->set_is_syntax_error(create<AST::SyntaxError>("Expected a close brace '}' to end a brace expansion"_string.release_value_but_fixme_should_propagate_errors(), true));
  1687. return spec;
  1688. }
  1689. restore_to(*rule_start);
  1690. return nullptr;
  1691. }
  1692. RefPtr<AST::Node> Parser::parse_brace_expansion_spec()
  1693. {
  1694. TemporaryChange is_in_brace_expansion { m_is_in_brace_expansion_spec, true };
  1695. ScopedValueRollback chars_change { m_extra_chars_not_allowed_in_barewords };
  1696. m_extra_chars_not_allowed_in_barewords.append(',');
  1697. auto rule_start = push_start();
  1698. Vector<NonnullRefPtr<AST::Node>> subexpressions;
  1699. if (next_is(","sv)) {
  1700. // Note that we don't consume the ',' here.
  1701. subexpressions.append(create<AST::StringLiteral>(String {}, AST::StringLiteral::EnclosureType::None));
  1702. } else {
  1703. auto start_expr = parse_expression();
  1704. if (start_expr) {
  1705. if (expect(".."sv)) {
  1706. if (auto end_expr = parse_expression()) {
  1707. if (end_expr->position().start_offset != start_expr->position().end_offset + 2)
  1708. end_expr->set_is_syntax_error(create<AST::SyntaxError>("Expected no whitespace between '..' and the following expression in brace expansion"_string.release_value_but_fixme_should_propagate_errors()));
  1709. return create<AST::Range>(start_expr.release_nonnull(), end_expr.release_nonnull());
  1710. }
  1711. return create<AST::Range>(start_expr.release_nonnull(), create<AST::SyntaxError>("Expected an expression to end range brace expansion with"_string.release_value_but_fixme_should_propagate_errors(), true));
  1712. }
  1713. }
  1714. if (start_expr)
  1715. subexpressions.append(start_expr.release_nonnull());
  1716. }
  1717. while (expect(',')) {
  1718. auto expr = parse_expression();
  1719. if (expr) {
  1720. subexpressions.append(expr.release_nonnull());
  1721. } else {
  1722. subexpressions.append(create<AST::StringLiteral>(String {}, AST::StringLiteral::EnclosureType::None));
  1723. }
  1724. }
  1725. if (subexpressions.is_empty())
  1726. return nullptr;
  1727. return create<AST::BraceExpansion>(move(subexpressions));
  1728. }
  1729. RefPtr<AST::Node> Parser::parse_heredoc_initiation_record()
  1730. {
  1731. if (!next_is("<<"sv))
  1732. return nullptr;
  1733. auto rule_start = push_start();
  1734. // '<' '<'
  1735. consume();
  1736. consume();
  1737. HeredocInitiationRecord record;
  1738. record.end = "<error>"_string.release_value_but_fixme_should_propagate_errors();
  1739. RefPtr<AST::SyntaxError> syntax_error_node;
  1740. // '-' | '~'
  1741. switch (peek()) {
  1742. case '-':
  1743. record.deindent = false;
  1744. consume();
  1745. break;
  1746. case '~':
  1747. record.deindent = true;
  1748. consume();
  1749. break;
  1750. default:
  1751. restore_to(*rule_start);
  1752. return nullptr;
  1753. }
  1754. // StringLiteral | bareword
  1755. if (auto bareword = parse_bareword()) {
  1756. if (!bareword->is_bareword()) {
  1757. syntax_error_node = create<AST::SyntaxError>(String::formatted("Expected a bareword or a quoted string, not {}", bareword->class_name()).release_value_but_fixme_should_propagate_errors());
  1758. } else {
  1759. if (bareword->is_syntax_error())
  1760. syntax_error_node = bareword->syntax_error_node();
  1761. else
  1762. record.end = static_cast<AST::BarewordLiteral*>(bareword.ptr())->text();
  1763. }
  1764. record.interpolate = true;
  1765. } else if (peek() == '\'') {
  1766. consume();
  1767. auto text = consume_while(is_not('\''));
  1768. bool is_error = false;
  1769. if (!expect('\''))
  1770. is_error = true;
  1771. if (is_error)
  1772. syntax_error_node = create<AST::SyntaxError>("Expected a terminating single quote"_string.release_value_but_fixme_should_propagate_errors(), true);
  1773. record.end = String::from_utf8(text).release_value_but_fixme_should_propagate_errors();
  1774. record.interpolate = false;
  1775. } else {
  1776. syntax_error_node = create<AST::SyntaxError>("Expected a bareword or a single-quoted string literal for heredoc end key"_string.release_value_but_fixme_should_propagate_errors(), true);
  1777. }
  1778. auto node = create<AST::Heredoc>(record.end, record.interpolate, record.deindent);
  1779. if (syntax_error_node)
  1780. node->set_is_syntax_error(*syntax_error_node);
  1781. else
  1782. node->set_is_syntax_error(*create<AST::SyntaxError>(String::formatted("Expected heredoc contents for heredoc with end key '{}'", node->end()).release_value_but_fixme_should_propagate_errors(), true));
  1783. record.node = node;
  1784. m_heredoc_initiations.append(move(record));
  1785. return node;
  1786. }
  1787. bool Parser::parse_heredoc_entries()
  1788. {
  1789. auto heredocs = move(m_heredoc_initiations);
  1790. m_heredoc_initiations.clear();
  1791. // Try to parse heredoc entries, as reverse recorded in the initiation records
  1792. for (auto& record : heredocs) {
  1793. auto rule_start = push_start();
  1794. if (m_rule_start_offsets.size() > max_allowed_nested_rule_depth) {
  1795. record.node->set_is_syntax_error(*create<AST::SyntaxError>(String::formatted("Expression nested too deep (max allowed is {})", max_allowed_nested_rule_depth).release_value_but_fixme_should_propagate_errors()));
  1796. continue;
  1797. }
  1798. bool found_key = false;
  1799. if (!record.interpolate) {
  1800. // Since no interpolation is allowed, just read lines until we hit the key
  1801. Optional<Offset> last_line_offset;
  1802. for (;;) {
  1803. if (at_end())
  1804. break;
  1805. if (peek() == '\n')
  1806. consume();
  1807. last_line_offset = current_position();
  1808. auto line = consume_while(is_not('\n'));
  1809. if (peek() == '\n')
  1810. consume();
  1811. if (line.trim_whitespace() == record.end) {
  1812. found_key = true;
  1813. break;
  1814. }
  1815. }
  1816. if (!last_line_offset.has_value())
  1817. last_line_offset = current_position();
  1818. // Now just wrap it in a StringLiteral and set it as the node's contents
  1819. auto node = create<AST::StringLiteral>(
  1820. String::from_utf8(m_input.substring_view(rule_start->offset, last_line_offset->offset - rule_start->offset)).release_value_but_fixme_should_propagate_errors(),
  1821. AST::StringLiteral::EnclosureType::None);
  1822. if (!found_key)
  1823. node->set_is_syntax_error(*create<AST::SyntaxError>(String::formatted("Expected to find the heredoc key '{}', but found Eof", record.end).release_value_but_fixme_should_propagate_errors(), true));
  1824. record.node->set_contents(move(node));
  1825. } else {
  1826. // Interpolation is allowed, so we're going to read doublequoted string innards
  1827. // until we find a line that contains the key
  1828. auto end_condition = move(m_end_condition);
  1829. found_key = false;
  1830. set_end_condition(make<Function<bool()>>([this, end = record.end, &found_key] {
  1831. if (found_key)
  1832. return true;
  1833. auto offset = current_position();
  1834. auto cond = move(m_end_condition);
  1835. ScopeGuard guard {
  1836. [&] {
  1837. m_end_condition = move(cond);
  1838. }
  1839. };
  1840. if (peek() == '\n') {
  1841. consume();
  1842. auto line = consume_while(is_not('\n'));
  1843. if (peek() == '\n')
  1844. consume();
  1845. if (line.trim_whitespace() == end) {
  1846. restore_to(offset.offset, offset.line);
  1847. found_key = true;
  1848. return true;
  1849. }
  1850. }
  1851. restore_to(offset.offset, offset.line);
  1852. return false;
  1853. }));
  1854. auto expr = parse_string_inner(StringEndCondition::Heredoc);
  1855. set_end_condition(move(end_condition));
  1856. if (found_key) {
  1857. auto offset = current_position();
  1858. if (peek() == '\n')
  1859. consume();
  1860. auto line = consume_while(is_not('\n'));
  1861. if (peek() == '\n')
  1862. consume();
  1863. if (line.trim_whitespace() != record.end)
  1864. restore_to(offset.offset, offset.line);
  1865. }
  1866. if (!expr && found_key) {
  1867. expr = create<AST::StringLiteral>(String {}, AST::StringLiteral::EnclosureType::None);
  1868. } else if (!expr) {
  1869. expr = create<AST::SyntaxError>(String::formatted("Expected to find a valid string inside a heredoc (with end key '{}')", record.end).release_value_but_fixme_should_propagate_errors(), true);
  1870. } else if (!found_key) {
  1871. expr->set_is_syntax_error(*create<AST::SyntaxError>(String::formatted("Expected to find the heredoc key '{}'", record.end).release_value_but_fixme_should_propagate_errors(), true));
  1872. }
  1873. record.node->set_contents(create<AST::DoubleQuotedString>(move(expr)));
  1874. }
  1875. }
  1876. return true;
  1877. }
  1878. StringView Parser::consume_while(Function<bool(char)> condition)
  1879. {
  1880. if (at_end())
  1881. return {};
  1882. auto start_offset = m_offset;
  1883. while (!at_end() && condition(peek()))
  1884. consume();
  1885. return m_input.substring_view(start_offset, m_offset - start_offset);
  1886. }
  1887. bool Parser::next_is(StringView next)
  1888. {
  1889. auto start = current_position();
  1890. auto res = expect(next);
  1891. restore_to(start.offset, start.line);
  1892. return res;
  1893. }
  1894. }