state.def 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  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. //
  916. // ... , ] (variant is Implicit)
  917. // ^
  918. // (state done)
  919. //
  920. // ... , ...
  921. // ^
  922. // 1. PatternListElementAs(Tuple|Explicit|Implicit)
  923. //
  924. // ...
  925. // ^
  926. // (state done)
  927. CARBON_PARSE_STATE_VARIANTS3(PatternListElementFinish, Tuple, Explicit,
  928. Implicit)
  929. // Handles processing of a tuple pattern (parentheses) or implicit parameter
  930. // list (square brackets).
  931. //
  932. // ( ) (variant is Tuple or Explicit)
  933. // ^
  934. // [ ] (variant is Implicit)
  935. // ^
  936. // 1. PatternListFinishAs(Tuple|Explicit|Implicit)
  937. //
  938. // ( ... ) (variant is Tuple or Explicit)
  939. // ^
  940. // [ ... ] (variant is Implicit)
  941. // ^
  942. // 1. PatternListElementAs(Tuple|Explicit|Implicit)
  943. // 2. PatternListFinishAs(Tuple|Explicit|Implicit)
  944. CARBON_PARSE_STATE_VARIANTS3(PatternList, Tuple, Explicit, Implicit)
  945. // Handles processing of a parameter list `]` or `)`.
  946. //
  947. // ( ... ) (variant is Tuple or Explicit)
  948. // ^
  949. // [ ... ] (variant is Implicit)
  950. // ^
  951. // (state done)
  952. CARBON_PARSE_STATE_VARIANTS3(PatternListFinish, Tuple, Explicit, Implicit)
  953. // Handles the processing of a `(condition)` up through the expression.
  954. //
  955. // if/while/match { (invalid)
  956. // ^
  957. // 1. ParenConditionAs(If|While|Match)Finish
  958. //
  959. // if/while/match ( ... )
  960. // ^
  961. // if/while/match ???
  962. // ^
  963. // 1. Expr
  964. // 2. ParenConditionAs(If|While|Match)Finish
  965. CARBON_PARSE_STATE_VARIANTS3(ParenCondition, If, While, Match)
  966. // Finishes the processing of a `(condition)` after the expression.
  967. //
  968. // if/while/match ( expr )
  969. // ^
  970. // if/while/match {
  971. // ^
  972. // if/while/match ??? {
  973. // ^
  974. // (state done)
  975. CARBON_PARSE_STATE_VARIANTS3(ParenConditionFinish, If, While, Match)
  976. // Handles compound member access `(` after `->` or `.`
  977. //
  978. // ( ... )
  979. // ^
  980. // 1. Expr
  981. // 2. OnlyParenExprFinish
  982. CARBON_PARSE_STATE(OnlyParenExpr)
  983. // Handles the `)` after `->(`... or `.(`...
  984. //
  985. // ( ... )
  986. // ^
  987. CARBON_PARSE_STATE(OnlyParenExprFinish)
  988. // Handles the `(` of an expression that's presumed to be a parenthesized
  989. // single expression, but may later be reclassified as a tuple literal.
  990. //
  991. // ( )
  992. // ^
  993. // 1. TupleLiteralFinish
  994. //
  995. // ( ... )
  996. // ^
  997. // 1. Expr
  998. // 2. ExprAfterOpenParenFinish
  999. // 3. ParenExprFinish (SPECIAL: may be replaced)
  1000. CARBON_PARSE_STATE(ParenExpr)
  1001. // Handles the `)` of a tuple literal.
  1002. //
  1003. // ( ... )
  1004. // ^
  1005. // (state done)
  1006. CARBON_PARSE_STATE(TupleLiteralFinish)
  1007. // Handles the end of an expression following an open parenthesis.
  1008. //
  1009. // ( ... , )
  1010. // ^
  1011. // (state done)
  1012. // SPECIAL: parent becomes TupleLiteralFinish
  1013. //
  1014. // ( ... , ... )
  1015. // ^
  1016. // 1. Expr
  1017. // 2. TupleLiteralElementFinish
  1018. // SPECIAL: parent becomes TupleLiteralFinish
  1019. //
  1020. // ( ... )
  1021. // ^
  1022. // (state done)
  1023. CARBON_PARSE_STATE(ExprAfterOpenParenFinish)
  1024. // Handles the end of an expression that is known to be an element of a tuple
  1025. // literal expression.
  1026. //
  1027. // ( ... , )
  1028. // ^
  1029. // (state done)
  1030. //
  1031. // ( ... , ... )
  1032. // ^
  1033. // 1. Expr
  1034. // 2. TupleLiteralElementFinish
  1035. //
  1036. // ( ... )
  1037. // ^
  1038. // (state done)
  1039. CARBON_PARSE_STATE(TupleLiteralElementFinish)
  1040. // Handles the `)` of a parenthesized single expression.
  1041. //
  1042. // ( ... )
  1043. // ^
  1044. // (state done)
  1045. CARBON_PARSE_STATE(ParenExprFinish)
  1046. // Handles processing of a pattern.
  1047. //
  1048. // ( ... )
  1049. // ^
  1050. // 1. PatternListAsTuple
  1051. //
  1052. // var ...
  1053. // ^
  1054. //
  1055. // 1. VariablePattern
  1056. //
  1057. // ...
  1058. // ^
  1059. // 1. BindingPattern
  1060. //
  1061. // ...
  1062. // ^
  1063. // 1. UnusedPattern
  1064. CARBON_PARSE_STATE(Pattern)
  1065. // Handles the initial part of a binding pattern, enqueuing type expression
  1066. // processing.
  1067. //
  1068. // TODO: treat `ref` as a unary pattern operator in order to avoid ambiguity
  1069. // with `ref` in expression patterns. See issue #6342.
  1070. //
  1071. // [ref] name: ...
  1072. // ^~~~~~~~~~~
  1073. // [ref] self: ...
  1074. // ^~~~~~~~~~~
  1075. // [ref] _: ...
  1076. // ^~~~~~~~
  1077. // 1. Expr
  1078. // 2. BindingPatternFinishAsRegular
  1079. //
  1080. // [template] name:! ...
  1081. // ^~~~~~~~~~~~~~~~~
  1082. // [template] self:! ...
  1083. // ^~~~~~~~~~~~~~~~~
  1084. // [template] _:! ...
  1085. // ^~~~~~~~~~~~~~
  1086. // 1. Expr
  1087. // 2. BindingPatternFinishAsGeneric
  1088. //
  1089. // name:? ...
  1090. // ^~~~~~
  1091. // self:? ...
  1092. // ^~~~~~
  1093. // _:? ...
  1094. // ^~~
  1095. // 1. Expr
  1096. // 2. BindingPatternFinishAsForm
  1097. //
  1098. // ???
  1099. // ^
  1100. // 1. BindingPatternFinishAsRegular
  1101. CARBON_PARSE_STATE(BindingPattern)
  1102. // Finishes binding pattern processing.
  1103. //
  1104. // name: type
  1105. // ^
  1106. // (state done)
  1107. CARBON_PARSE_STATE_VARIANTS3(BindingPatternFinish, Generic, Regular, Form)
  1108. // Handles `var` in a pattern context.
  1109. //
  1110. // var ...
  1111. // ^~~
  1112. // 1. Pattern
  1113. // 2. FinishVariablePattern
  1114. CARBON_PARSE_STATE(VariablePattern)
  1115. // Finishes `var` in a pattern context.
  1116. //
  1117. // var ...
  1118. // ^
  1119. // (state done)
  1120. CARBON_PARSE_STATE(FinishVariablePattern)
  1121. // Handles `unused` in a pattern context.
  1122. //
  1123. // unused ...
  1124. // ^~~~~~
  1125. // 1. Pattern
  1126. // 2. FinishUnusedPattern
  1127. CARBON_PARSE_STATE(UnusedPattern)
  1128. // Finishes `unused` in a pattern context.
  1129. //
  1130. // unused ...
  1131. // ^
  1132. // (state done)
  1133. CARBON_PARSE_STATE(FinishUnusedPattern)
  1134. // Handles a single statement. While typically within a statement block, this
  1135. // can also be used for error recovery where we expect a statement block and
  1136. // are missing braces.
  1137. //
  1138. // break ...
  1139. // ^~~~~
  1140. // 1. StatementBreakFinish
  1141. //
  1142. // continue ...
  1143. // ^~~~~~~~
  1144. // 1. StatementContinueFinish
  1145. //
  1146. // for ...
  1147. // ^~~
  1148. // 1. StatementForHeader
  1149. // 2. StatementForFinish
  1150. //
  1151. // if ...
  1152. // ^
  1153. // 1. StatementIf
  1154. //
  1155. // return ...
  1156. // ^
  1157. // 1. StatementReturn
  1158. //
  1159. // returned ...
  1160. // ^
  1161. // 1. VarAsReturned
  1162. //
  1163. // while ...
  1164. // ^
  1165. // 1. StatementWhile
  1166. //
  1167. // ...
  1168. // ^
  1169. // 1. Expr
  1170. // 2. ExprStatementFinish
  1171. //
  1172. // match ...
  1173. // ^
  1174. // 1. MatchIntroducer
  1175. //
  1176. // <decl introducer or modifier>
  1177. // ^
  1178. // 1. Decl
  1179. CARBON_PARSE_STATE(Statement)
  1180. // Handles `break` processing at the `;`.
  1181. //
  1182. // break ;
  1183. // ^
  1184. // (state done)
  1185. CARBON_PARSE_STATE(StatementBreakFinish)
  1186. // Handles `continue` processing at the `;`.
  1187. //
  1188. // continue ;
  1189. // ^
  1190. // (state done)
  1191. CARBON_PARSE_STATE(StatementContinueFinish)
  1192. // Handles `for` processing of `(`, proceeding to a pattern before continuing.
  1193. //
  1194. // for ( ... )
  1195. // ^
  1196. // for ???
  1197. // ^
  1198. // 1. Pattern
  1199. // 2. StatementForHeaderIn
  1200. CARBON_PARSE_STATE(StatementForHeader)
  1201. // Handles `for` processing of `in`, proceeding to an expression before
  1202. // continuing.
  1203. //
  1204. // for ( ... in ... )
  1205. // ^~
  1206. // for ( ... : ... )
  1207. // ^
  1208. // for ( ... ??? )
  1209. // ^
  1210. // 1. Expr
  1211. // 2. StatementForHeaderFinish
  1212. CARBON_PARSE_STATE(StatementForHeaderIn)
  1213. // Handles `for` processing of `)`, proceeding to the statement block.
  1214. //
  1215. // for ( ... ) ...
  1216. // ^
  1217. // 1. CodeBlock
  1218. CARBON_PARSE_STATE(StatementForHeaderFinish)
  1219. // Handles `for` processing after the final `}`.
  1220. //
  1221. // for ( ... ) { ... }
  1222. // ^
  1223. // (state done)
  1224. CARBON_PARSE_STATE(StatementForFinish)
  1225. // Handles `if` processing at the start.
  1226. //
  1227. // if ...
  1228. // ^~
  1229. // 1. ParenConditionAsIf
  1230. // 2. StatementIfConditionFinish
  1231. CARBON_PARSE_STATE(StatementIf)
  1232. // Handles `if` processing between the condition and start of the first code
  1233. // block.
  1234. //
  1235. // if ( ... ) ...
  1236. // ^
  1237. // 1. CodeBlock
  1238. // 2. StatementIfThenBlockFinish
  1239. CARBON_PARSE_STATE(StatementIfConditionFinish)
  1240. // Handles `if` processing after the end of the first code block, with the
  1241. // optional `else`.
  1242. //
  1243. // if ( ... ) { ... } else if ...
  1244. // ^~~~
  1245. // 1. StatementIf
  1246. // 2. StatementIfElseBlockFinish
  1247. //
  1248. // if ( ... ) { ... } else ...
  1249. // ^~~~
  1250. // 1. CodeBlock
  1251. // 2. StatementIfElseBlockFinish
  1252. //
  1253. // if ( ... ) { ... } ...
  1254. // (state done)
  1255. CARBON_PARSE_STATE(StatementIfThenBlockFinish)
  1256. // Handles `if` processing after a provided `else` code block.
  1257. //
  1258. // if ( ... ) { ... } else { ... }
  1259. // ^
  1260. // (state done)
  1261. CARBON_PARSE_STATE(StatementIfElseBlockFinish)
  1262. // Handles `return` processing.
  1263. //
  1264. // return ;
  1265. // ^~~~~~
  1266. // 1. StatementReturnFinish
  1267. //
  1268. // return var ...
  1269. // ^~~~~~~~~~
  1270. // 1. StatementReturnFinish
  1271. //
  1272. // return ...
  1273. // ^~~~~~
  1274. // 1. Expr
  1275. // 2. StatementReturnFinish
  1276. CARBON_PARSE_STATE(StatementReturn)
  1277. // Handles `return` processing at the `;`.
  1278. //
  1279. // return ... ;
  1280. // ^
  1281. // (state done)
  1282. CARBON_PARSE_STATE(StatementReturnFinish)
  1283. // Handles processing of statements within a scope.
  1284. //
  1285. // { ... }
  1286. // ^
  1287. // (state done)
  1288. //
  1289. // { ... ... }
  1290. // ^
  1291. // 1. Statement
  1292. // 2. StatementScopeLoop
  1293. CARBON_PARSE_STATE(StatementScopeLoop)
  1294. // Handles `while` processing.
  1295. //
  1296. // while ...
  1297. // ^~~~~
  1298. // 1. ParenConditionAsWhile
  1299. // 2. StatementWhileConditionFinish
  1300. CARBON_PARSE_STATE(StatementWhile)
  1301. // Handles `while` processing between the condition and start of the code block.
  1302. //
  1303. // while ( ... ) ...
  1304. // ^
  1305. // 1. CodeBlock
  1306. // 2. StatementWhileBlockFinish
  1307. CARBON_PARSE_STATE(StatementWhileConditionFinish)
  1308. // Handles `while` processing after the end of the code block.
  1309. //
  1310. // while ( ... ) { ... }
  1311. // ^
  1312. // (state done)
  1313. CARBON_PARSE_STATE(StatementWhileBlockFinish)
  1314. // Handles parsing after the declaration scope of a type.
  1315. //
  1316. // class/impl/interface/constraint ... { ... }
  1317. // ^
  1318. // (state done)
  1319. CARBON_PARSE_STATE_VARIANTS4(DeclDefinitionFinish, Class, Impl, Interface,
  1320. NamedConstraint)
  1321. // Handles processing of a type after its introducer.
  1322. //
  1323. // class/interface/constraint ...
  1324. // ^
  1325. // 1. DeclNameAndParams
  1326. // 2. DeclOrDefinitionAs(Class|Interface|NamedConstraint)
  1327. CARBON_PARSE_STATE_VARIANTS3(TypeAfterIntroducer, Class, Interface,
  1328. NamedConstraint)
  1329. // Handles processing of a type after its optional parameters.
  1330. //
  1331. // class/impl/interface/constraint name ( ... ) {
  1332. // ^
  1333. // 1. DeclScopeLoopAs(Class|Regular)
  1334. // 2. DeclDefinitionFinishAs(Class|Impl|Interface|NamedConstraint)
  1335. //
  1336. // class/impl/interface/constraint name ( ... ) ;
  1337. // ^
  1338. // class/impl/interface/constraint name ( ... ) ???
  1339. // ^
  1340. // (state done)
  1341. CARBON_PARSE_STATE_VARIANTS4(DeclOrDefinition, Class, Impl, Interface,
  1342. NamedConstraint)
  1343. // Handles processing of an `adapt T` declaration after the introducer.
  1344. //
  1345. // adapt T ;
  1346. // ^
  1347. // 1. Expr
  1348. // 2. AdaptDecl
  1349. CARBON_PARSE_STATE(AdaptAfterIntroducer)
  1350. // Handles processing of a completed `adapt T` declaration.
  1351. //
  1352. // adapt T ;
  1353. // ^
  1354. // adapt T ??? ;
  1355. // ^~~~~
  1356. // (state done)
  1357. CARBON_PARSE_STATE(AdaptDecl)
  1358. // Handles processing of a `base: B` after the introducer.
  1359. //
  1360. // base: B ;
  1361. // ^
  1362. // 1. Expr
  1363. // 2. BaseDecl
  1364. // base ??? ;
  1365. // ^~~
  1366. // (state done)
  1367. CARBON_PARSE_STATE(BaseAfterIntroducer)
  1368. // Handles processing of a completed `base: B` declaration.
  1369. //
  1370. // base: B ;
  1371. // ^
  1372. // base: B ??? ;
  1373. // ^~~~~
  1374. // (state done)
  1375. CARBON_PARSE_STATE(BaseDecl)
  1376. // Handles processing of an `impl...as` declaration after the introducer.
  1377. //
  1378. // impl forall [ ...
  1379. // ^~~~~~
  1380. // 1. PatternListAsImplicit
  1381. // 2. ImplAfterForall
  1382. // 3. DeclOrDefinitionAsImpl
  1383. // impl as ...
  1384. // ^~
  1385. // 1. Expr
  1386. // 2. DeclOrDefinitionAsImpl
  1387. // impl TypeExpression as ...
  1388. // ^
  1389. // 1. Expr
  1390. // 2. ImplBeforeAs
  1391. // 3. DeclOrDefinitionAsImpl
  1392. CARBON_PARSE_STATE(ImplAfterIntroducer)
  1393. // Handles processing of an `impl forall` declaration after the implicit
  1394. // parameter list.
  1395. //
  1396. // impl forall [ ... ] as ...
  1397. // ^~
  1398. // 1. Expr
  1399. // impl forall [ ... ] TypeExpression as ...
  1400. // ^
  1401. // 1. Expr
  1402. // 2. ImplBeforeAs
  1403. CARBON_PARSE_STATE(ImplAfterForall)
  1404. // Handles processing of the `as` in an `impl` declaration after the type
  1405. // expression.
  1406. //
  1407. // impl TypeExpression as ...
  1408. // ^~
  1409. // 1. Expr
  1410. CARBON_PARSE_STATE(ImplBeforeAs)
  1411. // Handles processing of a `require...impls` declaration after the introducer.
  1412. //
  1413. // require impls ...
  1414. // ^~~~~
  1415. // 1. Expr
  1416. // 2. RequireDecl
  1417. // require TypeExpression impls ...
  1418. // ^
  1419. // 1. Expr
  1420. // 2. RequireBeforeImpls
  1421. // 3. RequireDecl
  1422. CARBON_PARSE_STATE(RequireAfterIntroducer)
  1423. // Handles processing of the `impls` in a `require` declaration after the type
  1424. // expression.
  1425. //
  1426. // require TypeExpression impls ...
  1427. // ^~~~~
  1428. // 1. Expr
  1429. CARBON_PARSE_STATE(RequireBeforeImpls)
  1430. // Handles processing of a completed `require` declaration.
  1431. //
  1432. // require TypeExpression impls FacetTypeExpression ;
  1433. // ^
  1434. // (state done)
  1435. CARBON_PARSE_STATE(RequireDecl)
  1436. // Handles processing of an `observe` declaration after the introducer.
  1437. //
  1438. // observe TypeExpression ...
  1439. // ^
  1440. // 1. Expr
  1441. // 2. ObserveOperator
  1442. CARBON_PARSE_STATE(ObserveAfterIntroducer)
  1443. // Handles processing of an operator in an `observe` declaration.
  1444. //
  1445. // observe TypeExpression == TypeExpression ...
  1446. // ^~
  1447. // 1. Expr
  1448. // 2. ObserveFinishOperator
  1449. // observe TypeExpression impls ...
  1450. // ^~~~~
  1451. // 1. Expr
  1452. // 2. ObserveFinishOperator
  1453. CARBON_PARSE_STATE(ObserveOperator)
  1454. // Finishes an operator in an `observe` declaration.
  1455. //
  1456. // observe TypeExpression == TypeExpression ...
  1457. // ^
  1458. // 1. ObserveOperator
  1459. // observe TypeExpression == TypeExpression ;
  1460. // ^
  1461. // 1. ObserveDecl
  1462. CARBON_PARSE_STATE(ObserveFinishOperator)
  1463. // Handles processing of a completed `observe` declaration.
  1464. //
  1465. // observe ... ;
  1466. // ^
  1467. // (state done)
  1468. CARBON_PARSE_STATE(ObserveDecl)
  1469. // Handles the start of a `var` or `returned var` in a non-class context.
  1470. //
  1471. // var ... (variant is Regular)
  1472. // ^
  1473. // 1. Pattern
  1474. // 2. VarAfterPatternAsVar
  1475. // 3. VarFinishAsRegular
  1476. //
  1477. // returned var ... (variant is Returned)
  1478. // ^~~~~~~~~~~~
  1479. // 1. Pattern
  1480. // 2. VarAfterPatternAsVar
  1481. // 3. VarFinishAsRegular
  1482. //
  1483. // returned ??? ; (variant is Returned)
  1484. // ^~~~~~~~~~~~~~
  1485. // (state done)
  1486. CARBON_PARSE_STATE_VARIANTS2(Var, Regular, Returned)
  1487. // Handles `var` after the pattern, either followed by an initializer or the
  1488. // semicolon.
  1489. //
  1490. // var ... = ...
  1491. // ^
  1492. // var ... ??? = ...
  1493. // ^~~~~
  1494. // 1. Expr
  1495. //
  1496. // var ... ...
  1497. // ^
  1498. // (state done)
  1499. CARBON_PARSE_STATE_VARIANTS2(VarAfterPattern, Var, Field)
  1500. // Handles `var` parsing at the end.
  1501. //
  1502. // var ... ;
  1503. // ^
  1504. // var ... ??? ;
  1505. // ^~~~~
  1506. // (state done)
  1507. CARBON_PARSE_STATE_VARIANTS2(VarFinish, Regular, Field)
  1508. // Handles the beginning of a field declaration (`var` in a class context).
  1509. //
  1510. // var name : type
  1511. // ^~~~~~
  1512. // 1. Expr
  1513. // 2. VarAfterPatternAsField
  1514. // 3. VarFinishAsField
  1515. //
  1516. // var ??? ;
  1517. // ^~~~~
  1518. // var name ??? ;
  1519. // ^~~~~~~~~
  1520. // (state done)
  1521. CARBON_PARSE_STATE(FieldDecl)
  1522. // Handles the start of a `let`.
  1523. //
  1524. // let ...
  1525. // ^
  1526. // 1. Pattern
  1527. // 2. LetAfterPattern
  1528. // 3. LetFinish
  1529. CARBON_PARSE_STATE(Let)
  1530. // Handles `let` after the pattern, optionally followed by an initializer. The
  1531. // initializer is required except in an associated constant declaration, but
  1532. // that is enforced by check.
  1533. //
  1534. // let ... = ...
  1535. // ^
  1536. // let ... ??? = ...
  1537. // ^~~~~
  1538. // 1. Expr
  1539. //
  1540. // let ... ;
  1541. // ^
  1542. // let ... ??? ;
  1543. // ^~~
  1544. // (state done)
  1545. CARBON_PARSE_STATE_VARIANTS2(LetAfterPattern, Regular, AssociatedConstant)
  1546. // Handles `let` parsing at the end.
  1547. //
  1548. // let ... ;
  1549. // ^
  1550. // let ... ??? ;
  1551. // ^~~~~
  1552. // (state done)
  1553. CARBON_PARSE_STATE_VARIANTS2(LetFinish, Regular, AssociatedConstant)
  1554. // Handles the start of an associated constant declaration (`let` in an
  1555. // interface context).
  1556. //
  1557. // let name :! ...
  1558. // ^~~~~~~
  1559. // 1. AssociatedConstantFinish
  1560. // let ??? ;
  1561. // ^~~~~
  1562. // let name ??? ;
  1563. // ^~~~~~~~~
  1564. // (state done)
  1565. CARBON_PARSE_STATE(AssociatedConstant)
  1566. // Handles a choice's introducer.
  1567. //
  1568. // choice ...
  1569. // ^~~~~~
  1570. // 1. DeclNameAndParams
  1571. // 2. ChoiceDefinitionStart
  1572. // 3. ChoiceDefinitionFinish
  1573. CARBON_PARSE_STATE(ChoiceIntroducer)
  1574. // Handles processing of a choice after its optional parameters.
  1575. //
  1576. // choice name ... {}
  1577. // ^
  1578. // (state done)
  1579. //
  1580. // choice name ... { ... }
  1581. // ^
  1582. // 1. ChoiceAlternative
  1583. //
  1584. // choice name ... ???
  1585. // ^
  1586. // (state done)
  1587. CARBON_PARSE_STATE(ChoiceDefinitionStart)
  1588. // Starts alternative parsing.
  1589. //
  1590. // name( ... )
  1591. // ^~~~
  1592. // 1. ParamListAsRegular
  1593. // 2. ChoiceAlternativeFinish
  1594. // name ...
  1595. // ^~~~
  1596. // 1. ChoiceAlternativeFinish
  1597. CARBON_PARSE_STATE(ChoiceAlternative)
  1598. // Finishes parsing a choice's alternative, including the optional trailing `,`.
  1599. // If there are more alternatives, enqueues another alternative parsing state.
  1600. //
  1601. // ... , }
  1602. // ^
  1603. // (state done)
  1604. //
  1605. // ... , ...
  1606. // ^
  1607. // 1. ChoiceAlternative
  1608. //
  1609. // ...
  1610. // ^
  1611. // (state done)
  1612. CARBON_PARSE_STATE(ChoiceAlternativeFinish)
  1613. // Finishes a choice definition.
  1614. //
  1615. // choice ... }
  1616. // ^
  1617. // (state done)
  1618. CARBON_PARSE_STATE(ChoiceDefinitionFinish)
  1619. // Handles `match` introducer.
  1620. //
  1621. // match ...
  1622. // ^~~~~
  1623. // 1. ParenConditionAsMatch
  1624. // 2. MatchConditionFinish
  1625. CARBON_PARSE_STATE(MatchIntroducer)
  1626. // Handles `match` cases block start after the condition.
  1627. //
  1628. // match (...) { ... }
  1629. // ^
  1630. // 1. MatchCaseLoop
  1631. // 2. MatchStatementFinish
  1632. //
  1633. // match (...) ???
  1634. // ^
  1635. // (state done)
  1636. CARBON_PARSE_STATE(MatchConditionFinish)
  1637. // Handles `match` cases.
  1638. //
  1639. // match (...) { case ...}
  1640. // ^
  1641. // 1. MatchCaseIntroducer
  1642. // 2. MatchCaseLoop
  1643. // match (...) { default ...}
  1644. // ^
  1645. // 1. MatchDefaultIntroducer
  1646. // 2. MatchCaseLoopAfterDefault
  1647. CARBON_PARSE_STATE(MatchCaseLoop)
  1648. // Handles `match` `case` introducer.
  1649. //
  1650. // match (...) { case ...}
  1651. // ^~~~
  1652. // 1. Pattern
  1653. // 2. MatchCaseAfterPattern
  1654. CARBON_PARSE_STATE(MatchCaseIntroducer)
  1655. // Handles `match` case after pattern.
  1656. //
  1657. // match (...) { case ... => ... }
  1658. // ^
  1659. // 1. MatchCaseStart
  1660. // match (...) { case ... if (...) }
  1661. // ^~~~
  1662. // 1. Expr
  1663. // 2. MatchCaseGuardFinish
  1664. // 3. MatchCaseStart
  1665. CARBON_PARSE_STATE(MatchCaseAfterPattern)
  1666. // Handles `match` case guard closing parenthesis.
  1667. //
  1668. // match (...) { case ... if (...) => ... }
  1669. // ^
  1670. // (state done)
  1671. CARBON_PARSE_STATE(MatchCaseGuardFinish)
  1672. // Handles `match` case `=>` and `{` opening statements block.
  1673. //
  1674. // match (...) { case ... => {...} }
  1675. // ^~~~
  1676. // 1. StatementScopeLoop
  1677. // 2. MatchCaseFinish
  1678. //
  1679. // match (...) { case ... ??? }
  1680. // ^
  1681. // (state done)
  1682. //
  1683. // match (...) { case ... => ??? }
  1684. // ^
  1685. // (state done)
  1686. CARBON_PARSE_STATE(MatchCaseStart)
  1687. // Handles `match` case statements block closing `}`.
  1688. //
  1689. // match (...) { case ... => {...} }
  1690. // ^
  1691. // (state done)
  1692. CARBON_PARSE_STATE(MatchCaseFinish)
  1693. // Handles `match` default introducer, `=>` and `{` opening statements block.
  1694. //
  1695. // match (...) { default => {...} }
  1696. // ^~~~~~~~~~~~
  1697. // 1. StatementScopeLoop
  1698. // 2. MatchDefaultFinish
  1699. //
  1700. // match (...) { default ??? }
  1701. // ^
  1702. // (state done)
  1703. //
  1704. // match (...) { default => ??? }
  1705. // ^
  1706. // (state done)
  1707. CARBON_PARSE_STATE(MatchDefaultIntroducer)
  1708. // Handles `match` default case statements block closing `}`.
  1709. //
  1710. // match (...) { default => {...} }
  1711. // ^
  1712. // (state done)
  1713. CARBON_PARSE_STATE(MatchDefaultFinish)
  1714. // Handles `match` cases after the `default` case.
  1715. //
  1716. // match (...) { default => {...} case ... }
  1717. // ^~~~
  1718. // 1. MatchCaseLoopAfterDefault
  1719. //
  1720. // match (...) { default => {...} default ... }
  1721. // ^~~~~~~
  1722. // 1. MatchCaseLoopAfterDefault
  1723. //
  1724. // match (...) { default => {...} }
  1725. // ^
  1726. // (state done)
  1727. CARBON_PARSE_STATE(MatchCaseLoopAfterDefault)
  1728. // Finishes `match` statement.
  1729. //
  1730. // match (...) {...}
  1731. // ^
  1732. // (state done)
  1733. CARBON_PARSE_STATE(MatchStatementFinish)
  1734. #undef CARBON_PARSE_STATE