state.def 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // This is an X-macro header. It does not use `#include` guards, and instead is
  6. // designed to be `#include`ed after the x-macro is defined in order for its
  7. // inclusion to expand to the desired output. Macro definitions are cleaned up
  8. // at the end of this file.
  9. //
  10. // Supported x-macros are:
  11. // - CARBON_PARSE_STATE(Name)
  12. // Defines a parser state.
  13. //
  14. // Parser states may be clustered when there are multiple related variants,
  15. // named `StateAsVariant`. When there are variants, they share a common helper
  16. // function for most logic.
  17. //
  18. // The comments before each state describe the portion of the grammar that the
  19. // state is implementing, by giving an example of each kind of token sequence
  20. // that this state handles. In these examples, `...` indicates a sequence of
  21. // tokens handled by some other state, and `???` indicates a sequence of invalid
  22. // tokens. A trailing `??? ;` indicates an attempt to skip to the end of the
  23. // declaration, which may or may not actually find a `;` token.
  24. //
  25. // The position in the token stream before the state is indicated by the caret
  26. // `^` on the line below the example, and all tokens consumed by the state are
  27. // underlined by the caret and following `~`s. If no tokens are consumed, the
  28. // caret will point between tokens. Therefore, the position in the token stream
  29. // after the state is the first token in the example after the underlined
  30. // region.
  31. //
  32. // Following each set of examples, the output states for that situation are
  33. // listed. States are numbered in the order they'll be executed; in other
  34. // words, `1` is the top of the state stack. The comment `(state done)`
  35. // indicates that no new states are added to the stack.
  36. #ifndef CARBON_PARSE_STATE
  37. #error "Must define the x-macro to use this file."
  38. #define CARBON_PARSE_STATE(Name)
  39. #endif
  40. // Use CARBON_PARSE_STATE_VARIANTSN(State, Variant1, Variant2, ...) to generate
  41. // StateAsVariant1, StateAsVariant2, ... states.
  42. #define CARBON_PARSE_STATE_VARIANT(State, Variant) \
  43. CARBON_PARSE_STATE(State##As##Variant)
  44. #define CARBON_PARSE_STATE_VARIANTS2(State, Variant1, Variant2) \
  45. CARBON_PARSE_STATE_VARIANT(State, Variant1) \
  46. CARBON_PARSE_STATE_VARIANT(State, Variant2)
  47. #define CARBON_PARSE_STATE_VARIANTS3(State, Variant1, Variant2, Variant3) \
  48. CARBON_PARSE_STATE_VARIANT(State, Variant1) \
  49. CARBON_PARSE_STATE_VARIANTS2(State, Variant2, Variant3)
  50. #define CARBON_PARSE_STATE_VARIANTS4(State, Variant1, Variant2, Variant3, \
  51. Variant4) \
  52. CARBON_PARSE_STATE_VARIANT(State, Variant1) \
  53. CARBON_PARSE_STATE_VARIANTS3(State, Variant2, Variant3, Variant4)
  54. // Used as a default for StateStackEntry initialization in some cases. Should
  55. // not be put on the state stack.
  56. CARBON_PARSE_STATE(Invalid)
  57. // Handles an index expression:
  58. //
  59. // a[0]
  60. // ^
  61. // 1. Expr
  62. // 2. IndexExprFinish
  63. CARBON_PARSE_STATE(IndexExpr)
  64. // Handles finishing the index expression.
  65. //
  66. // a[0]
  67. // ^
  68. // (state done)
  69. CARBON_PARSE_STATE(IndexExprFinish)
  70. // Handles an array expression.
  71. //
  72. // [T; N]
  73. // ^
  74. // 1. Expr
  75. // 2. ArrayExprSemi
  76. CARBON_PARSE_STATE(ArrayExpr)
  77. // Handles ';' in an array expression.
  78. //
  79. // [T;]
  80. // ^
  81. // 1. ArrayExprFinish
  82. //
  83. // [T; N]
  84. // ^
  85. // 1. Expr
  86. // 2. ArrayExprFinish
  87. CARBON_PARSE_STATE(ArrayExprSemi)
  88. // Handles finishing the array expression.
  89. //
  90. // [T;]
  91. // ^
  92. // [T; N]
  93. // ^
  94. // (state done)
  95. CARBON_PARSE_STATE(ArrayExprFinish)
  96. // Handles the `{` of a brace expression.
  97. //
  98. // {}
  99. // ^
  100. // 1. BraceExprFinishAsUnknown
  101. //
  102. // { ... }
  103. // ^
  104. // 1. BraceExprParamAsUnknown
  105. // 2. BraceExprFinishAsUnknown
  106. CARBON_PARSE_STATE(BraceExpr)
  107. // Handles a brace expression parameter. Note this will always start as unknown,
  108. // but should be known after the first valid parameter. All later inconsistent
  109. // parameters are invalid.
  110. //
  111. // { .foo ... }
  112. // ^
  113. // 1. PeriodAsStruct
  114. // 2. BraceExprParamAfterDesignatorAs(Type|Value|Unknown)
  115. //
  116. // { ???
  117. // ^
  118. // 1. BraceExprParamFinishAs(Type|Value|Unknown)
  119. CARBON_PARSE_STATE_VARIANTS3(BraceExprParam, Type, Value, Unknown)
  120. // Handles a brace expression parameter after the initial designator. This
  121. // should be at a `:` or `=`, depending on whether it's a type or value literal.
  122. //
  123. // { .foo = bar ... }
  124. // ^
  125. // 1. Expr
  126. // 2. BraceExprParamFinishAsValue
  127. //
  128. // { .foo: bar ... }
  129. // ^
  130. // 1. Expr
  131. // 2. BraceExprParamFinishAsType
  132. //
  133. // { .foo ???
  134. // ^
  135. // 1. BraceExprParamFinishAs(Type|Value|Unknown)
  136. CARBON_PARSE_STATE_VARIANTS3(BraceExprParamAfterDesignator, Type, Value,
  137. Unknown)
  138. // Handles the end of a brace expression parameter.
  139. //
  140. // { ... }
  141. // ^
  142. // (state done)
  143. //
  144. // { .foo = bar, ... }
  145. // ^
  146. // 1. BraceExprParamAsValue
  147. //
  148. // { .foo: bar, ... }
  149. // ^
  150. // 1. BraceExprParamAsType
  151. //
  152. // { ??? , ... }
  153. // ^
  154. // 1. BraceExprParamAsUnknown
  155. CARBON_PARSE_STATE_VARIANTS3(BraceExprParamFinish, Type, Value, Unknown)
  156. // Handles the `}` of a brace expression.
  157. //
  158. // { ... }
  159. // ^
  160. // (state done)
  161. CARBON_PARSE_STATE_VARIANTS3(BraceExprFinish, Type, Value, Unknown)
  162. // Handles a call expression `(...)`.
  163. //
  164. // F()
  165. // ^
  166. // 1. CallExprFinish
  167. //
  168. // F( ...
  169. // ^
  170. // 1. Expr
  171. // 2. CallExprParamFinish
  172. // 3. CallExprFinish
  173. CARBON_PARSE_STATE(CallExpr)
  174. // Handles the `,` or `)` after a call parameter.
  175. //
  176. // F(a, ...)
  177. // ^
  178. // 1. Expr
  179. // 2. CallExprParamFinish
  180. //
  181. // F(a )
  182. // ^
  183. // (state done)
  184. CARBON_PARSE_STATE(CallExprParamFinish)
  185. // Handles finishing the call expression.
  186. //
  187. // F(a, b)
  188. // ^
  189. // (state done)
  190. CARBON_PARSE_STATE(CallExprFinish)
  191. // Handles processing at the `{` on a typical code block.
  192. //
  193. // if (cond) {
  194. // ^
  195. // 1. StatementScopeLoop
  196. // 2. CodeBlockFinish
  197. //
  198. // if (cond) ???
  199. // ^
  200. // 1. Statement
  201. // 2. CodeBlockFinish
  202. CARBON_PARSE_STATE(CodeBlock)
  203. // Handles processing at the `}` on a typical code block, after a statement
  204. // scope is done.
  205. //
  206. // if (cond) { ... }
  207. // ^
  208. // (state done)
  209. CARBON_PARSE_STATE(CodeBlockFinish)
  210. // Handles a declaration name and parameters, such as `Foo[...](...)`.
  211. //
  212. // name . ...
  213. // ^~~~~~
  214. // 1. DeclNameAndParams
  215. //
  216. // name [ ... ]
  217. // ^~~~
  218. // 1. PatternListAsImplicit
  219. // 2. DeclNameAndParamsAfterImplicit
  220. //
  221. // name ( ... )
  222. // ^~~~
  223. // 1. PatternListAsTuple
  224. // 2. DeclNameAndParamsAfterParams
  225. //
  226. // name ...
  227. // ^~~~
  228. // (state done)
  229. //
  230. // ???
  231. // ^
  232. // (state done)
  233. CARBON_PARSE_STATE(DeclNameAndParams)
  234. // Inside a declaration, conditionally starts explicit parameter handling. Only
  235. // used after implicit parameters.
  236. //
  237. // name [ ... ] ( ... )
  238. // ^
  239. // 1. PatternListAsTuple
  240. // 2. DeclNameAndParamsAfterParams
  241. //
  242. // name [ ... ] ???
  243. // ^
  244. // (state done)
  245. CARBON_PARSE_STATE(DeclNameAndParamsAfterImplicit)
  246. // Inside a declaration, conditionally starts another qualified name. Only used
  247. // after regular parameters.
  248. //
  249. // name [ ... ] ( ... ) .
  250. // ^
  251. // 1. DeclNameAndParams
  252. //
  253. // name [ ... ] ( ... ) ...
  254. // ^
  255. // (state done)
  256. CARBON_PARSE_STATE(DeclNameAndParamsAfterParams)
  257. // Handles processing of a declaration. Things like fn, class, interface, and so
  258. // on.
  259. //
  260. // abstract
  261. // ^~~~~~~~
  262. // base class
  263. // ^~~~
  264. // default
  265. // ^~~~~~~
  266. // export import
  267. // ^~~~~~
  268. // extend base
  269. // ^~~~~~
  270. // final
  271. // ^~~~~
  272. // impl fn
  273. // ^~~~
  274. // private
  275. // ^~~~~~~
  276. // protected
  277. // ^~~~~~~~~
  278. // virtual
  279. // ^~~~~~~
  280. // 1. Decl
  281. //
  282. // adapt ...
  283. // ^~~~~
  284. // 1. AdaptAfterIntroducer
  285. //
  286. // alias ...
  287. // ^~~~~
  288. // 1. Alias
  289. //
  290. // base : ...
  291. // ^~~~
  292. // 1. BaseAfterIntroducer
  293. //
  294. // choice ...
  295. // ^~~~~~
  296. // 1. ChoiceIntroducer
  297. //
  298. // class ...
  299. // ^~~~~
  300. // 1. TypeAfterIntroducerAsClass
  301. //
  302. // constraint ...
  303. // ^~~~~~~~~~
  304. // 1. TypeAfterIntroducerAsNamedConstraint
  305. //
  306. // export <name> ...
  307. // ^~~~~~
  308. // 1. ExportName
  309. //
  310. // fn ...
  311. // ^~
  312. // 1. FunctionIntroducer
  313. //
  314. // impl ...
  315. // ^~~~
  316. // 1. ImplAfterIntroducer
  317. //
  318. // import ... (in packaging directives)
  319. // ^~~~~~
  320. // 1. Import
  321. //
  322. // interface ...
  323. // ^~~~~~~~~
  324. // 1. TypeAfterIntroducerAsInterface
  325. //
  326. // let ...
  327. // ^~~
  328. // 1. Let
  329. //
  330. // library ... (in packaging directives)
  331. // ^~~~~~~
  332. // 1. Library
  333. //
  334. // namespace ...
  335. // ^~~~~~~~~
  336. // 1. Namespace
  337. //
  338. // package ... (in packaging directives)
  339. // ^~~~~~~
  340. // 1. Package
  341. //
  342. // var ...
  343. // ^~~
  344. // 1. VarAsDecl
  345. //
  346. // ;
  347. // ^
  348. // (state done)
  349. //
  350. // ??? ;
  351. // ^~~~~
  352. // (state done)
  353. CARBON_PARSE_STATE(Decl)
  354. // Handles processing of a declaration scope, which contains a sequence of
  355. // declarations.
  356. //
  357. // }
  358. // ^
  359. // <eof>
  360. // ^
  361. // (state done)
  362. //
  363. // ...
  364. // ^
  365. // 1. Decl
  366. // 2. DeclScopeLoop
  367. //
  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. // addr (variant is not Variable)
  924. // ^~~~
  925. // 3. BindingPatternAddr
  926. //
  927. // THEN
  928. //
  929. // name: ...
  930. // ^~~~~
  931. // self: ...
  932. // ^~~~~
  933. // 1. Expr
  934. // 2. BindingPatternFinishAsRegular
  935. //
  936. // [template] name:! ...
  937. // ^~~~~~~~~~~~~~~~~
  938. // [template] self:! ...
  939. // ^~~~~~~~~~~~~~~~~
  940. // 1. Expr
  941. // 2. BindingPatternFinishAsGeneric
  942. //
  943. // ???
  944. // ^
  945. // 1. BindingPatternFinishAsRegular
  946. CARBON_PARSE_STATE(BindingPattern)
  947. // Handles `addr` in a binding pattern.
  948. //
  949. // addr name: type
  950. // ^
  951. // (state done)
  952. CARBON_PARSE_STATE(BindingPatternAddr)
  953. // Finishes binding pattern processing.
  954. //
  955. // name: type
  956. // ^
  957. // (state done)
  958. CARBON_PARSE_STATE_VARIANTS2(BindingPatternFinish, Generic, Regular)
  959. // Handles a single statement. While typically within a statement block, this
  960. // can also be used for error recovery where we expect a statement block and
  961. // are missing braces.
  962. //
  963. // break ...
  964. // ^~~~~
  965. // 1. StatementBreakFinish
  966. //
  967. // continue ...
  968. // ^~~~~~~~
  969. // 1. StatementContinueFinish
  970. //
  971. // for ...
  972. // ^~~
  973. // 1. StatementForHeader
  974. // 2. StatementForFinish
  975. //
  976. // if ...
  977. // ^
  978. // 1. StatementIf
  979. //
  980. // return ...
  981. // ^
  982. // 1. StatementReturn
  983. //
  984. // returned ...
  985. // ^
  986. // 1. VarAsReturned
  987. //
  988. // while ...
  989. // ^
  990. // 1. StatementWhile
  991. //
  992. // ...
  993. // ^
  994. // 1. Expr
  995. // 2. ExprStatementFinish
  996. //
  997. // match ...
  998. // ^
  999. // 1. MatchIntroducer
  1000. //
  1001. // <decl introducer or modifier>
  1002. // ^
  1003. // 1. Decl
  1004. CARBON_PARSE_STATE(Statement)
  1005. // Handles `break` processing at the `;`.
  1006. //
  1007. // break ;
  1008. // ^
  1009. // (state done)
  1010. CARBON_PARSE_STATE(StatementBreakFinish)
  1011. // Handles `continue` processing at the `;`.
  1012. //
  1013. // continue ;
  1014. // ^
  1015. // (state done)
  1016. CARBON_PARSE_STATE(StatementContinueFinish)
  1017. // Handles `for` processing of `(var`, proceeding to a binding pattern before
  1018. // continuing.
  1019. //
  1020. // for ( var ... )
  1021. // ^
  1022. // 1. VarAsFor
  1023. // 2. StatementForHeaderIn
  1024. //
  1025. // for ( ???
  1026. // ^
  1027. // for ( ??? in ...
  1028. // ^~~~~~~~
  1029. // for ??? in ...
  1030. // ^~~~~~
  1031. // for ???
  1032. // ^
  1033. // 1. StatementForHeaderIn
  1034. CARBON_PARSE_STATE(StatementForHeader)
  1035. // Handles `for` processing of `in`, proceeding to an expression before
  1036. // continuing.
  1037. //
  1038. // for ( ... in ... )
  1039. // ^
  1040. // 1. Expr
  1041. // 2. StatementForHeaderFinish
  1042. CARBON_PARSE_STATE(StatementForHeaderIn)
  1043. // Handles `for` processing of `)`, proceeding to the statement block.
  1044. //
  1045. // for ( ... ) ...
  1046. // ^
  1047. // 1. CodeBlock
  1048. CARBON_PARSE_STATE(StatementForHeaderFinish)
  1049. // Handles `for` processing after the final `}`.
  1050. //
  1051. // for ( ... ) { ... }
  1052. // ^
  1053. // (state done)
  1054. CARBON_PARSE_STATE(StatementForFinish)
  1055. // Handles `if` processing at the start.
  1056. //
  1057. // if ...
  1058. // ^~
  1059. // 1. ParenConditionAsIf
  1060. // 2. StatementIfConditionFinish
  1061. CARBON_PARSE_STATE(StatementIf)
  1062. // Handles `if` processing between the condition and start of the first code
  1063. // block.
  1064. //
  1065. // if ( ... ) ...
  1066. // ^
  1067. // 1. CodeBlock
  1068. // 2. StatementIfThenBlockFinish
  1069. CARBON_PARSE_STATE(StatementIfConditionFinish)
  1070. // Handles `if` processing after the end of the first code block, with the
  1071. // optional `else`.
  1072. //
  1073. // if ( ... ) { ... } else if ...
  1074. // ^~~~
  1075. // 1. StatementIf
  1076. // 2. StatementIfElseBlockFinish
  1077. //
  1078. // if ( ... ) { ... } else ...
  1079. // ^~~~
  1080. // 1. CodeBlock
  1081. // 2. StatementIfElseBlockFinish
  1082. //
  1083. // if ( ... ) { ... } ...
  1084. // (state done)
  1085. CARBON_PARSE_STATE(StatementIfThenBlockFinish)
  1086. // Handles `if` processing after a provided `else` code block.
  1087. //
  1088. // if ( ... ) { ... } else { ... }
  1089. // ^
  1090. // (state done)
  1091. CARBON_PARSE_STATE(StatementIfElseBlockFinish)
  1092. // Handles `return` processing.
  1093. //
  1094. // return ;
  1095. // ^~~~~~
  1096. // 1. StatementReturnFinish
  1097. //
  1098. // return var ...
  1099. // ^~~~~~~~~~
  1100. // 1. StatementReturnFinish
  1101. //
  1102. // return ...
  1103. // ^~~~~~
  1104. // 1. Expr
  1105. // 2. StatementReturnFinish
  1106. CARBON_PARSE_STATE(StatementReturn)
  1107. // Handles `return` processing at the `;`.
  1108. //
  1109. // return ... ;
  1110. // ^
  1111. // (state done)
  1112. CARBON_PARSE_STATE(StatementReturnFinish)
  1113. // Handles processing of statements within a scope.
  1114. //
  1115. // { ... }
  1116. // ^
  1117. // (state done)
  1118. //
  1119. // { ... ... }
  1120. // ^
  1121. // 1. Statement
  1122. // 2. StatementScopeLoop
  1123. CARBON_PARSE_STATE(StatementScopeLoop)
  1124. // Handles `while` processing.
  1125. //
  1126. // while ...
  1127. // ^~~~~
  1128. // 1. ParenConditionAsWhile
  1129. // 2. StatementWhileConditionFinish
  1130. CARBON_PARSE_STATE(StatementWhile)
  1131. // Handles `while` processing between the condition and start of the code block.
  1132. //
  1133. // while ( ... ) ...
  1134. // ^
  1135. // 1. CodeBlock
  1136. // 2. StatementWhileBlockFinish
  1137. CARBON_PARSE_STATE(StatementWhileConditionFinish)
  1138. // Handles `while` processing after the end of the code block.
  1139. //
  1140. // while ( ... ) { ... }
  1141. // ^
  1142. // (state done)
  1143. CARBON_PARSE_STATE(StatementWhileBlockFinish)
  1144. // Handles parsing after the declaration scope of a type.
  1145. //
  1146. // class/impl/interface/constraint ... { ... }
  1147. // ^
  1148. // (state done)
  1149. CARBON_PARSE_STATE_VARIANTS4(DeclDefinitionFinish, Class, Impl, Interface,
  1150. NamedConstraint)
  1151. // Handles processing of a type after its introducer.
  1152. //
  1153. // class/interface/constraint ...
  1154. // ^
  1155. // 1. DeclNameAndParams
  1156. // 2. DeclOrDefinitionAs(Class|Interface|NamedConstraint)
  1157. CARBON_PARSE_STATE_VARIANTS3(TypeAfterIntroducer, Class, Interface,
  1158. NamedConstraint)
  1159. // Handles processing of a type after its optional parameters.
  1160. //
  1161. // class/impl/interface/constraint name ( ... ) {
  1162. // ^
  1163. // 1. DeclScopeLoop
  1164. // 2. DeclDefinitionFinishAs(Class|Impl|Interface|NamedConstraint)
  1165. //
  1166. // class/impl/interface/constraint name ( ... ) ;
  1167. // ^
  1168. // class/impl/interface/constraint name ( ... ) ???
  1169. // ^
  1170. // (state done)
  1171. CARBON_PARSE_STATE_VARIANTS4(DeclOrDefinition, Class, Impl, Interface,
  1172. NamedConstraint)
  1173. // Handles processing of an `adapt T` declaration after the introducer.
  1174. //
  1175. // adapt T ;
  1176. // ^
  1177. // 1. Expr
  1178. // 2. AdaptDecl
  1179. CARBON_PARSE_STATE(AdaptAfterIntroducer)
  1180. // Handles processing of a completed `adapt T` declaration.
  1181. //
  1182. // adapt T ;
  1183. // ^
  1184. // adapt T ??? ;
  1185. // ^~~~~
  1186. // (state done)
  1187. CARBON_PARSE_STATE(AdaptDecl)
  1188. // Handles processing of a `base: B` after the introducer.
  1189. //
  1190. // base: B ;
  1191. // ^
  1192. // 1. Expr
  1193. // 2. BaseDecl
  1194. // base ??? ;
  1195. // ^~~
  1196. // (state done)
  1197. CARBON_PARSE_STATE(BaseAfterIntroducer)
  1198. // Handles processing of a completed `base: B` declaration.
  1199. //
  1200. // base: B ;
  1201. // ^
  1202. // base: B ??? ;
  1203. // ^~~~~
  1204. // (state done)
  1205. CARBON_PARSE_STATE(BaseDecl)
  1206. // Handles processing of an `impl...as` declaration after the introducer.
  1207. //
  1208. // impl forall [ ...
  1209. // ^~~~~~
  1210. // 1. PatternListAsImplicit
  1211. // 2. ImplAfterForall
  1212. // 3. DeclOrDefinitionAsImpl
  1213. // impl as ...
  1214. // ^~
  1215. // 1. Expr
  1216. // 2. DeclOrDefinitionAsImpl
  1217. // impl type_expression as ...
  1218. // ^
  1219. // 1. Expr
  1220. // 2. ImplBeforeAs
  1221. // 3. DeclOrDefinitionAsImpl
  1222. CARBON_PARSE_STATE(ImplAfterIntroducer)
  1223. // Handles processing of an `impl forall` declaration after the implicit
  1224. // parameter list.
  1225. //
  1226. // impl forall [ ... ] as ...
  1227. // ^~
  1228. // 1. Expr
  1229. // impl forall [ ... ] type_expression as ...
  1230. // ^
  1231. // 1. Expr
  1232. // 2. ImplBeforeAs
  1233. CARBON_PARSE_STATE(ImplAfterForall)
  1234. // Handles processing of the `as` in an `impl` declaration after the type
  1235. // expression.
  1236. //
  1237. // impl TypeExpression as ...
  1238. // ^~
  1239. // 1. Expr
  1240. CARBON_PARSE_STATE(ImplBeforeAs)
  1241. // Handles the start of a `var` or `returned var`.
  1242. //
  1243. // var ... (variant is Decl)
  1244. // ^
  1245. // 1. Pattern
  1246. // 2. VarAfterPattern
  1247. // 3. VarFinishAsDecl
  1248. //
  1249. // var ... (variant is For)
  1250. // ^
  1251. // 1. Pattern
  1252. // 2. VarFinishAsFor
  1253. //
  1254. // returned var ... (variant is Returned)
  1255. // ^~~~~~~~~~~~
  1256. // 1. Pattern
  1257. // 2. VarAfterPattern
  1258. // 3. VarFinishAsDecl
  1259. //
  1260. // returned ??? ; (variant is Returned)
  1261. // ^~~~~~~~~~~~~~
  1262. // (state done)
  1263. CARBON_PARSE_STATE_VARIANTS3(Var, Decl, Returned, For)
  1264. // Handles `var` after the pattern, either followed by an initializer or the
  1265. // semicolon.
  1266. //
  1267. // var ... = ...
  1268. // ^
  1269. // var ... ??? = ...
  1270. // ^~~~~
  1271. // 1. Expr
  1272. //
  1273. // var ... ...
  1274. // ^
  1275. // (state done)
  1276. CARBON_PARSE_STATE(VarAfterPattern)
  1277. // Handles `var` parsing at the end.
  1278. //
  1279. // var ... ; (variant is Semicolon)
  1280. // ^
  1281. // var ... ??? ; (variant is Semicolon)
  1282. // ^~~~~
  1283. // (state done)
  1284. //
  1285. // var ... in (variant is For)
  1286. // ^~
  1287. // var ... : (variant is For, invalid)
  1288. // ^
  1289. // (state done)
  1290. CARBON_PARSE_STATE_VARIANTS2(VarFinish, Decl, For)
  1291. // Handles the start of a `let`.
  1292. //
  1293. // let ...
  1294. // ^
  1295. // 1. Pattern
  1296. // 2. LetAfterPattern
  1297. // 3. LetFinish
  1298. CARBON_PARSE_STATE(Let)
  1299. // Handles `let` after the pattern, optionally followed by an initializer. The
  1300. // initializer is required except in an associated constant declaration, but
  1301. // that is enforced by check.
  1302. //
  1303. // let ... = ...
  1304. // ^
  1305. // let ... ??? = ...
  1306. // ^~~~~
  1307. // 1. Expr
  1308. //
  1309. // let ... ;
  1310. // ^
  1311. // let ... ??? ;
  1312. // ^~~
  1313. // (state done)
  1314. CARBON_PARSE_STATE(LetAfterPattern)
  1315. // Handles `let` parsing at the end.
  1316. //
  1317. // let ... ;
  1318. // ^
  1319. // (state done)
  1320. CARBON_PARSE_STATE(LetFinish)
  1321. // Handles a choice's introducer.
  1322. //
  1323. // choice ...
  1324. // ^~~~~~
  1325. // 1. DeclNameAndParams
  1326. // 2. ChoiceAlternativeListStart
  1327. // 3. ChoiceDefinitionFinish
  1328. CARBON_PARSE_STATE(ChoiceIntroducer)
  1329. // Handles processing of a choice after its optional parameters.
  1330. //
  1331. // choice name ... {}
  1332. // ^
  1333. // (state done)
  1334. //
  1335. // choice name ... { ... }
  1336. // ^
  1337. // 1. ChoiceAlternative
  1338. //
  1339. // choice name ... ???
  1340. // ^
  1341. // (state done)
  1342. CARBON_PARSE_STATE(ChoiceDefinitionStart)
  1343. // Starts alternative parsing.
  1344. //
  1345. // name( ... )
  1346. // ^~~~
  1347. // 1. ParamListAsRegular
  1348. // 2. ChoiceAlternativeFinish
  1349. // name ...
  1350. // ^~~~
  1351. // 1. ChoiceAlternativeFinish
  1352. CARBON_PARSE_STATE(ChoiceAlternative)
  1353. // Finishes parsing a choice's alternative, including the optional trailing `,`.
  1354. // If there are more alternatives, enqueues another alternative parsing state.
  1355. //
  1356. // ... , }
  1357. // ^
  1358. // (state done)
  1359. //
  1360. // ... , ...
  1361. // ^
  1362. // 1. ChoiceAlternative
  1363. //
  1364. // ...
  1365. // ^
  1366. // (state done)
  1367. CARBON_PARSE_STATE(ChoiceAlternativeFinish)
  1368. // Finishes a choice definition.
  1369. //
  1370. // choice ... }
  1371. // ^
  1372. // (state done)
  1373. CARBON_PARSE_STATE(ChoiceDefinitionFinish)
  1374. // Handles `match` introducer.
  1375. //
  1376. // match ...
  1377. // ^~~~~
  1378. // 1. ParenConditionAsMatch
  1379. // 2. MatchConditionFinish
  1380. CARBON_PARSE_STATE(MatchIntroducer)
  1381. // Handles `match` cases block start after the condition.
  1382. //
  1383. // match (...) { ... }
  1384. // ^
  1385. // 1. MatchCaseLoop
  1386. // 2. MatchStatementFinish
  1387. //
  1388. // match (...) ???
  1389. // ^
  1390. // (state done)
  1391. CARBON_PARSE_STATE(MatchConditionFinish)
  1392. // Handles `match` cases.
  1393. //
  1394. // match (...) { case ...}
  1395. // ^
  1396. // 1. MatchCaseIntroducer
  1397. // 2. MatchCaseLoop
  1398. // match (...) { default ...}
  1399. // ^
  1400. // 1. MatchDefaultIntroducer
  1401. // 2. MatchCaseLoopAfterDefault
  1402. CARBON_PARSE_STATE(MatchCaseLoop)
  1403. // Handles `match` `case` introducer.
  1404. //
  1405. // match (...) { case ...}
  1406. // ^~~~
  1407. // 1. Pattern
  1408. // 2. MatchCaseAfterPattern
  1409. CARBON_PARSE_STATE(MatchCaseIntroducer)
  1410. // Handles `match` case after pattern.
  1411. //
  1412. // match (...) { case ... => ... }
  1413. // ^
  1414. // 1. MatchCaseStart
  1415. // match (...) { case ... if (...) }
  1416. // ^~~~
  1417. // 1. Expr
  1418. // 2. MatchCaseGuardFinish
  1419. // 3. MatchCaseStart
  1420. CARBON_PARSE_STATE(MatchCaseAfterPattern)
  1421. // Handles `match` case guard closing parenthesis.
  1422. //
  1423. // match (...) { case ... if (...) => ... }
  1424. // ^
  1425. // (state done)
  1426. CARBON_PARSE_STATE(MatchCaseGuardFinish)
  1427. // Handles `match` case `=>` and `{` opening statements block.
  1428. //
  1429. // match (...) { case ... => {...} }
  1430. // ^~~~
  1431. // 1. StatementScopeLoop
  1432. // 2. MatchCaseFinish
  1433. //
  1434. // match (...) { case ... ??? }
  1435. // ^
  1436. // (state done)
  1437. //
  1438. // match (...) { case ... => ??? }
  1439. // ^
  1440. // (state done)
  1441. CARBON_PARSE_STATE(MatchCaseStart)
  1442. // Handles `match` case statements block closing `}`.
  1443. //
  1444. // match (...) { case ... => {...} }
  1445. // ^
  1446. // (state done)
  1447. CARBON_PARSE_STATE(MatchCaseFinish)
  1448. // Handles `match` default introducer, `=>` and `{` opening statements block.
  1449. //
  1450. // match (...) { default => {...} }
  1451. // ^~~~~~~~~~~~
  1452. // 1. StatementScopeLoop
  1453. // 2. MatchDefaultFinish
  1454. //
  1455. // match (...) { default ??? }
  1456. // ^
  1457. // (state done)
  1458. //
  1459. // match (...) { default => ??? }
  1460. // ^
  1461. // (state done)
  1462. CARBON_PARSE_STATE(MatchDefaultIntroducer)
  1463. // Handles `match` default case statements block closing `}`.
  1464. //
  1465. // match (...) { default => {...} }
  1466. // ^
  1467. // (state done)
  1468. CARBON_PARSE_STATE(MatchDefaultFinish)
  1469. // Handles `match` cases after the `default` case.
  1470. //
  1471. // match (...) { default => {...} case ... }
  1472. // ^~~~
  1473. // 1. MatchCaseLoopAfterDefault
  1474. //
  1475. // match (...) { default => {...} default ... }
  1476. // ^~~~~~~
  1477. // 1. MatchCaseLoopAfterDefault
  1478. //
  1479. // match (...) { default => {...} }
  1480. // ^
  1481. // (state done)
  1482. CARBON_PARSE_STATE(MatchCaseLoopAfterDefault)
  1483. // Finishes `match` statement.
  1484. //
  1485. // match (...) {...}
  1486. // ^
  1487. // (state done)
  1488. CARBON_PARSE_STATE(MatchStatementFinish)
  1489. #undef CARBON_PARSE_STATE