state.def 40 KB

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