state.def 41 KB

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