state.def 37 KB

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