state.def 38 KB

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