parser_impl.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/parser/parser_impl.h"
  5. #include <cstdlib>
  6. #include "common/check.h"
  7. #include "llvm/ADT/Optional.h"
  8. #include "llvm/Support/FormatVariadic.h"
  9. #include "llvm/Support/raw_ostream.h"
  10. #include "toolchain/lexer/token_kind.h"
  11. #include "toolchain/lexer/tokenized_buffer.h"
  12. #include "toolchain/parser/parse_node_kind.h"
  13. #include "toolchain/parser/parse_tree.h"
  14. namespace Carbon {
  15. CARBON_DIAGNOSTIC(StackLimitExceeded, Error, "Exceeded recursion limit ({0})",
  16. int);
  17. // Manages the parser's stack depth, particularly decrementing on destruction.
  18. // This should only be instantiated through RETURN_IF_STACK_LIMITED.
  19. class ParseTree::Parser::ScopedStackStep {
  20. public:
  21. explicit ScopedStackStep(ParseTree::Parser* parser) : parser_(parser) {
  22. ++parser_->stack_depth_;
  23. }
  24. ~ScopedStackStep() { --parser_->stack_depth_; }
  25. auto VerifyUnderLimit() -> bool {
  26. if (parser_->stack_depth_ >= StackDepthLimit) {
  27. parser_->emitter_.Emit(*parser_->position_, StackLimitExceeded,
  28. ParseTree::StackDepthLimit);
  29. return false;
  30. }
  31. return true;
  32. }
  33. private:
  34. ParseTree::Parser* parser_;
  35. };
  36. // Encapsulates checking the stack and erroring if needed. This should be called
  37. // at the start of every parse function.
  38. #define RETURN_IF_STACK_LIMITED(error_return_expr) \
  39. ScopedStackStep scoped_stack_step(this); \
  40. if (!scoped_stack_step.VerifyUnderLimit()) { \
  41. return (error_return_expr); \
  42. }
  43. // A relative location for characters in errors.
  44. enum class RelativeLocation : int8_t {
  45. Around,
  46. After,
  47. Before,
  48. };
  49. // Adapts RelativeLocation for use with formatv.
  50. static auto operator<<(llvm::raw_ostream& out, RelativeLocation loc)
  51. -> llvm::raw_ostream& {
  52. switch (loc) {
  53. case RelativeLocation::Around:
  54. out << "around";
  55. break;
  56. case RelativeLocation::After:
  57. out << "after";
  58. break;
  59. case RelativeLocation::Before:
  60. out << "before";
  61. break;
  62. }
  63. return out;
  64. }
  65. CARBON_DIAGNOSTIC(ExpectedFunctionName, Error,
  66. "Expected function name after `fn` keyword.");
  67. CARBON_DIAGNOSTIC(ExpectedFunctionParams, Error,
  68. "Expected `(` after function name.");
  69. CARBON_DIAGNOSTIC(
  70. ExpectedFunctionBodyOrSemi, Error,
  71. "Expected function definition or `;` after function declaration.");
  72. CARBON_DIAGNOSTIC(ExpectedVariableName, Error,
  73. "Expected pattern in `var` declaration.");
  74. CARBON_DIAGNOSTIC(ExpectedParameterName, Error,
  75. "Expected parameter declaration.");
  76. CARBON_DIAGNOSTIC(ExpectedStructLiteralField, Error, "Expected {0}{1}{2}.",
  77. llvm::StringRef, llvm::StringRef, llvm::StringRef);
  78. CARBON_DIAGNOSTIC(UnrecognizedDeclaration, Error,
  79. "Unrecognized declaration introducer.");
  80. CARBON_DIAGNOSTIC(ExpectedCodeBlock, Error, "Expected braced code block.");
  81. CARBON_DIAGNOSTIC(ExpectedExpression, Error, "Expected expression.");
  82. CARBON_DIAGNOSTIC(ExpectedParenAfter, Error, "Expected `(` after `{0}`.",
  83. TokenKind);
  84. CARBON_DIAGNOSTIC(ExpectedCloseParen, Error, "Unexpected tokens before `)`.");
  85. CARBON_DIAGNOSTIC(ExpectedSemiAfterExpression, Error,
  86. "Expected `;` after expression.");
  87. CARBON_DIAGNOSTIC(ExpectedSemiAfter, Error, "Expected `;` after `{0}`.",
  88. TokenKind);
  89. CARBON_DIAGNOSTIC(ExpectedIdentifierAfterDot, Error,
  90. "Expected identifier after `.`.");
  91. CARBON_DIAGNOSTIC(UnexpectedTokenAfterListElement, Error,
  92. "Expected `,` or `{0}`.", TokenKind);
  93. CARBON_DIAGNOSTIC(BinaryOperatorRequiresWhitespace, Error,
  94. "Whitespace missing {0} binary operator.", RelativeLocation);
  95. CARBON_DIAGNOSTIC(UnaryOperatorHasWhitespace, Error,
  96. "Whitespace is not allowed {0} this unary operator.",
  97. RelativeLocation);
  98. CARBON_DIAGNOSTIC(UnaryOperatorRequiresWhitespace, Error,
  99. "Whitespace is required {0} this unary operator.",
  100. RelativeLocation);
  101. CARBON_DIAGNOSTIC(
  102. OperatorRequiresParentheses, Error,
  103. "Parentheses are required to disambiguate operator precedence.");
  104. ParseTree::Parser::Parser(ParseTree& tree_arg, TokenizedBuffer& tokens_arg,
  105. TokenDiagnosticEmitter& emitter)
  106. : tree_(tree_arg),
  107. tokens_(tokens_arg),
  108. emitter_(emitter),
  109. position_(tokens_.tokens().begin()),
  110. end_(tokens_.tokens().end()) {
  111. CHECK(std::find_if(position_, end_,
  112. [&](TokenizedBuffer::Token t) {
  113. return tokens_.GetKind(t) == TokenKind::EndOfFile();
  114. }) != end_)
  115. << "No EndOfFileToken in token buffer.";
  116. }
  117. auto ParseTree::Parser::Parse(TokenizedBuffer& tokens,
  118. TokenDiagnosticEmitter& emitter) -> ParseTree {
  119. ParseTree tree(tokens);
  120. // We expect to have a 1:1 correspondence between tokens and tree nodes, so
  121. // reserve the space we expect to need here to avoid allocation and copying
  122. // overhead.
  123. tree.node_impls_.reserve(tokens.size());
  124. Parser parser(tree, tokens, emitter);
  125. while (!parser.AtEndOfFile()) {
  126. if (!parser.ParseDeclaration()) {
  127. // We don't have an enclosing parse tree node to mark as erroneous, so
  128. // just mark the tree as a whole.
  129. tree.has_errors_ = true;
  130. }
  131. }
  132. parser.AddLeafNode(ParseNodeKind::FileEnd(), *parser.position_);
  133. CHECK(tree.Verify()) << "Parse tree built but does not verify!";
  134. return tree;
  135. }
  136. auto ParseTree::Parser::Consume(TokenKind kind) -> TokenizedBuffer::Token {
  137. CHECK(kind != TokenKind::EndOfFile()) << "Cannot consume the EOF token!";
  138. CHECK(NextTokenIs(kind)) << "The current token is the wrong kind!";
  139. TokenizedBuffer::Token t = *position_;
  140. ++position_;
  141. CHECK(position_ != end_)
  142. << "Reached end of tokens without finding EOF token.";
  143. return t;
  144. }
  145. auto ParseTree::Parser::ConsumeIf(TokenKind kind)
  146. -> llvm::Optional<TokenizedBuffer::Token> {
  147. if (!NextTokenIs(kind)) {
  148. return {};
  149. }
  150. return Consume(kind);
  151. }
  152. auto ParseTree::Parser::AddLeafNode(ParseNodeKind kind,
  153. TokenizedBuffer::Token token) -> Node {
  154. Node n(tree_.node_impls_.size());
  155. tree_.node_impls_.push_back(NodeImpl(kind, token, /*subtree_size_arg=*/1));
  156. return n;
  157. }
  158. auto ParseTree::Parser::ConsumeAndAddLeafNodeIf(TokenKind t_kind,
  159. ParseNodeKind n_kind)
  160. -> llvm::Optional<Node> {
  161. auto t = ConsumeIf(t_kind);
  162. if (!t) {
  163. return {};
  164. }
  165. return AddLeafNode(n_kind, *t);
  166. }
  167. auto ParseTree::Parser::MarkNodeError(Node n) -> void {
  168. tree_.node_impls_[n.index_].has_error = true;
  169. tree_.has_errors_ = true;
  170. }
  171. // A marker for the start of a node's subtree.
  172. //
  173. // This is used to track the size of the node's subtree. It can be used
  174. // repeatedly if multiple subtrees start at the same position.
  175. struct ParseTree::Parser::SubtreeStart {
  176. int tree_size;
  177. };
  178. auto ParseTree::Parser::GetSubtreeStartPosition() -> SubtreeStart {
  179. return {static_cast<int>(tree_.node_impls_.size())};
  180. }
  181. auto ParseTree::Parser::AddNode(ParseNodeKind n_kind, TokenizedBuffer::Token t,
  182. SubtreeStart start, bool has_error) -> Node {
  183. // The size of the subtree is the change in size from when we started this
  184. // subtree to now, but including the node we're about to add.
  185. int tree_stop_size = static_cast<int>(tree_.node_impls_.size()) + 1;
  186. int subtree_size = tree_stop_size - start.tree_size;
  187. Node n(tree_.node_impls_.size());
  188. tree_.node_impls_.push_back(NodeImpl(n_kind, t, subtree_size));
  189. if (has_error) {
  190. MarkNodeError(n);
  191. }
  192. return n;
  193. }
  194. auto ParseTree::Parser::SkipMatchingGroup() -> bool {
  195. TokenizedBuffer::Token t = *position_;
  196. TokenKind t_kind = tokens_.GetKind(t);
  197. if (!t_kind.IsOpeningSymbol()) {
  198. return false;
  199. }
  200. SkipTo(tokens_.GetMatchedClosingToken(t));
  201. Consume(t_kind.GetClosingSymbol());
  202. return true;
  203. }
  204. auto ParseTree::Parser::SkipTo(TokenizedBuffer::Token t) -> void {
  205. CHECK(t >= *position_) << "Tried to skip backwards.";
  206. position_ = TokenizedBuffer::TokenIterator(t);
  207. CHECK(position_ != end_) << "Skipped past EOF.";
  208. }
  209. auto ParseTree::Parser::FindNextOf(
  210. std::initializer_list<TokenKind> desired_kinds)
  211. -> llvm::Optional<TokenizedBuffer::Token> {
  212. auto new_position = position_;
  213. while (true) {
  214. TokenizedBuffer::Token token = *new_position;
  215. TokenKind kind = tokens_.GetKind(token);
  216. if (kind.IsOneOf(desired_kinds)) {
  217. return token;
  218. }
  219. // Step to the next token at the current bracketing level.
  220. if (kind.IsClosingSymbol() || kind == TokenKind::EndOfFile()) {
  221. // There are no more tokens at this level.
  222. return llvm::None;
  223. } else if (kind.IsOpeningSymbol()) {
  224. new_position =
  225. TokenizedBuffer::TokenIterator(tokens_.GetMatchedClosingToken(token));
  226. // Advance past the closing token.
  227. ++new_position;
  228. } else {
  229. ++new_position;
  230. }
  231. }
  232. }
  233. auto ParseTree::Parser::SkipPastLikelyEnd(TokenizedBuffer::Token skip_root,
  234. SemiHandler on_semi)
  235. -> llvm::Optional<Node> {
  236. if (AtEndOfFile()) {
  237. return llvm::None;
  238. }
  239. TokenizedBuffer::Line root_line = tokens_.GetLine(skip_root);
  240. int root_line_indent = tokens_.GetIndentColumnNumber(root_line);
  241. // We will keep scanning through tokens on the same line as the root or
  242. // lines with greater indentation than root's line.
  243. auto is_same_line_or_indent_greater_than_root =
  244. [&](TokenizedBuffer::Token t) {
  245. TokenizedBuffer::Line l = tokens_.GetLine(t);
  246. if (l == root_line) {
  247. return true;
  248. }
  249. return tokens_.GetIndentColumnNumber(l) > root_line_indent;
  250. };
  251. do {
  252. if (NextTokenKind() == TokenKind::CloseCurlyBrace()) {
  253. // Immediately bail out if we hit an unmatched close curly, this will
  254. // pop us up a level of the syntax grouping.
  255. return llvm::None;
  256. }
  257. // We assume that a semicolon is always intended to be the end of the
  258. // current construct.
  259. if (auto semi = ConsumeIf(TokenKind::Semi())) {
  260. return on_semi(*semi);
  261. }
  262. // Skip over any matching group of tokens_.
  263. if (SkipMatchingGroup()) {
  264. continue;
  265. }
  266. // Otherwise just step forward one token.
  267. Consume(NextTokenKind());
  268. } while (!AtEndOfFile() &&
  269. is_same_line_or_indent_greater_than_root(*position_));
  270. return llvm::None;
  271. }
  272. auto ParseTree::Parser::ParseCloseParen(TokenizedBuffer::Token open_paren,
  273. ParseNodeKind kind)
  274. -> llvm::Optional<Node> {
  275. if (auto close_paren =
  276. ConsumeAndAddLeafNodeIf(TokenKind::CloseParen(), kind)) {
  277. return close_paren;
  278. }
  279. // TODO: Include the location of the matching open_paren in the diagnostic.
  280. emitter_.Emit(*position_, ExpectedCloseParen);
  281. SkipTo(tokens_.GetMatchedClosingToken(open_paren));
  282. AddLeafNode(kind, Consume(TokenKind::CloseParen()));
  283. return llvm::None;
  284. }
  285. template <typename ListElementParser, typename ListCompletionHandler>
  286. auto ParseTree::Parser::ParseList(TokenKind open, TokenKind close,
  287. ListElementParser list_element_parser,
  288. ParseNodeKind comma_kind,
  289. ListCompletionHandler list_handler,
  290. bool allow_trailing_comma)
  291. -> llvm::Optional<Node> {
  292. // `(` element-list[opt] `)`
  293. //
  294. // element-list ::= element
  295. // ::= element `,` element-list
  296. TokenizedBuffer::Token open_paren = Consume(open);
  297. bool has_errors = false;
  298. bool any_commas = false;
  299. int64_t num_elements = 0;
  300. // Parse elements, if any are specified.
  301. if (!NextTokenIs(close)) {
  302. while (true) {
  303. bool element_error = !list_element_parser();
  304. has_errors |= element_error;
  305. ++num_elements;
  306. if (!NextTokenIsOneOf({close, TokenKind::Comma()})) {
  307. if (!element_error) {
  308. emitter_.Emit(*position_, UnexpectedTokenAfterListElement, close);
  309. }
  310. has_errors = true;
  311. auto end_of_element = FindNextOf({TokenKind::Comma(), close});
  312. // The lexer guarantees that parentheses are balanced.
  313. CHECK(end_of_element) << "missing matching `)` for `(`";
  314. SkipTo(*end_of_element);
  315. }
  316. if (NextTokenIs(close)) {
  317. break;
  318. }
  319. AddLeafNode(comma_kind, Consume(TokenKind::Comma()));
  320. any_commas = true;
  321. if (allow_trailing_comma && NextTokenIs(close)) {
  322. break;
  323. }
  324. }
  325. }
  326. bool is_single_item = num_elements == 1 && !any_commas;
  327. return list_handler(open_paren, is_single_item, Consume(close), has_errors);
  328. }
  329. auto ParseTree::Parser::ParsePattern(PatternKind kind) -> llvm::Optional<Node> {
  330. RETURN_IF_STACK_LIMITED(llvm::None);
  331. if (NextTokenIs(TokenKind::Identifier()) &&
  332. tokens_.GetKind(*(position_ + 1)) == TokenKind::Colon()) {
  333. // identifier `:` type
  334. auto start = GetSubtreeStartPosition();
  335. AddLeafNode(ParseNodeKind::DeclaredName(),
  336. Consume(TokenKind::Identifier()));
  337. auto colon = Consume(TokenKind::Colon());
  338. auto type = ParseType();
  339. return AddNode(ParseNodeKind::PatternBinding(), colon, start,
  340. /*has_error=*/!type);
  341. }
  342. switch (kind) {
  343. case PatternKind::Parameter:
  344. emitter_.Emit(*position_, ExpectedParameterName);
  345. break;
  346. case PatternKind::Variable:
  347. emitter_.Emit(*position_, ExpectedVariableName);
  348. break;
  349. }
  350. return llvm::None;
  351. }
  352. auto ParseTree::Parser::ParseFunctionParameter() -> llvm::Optional<Node> {
  353. RETURN_IF_STACK_LIMITED(llvm::None);
  354. return ParsePattern(PatternKind::Parameter);
  355. }
  356. auto ParseTree::Parser::ParseFunctionSignature() -> bool {
  357. RETURN_IF_STACK_LIMITED(false);
  358. auto start = GetSubtreeStartPosition();
  359. auto params = ParseParenList(
  360. [&] { return ParseFunctionParameter(); },
  361. ParseNodeKind::ParameterListComma(),
  362. [&](TokenizedBuffer::Token open_paren, bool /*is_single_item*/,
  363. TokenizedBuffer::Token close_paren, bool has_errors) {
  364. AddLeafNode(ParseNodeKind::ParameterListEnd(), close_paren);
  365. return AddNode(ParseNodeKind::ParameterList(), open_paren, start,
  366. has_errors);
  367. });
  368. auto start_return_type = GetSubtreeStartPosition();
  369. if (auto arrow = ConsumeIf(TokenKind::MinusGreater())) {
  370. auto return_type = ParseType();
  371. AddNode(ParseNodeKind::ReturnType(), *arrow, start_return_type,
  372. /*has_error=*/!return_type);
  373. if (!return_type) {
  374. return false;
  375. }
  376. }
  377. return params.hasValue();
  378. }
  379. auto ParseTree::Parser::ParseCodeBlock() -> llvm::Optional<Node> {
  380. RETURN_IF_STACK_LIMITED(llvm::None);
  381. llvm::Optional<TokenizedBuffer::Token> maybe_open_curly =
  382. ConsumeIf(TokenKind::OpenCurlyBrace());
  383. if (!maybe_open_curly) {
  384. // Recover by parsing a single statement.
  385. emitter_.Emit(*position_, ExpectedCodeBlock);
  386. return ParseStatement();
  387. }
  388. TokenizedBuffer::Token open_curly = *maybe_open_curly;
  389. auto start = GetSubtreeStartPosition();
  390. bool has_errors = false;
  391. // Loop over all the different possibly nested elements in the code block.
  392. while (!NextTokenIs(TokenKind::CloseCurlyBrace())) {
  393. if (!ParseStatement()) {
  394. // We detected and diagnosed an error of some kind. We can trivially skip
  395. // to the actual close curly brace from here.
  396. // FIXME: It would be better to skip to the next semicolon, or the next
  397. // token at the start of a line with the same indent as this one.
  398. SkipTo(tokens_.GetMatchedClosingToken(open_curly));
  399. has_errors = true;
  400. break;
  401. }
  402. }
  403. // We always reach here having set our position in the token stream to the
  404. // close curly brace.
  405. AddLeafNode(ParseNodeKind::CodeBlockEnd(),
  406. Consume(TokenKind::CloseCurlyBrace()));
  407. return AddNode(ParseNodeKind::CodeBlock(), open_curly, start, has_errors);
  408. }
  409. auto ParseTree::Parser::ParseFunctionDeclaration() -> Node {
  410. TokenizedBuffer::Token function_intro_token = Consume(TokenKind::Fn());
  411. auto start = GetSubtreeStartPosition();
  412. auto add_error_function_node = [&] {
  413. return AddNode(ParseNodeKind::FunctionDeclaration(), function_intro_token,
  414. start, /*has_error=*/true);
  415. };
  416. RETURN_IF_STACK_LIMITED(add_error_function_node());
  417. auto handle_semi_in_error_recovery = [&](TokenizedBuffer::Token semi) {
  418. return AddLeafNode(ParseNodeKind::DeclarationEnd(), semi);
  419. };
  420. auto name_n = ConsumeAndAddLeafNodeIf(TokenKind::Identifier(),
  421. ParseNodeKind::DeclaredName());
  422. if (!name_n) {
  423. emitter_.Emit(*position_, ExpectedFunctionName);
  424. // FIXME: We could change the lexer to allow us to synthesize certain
  425. // kinds of tokens and try to "recover" here, but unclear that this is
  426. // really useful.
  427. SkipPastLikelyEnd(function_intro_token, handle_semi_in_error_recovery);
  428. return add_error_function_node();
  429. }
  430. TokenizedBuffer::Token open_paren = *position_;
  431. if (tokens_.GetKind(open_paren) != TokenKind::OpenParen()) {
  432. emitter_.Emit(open_paren, ExpectedFunctionParams);
  433. SkipPastLikelyEnd(function_intro_token, handle_semi_in_error_recovery);
  434. return add_error_function_node();
  435. }
  436. TokenizedBuffer::Token close_paren =
  437. tokens_.GetMatchedClosingToken(open_paren);
  438. if (!ParseFunctionSignature()) {
  439. // Don't try to parse more of the function declaration, but consume a
  440. // declaration ending semicolon if found (without going to a new line).
  441. SkipPastLikelyEnd(function_intro_token, handle_semi_in_error_recovery);
  442. return add_error_function_node();
  443. }
  444. // See if we should parse a definition which is represented as a code block.
  445. if (NextTokenIs(TokenKind::OpenCurlyBrace())) {
  446. if (!ParseCodeBlock()) {
  447. return add_error_function_node();
  448. }
  449. } else if (!ConsumeAndAddLeafNodeIf(TokenKind::Semi(),
  450. ParseNodeKind::DeclarationEnd())) {
  451. emitter_.Emit(*position_, ExpectedFunctionBodyOrSemi);
  452. if (tokens_.GetLine(*position_) == tokens_.GetLine(close_paren)) {
  453. // Only need to skip if we've not already found a new line.
  454. SkipPastLikelyEnd(function_intro_token, handle_semi_in_error_recovery);
  455. }
  456. return add_error_function_node();
  457. }
  458. // Successfully parsed the function, add that node.
  459. return AddNode(ParseNodeKind::FunctionDeclaration(), function_intro_token,
  460. start);
  461. }
  462. auto ParseTree::Parser::ParseVariableDeclaration() -> Node {
  463. // `var` pattern [= expression] `;`
  464. TokenizedBuffer::Token var_token = Consume(TokenKind::Var());
  465. auto start = GetSubtreeStartPosition();
  466. RETURN_IF_STACK_LIMITED(AddNode(ParseNodeKind::VariableDeclaration(),
  467. var_token, start,
  468. /*has_error=*/true));
  469. auto pattern = ParsePattern(PatternKind::Variable);
  470. if (!pattern) {
  471. if (auto after_pattern =
  472. FindNextOf({TokenKind::Equal(), TokenKind::Semi()})) {
  473. SkipTo(*after_pattern);
  474. }
  475. }
  476. auto start_init = GetSubtreeStartPosition();
  477. if (auto equal_token = ConsumeIf(TokenKind::Equal())) {
  478. auto init = ParseExpression();
  479. AddNode(ParseNodeKind::VariableInitializer(), *equal_token, start_init,
  480. /*has_error=*/!init);
  481. }
  482. auto semi = ConsumeAndAddLeafNodeIf(TokenKind::Semi(),
  483. ParseNodeKind::DeclarationEnd());
  484. if (!semi) {
  485. emitter_.Emit(*position_, ExpectedSemiAfterExpression);
  486. SkipPastLikelyEnd(var_token, [&](TokenizedBuffer::Token semi) {
  487. return AddLeafNode(ParseNodeKind::DeclarationEnd(), semi);
  488. });
  489. }
  490. return AddNode(ParseNodeKind::VariableDeclaration(), var_token, start,
  491. /*has_error=*/!pattern || !semi);
  492. }
  493. auto ParseTree::Parser::ParseEmptyDeclaration() -> Node {
  494. return AddLeafNode(ParseNodeKind::EmptyDeclaration(),
  495. Consume(TokenKind::Semi()));
  496. }
  497. auto ParseTree::Parser::ParseDeclaration() -> llvm::Optional<Node> {
  498. RETURN_IF_STACK_LIMITED(llvm::None);
  499. switch (NextTokenKind()) {
  500. case TokenKind::Fn():
  501. return ParseFunctionDeclaration();
  502. case TokenKind::Var():
  503. return ParseVariableDeclaration();
  504. case TokenKind::Semi():
  505. return ParseEmptyDeclaration();
  506. case TokenKind::EndOfFile():
  507. return llvm::None;
  508. default:
  509. // Errors are handled outside the switch.
  510. break;
  511. }
  512. // We didn't recognize an introducer for a valid declaration.
  513. emitter_.Emit(*position_, UnrecognizedDeclaration);
  514. // Skip forward past any end of a declaration we simply didn't understand so
  515. // that we can find the start of the next declaration or the end of a scope.
  516. if (auto found_semi_n =
  517. SkipPastLikelyEnd(*position_, [&](TokenizedBuffer::Token semi) {
  518. return AddLeafNode(ParseNodeKind::EmptyDeclaration(), semi);
  519. })) {
  520. MarkNodeError(*found_semi_n);
  521. return *found_semi_n;
  522. }
  523. // Nothing, not even a semicolon found.
  524. return llvm::None;
  525. }
  526. auto ParseTree::Parser::ParseParenExpression() -> llvm::Optional<Node> {
  527. RETURN_IF_STACK_LIMITED(llvm::None);
  528. // parenthesized-expression ::= `(` expression `)`
  529. // tuple-literal ::= `(` `)`
  530. // ::= `(` expression `,` [expression-list [`,`]] `)`
  531. //
  532. // Parse the union of these, `(` [expression-list [`,`]] `)`, and work out
  533. // whether it's a tuple or a parenthesized expression afterwards.
  534. auto start = GetSubtreeStartPosition();
  535. return ParseParenList(
  536. [&] { return ParseExpression(); }, ParseNodeKind::TupleLiteralComma(),
  537. [&](TokenizedBuffer::Token open_paren, bool is_single_item,
  538. TokenizedBuffer::Token close_paren, bool has_arg_errors) {
  539. AddLeafNode(is_single_item ? ParseNodeKind::ParenExpressionEnd()
  540. : ParseNodeKind::TupleLiteralEnd(),
  541. close_paren);
  542. return AddNode(is_single_item ? ParseNodeKind::ParenExpression()
  543. : ParseNodeKind::TupleLiteral(),
  544. open_paren, start, has_arg_errors);
  545. },
  546. /*allow_trailing_comma=*/true);
  547. }
  548. auto ParseTree::Parser::ParseBraceExpression() -> llvm::Optional<Node> {
  549. RETURN_IF_STACK_LIMITED(llvm::None);
  550. // braced-expression ::= `{` [field-value-list] `}`
  551. // ::= `{` field-type-list `}`
  552. // field-value-list ::= field-value [`,`]
  553. // ::= field-value `,` field-value-list
  554. // field-value ::= `.` identifier `=` expression
  555. // field-type-list ::= field-type [`,`]
  556. // ::= field-type `,` field-type-list
  557. // field-type ::= `.` identifier `:` type
  558. //
  559. // Note that `{` `}` is the first form (an empty struct), but that an empty
  560. // struct value also behaves as an empty struct type.
  561. auto start = GetSubtreeStartPosition();
  562. enum Kind { Unknown, Value, Type };
  563. Kind kind = Unknown;
  564. return ParseList(
  565. TokenKind::OpenCurlyBrace(), TokenKind::CloseCurlyBrace(),
  566. [&]() -> llvm::Optional<Node> {
  567. auto start_elem = GetSubtreeStartPosition();
  568. auto diagnose_invalid_syntax = [&] {
  569. bool can_be_type = kind != Value;
  570. bool can_be_value = kind != Type;
  571. emitter_.Emit(*position_, ExpectedStructLiteralField,
  572. can_be_type ? "`.field: type`" : "",
  573. (can_be_type && can_be_value) ? " or " : "",
  574. can_be_value ? "`.field = value`" : "");
  575. return llvm::None;
  576. };
  577. if (!NextTokenIs(TokenKind::Period())) {
  578. return diagnose_invalid_syntax();
  579. }
  580. auto designator = ParseDesignatorExpression(
  581. start_elem, ParseNodeKind::StructFieldDesignator(),
  582. /*has_errors=*/false);
  583. if (!designator) {
  584. auto recovery_pos = FindNextOf(
  585. {TokenKind::Equal(), TokenKind::Colon(), TokenKind::Comma()});
  586. if (!recovery_pos ||
  587. tokens_.GetKind(*recovery_pos) == TokenKind::Comma()) {
  588. return llvm::None;
  589. }
  590. SkipTo(*recovery_pos);
  591. }
  592. // Work out the kind of this element
  593. Kind elem_kind = (NextTokenIs(TokenKind::Equal()) ? Value
  594. : NextTokenIs(TokenKind::Colon()) ? Type
  595. : Unknown);
  596. if (elem_kind == Unknown || (kind != Unknown && elem_kind != kind)) {
  597. return diagnose_invalid_syntax();
  598. }
  599. kind = elem_kind;
  600. // Struct type fields and value fields use the same grammar except that
  601. // one has a `:` separator and the other has an `=` separator.
  602. auto equal_or_colon_token =
  603. Consume(kind == Type ? TokenKind::Colon() : TokenKind::Equal());
  604. auto type_or_value = ParseExpression();
  605. return AddNode(kind == Type ? ParseNodeKind::StructFieldType()
  606. : ParseNodeKind::StructFieldValue(),
  607. equal_or_colon_token, start_elem,
  608. /*has_error=*/!designator || !type_or_value);
  609. },
  610. ParseNodeKind::StructComma(),
  611. [&](TokenizedBuffer::Token open_brace, bool /*is_single_item*/,
  612. TokenizedBuffer::Token close_brace, bool has_errors) {
  613. AddLeafNode(ParseNodeKind::StructEnd(), close_brace);
  614. return AddNode(kind == Type ? ParseNodeKind::StructTypeLiteral()
  615. : ParseNodeKind::StructLiteral(),
  616. open_brace, start, has_errors);
  617. },
  618. /*allow_trailing_comma=*/true);
  619. }
  620. auto ParseTree::Parser::ParsePrimaryExpression() -> llvm::Optional<Node> {
  621. RETURN_IF_STACK_LIMITED(llvm::None);
  622. llvm::Optional<ParseNodeKind> kind;
  623. switch (NextTokenKind()) {
  624. case TokenKind::Identifier():
  625. kind = ParseNodeKind::NameReference();
  626. break;
  627. case TokenKind::IntegerLiteral():
  628. case TokenKind::RealLiteral():
  629. case TokenKind::StringLiteral():
  630. case TokenKind::IntegerTypeLiteral():
  631. case TokenKind::UnsignedIntegerTypeLiteral():
  632. case TokenKind::FloatingPointTypeLiteral():
  633. kind = ParseNodeKind::Literal();
  634. break;
  635. case TokenKind::OpenParen():
  636. return ParseParenExpression();
  637. case TokenKind::OpenCurlyBrace():
  638. return ParseBraceExpression();
  639. default:
  640. emitter_.Emit(*position_, ExpectedExpression);
  641. return llvm::None;
  642. }
  643. return AddLeafNode(*kind, Consume(NextTokenKind()));
  644. }
  645. auto ParseTree::Parser::ParseDesignatorExpression(SubtreeStart start,
  646. ParseNodeKind kind,
  647. bool has_errors)
  648. -> llvm::Optional<Node> {
  649. // `.` identifier
  650. auto dot = Consume(TokenKind::Period());
  651. auto name = ConsumeIf(TokenKind::Identifier());
  652. if (name) {
  653. AddLeafNode(ParseNodeKind::DesignatedName(), *name);
  654. } else {
  655. emitter_.Emit(*position_, ExpectedIdentifierAfterDot);
  656. // If we see a keyword, assume it was intended to be the designated name.
  657. // TODO: Should keywords be valid in designators?
  658. if (NextTokenKind().IsKeyword()) {
  659. name = Consume(NextTokenKind());
  660. auto name_node = AddLeafNode(ParseNodeKind::DesignatedName(), *name);
  661. MarkNodeError(name_node);
  662. } else {
  663. has_errors = true;
  664. }
  665. }
  666. Node result = AddNode(kind, dot, start, has_errors);
  667. return name ? result : llvm::Optional<Node>();
  668. }
  669. auto ParseTree::Parser::ParseCallExpression(SubtreeStart start, bool has_errors)
  670. -> llvm::Optional<Node> {
  671. RETURN_IF_STACK_LIMITED(llvm::None);
  672. // `(` expression-list[opt] `)`
  673. //
  674. // expression-list ::= expression
  675. // ::= expression `,` expression-list
  676. return ParseParenList(
  677. [&] { return ParseExpression(); }, ParseNodeKind::CallExpressionComma(),
  678. [&](TokenizedBuffer::Token open_paren, bool /*is_single_item*/,
  679. TokenizedBuffer::Token close_paren, bool has_arg_errors) {
  680. AddLeafNode(ParseNodeKind::CallExpressionEnd(), close_paren);
  681. return AddNode(ParseNodeKind::CallExpression(), open_paren, start,
  682. has_errors || has_arg_errors);
  683. });
  684. }
  685. auto ParseTree::Parser::ParsePostfixExpression() -> llvm::Optional<Node> {
  686. RETURN_IF_STACK_LIMITED(llvm::None);
  687. auto start = GetSubtreeStartPosition();
  688. llvm::Optional<Node> expression = ParsePrimaryExpression();
  689. TokenizedBuffer::TokenIterator last_position = position_;
  690. while (true) {
  691. switch (NextTokenKind()) {
  692. case TokenKind::Period():
  693. expression = ParseDesignatorExpression(
  694. start, ParseNodeKind::DesignatorExpression(), !expression);
  695. break;
  696. case TokenKind::OpenParen():
  697. expression = ParseCallExpression(start, !expression);
  698. break;
  699. default:
  700. return expression;
  701. }
  702. // This is subject to an infinite loop if a child call fails, so monitor for
  703. // stalling.
  704. if (last_position == position_) {
  705. CHECK(expression == llvm::None);
  706. return expression;
  707. }
  708. last_position = position_;
  709. }
  710. }
  711. // Determines whether the given token is considered to be the start of an
  712. // operand according to the rules for infix operator parsing.
  713. static auto IsAssumedStartOfOperand(TokenKind kind) -> bool {
  714. return kind.IsOneOf({TokenKind::OpenParen(), TokenKind::Identifier(),
  715. TokenKind::IntegerLiteral(), TokenKind::RealLiteral(),
  716. TokenKind::StringLiteral()});
  717. }
  718. // Determines whether the given token is considered to be the end of an operand
  719. // according to the rules for infix operator parsing.
  720. static auto IsAssumedEndOfOperand(TokenKind kind) -> bool {
  721. return kind.IsOneOf({TokenKind::CloseParen(), TokenKind::CloseCurlyBrace(),
  722. TokenKind::CloseSquareBracket(), TokenKind::Identifier(),
  723. TokenKind::IntegerLiteral(), TokenKind::RealLiteral(),
  724. TokenKind::StringLiteral()});
  725. }
  726. // Determines whether the given token could possibly be the start of an operand.
  727. // This is conservatively correct, and will never incorrectly return `false`,
  728. // but can incorrectly return `true`.
  729. static auto IsPossibleStartOfOperand(TokenKind kind) -> bool {
  730. return !kind.IsOneOf({TokenKind::CloseParen(), TokenKind::CloseCurlyBrace(),
  731. TokenKind::CloseSquareBracket(), TokenKind::Comma(),
  732. TokenKind::Semi(), TokenKind::Colon()});
  733. }
  734. auto ParseTree::Parser::IsLexicallyValidInfixOperator() -> bool {
  735. CHECK(!AtEndOfFile()) << "Expected an operator token.";
  736. bool leading_space = tokens_.HasLeadingWhitespace(*position_);
  737. bool trailing_space = tokens_.HasTrailingWhitespace(*position_);
  738. // If there's whitespace on both sides, it's an infix operator.
  739. if (leading_space && trailing_space) {
  740. return true;
  741. }
  742. // If there's whitespace on exactly one side, it's not an infix operator.
  743. if (leading_space || trailing_space) {
  744. return false;
  745. }
  746. // Otherwise, for an infix operator, the preceding token must be any close
  747. // bracket, identifier, or literal and the next token must be an open paren,
  748. // identifier, or literal.
  749. if (position_ == tokens_.tokens().begin() ||
  750. !IsAssumedEndOfOperand(tokens_.GetKind(*(position_ - 1))) ||
  751. !IsAssumedStartOfOperand(tokens_.GetKind(*(position_ + 1)))) {
  752. return false;
  753. }
  754. return true;
  755. }
  756. auto ParseTree::Parser::DiagnoseOperatorFixity(OperatorFixity fixity) -> void {
  757. bool is_valid_as_infix = IsLexicallyValidInfixOperator();
  758. if (fixity == OperatorFixity::Infix) {
  759. // Infix operators must satisfy the infix operator rules.
  760. if (!is_valid_as_infix) {
  761. emitter_.Emit(*position_, BinaryOperatorRequiresWhitespace,
  762. tokens_.HasLeadingWhitespace(*position_)
  763. ? RelativeLocation::After
  764. : (tokens_.HasTrailingWhitespace(*position_)
  765. ? RelativeLocation::Before
  766. : RelativeLocation::Around));
  767. }
  768. } else {
  769. bool prefix = fixity == OperatorFixity::Prefix;
  770. // Whitespace is not permitted between a symbolic pre/postfix operator and
  771. // its operand.
  772. if (NextTokenKind().IsSymbol() &&
  773. (prefix ? tokens_.HasTrailingWhitespace(*position_)
  774. : tokens_.HasLeadingWhitespace(*position_))) {
  775. emitter_.Emit(
  776. *position_, UnaryOperatorHasWhitespace,
  777. prefix ? RelativeLocation::After : RelativeLocation::Before);
  778. }
  779. // Pre/postfix operators must not satisfy the infix operator rules.
  780. if (is_valid_as_infix) {
  781. emitter_.Emit(
  782. *position_, UnaryOperatorRequiresWhitespace,
  783. prefix ? RelativeLocation::Before : RelativeLocation::After);
  784. }
  785. }
  786. }
  787. auto ParseTree::Parser::IsTrailingOperatorInfix() -> bool {
  788. if (AtEndOfFile()) {
  789. return false;
  790. }
  791. // An operator that follows the infix operator rules is parsed as
  792. // infix, unless the next token means that it can't possibly be.
  793. if (IsLexicallyValidInfixOperator() &&
  794. IsPossibleStartOfOperand(tokens_.GetKind(*(position_ + 1)))) {
  795. return true;
  796. }
  797. // A trailing operator with leading whitespace that's not valid as infix is
  798. // not valid at all. If the next token looks like the start of an operand,
  799. // then parse as infix, otherwise as postfix. Either way we'll produce a
  800. // diagnostic later on.
  801. if (tokens_.HasLeadingWhitespace(*position_) &&
  802. IsAssumedStartOfOperand(tokens_.GetKind(*(position_ + 1)))) {
  803. return true;
  804. }
  805. return false;
  806. }
  807. auto ParseTree::Parser::ParseOperatorExpression(
  808. PrecedenceGroup ambient_precedence) -> llvm::Optional<Node> {
  809. RETURN_IF_STACK_LIMITED(llvm::None);
  810. auto start = GetSubtreeStartPosition();
  811. llvm::Optional<Node> lhs;
  812. PrecedenceGroup lhs_precedence = PrecedenceGroup::ForPostfixExpression();
  813. // Check for a prefix operator.
  814. if (auto operator_precedence = PrecedenceGroup::ForLeading(NextTokenKind());
  815. !operator_precedence) {
  816. lhs = ParsePostfixExpression();
  817. } else {
  818. if (PrecedenceGroup::GetPriority(ambient_precedence,
  819. *operator_precedence) !=
  820. OperatorPriority::RightFirst) {
  821. // The precedence rules don't permit this prefix operator in this
  822. // context. Diagnose this, but carry on and parse it anyway.
  823. emitter_.Emit(*position_, OperatorRequiresParentheses);
  824. } else {
  825. // Check that this operator follows the proper whitespace rules.
  826. DiagnoseOperatorFixity(OperatorFixity::Prefix);
  827. }
  828. auto operator_token = Consume(NextTokenKind());
  829. bool has_errors = !ParseOperatorExpression(*operator_precedence);
  830. lhs = AddNode(ParseNodeKind::PrefixOperator(), operator_token, start,
  831. has_errors);
  832. lhs_precedence = *operator_precedence;
  833. }
  834. // Consume a sequence of infix and postfix operators.
  835. while (auto trailing_operator = PrecedenceGroup::ForTrailing(
  836. NextTokenKind(), IsTrailingOperatorInfix())) {
  837. auto [operator_precedence, is_binary] = *trailing_operator;
  838. // FIXME: If this operator is ambiguous with either the ambient precedence
  839. // or the LHS precedence, and there's a variant with a different fixity
  840. // that would work, use that one instead for error recovery.
  841. if (PrecedenceGroup::GetPriority(ambient_precedence, operator_precedence) !=
  842. OperatorPriority::RightFirst) {
  843. // The precedence rules don't permit this operator in this context. Try
  844. // again in the enclosing expression context.
  845. return lhs;
  846. }
  847. if (PrecedenceGroup::GetPriority(lhs_precedence, operator_precedence) !=
  848. OperatorPriority::LeftFirst) {
  849. // Either the LHS operator and this operator are ambiguous, or the
  850. // LHS operaor is a unary operator that can't be nested within
  851. // this operator. Either way, parentheses are required.
  852. emitter_.Emit(*position_, OperatorRequiresParentheses);
  853. lhs = llvm::None;
  854. } else {
  855. DiagnoseOperatorFixity(is_binary ? OperatorFixity::Infix
  856. : OperatorFixity::Postfix);
  857. }
  858. auto operator_token = Consume(NextTokenKind());
  859. if (is_binary) {
  860. auto rhs = ParseOperatorExpression(operator_precedence);
  861. lhs = AddNode(ParseNodeKind::InfixOperator(), operator_token, start,
  862. /*has_error=*/!lhs || !rhs);
  863. } else {
  864. lhs = AddNode(ParseNodeKind::PostfixOperator(), operator_token, start,
  865. /*has_error=*/!lhs);
  866. }
  867. lhs_precedence = operator_precedence;
  868. }
  869. return lhs;
  870. }
  871. auto ParseTree::Parser::ParseExpression() -> llvm::Optional<Node> {
  872. RETURN_IF_STACK_LIMITED(llvm::None);
  873. return ParseOperatorExpression(PrecedenceGroup::ForTopLevelExpression());
  874. }
  875. auto ParseTree::Parser::ParseType() -> llvm::Optional<Node> {
  876. RETURN_IF_STACK_LIMITED(llvm::None);
  877. return ParseOperatorExpression(PrecedenceGroup::ForType());
  878. }
  879. auto ParseTree::Parser::ParseExpressionStatement() -> llvm::Optional<Node> {
  880. RETURN_IF_STACK_LIMITED(llvm::None);
  881. TokenizedBuffer::Token start_token = *position_;
  882. auto start = GetSubtreeStartPosition();
  883. bool has_errors = !ParseExpression();
  884. if (auto semi = ConsumeIf(TokenKind::Semi())) {
  885. return AddNode(ParseNodeKind::ExpressionStatement(), *semi, start,
  886. has_errors);
  887. }
  888. if (!has_errors) {
  889. emitter_.Emit(*position_, ExpectedSemiAfterExpression);
  890. }
  891. if (auto recovery_node =
  892. SkipPastLikelyEnd(start_token, [&](TokenizedBuffer::Token semi) {
  893. return AddNode(ParseNodeKind::ExpressionStatement(), semi, start,
  894. true);
  895. })) {
  896. return recovery_node;
  897. }
  898. // Found junk not even followed by a `;`.
  899. return llvm::None;
  900. }
  901. auto ParseTree::Parser::ParseParenCondition(TokenKind introducer)
  902. -> llvm::Optional<Node> {
  903. RETURN_IF_STACK_LIMITED(llvm::None);
  904. // `(` expression `)`
  905. auto start = GetSubtreeStartPosition();
  906. auto open_paren = ConsumeIf(TokenKind::OpenParen());
  907. if (!open_paren) {
  908. emitter_.Emit(*position_, ExpectedParenAfter, introducer);
  909. }
  910. auto expr = ParseExpression();
  911. if (!open_paren) {
  912. // Don't expect a matching closing paren if there wasn't an opening paren.
  913. return llvm::None;
  914. }
  915. auto close_paren =
  916. ParseCloseParen(*open_paren, ParseNodeKind::ConditionEnd());
  917. return AddNode(ParseNodeKind::Condition(), *open_paren, start,
  918. /*has_error=*/!expr || !close_paren);
  919. }
  920. auto ParseTree::Parser::ParseIfStatement() -> llvm::Optional<Node> {
  921. auto start = GetSubtreeStartPosition();
  922. auto if_token = Consume(TokenKind::If());
  923. auto cond = ParseParenCondition(TokenKind::If());
  924. auto then_case = ParseCodeBlock();
  925. bool else_has_errors = false;
  926. if (ConsumeAndAddLeafNodeIf(TokenKind::Else(),
  927. ParseNodeKind::IfStatementElse())) {
  928. // 'else if' is permitted as a special case.
  929. if (NextTokenIs(TokenKind::If())) {
  930. else_has_errors = !ParseIfStatement();
  931. } else {
  932. else_has_errors = !ParseCodeBlock();
  933. }
  934. }
  935. return AddNode(ParseNodeKind::IfStatement(), if_token, start,
  936. /*has_error=*/!cond || !then_case || else_has_errors);
  937. }
  938. auto ParseTree::Parser::ParseWhileStatement() -> llvm::Optional<Node> {
  939. RETURN_IF_STACK_LIMITED(llvm::None);
  940. auto start = GetSubtreeStartPosition();
  941. auto while_token = Consume(TokenKind::While());
  942. auto cond = ParseParenCondition(TokenKind::While());
  943. auto body = ParseCodeBlock();
  944. return AddNode(ParseNodeKind::WhileStatement(), while_token, start,
  945. /*has_error=*/!cond || !body);
  946. }
  947. auto ParseTree::Parser::ParseKeywordStatement(ParseNodeKind kind,
  948. KeywordStatementArgument argument)
  949. -> llvm::Optional<Node> {
  950. RETURN_IF_STACK_LIMITED(llvm::None);
  951. auto keyword_kind = NextTokenKind();
  952. CHECK(keyword_kind.IsKeyword());
  953. auto start = GetSubtreeStartPosition();
  954. auto keyword = Consume(keyword_kind);
  955. bool arg_error = false;
  956. if ((argument == KeywordStatementArgument::Optional &&
  957. NextTokenKind() != TokenKind::Semi()) ||
  958. argument == KeywordStatementArgument::Mandatory) {
  959. arg_error = !ParseExpression();
  960. }
  961. auto semi =
  962. ConsumeAndAddLeafNodeIf(TokenKind::Semi(), ParseNodeKind::StatementEnd());
  963. if (!semi) {
  964. emitter_.Emit(*position_, ExpectedSemiAfter, keyword_kind);
  965. // FIXME: Try to skip to a semicolon to recover.
  966. }
  967. return AddNode(kind, keyword, start, /*has_error=*/!semi || arg_error);
  968. }
  969. auto ParseTree::Parser::ParseStatement() -> llvm::Optional<Node> {
  970. RETURN_IF_STACK_LIMITED(llvm::None);
  971. switch (NextTokenKind()) {
  972. case TokenKind::Var():
  973. return ParseVariableDeclaration();
  974. case TokenKind::If():
  975. return ParseIfStatement();
  976. case TokenKind::While():
  977. return ParseWhileStatement();
  978. case TokenKind::Continue():
  979. return ParseKeywordStatement(ParseNodeKind::ContinueStatement(),
  980. KeywordStatementArgument::None);
  981. case TokenKind::Break():
  982. return ParseKeywordStatement(ParseNodeKind::BreakStatement(),
  983. KeywordStatementArgument::None);
  984. case TokenKind::Return():
  985. return ParseKeywordStatement(ParseNodeKind::ReturnStatement(),
  986. KeywordStatementArgument::Optional);
  987. default:
  988. // A statement with no introducer token can only be an expression
  989. // statement.
  990. return ParseExpressionStatement();
  991. }
  992. }
  993. } // namespace Carbon