state.def 27 KB

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