state.def 35 KB

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