state.def 36 KB

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