state.def 41 KB

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