state.def 35 KB

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