state.def 34 KB

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