state.def 35 KB

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