state.def 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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 this example, `...` 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. #endif
  39. // Use CARBON_PARSE_STATE_VARIANTSN(State, Variant1, Variant2, ...) to generate
  40. // StateAsVariant1, StateAsVariant2, ... states.
  41. #define CARBON_PARSE_STATE_VARIANT(State, Variant) \
  42. CARBON_PARSE_STATE(State##As##Variant)
  43. #define CARBON_PARSE_STATE_VARIANTS2(State, Variant1, Variant2) \
  44. CARBON_PARSE_STATE_VARIANT(State, Variant1) \
  45. CARBON_PARSE_STATE_VARIANT(State, Variant2)
  46. #define CARBON_PARSE_STATE_VARIANTS3(State, Variant1, Variant2, Variant3) \
  47. CARBON_PARSE_STATE_VARIANT(State, Variant1) \
  48. CARBON_PARSE_STATE_VARIANTS2(State, Variant2, Variant3)
  49. #define CARBON_PARSE_STATE_VARIANTS4(State, Variant1, Variant2, Variant3, \
  50. Variant4) \
  51. CARBON_PARSE_STATE_VARIANT(State, Variant1) \
  52. CARBON_PARSE_STATE_VARIANTS3(State, Variant2, Variant3, Variant4)
  53. // Handles an index expression:
  54. //
  55. // a[0]
  56. // ^
  57. // 1. Expression
  58. // 2. IndexExpressionFinish
  59. CARBON_PARSE_STATE(IndexExpr)
  60. // Handles finishing the index expression.
  61. //
  62. // a[0]
  63. // ^
  64. // (state done)
  65. CARBON_PARSE_STATE(IndexExprFinish)
  66. // Handles an array expression.
  67. //
  68. // [T; N]
  69. // ^
  70. // 1. Expr
  71. // 2. ArrayExprSemi
  72. CARBON_PARSE_STATE(ArrayExpr)
  73. // Handles ';' in an array expression.
  74. //
  75. // [T;]
  76. // ^
  77. // 1. ArrayExprFinish
  78. //
  79. // [T; N]
  80. // ^
  81. // 1. Expr
  82. // 2. ArrayExprFinish
  83. CARBON_PARSE_STATE(ArrayExprSemi)
  84. // Handles finishing the array expression.
  85. //
  86. // [T;]
  87. // ^
  88. // [T; N]
  89. // ^
  90. // (state done)
  91. CARBON_PARSE_STATE(ArrayExprFinish)
  92. // Handles the `{` of a brace expression.
  93. //
  94. // {}
  95. // ^
  96. // 1. BraceExprFinishAsUnknown
  97. //
  98. // { ... }
  99. // ^
  100. // 1. BraceExprParamAsUnknown
  101. // 2. BraceExprFinishAsUnknown
  102. CARBON_PARSE_STATE(BraceExpr)
  103. // Handles a brace expression parameter. Note this will always start as unknown,
  104. // but should be known after the first valid parameter. All later inconsistent
  105. // parameters are invalid.
  106. //
  107. // { .foo ... }
  108. // ^
  109. // 1. PeriodAsStruct
  110. // 2. BraceExprParamAfterDesignatorAs(Type|Value|Unknown)
  111. //
  112. // { ???
  113. // ^
  114. // 1. BraceExprParamFinishAs(Type|Value|Unknown)
  115. CARBON_PARSE_STATE_VARIANTS3(BraceExprParam, Type, Value, Unknown)
  116. // Handles a brace expression parameter after the initial designator. This
  117. // should be at a `:` or `=`, depending on whether it's a type or value literal.
  118. //
  119. // { .foo = bar ... }
  120. // ^
  121. // 1. Expr
  122. // 2. BraceExprParamFinishAsValue
  123. //
  124. // { .foo: bar ... }
  125. // ^
  126. // 1. Expr
  127. // 2. BraceExprParamFinishAsType
  128. //
  129. // { .foo ???
  130. // ^
  131. // 1. BraceExprParamFinishAs(Type|Value|Unknown)
  132. CARBON_PARSE_STATE_VARIANTS3(BraceExprParamAfterDesignator, Type, Value,
  133. Unknown)
  134. // Handles the end of a brace expression parameter.
  135. //
  136. // { ... }
  137. // ^
  138. // (state done)
  139. //
  140. // { .foo = bar, ... }
  141. // ^
  142. // 1. BraceExprParamAsValue
  143. //
  144. // { .foo: bar, ... }
  145. // ^
  146. // 1. BraceExprParamAsType
  147. //
  148. // { ??? , ... }
  149. // ^
  150. // 1. BraceExprParamAsUnknown
  151. CARBON_PARSE_STATE_VARIANTS3(BraceExprParamFinish, Type, Value, Unknown)
  152. // Handles the `}` of a brace expression.
  153. //
  154. // { ... }
  155. // ^
  156. // (state done)
  157. CARBON_PARSE_STATE_VARIANTS3(BraceExprFinish, Type, Value, Unknown)
  158. // Handles a call expression `(...)`.
  159. //
  160. // F()
  161. // ^
  162. // 1. CallExprFinish
  163. //
  164. // F( ...
  165. // ^
  166. // 1. Expr
  167. // 2. CallExprParamFinish
  168. // 3. CallExprFinish
  169. CARBON_PARSE_STATE(CallExpr)
  170. // Handles the `,` or `)` after a call parameter.
  171. //
  172. // F(a, ...)
  173. // ^
  174. // 1. Expr
  175. // 2. CallExprParamFinish
  176. //
  177. // F(a )
  178. // ^
  179. // (state done)
  180. CARBON_PARSE_STATE(CallExprParamFinish)
  181. // Handles finishing the call expression.
  182. //
  183. // F(a, b)
  184. // ^
  185. // (state done)
  186. CARBON_PARSE_STATE(CallExprFinish)
  187. // Handles processing at the `{` on a typical code block.
  188. //
  189. // if (cond) {
  190. // ^
  191. // 1. StatementScopeLoop
  192. // 2. CodeBlockFinish
  193. //
  194. // if (cond) ???
  195. // ^
  196. // 1. Statement
  197. // 2. CodeBlockFinish
  198. CARBON_PARSE_STATE(CodeBlock)
  199. // Handles processing at the `}` on a typical code block, after a statement
  200. // scope is done.
  201. //
  202. // if (cond) { ... }
  203. // ^
  204. // (state done)
  205. CARBON_PARSE_STATE(CodeBlockFinish)
  206. // Handles a declaration name and parameters, such as `Foo[...](...)`.
  207. //
  208. // Allowed parameters:
  209. // - None: `Foo` only.
  210. // - Optional: `Foo`, `Foo(...)`, or `Foo[...](...)`.
  211. // - Required: `Foo(...)` or `Foo[...](...)`.
  212. //
  213. // name . ...
  214. // ^~~~
  215. // 1. PeriodAsDecl
  216. // 2. DeclNameAndParamsAfterNameAs(None|Optional|Required)
  217. //
  218. // name ...
  219. // ^~~~
  220. // 1. DeclNameAndParamsAfterNameAs(None|Optional|Required)
  221. //
  222. // ???
  223. // ^
  224. // (state done)
  225. CARBON_PARSE_STATE_VARIANTS3(DeclNameAndParams, None, Optional, Required)
  226. // Handles a declaration name between the main name and implicit parameters.
  227. //
  228. // name . ...
  229. // ^
  230. // 1. PeriodAsDecl
  231. // 2. DeclNameAndParamsAfterNameAs(None|Optional|Required)
  232. //
  233. // name [ ... ] (variant is not None)
  234. // ^
  235. // 1. ParamListAsImplicit
  236. // 2. DeclNameAndParamsAfterImplicit
  237. //
  238. // name ( ... ) (variant is not None)
  239. // ^
  240. // 1. ParamListAsRegular
  241. //
  242. // name ... (variant is not Required)
  243. // ^
  244. // (state done)
  245. //
  246. // name ??? (variant is Required)
  247. // ^
  248. // (state done)
  249. CARBON_PARSE_STATE_VARIANTS3(DeclNameAndParamsAfterName, None, Optional,
  250. Required)
  251. // Handles regular parameters such as `(...)` for the general declaration case.
  252. // Only used after implicit parameters.
  253. //
  254. // name [ ... ] ( ... )
  255. // ^
  256. // 1. ParamListAsRegular
  257. //
  258. // name [ ... ] ???
  259. // ^
  260. // (state done)
  261. CARBON_PARSE_STATE(DeclNameAndParamsAfterImplicit)
  262. // Handles processing of a declaration scope. Things like fn, class, interface,
  263. // and so on.
  264. //
  265. // class ...
  266. // ^~~~~
  267. // abstract class ...
  268. // ^~~~~~~~~~~~~~
  269. // base class ...
  270. // ^~~~~~~~~~
  271. // 1. TypeAfterIntroducerAsClass
  272. // 2. DeclScopeLoop
  273. //
  274. // constraint ...
  275. // ^~~~~~~~~~
  276. // 1. TypeAfterIntroducerAsNamedConstraint
  277. // 2. DeclScopeLoop
  278. //
  279. // fn ...
  280. // ^
  281. // 1. FunctionIntroducer
  282. // 2. DeclScopeLoop
  283. //
  284. // interface ...
  285. // ^~~~~~~~~
  286. // 1. TypeAfterIntroducerAsInterface
  287. // 2. DeclScopeLoop
  288. //
  289. // namespace ...
  290. // ^
  291. // 1. Namespace
  292. // 2. DeclScopeLoop
  293. //
  294. // ;
  295. // ^
  296. // 1. DeclScopeLoop
  297. //
  298. // var ...
  299. // ^
  300. // 1. VarAsDecl
  301. // 2. DeclScopeLoop
  302. //
  303. // let ...
  304. // ^
  305. // 1. Let
  306. // 2. DeclScopeLoop
  307. //
  308. // ??? ;
  309. // ^~~~~
  310. // (state done)
  311. CARBON_PARSE_STATE(DeclScopeLoop)
  312. // Handles periods. Only does one `.<expression>` segment; the source is
  313. // responsible for handling chaining.
  314. //
  315. // The forms of this are:
  316. // - Qualified names in declarations.
  317. // - Member access expressions.
  318. // - Designated names in structs.
  319. //
  320. // Declarations and expressions have qualifiers such as `x.y`, while structs
  321. // have designators such as `.z`.
  322. //
  323. // . name
  324. // ^~~~~~
  325. // (state done)
  326. //
  327. // . ??? (??? consumed if it is a keyword)
  328. // ^
  329. // (state done)
  330. CARBON_PARSE_STATE_VARIANTS3(Period, Decl, Expr, Struct)
  331. // Handles `->name` expressions. Identical to PeriodAsExpr except for the
  332. // leading token.
  333. //
  334. // -> name
  335. // ^~~~~~~
  336. // (state done)
  337. //
  338. // -> ??? (??? consumed if it is a keyword)
  339. // ^~
  340. // (state done)
  341. CARBON_PARSE_STATE(ArrowExpr)
  342. // Handles processing of an expression.
  343. //
  344. // if ...
  345. // ^~
  346. // 1. Expr
  347. // 2. IfExprCondition
  348. // 3. IfExprFinish
  349. //
  350. // <prefix operator> ...
  351. // ^~~~~~~~~~~~~~~~~
  352. // 1. Expr
  353. // 2. ExprLoopForPrefix
  354. //
  355. // ...
  356. // ^
  357. // 1. ExprInPostfix
  358. // 2. ExprLoop
  359. CARBON_PARSE_STATE(Expr)
  360. // Handles the initial part of postfix expressions, such as an identifier or
  361. // literal value, then proceeds to the loop.
  362. //
  363. // identifier
  364. // ^~~~~~~~~~
  365. // literal
  366. // ^~~~~~~
  367. // self
  368. // ^~~~
  369. // Self
  370. // ^~~~
  371. // 1. ExprInPostfixLoop
  372. //
  373. // {
  374. // ^
  375. // 1. BraceExpr
  376. // 2. ExprInPostfixLoop
  377. //
  378. // (
  379. // ^
  380. // 1. ParenExpr
  381. // 2. ExprInPostfixLoop
  382. //
  383. // [
  384. // ^
  385. // 1. ArrayExpr
  386. // 2. ExprInPostfixLoop
  387. //
  388. // ???
  389. // ^
  390. // (state done)
  391. CARBON_PARSE_STATE(ExprInPostfix)
  392. // Handles looping through elements following the initial postfix expression,
  393. // such as designators or parenthesized parameters.
  394. //
  395. // expr . ...
  396. // ^
  397. // 1. PeriodAsExpr
  398. // 2. ExprInPostfixLoop
  399. //
  400. // expr -> ...
  401. // ^
  402. // 1. ArrowExpr
  403. // 2. ExprInPostfixLoop
  404. //
  405. // expr ( ... )
  406. // ^
  407. // 1. CallExpr
  408. // 2. ExprInPostfixLoop
  409. //
  410. // expr [ ... ]
  411. // ^
  412. // 1. IndexExprStart
  413. // 2. ExprInPostfixLoop
  414. //
  415. // ...
  416. // ^
  417. // (state done)
  418. CARBON_PARSE_STATE(ExprInPostfixLoop)
  419. // Handles processing of an expression.
  420. //
  421. // expr <binary operator> ...
  422. // ^~~~~~~~~~~~~~~~~
  423. // 1. Expr
  424. // 2. ExprLoopForBinary
  425. //
  426. // expr <postfix operator>
  427. // ^~~~~~~~~~~~~~~~~~
  428. // 1. ExprLoop
  429. //
  430. // expr ...
  431. // ^
  432. // (state done)
  433. CARBON_PARSE_STATE(ExprLoop)
  434. // Completes an ExprLoop pass by adding an infix operator, then goes back
  435. // to ExprLoop.
  436. //
  437. // expr <binary operator> expr ...
  438. // ^
  439. // 1. ExprLoop
  440. CARBON_PARSE_STATE(ExprLoopForBinary)
  441. // Completes an ExprLoop pass by adding a prefix operator, then goes back
  442. // to ExprLoop.
  443. //
  444. // <prefix operator> expr ...
  445. // ^
  446. // 1. ExprLoop
  447. CARBON_PARSE_STATE(ExprLoopForPrefix)
  448. // Completes the condition of an `if` expression and handles the `then` token.
  449. //
  450. // if expr then ...
  451. // ^~~~
  452. // 1. Expr
  453. // 2. IfExprFinishThen
  454. //
  455. // if expr ???
  456. // ^
  457. // (state done)
  458. CARBON_PARSE_STATE(IfExprFinishCondition)
  459. // Completes the first alternative in an `if` expression and handles the `else`
  460. // token.
  461. //
  462. // if expr then expr else ...
  463. // ^~~~
  464. // 1. Expr
  465. // 2. IfExprFinishElse
  466. //
  467. // if expr then expr ???
  468. // ^
  469. // (state done)
  470. CARBON_PARSE_STATE(IfExprFinishThen)
  471. // Completes the second alternative in an `if` expression.
  472. //
  473. // if expr then expr else expr
  474. // ^
  475. // (state done)
  476. CARBON_PARSE_STATE(IfExprFinishElse)
  477. // Completes an IfExpr.
  478. //
  479. // if expr then expr else expr
  480. // ^
  481. // if ???
  482. // ^
  483. // (state done)
  484. CARBON_PARSE_STATE(IfExprFinish)
  485. // Handles the `;` for an expression statement, which is different from most
  486. // keyword statements.
  487. //
  488. // expr ;
  489. // ^
  490. // expr ??? ;
  491. // ^~~~~
  492. // (state done)
  493. CARBON_PARSE_STATE(ExprStatementFinish)
  494. // Handles a function's introducer.
  495. //
  496. // fn ...
  497. // ^~
  498. // 1. DeclNameAndParamsAsRequired
  499. // 2. FunctionAfterParams
  500. CARBON_PARSE_STATE(FunctionIntroducer)
  501. // Handles processing of a function's syntax after `)`, primarily the
  502. // possibility a `->` return type is there. Always enqueues signature finish
  503. // handling.
  504. //
  505. // fn F(...) -> ...
  506. // ^~
  507. // 1. Expr
  508. // 2. FunctionReturnTypeFinish
  509. // 3. FunctionSignatureFinish
  510. //
  511. // fn F(...) ...
  512. // ^
  513. // 1. FunctionSignatureFinish
  514. CARBON_PARSE_STATE(FunctionAfterParams)
  515. // Finishes a function return type.
  516. //
  517. // fn F(...) -> expr ...
  518. // ^
  519. // (state done)
  520. CARBON_PARSE_STATE(FunctionReturnTypeFinish)
  521. // Finishes a function signature. If it's a declaration, the function is done;
  522. // otherwise, this also starts definition processing.
  523. //
  524. // fn ... ;
  525. // ^
  526. // (state done)
  527. //
  528. // fn ... {
  529. // ^
  530. // 1. StatementScopeLoop
  531. // 2. FunctionDefinitionFinish
  532. //
  533. // fn ... ??? ;
  534. // ^~~~~
  535. // (state done)
  536. CARBON_PARSE_STATE(FunctionSignatureFinish)
  537. // Finishes a function definition.
  538. //
  539. // fn ... }
  540. // ^
  541. // fn ... ;
  542. // ^
  543. // (state done)
  544. CARBON_PARSE_STATE(FunctionDefinitionFinish)
  545. // Handles `import`.
  546. //
  547. // import pkgname [library "libname"] ;
  548. // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  549. // import ??? ;
  550. // ^~~~~~~~~~~~
  551. // (state done)
  552. CARBON_PARSE_STATE(Import)
  553. // Handles `library` in directive form.
  554. //
  555. // Always:
  556. // (state done)
  557. CARBON_PARSE_STATE(Library)
  558. // Handles `namespace`.
  559. //
  560. // namespace ...
  561. // ^~~~~~~~~
  562. // 1. DeclNameAndParamsAsNone
  563. // 2. NamespaceFinish
  564. CARBON_PARSE_STATE(Namespace)
  565. // Handles `namespace` after the name.
  566. //
  567. // namespace ... ;
  568. // ^
  569. // namespace ... ??? ;
  570. // ^~~~~
  571. // (state done)
  572. CARBON_PARSE_STATE(NamespaceFinish)
  573. // Handles `package`.
  574. //
  575. // package pkgname [library "libname"] [api|impl] ;
  576. // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  577. // package ??? ;
  578. // ^~~~~~~~~~~~~
  579. // (state done)
  580. CARBON_PARSE_STATE(Package)
  581. // Starts parameter parsing.
  582. //
  583. // ...
  584. // ^
  585. // 1. PatternAs(ImplicitParam|Param)
  586. // 2. ParamFinishAs(Implicit|Regular)
  587. CARBON_PARSE_STATE_VARIANTS2(Param, Implicit, Regular)
  588. // Finishes parsing a parameter, including the optional trailing `,`. If there
  589. // are more parameters, enqueues another parameter parsing state.
  590. //
  591. // ... , )
  592. // ^
  593. // (state done)
  594. //
  595. // ... , ...
  596. // ^
  597. // 1. ParamAs(Implicit|Regular)
  598. //
  599. // ...
  600. // ^
  601. // (state done)
  602. CARBON_PARSE_STATE_VARIANTS2(ParamFinish, Implicit, Regular)
  603. // Handles processing of a parameter list `[` or `(`.
  604. //
  605. // ( ) (variant is Regular)
  606. // ^
  607. // [ ] (variant is Implicit)
  608. // ^
  609. // 1. ParamListFinishAs(Regular|Implicit)
  610. //
  611. // ( ... ) (variant is Regular)
  612. // ^
  613. // [ ... ] (variant is Implicit)
  614. // ^
  615. // 1. ParamAs(Regular|Implicit)
  616. // 2. ParamListFinishAs(Regular|Implicit)
  617. CARBON_PARSE_STATE_VARIANTS2(ParamList, Implicit, Regular)
  618. // Handles processing of a parameter list `]` or `)`.
  619. //
  620. // ( ... ) (variant is Regular)
  621. // ^
  622. // [ ... ] (variant is Implicit)
  623. // ^
  624. // (state done)
  625. CARBON_PARSE_STATE_VARIANTS2(ParamListFinish, Implicit, Regular)
  626. // Handles the processing of a `(condition)` up through the expression.
  627. //
  628. // if/while { (invalid)
  629. // ^
  630. // 1. ParenConditionAs(If|While)Finish
  631. //
  632. // if/while ( ... )
  633. // ^
  634. // if/while ???
  635. // ^
  636. // 1. Expr
  637. // 2. ParenConditionAs(If|While)Finish
  638. CARBON_PARSE_STATE_VARIANTS2(ParenCondition, If, While)
  639. // Finishes the processing of a `(condition)` after the expression.
  640. //
  641. // if/while ( expr )
  642. // ^
  643. // if/while {
  644. // ^
  645. // if/while ??? {
  646. // ^
  647. // (state done)
  648. CARBON_PARSE_STATE_VARIANTS2(ParenConditionFinish, If, While)
  649. // Handles the `(` of a parenthesized expression.
  650. //
  651. // ( )
  652. // ^
  653. // 1. ParenExprFinishAsTuple
  654. //
  655. // ( ... )
  656. // ^
  657. // 1. Expr
  658. // 2. ParenExprParamFinishAsUnknown
  659. // 3. ParenExprFinishAsNormal (SPECIAL: may be replaced)
  660. CARBON_PARSE_STATE(ParenExpr)
  661. // Handles the end of a parenthesized expression's parameter. This will start as
  662. // AsUnknown on the first parameter; if there are more, it switches to AsTuple
  663. // processing.
  664. //
  665. // ( ... , )
  666. // ^
  667. // (state done)
  668. // SPECIAL (variant is Unknown): parent becomes ParenExprFinishAsTuple
  669. //
  670. // ( ... , ... )
  671. // ^
  672. // 1. Expression
  673. // 2. ParenExpressionParamFinishAsTuple
  674. // SPECIAL (variant is Unknown): parent becomes ParenExprFinishAsTuple
  675. //
  676. // ...
  677. // ^
  678. // (state done)
  679. CARBON_PARSE_STATE_VARIANTS2(ParenExprParamFinish, Unknown, Tuple)
  680. // Handles the `)` of a parenthesized expression.
  681. //
  682. // ( ... )
  683. // ^
  684. // (state done)
  685. CARBON_PARSE_STATE_VARIANTS2(ParenExprFinish, Normal, Tuple)
  686. // Handles pattern parsing for a pattern, enqueuing type expression processing.
  687. // This covers parameter, `let`, and `var` support.
  688. //
  689. // template (variant is not Variable)
  690. // ^~~~~~~~
  691. // 4. PatternTemplate
  692. //
  693. // THEN
  694. //
  695. // addr (variant is not Variable)
  696. // ^~~~
  697. // 3. PatternAddress
  698. //
  699. // THEN
  700. //
  701. // name: ...
  702. // ^~~~~
  703. // self: ...
  704. // ^~~~~
  705. // 1. Expr
  706. // 2. PatternFinishAsRegular
  707. //
  708. // name:! ...
  709. // ^~~~~~
  710. // self:! ...
  711. // ^~~~~~
  712. // 1. Expr
  713. // 2. PatternFinishAsGeneric
  714. //
  715. // ???
  716. // ^
  717. // 1. PatternFinishAsRegular
  718. CARBON_PARSE_STATE_VARIANTS4(Pattern, ImplicitParam, Param, Variable,
  719. Let)
  720. // Handles `addr` in a pattern.
  721. //
  722. // addr name: type
  723. // ^
  724. // (state done)
  725. CARBON_PARSE_STATE(PatternAddress)
  726. // Handles `template` in a pattern.
  727. //
  728. // template name:! type
  729. // ^
  730. // (state done)
  731. CARBON_PARSE_STATE(PatternTemplate)
  732. // Finishes pattern processing.
  733. //
  734. // name: type
  735. // ^
  736. // (state done)
  737. CARBON_PARSE_STATE_VARIANTS2(PatternFinish, Generic, Regular)
  738. // Handles a single statement. While typically within a statement block, this
  739. // can also be used for error recovery where we expect a statement block and
  740. // are missing braces.
  741. //
  742. // break ...
  743. // ^~~~~
  744. // 1. StatementBreakFinish
  745. //
  746. // continue ...
  747. // ^~~~~~~~
  748. // 1. StatementContinueFinish
  749. //
  750. // for ...
  751. // ^~~
  752. // 1. StatementForHeader
  753. // 2. StatementForFinish
  754. //
  755. // if ...
  756. // ^
  757. // 1. StatementIf
  758. //
  759. // return ...
  760. // ^
  761. // 1. StatementReturn
  762. //
  763. // var ...
  764. // ^
  765. // 1. VarAsDecl
  766. //
  767. // returned ...
  768. // ^
  769. // 1. VarAsReturned
  770. //
  771. // while ...
  772. // ^
  773. // 1. StatementWhile
  774. //
  775. // ...
  776. // ^
  777. // 1. Expr
  778. // 2. ExprStatementFinish
  779. CARBON_PARSE_STATE(Statement)
  780. // Handles `break` processing at the `;`.
  781. //
  782. // break ;
  783. // ^
  784. // (state done)
  785. CARBON_PARSE_STATE(StatementBreakFinish)
  786. // Handles `continue` processing at the `;`.
  787. //
  788. // continue ;
  789. // ^
  790. // (state done)
  791. CARBON_PARSE_STATE(StatementContinueFinish)
  792. // Handles `for` processing of `(var`, proceeding to a pattern before
  793. // continuing.
  794. //
  795. // for ( var ... )
  796. // ^
  797. // 1. VarAsFor
  798. // 2. StatementForHeaderIn
  799. //
  800. // for ( ???
  801. // ^
  802. // for ( ??? in ...
  803. // ^~~~~~~~
  804. // for ??? in ...
  805. // ^~~~~~
  806. // for ???
  807. // ^
  808. // 1. StatementForHeaderIn
  809. CARBON_PARSE_STATE(StatementForHeader)
  810. // Handles `for` processing of `in`, proceeding to an expression before
  811. // continuing.
  812. //
  813. // for ( ... in ... )
  814. // ^
  815. // 1. Expr
  816. // 2. StatementForHeaderFinish
  817. CARBON_PARSE_STATE(StatementForHeaderIn)
  818. // Handles `for` processing of `)`, proceeding to the statement block.
  819. //
  820. // for ( ... ) ...
  821. // ^
  822. // 1. CodeBlock
  823. CARBON_PARSE_STATE(StatementForHeaderFinish)
  824. // Handles `for` processing after the final `}`.
  825. //
  826. // for ( ... ) { ... }
  827. // ^
  828. // (state done)
  829. CARBON_PARSE_STATE(StatementForFinish)
  830. // Handles `if` processing at the start.
  831. //
  832. // if ...
  833. // ^~
  834. // 1. ParenConditionAsIf
  835. // 2. StatementIfConditionFinish
  836. CARBON_PARSE_STATE(StatementIf)
  837. // Handles `if` processing between the condition and start of the first code
  838. // block.
  839. //
  840. // if ( ... ) ...
  841. // ^
  842. // 1. CodeBlock
  843. // 2. StatementIfThenBlockFinish
  844. CARBON_PARSE_STATE(StatementIfConditionFinish)
  845. // Handles `if` processing after the end of the first code block, with the
  846. // optional `else`.
  847. //
  848. // if ( ... ) { ... } else if ...
  849. // ^~~~
  850. // 1. StatementIf
  851. // 2. StatementIfElseBlockFinish
  852. //
  853. // if ( ... ) { ... } else ...
  854. // ^~~~
  855. // 1. CodeBlock
  856. // 2. StatementIfElseBlockFinish
  857. //
  858. // if ( ... ) { ... } ...
  859. // (state done)
  860. CARBON_PARSE_STATE(StatementIfThenBlockFinish)
  861. // Handles `if` processing after a provided `else` code block.
  862. //
  863. // if ( ... ) { ... } else { ... }
  864. // ^
  865. // (state done)
  866. CARBON_PARSE_STATE(StatementIfElseBlockFinish)
  867. // Handles `return` processing.
  868. //
  869. // return ;
  870. // ^~~~~~
  871. // 1. StatementReturnFinish
  872. //
  873. // return var ...
  874. // ^~~~~~~~~~
  875. // 1. StatementReturnFinish
  876. //
  877. // return ...
  878. // ^~~~~~
  879. // 1. Expr
  880. // 2. StatementReturnFinish
  881. CARBON_PARSE_STATE(StatementReturn)
  882. // Handles `return` processing at the `;`.
  883. //
  884. // return ... ;
  885. // ^
  886. // (state done)
  887. CARBON_PARSE_STATE(StatementReturnFinish)
  888. // Handles processing of statements within a scope.
  889. //
  890. // { ... }
  891. // ^
  892. // (state done)
  893. //
  894. // { ... ... }
  895. // ^
  896. // 1. Statement
  897. // 2. StatementScopeLoop
  898. CARBON_PARSE_STATE(StatementScopeLoop)
  899. // Handles `while` processing.
  900. //
  901. // while ...
  902. // ^~~~~
  903. // 1. ParenConditionAsWhile
  904. // 2. StatementWhileConditionFinish
  905. CARBON_PARSE_STATE(StatementWhile)
  906. // Handles `while` processing between the condition and start of the code block.
  907. //
  908. // while ( ... ) ...
  909. // ^
  910. // 1. CodeBlock
  911. // 2. StatementWhileBlockFinish
  912. CARBON_PARSE_STATE(StatementWhileConditionFinish)
  913. // Handles `while` processing after the end of the code block.
  914. //
  915. // while ( ... ) { ... }
  916. // ^
  917. // (state done)
  918. CARBON_PARSE_STATE(StatementWhileBlockFinish)
  919. // Handles parsing after the declaration scope of a type.
  920. //
  921. // class/interface/constraint { ... }
  922. // ^
  923. // (state done)
  924. CARBON_PARSE_STATE_VARIANTS3(TypeDefinitionFinish, Class, Interface,
  925. NamedConstraint)
  926. // Handles processing of a type after its introducer.
  927. //
  928. // class/interface/constraint ...
  929. // ^~~~~~~~~~~~~~~~~~~~~~~~~~
  930. // 1. DeclNameAndParamsAsOptional
  931. // 2. TypeAfterParamsAs(Class|Interface|NamedConstraint)
  932. CARBON_PARSE_STATE_VARIANTS3(TypeAfterIntroducer, Class, Interface, NamedConstraint)
  933. // Handles processing of a type after its optional parameters.
  934. //
  935. // class/interface/constraint name ( ... ) {
  936. // ^
  937. // 1. DeclScopeLoop
  938. // 2. TypeDefinitionFinishAs(Class|Interface|NamedConstraint)
  939. //
  940. // class/interface/constraint name ( ... ) ;
  941. // ^
  942. // class/interface/constraint name ( ... ) ???
  943. // ^
  944. // (state done)
  945. CARBON_PARSE_STATE_VARIANTS3(TypeAfterParams, Class, Interface, NamedConstraint)
  946. // Handles the start of a `var` or `returned var`.
  947. //
  948. // var ... (variant is not Returned)
  949. // ^~~
  950. // 1. PatternAsVariable
  951. // 2. VarAfterPattern
  952. // 3. VarFinishAs(Decl|For)
  953. //
  954. // returned var ... (variant is Returned)
  955. // ^~~~~~~~~~~~
  956. // 1. PatternAsVariable
  957. // 2. VarAfterPattern
  958. // 3. VarFinishAsDecl
  959. //
  960. // returned ??? ; (variant is Returned)
  961. // ^~~~~~~~~~~~~~
  962. // (state done)
  963. CARBON_PARSE_STATE_VARIANTS3(Var, Decl, Returned, For)
  964. // Handles `var` after the pattern, either followed by an initializer or the
  965. // semicolon.
  966. //
  967. // var ... = ...
  968. // ^
  969. // var ... ??? = ...
  970. // ^~~~~
  971. // 1. Expr
  972. //
  973. // var ... ...
  974. // ^
  975. // (state done)
  976. CARBON_PARSE_STATE(VarAfterPattern)
  977. // Handles `var` parsing at the end.
  978. //
  979. // var ... ; (variant is Semicolon)
  980. // ^
  981. // var ... ??? ; (variant is Semicolon)
  982. // ^~~~~
  983. // (state done)
  984. //
  985. // var ... in (variant is For)
  986. // ^~
  987. // var ... : (variant is For, invalid)
  988. // ^
  989. // (state done)
  990. CARBON_PARSE_STATE_VARIANTS2(VarFinish, Decl, For)
  991. // Handles the start of a `let`.
  992. //
  993. // let ...
  994. // ^~~
  995. // 1. PatternAsLet
  996. // 2. LetAfterPattern
  997. // 3. LetFinish
  998. CARBON_PARSE_STATE(Let)
  999. // Handles `let` after the pattern, followed by an initializer.
  1000. //
  1001. // let ... = ...
  1002. // ^
  1003. // let ... ??? = ...
  1004. // ^~~~~
  1005. // 1. Expr
  1006. //
  1007. // let ... ??? ;
  1008. // ^~~
  1009. // (state done)
  1010. CARBON_PARSE_STATE(LetAfterPattern)
  1011. // Handles `let` parsing at the end.
  1012. //
  1013. // let ... ;
  1014. // ^
  1015. // (state done)
  1016. CARBON_PARSE_STATE(LetFinish)
  1017. #undef CARBON_PARSE_STATE