state.def 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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. // 1. TypeIntroducerAsClass
  268. // 2. DeclScopeLoop
  269. //
  270. // constraint ...
  271. // ^
  272. // 1. TypeIntroducerAsNamedConstraint
  273. // 2. DeclScopeLoop
  274. //
  275. // fn ...
  276. // ^
  277. // 1. FunctionIntroducer
  278. // 2. DeclScopeLoop
  279. //
  280. // interface ...
  281. // ^
  282. // 1. TypeIntroducerAsInterface
  283. // 2. DeclScopeLoop
  284. //
  285. // namespace ...
  286. // ^
  287. // 1. Namespace
  288. // 2. DeclScopeLoop
  289. //
  290. // ;
  291. // ^
  292. // 1. DeclScopeLoop
  293. //
  294. // var ...
  295. // ^
  296. // 1. VarAsDecl
  297. // 2. DeclScopeLoop
  298. //
  299. // let ...
  300. // ^
  301. // 1. Let
  302. // 2. DeclScopeLoop
  303. //
  304. // ??? ;
  305. // ^~~~~
  306. // (state done)
  307. CARBON_PARSE_STATE(DeclScopeLoop)
  308. // Handles periods. Only does one `.<expression>` segment; the source is
  309. // responsible for handling chaining.
  310. //
  311. // The forms of this are:
  312. // - Qualified names in declarations.
  313. // - Member access expressions.
  314. // - Designated names in structs.
  315. //
  316. // Declarations and expressions have qualifiers such as `x.y`, while structs
  317. // have designators such as `.z`.
  318. //
  319. // . name
  320. // ^~~~~~
  321. // (state done)
  322. //
  323. // . ??? (??? consumed if it is a keyword)
  324. // ^
  325. // (state done)
  326. CARBON_PARSE_STATE_VARIANTS3(Period, Decl, Expr, Struct)
  327. // Handles `->name` expressions. Identical to PeriodAsExpr except for the
  328. // leading token.
  329. //
  330. // -> name
  331. // ^~~~~~~
  332. // (state done)
  333. //
  334. // -> ??? (??? consumed if it is a keyword)
  335. // ^~
  336. // (state done)
  337. CARBON_PARSE_STATE(ArrowExpr)
  338. // Handles processing of an expression.
  339. //
  340. // if ...
  341. // ^~
  342. // 1. Expr
  343. // 2. IfExprCondition
  344. // 3. IfExprFinish
  345. //
  346. // <prefix operator> ...
  347. // ^~~~~~~~~~~~~~~~~
  348. // 1. Expr
  349. // 2. ExprLoopForPrefix
  350. //
  351. // ...
  352. // ^
  353. // 1. ExprInPostfix
  354. // 2. ExprLoop
  355. CARBON_PARSE_STATE(Expr)
  356. // Handles the initial part of postfix expressions, such as an identifier or
  357. // literal value, then proceeds to the loop.
  358. //
  359. // identifier
  360. // ^~~~~~~~~~
  361. // literal
  362. // ^~~~~~~
  363. // self
  364. // ^~~~
  365. // Self
  366. // ^~~~
  367. // 1. ExprInPostfixLoop
  368. //
  369. // {
  370. // ^
  371. // 1. BraceExpr
  372. // 2. ExprInPostfixLoop
  373. //
  374. // (
  375. // ^
  376. // 1. ParenExpr
  377. // 2. ExprInPostfixLoop
  378. //
  379. // [
  380. // ^
  381. // 1. ArrayExpr
  382. // 2. ExprInPostfixLoop
  383. //
  384. // ???
  385. // ^
  386. // (state done)
  387. CARBON_PARSE_STATE(ExprInPostfix)
  388. // Handles looping through elements following the initial postfix expression,
  389. // such as designators or parenthesized parameters.
  390. //
  391. // expr . ...
  392. // ^
  393. // 1. PeriodAsExpr
  394. // 2. ExprInPostfixLoop
  395. //
  396. // expr -> ...
  397. // ^
  398. // 1. ArrowExpr
  399. // 2. ExprInPostfixLoop
  400. //
  401. // expr ( ... )
  402. // ^
  403. // 1. CallExpr
  404. // 2. ExprInPostfixLoop
  405. //
  406. // expr [ ... ]
  407. // ^
  408. // 1. IndexExprStart
  409. // 2. ExprInPostfixLoop
  410. //
  411. // ...
  412. // ^
  413. // (state done)
  414. CARBON_PARSE_STATE(ExprInPostfixLoop)
  415. // Handles processing of an expression.
  416. //
  417. // expr <binary operator> ...
  418. // ^~~~~~~~~~~~~~~~~
  419. // 1. Expr
  420. // 2. ExprLoopForBinary
  421. //
  422. // expr <postfix operator>
  423. // ^~~~~~~~~~~~~~~~~~
  424. // 1. ExprLoop
  425. //
  426. // expr ...
  427. // ^
  428. // (state done)
  429. CARBON_PARSE_STATE(ExprLoop)
  430. // Completes an ExprLoop pass by adding an infix operator, then goes back
  431. // to ExprLoop.
  432. //
  433. // expr <binary operator> expr ...
  434. // ^
  435. // 1. ExprLoop
  436. CARBON_PARSE_STATE(ExprLoopForBinary)
  437. // Completes an ExprLoop pass by adding a prefix operator, then goes back
  438. // to ExprLoop.
  439. //
  440. // <prefix operator> expr ...
  441. // ^
  442. // 1. ExprLoop
  443. CARBON_PARSE_STATE(ExprLoopForPrefix)
  444. // Completes the condition of an `if` expression and handles the `then` token.
  445. //
  446. // if expr then ...
  447. // ^~~~
  448. // 1. Expr
  449. // 2. IfExprFinishThen
  450. //
  451. // if expr ???
  452. // ^
  453. // (state done)
  454. CARBON_PARSE_STATE(IfExprFinishCondition)
  455. // Completes the first alternative in an `if` expression and handles the `else`
  456. // token.
  457. //
  458. // if expr then expr else ...
  459. // ^~~~
  460. // 1. Expr
  461. // 2. IfExprFinishElse
  462. //
  463. // if expr then expr ???
  464. // ^
  465. // (state done)
  466. CARBON_PARSE_STATE(IfExprFinishThen)
  467. // Completes the second alternative in an `if` expression.
  468. //
  469. // if expr then expr else expr
  470. // ^
  471. // (state done)
  472. CARBON_PARSE_STATE(IfExprFinishElse)
  473. // Completes an IfExpr.
  474. //
  475. // if expr then expr else expr
  476. // ^
  477. // if ???
  478. // ^
  479. // (state done)
  480. CARBON_PARSE_STATE(IfExprFinish)
  481. // Handles the `;` for an expression statement, which is different from most
  482. // keyword statements.
  483. //
  484. // expr ;
  485. // ^
  486. // expr ??? ;
  487. // ^~~~~
  488. // (state done)
  489. CARBON_PARSE_STATE(ExprStatementFinish)
  490. // Handles a function's introducer.
  491. //
  492. // fn ...
  493. // ^~
  494. // 1. DeclNameAndParamsAsRequired
  495. // 2. FunctionAfterParams
  496. CARBON_PARSE_STATE(FunctionIntroducer)
  497. // Handles processing of a function's syntax after `)`, primarily the
  498. // possibility a `->` return type is there. Always enqueues signature finish
  499. // handling.
  500. //
  501. // fn F(...) -> ...
  502. // ^~
  503. // 1. Expr
  504. // 2. FunctionReturnTypeFinish
  505. // 3. FunctionSignatureFinish
  506. //
  507. // fn F(...) ...
  508. // ^
  509. // 1. FunctionSignatureFinish
  510. CARBON_PARSE_STATE(FunctionAfterParams)
  511. // Finishes a function return type.
  512. //
  513. // fn F(...) -> expr ...
  514. // ^
  515. // (state done)
  516. CARBON_PARSE_STATE(FunctionReturnTypeFinish)
  517. // Finishes a function signature. If it's a declaration, the function is done;
  518. // otherwise, this also starts definition processing.
  519. //
  520. // fn ... ;
  521. // ^
  522. // (state done)
  523. //
  524. // fn ... {
  525. // ^
  526. // 1. StatementScopeLoop
  527. // 2. FunctionDefinitionFinish
  528. //
  529. // fn ... ??? ;
  530. // ^~~~~
  531. // (state done)
  532. CARBON_PARSE_STATE(FunctionSignatureFinish)
  533. // Finishes a function definition.
  534. //
  535. // fn ... }
  536. // ^
  537. // fn ... ;
  538. // ^
  539. // (state done)
  540. CARBON_PARSE_STATE(FunctionDefinitionFinish)
  541. // Handles `import`.
  542. //
  543. // import pkgname [library "libname"] ;
  544. // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  545. // import ??? ;
  546. // ^~~~~~~~~~~~
  547. // (state done)
  548. CARBON_PARSE_STATE(Import)
  549. // Handles `namespace`.
  550. //
  551. // namespace ...
  552. // ^~~~~~~~~
  553. // 1. DeclNameAndParamsAsNone
  554. // 2. NamespaceFinish
  555. CARBON_PARSE_STATE(Namespace)
  556. // Handles `namespace` after the name.
  557. //
  558. // namespace ... ;
  559. // ^
  560. // namespace ... ??? ;
  561. // ^~~~~
  562. // (state done)
  563. CARBON_PARSE_STATE(NamespaceFinish)
  564. // Handles `package`.
  565. //
  566. // package pkgname [library "libname"] [api|impl] ;
  567. // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  568. // package ??? ;
  569. // ^~~~~~~~~~~~~
  570. // (state done)
  571. CARBON_PARSE_STATE(Package)
  572. // Starts parameter parsing.
  573. //
  574. // ...
  575. // ^
  576. // 1. PatternAs(ImplicitParam|Param)
  577. // 2. ParamFinishAs(Implicit|Regular)
  578. CARBON_PARSE_STATE_VARIANTS2(Param, Implicit, Regular)
  579. // Finishes parsing a parameter, including the optional trailing `,`. If there
  580. // are more parameters, enqueues another parameter parsing state.
  581. //
  582. // ... , )
  583. // ^
  584. // (state done)
  585. //
  586. // ... , ...
  587. // ^
  588. // 1. ParamAs(Implicit|Regular)
  589. //
  590. // ...
  591. // ^
  592. // (state done)
  593. CARBON_PARSE_STATE_VARIANTS2(ParamFinish, Implicit, Regular)
  594. // Handles processing of a parameter list `[` or `(`.
  595. //
  596. // ( ) (variant is Regular)
  597. // ^
  598. // [ ] (variant is Implicit)
  599. // ^
  600. // 1. ParamListFinishAs(Regular|Implicit)
  601. //
  602. // ( ... ) (variant is Regular)
  603. // ^
  604. // [ ... ] (variant is Implicit)
  605. // ^
  606. // 1. ParamAs(Regular|Implicit)
  607. // 2. ParamListFinishAs(Regular|Implicit)
  608. CARBON_PARSE_STATE_VARIANTS2(ParamList, Implicit, Regular)
  609. // Handles processing of a parameter list `]` or `)`.
  610. //
  611. // ( ... ) (variant is Regular)
  612. // ^
  613. // [ ... ] (variant is Implicit)
  614. // ^
  615. // (state done)
  616. CARBON_PARSE_STATE_VARIANTS2(ParamListFinish, Implicit, Regular)
  617. // Handles the processing of a `(condition)` up through the expression.
  618. //
  619. // if/while { (invalid)
  620. // ^
  621. // 1. ParenConditionAs(If|While)Finish
  622. //
  623. // if/while ( ... )
  624. // ^
  625. // if/while ???
  626. // ^
  627. // 1. Expr
  628. // 2. ParenConditionAs(If|While)Finish
  629. CARBON_PARSE_STATE_VARIANTS2(ParenCondition, If, While)
  630. // Finishes the processing of a `(condition)` after the expression.
  631. //
  632. // if/while ( expr )
  633. // ^
  634. // if/while {
  635. // ^
  636. // if/while ??? {
  637. // ^
  638. // (state done)
  639. CARBON_PARSE_STATE_VARIANTS2(ParenConditionFinish, If, While)
  640. // Handles the `(` of a parenthesized expression.
  641. //
  642. // ( )
  643. // ^
  644. // 1. ParenExprFinishAsTuple
  645. //
  646. // ( ... )
  647. // ^
  648. // 1. Expr
  649. // 2. ParenExprParamFinishAsUnknown
  650. // 3. ParenExprFinishAsNormal (SPECIAL: may be replaced)
  651. CARBON_PARSE_STATE(ParenExpr)
  652. // Handles the end of a parenthesized expression's parameter. This will start as
  653. // AsUnknown on the first parameter; if there are more, it switches to AsTuple
  654. // processing.
  655. //
  656. // ( ... , )
  657. // ^
  658. // (state done)
  659. // SPECIAL (variant is Unknown): parent becomes ParenExprFinishAsTuple
  660. //
  661. // ( ... , ... )
  662. // ^
  663. // 1. Expression
  664. // 2. ParenExpressionParamFinishAsTuple
  665. // SPECIAL (variant is Unknown): parent becomes ParenExprFinishAsTuple
  666. //
  667. // ...
  668. // ^
  669. // (state done)
  670. CARBON_PARSE_STATE_VARIANTS2(ParenExprParamFinish, Unknown, Tuple)
  671. // Handles the `)` of a parenthesized expression.
  672. //
  673. // ( ... )
  674. // ^
  675. // (state done)
  676. CARBON_PARSE_STATE_VARIANTS2(ParenExprFinish, Normal, Tuple)
  677. // Handles pattern parsing for a pattern, enqueuing type expression processing.
  678. // This covers parameter, `let`, and `var` support.
  679. //
  680. // template (variant is not Variable)
  681. // ^~~~~~~~
  682. // 4. PatternTemplate
  683. //
  684. // THEN
  685. //
  686. // addr (variant is not Variable)
  687. // ^~~~
  688. // 3. PatternAddress
  689. //
  690. // THEN
  691. //
  692. // name: ...
  693. // ^~~~~
  694. // self: ...
  695. // ^~~~~
  696. // 1. Expr
  697. // 2. PatternFinishAsRegular
  698. //
  699. // name:! ...
  700. // ^~~~~~
  701. // self:! ...
  702. // ^~~~~~
  703. // 1. Expr
  704. // 2. PatternFinishAsGeneric
  705. //
  706. // ???
  707. // ^
  708. // 1. PatternFinishAsRegular
  709. CARBON_PARSE_STATE_VARIANTS4(Pattern, ImplicitParam, Param, Variable,
  710. Let)
  711. // Handles `addr` in a pattern.
  712. //
  713. // addr name: type
  714. // ^
  715. // (state done)
  716. CARBON_PARSE_STATE(PatternAddress)
  717. // Handles `template` in a pattern.
  718. //
  719. // template name:! type
  720. // ^
  721. // (state done)
  722. CARBON_PARSE_STATE(PatternTemplate)
  723. // Finishes pattern processing.
  724. //
  725. // name: type
  726. // ^
  727. // (state done)
  728. CARBON_PARSE_STATE_VARIANTS2(PatternFinish, Generic, Regular)
  729. // Handles a single statement. While typically within a statement block, this
  730. // can also be used for error recovery where we expect a statement block and
  731. // are missing braces.
  732. //
  733. // break ...
  734. // ^~~~~
  735. // 1. StatementBreakFinish
  736. //
  737. // continue ...
  738. // ^~~~~~~~
  739. // 1. StatementContinueFinish
  740. //
  741. // for ...
  742. // ^~~
  743. // 1. StatementForHeader
  744. // 2. StatementForFinish
  745. //
  746. // if ...
  747. // ^
  748. // 1. StatementIf
  749. //
  750. // return ...
  751. // ^
  752. // 1. StatementReturn
  753. //
  754. // var ...
  755. // ^
  756. // 1. VarAsDecl
  757. //
  758. // returned ...
  759. // ^
  760. // 1. VarAsReturned
  761. //
  762. // while ...
  763. // ^
  764. // 1. StatementWhile
  765. //
  766. // ...
  767. // ^
  768. // 1. Expr
  769. // 2. ExprStatementFinish
  770. CARBON_PARSE_STATE(Statement)
  771. // Handles `break` processing at the `;`.
  772. //
  773. // break ;
  774. // ^
  775. // (state done)
  776. CARBON_PARSE_STATE(StatementBreakFinish)
  777. // Handles `continue` processing at the `;`.
  778. //
  779. // continue ;
  780. // ^
  781. // (state done)
  782. CARBON_PARSE_STATE(StatementContinueFinish)
  783. // Handles `for` processing of `(var`, proceeding to a pattern before
  784. // continuing.
  785. //
  786. // for ( var ... )
  787. // ^
  788. // 1. VarAsFor
  789. // 2. StatementForHeaderIn
  790. //
  791. // for ( ???
  792. // ^
  793. // for ( ??? in ...
  794. // ^~~~~~~~
  795. // for ??? in ...
  796. // ^~~~~~
  797. // for ???
  798. // ^
  799. // 1. StatementForHeaderIn
  800. CARBON_PARSE_STATE(StatementForHeader)
  801. // Handles `for` processing of `in`, proceeding to an expression before
  802. // continuing.
  803. //
  804. // for ( ... in ... )
  805. // ^
  806. // 1. Expr
  807. // 2. StatementForHeaderFinish
  808. CARBON_PARSE_STATE(StatementForHeaderIn)
  809. // Handles `for` processing of `)`, proceeding to the statement block.
  810. //
  811. // for ( ... ) ...
  812. // ^
  813. // 1. CodeBlock
  814. CARBON_PARSE_STATE(StatementForHeaderFinish)
  815. // Handles `for` processing after the final `}`.
  816. //
  817. // for ( ... ) { ... }
  818. // ^
  819. // (state done)
  820. CARBON_PARSE_STATE(StatementForFinish)
  821. // Handles `if` processing at the start.
  822. //
  823. // if ...
  824. // ^~
  825. // 1. ParenConditionAsIf
  826. // 2. StatementIfConditionFinish
  827. CARBON_PARSE_STATE(StatementIf)
  828. // Handles `if` processing between the condition and start of the first code
  829. // block.
  830. //
  831. // if ( ... ) ...
  832. // ^
  833. // 1. CodeBlock
  834. // 2. StatementIfThenBlockFinish
  835. CARBON_PARSE_STATE(StatementIfConditionFinish)
  836. // Handles `if` processing after the end of the first code block, with the
  837. // optional `else`.
  838. //
  839. // if ( ... ) { ... } else if ...
  840. // ^~~~
  841. // 1. StatementIf
  842. // 2. StatementIfElseBlockFinish
  843. //
  844. // if ( ... ) { ... } else ...
  845. // ^~~~
  846. // 1. CodeBlock
  847. // 2. StatementIfElseBlockFinish
  848. //
  849. // if ( ... ) { ... } ...
  850. // (state done)
  851. CARBON_PARSE_STATE(StatementIfThenBlockFinish)
  852. // Handles `if` processing after a provided `else` code block.
  853. //
  854. // if ( ... ) { ... } else { ... }
  855. // ^
  856. // (state done)
  857. CARBON_PARSE_STATE(StatementIfElseBlockFinish)
  858. // Handles `return` processing.
  859. //
  860. // return ;
  861. // ^~~~~~
  862. // 1. StatementReturnFinish
  863. //
  864. // return var ...
  865. // ^~~~~~~~~~
  866. // 1. StatementReturnFinish
  867. //
  868. // return ...
  869. // ^~~~~~
  870. // 1. Expr
  871. // 2. StatementReturnFinish
  872. CARBON_PARSE_STATE(StatementReturn)
  873. // Handles `return` processing at the `;`.
  874. //
  875. // return ... ;
  876. // ^
  877. // (state done)
  878. CARBON_PARSE_STATE(StatementReturnFinish)
  879. // Handles processing of statements within a scope.
  880. //
  881. // { ... }
  882. // ^
  883. // (state done)
  884. //
  885. // { ... ... }
  886. // ^
  887. // 1. Statement
  888. // 2. StatementScopeLoop
  889. CARBON_PARSE_STATE(StatementScopeLoop)
  890. // Handles `while` processing.
  891. //
  892. // while ...
  893. // ^~~~~
  894. // 1. ParenConditionAsWhile
  895. // 2. StatementWhileConditionFinish
  896. CARBON_PARSE_STATE(StatementWhile)
  897. // Handles `while` processing between the condition and start of the code block.
  898. //
  899. // while ( ... ) ...
  900. // ^
  901. // 1. CodeBlock
  902. // 2. StatementWhileBlockFinish
  903. CARBON_PARSE_STATE(StatementWhileConditionFinish)
  904. // Handles `while` processing after the end of the code block.
  905. //
  906. // while ( ... ) { ... }
  907. // ^
  908. // (state done)
  909. CARBON_PARSE_STATE(StatementWhileBlockFinish)
  910. // Handles parsing after the declaration scope of a type.
  911. //
  912. // class/interface/constraint { ... }
  913. // ^
  914. // (state done)
  915. CARBON_PARSE_STATE_VARIANTS3(TypeDefinitionFinish, Class, Interface,
  916. NamedConstraint)
  917. // Handles processing of a type's introducer.
  918. //
  919. // class/interface/constraint ...
  920. // ^~~~~~~~~~~~~~~~~~~~~~~~~~
  921. // 1. DeclNameAndParamsAsOptional
  922. // 2. TypeAfterParamsAs(Class|Interface|NamedConstraint)
  923. CARBON_PARSE_STATE_VARIANTS3(TypeIntroducer, Class, Interface, NamedConstraint)
  924. // Handles processing of a type after its optional parameters.
  925. //
  926. // class/interface/constraint name ( ... ) {
  927. // ^
  928. // 1. DeclScopeLoop
  929. // 2. TypeDefinitionFinishAs(Class|Interface|NamedConstraint)
  930. //
  931. // class/interface/constraint name ( ... ) ;
  932. // ^
  933. // class/interface/constraint name ( ... ) ???
  934. // ^
  935. // (state done)
  936. CARBON_PARSE_STATE_VARIANTS3(TypeAfterParams, Class, Interface, NamedConstraint)
  937. // Handles the start of a `var` or `returned var`.
  938. //
  939. // var ... (variant is not Returned)
  940. // ^~~
  941. // 1. PatternAsVariable
  942. // 2. VarAfterPattern
  943. // 3. VarFinishAs(Decl|For)
  944. //
  945. // returned var ... (variant is Returned)
  946. // ^~~~~~~~~~~~
  947. // 1. PatternAsVariable
  948. // 2. VarAfterPattern
  949. // 3. VarFinishAsDecl
  950. //
  951. // returned ??? ; (variant is Returned)
  952. // ^~~~~~~~~~~~~~
  953. // (state done)
  954. CARBON_PARSE_STATE_VARIANTS3(Var, Decl, Returned, For)
  955. // Handles `var` after the pattern, either followed by an initializer or the
  956. // semicolon.
  957. //
  958. // var ... = ...
  959. // ^
  960. // var ... ??? = ...
  961. // ^~~~~
  962. // 1. Expr
  963. //
  964. // var ... ...
  965. // ^
  966. // (state done)
  967. CARBON_PARSE_STATE(VarAfterPattern)
  968. // Handles `var` parsing at the end.
  969. //
  970. // var ... ; (variant is Semicolon)
  971. // ^
  972. // var ... ??? ; (variant is Semicolon)
  973. // ^~~~~
  974. // (state done)
  975. //
  976. // var ... in (variant is For)
  977. // ^~
  978. // var ... : (variant is For, invalid)
  979. // ^
  980. // (state done)
  981. CARBON_PARSE_STATE_VARIANTS2(VarFinish, Decl, For)
  982. // Handles the start of a `let`.
  983. //
  984. // let ...
  985. // ^~~
  986. // 1. PatternAsLet
  987. // 2. LetAfterPattern
  988. // 3. LetFinish
  989. CARBON_PARSE_STATE(Let)
  990. // Handles `let` after the pattern, followed by an initializer.
  991. //
  992. // let ... = ...
  993. // ^
  994. // let ... ??? = ...
  995. // ^~~~~
  996. // 1. Expr
  997. //
  998. // let ... ??? ;
  999. // ^~~
  1000. // (state done)
  1001. CARBON_PARSE_STATE(LetAfterPattern)
  1002. // Handles `let` parsing at the end.
  1003. //
  1004. // let ... ;
  1005. // ^
  1006. // (state done)
  1007. CARBON_PARSE_STATE(LetFinish)
  1008. #undef CARBON_PARSE_STATE