state.def 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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 `namespace`.
  554. //
  555. // namespace ...
  556. // ^~~~~~~~~
  557. // 1. DeclNameAndParamsAsNone
  558. // 2. NamespaceFinish
  559. CARBON_PARSE_STATE(Namespace)
  560. // Handles `namespace` after the name.
  561. //
  562. // namespace ... ;
  563. // ^
  564. // namespace ... ??? ;
  565. // ^~~~~
  566. // (state done)
  567. CARBON_PARSE_STATE(NamespaceFinish)
  568. // Handles `package`.
  569. //
  570. // package pkgname [library "libname"] [api|impl] ;
  571. // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  572. // package ??? ;
  573. // ^~~~~~~~~~~~~
  574. // (state done)
  575. CARBON_PARSE_STATE(Package)
  576. // Starts parameter parsing.
  577. //
  578. // ...
  579. // ^
  580. // 1. PatternAs(ImplicitParam|Param)
  581. // 2. ParamFinishAs(Implicit|Regular)
  582. CARBON_PARSE_STATE_VARIANTS2(Param, Implicit, Regular)
  583. // Finishes parsing a parameter, including the optional trailing `,`. If there
  584. // are more parameters, enqueues another parameter parsing state.
  585. //
  586. // ... , )
  587. // ^
  588. // (state done)
  589. //
  590. // ... , ...
  591. // ^
  592. // 1. ParamAs(Implicit|Regular)
  593. //
  594. // ...
  595. // ^
  596. // (state done)
  597. CARBON_PARSE_STATE_VARIANTS2(ParamFinish, Implicit, Regular)
  598. // Handles processing of a parameter list `[` or `(`.
  599. //
  600. // ( ) (variant is Regular)
  601. // ^
  602. // [ ] (variant is Implicit)
  603. // ^
  604. // 1. ParamListFinishAs(Regular|Implicit)
  605. //
  606. // ( ... ) (variant is Regular)
  607. // ^
  608. // [ ... ] (variant is Implicit)
  609. // ^
  610. // 1. ParamAs(Regular|Implicit)
  611. // 2. ParamListFinishAs(Regular|Implicit)
  612. CARBON_PARSE_STATE_VARIANTS2(ParamList, Implicit, Regular)
  613. // Handles processing of a parameter list `]` or `)`.
  614. //
  615. // ( ... ) (variant is Regular)
  616. // ^
  617. // [ ... ] (variant is Implicit)
  618. // ^
  619. // (state done)
  620. CARBON_PARSE_STATE_VARIANTS2(ParamListFinish, Implicit, Regular)
  621. // Handles the processing of a `(condition)` up through the expression.
  622. //
  623. // if/while { (invalid)
  624. // ^
  625. // 1. ParenConditionAs(If|While)Finish
  626. //
  627. // if/while ( ... )
  628. // ^
  629. // if/while ???
  630. // ^
  631. // 1. Expr
  632. // 2. ParenConditionAs(If|While)Finish
  633. CARBON_PARSE_STATE_VARIANTS2(ParenCondition, If, While)
  634. // Finishes the processing of a `(condition)` after the expression.
  635. //
  636. // if/while ( expr )
  637. // ^
  638. // if/while {
  639. // ^
  640. // if/while ??? {
  641. // ^
  642. // (state done)
  643. CARBON_PARSE_STATE_VARIANTS2(ParenConditionFinish, If, While)
  644. // Handles the `(` of a parenthesized expression.
  645. //
  646. // ( )
  647. // ^
  648. // 1. ParenExprFinishAsTuple
  649. //
  650. // ( ... )
  651. // ^
  652. // 1. Expr
  653. // 2. ParenExprParamFinishAsUnknown
  654. // 3. ParenExprFinishAsNormal (SPECIAL: may be replaced)
  655. CARBON_PARSE_STATE(ParenExpr)
  656. // Handles the end of a parenthesized expression's parameter. This will start as
  657. // AsUnknown on the first parameter; if there are more, it switches to AsTuple
  658. // processing.
  659. //
  660. // ( ... , )
  661. // ^
  662. // (state done)
  663. // SPECIAL (variant is Unknown): parent becomes ParenExprFinishAsTuple
  664. //
  665. // ( ... , ... )
  666. // ^
  667. // 1. Expression
  668. // 2. ParenExpressionParamFinishAsTuple
  669. // SPECIAL (variant is Unknown): parent becomes ParenExprFinishAsTuple
  670. //
  671. // ...
  672. // ^
  673. // (state done)
  674. CARBON_PARSE_STATE_VARIANTS2(ParenExprParamFinish, Unknown, Tuple)
  675. // Handles the `)` of a parenthesized expression.
  676. //
  677. // ( ... )
  678. // ^
  679. // (state done)
  680. CARBON_PARSE_STATE_VARIANTS2(ParenExprFinish, Normal, Tuple)
  681. // Handles pattern parsing for a pattern, enqueuing type expression processing.
  682. // This covers parameter, `let`, and `var` support.
  683. //
  684. // template (variant is not Variable)
  685. // ^~~~~~~~
  686. // 4. PatternTemplate
  687. //
  688. // THEN
  689. //
  690. // addr (variant is not Variable)
  691. // ^~~~
  692. // 3. PatternAddress
  693. //
  694. // THEN
  695. //
  696. // name: ...
  697. // ^~~~~
  698. // self: ...
  699. // ^~~~~
  700. // 1. Expr
  701. // 2. PatternFinishAsRegular
  702. //
  703. // name:! ...
  704. // ^~~~~~
  705. // self:! ...
  706. // ^~~~~~
  707. // 1. Expr
  708. // 2. PatternFinishAsGeneric
  709. //
  710. // ???
  711. // ^
  712. // 1. PatternFinishAsRegular
  713. CARBON_PARSE_STATE_VARIANTS4(Pattern, ImplicitParam, Param, Variable,
  714. Let)
  715. // Handles `addr` in a pattern.
  716. //
  717. // addr name: type
  718. // ^
  719. // (state done)
  720. CARBON_PARSE_STATE(PatternAddress)
  721. // Handles `template` in a pattern.
  722. //
  723. // template name:! type
  724. // ^
  725. // (state done)
  726. CARBON_PARSE_STATE(PatternTemplate)
  727. // Finishes pattern processing.
  728. //
  729. // name: type
  730. // ^
  731. // (state done)
  732. CARBON_PARSE_STATE_VARIANTS2(PatternFinish, Generic, Regular)
  733. // Handles a single statement. While typically within a statement block, this
  734. // can also be used for error recovery where we expect a statement block and
  735. // are missing braces.
  736. //
  737. // break ...
  738. // ^~~~~
  739. // 1. StatementBreakFinish
  740. //
  741. // continue ...
  742. // ^~~~~~~~
  743. // 1. StatementContinueFinish
  744. //
  745. // for ...
  746. // ^~~
  747. // 1. StatementForHeader
  748. // 2. StatementForFinish
  749. //
  750. // if ...
  751. // ^
  752. // 1. StatementIf
  753. //
  754. // return ...
  755. // ^
  756. // 1. StatementReturn
  757. //
  758. // var ...
  759. // ^
  760. // 1. VarAsDecl
  761. //
  762. // returned ...
  763. // ^
  764. // 1. VarAsReturned
  765. //
  766. // while ...
  767. // ^
  768. // 1. StatementWhile
  769. //
  770. // ...
  771. // ^
  772. // 1. Expr
  773. // 2. ExprStatementFinish
  774. CARBON_PARSE_STATE(Statement)
  775. // Handles `break` processing at the `;`.
  776. //
  777. // break ;
  778. // ^
  779. // (state done)
  780. CARBON_PARSE_STATE(StatementBreakFinish)
  781. // Handles `continue` processing at the `;`.
  782. //
  783. // continue ;
  784. // ^
  785. // (state done)
  786. CARBON_PARSE_STATE(StatementContinueFinish)
  787. // Handles `for` processing of `(var`, proceeding to a pattern before
  788. // continuing.
  789. //
  790. // for ( var ... )
  791. // ^
  792. // 1. VarAsFor
  793. // 2. StatementForHeaderIn
  794. //
  795. // for ( ???
  796. // ^
  797. // for ( ??? in ...
  798. // ^~~~~~~~
  799. // for ??? in ...
  800. // ^~~~~~
  801. // for ???
  802. // ^
  803. // 1. StatementForHeaderIn
  804. CARBON_PARSE_STATE(StatementForHeader)
  805. // Handles `for` processing of `in`, proceeding to an expression before
  806. // continuing.
  807. //
  808. // for ( ... in ... )
  809. // ^
  810. // 1. Expr
  811. // 2. StatementForHeaderFinish
  812. CARBON_PARSE_STATE(StatementForHeaderIn)
  813. // Handles `for` processing of `)`, proceeding to the statement block.
  814. //
  815. // for ( ... ) ...
  816. // ^
  817. // 1. CodeBlock
  818. CARBON_PARSE_STATE(StatementForHeaderFinish)
  819. // Handles `for` processing after the final `}`.
  820. //
  821. // for ( ... ) { ... }
  822. // ^
  823. // (state done)
  824. CARBON_PARSE_STATE(StatementForFinish)
  825. // Handles `if` processing at the start.
  826. //
  827. // if ...
  828. // ^~
  829. // 1. ParenConditionAsIf
  830. // 2. StatementIfConditionFinish
  831. CARBON_PARSE_STATE(StatementIf)
  832. // Handles `if` processing between the condition and start of the first code
  833. // block.
  834. //
  835. // if ( ... ) ...
  836. // ^
  837. // 1. CodeBlock
  838. // 2. StatementIfThenBlockFinish
  839. CARBON_PARSE_STATE(StatementIfConditionFinish)
  840. // Handles `if` processing after the end of the first code block, with the
  841. // optional `else`.
  842. //
  843. // if ( ... ) { ... } else if ...
  844. // ^~~~
  845. // 1. StatementIf
  846. // 2. StatementIfElseBlockFinish
  847. //
  848. // if ( ... ) { ... } else ...
  849. // ^~~~
  850. // 1. CodeBlock
  851. // 2. StatementIfElseBlockFinish
  852. //
  853. // if ( ... ) { ... } ...
  854. // (state done)
  855. CARBON_PARSE_STATE(StatementIfThenBlockFinish)
  856. // Handles `if` processing after a provided `else` code block.
  857. //
  858. // if ( ... ) { ... } else { ... }
  859. // ^
  860. // (state done)
  861. CARBON_PARSE_STATE(StatementIfElseBlockFinish)
  862. // Handles `return` processing.
  863. //
  864. // return ;
  865. // ^~~~~~
  866. // 1. StatementReturnFinish
  867. //
  868. // return var ...
  869. // ^~~~~~~~~~
  870. // 1. StatementReturnFinish
  871. //
  872. // return ...
  873. // ^~~~~~
  874. // 1. Expr
  875. // 2. StatementReturnFinish
  876. CARBON_PARSE_STATE(StatementReturn)
  877. // Handles `return` processing at the `;`.
  878. //
  879. // return ... ;
  880. // ^
  881. // (state done)
  882. CARBON_PARSE_STATE(StatementReturnFinish)
  883. // Handles processing of statements within a scope.
  884. //
  885. // { ... }
  886. // ^
  887. // (state done)
  888. //
  889. // { ... ... }
  890. // ^
  891. // 1. Statement
  892. // 2. StatementScopeLoop
  893. CARBON_PARSE_STATE(StatementScopeLoop)
  894. // Handles `while` processing.
  895. //
  896. // while ...
  897. // ^~~~~
  898. // 1. ParenConditionAsWhile
  899. // 2. StatementWhileConditionFinish
  900. CARBON_PARSE_STATE(StatementWhile)
  901. // Handles `while` processing between the condition and start of the code block.
  902. //
  903. // while ( ... ) ...
  904. // ^
  905. // 1. CodeBlock
  906. // 2. StatementWhileBlockFinish
  907. CARBON_PARSE_STATE(StatementWhileConditionFinish)
  908. // Handles `while` processing after the end of the code block.
  909. //
  910. // while ( ... ) { ... }
  911. // ^
  912. // (state done)
  913. CARBON_PARSE_STATE(StatementWhileBlockFinish)
  914. // Handles parsing after the declaration scope of a type.
  915. //
  916. // class/interface/constraint { ... }
  917. // ^
  918. // (state done)
  919. CARBON_PARSE_STATE_VARIANTS3(TypeDefinitionFinish, Class, Interface,
  920. NamedConstraint)
  921. // Handles processing of a type after its introducer.
  922. //
  923. // class/interface/constraint ...
  924. // ^~~~~~~~~~~~~~~~~~~~~~~~~~
  925. // 1. DeclNameAndParamsAsOptional
  926. // 2. TypeAfterParamsAs(Class|Interface|NamedConstraint)
  927. CARBON_PARSE_STATE_VARIANTS3(TypeAfterIntroducer, Class, Interface, NamedConstraint)
  928. // Handles processing of a type after its optional parameters.
  929. //
  930. // class/interface/constraint name ( ... ) {
  931. // ^
  932. // 1. DeclScopeLoop
  933. // 2. TypeDefinitionFinishAs(Class|Interface|NamedConstraint)
  934. //
  935. // class/interface/constraint name ( ... ) ;
  936. // ^
  937. // class/interface/constraint name ( ... ) ???
  938. // ^
  939. // (state done)
  940. CARBON_PARSE_STATE_VARIANTS3(TypeAfterParams, Class, Interface, NamedConstraint)
  941. // Handles the start of a `var` or `returned var`.
  942. //
  943. // var ... (variant is not Returned)
  944. // ^~~
  945. // 1. PatternAsVariable
  946. // 2. VarAfterPattern
  947. // 3. VarFinishAs(Decl|For)
  948. //
  949. // returned var ... (variant is Returned)
  950. // ^~~~~~~~~~~~
  951. // 1. PatternAsVariable
  952. // 2. VarAfterPattern
  953. // 3. VarFinishAsDecl
  954. //
  955. // returned ??? ; (variant is Returned)
  956. // ^~~~~~~~~~~~~~
  957. // (state done)
  958. CARBON_PARSE_STATE_VARIANTS3(Var, Decl, Returned, For)
  959. // Handles `var` after the pattern, either followed by an initializer or the
  960. // semicolon.
  961. //
  962. // var ... = ...
  963. // ^
  964. // var ... ??? = ...
  965. // ^~~~~
  966. // 1. Expr
  967. //
  968. // var ... ...
  969. // ^
  970. // (state done)
  971. CARBON_PARSE_STATE(VarAfterPattern)
  972. // Handles `var` parsing at the end.
  973. //
  974. // var ... ; (variant is Semicolon)
  975. // ^
  976. // var ... ??? ; (variant is Semicolon)
  977. // ^~~~~
  978. // (state done)
  979. //
  980. // var ... in (variant is For)
  981. // ^~
  982. // var ... : (variant is For, invalid)
  983. // ^
  984. // (state done)
  985. CARBON_PARSE_STATE_VARIANTS2(VarFinish, Decl, For)
  986. // Handles the start of a `let`.
  987. //
  988. // let ...
  989. // ^~~
  990. // 1. PatternAsLet
  991. // 2. LetAfterPattern
  992. // 3. LetFinish
  993. CARBON_PARSE_STATE(Let)
  994. // Handles `let` after the pattern, followed by an initializer.
  995. //
  996. // let ... = ...
  997. // ^
  998. // let ... ??? = ...
  999. // ^~~~~
  1000. // 1. Expr
  1001. //
  1002. // let ... ??? ;
  1003. // ^~~
  1004. // (state done)
  1005. CARBON_PARSE_STATE(LetAfterPattern)
  1006. // Handles `let` parsing at the end.
  1007. //
  1008. // let ... ;
  1009. // ^
  1010. // (state done)
  1011. CARBON_PARSE_STATE(LetFinish)
  1012. #undef CARBON_PARSE_STATE