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