parser_state.def 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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. //
  5. // This is an X-macro header. It does not use `#include` guards, and instead is
  6. // designed to be `#include`ed after the x-macro is defined in order for its
  7. // inclusion to expand to the desired output. Macro definitions are cleaned up
  8. // at the end of this file.
  9. //
  10. // Supported x-macros are:
  11. // - CARBON_PARSER_STATE(Name)
  12. // Defines a parser state.
  13. //
  14. // Parser states may be clustered when there are multiple related variants,
  15. // named `StateAsVariant`. When there are variants, they share a common helper
  16. // function for most logic.
  17. //
  18. // Comments will describe a series of possible output states. States are listed
  19. // in the order they'll be executed; in other words, `1` is the top of the state
  20. // stack. The comment `(state done)` indicates that no new states are added to
  21. // the stack.
  22. //
  23. // Where state output is conditional on a lexed token, the name of
  24. // the TokenKind should be used rather than the string name in order to make it
  25. // easier to compare with code.
  26. #ifndef CARBON_PARSER_STATE
  27. #error "Must define the x-macro to use this file."
  28. #endif
  29. // Use CARBON_PARSER_STATE_VARIANTSN(State, Variant1, Variant2, ...) to generate
  30. // StateAsVariant1, StateAsVariant2, ... states.
  31. #define CARBON_PARSER_STATE_VARIANT(State, Variant) \
  32. CARBON_PARSER_STATE(State##As##Variant)
  33. #define CARBON_PARSER_STATE_VARIANTS2(State, Variant1, Variant2) \
  34. CARBON_PARSER_STATE_VARIANT(State, Variant1) \
  35. CARBON_PARSER_STATE_VARIANT(State, Variant2)
  36. #define CARBON_PARSER_STATE_VARIANTS3(State, Variant1, Variant2, Variant3) \
  37. CARBON_PARSER_STATE_VARIANT(State, Variant1) \
  38. CARBON_PARSER_STATE_VARIANTS2(State, Variant2, Variant3)
  39. // Handles an index expression `a[0]`.
  40. //
  41. // Always:
  42. // 1. Expression
  43. // 2. IndexExpressionFinish
  44. CARBON_PARSER_STATE(IndexExpression)
  45. // Handles finishing the index expression.
  46. //
  47. // Always:
  48. // (state done)
  49. CARBON_PARSER_STATE(IndexExpressionFinish)
  50. // Handles an array expression `[T; N]`.
  51. //
  52. // Always:
  53. // 1. Expression
  54. // 2. ArrayExpressionSemi
  55. CARBON_PARSER_STATE(ArrayExpression)
  56. // Handles ';' in an array expression `[T; N]`.
  57. //
  58. // If `CloseSquareBracket`:
  59. // 1. ArrayExpressionFinish
  60. // Else:
  61. // 1. Expression
  62. // 2. ArrayExpressionFinish
  63. CARBON_PARSER_STATE(ArrayExpressionSemi)
  64. // Handles finishing the array expression.
  65. //
  66. // Always:
  67. // (state done)
  68. CARBON_PARSER_STATE(ArrayExpressionFinish)
  69. // Handles the `{` of a brace expression.
  70. //
  71. // If `CloseCurlyBrace`:
  72. // 1. BraceExpressionFinishAsUnknown
  73. // Else:
  74. // 1. BraceExpressionParameterAsUnknown
  75. // 2. BraceExpressionFinishAsUnknown
  76. CARBON_PARSER_STATE(BraceExpression)
  77. // Handles a brace expression parameter. Note this will always start as unknown,
  78. // but should be known after the first valid parameter. All later inconsistent
  79. // parameters are invalid.
  80. //
  81. // If valid:
  82. // 1. DesignatorExpressionAsStruct
  83. // 2. BraceExpressionParameterAfterDesignatorAs(Type|Value|Unknown)
  84. // Else:
  85. // 1. BraceExpressionParameterFinishAs(Type|Value|Unknown)
  86. CARBON_PARSER_STATE_VARIANTS3(BraceExpressionParameter, Type, Value, Unknown)
  87. // Handles a brace expression parameter after the initial designator. This
  88. // should be at a `:` or `=`, depending on whether it's a type or value literal.
  89. //
  90. // If valid:
  91. // 1. Expression
  92. // 2. BraceExpressionParameterFinishAs(Type|Value|Unknown)
  93. // Else:
  94. // 1. BraceExpressionParameterFinishAs(Type|Value|Unknown)
  95. CARBON_PARSER_STATE_VARIANTS3(BraceExpressionParameterAfterDesignator, Type,
  96. Value, Unknown)
  97. // Handles the end of a brace expression parameter.
  98. //
  99. // If `Comma`:
  100. // 1. BraceExpressionParameterAsUnknown
  101. // Else:
  102. // (state done)
  103. CARBON_PARSER_STATE_VARIANTS3(BraceExpressionParameterFinish, Type, Value,
  104. Unknown)
  105. // Handles the `}` of a brace expression.
  106. //
  107. // Always:
  108. // (state done)
  109. CARBON_PARSER_STATE_VARIANTS3(BraceExpressionFinish, Type, Value, Unknown)
  110. // Handles a call expression `(...)`.
  111. //
  112. // If `CloseParen`:
  113. // 1. CallExpressionFinish
  114. // Else:
  115. // 1. Expression
  116. // 2. CallExpressionParameterFinish
  117. // 3. CallExpressionFinish
  118. CARBON_PARSER_STATE(CallExpression)
  119. // Handles the `,` or `)` after a call parameter.
  120. //
  121. // If `Comma`:
  122. // 1. Expression
  123. // 2. CallExpressionParameterFinish
  124. // Else:
  125. // (state done)
  126. CARBON_PARSER_STATE(CallExpressionParameterFinish)
  127. // Handles finishing the call expression.
  128. //
  129. // Always:
  130. // (state done)
  131. CARBON_PARSER_STATE(CallExpressionFinish)
  132. // Handles processing at the `{` on a typical code block.
  133. //
  134. // If `OpenCurlyBrace`:
  135. // 1. StatementScopeLoop
  136. // 2. CodeBlockFinish
  137. // Else:
  138. // 1. Statement
  139. // 2. CodeBlockFinish
  140. CARBON_PARSER_STATE(CodeBlock)
  141. // Handles processing at the `}` on a typical code block, after a statement
  142. // scope is done.
  143. //
  144. // Always:
  145. // (state done)
  146. CARBON_PARSER_STATE(CodeBlockFinish)
  147. // Handles a declaration name and parameters, such as `Foo[...](...)`.
  148. //
  149. // Allowed parameters:
  150. // - None: `Foo` only.
  151. // - Optional: `Foo`, `Foo(...)`, or `Foo[...](...)`.
  152. // - Required: ``Foo(...)` or `Foo[...](...)`.
  153. //
  154. // If `Identifier` followed by `Period`:
  155. // 1. DeclarationNameAndParamsAfterNameAs(None|Optional|Required)
  156. // If `Identifier`:
  157. // 1. DeclarationNameAndParamsAfterNameAs(None|Optional|Required)
  158. // 2. PeriodAsDeclaration
  159. // Else:
  160. // (state done)
  161. CARBON_PARSER_STATE_VARIANTS3(DeclarationNameAndParams, None, Optional,
  162. Required)
  163. // Handles a declaration name between the main name and deduced parameters.
  164. //
  165. // For `None`, parameters aren't supported so only `Period` or `Else` paths are
  166. // used.
  167. //
  168. // If `Period`:
  169. // 1. DeclarationNameAndParamsAfterNameAs(None|Optional|Required)
  170. // 2. PeriodAsDeclaration
  171. // If `OpenSquareBracket`:
  172. // 1. ParameterListAsDeduced
  173. // 2. DeclarationNameAndParamsAfterDeduced
  174. // If `OpenParen`:
  175. // 1. ParameterListAsRegular
  176. // Else:
  177. // (state done)
  178. CARBON_PARSER_STATE_VARIANTS3(DeclarationNameAndParamsAfterName, None, Optional,
  179. Required)
  180. // Handles regular parameters such as `(...)` for the general declaration case.
  181. // Only used after deduced parameters.
  182. //
  183. // If `OpenParen`:
  184. // 1. ParameterListAsRegular
  185. // Else:
  186. // (state done)
  187. CARBON_PARSER_STATE(DeclarationNameAndParamsAfterDeduced)
  188. // Handles processing of a declaration scope. Things like fn, class, interface,
  189. // and so on.
  190. //
  191. // If `EndOfFile`:
  192. // (state done)
  193. // If `Class`:
  194. // 1. TypeIntroducerAsClass
  195. // 2. DeclarationScopeLoop
  196. // If `Constraint`:
  197. // 1. TypeIntroducerAsNamedConstraint
  198. // 2. DeclarationScopeLoop
  199. // If `Fn`:
  200. // 1. FunctionIntroducer
  201. // 2. DeclarationScopeLoop
  202. // If `Interface`:
  203. // 1. TypeIntroducerAsInterface
  204. // 2. DeclarationScopeLoop
  205. // If `Namespace`:
  206. // 2. Namespace
  207. // 3. DeclarationScopeLoop
  208. // If `Semi`:
  209. // 1. DeclarationScopeLoop
  210. // If `Var`:
  211. // 1. Var
  212. // 2. DeclarationScopeLoop
  213. // Else:
  214. // 1. DeclarationScopeLoop
  215. CARBON_PARSER_STATE(DeclarationScopeLoop)
  216. // Handles periods. Only does one `.<expression>` segment; the source is
  217. // responsible for handling chaining.
  218. //
  219. // The forms of this are:
  220. // - Designated names in structs.
  221. // - Qualified names in declarations.
  222. // - Member access expressions.
  223. //
  224. // Declarations and expressions have qualifiers such as `x.y`, while structs
  225. // have designators such as `.z`.
  226. //
  227. // Always:
  228. // (state done)
  229. CARBON_PARSER_STATE_VARIANTS3(Period, Declaration, Expression, Struct)
  230. // Handles `->name` expressions. Identical to PeriodAsExpression except for the
  231. // leading token.
  232. //
  233. // Always:
  234. // (state done)
  235. CARBON_PARSER_STATE(ArrowExpression)
  236. // Handles processing of an expression.
  237. //
  238. // If `If`:
  239. // 1. Expression
  240. // 2. IfExpressionCondition
  241. // 3. IfExpressionFinish
  242. // Else if valid prefix operator:
  243. // 1. Expression
  244. // 2. ExpressionLoopForPrefix
  245. // Else:
  246. // 1. ExpressionInPostfix
  247. // 2. ExpressionLoop
  248. CARBON_PARSER_STATE(Expression)
  249. // Handles the initial part of postfix expressions, such as an identifier or
  250. // literal value, then proceeds to the loop.
  251. //
  252. // If `Identifier` or literal (including type literals):
  253. // 1. ExpressionInPostfixLoop
  254. // If `OpenCurlyBrace`:
  255. // 1. BraceExpression
  256. // 2. ExpressionInPostfixLoop
  257. // If `OpenParen`:
  258. // 1. ParenExpression
  259. // 2. ExpressionInPostfixLoop
  260. // If `OpenSquareBracket`:
  261. // 1. ArrayExpression
  262. // 2. ExpressionInPostfixLoop
  263. // Else:
  264. // (state done)
  265. CARBON_PARSER_STATE(ExpressionInPostfix)
  266. // Handles looping through elements following the initial postfix expression,
  267. // such as designators or parenthesized parameters.
  268. //
  269. // If `Period`:
  270. // 1. PeriodAsExpression
  271. // 2. ExpressionInPostfixLoop
  272. // If `MinusGreater`:
  273. // 1. ArrowExpression
  274. // 2. ExpressionInPostfixLoop
  275. // If `OpenParen`:
  276. // 1. CallExpression
  277. // 2. ExpressionInPostfixLoop
  278. // If `OpenSquareBracket`:
  279. // 1. IndexExpressionStart
  280. // 2. ExpressionInPostfixLoop
  281. // Else:
  282. // (state done)
  283. CARBON_PARSER_STATE(ExpressionInPostfixLoop)
  284. // Handles processing of an expression.
  285. //
  286. // If binary operator:
  287. // 1. Expression
  288. // 2. ExpressionLoopForBinary
  289. // If postfix operator:
  290. // 1. ExpressionLoop
  291. // Else:
  292. // (state done)
  293. CARBON_PARSER_STATE(ExpressionLoop)
  294. // Completes an ExpressionLoop pass by adding an infix operator, then goes back
  295. // to ExpressionLoop.
  296. //
  297. // Always:
  298. // 1. ExpressionLoop
  299. CARBON_PARSER_STATE(ExpressionLoopForBinary)
  300. // Completes an ExpressionLoop pass by adding a prefix operator, then goes back
  301. // to ExpressionLoop.
  302. //
  303. // Always:
  304. // 1. ExpressionLoop
  305. CARBON_PARSER_STATE(ExpressionLoopForPrefix)
  306. // Completes the condition of an `if` expression and handles the `then` token.
  307. //
  308. // If `Then`:
  309. // 1. Expression
  310. // 2. IfExpressionFinishThen
  311. // Else:
  312. // (state done)
  313. CARBON_PARSER_STATE(IfExpressionFinishCondition)
  314. // Completes the first alternative in an `if` expression and handles the `else`
  315. // token.
  316. //
  317. // If `Else`:
  318. // 1. Expression
  319. // 2. IfExpressionFinishElse
  320. // Else:
  321. // (state done)
  322. CARBON_PARSER_STATE(IfExpressionFinishThen)
  323. // Completes the second alternative in an `if` expression.
  324. //
  325. // Always:
  326. // (state done)
  327. CARBON_PARSER_STATE(IfExpressionFinishElse)
  328. // Completes an IfExpression.
  329. //
  330. // Always:
  331. // (state done)
  332. CARBON_PARSER_STATE(IfExpressionFinish)
  333. // Handles the `;` for an expression statement, which is different from most
  334. // keyword statements.
  335. //
  336. // Always:
  337. // (state done)
  338. CARBON_PARSER_STATE(ExpressionStatementFinish)
  339. // Handles a function's introducer.
  340. //
  341. // If invalid:
  342. // (state done)
  343. // Else:
  344. // 1. DeclarationNameAndParamsAsRequired
  345. // 2. FunctionAfterParameters
  346. CARBON_PARSER_STATE(FunctionIntroducer)
  347. // Handles processing of a function's syntax after `)`, primarily the
  348. // possibility a `->` return type is there. Always enqueues signature finish
  349. // handling.
  350. //
  351. // If `MinusGreater`:
  352. // 1. Expression
  353. // 2. FunctionReturnTypeFinish
  354. // 3. FunctionSignatureFinish
  355. // Else:
  356. // 1. FunctionSignatureFinish
  357. CARBON_PARSER_STATE(FunctionAfterParameters)
  358. // Finishes a function return type.
  359. //
  360. // Always:
  361. // (state done)
  362. CARBON_PARSER_STATE(FunctionReturnTypeFinish)
  363. // Finishes a function signature. If it's a declaration, the function is done;
  364. // otherwise, this also starts definition processing.
  365. //
  366. // If `Semi`:
  367. // (state done)
  368. // If `OpenCurlyBrace`:
  369. // 1. StatementScopeLoop
  370. // 2. FunctionDefinitionFinish
  371. // Else:
  372. // (state done)
  373. CARBON_PARSER_STATE(FunctionSignatureFinish)
  374. // Finishes a function definition.
  375. //
  376. // Always:
  377. // (state done)
  378. CARBON_PARSER_STATE(FunctionDefinitionFinish)
  379. // Handles `namespace`.
  380. //
  381. // Always:
  382. // 1. DeclarationNameAndParamsAsNone
  383. // 2. NamespaceFinish
  384. CARBON_PARSER_STATE(Namespace)
  385. // Handles `namespace` after the name.
  386. //
  387. // Always:
  388. // (state done)
  389. CARBON_PARSER_STATE(NamespaceFinish)
  390. // Handles `package`.
  391. //
  392. // Always:
  393. // (state done)
  394. CARBON_PARSER_STATE(Package)
  395. // Starts deduced parameter processing.
  396. //
  397. // Always:
  398. // 1. PatternAs(DeducedParameter|Parameter)
  399. // 2. ParameterFinishAs(Deduced|Regular)
  400. CARBON_PARSER_STATE_VARIANTS2(Parameter, Deduced, Regular)
  401. // Finishes deduced parameter processing, including `,`. If there are more
  402. // parameters, enqueues another parameter processing state.
  403. //
  404. // If `Comma` without the list close token:
  405. // 1. ParameterAs(Deduced|Regular)
  406. // Else:
  407. // (state done)
  408. CARBON_PARSER_STATE_VARIANTS2(ParameterFinish, Deduced, Regular)
  409. // Handles processing of a parameter list `[` or `(`.
  410. //
  411. // If the list close token:
  412. // 1. ParameterListFinishAs(Deduced|Regular)
  413. // Else:
  414. // 1. ParameterAs(Deduced|Regular)
  415. // 2. ParameterListFinishAs(Deduced|Regular)
  416. CARBON_PARSER_STATE_VARIANTS2(ParameterList, Deduced, Regular)
  417. // Handles processing of a parameter list `]` or `)`.
  418. //
  419. // Always:
  420. // (state done)
  421. CARBON_PARSER_STATE_VARIANTS2(ParameterListFinish, Deduced, Regular)
  422. // Handles the processing of a `(condition)` up through the expression.
  423. //
  424. // If `OpenCurlyBrace`:
  425. // 1. ParenConditionAs(If|While)Finish
  426. // Else:
  427. // 1. Expression
  428. // 2. ParenConditionAs(If|While)Finish
  429. CARBON_PARSER_STATE_VARIANTS2(ParenCondition, If, While)
  430. // Finishes the processing of a `(condition)` after the expression.
  431. //
  432. // Always:
  433. // (state done)
  434. CARBON_PARSER_STATE_VARIANTS2(ParenConditionFinish, If, While)
  435. // Handles the `(` of a parenthesized expression.
  436. //
  437. // If `CloseParen`:
  438. // 1. ParenExpressionFinishAsTuple
  439. // Else:
  440. // 1. Expression
  441. // 2. ParenExpressionParameterFinishAsUnknown
  442. // 3. ParenExpressionFinish
  443. CARBON_PARSER_STATE(ParenExpression)
  444. // Handles the end of a parenthesized expression's parameter. This will start as
  445. // AsUnknown on the first parameter; if there are more, it switches to AsTuple
  446. // processing.
  447. //
  448. // If `Comma` without `CloseParen`:
  449. // 1. Expression
  450. // 2. ParenExpressionParameterFinishAsTuple
  451. // SPECIAL: Parent becomes ParenExpressionFinishAsTuple
  452. // If `Comma` with `CloseParen`:
  453. // (state done)
  454. // SPECIAL: Parent becomes ParenExpressionFinishAsTuple
  455. // Else `CloseParen`:
  456. // (state done)
  457. CARBON_PARSER_STATE_VARIANTS2(ParenExpressionParameterFinish, Unknown, Tuple)
  458. // Handles the `)` of a parenthesized expression.
  459. //
  460. // Always:
  461. // (state done)
  462. CARBON_PARSER_STATE_VARIANTS2(ParenExpressionFinish, Normal, Tuple)
  463. // Handles pattern parsing for a pattern, enqueuing type expression processing.
  464. // This covers parameter and `var` support.
  465. //
  466. // If valid:
  467. // 1. Expression
  468. // 2. PatternFinishAs(Generic|Regular)
  469. // Else:
  470. // 1. PatternFinish
  471. CARBON_PARSER_STATE_VARIANTS3(Pattern, DeducedParameter, Parameter, Variable)
  472. // Handles `addr` in a pattern.
  473. //
  474. // Always:
  475. // (state done)
  476. CARBON_PARSER_STATE(PatternAddress)
  477. // Handles `template` in a pattern.
  478. //
  479. // Always:
  480. // (state done)
  481. CARBON_PARSER_STATE(PatternTemplate)
  482. // Finishes pattern processing.
  483. //
  484. // Always:
  485. // (state done)
  486. CARBON_PARSER_STATE_VARIANTS2(PatternFinish, Generic, Regular)
  487. // Handles a single statement. While typically within a statement block, this
  488. // can also be used for error recovery where we expect a statement block and
  489. // are missing braces.
  490. //
  491. // If `Break`:
  492. // 1. StatementBreakFinish
  493. // (state done)
  494. // If `Continue`:
  495. // 1. StatementContinueFinish
  496. // (state done)
  497. // If `For`:
  498. // 1. StatementForHeader
  499. // 2. StatementForFinish
  500. // If `If`:
  501. // 1. StatementIf
  502. // If `Return`:
  503. // 1. StatementReturn
  504. // If `Var`:
  505. // 1. VarAsSemicolon
  506. // If `While`:
  507. // 1. StatementWhile
  508. // Else:
  509. // 1. Expression
  510. // 2. ExpressionStatementFinish
  511. CARBON_PARSER_STATE(Statement)
  512. // Handles `break` processing at the `;`.
  513. //
  514. // Always:
  515. // (state done)
  516. CARBON_PARSER_STATE(StatementBreakFinish)
  517. // Handles `continue` processing at the `;`.
  518. //
  519. // Always:
  520. // (state done)
  521. CARBON_PARSER_STATE(StatementContinueFinish)
  522. // Handles `for` processing of `(var`, proceeding to a pattern before
  523. // continuing.
  524. //
  525. // If no `OpenParen`:
  526. // 1. CodeBlock
  527. // If `Var`:
  528. // 1. VarAsFor
  529. // 2. StatementForHeaderIn
  530. // Else:
  531. // 1. StatementForHeaderIn
  532. CARBON_PARSER_STATE(StatementForHeader)
  533. // Handles `for` procesisng of `in`, proceeding to an expression before
  534. // continuing.
  535. //
  536. // If `In` or `Colon`:
  537. // 1. Expression
  538. // 2. StatementForHeaderFinish
  539. // Else:
  540. // 1. StatementForHeaderFinish
  541. CARBON_PARSER_STATE(StatementForHeaderIn)
  542. // Handles `for` processing of `)`, proceeding to the statement block.
  543. //
  544. // Always:
  545. // 1. CodeBlock
  546. CARBON_PARSER_STATE(StatementForHeaderFinish)
  547. // Handles `for` processing at the final `}`.
  548. //
  549. // Always:
  550. // (state done)
  551. CARBON_PARSER_STATE(StatementForFinish)
  552. // Handles `if` processing at the start.
  553. //
  554. // Always:
  555. // 1. ParenConditionAsIf
  556. // 2. StatementIfConditionFinish
  557. CARBON_PARSER_STATE(StatementIf)
  558. // Handles `if` processing between the condition and start of the first code
  559. // block.
  560. //
  561. // Always:
  562. // 1. CodeBlock
  563. // 2. StatementIfThenBlockFinish
  564. CARBON_PARSER_STATE(StatementIfConditionFinish)
  565. // Handles `if` processing after the end of the first code block, with the
  566. // optional `else`.
  567. //
  568. // If `Else` then `If`:
  569. // 1. CodeBlock
  570. // 2. StatementIfElseBlockFinish
  571. // If `Else`:
  572. // 1. StatementIf
  573. // 2. StatementIfElseBlockFinish
  574. // Else:
  575. // (state done)
  576. CARBON_PARSER_STATE(StatementIfThenBlockFinish)
  577. // Handles `if` processing after a provided `else` code block.
  578. //
  579. // Always:
  580. // (state done)
  581. CARBON_PARSER_STATE(StatementIfElseBlockFinish)
  582. // Handles `return` processing.
  583. //
  584. // If `Semi`:
  585. // 1. StatementReturnFinish
  586. // Else:
  587. // 1. Expression
  588. // 2. StatementReturnFinish
  589. CARBON_PARSER_STATE(StatementReturn)
  590. // Handles `return` processing at the `;` when there's an expression.
  591. //
  592. // Always:
  593. // (state done)
  594. CARBON_PARSER_STATE(StatementReturnFinish)
  595. // Handles processing of statements within a scope.
  596. //
  597. // If `CloseCurlyBrace`:
  598. // (state done)
  599. // Else:
  600. // 1. Statement
  601. // 2. StatementScopeLoop
  602. CARBON_PARSER_STATE(StatementScopeLoop)
  603. // Handles `while` processing.
  604. //
  605. // Always:
  606. // 1. ParenConditionAsWhile
  607. // 2. StatementWhileConditionFinish
  608. CARBON_PARSER_STATE(StatementWhile)
  609. // Handles `while` processing between the condition and start of the code block.
  610. //
  611. // Always:
  612. // 1. CodeBlock
  613. // 2. StatementWhileBlockFinish
  614. CARBON_PARSER_STATE(StatementWhileConditionFinish)
  615. // Handles `while` processing after the end of the code block.
  616. //
  617. // Always:
  618. // (state done)
  619. CARBON_PARSER_STATE(StatementWhileBlockFinish)
  620. // Handles parsing after the declaration scope of a type.
  621. //
  622. // Always:
  623. // (state done)
  624. CARBON_PARSER_STATE_VARIANTS3(TypeDefinitionFinish, Class, Interface,
  625. NamedConstraint)
  626. // Handles processing of a type's introducer.
  627. //
  628. // Always:
  629. // 1. DeclarationNameAndParamsAsOptional
  630. // 2. TypeAfterParamsAs(Class|Interface|NamedConstraint)
  631. CARBON_PARSER_STATE_VARIANTS3(TypeIntroducer, Class, Interface, NamedConstraint)
  632. // Handles processing of a type after its optional parameters.
  633. //
  634. // If `Semi`:
  635. // (state done)
  636. // If `OpenCurlyBrace`:
  637. // 1. DeclarationScopeLoop
  638. // 2. TypeDefinitionFinishAs(Class|Interface|NamedConstraint)
  639. // Else:
  640. // (state done)
  641. CARBON_PARSER_STATE_VARIANTS3(TypeAfterParams, Class, Interface,
  642. NamedConstraint)
  643. // Handles the start of a `var`.
  644. //
  645. // Always:
  646. // 1. PatternAsVariable
  647. // 2. VarAfterPattern
  648. // 3. VarFinishAs(Semicolon|For)
  649. CARBON_PARSER_STATE_VARIANTS2(Var, Semicolon, For)
  650. // Handles `var` after the pattern, either followed by an initializer or the
  651. // semicolon.
  652. //
  653. // If `Equal`:
  654. // 1. Expression
  655. // Else:
  656. // (state done)
  657. CARBON_PARSER_STATE(VarAfterPattern)
  658. // Handles `var` parsing at the end.
  659. //
  660. // Always:
  661. // (state done)
  662. CARBON_PARSER_STATE_VARIANTS2(VarFinish, Semicolon, For)
  663. #undef CARBON_PARSER_STATE