state.def 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  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. #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 beginning of a requirement expression after a `where` operator in
  580. // an expression.
  581. // TODO: Also a `require` declaration?
  582. //
  583. // expr where .designator = ...
  584. // ^~~~~~~~~~~~~
  585. // 1. Expr
  586. // 2. RequirementOperatorFinish
  587. // expr where ...
  588. // ^
  589. // 1. Expr
  590. // 2. RequirementOperator
  591. CARBON_PARSE_STATE(RequirementBegin)
  592. // Handles a requirement operator in a `where` expression.
  593. //
  594. // expr where expr impls ...
  595. // ^~~~
  596. // expr where expr = ...
  597. // ^
  598. // expr where expr == ...
  599. // ^~
  600. // 1. Expr
  601. // 2. RequirementOperatorFinish
  602. CARBON_PARSE_STATE(RequirementOperator)
  603. // Finishes a requirement operator in a `where` expression.
  604. //
  605. // expr where expr impls expr
  606. // ^
  607. // expr where expr = expr
  608. // ^
  609. // expr where expr == expr
  610. // ^
  611. // (state done)
  612. // expr where expr impls expr and
  613. // ^~~
  614. // expr where expr = expr and
  615. // ^~~
  616. // expr where expr == expr and
  617. // ^~~
  618. // 1. RequirementBegin
  619. CARBON_PARSE_STATE(RequirementOperatorFinish)
  620. // Finishes an `where` expression.
  621. //
  622. // expr where requirement
  623. // ^
  624. // (state done)
  625. CARBON_PARSE_STATE(WhereFinish)
  626. // Handles the `;` for an expression statement, which is different from most
  627. // keyword statements.
  628. //
  629. // expr ;
  630. // ^
  631. // expr ??? ;
  632. // ^~~~~
  633. // (state done)
  634. CARBON_PARSE_STATE(ExprStatementFinish)
  635. // Handles a function's introducer.
  636. //
  637. // fn ...
  638. // ^
  639. // 1. DeclNameAndParams
  640. // 2. FunctionAfterParams
  641. CARBON_PARSE_STATE(FunctionIntroducer)
  642. // Handles processing of a function's syntax after `)`, primarily the
  643. // possibility a `->` return type is there. Always enqueues signature finish
  644. // handling.
  645. //
  646. // fn F(...) -> ...
  647. // ^~
  648. // 1. Expr
  649. // 2. FunctionReturnTypeFinish
  650. // 3. FunctionSignatureFinish
  651. //
  652. // fn F(...) ...
  653. // ^
  654. // 1. FunctionSignatureFinish
  655. CARBON_PARSE_STATE(FunctionAfterParams)
  656. // Finishes a function return type.
  657. //
  658. // fn F(...) -> expr ...
  659. // ^
  660. // (state done)
  661. CARBON_PARSE_STATE(FunctionReturnTypeFinish)
  662. // Finishes a function signature. If it's a declaration, the function is done;
  663. // otherwise, this also starts definition processing.
  664. //
  665. // fn ... ;
  666. // ^
  667. // (state done)
  668. //
  669. // fn ... {
  670. // ^
  671. // 1. StatementScopeLoop
  672. // 2. FunctionDefinitionFinish
  673. //
  674. // fn ... ??? ;
  675. // ^~~~~
  676. // (state done)
  677. CARBON_PARSE_STATE(FunctionSignatureFinish)
  678. // Finishes a function definition.
  679. //
  680. // fn ... }
  681. // ^
  682. // fn ... ;
  683. // ^
  684. // (state done)
  685. CARBON_PARSE_STATE(FunctionDefinitionFinish)
  686. // Handles `export <name>`.
  687. //
  688. // export Name;
  689. // ^
  690. // 1. DeclNameAndParams
  691. // 2. ExportFinish
  692. CARBON_PARSE_STATE(ExportName)
  693. // Finishes an `export <name>`.
  694. //
  695. // export Name;
  696. // ^
  697. // (state done)
  698. CARBON_PARSE_STATE(ExportNameFinish)
  699. // Handles `import`.
  700. //
  701. // import pkgname [library "libname"] ;
  702. // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  703. // import library "libname" ;
  704. // ^~~~~~~~~~~~~~~~~~~
  705. // import ??? ;
  706. // ^~~~~
  707. // (state done)
  708. CARBON_PARSE_STATE(Import)
  709. // Handles `library` in declaration form.
  710. //
  711. // library "libname" ;
  712. // ^~~~~~~~~~~
  713. // library ??? ;
  714. // ^~~~~
  715. // (state done)
  716. CARBON_PARSE_STATE(Library)
  717. // Handles `namespace`.
  718. //
  719. // namespace ...
  720. // ^
  721. // 1. DeclNameAndParams
  722. // 2. NamespaceFinish
  723. CARBON_PARSE_STATE(Namespace)
  724. // Handles `namespace` after the name.
  725. //
  726. // namespace ... ;
  727. // ^
  728. // namespace ... ??? ;
  729. // ^~~~~
  730. // (state done)
  731. CARBON_PARSE_STATE(NamespaceFinish)
  732. // Handles `package`.
  733. //
  734. // package pkgname [library "libname"] ;
  735. // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  736. // package ??? ;
  737. // ^~~~~
  738. // (state done)
  739. CARBON_PARSE_STATE(Package)
  740. // Handles `alias` after the introducer.
  741. //
  742. // alias ...
  743. // ^
  744. // 1. DeclNameAndParams
  745. // 2. AliasInitializer
  746. CARBON_PARSE_STATE(Alias)
  747. // Handles `alias` after the name.
  748. //
  749. // alias ... = ... ;
  750. // ^
  751. // 1. Expr
  752. // 2. AliasFinish
  753. //
  754. // alias ???
  755. // ^
  756. // (state done)
  757. CARBON_PARSE_STATE(AliasAfterName)
  758. // Handles `alias` at the end.
  759. //
  760. // alias ... ;
  761. // ^
  762. // alias ???
  763. // ^
  764. // (state done)
  765. CARBON_PARSE_STATE(AliasFinish)
  766. // Starts parsing a pattern in a comma-separated list. The variants mark
  767. // whether it is part of an implicit parameter list or a tuple pattern.
  768. //
  769. // ...
  770. // ^
  771. // 1. BindingPattern
  772. // 2. PatternListElementFinishAs(Implicit|Tuple)
  773. CARBON_PARSE_STATE_VARIANTS2(PatternListElement, Implicit, Tuple)
  774. // Finishes parsing a pattern in a comma-separated list, including the
  775. // optional trailing `,`. If there are more patterns, enqueues another
  776. // pattern parsing state.
  777. //
  778. // ... , ) (variant is Tuple)
  779. // ^
  780. // (state done)
  781. //
  782. // ... , ] (variant is Implicit)
  783. // ^
  784. // (state done)
  785. //
  786. // ... , ...
  787. // ^
  788. // 1. PatternListElementAs(Implicit|Tuple)
  789. //
  790. // ...
  791. // ^
  792. // (state done)
  793. CARBON_PARSE_STATE_VARIANTS2(PatternListElementFinish, Implicit, Tuple)
  794. // Handles processing of a tuple pattern (parentheses) or implicit parameter
  795. // list (square brackets).
  796. //
  797. // ( ) (variant is Tuple)
  798. // ^
  799. // [ ] (variant is Implicit)
  800. // ^
  801. // 1. PatternListFinishAs(Tuple|Implicit)
  802. //
  803. // ( ... ) (variant is Tuple)
  804. // ^
  805. // [ ... ] (variant is Implicit)
  806. // ^
  807. // 1. PatternListElementAs(Tuple|Implicit)
  808. // 2. PatternListFinishAs(Tuple|Implicit)
  809. CARBON_PARSE_STATE_VARIANTS2(PatternList, Implicit, Tuple)
  810. // Handles processing of a parameter list `]` or `)`.
  811. //
  812. // ( ... ) (variant is Tuple)
  813. // ^
  814. // [ ... ] (variant is Implicit)
  815. // ^
  816. // (state done)
  817. CARBON_PARSE_STATE_VARIANTS2(PatternListFinish, Implicit, Tuple)
  818. // Handles the processing of a `(condition)` up through the expression.
  819. //
  820. // if/while/match { (invalid)
  821. // ^
  822. // 1. ParenConditionAs(If|While|Match)Finish
  823. //
  824. // if/while/match ( ... )
  825. // ^
  826. // if/while/match ???
  827. // ^
  828. // 1. Expr
  829. // 2. ParenConditionAs(If|While|Match)Finish
  830. CARBON_PARSE_STATE_VARIANTS3(ParenCondition, If, While, Match)
  831. // Finishes the processing of a `(condition)` after the expression.
  832. //
  833. // if/while/match ( expr )
  834. // ^
  835. // if/while/match {
  836. // ^
  837. // if/while/match ??? {
  838. // ^
  839. // (state done)
  840. CARBON_PARSE_STATE_VARIANTS3(ParenConditionFinish, If, While, Match)
  841. // Handles compound member access `(` after `->` or `.`
  842. //
  843. // ( ... )
  844. // ^
  845. // 1. Expr
  846. // 2. OnlyParenExprFinish
  847. CARBON_PARSE_STATE(OnlyParenExpr)
  848. // Handles the `)` after `->(`... or `.(`...
  849. //
  850. // ( ... )
  851. // ^
  852. CARBON_PARSE_STATE(OnlyParenExprFinish)
  853. // Handles the `(` of a parenthesized single expression
  854. //
  855. // ( )
  856. // ^
  857. // 1. TupleLiteralFinish
  858. //
  859. // ( ... )
  860. // ^
  861. // 1. Expr
  862. // 2. ExprAfterOpenParenFinish
  863. // 3. ParenExprFinish (SPECIAL: may be replaced)
  864. CARBON_PARSE_STATE(ParenExpr)
  865. // Handles the `)` of a tuple literal.
  866. //
  867. // ( ... )
  868. // ^
  869. // (state done)
  870. CARBON_PARSE_STATE(TupleLiteralFinish)
  871. // Handles the end of an expression following an open parenthesis.
  872. //
  873. // ( ... , )
  874. // ^
  875. // (state done)
  876. // SPECIAL: parent becomes TupleLiteralFinish
  877. //
  878. // ( ... , ... )
  879. // ^
  880. // 1. Expr
  881. // 2. TupleLiteralElementFinish
  882. // SPECIAL: parent becomes TupleLiteralFinish
  883. //
  884. // ( ... )
  885. // ^
  886. // (state done)
  887. CARBON_PARSE_STATE(ExprAfterOpenParenFinish)
  888. // Handles the end of an expression that is known to be an element of a tuple
  889. // literal expression.
  890. //
  891. // ( ... , )
  892. // ^
  893. // (state done)
  894. //
  895. // ( ... , ... )
  896. // ^
  897. // 1. Expr
  898. // 2. TupleLiteralElementFinish
  899. //
  900. // ( ... )
  901. // ^
  902. // (state done)
  903. CARBON_PARSE_STATE(TupleLiteralElementFinish)
  904. // Handles the `)` of a parenthesized single expression.
  905. //
  906. // ( ... )
  907. // ^
  908. // (state done)
  909. CARBON_PARSE_STATE(ParenExprFinish)
  910. // Handles processing of a pattern.
  911. //
  912. // ( ... )
  913. // ^
  914. // 1. PatternListAsTuple
  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. // template (variant is not Variable)
  924. // ^~~~~~~~
  925. // 4. BindingPatternTemplate
  926. //
  927. // THEN
  928. //
  929. // addr (variant is not Variable)
  930. // ^~~~
  931. // 3. BindingPatternAddr
  932. //
  933. // THEN
  934. //
  935. // name: ...
  936. // ^~~~~
  937. // self: ...
  938. // ^~~~~
  939. // 1. Expr
  940. // 2. BindingPatternFinishAsRegular
  941. //
  942. // name:! ...
  943. // ^~~~~~
  944. // self:! ...
  945. // ^~~~~~
  946. // 1. Expr
  947. // 2. BindingPatternFinishAsGeneric
  948. //
  949. // ???
  950. // ^
  951. // 1. BindingPatternFinishAsRegular
  952. CARBON_PARSE_STATE(BindingPattern)
  953. // Handles `addr` in a binding pattern.
  954. //
  955. // addr name: type
  956. // ^
  957. // (state done)
  958. CARBON_PARSE_STATE(BindingPatternAddr)
  959. // Handles `template` in a binding pattern.
  960. //
  961. // template name:! type
  962. // ^
  963. // (state done)
  964. CARBON_PARSE_STATE(BindingPatternTemplate)
  965. // Finishes binding pattern processing.
  966. //
  967. // name: type
  968. // ^
  969. // (state done)
  970. CARBON_PARSE_STATE_VARIANTS2(BindingPatternFinish, Generic, Regular)
  971. // Handles a single statement. While typically within a statement block, this
  972. // can also be used for error recovery where we expect a statement block and
  973. // are missing braces.
  974. //
  975. // break ...
  976. // ^~~~~
  977. // 1. StatementBreakFinish
  978. //
  979. // continue ...
  980. // ^~~~~~~~
  981. // 1. StatementContinueFinish
  982. //
  983. // for ...
  984. // ^~~
  985. // 1. StatementForHeader
  986. // 2. StatementForFinish
  987. //
  988. // if ...
  989. // ^
  990. // 1. StatementIf
  991. //
  992. // return ...
  993. // ^
  994. // 1. StatementReturn
  995. //
  996. // var ...
  997. // ^
  998. // 1. VarAsDecl
  999. //
  1000. // returned ...
  1001. // ^
  1002. // 1. VarAsReturned
  1003. //
  1004. // while ...
  1005. // ^
  1006. // 1. StatementWhile
  1007. //
  1008. // ...
  1009. // ^
  1010. // 1. Expr
  1011. // 2. ExprStatementFinish
  1012. //
  1013. // match ...
  1014. // ^
  1015. // 1. MatchIntroducer
  1016. CARBON_PARSE_STATE(Statement)
  1017. // Handles `break` processing at the `;`.
  1018. //
  1019. // break ;
  1020. // ^
  1021. // (state done)
  1022. CARBON_PARSE_STATE(StatementBreakFinish)
  1023. // Handles `continue` processing at the `;`.
  1024. //
  1025. // continue ;
  1026. // ^
  1027. // (state done)
  1028. CARBON_PARSE_STATE(StatementContinueFinish)
  1029. // Handles `for` processing of `(var`, proceeding to a binding pattern before
  1030. // continuing.
  1031. //
  1032. // for ( var ... )
  1033. // ^
  1034. // 1. VarAsFor
  1035. // 2. StatementForHeaderIn
  1036. //
  1037. // for ( ???
  1038. // ^
  1039. // for ( ??? in ...
  1040. // ^~~~~~~~
  1041. // for ??? in ...
  1042. // ^~~~~~
  1043. // for ???
  1044. // ^
  1045. // 1. StatementForHeaderIn
  1046. CARBON_PARSE_STATE(StatementForHeader)
  1047. // Handles `for` processing of `in`, proceeding to an expression before
  1048. // continuing.
  1049. //
  1050. // for ( ... in ... )
  1051. // ^
  1052. // 1. Expr
  1053. // 2. StatementForHeaderFinish
  1054. CARBON_PARSE_STATE(StatementForHeaderIn)
  1055. // Handles `for` processing of `)`, proceeding to the statement block.
  1056. //
  1057. // for ( ... ) ...
  1058. // ^
  1059. // 1. CodeBlock
  1060. CARBON_PARSE_STATE(StatementForHeaderFinish)
  1061. // Handles `for` processing after the final `}`.
  1062. //
  1063. // for ( ... ) { ... }
  1064. // ^
  1065. // (state done)
  1066. CARBON_PARSE_STATE(StatementForFinish)
  1067. // Handles `if` processing at the start.
  1068. //
  1069. // if ...
  1070. // ^~
  1071. // 1. ParenConditionAsIf
  1072. // 2. StatementIfConditionFinish
  1073. CARBON_PARSE_STATE(StatementIf)
  1074. // Handles `if` processing between the condition and start of the first code
  1075. // block.
  1076. //
  1077. // if ( ... ) ...
  1078. // ^
  1079. // 1. CodeBlock
  1080. // 2. StatementIfThenBlockFinish
  1081. CARBON_PARSE_STATE(StatementIfConditionFinish)
  1082. // Handles `if` processing after the end of the first code block, with the
  1083. // optional `else`.
  1084. //
  1085. // if ( ... ) { ... } else if ...
  1086. // ^~~~
  1087. // 1. StatementIf
  1088. // 2. StatementIfElseBlockFinish
  1089. //
  1090. // if ( ... ) { ... } else ...
  1091. // ^~~~
  1092. // 1. CodeBlock
  1093. // 2. StatementIfElseBlockFinish
  1094. //
  1095. // if ( ... ) { ... } ...
  1096. // (state done)
  1097. CARBON_PARSE_STATE(StatementIfThenBlockFinish)
  1098. // Handles `if` processing after a provided `else` code block.
  1099. //
  1100. // if ( ... ) { ... } else { ... }
  1101. // ^
  1102. // (state done)
  1103. CARBON_PARSE_STATE(StatementIfElseBlockFinish)
  1104. // Handles `return` processing.
  1105. //
  1106. // return ;
  1107. // ^~~~~~
  1108. // 1. StatementReturnFinish
  1109. //
  1110. // return var ...
  1111. // ^~~~~~~~~~
  1112. // 1. StatementReturnFinish
  1113. //
  1114. // return ...
  1115. // ^~~~~~
  1116. // 1. Expr
  1117. // 2. StatementReturnFinish
  1118. CARBON_PARSE_STATE(StatementReturn)
  1119. // Handles `return` processing at the `;`.
  1120. //
  1121. // return ... ;
  1122. // ^
  1123. // (state done)
  1124. CARBON_PARSE_STATE(StatementReturnFinish)
  1125. // Handles processing of statements within a scope.
  1126. //
  1127. // { ... }
  1128. // ^
  1129. // (state done)
  1130. //
  1131. // { ... ... }
  1132. // ^
  1133. // 1. Statement
  1134. // 2. StatementScopeLoop
  1135. CARBON_PARSE_STATE(StatementScopeLoop)
  1136. // Handles `while` processing.
  1137. //
  1138. // while ...
  1139. // ^~~~~
  1140. // 1. ParenConditionAsWhile
  1141. // 2. StatementWhileConditionFinish
  1142. CARBON_PARSE_STATE(StatementWhile)
  1143. // Handles `while` processing between the condition and start of the code block.
  1144. //
  1145. // while ( ... ) ...
  1146. // ^
  1147. // 1. CodeBlock
  1148. // 2. StatementWhileBlockFinish
  1149. CARBON_PARSE_STATE(StatementWhileConditionFinish)
  1150. // Handles `while` processing after the end of the code block.
  1151. //
  1152. // while ( ... ) { ... }
  1153. // ^
  1154. // (state done)
  1155. CARBON_PARSE_STATE(StatementWhileBlockFinish)
  1156. // Handles parsing after the declaration scope of a type.
  1157. //
  1158. // class/impl/interface/constraint ... { ... }
  1159. // ^
  1160. // (state done)
  1161. CARBON_PARSE_STATE_VARIANTS4(DeclDefinitionFinish, Class, Impl, Interface,
  1162. NamedConstraint)
  1163. // Handles processing of a type after its introducer.
  1164. //
  1165. // class/interface/constraint ...
  1166. // ^
  1167. // 1. DeclNameAndParams
  1168. // 2. DeclOrDefinitionAs(Class|Interface|NamedConstraint)
  1169. CARBON_PARSE_STATE_VARIANTS3(TypeAfterIntroducer, Class, Interface,
  1170. NamedConstraint)
  1171. // Handles processing of a type after its optional parameters.
  1172. //
  1173. // class/impl/interface/constraint name ( ... ) {
  1174. // ^
  1175. // 1. DeclScopeLoop
  1176. // 2. DeclDefinitionFinishAs(Class|Impl|Interface|NamedConstraint)
  1177. //
  1178. // class/impl/interface/constraint name ( ... ) ;
  1179. // ^
  1180. // class/impl/interface/constraint name ( ... ) ???
  1181. // ^
  1182. // (state done)
  1183. CARBON_PARSE_STATE_VARIANTS4(DeclOrDefinition, Class, Impl, Interface,
  1184. NamedConstraint)
  1185. // Handles processing of an `adapt T` declaration after the introducer.
  1186. //
  1187. // adapt T ;
  1188. // ^
  1189. // 1. Expr
  1190. // 2. AdaptDecl
  1191. CARBON_PARSE_STATE(AdaptAfterIntroducer)
  1192. // Handles processing of a completed `adapt T` declaration.
  1193. //
  1194. // adapt T ;
  1195. // ^
  1196. // adapt T ??? ;
  1197. // ^~~~~
  1198. // (state done)
  1199. CARBON_PARSE_STATE(AdaptDecl)
  1200. // Handles processing of a `base: B` after the introducer.
  1201. //
  1202. // base: B ;
  1203. // ^
  1204. // 1. Expr
  1205. // 2. BaseDecl
  1206. // base ??? ;
  1207. // ^~~
  1208. // (state done)
  1209. CARBON_PARSE_STATE(BaseAfterIntroducer)
  1210. // Handles processing of a completed `base: B` declaration.
  1211. //
  1212. // base: B ;
  1213. // ^
  1214. // base: B ??? ;
  1215. // ^~~~~
  1216. // (state done)
  1217. CARBON_PARSE_STATE(BaseDecl)
  1218. // Handles processing of an `impl...as` declaration after the introducer.
  1219. //
  1220. // impl forall [ ...
  1221. // ^~~~~~
  1222. // 1. PatternListAsImplicit
  1223. // 2. ImplAfterForall
  1224. // 3. DeclOrDefinitionAsImpl
  1225. // impl as ...
  1226. // ^~
  1227. // 1. Expr
  1228. // 2. DeclOrDefinitionAsImpl
  1229. // impl type_expression as ...
  1230. // ^
  1231. // 1. Expr
  1232. // 2. ImplBeforeAs
  1233. // 3. DeclOrDefinitionAsImpl
  1234. CARBON_PARSE_STATE(ImplAfterIntroducer)
  1235. // Handles processing of an `impl forall` declaration after the implicit
  1236. // parameter list.
  1237. //
  1238. // impl forall [ ... ] as ...
  1239. // ^~
  1240. // 1. Expr
  1241. // impl forall [ ... ] type_expression as ...
  1242. // ^
  1243. // 1. Expr
  1244. // 2. ImplBeforeAs
  1245. CARBON_PARSE_STATE(ImplAfterForall)
  1246. // Handles processing of the `as` in an `impl` declaration after the type
  1247. // expression.
  1248. //
  1249. // impl TypeExpression as ...
  1250. // ^~
  1251. // 1. Expr
  1252. CARBON_PARSE_STATE(ImplBeforeAs)
  1253. // Handles the start of a `var` or `returned var`.
  1254. //
  1255. // var ... (variant is Decl)
  1256. // ^
  1257. // 1. Pattern
  1258. // 2. VarAfterPattern
  1259. // 3. VarFinishAsDecl
  1260. //
  1261. // var ... (variant is For)
  1262. // ^
  1263. // 1. Pattern
  1264. // 2. VarFinishAsFor
  1265. //
  1266. // returned var ... (variant is Returned)
  1267. // ^~~~~~~~~~~~
  1268. // 1. Pattern
  1269. // 2. VarAfterPattern
  1270. // 3. VarFinishAsDecl
  1271. //
  1272. // returned ??? ; (variant is Returned)
  1273. // ^~~~~~~~~~~~~~
  1274. // (state done)
  1275. CARBON_PARSE_STATE_VARIANTS3(Var, Decl, Returned, For)
  1276. // Handles `var` after the pattern, either followed by an initializer or the
  1277. // semicolon.
  1278. //
  1279. // var ... = ...
  1280. // ^
  1281. // var ... ??? = ...
  1282. // ^~~~~
  1283. // 1. Expr
  1284. //
  1285. // var ... ...
  1286. // ^
  1287. // (state done)
  1288. CARBON_PARSE_STATE(VarAfterPattern)
  1289. // Handles `var` parsing at the end.
  1290. //
  1291. // var ... ; (variant is Semicolon)
  1292. // ^
  1293. // var ... ??? ; (variant is Semicolon)
  1294. // ^~~~~
  1295. // (state done)
  1296. //
  1297. // var ... in (variant is For)
  1298. // ^~
  1299. // var ... : (variant is For, invalid)
  1300. // ^
  1301. // (state done)
  1302. CARBON_PARSE_STATE_VARIANTS2(VarFinish, Decl, For)
  1303. // Handles the start of a `let`.
  1304. //
  1305. // let ...
  1306. // ^
  1307. // 1. Pattern
  1308. // 2. LetAfterPattern
  1309. // 3. LetFinish
  1310. CARBON_PARSE_STATE(Let)
  1311. // Handles `let` after the pattern, optionally followed by an initializer. The
  1312. // initializer is required except in an associated constant declaration, but
  1313. // that is enforced by check.
  1314. //
  1315. // let ... = ...
  1316. // ^
  1317. // let ... ??? = ...
  1318. // ^~~~~
  1319. // 1. Expr
  1320. //
  1321. // let ... ;
  1322. // ^
  1323. // let ... ??? ;
  1324. // ^~~
  1325. // (state done)
  1326. CARBON_PARSE_STATE(LetAfterPattern)
  1327. // Handles `let` parsing at the end.
  1328. //
  1329. // let ... ;
  1330. // ^
  1331. // (state done)
  1332. CARBON_PARSE_STATE(LetFinish)
  1333. // Handles a choice's introducer.
  1334. //
  1335. // choice ...
  1336. // ^~~~~~
  1337. // 1. DeclNameAndParams
  1338. // 2. ChoiceAlternativeListStart
  1339. // 3. ChoiceDefinitionFinish
  1340. CARBON_PARSE_STATE(ChoiceIntroducer)
  1341. // Handles processing of a choice after its optional parameters.
  1342. //
  1343. // choice name ... {}
  1344. // ^
  1345. // (state done)
  1346. //
  1347. // choice name ... { ... }
  1348. // ^
  1349. // 1. ChoiceAlternative
  1350. //
  1351. // choice name ... ???
  1352. // ^
  1353. // (state done)
  1354. CARBON_PARSE_STATE(ChoiceDefinitionStart)
  1355. // Starts alternative parsing.
  1356. //
  1357. // name( ... )
  1358. // ^~~~
  1359. // 1. ParamListAsRegular
  1360. // 2. ChoiceAlternativeFinish
  1361. // name ...
  1362. // ^~~~
  1363. // 1. ChoiceAlternativeFinish
  1364. CARBON_PARSE_STATE(ChoiceAlternative)
  1365. // Finishes parsing a choice's alternative, including the optional trailing `,`.
  1366. // If there are more alternatives, enqueues another alternative parsing state.
  1367. //
  1368. // ... , }
  1369. // ^
  1370. // (state done)
  1371. //
  1372. // ... , ...
  1373. // ^
  1374. // 1. ChoiceAlternative
  1375. //
  1376. // ...
  1377. // ^
  1378. // (state done)
  1379. CARBON_PARSE_STATE(ChoiceAlternativeFinish)
  1380. // Finishes a choice definition.
  1381. //
  1382. // choice ... }
  1383. // ^
  1384. // (state done)
  1385. CARBON_PARSE_STATE(ChoiceDefinitionFinish)
  1386. // Handles `match` introducer.
  1387. //
  1388. // match ...
  1389. // ^~~~~
  1390. // 1. ParenConditionAsMatch
  1391. // 2. MatchConditionFinish
  1392. CARBON_PARSE_STATE(MatchIntroducer)
  1393. // Handles `match` cases block start after the condition.
  1394. //
  1395. // match (...) { ... }
  1396. // ^
  1397. // 1. MatchCaseLoop
  1398. // 2. MatchStatementFinish
  1399. //
  1400. // match (...) ???
  1401. // ^
  1402. // (state done)
  1403. CARBON_PARSE_STATE(MatchConditionFinish)
  1404. // Handles `match` cases.
  1405. //
  1406. // match (...) { case ...}
  1407. // ^
  1408. // 1. MatchCaseIntroducer
  1409. // 2. MatchCaseLoop
  1410. // match (...) { default ...}
  1411. // ^
  1412. // 1. MatchDefaultIntroducer
  1413. // 2. MatchCaseLoopAfterDefault
  1414. CARBON_PARSE_STATE(MatchCaseLoop)
  1415. // Handles `match` `case` introducer.
  1416. //
  1417. // match (...) { case ...}
  1418. // ^~~~
  1419. // 1. Pattern
  1420. // 2. MatchCaseAfterPattern
  1421. CARBON_PARSE_STATE(MatchCaseIntroducer)
  1422. // Handles `match` case after pattern.
  1423. //
  1424. // match (...) { case ... => ... }
  1425. // ^
  1426. // 1. MatchCaseStart
  1427. // match (...) { case ... if (...) }
  1428. // ^~~~
  1429. // 1. Expr
  1430. // 2. MatchCaseGuardFinish
  1431. // 3. MatchCaseStart
  1432. CARBON_PARSE_STATE(MatchCaseAfterPattern)
  1433. // Handles `match` case guard closing parenthesis.
  1434. //
  1435. // match (...) { case ... if (...) => ... }
  1436. // ^
  1437. // (state done)
  1438. CARBON_PARSE_STATE(MatchCaseGuardFinish)
  1439. // Handles `match` case `=>` and `{` opening statements block.
  1440. //
  1441. // match (...) { case ... => {...} }
  1442. // ^~~~
  1443. // 1. StatementScopeLoop
  1444. // 2. MatchCaseFinish
  1445. //
  1446. // match (...) { case ... ??? }
  1447. // ^
  1448. // (state done)
  1449. //
  1450. // match (...) { case ... => ??? }
  1451. // ^
  1452. // (state done)
  1453. CARBON_PARSE_STATE(MatchCaseStart)
  1454. // Handles `match` case statements block closing `}`.
  1455. //
  1456. // match (...) { case ... => {...} }
  1457. // ^
  1458. // (state done)
  1459. CARBON_PARSE_STATE(MatchCaseFinish)
  1460. // Handles `match` default introducer, `=>` and `{` opening statements block.
  1461. //
  1462. // match (...) { default => {...} }
  1463. // ^~~~~~~~~~~~
  1464. // 1. StatementScopeLoop
  1465. // 2. MatchDefaultFinish
  1466. //
  1467. // match (...) { default ??? }
  1468. // ^
  1469. // (state done)
  1470. //
  1471. // match (...) { default => ??? }
  1472. // ^
  1473. // (state done)
  1474. CARBON_PARSE_STATE(MatchDefaultIntroducer)
  1475. // Handles `match` default case statements block closing `}`.
  1476. //
  1477. // match (...) { default => {...} }
  1478. // ^
  1479. // (state done)
  1480. CARBON_PARSE_STATE(MatchDefaultFinish)
  1481. // Handles `match` cases after the `default` case.
  1482. //
  1483. // match (...) { default => {...} case ... }
  1484. // ^~~~
  1485. // 1. MatchCaseLoopAfterDefault
  1486. //
  1487. // match (...) { default => {...} default ... }
  1488. // ^~~~~~~
  1489. // 1. MatchCaseLoopAfterDefault
  1490. //
  1491. // match (...) { default => {...} }
  1492. // ^
  1493. // (state done)
  1494. CARBON_PARSE_STATE(MatchCaseLoopAfterDefault)
  1495. // Finishes `match` statement.
  1496. //
  1497. // match (...) {...}
  1498. // ^
  1499. // (state done)
  1500. CARBON_PARSE_STATE(MatchStatementFinish)
  1501. #undef CARBON_PARSE_STATE