state.def 38 KB

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