state.def 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  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. // adapt ...
  289. // ^~~~~
  290. // 1. Expr
  291. // 2. AdaptDecl
  292. // 3. DeclScopeLoop
  293. //
  294. // alias ...
  295. // ^~~~~
  296. // 1. Alias
  297. // 2. DeclScopeLoop
  298. //
  299. // class ...
  300. // ^~~~~
  301. // 1. TypeAfterIntroducerAsClass
  302. // 2. DeclScopeLoop
  303. //
  304. // base : ...
  305. // ^~~~~~
  306. // 1. Expr
  307. // 2. BaseDecl
  308. // 3. DeclScopeLoop
  309. //
  310. // constraint ...
  311. // ^~~~~~~~~~
  312. // 1. TypeAfterIntroducerAsNamedConstraint
  313. // 2. DeclScopeLoop
  314. //
  315. // fn ...
  316. // ^~
  317. // 1. FunctionIntroducer
  318. // 2. DeclScopeLoop
  319. //
  320. // impl ...
  321. // ^~~~
  322. // 1. ImplAfterIntroducer
  323. // 2. DeclScopeLoop
  324. //
  325. // interface ...
  326. // ^~~~~~~~~
  327. // 1. TypeAfterIntroducerAsInterface
  328. // 2. DeclScopeLoop
  329. //
  330. // namespace ...
  331. // ^~~~~~~~~
  332. // 1. Namespace
  333. // 2. DeclScopeLoop
  334. //
  335. // ;
  336. // ^
  337. // 1. DeclScopeLoop
  338. //
  339. // var ...
  340. // ^~~
  341. // 1. VarAsDecl
  342. // 2. DeclScopeLoop
  343. //
  344. // let ...
  345. // ^~~
  346. // 1. Let
  347. // 2. DeclScopeLoop
  348. //
  349. // choice ...
  350. // ^
  351. // 1. ChoiceIntroducer
  352. // 2. DeclScopeLoop
  353. //
  354. // ??? ;
  355. // ^~~~~
  356. // (state done)
  357. CARBON_PARSE_STATE(DeclScopeLoop)
  358. // Handles periods. Only does one `.<expression>` segment; the source is
  359. // responsible for handling chaining.
  360. //
  361. // The forms of this are:
  362. // - Qualified names in declarations.
  363. // - Member access expressions.
  364. // - Designated names in structs.
  365. //
  366. // Declarations and expressions have qualifiers such as `x.y`, while structs
  367. // have designators such as `.z`.
  368. //
  369. // . name
  370. // ^~~~~~
  371. // -> name
  372. // ^~~~~~~
  373. // . base (variant is not Decl)
  374. // ^~~~~~
  375. // -> base (variant is not Decl)
  376. // ^~~~~~~
  377. // . ??? (??? consumed if it is a keyword)
  378. // ^
  379. // -> ??? (??? consumed if it is a keyword)
  380. // ^~
  381. // (state done)
  382. //
  383. // expr . ( ... )
  384. // ^
  385. // 1. OnlyParenExpr
  386. // 2. CompoundMemberAccess
  387. //
  388. // expr -> ( ... )
  389. // ^~
  390. // 1. OnlyParenExpr
  391. // 2. CompoundPointerMemberAccess
  392. CARBON_PARSE_STATE_VARIANTS3(Period, Decl, Expr, Struct)
  393. // Handles a compound member access after we parse the name expression.
  394. //
  395. // expr . ( expr )
  396. // ^
  397. CARBON_PARSE_STATE(CompoundMemberAccess)
  398. // Handles a compound pointer member access after we parse the name expression.
  399. //
  400. // expr -> ( expr )
  401. // ^
  402. CARBON_PARSE_STATE(CompoundPointerMemberAccess)
  403. // Handles `->name` expressions. Identical to PeriodAsExpr except for the
  404. // leading token.
  405. //
  406. // -> name
  407. // ^~~~~~~
  408. // (state done)
  409. //
  410. // -> ??? (??? consumed if it is a keyword)
  411. // ^~
  412. // (state done)
  413. CARBON_PARSE_STATE(ArrowExpr)
  414. // Handles processing of an expression.
  415. //
  416. // if ...
  417. // ^~
  418. // 1. Expr
  419. // 2. IfExprCondition
  420. // 3. IfExprFinish
  421. //
  422. // <prefix operator> ...
  423. // ^~~~~~~~~~~~~~~~~
  424. // 1. Expr
  425. // 2. ExprLoopForPrefix
  426. //
  427. // ...
  428. // ^
  429. // 1. ExprInPostfix
  430. // 2. ExprLoop
  431. CARBON_PARSE_STATE(Expr)
  432. // Handles the initial part of postfix expressions, such as an identifier or
  433. // literal value, then proceeds to the loop.
  434. //
  435. // identifier
  436. // ^~~~~~~~~~
  437. // literal
  438. // ^~~~~~~
  439. // self
  440. // ^~~~
  441. // Self
  442. // ^~~~
  443. // 1. ExprInPostfixLoop
  444. //
  445. // {
  446. // ^
  447. // 1. BraceExpr
  448. // 2. ExprInPostfixLoop
  449. //
  450. // (
  451. // ^
  452. // 1. ParenExpr
  453. // 2. ExprInPostfixLoop
  454. //
  455. // [
  456. // ^
  457. // 1. ArrayExpr
  458. // 2. ExprInPostfixLoop
  459. //
  460. // ???
  461. // ^
  462. // (state done)
  463. CARBON_PARSE_STATE(ExprInPostfix)
  464. // Handles looping through elements following the initial postfix expression,
  465. // such as designators or parenthesized parameters.
  466. //
  467. // expr . ...
  468. // ^
  469. // 1. PeriodAsExpr
  470. // 2. ExprInPostfixLoop
  471. //
  472. // expr -> ...
  473. // ^
  474. // 1. ArrowExpr
  475. // 2. ExprInPostfixLoop
  476. //
  477. // expr ( ... )
  478. // ^
  479. // 1. CallExpr
  480. // 2. ExprInPostfixLoop
  481. //
  482. // expr [ ... ]
  483. // ^
  484. // 1. IndexExprStart
  485. // 2. ExprInPostfixLoop
  486. //
  487. // ...
  488. // ^
  489. // (state done)
  490. CARBON_PARSE_STATE(ExprInPostfixLoop)
  491. // Handles processing of an expression.
  492. //
  493. // expr <infix operator> ...
  494. // ^~~~~~~~~~~~~~~~
  495. // 1. Expr
  496. // 2. ExprLoopForBinary
  497. //
  498. // expr <postfix operator>
  499. // ^~~~~~~~~~~~~~~~~~
  500. // 1. ExprLoop
  501. //
  502. // expr <short circuit operator> ...
  503. // ^~~~~~~~~~~~~~~~~~~~~~~~
  504. // 1. Expr
  505. // 2. ExprLoopForShortCircuitOperator
  506. //
  507. // expr ...
  508. // ^
  509. // (state done)
  510. CARBON_PARSE_STATE(ExprLoop)
  511. // Completes an ExprLoop pass by adding an infix operator, then goes back
  512. // to ExprLoop.
  513. //
  514. // expr <infix operator> expr ...
  515. // ^
  516. // 1. ExprLoop
  517. CARBON_PARSE_STATE(ExprLoopForInfixOperator)
  518. // Completes an ExprLoop pass by adding a prefix operator, then goes back
  519. // to ExprLoop.
  520. //
  521. // <prefix operator> expr ...
  522. // ^
  523. // 1. ExprLoop
  524. CARBON_PARSE_STATE(ExprLoopForPrefixOperator)
  525. // Completes an ExprLoop pass by adding a short circuit operator, then goes back
  526. // to ExprLoop.
  527. //
  528. // expr <short circuit operator> expr ...
  529. // ^
  530. // 1. ExprLoop
  531. CARBON_PARSE_STATE_VARIANTS2(ExprLoopForShortCircuitOperator, And, Or)
  532. // Completes the condition of an `if` expression and handles the `then` token.
  533. //
  534. // if expr then ...
  535. // ^~~~
  536. // 1. Expr
  537. // 2. IfExprFinishThen
  538. //
  539. // if expr ???
  540. // ^
  541. // (state done)
  542. CARBON_PARSE_STATE(IfExprFinishCondition)
  543. // Completes the first alternative in an `if` expression and handles the `else`
  544. // token.
  545. //
  546. // if expr then expr else ...
  547. // ^~~~
  548. // 1. Expr
  549. // 2. IfExprFinishElse
  550. //
  551. // if expr then expr ???
  552. // ^
  553. // (state done)
  554. CARBON_PARSE_STATE(IfExprFinishThen)
  555. // Completes the second alternative in an `if` expression.
  556. //
  557. // if expr then expr else expr
  558. // ^
  559. // (state done)
  560. CARBON_PARSE_STATE(IfExprFinishElse)
  561. // Completes an IfExpr.
  562. //
  563. // if expr then expr else expr
  564. // ^
  565. // if ???
  566. // ^
  567. // (state done)
  568. CARBON_PARSE_STATE(IfExprFinish)
  569. // Handles the `;` for an expression statement, which is different from most
  570. // keyword statements.
  571. //
  572. // expr ;
  573. // ^
  574. // expr ??? ;
  575. // ^~~~~
  576. // (state done)
  577. CARBON_PARSE_STATE(ExprStatementFinish)
  578. // Handles a function's introducer.
  579. //
  580. // fn ...
  581. // ^
  582. // 1. DeclNameAndParamsAsRequired
  583. // 2. FunctionAfterParams
  584. CARBON_PARSE_STATE(FunctionIntroducer)
  585. // Handles processing of a function's syntax after `)`, primarily the
  586. // possibility a `->` return type is there. Always enqueues signature finish
  587. // handling.
  588. //
  589. // fn F(...) -> ...
  590. // ^~
  591. // 1. Expr
  592. // 2. FunctionReturnTypeFinish
  593. // 3. FunctionSignatureFinish
  594. //
  595. // fn F(...) ...
  596. // ^
  597. // 1. FunctionSignatureFinish
  598. CARBON_PARSE_STATE(FunctionAfterParams)
  599. // Finishes a function return type.
  600. //
  601. // fn F(...) -> expr ...
  602. // ^
  603. // (state done)
  604. CARBON_PARSE_STATE(FunctionReturnTypeFinish)
  605. // Finishes a function signature. If it's a declaration, the function is done;
  606. // otherwise, this also starts definition processing.
  607. //
  608. // fn ... ;
  609. // ^
  610. // (state done)
  611. //
  612. // fn ... {
  613. // ^
  614. // 1. StatementScopeLoop
  615. // 2. FunctionDefinitionFinish
  616. //
  617. // fn ... ??? ;
  618. // ^~~~~
  619. // (state done)
  620. CARBON_PARSE_STATE(FunctionSignatureFinish)
  621. // Finishes a function definition.
  622. //
  623. // fn ... }
  624. // ^
  625. // fn ... ;
  626. // ^
  627. // (state done)
  628. CARBON_PARSE_STATE(FunctionDefinitionFinish)
  629. // Handles `import`.
  630. //
  631. // import pkgname [library "libname"] ;
  632. // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  633. // import ??? ;
  634. // ^~~~~~~~~~~~
  635. // (state done)
  636. CARBON_PARSE_STATE(Import)
  637. // Handles `library` in directive form.
  638. //
  639. // Always:
  640. // (state done)
  641. CARBON_PARSE_STATE(Library)
  642. // Handles `namespace`.
  643. //
  644. // namespace ...
  645. // ^
  646. // 1. DeclNameAndParamsAsNone
  647. // 2. NamespaceFinish
  648. CARBON_PARSE_STATE(Namespace)
  649. // Handles `namespace` after the name.
  650. //
  651. // namespace ... ;
  652. // ^
  653. // namespace ... ??? ;
  654. // ^~~~~
  655. // (state done)
  656. CARBON_PARSE_STATE(NamespaceFinish)
  657. // Handles `package`.
  658. //
  659. // package pkgname [library "libname"] [api|impl] ;
  660. // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  661. // package ??? ;
  662. // ^~~~~~~~~~~~~
  663. // (state done)
  664. CARBON_PARSE_STATE(Package)
  665. // Handles `alias` after the introducer.
  666. //
  667. // alias ...
  668. // ^
  669. // 1. DeclNameAndParamsAsNone
  670. // 2. AliasInitializer
  671. CARBON_PARSE_STATE(Alias)
  672. // Handles `alias` after the name.
  673. //
  674. // alias ... = ... ;
  675. // ^
  676. // 1. Expr
  677. // 2. AliasFinish
  678. //
  679. // alias ???
  680. // ^
  681. // (state done)
  682. CARBON_PARSE_STATE(AliasAfterName)
  683. // Handles `alias` at the end.
  684. //
  685. // alias ... ;
  686. // ^
  687. // alias ???
  688. // ^
  689. // (state done)
  690. CARBON_PARSE_STATE(AliasFinish)
  691. // Starts parsing a pattern in a comma-separated list. The variants mark
  692. // whether it is part of an implicit parameter list or a tuple pattern.
  693. //
  694. // ...
  695. // ^
  696. // 1. BindingPattern
  697. // 2. PatternListElementFinishAs(Implicit|Tuple)
  698. CARBON_PARSE_STATE_VARIANTS2(PatternListElement, Implicit, Tuple)
  699. // Finishes parsing a pattern in a comma-separated list, including the
  700. // optional trailing `,`. If there are more patterns, enqueues another
  701. // pattern parsing state.
  702. //
  703. // ... , ) (variant is Tuple)
  704. // ^
  705. // (state done)
  706. //
  707. // ... , ] (variant is Implicit)
  708. // ^
  709. // (state done)
  710. //
  711. // ... , ...
  712. // ^
  713. // 1. PatternListElementAs(Implicit|Tuple)
  714. //
  715. // ...
  716. // ^
  717. // (state done)
  718. CARBON_PARSE_STATE_VARIANTS2(PatternListElementFinish, Implicit, Tuple)
  719. // Handles processing of a tuple pattern (parentheses) or implicit parameter
  720. // list (square brackets).
  721. //
  722. // ( ) (variant is Tuple)
  723. // ^
  724. // [ ] (variant is Implicit)
  725. // ^
  726. // 1. PatternListFinishAs(Tuple|Implicit)
  727. //
  728. // ( ... ) (variant is Tuple)
  729. // ^
  730. // [ ... ] (variant is Implicit)
  731. // ^
  732. // 1. PatternListElementAs(Tuple|Implicit)
  733. // 2. PatternListFinishAs(Tuple|Implicit)
  734. CARBON_PARSE_STATE_VARIANTS2(PatternList, Implicit, Tuple)
  735. // Handles processing of a parameter list `]` or `)`.
  736. //
  737. // ( ... ) (variant is Tuple)
  738. // ^
  739. // [ ... ] (variant is Implicit)
  740. // ^
  741. // (state done)
  742. CARBON_PARSE_STATE_VARIANTS2(PatternListFinish, Implicit, Tuple)
  743. // Handles the processing of a `(condition)` up through the expression.
  744. //
  745. // if/while/match { (invalid)
  746. // ^
  747. // 1. ParenConditionAs(If|While|Match)Finish
  748. //
  749. // if/while/match ( ... )
  750. // ^
  751. // if/while/match ???
  752. // ^
  753. // 1. Expr
  754. // 2. ParenConditionAs(If|While|Match)Finish
  755. CARBON_PARSE_STATE_VARIANTS3(ParenCondition, If, While, Match)
  756. // Finishes the processing of a `(condition)` after the expression.
  757. //
  758. // if/while/match ( expr )
  759. // ^
  760. // if/while/match {
  761. // ^
  762. // if/while/match ??? {
  763. // ^
  764. // (state done)
  765. CARBON_PARSE_STATE_VARIANTS3(ParenConditionFinish, If, While, Match)
  766. // Handles compound member access `(` after `->` or `.`
  767. //
  768. // ( ... )
  769. // ^
  770. // 1. Expr
  771. // 2. OnlyParenExprFinish
  772. CARBON_PARSE_STATE(OnlyParenExpr)
  773. // Handles the `)` after `->(`... or `.(`...
  774. //
  775. // ( ... )
  776. // ^
  777. CARBON_PARSE_STATE(OnlyParenExprFinish)
  778. // Handles the `(` of a parenthesized single expression
  779. //
  780. // ( )
  781. // ^
  782. // 1. TupleLiteralFinish
  783. //
  784. // ( ... )
  785. // ^
  786. // 1. Expr
  787. // 2. ExprAfterOpenParenFinish
  788. // 3. ParenExprFinish (SPECIAL: may be replaced)
  789. CARBON_PARSE_STATE(ParenExpr)
  790. // Handles the `)` of a tuple literal.
  791. //
  792. // ( ... )
  793. // ^
  794. // (state done)
  795. CARBON_PARSE_STATE(TupleLiteralFinish)
  796. // Handles the end of an expression following an open parenthesis.
  797. //
  798. // ( ... , )
  799. // ^
  800. // (state done)
  801. // SPECIAL: parent becomes TupleLiteralFinish
  802. //
  803. // ( ... , ... )
  804. // ^
  805. // 1. Expr
  806. // 2. TupleLiteralElementFinish
  807. // SPECIAL: parent becomes TupleLiteralFinish
  808. //
  809. // ( ... )
  810. // ^
  811. // (state done)
  812. CARBON_PARSE_STATE(ExprAfterOpenParenFinish)
  813. // Handles the end of an expression that is known to be an element of a tuple
  814. // literal expression.
  815. //
  816. // ( ... , )
  817. // ^
  818. // (state done)
  819. //
  820. // ( ... , ... )
  821. // ^
  822. // 1. Expr
  823. // 2. TupleLiteralElementFinish
  824. //
  825. // ( ... )
  826. // ^
  827. // (state done)
  828. CARBON_PARSE_STATE(TupleLiteralElementFinish)
  829. // Handles the `)` of a parenthesized single expression.
  830. //
  831. // ( ... )
  832. // ^
  833. // (state done)
  834. CARBON_PARSE_STATE(ParenExprFinish)
  835. // Handles processing of a pattern.
  836. //
  837. // ( ... )
  838. // ^
  839. // 1. PatternListAsTuple
  840. //
  841. // ...
  842. // ^
  843. // 1. BindingPattern
  844. CARBON_PARSE_STATE(Pattern)
  845. // Handles the initial part of a binding pattern, enqueuing type expression
  846. // processing.
  847. //
  848. // template (variant is not Variable)
  849. // ^~~~~~~~
  850. // 4. BindingPatternTemplate
  851. //
  852. // THEN
  853. //
  854. // addr (variant is not Variable)
  855. // ^~~~
  856. // 3. BindingPatternAddr
  857. //
  858. // THEN
  859. //
  860. // name: ...
  861. // ^~~~~
  862. // self: ...
  863. // ^~~~~
  864. // 1. Expr
  865. // 2. BindingPatternFinishAsRegular
  866. //
  867. // name:! ...
  868. // ^~~~~~
  869. // self:! ...
  870. // ^~~~~~
  871. // 1. Expr
  872. // 2. BindingPatternFinishAsGeneric
  873. //
  874. // ???
  875. // ^
  876. // 1. BindingPatternFinishAsRegular
  877. CARBON_PARSE_STATE(BindingPattern)
  878. // Handles `addr` in a binding pattern.
  879. //
  880. // addr name: type
  881. // ^
  882. // (state done)
  883. CARBON_PARSE_STATE(BindingPatternAddr)
  884. // Handles `template` in a binding pattern.
  885. //
  886. // template name:! type
  887. // ^
  888. // (state done)
  889. CARBON_PARSE_STATE(BindingPatternTemplate)
  890. // Finishes binding pattern processing.
  891. //
  892. // name: type
  893. // ^
  894. // (state done)
  895. CARBON_PARSE_STATE_VARIANTS2(BindingPatternFinish, Generic, Regular)
  896. // Handles a single statement. While typically within a statement block, this
  897. // can also be used for error recovery where we expect a statement block and
  898. // are missing braces.
  899. //
  900. // break ...
  901. // ^~~~~
  902. // 1. StatementBreakFinish
  903. //
  904. // continue ...
  905. // ^~~~~~~~
  906. // 1. StatementContinueFinish
  907. //
  908. // for ...
  909. // ^~~
  910. // 1. StatementForHeader
  911. // 2. StatementForFinish
  912. //
  913. // if ...
  914. // ^
  915. // 1. StatementIf
  916. //
  917. // return ...
  918. // ^
  919. // 1. StatementReturn
  920. //
  921. // var ...
  922. // ^
  923. // 1. VarAsDecl
  924. //
  925. // returned ...
  926. // ^
  927. // 1. VarAsReturned
  928. //
  929. // while ...
  930. // ^
  931. // 1. StatementWhile
  932. //
  933. // ...
  934. // ^
  935. // 1. Expr
  936. // 2. ExprStatementFinish
  937. //
  938. // match ...
  939. // ^
  940. // 1. MatchIntroducer
  941. CARBON_PARSE_STATE(Statement)
  942. // Handles `break` processing at the `;`.
  943. //
  944. // break ;
  945. // ^
  946. // (state done)
  947. CARBON_PARSE_STATE(StatementBreakFinish)
  948. // Handles `continue` processing at the `;`.
  949. //
  950. // continue ;
  951. // ^
  952. // (state done)
  953. CARBON_PARSE_STATE(StatementContinueFinish)
  954. // Handles `for` processing of `(var`, proceeding to a binding pattern before
  955. // continuing.
  956. //
  957. // for ( var ... )
  958. // ^
  959. // 1. VarAsFor
  960. // 2. StatementForHeaderIn
  961. //
  962. // for ( ???
  963. // ^
  964. // for ( ??? in ...
  965. // ^~~~~~~~
  966. // for ??? in ...
  967. // ^~~~~~
  968. // for ???
  969. // ^
  970. // 1. StatementForHeaderIn
  971. CARBON_PARSE_STATE(StatementForHeader)
  972. // Handles `for` processing of `in`, proceeding to an expression before
  973. // continuing.
  974. //
  975. // for ( ... in ... )
  976. // ^
  977. // 1. Expr
  978. // 2. StatementForHeaderFinish
  979. CARBON_PARSE_STATE(StatementForHeaderIn)
  980. // Handles `for` processing of `)`, proceeding to the statement block.
  981. //
  982. // for ( ... ) ...
  983. // ^
  984. // 1. CodeBlock
  985. CARBON_PARSE_STATE(StatementForHeaderFinish)
  986. // Handles `for` processing after the final `}`.
  987. //
  988. // for ( ... ) { ... }
  989. // ^
  990. // (state done)
  991. CARBON_PARSE_STATE(StatementForFinish)
  992. // Handles `if` processing at the start.
  993. //
  994. // if ...
  995. // ^~
  996. // 1. ParenConditionAsIf
  997. // 2. StatementIfConditionFinish
  998. CARBON_PARSE_STATE(StatementIf)
  999. // Handles `if` processing between the condition and start of the first code
  1000. // block.
  1001. //
  1002. // if ( ... ) ...
  1003. // ^
  1004. // 1. CodeBlock
  1005. // 2. StatementIfThenBlockFinish
  1006. CARBON_PARSE_STATE(StatementIfConditionFinish)
  1007. // Handles `if` processing after the end of the first code block, with the
  1008. // optional `else`.
  1009. //
  1010. // if ( ... ) { ... } else if ...
  1011. // ^~~~
  1012. // 1. StatementIf
  1013. // 2. StatementIfElseBlockFinish
  1014. //
  1015. // if ( ... ) { ... } else ...
  1016. // ^~~~
  1017. // 1. CodeBlock
  1018. // 2. StatementIfElseBlockFinish
  1019. //
  1020. // if ( ... ) { ... } ...
  1021. // (state done)
  1022. CARBON_PARSE_STATE(StatementIfThenBlockFinish)
  1023. // Handles `if` processing after a provided `else` code block.
  1024. //
  1025. // if ( ... ) { ... } else { ... }
  1026. // ^
  1027. // (state done)
  1028. CARBON_PARSE_STATE(StatementIfElseBlockFinish)
  1029. // Handles `return` processing.
  1030. //
  1031. // return ;
  1032. // ^~~~~~
  1033. // 1. StatementReturnFinish
  1034. //
  1035. // return var ...
  1036. // ^~~~~~~~~~
  1037. // 1. StatementReturnFinish
  1038. //
  1039. // return ...
  1040. // ^~~~~~
  1041. // 1. Expr
  1042. // 2. StatementReturnFinish
  1043. CARBON_PARSE_STATE(StatementReturn)
  1044. // Handles `return` processing at the `;`.
  1045. //
  1046. // return ... ;
  1047. // ^
  1048. // (state done)
  1049. CARBON_PARSE_STATE(StatementReturnFinish)
  1050. // Handles processing of statements within a scope.
  1051. //
  1052. // { ... }
  1053. // ^
  1054. // (state done)
  1055. //
  1056. // { ... ... }
  1057. // ^
  1058. // 1. Statement
  1059. // 2. StatementScopeLoop
  1060. CARBON_PARSE_STATE(StatementScopeLoop)
  1061. // Handles `while` processing.
  1062. //
  1063. // while ...
  1064. // ^~~~~
  1065. // 1. ParenConditionAsWhile
  1066. // 2. StatementWhileConditionFinish
  1067. CARBON_PARSE_STATE(StatementWhile)
  1068. // Handles `while` processing between the condition and start of the code block.
  1069. //
  1070. // while ( ... ) ...
  1071. // ^
  1072. // 1. CodeBlock
  1073. // 2. StatementWhileBlockFinish
  1074. CARBON_PARSE_STATE(StatementWhileConditionFinish)
  1075. // Handles `while` processing after the end of the code block.
  1076. //
  1077. // while ( ... ) { ... }
  1078. // ^
  1079. // (state done)
  1080. CARBON_PARSE_STATE(StatementWhileBlockFinish)
  1081. // Handles parsing after the declaration scope of a type.
  1082. //
  1083. // class/impl/interface/constraint ... { ... }
  1084. // ^
  1085. // (state done)
  1086. CARBON_PARSE_STATE_VARIANTS4(DeclDefinitionFinish, Class, Impl, Interface,
  1087. NamedConstraint)
  1088. // Handles processing of a type after its introducer.
  1089. //
  1090. // class/interface/constraint ...
  1091. // ^
  1092. // 1. DeclNameAndParamsAsOptional
  1093. // 2. DeclOrDefinitionAs(Class|Interface|NamedConstraint)
  1094. CARBON_PARSE_STATE_VARIANTS3(TypeAfterIntroducer, Class, Interface,
  1095. NamedConstraint)
  1096. // Handles processing of a type after its optional parameters.
  1097. //
  1098. // class/impl/interface/constraint name ( ... ) {
  1099. // ^
  1100. // 1. DeclScopeLoop
  1101. // 2. DeclDefinitionFinishAs(Class|Impl|Interface|NamedConstraint)
  1102. //
  1103. // class/impl/interface/constraint name ( ... ) ;
  1104. // ^
  1105. // class/impl/interface/constraint name ( ... ) ???
  1106. // ^
  1107. // (state done)
  1108. CARBON_PARSE_STATE_VARIANTS4(DeclOrDefinition, Class, Impl, Interface,
  1109. NamedConstraint)
  1110. // Handles processing of a completed `adapt T` declaration.
  1111. //
  1112. // adapt T ;
  1113. // ^
  1114. // adapt T ??? ;
  1115. // ^~~~~
  1116. // (state done)
  1117. CARBON_PARSE_STATE(AdaptDecl)
  1118. // Handles processing of a completed `base: B` declaration.
  1119. //
  1120. // base: B ;
  1121. // ^
  1122. // base: B ??? ;
  1123. // ^~~~~
  1124. // (state done)
  1125. CARBON_PARSE_STATE(BaseDecl)
  1126. // Handles processing of an `impl...as` declaration after the introducer.
  1127. //
  1128. // impl forall [ ...
  1129. // ^~~~~~
  1130. // 1. PatternListAsImplicit
  1131. // 2. ImplAfterForall
  1132. // 3. DeclOrDefinitionAsImpl
  1133. // impl as ...
  1134. // ^~
  1135. // 1. Expr
  1136. // 2. DeclOrDefinitionAsImpl
  1137. // impl type_expression as ...
  1138. // ^
  1139. // 1. Expr
  1140. // 2. ImplBeforeAs
  1141. // 3. DeclOrDefinitionAsImpl
  1142. CARBON_PARSE_STATE(ImplAfterIntroducer)
  1143. // Handles processing of an `impl forall` declaration after the implicit
  1144. // parameter list.
  1145. //
  1146. // impl forall [ ... ] as ...
  1147. // ^~
  1148. // 1. Expr
  1149. // impl forall [ ... ] type_expression as ...
  1150. // ^
  1151. // 1. Expr
  1152. // 2. ImplBeforeAs
  1153. CARBON_PARSE_STATE(ImplAfterForall)
  1154. // Handles processing of the `as` in an `impl` declaration after the type
  1155. // expression.
  1156. //
  1157. // impl TypeExpression as ...
  1158. // ^~
  1159. // 1. Expr
  1160. CARBON_PARSE_STATE(ImplBeforeAs)
  1161. // Handles the start of a `var` or `returned var`.
  1162. //
  1163. // var ... (variant is Decl)
  1164. // ^
  1165. // 1. Pattern
  1166. // 2. VarAfterPattern
  1167. // 3. VarFinishAsDecl
  1168. //
  1169. // var ... (variant is For)
  1170. // ^
  1171. // 1. Pattern
  1172. // 2. VarFinishAsFor
  1173. //
  1174. // returned var ... (variant is Returned)
  1175. // ^~~~~~~~~~~~
  1176. // 1. Pattern
  1177. // 2. VarAfterPattern
  1178. // 3. VarFinishAsDecl
  1179. //
  1180. // returned ??? ; (variant is Returned)
  1181. // ^~~~~~~~~~~~~~
  1182. // (state done)
  1183. CARBON_PARSE_STATE_VARIANTS3(Var, Decl, Returned, For)
  1184. // Handles `var` after the pattern, either followed by an initializer or the
  1185. // semicolon.
  1186. //
  1187. // var ... = ...
  1188. // ^
  1189. // var ... ??? = ...
  1190. // ^~~~~
  1191. // 1. Expr
  1192. //
  1193. // var ... ...
  1194. // ^
  1195. // (state done)
  1196. CARBON_PARSE_STATE(VarAfterPattern)
  1197. // Handles `var` parsing at the end.
  1198. //
  1199. // var ... ; (variant is Semicolon)
  1200. // ^
  1201. // var ... ??? ; (variant is Semicolon)
  1202. // ^~~~~
  1203. // (state done)
  1204. //
  1205. // var ... in (variant is For)
  1206. // ^~
  1207. // var ... : (variant is For, invalid)
  1208. // ^
  1209. // (state done)
  1210. CARBON_PARSE_STATE_VARIANTS2(VarFinish, Decl, For)
  1211. // Handles the start of a `let`.
  1212. //
  1213. // let ...
  1214. // ^
  1215. // 1. Pattern
  1216. // 2. LetAfterPattern
  1217. // 3. LetFinish
  1218. CARBON_PARSE_STATE(Let)
  1219. // Handles `let` after the pattern, optionally followed by an initializer. The
  1220. // initializer is required except in an associated constant declaration, but
  1221. // that is enforced by check.
  1222. //
  1223. // let ... = ...
  1224. // ^
  1225. // let ... ??? = ...
  1226. // ^~~~~
  1227. // 1. Expr
  1228. //
  1229. // let ... ;
  1230. // ^
  1231. // let ... ??? ;
  1232. // ^~~
  1233. // (state done)
  1234. CARBON_PARSE_STATE(LetAfterPattern)
  1235. // Handles `let` parsing at the end.
  1236. //
  1237. // let ... ;
  1238. // ^
  1239. // (state done)
  1240. CARBON_PARSE_STATE(LetFinish)
  1241. // Handles a choice's introducer.
  1242. //
  1243. // choice ...
  1244. // ^~~~~~
  1245. // 1. DeclNameAndParamsAsOptional
  1246. // 2. ChoiceAlternativeListStart
  1247. // 3. ChoiceDefinitionFinish
  1248. CARBON_PARSE_STATE(ChoiceIntroducer)
  1249. // Handles processing of a choice after its optional parameters.
  1250. //
  1251. // choice name ... {}
  1252. // ^
  1253. // (state done)
  1254. //
  1255. // choice name ... { ... }
  1256. // ^
  1257. // 1. ChoiceAlternative
  1258. //
  1259. // choice name ... ???
  1260. // ^
  1261. // (state done)
  1262. CARBON_PARSE_STATE(ChoiceDefinitionStart)
  1263. // Starts alternative parsing.
  1264. //
  1265. // name( ... )
  1266. // ^~~~
  1267. // 1. ParamListAsRegular
  1268. // 2. ChoiceAlternativeFinish
  1269. // name ...
  1270. // ^~~~
  1271. // 1. ChoiceAlternativeFinish
  1272. CARBON_PARSE_STATE(ChoiceAlternative)
  1273. // Finishes parsing a choice's alternative, including the optional trailing `,`.
  1274. // If there are more alternatives, enqueues another alternative parsing state.
  1275. //
  1276. // ... , }
  1277. // ^
  1278. // (state done)
  1279. //
  1280. // ... , ...
  1281. // ^
  1282. // 1. ChoiceAlternative
  1283. //
  1284. // ...
  1285. // ^
  1286. // (state done)
  1287. CARBON_PARSE_STATE(ChoiceAlternativeFinish)
  1288. // Finishes a choice definition.
  1289. //
  1290. // choice ... }
  1291. // ^
  1292. // (state done)
  1293. CARBON_PARSE_STATE(ChoiceDefinitionFinish)
  1294. // Handles `match` introducer.
  1295. //
  1296. // match ...
  1297. // ^~~~~
  1298. // 1. ParenConditionAsMatch
  1299. // 2. MatchConditionFinish
  1300. CARBON_PARSE_STATE(MatchIntroducer)
  1301. // Handles `match` cases block start after the condition.
  1302. //
  1303. // match (...) { ... }
  1304. // ^
  1305. // 1. MatchCaseLoop
  1306. // 2. MatchStatementFinish
  1307. //
  1308. // match (...) ???
  1309. // ^
  1310. // (state done)
  1311. CARBON_PARSE_STATE(MatchConditionFinish)
  1312. // Handles `match` cases.
  1313. //
  1314. // match (...) { case ...}
  1315. // ^
  1316. // 1. MatchCaseIntroducer
  1317. // 2. MatchCaseLoop
  1318. // match (...) { default ...}
  1319. // ^
  1320. // 1. MatchDefaultIntroducer
  1321. // 2. MatchCaseLoopAfterDefault
  1322. CARBON_PARSE_STATE(MatchCaseLoop)
  1323. // Handles `match` `case` introducer.
  1324. //
  1325. // match (...) { case ...}
  1326. // ^~~~
  1327. // 1. Pattern
  1328. // 2. MatchCaseAfterPattern
  1329. CARBON_PARSE_STATE(MatchCaseIntroducer)
  1330. // Handles `match` case after pattern.
  1331. //
  1332. // match (...) { case ... => ... }
  1333. // ^
  1334. // 1. MatchCaseStart
  1335. // match (...) { case ... if (...) }
  1336. // ^~~~
  1337. // 1. Expr
  1338. // 2. MatchCaseGuardFinish
  1339. // 3. MatchCaseStart
  1340. CARBON_PARSE_STATE(MatchCaseAfterPattern)
  1341. // Handles `match` case guard closing parenthesis.
  1342. //
  1343. // match (...) { case ... if (...) => ... }
  1344. // ^
  1345. // (state done)
  1346. CARBON_PARSE_STATE(MatchCaseGuardFinish)
  1347. // Handles `match` case `=>` and `{` opening statements block.
  1348. //
  1349. // match (...) { case ... => {...} }
  1350. // ^~~~
  1351. // 1. StatementScopeLoop
  1352. // 2. MatchCaseFinish
  1353. //
  1354. // match (...) { case ... ??? }
  1355. // ^
  1356. // (state done)
  1357. //
  1358. // match (...) { case ... => ??? }
  1359. // ^
  1360. // (state done)
  1361. CARBON_PARSE_STATE(MatchCaseStart)
  1362. // Handles `match` case statements block closing `}`.
  1363. //
  1364. // match (...) { case ... => {...} }
  1365. // ^
  1366. // (state done)
  1367. CARBON_PARSE_STATE(MatchCaseFinish)
  1368. // Handles `match` default introducer, `=>` and `{` opening statements block.
  1369. //
  1370. // match (...) { default => {...} }
  1371. // ^~~~~~~~~~~~
  1372. // 1. StatementScopeLoop
  1373. // 2. MatchDefaultFinish
  1374. //
  1375. // match (...) { default ??? }
  1376. // ^
  1377. // (state done)
  1378. //
  1379. // match (...) { default => ??? }
  1380. // ^
  1381. // (state done)
  1382. CARBON_PARSE_STATE(MatchDefaultIntroducer)
  1383. // Handles `match` default case statements block closing `}`.
  1384. //
  1385. // match (...) { default => {...} }
  1386. // ^
  1387. // (state done)
  1388. CARBON_PARSE_STATE(MatchDefaultFinish)
  1389. // Handles `match` cases after the `default` case.
  1390. //
  1391. // match (...) { default => {...} case ... }
  1392. // ^~~~
  1393. // 1. MatchCaseLoopAfterDefault
  1394. //
  1395. // match (...) { default => {...} default ... }
  1396. // ^~~~~~~
  1397. // 1. MatchCaseLoopAfterDefault
  1398. //
  1399. // match (...) { default => {...} }
  1400. // ^
  1401. // (state done)
  1402. CARBON_PARSE_STATE(MatchCaseLoopAfterDefault)
  1403. // Finishes `match` statement.
  1404. //
  1405. // match (...) {...}
  1406. // ^
  1407. // (state done)
  1408. CARBON_PARSE_STATE(MatchStatementFinish)
  1409. #undef CARBON_PARSE_STATE