state.def 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  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. // choice ...
  339. // ^
  340. // 1. ChoiceIntroducer
  341. // 2. DeclScopeLoop
  342. //
  343. // ??? ;
  344. // ^~~~~
  345. // (state done)
  346. CARBON_PARSE_STATE(DeclScopeLoop)
  347. // Handles periods. Only does one `.<expression>` segment; the source is
  348. // responsible for handling chaining.
  349. //
  350. // The forms of this are:
  351. // - Qualified names in declarations.
  352. // - Member access expressions.
  353. // - Designated names in structs.
  354. //
  355. // Declarations and expressions have qualifiers such as `x.y`, while structs
  356. // have designators such as `.z`.
  357. //
  358. // . name
  359. // ^~~~~~
  360. // (state done)
  361. //
  362. // . ??? (??? consumed if it is a keyword)
  363. // ^
  364. // (state done)
  365. CARBON_PARSE_STATE_VARIANTS3(Period, Decl, Expr, Struct)
  366. // Handles `->name` expressions. Identical to PeriodAsExpr except for the
  367. // leading token.
  368. //
  369. // -> name
  370. // ^~~~~~~
  371. // (state done)
  372. //
  373. // -> ??? (??? consumed if it is a keyword)
  374. // ^~
  375. // (state done)
  376. CARBON_PARSE_STATE(ArrowExpr)
  377. // Handles processing of an expression.
  378. //
  379. // if ...
  380. // ^~
  381. // 1. Expr
  382. // 2. IfExprCondition
  383. // 3. IfExprFinish
  384. //
  385. // <prefix operator> ...
  386. // ^~~~~~~~~~~~~~~~~
  387. // 1. Expr
  388. // 2. ExprLoopForPrefix
  389. //
  390. // ...
  391. // ^
  392. // 1. ExprInPostfix
  393. // 2. ExprLoop
  394. CARBON_PARSE_STATE(Expr)
  395. // Handles the initial part of postfix expressions, such as an identifier or
  396. // literal value, then proceeds to the loop.
  397. //
  398. // identifier
  399. // ^~~~~~~~~~
  400. // literal
  401. // ^~~~~~~
  402. // self
  403. // ^~~~
  404. // Self
  405. // ^~~~
  406. // 1. ExprInPostfixLoop
  407. //
  408. // {
  409. // ^
  410. // 1. BraceExpr
  411. // 2. ExprInPostfixLoop
  412. //
  413. // (
  414. // ^
  415. // 1. ParenExpr
  416. // 2. ExprInPostfixLoop
  417. //
  418. // [
  419. // ^
  420. // 1. ArrayExpr
  421. // 2. ExprInPostfixLoop
  422. //
  423. // ???
  424. // ^
  425. // (state done)
  426. CARBON_PARSE_STATE(ExprInPostfix)
  427. // Handles looping through elements following the initial postfix expression,
  428. // such as designators or parenthesized parameters.
  429. //
  430. // expr . ...
  431. // ^
  432. // 1. PeriodAsExpr
  433. // 2. ExprInPostfixLoop
  434. //
  435. // expr -> ...
  436. // ^
  437. // 1. ArrowExpr
  438. // 2. ExprInPostfixLoop
  439. //
  440. // expr ( ... )
  441. // ^
  442. // 1. CallExpr
  443. // 2. ExprInPostfixLoop
  444. //
  445. // expr [ ... ]
  446. // ^
  447. // 1. IndexExprStart
  448. // 2. ExprInPostfixLoop
  449. //
  450. // ...
  451. // ^
  452. // (state done)
  453. CARBON_PARSE_STATE(ExprInPostfixLoop)
  454. // Handles processing of an expression.
  455. //
  456. // expr <infix operator> ...
  457. // ^~~~~~~~~~~~~~~~
  458. // 1. Expr
  459. // 2. ExprLoopForBinary
  460. //
  461. // expr <postfix operator>
  462. // ^~~~~~~~~~~~~~~~~~
  463. // 1. ExprLoop
  464. //
  465. // expr <short circuit operator> ...
  466. // ^~~~~~~~~~~~~~~~~~~~~~~~
  467. // 1. Expr
  468. // 2. ExprLoopForShortCircuitOperator
  469. //
  470. // expr ...
  471. // ^
  472. // (state done)
  473. CARBON_PARSE_STATE(ExprLoop)
  474. // Completes an ExprLoop pass by adding an infix operator, then goes back
  475. // to ExprLoop.
  476. //
  477. // expr <infix operator> expr ...
  478. // ^
  479. // 1. ExprLoop
  480. CARBON_PARSE_STATE(ExprLoopForInfixOperator)
  481. // Completes an ExprLoop pass by adding a prefix operator, then goes back
  482. // to ExprLoop.
  483. //
  484. // <prefix operator> expr ...
  485. // ^
  486. // 1. ExprLoop
  487. CARBON_PARSE_STATE(ExprLoopForPrefixOperator)
  488. // Completes an ExprLoop pass by adding a short circuit operator, then goes back
  489. // to ExprLoop.
  490. //
  491. // expr <short circuit operator> expr ...
  492. // ^
  493. // 1. ExprLoop
  494. CARBON_PARSE_STATE_VARIANTS2(ExprLoopForShortCircuitOperator, And, Or)
  495. // Completes the condition of an `if` expression and handles the `then` token.
  496. //
  497. // if expr then ...
  498. // ^~~~
  499. // 1. Expr
  500. // 2. IfExprFinishThen
  501. //
  502. // if expr ???
  503. // ^
  504. // (state done)
  505. CARBON_PARSE_STATE(IfExprFinishCondition)
  506. // Completes the first alternative in an `if` expression and handles the `else`
  507. // token.
  508. //
  509. // if expr then expr else ...
  510. // ^~~~
  511. // 1. Expr
  512. // 2. IfExprFinishElse
  513. //
  514. // if expr then expr ???
  515. // ^
  516. // (state done)
  517. CARBON_PARSE_STATE(IfExprFinishThen)
  518. // Completes the second alternative in an `if` expression.
  519. //
  520. // if expr then expr else expr
  521. // ^
  522. // (state done)
  523. CARBON_PARSE_STATE(IfExprFinishElse)
  524. // Completes an IfExpr.
  525. //
  526. // if expr then expr else expr
  527. // ^
  528. // if ???
  529. // ^
  530. // (state done)
  531. CARBON_PARSE_STATE(IfExprFinish)
  532. // Handles the `;` for an expression statement, which is different from most
  533. // keyword statements.
  534. //
  535. // expr ;
  536. // ^
  537. // expr ??? ;
  538. // ^~~~~
  539. // (state done)
  540. CARBON_PARSE_STATE(ExprStatementFinish)
  541. // Handles a function's introducer.
  542. //
  543. // fn ...
  544. // ^
  545. // 1. DeclNameAndParamsAsRequired
  546. // 2. FunctionAfterParams
  547. CARBON_PARSE_STATE(FunctionIntroducer)
  548. // Handles processing of a function's syntax after `)`, primarily the
  549. // possibility a `->` return type is there. Always enqueues signature finish
  550. // handling.
  551. //
  552. // fn F(...) -> ...
  553. // ^~
  554. // 1. Expr
  555. // 2. FunctionReturnTypeFinish
  556. // 3. FunctionSignatureFinish
  557. //
  558. // fn F(...) ...
  559. // ^
  560. // 1. FunctionSignatureFinish
  561. CARBON_PARSE_STATE(FunctionAfterParams)
  562. // Finishes a function return type.
  563. //
  564. // fn F(...) -> expr ...
  565. // ^
  566. // (state done)
  567. CARBON_PARSE_STATE(FunctionReturnTypeFinish)
  568. // Finishes a function signature. If it's a declaration, the function is done;
  569. // otherwise, this also starts definition processing.
  570. //
  571. // fn ... ;
  572. // ^
  573. // (state done)
  574. //
  575. // fn ... {
  576. // ^
  577. // 1. StatementScopeLoop
  578. // 2. FunctionDefinitionFinish
  579. //
  580. // fn ... ??? ;
  581. // ^~~~~
  582. // (state done)
  583. CARBON_PARSE_STATE(FunctionSignatureFinish)
  584. // Finishes a function definition.
  585. //
  586. // fn ... }
  587. // ^
  588. // fn ... ;
  589. // ^
  590. // (state done)
  591. CARBON_PARSE_STATE(FunctionDefinitionFinish)
  592. // Handles `import`.
  593. //
  594. // import pkgname [library "libname"] ;
  595. // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  596. // import ??? ;
  597. // ^~~~~~~~~~~~
  598. // (state done)
  599. CARBON_PARSE_STATE(Import)
  600. // Handles `library` in directive form.
  601. //
  602. // Always:
  603. // (state done)
  604. CARBON_PARSE_STATE(Library)
  605. // Handles `namespace`.
  606. //
  607. // namespace ...
  608. // ^
  609. // 1. DeclNameAndParamsAsNone
  610. // 2. NamespaceFinish
  611. CARBON_PARSE_STATE(Namespace)
  612. // Handles `namespace` after the name.
  613. //
  614. // namespace ... ;
  615. // ^
  616. // namespace ... ??? ;
  617. // ^~~~~
  618. // (state done)
  619. CARBON_PARSE_STATE(NamespaceFinish)
  620. // Handles `package`.
  621. //
  622. // package pkgname [library "libname"] [api|impl] ;
  623. // ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  624. // package ??? ;
  625. // ^~~~~~~~~~~~~
  626. // (state done)
  627. CARBON_PARSE_STATE(Package)
  628. // Starts parsing a pattern in a comma-separated list. The variants mark
  629. // whether it is part of an implicit parameter list or a tuple pattern.
  630. //
  631. // ...
  632. // ^
  633. // 1. BindingPattern
  634. // 2. PatternListElementFinishAs(Implicit|Tuple)
  635. CARBON_PARSE_STATE_VARIANTS2(PatternListElement, Implicit, Tuple)
  636. // Finishes parsing a pattern in a comma-separated list, including the
  637. // optional trailing `,`. If there are more patterns, enqueues another
  638. // pattern parsing state.
  639. //
  640. // ... , ) (variant is Tuple)
  641. // ^
  642. // (state done)
  643. //
  644. // ... , ] (variant is Implicit)
  645. // ^
  646. // (state done)
  647. //
  648. // ... , ...
  649. // ^
  650. // 1. PatternListElementAs(Implicit|Tuple)
  651. //
  652. // ...
  653. // ^
  654. // (state done)
  655. CARBON_PARSE_STATE_VARIANTS2(PatternListElementFinish, Implicit, Tuple)
  656. // Handles processing of a tuple pattern (parentheses) or implicit parameter
  657. // list (square brackets).
  658. //
  659. // ( ) (variant is Tuple)
  660. // ^
  661. // [ ] (variant is Implicit)
  662. // ^
  663. // 1. PatternListFinishAs(Tuple|Implicit)
  664. //
  665. // ( ... ) (variant is Tuple)
  666. // ^
  667. // [ ... ] (variant is Implicit)
  668. // ^
  669. // 1. PatternListElementAs(Tuple|Implicit)
  670. // 2. PatternListFinishAs(Tuple|Implicit)
  671. CARBON_PARSE_STATE_VARIANTS2(PatternList, Implicit, Tuple)
  672. // Handles processing of a parameter list `]` or `)`.
  673. //
  674. // ( ... ) (variant is Tuple)
  675. // ^
  676. // [ ... ] (variant is Implicit)
  677. // ^
  678. // (state done)
  679. CARBON_PARSE_STATE_VARIANTS2(PatternListFinish, Implicit, Tuple)
  680. // Handles the processing of a `(condition)` up through the expression.
  681. //
  682. // if/while { (invalid)
  683. // ^
  684. // 1. ParenConditionAs(If|While)Finish
  685. //
  686. // if/while ( ... )
  687. // ^
  688. // if/while ???
  689. // ^
  690. // 1. Expr
  691. // 2. ParenConditionAs(If|While)Finish
  692. CARBON_PARSE_STATE_VARIANTS2(ParenCondition, If, While)
  693. // Finishes the processing of a `(condition)` after the expression.
  694. //
  695. // if/while ( expr )
  696. // ^
  697. // if/while {
  698. // ^
  699. // if/while ??? {
  700. // ^
  701. // (state done)
  702. CARBON_PARSE_STATE_VARIANTS2(ParenConditionFinish, If, While)
  703. // Handles the `(` of a parenthesized single expression
  704. //
  705. // ( )
  706. // ^
  707. // 1. TupleLiteralFinish
  708. //
  709. // ( ... )
  710. // ^
  711. // 1. Expr
  712. // 2. ExprAfterOpenParenFinish
  713. // 3. ParenExprFinish (SPECIAL: may be replaced)
  714. CARBON_PARSE_STATE(ParenExpr)
  715. // Handles the `)` of a tuple literal.
  716. //
  717. // ( ... )
  718. // ^
  719. // (state done)
  720. CARBON_PARSE_STATE(TupleLiteralFinish)
  721. // Handles the end of an expression following an open parenthesis.
  722. //
  723. // ( ... , )
  724. // ^
  725. // (state done)
  726. // SPECIAL: parent becomes TupleLiteralFinish
  727. //
  728. // ( ... , ... )
  729. // ^
  730. // 1. Expression
  731. // 2. TupleLiteralElementFinish
  732. // SPECIAL: parent becomes TupleLiteralFinish
  733. //
  734. // ( ... )
  735. // ^
  736. // (state done)
  737. CARBON_PARSE_STATE(ExprAfterOpenParenFinish)
  738. // Handles the end of an expression that is known to be an element of a tuple
  739. // literal expression.
  740. //
  741. // ( ... , )
  742. // ^
  743. // (state done)
  744. //
  745. // ( ... , ... )
  746. // ^
  747. // 1. Expression
  748. // 2. TupleLiteralElementFinish
  749. //
  750. // ( ... )
  751. // ^
  752. // (state done)
  753. CARBON_PARSE_STATE(TupleLiteralElementFinish)
  754. // Handles the `)` of a parenthesized single expression.
  755. //
  756. // ( ... )
  757. // ^
  758. // (state done)
  759. CARBON_PARSE_STATE(ParenExprFinish)
  760. // Handles processing of a pattern.
  761. //
  762. // ( ... )
  763. // ^
  764. // 1. PatternListAsTuple
  765. //
  766. // ...
  767. // ^
  768. // 1. BindingPattern
  769. CARBON_PARSE_STATE(Pattern)
  770. // Handles the initial part of a binding pattern, enqueuing type expression
  771. // processing.
  772. //
  773. // template (variant is not Variable)
  774. // ^~~~~~~~
  775. // 4. BindingPatternTemplate
  776. //
  777. // THEN
  778. //
  779. // addr (variant is not Variable)
  780. // ^~~~
  781. // 3. BindingPatternAddr
  782. //
  783. // THEN
  784. //
  785. // name: ...
  786. // ^~~~~
  787. // self: ...
  788. // ^~~~~
  789. // 1. Expr
  790. // 2. BindingPatternFinishAsRegular
  791. //
  792. // name:! ...
  793. // ^~~~~~
  794. // self:! ...
  795. // ^~~~~~
  796. // 1. Expr
  797. // 2. BindingPatternFinishAsGeneric
  798. //
  799. // ???
  800. // ^
  801. // 1. BindingPatternFinishAsRegular
  802. CARBON_PARSE_STATE(BindingPattern)
  803. // Handles `addr` in a binding pattern.
  804. //
  805. // addr name: type
  806. // ^
  807. // (state done)
  808. CARBON_PARSE_STATE(BindingPatternAddr)
  809. // Handles `template` in a binding pattern.
  810. //
  811. // template name:! type
  812. // ^
  813. // (state done)
  814. CARBON_PARSE_STATE(BindingPatternTemplate)
  815. // Finishes binding pattern processing.
  816. //
  817. // name: type
  818. // ^
  819. // (state done)
  820. CARBON_PARSE_STATE_VARIANTS2(BindingPatternFinish, Generic, Regular)
  821. // Handles a single statement. While typically within a statement block, this
  822. // can also be used for error recovery where we expect a statement block and
  823. // are missing braces.
  824. //
  825. // break ...
  826. // ^~~~~
  827. // 1. StatementBreakFinish
  828. //
  829. // continue ...
  830. // ^~~~~~~~
  831. // 1. StatementContinueFinish
  832. //
  833. // for ...
  834. // ^~~
  835. // 1. StatementForHeader
  836. // 2. StatementForFinish
  837. //
  838. // if ...
  839. // ^
  840. // 1. StatementIf
  841. //
  842. // return ...
  843. // ^
  844. // 1. StatementReturn
  845. //
  846. // var ...
  847. // ^
  848. // 1. VarAsDecl
  849. //
  850. // returned ...
  851. // ^
  852. // 1. VarAsReturned
  853. //
  854. // while ...
  855. // ^
  856. // 1. StatementWhile
  857. //
  858. // ...
  859. // ^
  860. // 1. Expr
  861. // 2. ExprStatementFinish
  862. CARBON_PARSE_STATE(Statement)
  863. // Handles `break` processing at the `;`.
  864. //
  865. // break ;
  866. // ^
  867. // (state done)
  868. CARBON_PARSE_STATE(StatementBreakFinish)
  869. // Handles `continue` processing at the `;`.
  870. //
  871. // continue ;
  872. // ^
  873. // (state done)
  874. CARBON_PARSE_STATE(StatementContinueFinish)
  875. // Handles `for` processing of `(var`, proceeding to a binding pattern before
  876. // continuing.
  877. //
  878. // for ( var ... )
  879. // ^
  880. // 1. VarAsFor
  881. // 2. StatementForHeaderIn
  882. //
  883. // for ( ???
  884. // ^
  885. // for ( ??? in ...
  886. // ^~~~~~~~
  887. // for ??? in ...
  888. // ^~~~~~
  889. // for ???
  890. // ^
  891. // 1. StatementForHeaderIn
  892. CARBON_PARSE_STATE(StatementForHeader)
  893. // Handles `for` processing of `in`, proceeding to an expression before
  894. // continuing.
  895. //
  896. // for ( ... in ... )
  897. // ^
  898. // 1. Expr
  899. // 2. StatementForHeaderFinish
  900. CARBON_PARSE_STATE(StatementForHeaderIn)
  901. // Handles `for` processing of `)`, proceeding to the statement block.
  902. //
  903. // for ( ... ) ...
  904. // ^
  905. // 1. CodeBlock
  906. CARBON_PARSE_STATE(StatementForHeaderFinish)
  907. // Handles `for` processing after the final `}`.
  908. //
  909. // for ( ... ) { ... }
  910. // ^
  911. // (state done)
  912. CARBON_PARSE_STATE(StatementForFinish)
  913. // Handles `if` processing at the start.
  914. //
  915. // if ...
  916. // ^~
  917. // 1. ParenConditionAsIf
  918. // 2. StatementIfConditionFinish
  919. CARBON_PARSE_STATE(StatementIf)
  920. // Handles `if` processing between the condition and start of the first code
  921. // block.
  922. //
  923. // if ( ... ) ...
  924. // ^
  925. // 1. CodeBlock
  926. // 2. StatementIfThenBlockFinish
  927. CARBON_PARSE_STATE(StatementIfConditionFinish)
  928. // Handles `if` processing after the end of the first code block, with the
  929. // optional `else`.
  930. //
  931. // if ( ... ) { ... } else if ...
  932. // ^~~~
  933. // 1. StatementIf
  934. // 2. StatementIfElseBlockFinish
  935. //
  936. // if ( ... ) { ... } else ...
  937. // ^~~~
  938. // 1. CodeBlock
  939. // 2. StatementIfElseBlockFinish
  940. //
  941. // if ( ... ) { ... } ...
  942. // (state done)
  943. CARBON_PARSE_STATE(StatementIfThenBlockFinish)
  944. // Handles `if` processing after a provided `else` code block.
  945. //
  946. // if ( ... ) { ... } else { ... }
  947. // ^
  948. // (state done)
  949. CARBON_PARSE_STATE(StatementIfElseBlockFinish)
  950. // Handles `return` processing.
  951. //
  952. // return ;
  953. // ^~~~~~
  954. // 1. StatementReturnFinish
  955. //
  956. // return var ...
  957. // ^~~~~~~~~~
  958. // 1. StatementReturnFinish
  959. //
  960. // return ...
  961. // ^~~~~~
  962. // 1. Expr
  963. // 2. StatementReturnFinish
  964. CARBON_PARSE_STATE(StatementReturn)
  965. // Handles `return` processing at the `;`.
  966. //
  967. // return ... ;
  968. // ^
  969. // (state done)
  970. CARBON_PARSE_STATE(StatementReturnFinish)
  971. // Handles processing of statements within a scope.
  972. //
  973. // { ... }
  974. // ^
  975. // (state done)
  976. //
  977. // { ... ... }
  978. // ^
  979. // 1. Statement
  980. // 2. StatementScopeLoop
  981. CARBON_PARSE_STATE(StatementScopeLoop)
  982. // Handles `while` processing.
  983. //
  984. // while ...
  985. // ^~~~~
  986. // 1. ParenConditionAsWhile
  987. // 2. StatementWhileConditionFinish
  988. CARBON_PARSE_STATE(StatementWhile)
  989. // Handles `while` processing between the condition and start of the code block.
  990. //
  991. // while ( ... ) ...
  992. // ^
  993. // 1. CodeBlock
  994. // 2. StatementWhileBlockFinish
  995. CARBON_PARSE_STATE(StatementWhileConditionFinish)
  996. // Handles `while` processing after the end of the code block.
  997. //
  998. // while ( ... ) { ... }
  999. // ^
  1000. // (state done)
  1001. CARBON_PARSE_STATE(StatementWhileBlockFinish)
  1002. // Handles parsing after the declaration scope of a type.
  1003. //
  1004. // class/impl/interface/constraint ... { ... }
  1005. // ^
  1006. // (state done)
  1007. CARBON_PARSE_STATE_VARIANTS4(DeclDefinitionFinish, Class, Impl, Interface,
  1008. NamedConstraint)
  1009. // Handles processing of a type after its introducer.
  1010. //
  1011. // class/interface/constraint ...
  1012. // ^
  1013. // 1. DeclNameAndParamsAsOptional
  1014. // 2. DeclOrDefinitionAs(Class|Interface|NamedConstraint)
  1015. CARBON_PARSE_STATE_VARIANTS3(TypeAfterIntroducer, Class, Interface,
  1016. NamedConstraint)
  1017. // Handles processing of a type after its optional parameters.
  1018. //
  1019. // class/impl/interface/constraint name ( ... ) {
  1020. // ^
  1021. // 1. DeclScopeLoop
  1022. // 2. DeclDefinitionFinishAs(Class|Impl|Interface|NamedConstraint)
  1023. //
  1024. // class/impl/interface/constraint name ( ... ) ;
  1025. // ^
  1026. // class/impl/interface/constraint name ( ... ) ???
  1027. // ^
  1028. // (state done)
  1029. CARBON_PARSE_STATE_VARIANTS4(DeclOrDefinition, Class, Impl, Interface, NamedConstraint)
  1030. // Handles processing of a completed `base: B` declaration.
  1031. //
  1032. // base: B ;
  1033. // ^
  1034. // base: B ??? ;
  1035. // ^
  1036. // (state done)
  1037. CARBON_PARSE_STATE(BaseDecl)
  1038. // Handles processing of an `impl...as` declaration after the introducer.
  1039. //
  1040. // impl forall [ ...
  1041. // ^~~~~~
  1042. // 1. PatternListAsImplicit
  1043. // 2. ImplAfterForall
  1044. // 3. DeclOrDefinitionAsImpl
  1045. // impl as ...
  1046. // ^~
  1047. // 1. Expr
  1048. // 2. DeclOrDefinitionAsImpl
  1049. // impl type_expression as ...
  1050. // ^
  1051. // 1. Expr
  1052. // 2. ImplBeforeAs
  1053. // 3. DeclOrDefinitionAsImpl
  1054. CARBON_PARSE_STATE(ImplAfterIntroducer)
  1055. // Handles processing of an `impl forall` declaration after the implicit
  1056. // parameter list.
  1057. //
  1058. // impl forall [ ... ] as ...
  1059. // ^~
  1060. // 1. Expr
  1061. // impl forall [ ... ] type_expression as ...
  1062. // ^
  1063. // 1. Expr
  1064. // 2. ImplBeforeAs
  1065. CARBON_PARSE_STATE(ImplAfterForall)
  1066. // Handles processing of the `as` in an `impl` declaration after the type
  1067. // expression.
  1068. //
  1069. // impl TypeExpression as ...
  1070. // ^~
  1071. // 1. Expr
  1072. CARBON_PARSE_STATE(ImplBeforeAs)
  1073. // Handles the start of a `var` or `returned var`.
  1074. //
  1075. // var ... (variant is Decl)
  1076. // ^
  1077. // 1. Pattern
  1078. // 2. VarAfterPattern
  1079. // 3. VarFinishAsDecl
  1080. //
  1081. // var ... (variant is For)
  1082. // ^
  1083. // 1. Pattern
  1084. // 2. VarFinishAsFor
  1085. //
  1086. // returned var ... (variant is Returned)
  1087. // ^~~~~~~~~~~~
  1088. // 1. Pattern
  1089. // 2. VarAfterPattern
  1090. // 3. VarFinishAsDecl
  1091. //
  1092. // returned ??? ; (variant is Returned)
  1093. // ^~~~~~~~~~~~~~
  1094. // (state done)
  1095. CARBON_PARSE_STATE_VARIANTS3(Var, Decl, Returned, For)
  1096. // Handles `var` after the pattern, either followed by an initializer or the
  1097. // semicolon.
  1098. //
  1099. // var ... = ...
  1100. // ^
  1101. // var ... ??? = ...
  1102. // ^~~~~
  1103. // 1. Expr
  1104. // 2. VarInitializer
  1105. //
  1106. // var ... ...
  1107. // ^
  1108. // (state done)
  1109. CARBON_PARSE_STATE(VarAfterPattern)
  1110. // var ... = ... ;
  1111. // ^
  1112. //
  1113. // (state done)
  1114. CARBON_PARSE_STATE(VarInitializer)
  1115. // Handles `var` parsing at the end.
  1116. //
  1117. // var ... ; (variant is Semicolon)
  1118. // ^
  1119. // var ... ??? ; (variant is Semicolon)
  1120. // ^~~~~
  1121. // (state done)
  1122. //
  1123. // var ... in (variant is For)
  1124. // ^~
  1125. // var ... : (variant is For, invalid)
  1126. // ^
  1127. // (state done)
  1128. CARBON_PARSE_STATE_VARIANTS2(VarFinish, Decl, For)
  1129. // Handles the start of a `let`.
  1130. //
  1131. // let ...
  1132. // ^
  1133. // 1. Pattern
  1134. // 2. LetAfterPattern
  1135. // 3. LetFinish
  1136. CARBON_PARSE_STATE(Let)
  1137. // Handles `let` after the pattern, followed by an initializer.
  1138. //
  1139. // let ... = ...
  1140. // ^
  1141. // let ... ??? = ...
  1142. // ^~~~~
  1143. // 1. Expr
  1144. //
  1145. // let ... ??? ;
  1146. // ^~~
  1147. // (state done)
  1148. CARBON_PARSE_STATE(LetAfterPattern)
  1149. // Handles `let` parsing at the end.
  1150. //
  1151. // let ... ;
  1152. // ^
  1153. // (state done)
  1154. CARBON_PARSE_STATE(LetFinish)
  1155. // Handles a choice's introducer.
  1156. //
  1157. // choice ...
  1158. // ^~~~~~
  1159. // 1. DeclNameAndParamsAsOptional
  1160. // 2. ChoiceAlternativeListStart
  1161. // 3. ChoiceDefinitionFinish
  1162. CARBON_PARSE_STATE(ChoiceIntroducer)
  1163. // Handles processing of a choice after its optional parameters.
  1164. //
  1165. // choice name ... {}
  1166. // ^
  1167. // (state done)
  1168. //
  1169. // choice name ... { ... }
  1170. // ^
  1171. // 1. ChoiceAlternative
  1172. //
  1173. // choice name ... ???
  1174. // ^
  1175. // (state done)
  1176. CARBON_PARSE_STATE(ChoiceDefinitionStart)
  1177. // Starts alternative parsing.
  1178. //
  1179. // name( ... )
  1180. // ^~~~
  1181. // 1. ParamListAsRegular
  1182. // 2. ChoiceAlternativeFinish
  1183. // name ...
  1184. // ^~~~
  1185. // 1. ChoiceAlternativeFinish
  1186. CARBON_PARSE_STATE(ChoiceAlternative)
  1187. // Finishes parsing a choice's alternative, including the optional trailing `,`. If there
  1188. // are more alternatives, enqueues another alternative parsing state.
  1189. //
  1190. // ... , }
  1191. // ^
  1192. // (state done)
  1193. //
  1194. // ... , ...
  1195. // ^
  1196. // 1. ChoiceAlternative
  1197. //
  1198. // ...
  1199. // ^
  1200. // (state done)
  1201. CARBON_PARSE_STATE(ChoiceAlternativeFinish)
  1202. // Finishes a choice definition.
  1203. //
  1204. // choice ... }
  1205. // ^
  1206. // (state done)
  1207. CARBON_PARSE_STATE(ChoiceDefinitionFinish)
  1208. #undef CARBON_PARSE_STATE