state.def 37 KB

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