Parser.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. /*
  2. * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifdef CPP_DEBUG
  27. # define DEBUG_SPAM
  28. #endif
  29. #include "Parser.h"
  30. #include "AST.h"
  31. #include <AK/Debug.h>
  32. #include <AK/ScopeGuard.h>
  33. #include <AK/ScopeLogger.h>
  34. #include <LibCpp/Lexer.h>
  35. namespace Cpp {
  36. Parser::Parser(const StringView& program, const String& filename, Preprocessor::Definitions&& definitions)
  37. : m_definitions(move(definitions))
  38. , m_filename(filename)
  39. {
  40. initialize_program_tokens(program);
  41. #if CPP_DEBUG
  42. dbgln("Tokens:");
  43. for (auto& token : m_tokens) {
  44. StringView text;
  45. if (token.start().line != token.end().line || token.start().column > token.end().column)
  46. text = {};
  47. else
  48. text = text_of_token(token);
  49. dbgln("{} {}:{}-{}:{} ({})", token.to_string(), token.start().line, token.start().column, token.end().line, token.end().column, text);
  50. }
  51. #endif
  52. }
  53. void Parser::initialize_program_tokens(const StringView& program)
  54. {
  55. Lexer lexer(program);
  56. for (auto& token : lexer.lex()) {
  57. if (token.type() == Token::Type::Whitespace)
  58. continue;
  59. if (token.type() == Token::Type::Identifier) {
  60. if (auto defined_value = m_definitions.find(text_of_token(token)); defined_value != m_definitions.end()) {
  61. add_tokens_for_preprocessor(token, defined_value->value);
  62. m_replaced_preprocessor_tokens.append({ token, defined_value->value });
  63. continue;
  64. }
  65. }
  66. m_tokens.append(move(token));
  67. }
  68. }
  69. NonnullRefPtr<TranslationUnit> Parser::parse()
  70. {
  71. SCOPE_LOGGER();
  72. if (m_tokens.is_empty())
  73. return create_root_ast_node({}, {});
  74. auto unit = create_root_ast_node(m_tokens.first().start(), m_tokens.last().end());
  75. unit->m_declarations = parse_declarations_in_translation_unit(*unit);
  76. return unit;
  77. }
  78. NonnullRefPtrVector<Declaration> Parser::parse_declarations_in_translation_unit(ASTNode& parent)
  79. {
  80. NonnullRefPtrVector<Declaration> declarations;
  81. while (!eof()) {
  82. auto declaration = parse_single_declaration_in_translation_unit(parent);
  83. if (declaration) {
  84. declarations.append(declaration.release_nonnull());
  85. } else {
  86. error("unexpected token");
  87. consume();
  88. }
  89. }
  90. return declarations;
  91. }
  92. RefPtr<Declaration> Parser::parse_single_declaration_in_translation_unit(ASTNode& parent)
  93. {
  94. while (!eof()) {
  95. if (match_comment()) {
  96. consume(Token::Type::Comment);
  97. continue;
  98. }
  99. if (match_preprocessor()) {
  100. consume_preprocessor();
  101. continue;
  102. }
  103. auto declaration = match_declaration_in_translation_unit();
  104. if (declaration.has_value()) {
  105. return parse_declaration(parent, declaration.value());
  106. }
  107. return {};
  108. }
  109. return {};
  110. }
  111. NonnullRefPtr<Declaration> Parser::parse_declaration(ASTNode& parent, DeclarationType declaration_type)
  112. {
  113. switch (declaration_type) {
  114. case DeclarationType::Function:
  115. return parse_function_declaration(parent);
  116. case DeclarationType::Variable:
  117. return parse_variable_declaration(parent);
  118. case DeclarationType::Enum:
  119. return parse_enum_declaration(parent);
  120. case DeclarationType::Struct:
  121. return parse_struct_or_class_declaration(parent, StructOrClassDeclaration::Type::Struct);
  122. case DeclarationType::Namespace:
  123. return parse_namespace_declaration(parent);
  124. default:
  125. error("unexpected declaration type");
  126. return create_ast_node<InvalidDeclaration>(parent, position(), position());
  127. }
  128. }
  129. NonnullRefPtr<FunctionDeclaration> Parser::parse_function_declaration(ASTNode& parent)
  130. {
  131. auto func = create_ast_node<FunctionDeclaration>(parent, position(), {});
  132. func->m_qualifiers = parse_function_qualifiers();
  133. func->m_return_type = parse_type(*func);
  134. auto function_name = consume(Token::Type::Identifier);
  135. func->m_name = text_of_token(function_name);
  136. consume(Token::Type::LeftParen);
  137. auto parameters = parse_parameter_list(*func);
  138. if (parameters.has_value())
  139. func->m_parameters = move(parameters.value());
  140. consume(Token::Type::RightParen);
  141. RefPtr<FunctionDefinition> body;
  142. Position func_end {};
  143. if (peek(Token::Type::LeftCurly).has_value()) {
  144. body = parse_function_definition(*func);
  145. func_end = body->end();
  146. } else {
  147. func_end = position();
  148. if (match_attribute_specification())
  149. consume_attribute_specification(); // we don't use the value of __attribute__
  150. consume(Token::Type::Semicolon);
  151. }
  152. func->m_definition = move(body);
  153. func->set_end(func_end);
  154. return func;
  155. }
  156. NonnullRefPtr<FunctionDefinition> Parser::parse_function_definition(ASTNode& parent)
  157. {
  158. SCOPE_LOGGER();
  159. auto func = create_ast_node<FunctionDefinition>(parent, position(), {});
  160. consume(Token::Type::LeftCurly);
  161. while (!eof() && peek().type() != Token::Type::RightCurly) {
  162. func->statements().append(parse_statement(func));
  163. }
  164. func->set_end(position());
  165. if (!eof())
  166. consume(Token::Type::RightCurly);
  167. return func;
  168. }
  169. NonnullRefPtr<Statement> Parser::parse_statement(ASTNode& parent)
  170. {
  171. SCOPE_LOGGER();
  172. ArmedScopeGuard consume_semicolon([this]() {
  173. consume(Token::Type::Semicolon);
  174. });
  175. if (match_block_statement()) {
  176. consume_semicolon.disarm();
  177. return parse_block_statement(parent);
  178. }
  179. if (match_comment()) {
  180. consume_semicolon.disarm();
  181. return parse_comment(parent);
  182. }
  183. if (match_variable_declaration()) {
  184. return parse_variable_declaration(parent, false);
  185. }
  186. if (match_expression()) {
  187. return parse_expression(parent);
  188. }
  189. if (match_keyword("return")) {
  190. return parse_return_statement(parent);
  191. }
  192. if (match_keyword("for")) {
  193. consume_semicolon.disarm();
  194. return parse_for_statement(parent);
  195. }
  196. if (match_keyword("if")) {
  197. consume_semicolon.disarm();
  198. return parse_if_statement(parent);
  199. } else {
  200. error("unexpected statement type");
  201. consume_semicolon.disarm();
  202. consume();
  203. return create_ast_node<InvalidStatement>(parent, position(), position());
  204. }
  205. }
  206. NonnullRefPtr<Comment> Parser::parse_comment(ASTNode& parent)
  207. {
  208. auto comment = create_ast_node<Comment>(parent, position(), {});
  209. consume(Token::Type::Comment);
  210. comment->set_end(position());
  211. return comment;
  212. }
  213. bool Parser::match_block_statement()
  214. {
  215. return peek().type() == Token::Type::LeftCurly;
  216. }
  217. NonnullRefPtr<BlockStatement> Parser::parse_block_statement(ASTNode& parent)
  218. {
  219. SCOPE_LOGGER();
  220. auto block_statement = create_ast_node<BlockStatement>(parent, position(), {});
  221. consume(Token::Type::LeftCurly);
  222. while (!eof() && peek().type() != Token::Type::RightCurly) {
  223. block_statement->m_statements.append(parse_statement(*block_statement));
  224. }
  225. consume(Token::Type::RightCurly);
  226. block_statement->set_end(position());
  227. return block_statement;
  228. }
  229. Parser::TemplatizedMatchResult Parser::match_type()
  230. {
  231. save_state();
  232. ScopeGuard state_guard = [this] { load_state(); };
  233. parse_type_qualifiers();
  234. if (match_keyword("struct")) {
  235. consume(Token::Type::Keyword); // Consume struct prefix
  236. }
  237. if (!match_name())
  238. return TemplatizedMatchResult::NoMatch;
  239. parse_name(*m_root_node);
  240. if (peek(Token::Type::Less).has_value()) {
  241. if (match_template_arguments()) {
  242. return TemplatizedMatchResult::Templatized;
  243. }
  244. return TemplatizedMatchResult::NoMatch;
  245. }
  246. return TemplatizedMatchResult::Regular;
  247. }
  248. bool Parser::match_template_arguments()
  249. {
  250. save_state();
  251. ScopeGuard state_guard = [this] { load_state(); };
  252. if (!peek(Token::Type::Less).has_value())
  253. return false;
  254. consume();
  255. while (!eof() && peek().type() != Token::Type::Greater) {
  256. if (match_type() == TemplatizedMatchResult::NoMatch)
  257. return false;
  258. parse_type(*m_root_node);
  259. }
  260. return peek().type() == Token::Type::Greater;
  261. }
  262. NonnullRefPtrVector<Type> Parser::parse_template_arguments(ASTNode& parent)
  263. {
  264. SCOPE_LOGGER();
  265. consume(Token::Type::Less);
  266. NonnullRefPtrVector<Type> template_arguments;
  267. while (!eof() && peek().type() != Token::Type::Greater) {
  268. template_arguments.append(parse_type(parent));
  269. }
  270. consume(Token::Type::Greater);
  271. return template_arguments;
  272. }
  273. bool Parser::match_variable_declaration()
  274. {
  275. SCOPE_LOGGER();
  276. save_state();
  277. ScopeGuard state_guard = [this] { load_state(); };
  278. if (match_type() == TemplatizedMatchResult::NoMatch) {
  279. return false;
  280. }
  281. VERIFY(m_root_node);
  282. parse_type(*m_root_node);
  283. // Identifier
  284. if (!peek(Token::Type::Identifier).has_value()) {
  285. return false;
  286. }
  287. consume();
  288. if (match(Token::Type::Equals)) {
  289. consume(Token::Type::Equals);
  290. if (!match_expression()) {
  291. error("initial value of variable is not an expression");
  292. return false;
  293. }
  294. return true;
  295. }
  296. return match(Token::Type::Semicolon);
  297. }
  298. NonnullRefPtr<VariableDeclaration> Parser::parse_variable_declaration(ASTNode& parent, bool expect_semicolon)
  299. {
  300. SCOPE_LOGGER();
  301. auto var = create_ast_node<VariableDeclaration>(parent, position(), {});
  302. if (!match_variable_declaration()) {
  303. error("unexpected token for variable type");
  304. var->set_end(position());
  305. return var;
  306. }
  307. var->m_type = parse_type(var);
  308. auto identifier_token = consume(Token::Type::Identifier);
  309. RefPtr<Expression> initial_value;
  310. if (match(Token::Type::Equals)) {
  311. consume(Token::Type::Equals);
  312. initial_value = parse_expression(var);
  313. }
  314. if (expect_semicolon)
  315. consume(Token::Type::Semicolon);
  316. var->set_end(position());
  317. var->m_name = text_of_token(identifier_token);
  318. var->m_initial_value = move(initial_value);
  319. return var;
  320. }
  321. NonnullRefPtr<Expression> Parser::parse_expression(ASTNode& parent)
  322. {
  323. SCOPE_LOGGER();
  324. auto expression = parse_primary_expression(parent);
  325. // TODO: remove eof() logic, should still work without it
  326. if (eof() || match(Token::Type::Semicolon)) {
  327. return expression;
  328. }
  329. NonnullRefPtrVector<Expression> secondary_expressions;
  330. while (match_secondary_expression()) {
  331. // FIXME: Handle operator precedence
  332. expression = parse_secondary_expression(parent, expression);
  333. secondary_expressions.append(expression);
  334. }
  335. for (size_t i = 0; secondary_expressions.size() != 0 && i < secondary_expressions.size() - 1; ++i) {
  336. secondary_expressions[i].set_parent(secondary_expressions[i + 1]);
  337. }
  338. return expression;
  339. }
  340. bool Parser::match_secondary_expression()
  341. {
  342. auto type = peek().type();
  343. return type == Token::Type::Plus
  344. || type == Token::Type::PlusEquals
  345. || type == Token::Type::Minus
  346. || type == Token::Type::MinusEquals
  347. || type == Token::Type::Asterisk
  348. || type == Token::Type::AsteriskEquals
  349. || type == Token::Type::Percent
  350. || type == Token::Type::PercentEquals
  351. || type == Token::Type::Equals
  352. || type == Token::Type::Greater
  353. || type == Token::Type::Greater
  354. || type == Token::Type::Less
  355. || type == Token::Type::LessEquals
  356. || type == Token::Type::Dot
  357. || type == Token::Type::PlusPlus
  358. || type == Token::Type::MinusMinus
  359. || type == Token::Type::And
  360. || type == Token::Type::AndEquals
  361. || type == Token::Type::Pipe
  362. || type == Token::Type::PipeEquals
  363. || type == Token::Type::Caret
  364. || type == Token::Type::CaretEquals
  365. || type == Token::Type::LessLess
  366. || type == Token::Type::LessLessEquals
  367. || type == Token::Type::GreaterGreater
  368. || type == Token::Type::GreaterGreaterEquals
  369. || type == Token::Type::EqualsEquals
  370. || type == Token::Type::AndAnd
  371. || type == Token::Type::PipePipe
  372. || type == Token::Type::ExclamationMarkEquals
  373. || type == Token::Type::PipePipe;
  374. }
  375. NonnullRefPtr<Expression> Parser::parse_primary_expression(ASTNode& parent)
  376. {
  377. SCOPE_LOGGER();
  378. // TODO: remove eof() logic, should still work without it
  379. if (eof()) {
  380. auto node = create_ast_node<Identifier>(parent, position(), position());
  381. return node;
  382. }
  383. if (match_unary_expression())
  384. return parse_unary_expression(parent);
  385. if (match_literal()) {
  386. return parse_literal(parent);
  387. }
  388. if (match_cpp_cast_expression())
  389. return parse_cpp_cast_expression(parent);
  390. if (match_sizeof_expression())
  391. return parse_sizeof_expression(parent);
  392. if (match_braced_init_list())
  393. return parse_braced_init_list(parent);
  394. if (match_name()) {
  395. if (match_function_call() != TemplatizedMatchResult::NoMatch)
  396. return parse_function_call(parent);
  397. return parse_name(parent);
  398. }
  399. error("could not parse primary expression");
  400. auto token = consume();
  401. return create_ast_node<InvalidExpression>(parent, token.start(), token.end());
  402. }
  403. bool Parser::match_literal()
  404. {
  405. switch (peek().type()) {
  406. case Token::Type::Integer:
  407. return true;
  408. case Token::Type::SingleQuotedString:
  409. return true;
  410. case Token::Type::DoubleQuotedString:
  411. return true;
  412. case Token::Type::Float:
  413. return true;
  414. case Token::Type::Keyword: {
  415. return match_boolean_literal() || peek().text() == "nullptr";
  416. }
  417. default:
  418. return false;
  419. }
  420. }
  421. bool Parser::match_unary_expression()
  422. {
  423. auto type = peek().type();
  424. return type == Token::Type::PlusPlus
  425. || type == Token::Type::MinusMinus
  426. || type == Token::Type::ExclamationMark
  427. || type == Token::Type::Tilde
  428. || type == Token::Type::Plus
  429. || type == Token::Type::Minus
  430. || type == Token::Type::And;
  431. }
  432. NonnullRefPtr<UnaryExpression> Parser::parse_unary_expression(ASTNode& parent)
  433. {
  434. auto unary_exp = create_ast_node<UnaryExpression>(parent, position(), {});
  435. auto op_token = consume();
  436. UnaryOp op { UnaryOp::Invalid };
  437. switch (op_token.type()) {
  438. case Token::Type::Minus:
  439. op = UnaryOp::Minus;
  440. break;
  441. case Token::Type::Plus:
  442. op = UnaryOp::Plus;
  443. break;
  444. case Token::Type::ExclamationMark:
  445. op = UnaryOp::Not;
  446. break;
  447. case Token::Type::Tilde:
  448. op = UnaryOp::BitwiseNot;
  449. break;
  450. case Token::Type::PlusPlus:
  451. op = UnaryOp::PlusPlus;
  452. break;
  453. case Token::Type::And:
  454. op = UnaryOp::Address;
  455. break;
  456. default:
  457. break;
  458. }
  459. unary_exp->m_op = op;
  460. auto lhs = parse_expression(*unary_exp);
  461. unary_exp->m_lhs = lhs;
  462. unary_exp->set_end(lhs->end());
  463. return unary_exp;
  464. }
  465. NonnullRefPtr<Expression> Parser::parse_literal(ASTNode& parent)
  466. {
  467. switch (peek().type()) {
  468. case Token::Type::Integer: {
  469. auto token = consume();
  470. return create_ast_node<NumericLiteral>(parent, token.start(), token.end(), text_of_token(token));
  471. }
  472. case Token::Type::SingleQuotedString:
  473. [[fallthrough]];
  474. case Token::Type::DoubleQuotedString:
  475. return parse_string_literal(parent);
  476. case Token::Type::Keyword: {
  477. if (match_boolean_literal())
  478. return parse_boolean_literal(parent);
  479. if (peek().text() == "nullptr") {
  480. auto token = consume();
  481. return create_ast_node<NullPointerLiteral>(parent, token.start(), token.end());
  482. }
  483. [[fallthrough]];
  484. }
  485. default: {
  486. error("could not parse literal");
  487. auto token = consume();
  488. return create_ast_node<InvalidExpression>(parent, token.start(), token.end());
  489. }
  490. }
  491. }
  492. NonnullRefPtr<Expression> Parser::parse_secondary_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs)
  493. {
  494. SCOPE_LOGGER();
  495. switch (peek().type()) {
  496. case Token::Type::Plus:
  497. return parse_binary_expression(parent, lhs, BinaryOp::Addition);
  498. case Token::Type::Less:
  499. return parse_binary_expression(parent, lhs, BinaryOp::LessThan);
  500. case Token::Type::EqualsEquals:
  501. return parse_binary_expression(parent, lhs, BinaryOp::EqualsEquals);
  502. case Token::Type::ExclamationMarkEquals:
  503. return parse_binary_expression(parent, lhs, BinaryOp::NotEqual);
  504. case Token::Type::And:
  505. return parse_binary_expression(parent, lhs, BinaryOp::BitwiseAnd);
  506. case Token::Type::AndAnd:
  507. return parse_binary_expression(parent, lhs, BinaryOp::LogicalAnd);
  508. case Token::Type::Pipe:
  509. return parse_binary_expression(parent, lhs, BinaryOp::BitwiseOr);
  510. case Token::Type::PipePipe:
  511. return parse_binary_expression(parent, lhs, BinaryOp::LogicalOr);
  512. case Token::Type::Equals:
  513. return parse_assignment_expression(parent, lhs, AssignmentOp::Assignment);
  514. case Token::Type::Dot: {
  515. consume();
  516. auto exp = create_ast_node<MemberExpression>(parent, lhs->start(), {});
  517. lhs->set_parent(*exp);
  518. exp->m_object = move(lhs);
  519. exp->m_property = parse_expression(*exp);
  520. exp->set_end(position());
  521. return exp;
  522. }
  523. default: {
  524. error(String::formatted("unexpected operator for expression. operator: {}", peek().to_string()));
  525. auto token = consume();
  526. return create_ast_node<InvalidExpression>(parent, token.start(), token.end());
  527. }
  528. }
  529. }
  530. NonnullRefPtr<BinaryExpression> Parser::parse_binary_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs, BinaryOp op)
  531. {
  532. consume(); // Operator
  533. auto exp = create_ast_node<BinaryExpression>(parent, lhs->start(), {});
  534. lhs->set_parent(*exp);
  535. exp->m_op = op;
  536. exp->m_lhs = move(lhs);
  537. auto rhs = parse_expression(exp);
  538. exp->set_end(rhs->end());
  539. exp->m_rhs = move(rhs);
  540. return exp;
  541. }
  542. NonnullRefPtr<AssignmentExpression> Parser::parse_assignment_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs, AssignmentOp op)
  543. {
  544. consume(); // Operator
  545. auto exp = create_ast_node<AssignmentExpression>(parent, lhs->start(), {});
  546. lhs->set_parent(*exp);
  547. exp->m_op = op;
  548. exp->m_lhs = move(lhs);
  549. auto rhs = parse_expression(exp);
  550. exp->set_end(rhs->end());
  551. exp->m_rhs = move(rhs);
  552. return exp;
  553. }
  554. Optional<Parser::DeclarationType> Parser::match_declaration_in_translation_unit()
  555. {
  556. if (match_function_declaration())
  557. return DeclarationType::Function;
  558. if (match_enum_declaration())
  559. return DeclarationType::Enum;
  560. if (match_struct_declaration())
  561. return DeclarationType::Struct;
  562. if (match_namespace_declaration())
  563. return DeclarationType::Namespace;
  564. if (match_variable_declaration())
  565. return DeclarationType::Variable;
  566. return {};
  567. }
  568. bool Parser::match_enum_declaration()
  569. {
  570. return match_keyword("enum");
  571. }
  572. bool Parser::match_struct_declaration()
  573. {
  574. return match_keyword("struct");
  575. }
  576. bool Parser::match_namespace_declaration()
  577. {
  578. return match_keyword("namespace");
  579. }
  580. bool Parser::match_function_declaration()
  581. {
  582. save_state();
  583. ScopeGuard state_guard = [this] { load_state(); };
  584. parse_function_qualifiers();
  585. if (match_type() == TemplatizedMatchResult::NoMatch)
  586. return false;
  587. VERIFY(m_root_node);
  588. parse_type(*m_root_node);
  589. if (!peek(Token::Type::Identifier).has_value())
  590. return false;
  591. consume();
  592. if (!peek(Token::Type::LeftParen).has_value())
  593. return false;
  594. consume();
  595. while (consume().type() != Token::Type::RightParen && !eof()) { };
  596. if (peek(Token::Type::Semicolon).has_value() || peek(Token::Type::LeftCurly).has_value())
  597. return true;
  598. if (match_attribute_specification()) {
  599. consume_attribute_specification();
  600. return peek(Token::Type::Semicolon).has_value();
  601. }
  602. return false;
  603. }
  604. Optional<NonnullRefPtrVector<Parameter>> Parser::parse_parameter_list(ASTNode& parent)
  605. {
  606. SCOPE_LOGGER();
  607. NonnullRefPtrVector<Parameter> parameters;
  608. while (peek().type() != Token::Type::RightParen && !eof()) {
  609. if (match_ellipsis()) {
  610. auto last_dot = consume();
  611. while (peek().type() == Token::Type::Dot)
  612. last_dot = consume();
  613. auto param = create_ast_node<Parameter>(parent, position(), last_dot.end(), StringView {});
  614. param->m_is_ellipsis = true;
  615. parameters.append(move(param));
  616. } else {
  617. auto type = parse_type(parent);
  618. auto name_identifier = peek(Token::Type::Identifier);
  619. if (name_identifier.has_value())
  620. consume(Token::Type::Identifier);
  621. StringView name;
  622. if (name_identifier.has_value())
  623. name = text_of_token(name_identifier.value());
  624. auto param = create_ast_node<Parameter>(parent, type->start(), name_identifier.has_value() ? name_identifier.value().end() : type->end(), name);
  625. param->m_type = move(type);
  626. parameters.append(move(param));
  627. }
  628. if (peek(Token::Type::Comma).has_value())
  629. consume(Token::Type::Comma);
  630. }
  631. return parameters;
  632. }
  633. bool Parser::match_comment()
  634. {
  635. return match(Token::Type::Comment);
  636. }
  637. bool Parser::match_whitespace()
  638. {
  639. return match(Token::Type::Whitespace);
  640. }
  641. bool Parser::match_preprocessor()
  642. {
  643. return match(Token::Type::PreprocessorStatement) || match(Token::Type::IncludeStatement);
  644. }
  645. void Parser::consume_preprocessor()
  646. {
  647. SCOPE_LOGGER();
  648. switch (peek().type()) {
  649. case Token::Type::PreprocessorStatement:
  650. consume();
  651. break;
  652. case Token::Type::IncludeStatement:
  653. consume();
  654. consume(Token::Type::IncludePath);
  655. break;
  656. default:
  657. error("unexpected token while parsing preprocessor statement");
  658. consume();
  659. }
  660. }
  661. Optional<Token> Parser::consume_whitespace()
  662. {
  663. SCOPE_LOGGER();
  664. return consume(Token::Type::Whitespace);
  665. }
  666. Token Parser::consume(Token::Type type)
  667. {
  668. auto token = consume();
  669. if (token.type() != type)
  670. error(String::formatted("expected {} at {}:{}, found: {}", Token::type_to_string(type), token.start().line, token.start().column, Token::type_to_string(token.type())));
  671. return token;
  672. }
  673. bool Parser::match(Token::Type type)
  674. {
  675. return peek().type() == type;
  676. }
  677. Token Parser::consume()
  678. {
  679. if (eof()) {
  680. error("C++ Parser: out of tokens");
  681. return { Token::Type::EOF_TOKEN, position(), position(), {} };
  682. }
  683. return m_tokens[m_state.token_index++];
  684. }
  685. Token Parser::peek(size_t offset) const
  686. {
  687. if (m_state.token_index + offset >= m_tokens.size())
  688. return { Token::Type::EOF_TOKEN, position(), position(), {} };
  689. return m_tokens[m_state.token_index + offset];
  690. }
  691. Optional<Token> Parser::peek(Token::Type type) const
  692. {
  693. auto token = peek();
  694. if (token.type() == type)
  695. return token;
  696. return {};
  697. }
  698. void Parser::save_state()
  699. {
  700. m_saved_states.append(m_state);
  701. }
  702. void Parser::load_state()
  703. {
  704. m_state = m_saved_states.take_last();
  705. }
  706. StringView Parser::text_of_token(const Cpp::Token& token) const
  707. {
  708. return token.text();
  709. }
  710. String Parser::text_of_node(const ASTNode& node) const
  711. {
  712. return text_in_range(node.start(), node.end());
  713. }
  714. String Parser::text_in_range(Position start, Position end) const
  715. {
  716. auto start_token_index = index_of_token_at(start);
  717. auto end_node_index = index_of_token_at(end);
  718. VERIFY(start_token_index.has_value());
  719. VERIFY(end_node_index.has_value());
  720. StringBuilder text;
  721. for (size_t i = start_token_index.value(); i <= end_node_index.value(); ++i) {
  722. text.append(m_tokens[i].text());
  723. }
  724. return text.build();
  725. }
  726. void Parser::error(StringView message)
  727. {
  728. SCOPE_LOGGER();
  729. if (message.is_null() || message.is_empty())
  730. message = "<empty>";
  731. String formatted_message;
  732. if (m_state.token_index >= m_tokens.size()) {
  733. formatted_message = String::formatted("C++ Parsed error on EOF.{}", message);
  734. } else {
  735. formatted_message = String::formatted("C++ Parser error: {}. token: {} ({}:{})",
  736. message,
  737. m_state.token_index < m_tokens.size() ? text_of_token(m_tokens[m_state.token_index]) : "EOF",
  738. m_tokens[m_state.token_index].start().line,
  739. m_tokens[m_state.token_index].start().column);
  740. }
  741. m_state.errors.append(formatted_message);
  742. }
  743. bool Parser::match_expression()
  744. {
  745. auto token_type = peek().type();
  746. return match_literal()
  747. || token_type == Token::Type::Identifier
  748. || match_unary_expression()
  749. || match_cpp_cast_expression()
  750. || match_sizeof_expression()
  751. || match_braced_init_list();
  752. }
  753. bool Parser::eof() const
  754. {
  755. return m_state.token_index >= m_tokens.size();
  756. }
  757. Position Parser::position() const
  758. {
  759. if (eof())
  760. return m_tokens.last().end();
  761. return peek().start();
  762. }
  763. RefPtr<ASTNode> Parser::eof_node() const
  764. {
  765. VERIFY(m_tokens.size());
  766. return node_at(m_tokens.last().end());
  767. }
  768. RefPtr<ASTNode> Parser::node_at(Position pos) const
  769. {
  770. auto index = index_of_node_at(pos);
  771. if (!index.has_value())
  772. return nullptr;
  773. return m_nodes[index.value()];
  774. }
  775. Optional<size_t> Parser::index_of_node_at(Position pos) const
  776. {
  777. VERIFY(!m_tokens.is_empty());
  778. Optional<size_t> match_node_index;
  779. auto node_span = [](const ASTNode& node) {
  780. VERIFY(node.end().line >= node.start().line);
  781. VERIFY((node.end().line > node.start().line) || (node.end().column >= node.start().column));
  782. return Position { node.end().line - node.start().line, node.start().line != node.end().line ? 0 : node.end().column - node.start().column };
  783. };
  784. for (size_t node_index = 0; node_index < m_nodes.size(); ++node_index) {
  785. auto& node = m_nodes[node_index];
  786. if (node.start() > pos || node.end() < pos)
  787. continue;
  788. if (!match_node_index.has_value() || (node_span(node) < node_span(m_nodes[match_node_index.value()])))
  789. match_node_index = node_index;
  790. }
  791. return match_node_index;
  792. }
  793. Optional<Token> Parser::token_at(Position pos) const
  794. {
  795. auto index = index_of_token_at(pos);
  796. if (!index.has_value())
  797. return {};
  798. return m_tokens[index.value()];
  799. }
  800. Optional<size_t> Parser::index_of_token_at(Position pos) const
  801. {
  802. for (size_t token_index = 0; token_index < m_tokens.size(); ++token_index) {
  803. auto token = m_tokens[token_index];
  804. if (token.start() > pos || token.end() < pos)
  805. continue;
  806. return token_index;
  807. }
  808. return {};
  809. }
  810. void Parser::print_tokens() const
  811. {
  812. for (auto& token : m_tokens) {
  813. dbgln("{}", token.to_string());
  814. }
  815. }
  816. Parser::TemplatizedMatchResult Parser::match_function_call()
  817. {
  818. save_state();
  819. ScopeGuard state_guard = [this] { load_state(); };
  820. if (!match_name())
  821. return TemplatizedMatchResult::NoMatch;
  822. parse_name(*m_root_node);
  823. bool is_templatized = false;
  824. if (match_template_arguments()) {
  825. is_templatized = true;
  826. parse_template_arguments(*m_root_node);
  827. }
  828. if (!match(Token::Type::LeftParen))
  829. return TemplatizedMatchResult::NoMatch;
  830. return is_templatized ? TemplatizedMatchResult::Templatized : TemplatizedMatchResult::Regular;
  831. }
  832. NonnullRefPtr<FunctionCall> Parser::parse_function_call(ASTNode& parent)
  833. {
  834. SCOPE_LOGGER();
  835. auto match_result = match_type();
  836. if (match_result == TemplatizedMatchResult::NoMatch) {
  837. error("expected type");
  838. return create_ast_node<FunctionCall>(parent, position(), position());
  839. }
  840. bool is_templatized = match_result == TemplatizedMatchResult::Templatized;
  841. RefPtr<FunctionCall> call;
  842. if (is_templatized) {
  843. call = create_ast_node<TemplatizedFunctionCall>(parent, position(), {});
  844. } else {
  845. call = create_ast_node<FunctionCall>(parent, position(), {});
  846. }
  847. call->m_name = parse_name(*call);
  848. if (is_templatized) {
  849. static_cast<TemplatizedFunctionCall&>(*call).m_template_arguments = parse_template_arguments(*call);
  850. }
  851. NonnullRefPtrVector<Expression> args;
  852. consume(Token::Type::LeftParen);
  853. while (peek().type() != Token::Type::RightParen && !eof()) {
  854. args.append(parse_expression(*call));
  855. if (peek().type() == Token::Type::Comma)
  856. consume(Token::Type::Comma);
  857. }
  858. consume(Token::Type::RightParen);
  859. call->m_arguments = move(args);
  860. call->set_end(position());
  861. return call.release_nonnull();
  862. }
  863. NonnullRefPtr<StringLiteral> Parser::parse_string_literal(ASTNode& parent)
  864. {
  865. SCOPE_LOGGER();
  866. Optional<size_t> start_token_index;
  867. Optional<size_t> end_token_index;
  868. while (!eof()) {
  869. auto token = peek();
  870. if (token.type() != Token::Type::DoubleQuotedString && token.type() != Token::Type::SingleQuotedString && token.type() != Token::Type::EscapeSequence) {
  871. VERIFY(start_token_index.has_value());
  872. end_token_index = m_state.token_index - 1;
  873. break;
  874. }
  875. if (!start_token_index.has_value())
  876. start_token_index = m_state.token_index;
  877. consume();
  878. }
  879. // String was not terminated
  880. if (!end_token_index.has_value()) {
  881. end_token_index = m_tokens.size() - 1;
  882. }
  883. VERIFY(start_token_index.has_value());
  884. VERIFY(end_token_index.has_value());
  885. Token start_token = m_tokens[start_token_index.value()];
  886. Token end_token = m_tokens[end_token_index.value()];
  887. auto text = text_in_range(start_token.start(), end_token.end());
  888. auto string_literal = create_ast_node<StringLiteral>(parent, start_token.start(), end_token.end());
  889. string_literal->m_value = text;
  890. return string_literal;
  891. }
  892. NonnullRefPtr<ReturnStatement> Parser::parse_return_statement(ASTNode& parent)
  893. {
  894. SCOPE_LOGGER();
  895. auto return_statement = create_ast_node<ReturnStatement>(parent, position(), {});
  896. consume(Token::Type::Keyword);
  897. if (!peek(Token::Type::Semicolon).has_value()) {
  898. auto expression = parse_expression(*return_statement);
  899. return_statement->m_value = expression;
  900. }
  901. return_statement->set_end(position());
  902. return return_statement;
  903. }
  904. NonnullRefPtr<EnumDeclaration> Parser::parse_enum_declaration(ASTNode& parent)
  905. {
  906. SCOPE_LOGGER();
  907. auto enum_decl = create_ast_node<EnumDeclaration>(parent, position(), {});
  908. consume_keyword("enum");
  909. auto name_token = consume(Token::Type::Identifier);
  910. enum_decl->m_name = text_of_token(name_token);
  911. consume(Token::Type::LeftCurly);
  912. while (!eof() && peek().type() != Token::Type::RightCurly) {
  913. enum_decl->m_entries.append(text_of_token(consume(Token::Type::Identifier)));
  914. if (peek().type() != Token::Type::Comma) {
  915. break;
  916. }
  917. consume(Token::Type::Comma);
  918. }
  919. consume(Token::Type::RightCurly);
  920. consume(Token::Type::Semicolon);
  921. enum_decl->set_end(position());
  922. return enum_decl;
  923. }
  924. Token Parser::consume_keyword(const String& keyword)
  925. {
  926. auto token = consume();
  927. if (token.type() != Token::Type::Keyword) {
  928. error(String::formatted("unexpected token: {}, expected Keyword", token.to_string()));
  929. return token;
  930. }
  931. if (text_of_token(token) != keyword) {
  932. error(String::formatted("unexpected keyword: {}, expected {}", text_of_token(token), keyword));
  933. return token;
  934. }
  935. return token;
  936. }
  937. bool Parser::match_keyword(const String& keyword)
  938. {
  939. auto token = peek();
  940. if (token.type() != Token::Type::Keyword) {
  941. return false;
  942. }
  943. if (text_of_token(token) != keyword) {
  944. return false;
  945. }
  946. return true;
  947. }
  948. NonnullRefPtr<StructOrClassDeclaration> Parser::parse_struct_or_class_declaration(ASTNode& parent, StructOrClassDeclaration::Type type)
  949. {
  950. SCOPE_LOGGER();
  951. auto decl = create_ast_node<StructOrClassDeclaration>(parent, position(), {}, type);
  952. switch (type) {
  953. case StructOrClassDeclaration::Type::Struct:
  954. consume_keyword("struct");
  955. break;
  956. case StructOrClassDeclaration::Type::Class:
  957. consume_keyword("class");
  958. break;
  959. }
  960. auto name_token = consume(Token::Type::Identifier);
  961. decl->m_name = text_of_token(name_token);
  962. consume(Token::Type::LeftCurly);
  963. while (!eof() && peek().type() != Token::Type::RightCurly) {
  964. decl->m_members.append(parse_member_declaration(*decl));
  965. }
  966. consume(Token::Type::RightCurly);
  967. consume(Token::Type::Semicolon);
  968. decl->set_end(position());
  969. return decl;
  970. }
  971. NonnullRefPtr<MemberDeclaration> Parser::parse_member_declaration(ASTNode& parent)
  972. {
  973. SCOPE_LOGGER();
  974. auto member_decl = create_ast_node<MemberDeclaration>(parent, position(), {});
  975. member_decl->m_type = parse_type(*member_decl);
  976. auto identifier_token = consume(Token::Type::Identifier);
  977. member_decl->m_name = text_of_token(identifier_token);
  978. if (match_braced_init_list()) {
  979. member_decl->m_initial_value = parse_braced_init_list(*member_decl);
  980. }
  981. consume(Token::Type::Semicolon);
  982. member_decl->set_end(position());
  983. return member_decl;
  984. }
  985. NonnullRefPtr<BooleanLiteral> Parser::parse_boolean_literal(ASTNode& parent)
  986. {
  987. SCOPE_LOGGER();
  988. auto token = consume(Token::Type::Keyword);
  989. auto text = text_of_token(token);
  990. // text == "true" || text == "false";
  991. bool value = (text == "true");
  992. return create_ast_node<BooleanLiteral>(parent, token.start(), token.end(), value);
  993. }
  994. bool Parser::match_boolean_literal()
  995. {
  996. auto token = peek();
  997. if (token.type() != Token::Type::Keyword)
  998. return false;
  999. auto text = text_of_token(token);
  1000. return text == "true" || text == "false";
  1001. }
  1002. NonnullRefPtr<Type> Parser::parse_type(ASTNode& parent)
  1003. {
  1004. SCOPE_LOGGER();
  1005. auto match_result = match_type();
  1006. if (match_result == TemplatizedMatchResult::NoMatch) {
  1007. auto token = consume();
  1008. return create_ast_node<Type>(parent, token.start(), token.end());
  1009. }
  1010. bool is_templatized = match_result == TemplatizedMatchResult::Templatized;
  1011. RefPtr<Type> type;
  1012. if (is_templatized) {
  1013. type = create_ast_node<TemplatizedType>(parent, position(), {});
  1014. } else {
  1015. type = create_ast_node<Type>(parent, position(), {});
  1016. }
  1017. auto qualifiers = parse_type_qualifiers();
  1018. type->m_qualifiers = move(qualifiers);
  1019. if (match_keyword("struct")) {
  1020. consume(Token::Type::Keyword); // Consume struct prefix
  1021. }
  1022. if (!match_name()) {
  1023. type->set_end(position());
  1024. error(String::formatted("expected name instead of: {}", peek().text()));
  1025. return type.release_nonnull();
  1026. }
  1027. type->m_name = parse_name(*type);
  1028. if (is_templatized) {
  1029. static_cast<TemplatizedType&>(*type).m_template_arguments = parse_template_arguments(*type);
  1030. }
  1031. while (!eof() && peek().type() == Token::Type::Asterisk) {
  1032. type->set_end(position());
  1033. auto asterisk = consume();
  1034. auto ptr = create_ast_node<Pointer>(parent, asterisk.start(), asterisk.end());
  1035. type->set_parent(*ptr);
  1036. ptr->m_pointee = type;
  1037. type = ptr;
  1038. }
  1039. type->set_end(position());
  1040. return type.release_nonnull();
  1041. }
  1042. NonnullRefPtr<ForStatement> Parser::parse_for_statement(ASTNode& parent)
  1043. {
  1044. SCOPE_LOGGER();
  1045. auto for_statement = create_ast_node<ForStatement>(parent, position(), {});
  1046. consume(Token::Type::Keyword);
  1047. consume(Token::Type::LeftParen);
  1048. if (peek().type() != Token::Type::Semicolon)
  1049. for_statement->m_init = parse_variable_declaration(*for_statement, false);
  1050. consume(Token::Type::Semicolon);
  1051. if (peek().type() != Token::Type::Semicolon)
  1052. for_statement->m_test = parse_expression(*for_statement);
  1053. consume(Token::Type::Semicolon);
  1054. if (peek().type() != Token::Type::RightParen)
  1055. for_statement->m_update = parse_expression(*for_statement);
  1056. consume(Token::Type::RightParen);
  1057. for_statement->m_body = parse_statement(*for_statement);
  1058. for_statement->set_end(for_statement->m_body->end());
  1059. return for_statement;
  1060. }
  1061. NonnullRefPtr<IfStatement> Parser::parse_if_statement(ASTNode& parent)
  1062. {
  1063. SCOPE_LOGGER();
  1064. auto if_statement = create_ast_node<IfStatement>(parent, position(), {});
  1065. consume(Token::Type::Keyword);
  1066. consume(Token::Type::LeftParen);
  1067. if_statement->m_predicate = parse_expression(*if_statement);
  1068. consume(Token::Type::RightParen);
  1069. if_statement->m_then = parse_statement(*if_statement);
  1070. if (match_keyword("else")) {
  1071. consume(Token::Type::Keyword);
  1072. if_statement->m_else = parse_statement(*if_statement);
  1073. if_statement->set_end(if_statement->m_else->end());
  1074. } else {
  1075. if_statement->set_end(if_statement->m_then->end());
  1076. }
  1077. return if_statement;
  1078. }
  1079. Vector<StringView> Parser::parse_type_qualifiers()
  1080. {
  1081. SCOPE_LOGGER();
  1082. Vector<StringView> qualifiers;
  1083. while (!eof()) {
  1084. auto token = peek();
  1085. if (token.type() != Token::Type::Keyword)
  1086. break;
  1087. auto text = text_of_token(token);
  1088. if (text == "static" || text == "const") {
  1089. qualifiers.append(text);
  1090. consume();
  1091. } else {
  1092. break;
  1093. }
  1094. }
  1095. return qualifiers;
  1096. }
  1097. Vector<StringView> Parser::parse_function_qualifiers()
  1098. {
  1099. SCOPE_LOGGER();
  1100. Vector<StringView> qualifiers;
  1101. while (!eof()) {
  1102. auto token = peek();
  1103. if (token.type() != Token::Type::Keyword)
  1104. break;
  1105. auto text = text_of_token(token);
  1106. if (text == "static" || text == "inline") {
  1107. qualifiers.append(text);
  1108. consume();
  1109. } else {
  1110. break;
  1111. }
  1112. }
  1113. return qualifiers;
  1114. }
  1115. bool Parser::match_attribute_specification()
  1116. {
  1117. return text_of_token(peek()) == "__attribute__";
  1118. }
  1119. void Parser::consume_attribute_specification()
  1120. {
  1121. consume(); // __attribute__
  1122. consume(Token::Type::LeftParen);
  1123. size_t left_count = 1;
  1124. while (!eof()) {
  1125. auto token = consume();
  1126. if (token.type() == Token::Type::LeftParen) {
  1127. ++left_count;
  1128. }
  1129. if (token.type() == Token::Type::RightParen) {
  1130. --left_count;
  1131. }
  1132. if (left_count == 0)
  1133. return;
  1134. }
  1135. }
  1136. bool Parser::match_ellipsis()
  1137. {
  1138. if (m_state.token_index > m_tokens.size() - 3)
  1139. return false;
  1140. return peek().type() == Token::Type::Dot && peek().type() == Token::Type::Dot && peek().type() == Token::Type::Dot;
  1141. }
  1142. void Parser::add_tokens_for_preprocessor(Token& replaced_token, Preprocessor::DefinedValue& definition)
  1143. {
  1144. if (!definition.value.has_value())
  1145. return;
  1146. Lexer lexer(definition.value.value());
  1147. for (auto token : lexer.lex()) {
  1148. if (token.type() == Token::Type::Whitespace)
  1149. continue;
  1150. token.set_start(replaced_token.start());
  1151. token.set_end(replaced_token.end());
  1152. m_tokens.append(move(token));
  1153. }
  1154. }
  1155. NonnullRefPtr<NamespaceDeclaration> Parser::parse_namespace_declaration(ASTNode& parent, bool is_nested_namespace)
  1156. {
  1157. auto namespace_decl = create_ast_node<NamespaceDeclaration>(parent, position(), {});
  1158. if (!is_nested_namespace)
  1159. consume(Token::Type::Keyword);
  1160. auto name_token = consume(Token::Type::Identifier);
  1161. namespace_decl->m_name = name_token.text();
  1162. if (peek().type() == Token::Type::ColonColon) {
  1163. consume(Token::Type::ColonColon);
  1164. namespace_decl->m_declarations.append(parse_namespace_declaration(*namespace_decl, true));
  1165. namespace_decl->set_end(position());
  1166. return namespace_decl;
  1167. }
  1168. consume(Token::Type::LeftCurly);
  1169. while (!eof() && peek().type() != Token::Type::RightCurly) {
  1170. auto declaration = parse_single_declaration_in_translation_unit(*namespace_decl);
  1171. if (declaration) {
  1172. namespace_decl->m_declarations.append(declaration.release_nonnull());
  1173. } else {
  1174. error("unexpected token");
  1175. consume();
  1176. }
  1177. }
  1178. consume(Token::Type::RightCurly);
  1179. namespace_decl->set_end(position());
  1180. return namespace_decl;
  1181. }
  1182. bool Parser::match_name()
  1183. {
  1184. auto type = peek().type();
  1185. return type == Token::Type::Identifier || type == Token::Type::KnownType;
  1186. }
  1187. NonnullRefPtr<Name> Parser::parse_name(ASTNode& parent)
  1188. {
  1189. auto name_node = create_ast_node<Name>(parent, position(), {});
  1190. while (!eof() && (peek().type() == Token::Type::Identifier || peek().type() == Token::Type::KnownType)) {
  1191. auto token = consume();
  1192. name_node->m_scope.append(create_ast_node<Identifier>(*name_node, token.start(), token.end(), token.text()));
  1193. if (peek().type() == Token::Type::ColonColon)
  1194. consume();
  1195. else
  1196. break;
  1197. }
  1198. VERIFY(!name_node->m_scope.is_empty());
  1199. name_node->m_name = name_node->m_scope.take_last();
  1200. name_node->set_end(position());
  1201. return name_node;
  1202. }
  1203. bool Parser::match_cpp_cast_expression()
  1204. {
  1205. save_state();
  1206. ScopeGuard state_guard = [this] { load_state(); };
  1207. auto token = consume();
  1208. if (token.type() != Token::Type::Keyword)
  1209. return false;
  1210. auto text = token.text();
  1211. if (text == "static_cast" || text == "reinterpret_cast" || text == "dynamic_cast" || text == "const_cast")
  1212. return true;
  1213. return false;
  1214. }
  1215. NonnullRefPtr<CppCastExpression> Parser::parse_cpp_cast_expression(ASTNode& parent)
  1216. {
  1217. auto cast_expression = create_ast_node<CppCastExpression>(parent, position(), {});
  1218. cast_expression->m_cast_type = consume(Token::Type::Keyword).text();
  1219. consume(Token::Type::Less);
  1220. cast_expression->m_type = parse_type(*cast_expression);
  1221. consume(Token::Type::Greater);
  1222. consume(Token::Type::LeftParen);
  1223. cast_expression->m_expression = parse_expression(*cast_expression);
  1224. consume(Token::Type::RightParen);
  1225. cast_expression->set_end(position());
  1226. return cast_expression;
  1227. }
  1228. bool Parser::match_sizeof_expression()
  1229. {
  1230. return match_keyword("sizeof");
  1231. }
  1232. NonnullRefPtr<SizeofExpression> Parser::parse_sizeof_expression(ASTNode& parent)
  1233. {
  1234. auto exp = create_ast_node<SizeofExpression>(parent, position(), {});
  1235. consume(Token::Type::Keyword);
  1236. consume(Token::Type::LeftParen);
  1237. exp->m_type = parse_type(parent);
  1238. consume(Token::Type::RightParen);
  1239. exp->set_end(position());
  1240. return exp;
  1241. }
  1242. bool Parser::match_braced_init_list()
  1243. {
  1244. return match(Token::Type::LeftCurly);
  1245. }
  1246. NonnullRefPtr<BracedInitList> Parser::parse_braced_init_list(ASTNode& parent)
  1247. {
  1248. auto init_list = create_ast_node<BracedInitList>(parent, position(), {});
  1249. consume(Token::Type::LeftCurly);
  1250. while (!eof() && peek().type() != Token::Type::RightCurly) {
  1251. init_list->m_expressions.append(parse_expression(*init_list));
  1252. }
  1253. consume(Token::Type::RightCurly);
  1254. init_list->set_end(position());
  1255. return init_list;
  1256. }
  1257. }