state.def 38 KB

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