state.def 34 KB

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