state.def 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  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_PARSE_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. // The comments before each state describe the portion of the grammar that the
  19. // state is implementing, by giving an example of each kind of token sequence
  20. // that this state handles. In this example, `...` indicates a sequence of
  21. // tokens handled by some other state, and `???` indicates a sequence of invalid
  22. // tokens. A trailing `??? ;` indicates an attempt to skip to the end of the
  23. // declaration, which may or may not actually find a `;` token.
  24. //
  25. // The position in the token stream before the state is indicated by the caret
  26. // `^` on the line below the example, and all tokens consumed by the state are
  27. // underlined by the caret and following `~`s. If no tokens are consumed, the
  28. // caret will point between tokens. Therefore, the position in the token stream
  29. // after the state is the first token in the example after the underlined
  30. // region.
  31. //
  32. // Following each set of examples, the output states for that situation are
  33. // listed. States are numbered in the order they'll be executed; in other
  34. // words, `1` is the top of the state stack. The comment `(state done)`
  35. // indicates that no new states are added to the stack.
  36. #ifndef CARBON_PARSE_STATE
  37. #error "Must define the x-macro to use this file."
  38. #endif
  39. // Use CARBON_PARSE_STATE_VARIANTSN(State, Variant1, Variant2, ...) to generate
  40. // StateAsVariant1, StateAsVariant2, ... states.
  41. #define CARBON_PARSE_STATE_VARIANT(State, Variant) \
  42. CARBON_PARSE_STATE(State##As##Variant)
  43. #define CARBON_PARSE_STATE_VARIANTS2(State, Variant1, Variant2) \
  44. CARBON_PARSE_STATE_VARIANT(State, Variant1) \
  45. CARBON_PARSE_STATE_VARIANT(State, Variant2)
  46. #define CARBON_PARSE_STATE_VARIANTS3(State, Variant1, Variant2, Variant3) \
  47. CARBON_PARSE_STATE_VARIANT(State, Variant1) \
  48. CARBON_PARSE_STATE_VARIANTS2(State, Variant2, Variant3)
  49. #define CARBON_PARSE_STATE_VARIANTS4(State, Variant1, Variant2, Variant3, \
  50. Variant4) \
  51. CARBON_PARSE_STATE_VARIANT(State, Variant1) \
  52. CARBON_PARSE_STATE_VARIANTS3(State, Variant2, Variant3, Variant4)
  53. // Used as a default for StateStackEntry initialization in some cases. Should
  54. // not be put on the state stack.
  55. CARBON_PARSE_STATE(Invalid)
  56. // Handles an index expression:
  57. //
  58. // a[0]
  59. // ^
  60. // 1. Expr
  61. // 2. IndexExprFinish
  62. CARBON_PARSE_STATE(IndexExpr)
  63. // Handles finishing the index expression.
  64. //
  65. // a[0]
  66. // ^
  67. // (state done)
  68. CARBON_PARSE_STATE(IndexExprFinish)
  69. // Handles an array expression.
  70. //
  71. // [T; N]
  72. // ^
  73. // 1. Expr
  74. // 2. ArrayExprSemi
  75. CARBON_PARSE_STATE(ArrayExpr)
  76. // Handles ';' in an array expression.
  77. //
  78. // [T;]
  79. // ^
  80. // 1. ArrayExprFinish
  81. //
  82. // [T; N]
  83. // ^
  84. // 1. Expr
  85. // 2. ArrayExprFinish
  86. CARBON_PARSE_STATE(ArrayExprSemi)
  87. // Handles finishing the array expression.
  88. //
  89. // [T;]
  90. // ^
  91. // [T; N]
  92. // ^
  93. // (state done)
  94. CARBON_PARSE_STATE(ArrayExprFinish)
  95. // Handles the `{` of a brace expression.
  96. //
  97. // {}
  98. // ^
  99. // 1. BraceExprFinishAsUnknown
  100. //
  101. // { ... }
  102. // ^
  103. // 1. BraceExprParamAsUnknown
  104. // 2. BraceExprFinishAsUnknown
  105. CARBON_PARSE_STATE(BraceExpr)
  106. // Handles a brace expression parameter. Note this will always start as unknown,
  107. // but should be known after the first valid parameter. All later inconsistent
  108. // parameters are invalid.
  109. //
  110. // { .foo ... }
  111. // ^
  112. // 1. PeriodAsStruct
  113. // 2. BraceExprParamAfterDesignatorAs(Type|Value|Unknown)
  114. //
  115. // { ???
  116. // ^
  117. // 1. BraceExprParamFinishAs(Type|Value|Unknown)
  118. CARBON_PARSE_STATE_VARIANTS3(BraceExprParam, Type, Value, Unknown)
  119. // Handles a brace expression parameter after the initial designator. This
  120. // should be at a `:` or `=`, depending on whether it's a type or value literal.
  121. //
  122. // { .foo = bar ... }
  123. // ^
  124. // 1. Expr
  125. // 2. BraceExprParamFinishAsValue
  126. //
  127. // { .foo: bar ... }
  128. // ^
  129. // 1. Expr
  130. // 2. BraceExprParamFinishAsType
  131. //
  132. // { .foo ???
  133. // ^
  134. // 1. BraceExprParamFinishAs(Type|Value|Unknown)
  135. CARBON_PARSE_STATE_VARIANTS3(BraceExprParamAfterDesignator, Type, Value,
  136. Unknown)
  137. // Handles the end of a brace expression parameter.
  138. //
  139. // { ... }
  140. // ^
  141. // (state done)
  142. //
  143. // { .foo = bar, ... }
  144. // ^
  145. // 1. BraceExprParamAsValue
  146. //
  147. // { .foo: bar, ... }
  148. // ^
  149. // 1. BraceExprParamAsType
  150. //
  151. // { ??? , ... }
  152. // ^
  153. // 1. BraceExprParamAsUnknown
  154. CARBON_PARSE_STATE_VARIANTS3(BraceExprParamFinish, Type, Value, Unknown)
  155. // Handles the `}` of a brace expression.
  156. //
  157. // { ... }
  158. // ^
  159. // (state done)
  160. CARBON_PARSE_STATE_VARIANTS3(BraceExprFinish, Type, Value, Unknown)
  161. // Handles a call expression `(...)`.
  162. //
  163. // F()
  164. // ^
  165. // 1. CallExprFinish
  166. //
  167. // F( ...
  168. // ^
  169. // 1. Expr
  170. // 2. CallExprParamFinish
  171. // 3. CallExprFinish
  172. CARBON_PARSE_STATE(CallExpr)
  173. // Handles the `,` or `)` after a call parameter.
  174. //
  175. // F(a, ...)
  176. // ^
  177. // 1. Expr
  178. // 2. CallExprParamFinish
  179. //
  180. // F(a )
  181. // ^
  182. // (state done)
  183. CARBON_PARSE_STATE(CallExprParamFinish)
  184. // Handles finishing the call expression.
  185. //
  186. // F(a, b)
  187. // ^
  188. // (state done)
  189. CARBON_PARSE_STATE(CallExprFinish)
  190. // Handles processing at the `{` on a typical code block.
  191. //
  192. // if (cond) {
  193. // ^
  194. // 1. StatementScopeLoop
  195. // 2. CodeBlockFinish
  196. //
  197. // if (cond) ???
  198. // ^
  199. // 1. Statement
  200. // 2. CodeBlockFinish
  201. CARBON_PARSE_STATE(CodeBlock)
  202. // Handles processing at the `}` on a typical code block, after a statement
  203. // scope is done.
  204. //
  205. // if (cond) { ... }
  206. // ^
  207. // (state done)
  208. CARBON_PARSE_STATE(CodeBlockFinish)
  209. // Handles a declaration name and parameters, such as `Foo[...](...)`.
  210. //
  211. // Allowed parameters:
  212. // - None: `Foo` only.
  213. // - Optional: `Foo`, `Foo(...)`, or `Foo[...](...)`.
  214. // - Required: `Foo(...)` or `Foo[...](...)`.
  215. //
  216. // name . ...
  217. // ^~~~
  218. // 1. PeriodAsDecl
  219. // 2. DeclNameAndParamsAfterNameAs(None|Optional|Required)
  220. //
  221. // name ...
  222. // ^~~~
  223. // 1. DeclNameAndParamsAfterNameAs(None|Optional|Required)
  224. //
  225. // ???
  226. // ^
  227. // (state done)
  228. CARBON_PARSE_STATE_VARIANTS3(DeclNameAndParams, None, Optional, Required)
  229. // Handles a declaration name between the main name and implicit parameters.
  230. //
  231. // name . ...
  232. // ^
  233. // 1. PeriodAsDecl
  234. // 2. DeclNameAndParamsAfterNameAs(None|Optional|Required)
  235. //
  236. // name [ ... ] (variant is not None)
  237. // ^
  238. // 1. PatternListAsImplicit
  239. // 2. DeclNameAndParamsAfterImplicit
  240. //
  241. // name ( ... ) (variant is not None)
  242. // ^
  243. // 1. PatternListAsTuple
  244. //
  245. // name ... (variant is not Required)
  246. // ^
  247. // (state done)
  248. //
  249. // name ??? (variant is Required)
  250. // ^
  251. // (state done)
  252. CARBON_PARSE_STATE_VARIANTS3(DeclNameAndParamsAfterName, None, Optional,
  253. Required)
  254. // Handles regular parameters such as `(...)` for the general declaration case.
  255. // Only used after implicit parameters.
  256. //
  257. // name [ ... ] ( ... )
  258. // ^
  259. // 1. PatternListAsTuple
  260. //
  261. // name [ ... ] ???
  262. // ^
  263. // (state done)
  264. CARBON_PARSE_STATE(DeclNameAndParamsAfterImplicit)
  265. // Handles processing of a declaration scope. Things like fn, class, interface,
  266. // and so on.
  267. //
  268. // abstract
  269. // ^~~~~~~~
  270. // base class
  271. // ^~~~
  272. // default
  273. // ^~~~~~~
  274. // extend base
  275. // ^~~~~~
  276. // final
  277. // ^~~~~
  278. // impl fn
  279. // ^~~~
  280. // private
  281. // ^~~~~~~
  282. // protected
  283. // ^~~~~~~~~
  284. // virtual
  285. // ^~~~~~~
  286. // 1. DeclScopeLoop
  287. //
  288. // alias ...
  289. // ^~~~~
  290. // 1. Alias
  291. // 2. DeclScopeLoop
  292. //
  293. // class ...
  294. // ^~~~~
  295. // 1. TypeAfterIntroducerAsClass
  296. // 2. DeclScopeLoop
  297. //
  298. // base : ...
  299. // ^~~~~~
  300. // 1. Expr
  301. // 2. BaseDecl
  302. // 3. DeclScopeLoop
  303. //
  304. // constraint ...
  305. // ^~~~~~~~~~
  306. // 1. TypeAfterIntroducerAsNamedConstraint
  307. // 2. DeclScopeLoop
  308. //
  309. // fn ...
  310. // ^~
  311. // 1. FunctionIntroducer
  312. // 2. DeclScopeLoop
  313. //
  314. // impl ...
  315. // ^~~~
  316. // 1. ImplAfterIntroducer
  317. // 2. DeclScopeLoop
  318. //
  319. // interface ...
  320. // ^~~~~~~~~
  321. // 1. TypeAfterIntroducerAsInterface
  322. // 2. DeclScopeLoop
  323. //
  324. // namespace ...
  325. // ^~~~~~~~~
  326. // 1. Namespace
  327. // 2. DeclScopeLoop
  328. //
  329. // ;
  330. // ^
  331. // 1. DeclScopeLoop
  332. //
  333. // var ...
  334. // ^~~
  335. // 1. VarAsDecl
  336. // 2. DeclScopeLoop
  337. //
  338. // let ...
  339. // ^~~
  340. // 1. Let
  341. // 2. DeclScopeLoop
  342. //
  343. // choice ...
  344. // ^
  345. // 1. ChoiceIntroducer
  346. // 2. DeclScopeLoop
  347. //
  348. // ??? ;
  349. // ^~~~~
  350. // (state done)
  351. CARBON_PARSE_STATE(DeclScopeLoop)
  352. // Handles periods. Only does one `.<expression>` segment; the source is
  353. // responsible for handling chaining.
  354. //
  355. // The forms of this are:
  356. // - Qualified names in declarations.
  357. // - Member access expressions.
  358. // - Designated names in structs.
  359. //
  360. // Declarations and expressions have qualifiers such as `x.y`, while structs
  361. // have designators such as `.z`.
  362. //
  363. // . name
  364. // ^~~~~~
  365. // -> name
  366. // ^~~~~~~
  367. // . base (variant is not Decl)
  368. // ^~~~~~
  369. // -> base (variant is not Decl)
  370. // ^~~~~~~
  371. // . ??? (??? consumed if it is a keyword)
  372. // ^
  373. // -> ??? (??? consumed if it is a keyword)
  374. // ^~
  375. // (state done)
  376. //
  377. // expr . ( ... )
  378. // ^
  379. // 1. ParenExpr
  380. // 2. CompoundMemberAccess
  381. //
  382. // expr -> ( ... )
  383. // ^~
  384. // 1. ParenExpr
  385. // 2. CompoundPointerMemberAccess
  386. CARBON_PARSE_STATE_VARIANTS3(Period, Decl, Expr, Struct)
  387. // Handles a compound member access after we parse the name expression.
  388. //
  389. // expr . ( expr )
  390. // ^
  391. CARBON_PARSE_STATE(CompoundMemberAccess)
  392. // Handles a compound pointer member access after we parse the name expression.
  393. //
  394. // expr -> ( expr )
  395. // ^
  396. CARBON_PARSE_STATE(CompoundPointerMemberAccess)
  397. // Handles `->name` expressions. Identical to PeriodAsExpr except for the
  398. // leading token.
  399. //
  400. // -> name
  401. // ^~~~~~~
  402. // (state done)
  403. //
  404. // -> ??? (??? consumed if it is a keyword)
  405. // ^~
  406. // (state done)
  407. CARBON_PARSE_STATE(ArrowExpr)
  408. // Handles processing of an expression.
  409. //
  410. // if ...
  411. // ^~
  412. // 1. Expr
  413. // 2. IfExprCondition
  414. // 3. IfExprFinish
  415. //
  416. // <prefix operator> ...
  417. // ^~~~~~~~~~~~~~~~~
  418. // 1. Expr
  419. // 2. ExprLoopForPrefix
  420. //
  421. // ...
  422. // ^
  423. // 1. ExprInPostfix
  424. // 2. ExprLoop
  425. CARBON_PARSE_STATE(Expr)
  426. // Handles the initial part of postfix expressions, such as an identifier or
  427. // literal value, then proceeds to the loop.
  428. //
  429. // identifier
  430. // ^~~~~~~~~~
  431. // literal
  432. // ^~~~~~~
  433. // self
  434. // ^~~~
  435. // Self
  436. // ^~~~
  437. // 1. ExprInPostfixLoop
  438. //
  439. // {
  440. // ^
  441. // 1. BraceExpr
  442. // 2. ExprInPostfixLoop
  443. //
  444. // (
  445. // ^
  446. // 1. ParenExpr
  447. // 2. ExprInPostfixLoop
  448. //
  449. // [
  450. // ^
  451. // 1. ArrayExpr
  452. // 2. ExprInPostfixLoop
  453. //
  454. // ???
  455. // ^
  456. // (state done)
  457. CARBON_PARSE_STATE(ExprInPostfix)
  458. // Handles looping through elements following the initial postfix expression,
  459. // such as designators or parenthesized parameters.
  460. //
  461. // expr . ...
  462. // ^
  463. // 1. PeriodAsExpr
  464. // 2. ExprInPostfixLoop
  465. //
  466. // expr -> ...
  467. // ^
  468. // 1. ArrowExpr
  469. // 2. ExprInPostfixLoop
  470. //
  471. // expr ( ... )
  472. // ^
  473. // 1. CallExpr
  474. // 2. ExprInPostfixLoop
  475. //
  476. // expr [ ... ]
  477. // ^
  478. // 1. IndexExprStart
  479. // 2. ExprInPostfixLoop
  480. //
  481. // ...
  482. // ^
  483. // (state done)
  484. CARBON_PARSE_STATE(ExprInPostfixLoop)
  485. // Handles processing of an expression.
  486. //
  487. // expr <infix operator> ...
  488. // ^~~~~~~~~~~~~~~~
  489. // 1. Expr
  490. // 2. ExprLoopForBinary
  491. //
  492. // expr <postfix operator>
  493. // ^~~~~~~~~~~~~~~~~~
  494. // 1. ExprLoop
  495. //
  496. // expr <short circuit operator> ...
  497. // ^~~~~~~~~~~~~~~~~~~~~~~~
  498. // 1. Expr
  499. // 2. ExprLoopForShortCircuitOperator
  500. //
  501. // expr ...
  502. // ^
  503. // (state done)
  504. CARBON_PARSE_STATE(ExprLoop)
  505. // Completes an ExprLoop pass by adding an infix operator, then goes back
  506. // to ExprLoop.
  507. //
  508. // expr <infix operator> expr ...
  509. // ^
  510. // 1. ExprLoop
  511. CARBON_PARSE_STATE(ExprLoopForInfixOperator)
  512. // Completes an ExprLoop pass by adding a prefix operator, then goes back
  513. // to ExprLoop.
  514. //
  515. // <prefix operator> expr ...
  516. // ^
  517. // 1. ExprLoop
  518. CARBON_PARSE_STATE(ExprLoopForPrefixOperator)
  519. // Completes an ExprLoop pass by adding a short circuit operator, then goes back
  520. // to ExprLoop.
  521. //
  522. // expr <short circuit operator> expr ...
  523. // ^
  524. // 1. ExprLoop
  525. CARBON_PARSE_STATE_VARIANTS2(ExprLoopForShortCircuitOperator, And, Or)
  526. // Completes the condition of an `if` expression and handles the `then` token.
  527. //
  528. // if expr then ...
  529. // ^~~~
  530. // 1. Expr
  531. // 2. IfExprFinishThen
  532. //
  533. // if expr ???
  534. // ^
  535. // (state done)
  536. CARBON_PARSE_STATE(IfExprFinishCondition)
  537. // Completes the first alternative in an `if` expression and handles the `else`
  538. // token.
  539. //
  540. // if expr then expr else ...
  541. // ^~~~
  542. // 1. Expr
  543. // 2. IfExprFinishElse
  544. //
  545. // if expr then expr ???
  546. // ^
  547. // (state done)
  548. CARBON_PARSE_STATE(IfExprFinishThen)
  549. // Completes the second alternative in an `if` expression.
  550. //
  551. // if expr then expr else expr
  552. // ^
  553. // (state done)
  554. CARBON_PARSE_STATE(IfExprFinishElse)
  555. // Completes an IfExpr.
  556. //
  557. // if expr then expr else expr
  558. // ^
  559. // if ???
  560. // ^
  561. // (state done)
  562. CARBON_PARSE_STATE(IfExprFinish)
  563. // Handles the `;` for an expression statement, which is different from most
  564. // keyword statements.
  565. //
  566. // expr ;
  567. // ^
  568. // expr ??? ;
  569. // ^~~~~
  570. // (state done)
  571. CARBON_PARSE_STATE(ExprStatementFinish)
  572. // Handles a function's introducer.
  573. //
  574. // fn ...
  575. // ^
  576. // 1. DeclNameAndParamsAsRequired
  577. // 2. FunctionAfterParams
  578. CARBON_PARSE_STATE(FunctionIntroducer)
  579. // Handles processing of a function's syntax after `)`, primarily the
  580. // possibility a `->` return type is there. Always enqueues signature finish
  581. // handling.
  582. //
  583. // fn F(...) -> ...
  584. // ^~
  585. // 1. Expr
  586. // 2. FunctionReturnTypeFinish
  587. // 3. FunctionSignatureFinish
  588. //
  589. // fn F(...) ...
  590. // ^
  591. // 1. FunctionSignatureFinish
  592. CARBON_PARSE_STATE(FunctionAfterParams)
  593. // Finishes a function return type.
  594. //
  595. // fn F(...) -> expr ...
  596. // ^
  597. // (state done)
  598. CARBON_PARSE_STATE(FunctionReturnTypeFinish)
  599. // Finishes a function signature. If it's a declaration, the function is done;
  600. // otherwise, this also starts definition processing.
  601. //
  602. // fn ... ;
  603. // ^
  604. // (state done)
  605. //
  606. // fn ... {
  607. // ^
  608. // 1. StatementScopeLoop
  609. // 2. FunctionDefinitionFinish
  610. //
  611. // fn ... ??? ;
  612. // ^~~~~
  613. // (state done)
  614. CARBON_PARSE_STATE(FunctionSignatureFinish)
  615. // Finishes a function definition.
  616. //
  617. // fn ... }
  618. // ^
  619. // fn ... ;
  620. // ^
  621. // (state done)
  622. CARBON_PARSE_STATE(FunctionDefinitionFinish)
  623. // Handles `import`.
  624. //
  625. // import pkgname [library "libname"] ;
  626. // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  627. // import ??? ;
  628. // ^~~~~~~~~~~~
  629. // (state done)
  630. CARBON_PARSE_STATE(Import)
  631. // Handles `library` in directive form.
  632. //
  633. // Always:
  634. // (state done)
  635. CARBON_PARSE_STATE(Library)
  636. // Handles `namespace`.
  637. //
  638. // namespace ...
  639. // ^
  640. // 1. DeclNameAndParamsAsNone
  641. // 2. NamespaceFinish
  642. CARBON_PARSE_STATE(Namespace)
  643. // Handles `namespace` after the name.
  644. //
  645. // namespace ... ;
  646. // ^
  647. // namespace ... ??? ;
  648. // ^~~~~
  649. // (state done)
  650. CARBON_PARSE_STATE(NamespaceFinish)
  651. // Handles `package`.
  652. //
  653. // package pkgname [library "libname"] [api|impl] ;
  654. // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  655. // package ??? ;
  656. // ^~~~~~~~~~~~~
  657. // (state done)
  658. CARBON_PARSE_STATE(Package)
  659. // Handles `alias` after the introducer.
  660. //
  661. // alias ...
  662. // ^
  663. // 1. DeclNameAndParamsAsNone
  664. // 2. AliasInitializer
  665. CARBON_PARSE_STATE(Alias)
  666. // Handles `alias` after the name.
  667. //
  668. // alias ... = ... ;
  669. // ^
  670. // 1. Expr
  671. // 2. AliasFinish
  672. //
  673. // alias ???
  674. // ^
  675. // (state done)
  676. CARBON_PARSE_STATE(AliasAfterName)
  677. // Handles `alias` at the end.
  678. //
  679. // alias ... ;
  680. // ^
  681. // alias ???
  682. // ^
  683. // (state done)
  684. CARBON_PARSE_STATE(AliasFinish)
  685. // Starts parsing a pattern in a comma-separated list. The variants mark
  686. // whether it is part of an implicit parameter list or a tuple pattern.
  687. //
  688. // ...
  689. // ^
  690. // 1. BindingPattern
  691. // 2. PatternListElementFinishAs(Implicit|Tuple)
  692. CARBON_PARSE_STATE_VARIANTS2(PatternListElement, Implicit, Tuple)
  693. // Finishes parsing a pattern in a comma-separated list, including the
  694. // optional trailing `,`. If there are more patterns, enqueues another
  695. // pattern parsing state.
  696. //
  697. // ... , ) (variant is Tuple)
  698. // ^
  699. // (state done)
  700. //
  701. // ... , ] (variant is Implicit)
  702. // ^
  703. // (state done)
  704. //
  705. // ... , ...
  706. // ^
  707. // 1. PatternListElementAs(Implicit|Tuple)
  708. //
  709. // ...
  710. // ^
  711. // (state done)
  712. CARBON_PARSE_STATE_VARIANTS2(PatternListElementFinish, Implicit, Tuple)
  713. // Handles processing of a tuple pattern (parentheses) or implicit parameter
  714. // list (square brackets).
  715. //
  716. // ( ) (variant is Tuple)
  717. // ^
  718. // [ ] (variant is Implicit)
  719. // ^
  720. // 1. PatternListFinishAs(Tuple|Implicit)
  721. //
  722. // ( ... ) (variant is Tuple)
  723. // ^
  724. // [ ... ] (variant is Implicit)
  725. // ^
  726. // 1. PatternListElementAs(Tuple|Implicit)
  727. // 2. PatternListFinishAs(Tuple|Implicit)
  728. CARBON_PARSE_STATE_VARIANTS2(PatternList, Implicit, Tuple)
  729. // Handles processing of a parameter list `]` or `)`.
  730. //
  731. // ( ... ) (variant is Tuple)
  732. // ^
  733. // [ ... ] (variant is Implicit)
  734. // ^
  735. // (state done)
  736. CARBON_PARSE_STATE_VARIANTS2(PatternListFinish, Implicit, Tuple)
  737. // Handles the processing of a `(condition)` up through the expression.
  738. //
  739. // if/while/match { (invalid)
  740. // ^
  741. // 1. ParenConditionAs(If|While|Match)Finish
  742. //
  743. // if/while/match ( ... )
  744. // ^
  745. // if/while/match ???
  746. // ^
  747. // 1. Expr
  748. // 2. ParenConditionAs(If|While|Match)Finish
  749. CARBON_PARSE_STATE_VARIANTS3(ParenCondition, If, While, Match)
  750. // Finishes the processing of a `(condition)` after the expression.
  751. //
  752. // if/while/match ( expr )
  753. // ^
  754. // if/while/match {
  755. // ^
  756. // if/while/match ??? {
  757. // ^
  758. // (state done)
  759. CARBON_PARSE_STATE_VARIANTS3(ParenConditionFinish, If, While, Match)
  760. // Handles the `(` of a parenthesized single expression
  761. //
  762. // ( )
  763. // ^
  764. // 1. TupleLiteralFinish
  765. //
  766. // ( ... )
  767. // ^
  768. // 1. Expr
  769. // 2. ExprAfterOpenParenFinish
  770. // 3. ParenExprFinish (SPECIAL: may be replaced)
  771. CARBON_PARSE_STATE(ParenExpr)
  772. // Handles the `)` of a tuple literal.
  773. //
  774. // ( ... )
  775. // ^
  776. // (state done)
  777. CARBON_PARSE_STATE(TupleLiteralFinish)
  778. // Handles the end of an expression following an open parenthesis.
  779. //
  780. // ( ... , )
  781. // ^
  782. // (state done)
  783. // SPECIAL: parent becomes TupleLiteralFinish
  784. //
  785. // ( ... , ... )
  786. // ^
  787. // 1. Expr
  788. // 2. TupleLiteralElementFinish
  789. // SPECIAL: parent becomes TupleLiteralFinish
  790. //
  791. // ( ... )
  792. // ^
  793. // (state done)
  794. CARBON_PARSE_STATE(ExprAfterOpenParenFinish)
  795. // Handles the end of an expression that is known to be an element of a tuple
  796. // literal expression.
  797. //
  798. // ( ... , )
  799. // ^
  800. // (state done)
  801. //
  802. // ( ... , ... )
  803. // ^
  804. // 1. Expr
  805. // 2. TupleLiteralElementFinish
  806. //
  807. // ( ... )
  808. // ^
  809. // (state done)
  810. CARBON_PARSE_STATE(TupleLiteralElementFinish)
  811. // Handles the `)` of a parenthesized single expression.
  812. //
  813. // ( ... )
  814. // ^
  815. // (state done)
  816. CARBON_PARSE_STATE(ParenExprFinish)
  817. // Handles processing of a pattern.
  818. //
  819. // ( ... )
  820. // ^
  821. // 1. PatternListAsTuple
  822. //
  823. // ...
  824. // ^
  825. // 1. BindingPattern
  826. CARBON_PARSE_STATE(Pattern)
  827. // Handles the initial part of a binding pattern, enqueuing type expression
  828. // processing.
  829. //
  830. // template (variant is not Variable)
  831. // ^~~~~~~~
  832. // 4. BindingPatternTemplate
  833. //
  834. // THEN
  835. //
  836. // addr (variant is not Variable)
  837. // ^~~~
  838. // 3. BindingPatternAddr
  839. //
  840. // THEN
  841. //
  842. // name: ...
  843. // ^~~~~
  844. // self: ...
  845. // ^~~~~
  846. // 1. Expr
  847. // 2. BindingPatternFinishAsRegular
  848. //
  849. // name:! ...
  850. // ^~~~~~
  851. // self:! ...
  852. // ^~~~~~
  853. // 1. Expr
  854. // 2. BindingPatternFinishAsGeneric
  855. //
  856. // ???
  857. // ^
  858. // 1. BindingPatternFinishAsRegular
  859. CARBON_PARSE_STATE(BindingPattern)
  860. // Handles `addr` in a binding pattern.
  861. //
  862. // addr name: type
  863. // ^
  864. // (state done)
  865. CARBON_PARSE_STATE(BindingPatternAddr)
  866. // Handles `template` in a binding pattern.
  867. //
  868. // template name:! type
  869. // ^
  870. // (state done)
  871. CARBON_PARSE_STATE(BindingPatternTemplate)
  872. // Finishes binding pattern processing.
  873. //
  874. // name: type
  875. // ^
  876. // (state done)
  877. CARBON_PARSE_STATE_VARIANTS2(BindingPatternFinish, Generic, Regular)
  878. // Handles a single statement. While typically within a statement block, this
  879. // can also be used for error recovery where we expect a statement block and
  880. // are missing braces.
  881. //
  882. // break ...
  883. // ^~~~~
  884. // 1. StatementBreakFinish
  885. //
  886. // continue ...
  887. // ^~~~~~~~
  888. // 1. StatementContinueFinish
  889. //
  890. // for ...
  891. // ^~~
  892. // 1. StatementForHeader
  893. // 2. StatementForFinish
  894. //
  895. // if ...
  896. // ^
  897. // 1. StatementIf
  898. //
  899. // return ...
  900. // ^
  901. // 1. StatementReturn
  902. //
  903. // var ...
  904. // ^
  905. // 1. VarAsDecl
  906. //
  907. // returned ...
  908. // ^
  909. // 1. VarAsReturned
  910. //
  911. // while ...
  912. // ^
  913. // 1. StatementWhile
  914. //
  915. // ...
  916. // ^
  917. // 1. Expr
  918. // 2. ExprStatementFinish
  919. //
  920. // match ...
  921. // ^
  922. // 1. MatchIntroducer
  923. CARBON_PARSE_STATE(Statement)
  924. // Handles `break` processing at the `;`.
  925. //
  926. // break ;
  927. // ^
  928. // (state done)
  929. CARBON_PARSE_STATE(StatementBreakFinish)
  930. // Handles `continue` processing at the `;`.
  931. //
  932. // continue ;
  933. // ^
  934. // (state done)
  935. CARBON_PARSE_STATE(StatementContinueFinish)
  936. // Handles `for` processing of `(var`, proceeding to a binding pattern before
  937. // continuing.
  938. //
  939. // for ( var ... )
  940. // ^
  941. // 1. VarAsFor
  942. // 2. StatementForHeaderIn
  943. //
  944. // for ( ???
  945. // ^
  946. // for ( ??? in ...
  947. // ^~~~~~~~
  948. // for ??? in ...
  949. // ^~~~~~
  950. // for ???
  951. // ^
  952. // 1. StatementForHeaderIn
  953. CARBON_PARSE_STATE(StatementForHeader)
  954. // Handles `for` processing of `in`, proceeding to an expression before
  955. // continuing.
  956. //
  957. // for ( ... in ... )
  958. // ^
  959. // 1. Expr
  960. // 2. StatementForHeaderFinish
  961. CARBON_PARSE_STATE(StatementForHeaderIn)
  962. // Handles `for` processing of `)`, proceeding to the statement block.
  963. //
  964. // for ( ... ) ...
  965. // ^
  966. // 1. CodeBlock
  967. CARBON_PARSE_STATE(StatementForHeaderFinish)
  968. // Handles `for` processing after the final `}`.
  969. //
  970. // for ( ... ) { ... }
  971. // ^
  972. // (state done)
  973. CARBON_PARSE_STATE(StatementForFinish)
  974. // Handles `if` processing at the start.
  975. //
  976. // if ...
  977. // ^~
  978. // 1. ParenConditionAsIf
  979. // 2. StatementIfConditionFinish
  980. CARBON_PARSE_STATE(StatementIf)
  981. // Handles `if` processing between the condition and start of the first code
  982. // block.
  983. //
  984. // if ( ... ) ...
  985. // ^
  986. // 1. CodeBlock
  987. // 2. StatementIfThenBlockFinish
  988. CARBON_PARSE_STATE(StatementIfConditionFinish)
  989. // Handles `if` processing after the end of the first code block, with the
  990. // optional `else`.
  991. //
  992. // if ( ... ) { ... } else if ...
  993. // ^~~~
  994. // 1. StatementIf
  995. // 2. StatementIfElseBlockFinish
  996. //
  997. // if ( ... ) { ... } else ...
  998. // ^~~~
  999. // 1. CodeBlock
  1000. // 2. StatementIfElseBlockFinish
  1001. //
  1002. // if ( ... ) { ... } ...
  1003. // (state done)
  1004. CARBON_PARSE_STATE(StatementIfThenBlockFinish)
  1005. // Handles `if` processing after a provided `else` code block.
  1006. //
  1007. // if ( ... ) { ... } else { ... }
  1008. // ^
  1009. // (state done)
  1010. CARBON_PARSE_STATE(StatementIfElseBlockFinish)
  1011. // Handles `return` processing.
  1012. //
  1013. // return ;
  1014. // ^~~~~~
  1015. // 1. StatementReturnFinish
  1016. //
  1017. // return var ...
  1018. // ^~~~~~~~~~
  1019. // 1. StatementReturnFinish
  1020. //
  1021. // return ...
  1022. // ^~~~~~
  1023. // 1. Expr
  1024. // 2. StatementReturnFinish
  1025. CARBON_PARSE_STATE(StatementReturn)
  1026. // Handles `return` processing at the `;`.
  1027. //
  1028. // return ... ;
  1029. // ^
  1030. // (state done)
  1031. CARBON_PARSE_STATE(StatementReturnFinish)
  1032. // Handles processing of statements within a scope.
  1033. //
  1034. // { ... }
  1035. // ^
  1036. // (state done)
  1037. //
  1038. // { ... ... }
  1039. // ^
  1040. // 1. Statement
  1041. // 2. StatementScopeLoop
  1042. CARBON_PARSE_STATE(StatementScopeLoop)
  1043. // Handles `while` processing.
  1044. //
  1045. // while ...
  1046. // ^~~~~
  1047. // 1. ParenConditionAsWhile
  1048. // 2. StatementWhileConditionFinish
  1049. CARBON_PARSE_STATE(StatementWhile)
  1050. // Handles `while` processing between the condition and start of the code block.
  1051. //
  1052. // while ( ... ) ...
  1053. // ^
  1054. // 1. CodeBlock
  1055. // 2. StatementWhileBlockFinish
  1056. CARBON_PARSE_STATE(StatementWhileConditionFinish)
  1057. // Handles `while` processing after the end of the code block.
  1058. //
  1059. // while ( ... ) { ... }
  1060. // ^
  1061. // (state done)
  1062. CARBON_PARSE_STATE(StatementWhileBlockFinish)
  1063. // Handles parsing after the declaration scope of a type.
  1064. //
  1065. // class/impl/interface/constraint ... { ... }
  1066. // ^
  1067. // (state done)
  1068. CARBON_PARSE_STATE_VARIANTS4(DeclDefinitionFinish, Class, Impl, Interface,
  1069. NamedConstraint)
  1070. // Handles processing of a type after its introducer.
  1071. //
  1072. // class/interface/constraint ...
  1073. // ^
  1074. // 1. DeclNameAndParamsAsOptional
  1075. // 2. DeclOrDefinitionAs(Class|Interface|NamedConstraint)
  1076. CARBON_PARSE_STATE_VARIANTS3(TypeAfterIntroducer, Class, Interface,
  1077. NamedConstraint)
  1078. // Handles processing of a type after its optional parameters.
  1079. //
  1080. // class/impl/interface/constraint name ( ... ) {
  1081. // ^
  1082. // 1. DeclScopeLoop
  1083. // 2. DeclDefinitionFinishAs(Class|Impl|Interface|NamedConstraint)
  1084. //
  1085. // class/impl/interface/constraint name ( ... ) ;
  1086. // ^
  1087. // class/impl/interface/constraint name ( ... ) ???
  1088. // ^
  1089. // (state done)
  1090. CARBON_PARSE_STATE_VARIANTS4(DeclOrDefinition, Class, Impl, Interface, NamedConstraint)
  1091. // Handles processing of a completed `base: B` declaration.
  1092. //
  1093. // base: B ;
  1094. // ^
  1095. // base: B ??? ;
  1096. // ^
  1097. // (state done)
  1098. CARBON_PARSE_STATE(BaseDecl)
  1099. // Handles processing of an `impl...as` declaration after the introducer.
  1100. //
  1101. // impl forall [ ...
  1102. // ^~~~~~
  1103. // 1. PatternListAsImplicit
  1104. // 2. ImplAfterForall
  1105. // 3. DeclOrDefinitionAsImpl
  1106. // impl as ...
  1107. // ^~
  1108. // 1. Expr
  1109. // 2. DeclOrDefinitionAsImpl
  1110. // impl type_expression as ...
  1111. // ^
  1112. // 1. Expr
  1113. // 2. ImplBeforeAs
  1114. // 3. DeclOrDefinitionAsImpl
  1115. CARBON_PARSE_STATE(ImplAfterIntroducer)
  1116. // Handles processing of an `impl forall` declaration after the implicit
  1117. // parameter list.
  1118. //
  1119. // impl forall [ ... ] as ...
  1120. // ^~
  1121. // 1. Expr
  1122. // impl forall [ ... ] type_expression as ...
  1123. // ^
  1124. // 1. Expr
  1125. // 2. ImplBeforeAs
  1126. CARBON_PARSE_STATE(ImplAfterForall)
  1127. // Handles processing of the `as` in an `impl` declaration after the type
  1128. // expression.
  1129. //
  1130. // impl TypeExpression as ...
  1131. // ^~
  1132. // 1. Expr
  1133. CARBON_PARSE_STATE(ImplBeforeAs)
  1134. // Handles the start of a `var` or `returned var`.
  1135. //
  1136. // var ... (variant is Decl)
  1137. // ^
  1138. // 1. Pattern
  1139. // 2. VarAfterPattern
  1140. // 3. VarFinishAsDecl
  1141. //
  1142. // var ... (variant is For)
  1143. // ^
  1144. // 1. Pattern
  1145. // 2. VarFinishAsFor
  1146. //
  1147. // returned var ... (variant is Returned)
  1148. // ^~~~~~~~~~~~
  1149. // 1. Pattern
  1150. // 2. VarAfterPattern
  1151. // 3. VarFinishAsDecl
  1152. //
  1153. // returned ??? ; (variant is Returned)
  1154. // ^~~~~~~~~~~~~~
  1155. // (state done)
  1156. CARBON_PARSE_STATE_VARIANTS3(Var, Decl, Returned, For)
  1157. // Handles `var` after the pattern, either followed by an initializer or the
  1158. // semicolon.
  1159. //
  1160. // var ... = ...
  1161. // ^
  1162. // var ... ??? = ...
  1163. // ^~~~~
  1164. // 1. Expr
  1165. //
  1166. // var ... ...
  1167. // ^
  1168. // (state done)
  1169. CARBON_PARSE_STATE(VarAfterPattern)
  1170. // Handles `var` parsing at the end.
  1171. //
  1172. // var ... ; (variant is Semicolon)
  1173. // ^
  1174. // var ... ??? ; (variant is Semicolon)
  1175. // ^~~~~
  1176. // (state done)
  1177. //
  1178. // var ... in (variant is For)
  1179. // ^~
  1180. // var ... : (variant is For, invalid)
  1181. // ^
  1182. // (state done)
  1183. CARBON_PARSE_STATE_VARIANTS2(VarFinish, Decl, For)
  1184. // Handles the start of a `let`.
  1185. //
  1186. // let ...
  1187. // ^
  1188. // 1. Pattern
  1189. // 2. LetAfterPattern
  1190. // 3. LetFinish
  1191. CARBON_PARSE_STATE(Let)
  1192. // Handles `let` after the pattern, optionally followed by an initializer. The
  1193. // initializer is required except in an associated constant declaration, but
  1194. // that is enforced by check.
  1195. //
  1196. // let ... = ...
  1197. // ^
  1198. // let ... ??? = ...
  1199. // ^~~~~
  1200. // 1. Expr
  1201. //
  1202. // let ... ;
  1203. // ^
  1204. // let ... ??? ;
  1205. // ^~~
  1206. // (state done)
  1207. CARBON_PARSE_STATE(LetAfterPattern)
  1208. // Handles `let` parsing at the end.
  1209. //
  1210. // let ... ;
  1211. // ^
  1212. // (state done)
  1213. CARBON_PARSE_STATE(LetFinish)
  1214. // Handles a choice's introducer.
  1215. //
  1216. // choice ...
  1217. // ^~~~~~
  1218. // 1. DeclNameAndParamsAsOptional
  1219. // 2. ChoiceAlternativeListStart
  1220. // 3. ChoiceDefinitionFinish
  1221. CARBON_PARSE_STATE(ChoiceIntroducer)
  1222. // Handles processing of a choice after its optional parameters.
  1223. //
  1224. // choice name ... {}
  1225. // ^
  1226. // (state done)
  1227. //
  1228. // choice name ... { ... }
  1229. // ^
  1230. // 1. ChoiceAlternative
  1231. //
  1232. // choice name ... ???
  1233. // ^
  1234. // (state done)
  1235. CARBON_PARSE_STATE(ChoiceDefinitionStart)
  1236. // Starts alternative parsing.
  1237. //
  1238. // name( ... )
  1239. // ^~~~
  1240. // 1. ParamListAsRegular
  1241. // 2. ChoiceAlternativeFinish
  1242. // name ...
  1243. // ^~~~
  1244. // 1. ChoiceAlternativeFinish
  1245. CARBON_PARSE_STATE(ChoiceAlternative)
  1246. // Finishes parsing a choice's alternative, including the optional trailing `,`. If there
  1247. // are more alternatives, enqueues another alternative parsing state.
  1248. //
  1249. // ... , }
  1250. // ^
  1251. // (state done)
  1252. //
  1253. // ... , ...
  1254. // ^
  1255. // 1. ChoiceAlternative
  1256. //
  1257. // ...
  1258. // ^
  1259. // (state done)
  1260. CARBON_PARSE_STATE(ChoiceAlternativeFinish)
  1261. // Finishes a choice definition.
  1262. //
  1263. // choice ... }
  1264. // ^
  1265. // (state done)
  1266. CARBON_PARSE_STATE(ChoiceDefinitionFinish)
  1267. // Handles `match` introducer.
  1268. //
  1269. // match ...
  1270. // ^~~~~
  1271. // 1. ParenConditionAsMatch
  1272. // 2. MatchConditionFinish
  1273. CARBON_PARSE_STATE(MatchIntroducer)
  1274. // Handles `match` cases block start after the condition.
  1275. //
  1276. // match (...) { ... }
  1277. // ^
  1278. // 1. MatchCaseLoop
  1279. // 2. MatchStatementFinish
  1280. //
  1281. // match (...) ???
  1282. // ^
  1283. // (state done)
  1284. CARBON_PARSE_STATE(MatchConditionFinish)
  1285. // Handles `match` cases.
  1286. //
  1287. // match (...) { case ...}
  1288. // ^
  1289. // 1. MatchCaseIntroducer
  1290. // 2. MatchCaseLoop
  1291. // match (...) { default ...}
  1292. // ^
  1293. // 1. MatchDefaultIntroducer
  1294. // 2. MatchCaseLoopAfterDefault
  1295. CARBON_PARSE_STATE(MatchCaseLoop)
  1296. // Handles `match` `case` introducer.
  1297. //
  1298. // match (...) { case ...}
  1299. // ^~~~
  1300. // 1. Pattern
  1301. // 2. MatchCaseAfterPattern
  1302. CARBON_PARSE_STATE(MatchCaseIntroducer)
  1303. // Handles `match` case after pattern.
  1304. //
  1305. // match (...) { case ... => ... }
  1306. // ^
  1307. // 1. MatchCaseStart
  1308. // match (...) { case ... if (...) }
  1309. // ^~~~
  1310. // 1. Expr
  1311. // 2. MatchCaseGuardFinish
  1312. // 3. MatchCaseStart
  1313. CARBON_PARSE_STATE(MatchCaseAfterPattern)
  1314. // Handles `match` case guard closing parenthesis.
  1315. //
  1316. // match (...) { case ... if (...) => ... }
  1317. // ^
  1318. // (state done)
  1319. CARBON_PARSE_STATE(MatchCaseGuardFinish)
  1320. // Handles `match` case `=>` and `{` opening statements block.
  1321. //
  1322. // match (...) { case ... => {...} }
  1323. // ^~~~
  1324. // 1. StatementScopeLoop
  1325. // 2. MatchCaseFinish
  1326. //
  1327. // match (...) { case ... ??? }
  1328. // ^
  1329. // (state done)
  1330. //
  1331. // match (...) { case ... => ??? }
  1332. // ^
  1333. // (state done)
  1334. CARBON_PARSE_STATE(MatchCaseStart)
  1335. // Handles `match` case statements block closing `}`.
  1336. //
  1337. // match (...) { case ... => {...} }
  1338. // ^
  1339. // (state done)
  1340. CARBON_PARSE_STATE(MatchCaseFinish)
  1341. // Handles `match` default introducer, `=>` and `{` opening statements block.
  1342. //
  1343. // match (...) { default => {...} }
  1344. // ^~~~~~~~~~~~
  1345. // 1. StatementScopeLoop
  1346. // 2. MatchDefaultFinish
  1347. //
  1348. // match (...) { default ??? }
  1349. // ^
  1350. // (state done)
  1351. //
  1352. // match (...) { default => ??? }
  1353. // ^
  1354. // (state done)
  1355. CARBON_PARSE_STATE(MatchDefaultIntroducer)
  1356. // Handles `match` default case statements block closing `}`.
  1357. //
  1358. // match (...) { default => {...} }
  1359. // ^
  1360. // (state done)
  1361. CARBON_PARSE_STATE(MatchDefaultFinish)
  1362. // Handles `match` cases after the `default` case.
  1363. //
  1364. // match (...) { default => {...} case ... }
  1365. // ^~~~
  1366. // 1. MatchCaseLoopAfterDefault
  1367. //
  1368. // match (...) { default => {...} default ... }
  1369. // ^~~~~~~
  1370. // 1. MatchCaseLoopAfterDefault
  1371. //
  1372. // match (...) { default => {...} }
  1373. // ^
  1374. // (state done)
  1375. CARBON_PARSE_STATE(MatchCaseLoopAfterDefault)
  1376. // Finishes `match` statement.
  1377. //
  1378. // match (...) {...}
  1379. // ^
  1380. // (state done)
  1381. CARBON_PARSE_STATE(MatchStatementFinish)
  1382. #undef CARBON_PARSE_STATE