parser_state.def 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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_PARSER_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. // Comments will describe a series of possible output states. States are listed
  19. // in the order they'll be executed; in other words, `1` is the top of the state
  20. // stack. The comment `(state done)` indicates that no new states are added to
  21. // the stack.
  22. //
  23. // Where state output is conditional on a lexed token, the name of
  24. // the TokenKind should be used rather than the string name in order to make it
  25. // easier to compare with code.
  26. #ifndef CARBON_PARSER_STATE
  27. #error "Must define the x-macro to use this file."
  28. #endif
  29. // Use CARBON_PARSER_STATE_VARIANTSN(State, Variant1, Variant2, ...) to generate
  30. // StateAsVariant1, StateAsVariant2, ... states.
  31. #define CARBON_PARSER_STATE_VARIANT(State, Variant) \
  32. CARBON_PARSER_STATE(State##As##Variant)
  33. #define CARBON_PARSER_STATE_VARIANTS2(State, Variant1, Variant2) \
  34. CARBON_PARSER_STATE_VARIANT(State, Variant1) \
  35. CARBON_PARSER_STATE_VARIANT(State, Variant2)
  36. #define CARBON_PARSER_STATE_VARIANTS3(State, Variant1, Variant2, Variant3) \
  37. CARBON_PARSER_STATE_VARIANT(State, Variant1) \
  38. CARBON_PARSER_STATE_VARIANTS2(State, Variant2, Variant3)
  39. // Handles the `{` of a brace expression.
  40. //
  41. // If `CloseCurlyBrace`:
  42. // 1. BraceExpressionFinishAsUnknown
  43. // Else:
  44. // 1. BraceExpressionParameterAsUnknown
  45. // 2. BraceExpressionFinishAsUnknown
  46. CARBON_PARSER_STATE(BraceExpression)
  47. // Handles a brace expression parameter. Note this will always start as unknown,
  48. // but should be known after the first valid parameter. All later inconsistent
  49. // parameters are invalid.
  50. //
  51. // If valid:
  52. // 1. DesignatorExpressionAsStruct
  53. // 2. BraceExpressionParameterAfterDesignatorAs(Type|Value|Unknown)
  54. // Else:
  55. // 1. BraceExpressionParameterFinishAs(Type|Value|Unknown)
  56. CARBON_PARSER_STATE_VARIANTS3(BraceExpressionParameter, Type, Value, Unknown)
  57. // Handles a brace expression parameter after the initial designator. This
  58. // should be at a `:` or `=`, depending on whether it's a type or value literal.
  59. //
  60. // If valid:
  61. // 1. Expression
  62. // 2. BraceExpressionParameterFinishAs(Type|Value|Unknown)
  63. // Else:
  64. // 1. BraceExpressionParameterFinishAs(Type|Value|Unknown)
  65. CARBON_PARSER_STATE_VARIANTS3(BraceExpressionParameterAfterDesignator, Type,
  66. Value, Unknown)
  67. // Handles the end of a brace expression parameter.
  68. //
  69. // If `Comma`:
  70. // 1. BraceExpressionParameterAsUnknown
  71. // Else:
  72. // (state done)
  73. CARBON_PARSER_STATE_VARIANTS3(BraceExpressionParameterFinish, Type, Value,
  74. Unknown)
  75. // Handles the `}` of a brace expression.
  76. //
  77. // Always:
  78. // (state done)
  79. CARBON_PARSER_STATE_VARIANTS3(BraceExpressionFinish, Type, Value, Unknown)
  80. // Handles a call expression `(...)`.
  81. //
  82. // If `CloseParen`:
  83. // 1. CallExpressionFinish
  84. // Else:
  85. // 1. Expression
  86. // 2. CallExpressionParameterFinish
  87. // 3. CallExpressionFinish
  88. CARBON_PARSER_STATE(CallExpression)
  89. // Handles the `,` or `)` after a call parameter.
  90. //
  91. // If `Comma`:
  92. // 1. Expression
  93. // 2. CallExpressionParameterFinish
  94. // Else:
  95. // (state done)
  96. CARBON_PARSER_STATE(CallExpressionParameterFinish)
  97. // Handles finishing the call expression.
  98. //
  99. // Always:
  100. // (state done)
  101. CARBON_PARSER_STATE(CallExpressionFinish)
  102. // Handles processing at the `{` on a typical code block.
  103. //
  104. // If `OpenCurlyBrace`:
  105. // 1. StatementScopeLoop
  106. // 2. CodeBlockFinish
  107. // Else:
  108. // 1. Statement
  109. // 2. CodeBlockFinish
  110. CARBON_PARSER_STATE(CodeBlock)
  111. // Handles processing at the `}` on a typical code block, after a statement
  112. // scope is done.
  113. //
  114. // Always:
  115. // (state done)
  116. CARBON_PARSER_STATE(CodeBlockFinish)
  117. // Handles a general declaration name and parameters, such as `Foo[...](...)`.
  118. //
  119. // If `OpenSquareBracket`:
  120. // 1. ParameterListAsDeduced
  121. // 2. DeclarationNameAndParamsAfterDeduced
  122. // If `OpenParen`:
  123. // 1. ParameterListAsRegular
  124. // Else:
  125. // (state done)
  126. CARBON_PARSER_STATE_VARIANTS2(DeclarationNameAndParams, Optional, Required)
  127. // Handles regular parameters such as `(...)` for the general declaration case.
  128. // Only used after deduced parameters.
  129. //
  130. // If `OpenParen`:
  131. // 1. ParameterListAsRegular
  132. // Else:
  133. // (state done)
  134. CARBON_PARSER_STATE(DeclarationNameAndParamsAfterDeduced)
  135. // Handles processing of a declaration scope. Things like fn, class, interface,
  136. // and so on.
  137. //
  138. // If `EndOfFile`:
  139. // (state done)
  140. // If `Class`:
  141. // 1. TypeIntroducerAsClass
  142. // 2. DeclarationScopeLoop
  143. // If `Constraint`:
  144. // 1. TypeIntroducerAsNamedConstraint
  145. // 2. DeclarationScopeLoop
  146. // If `Fn`:
  147. // 1. FunctionIntroducer
  148. // 2. DeclarationScopeLoop
  149. // If `Interface`:
  150. // 1. TypeIntroducerAsInterface
  151. // 2. DeclarationScopeLoop
  152. // If `Semi`:
  153. // 1. DeclarationScopeLoop
  154. // If `Var`:
  155. // 1. Var
  156. // 2. DeclarationScopeLoop
  157. // Else:
  158. // 1. DeclarationScopeLoop
  159. CARBON_PARSER_STATE(DeclarationScopeLoop)
  160. // Handles a designator expression, such as `.z` in `x.(y.z)`.
  161. //
  162. // Always:
  163. // (state done)
  164. CARBON_PARSER_STATE_VARIANTS2(Designator, Expression, Struct)
  165. // Handles processing of an expression.
  166. //
  167. // If `If`:
  168. // 1. Expression
  169. // 2. ExpressionThen
  170. // 3. ExpressionIfFinish
  171. // Else if valid prefix operator:
  172. // 1. Expression
  173. // 2. ExpressionLoopForPrefix
  174. // Else:
  175. // 1. ExpressionInPostfix
  176. // 2. ExpressionLoop
  177. CARBON_PARSER_STATE(Expression)
  178. // Handles the initial part of postfix expressions, such as an identifier or
  179. // literal value, then proceeds to the loop.
  180. //
  181. // If `Identifier` or literal (including type literals):
  182. // 1. ExpressionInPostfixLoop
  183. // If `OpenCurlyBrace`:
  184. // 1. BraceExpression
  185. // 2. ExpressionInPostfixLoop
  186. // If `OpenParen`:
  187. // 1. ParenExpression
  188. // 2. ExpressionInPostfixLoop
  189. // Else:
  190. // (state done)
  191. CARBON_PARSER_STATE(ExpressionInPostfix)
  192. // Handles looping through elements following the initial postfix expression,
  193. // such as designators or parenthesized parameters.
  194. //
  195. // If `Period`:
  196. // 1. DesignatorAsExpression
  197. // 2. ExpressionInPostfixLoop
  198. // If `OpenParen`:
  199. // 1. CallExpression
  200. // 2. ExpressionInPostfixLoop
  201. // Else:
  202. // (state done)
  203. CARBON_PARSER_STATE(ExpressionInPostfixLoop)
  204. // Handles processing of an expression.
  205. //
  206. // If binary operator:
  207. // 1. Expression
  208. // 2. ExpressionLoopForBinary
  209. // If postfix operator:
  210. // 1. ExpressionLoop
  211. // Else:
  212. // (state done)
  213. CARBON_PARSER_STATE(ExpressionLoop)
  214. // Completes an ExpressionLoop pass by adding an infix operator, then goes back
  215. // to ExpressionLoop.
  216. //
  217. // Always:
  218. // 1. ExpressionLoop
  219. CARBON_PARSER_STATE(ExpressionLoopForBinary)
  220. // Completes an ExpressionLoop pass by adding a prefix operator, then goes back
  221. // to ExpressionLoop.
  222. //
  223. // Always:
  224. // 1. ExpressionLoop
  225. CARBON_PARSER_STATE(ExpressionLoopForPrefix)
  226. // Completes an IfExpression.
  227. //
  228. // Always:
  229. // (state done)
  230. CARBON_PARSER_STATE(ExpressionIfFinish)
  231. // Handles the `then` token in an `if` expression.
  232. //
  233. // If `Then`:
  234. // 1. Expression
  235. // 2. ExpressionElse
  236. // Else:
  237. // (state done)
  238. CARBON_PARSER_STATE(ExpressionThen)
  239. // Handles the `else` token in an `if` expression.
  240. //
  241. // If `Else`:
  242. // 1. Expression
  243. // Else:
  244. // (state done)
  245. CARBON_PARSER_STATE(ExpressionElse)
  246. // Handles the `;` for an expression statement, which is different from most
  247. // keyword statements.
  248. //
  249. // Always:
  250. // (state done)
  251. CARBON_PARSER_STATE(ExpressionStatementFinish)
  252. // Handles a function's introducer.
  253. //
  254. // If invalid:
  255. // (state done)
  256. // Else:
  257. // 1. DeclarationNameAndParamsAsRequired
  258. // 2. FunctionAfterParameters
  259. CARBON_PARSER_STATE(FunctionIntroducer)
  260. // Handles processing of a function's syntax after `)`, primarily the
  261. // possibility a `->` return type is there. Always enqueues signature finish
  262. // handling.
  263. //
  264. // If `MinusGreater`:
  265. // 1. Expression
  266. // 2. FunctionReturnTypeFinish
  267. // 3. FunctionSignatureFinish
  268. // Else:
  269. // 1. FunctionSignatureFinish
  270. CARBON_PARSER_STATE(FunctionAfterParameters)
  271. // Finishes a function return type.
  272. //
  273. // Always:
  274. // (state done)
  275. CARBON_PARSER_STATE(FunctionReturnTypeFinish)
  276. // Finishes a function signature. If it's a declaration, the function is done;
  277. // otherwise, this also starts definition processing.
  278. //
  279. // If `Semi`:
  280. // (state done)
  281. // If `OpenCurlyBrace`:
  282. // 1. StatementScopeLoop
  283. // 2. FunctionDefinitionFinish
  284. // Else:
  285. // (state done)
  286. CARBON_PARSER_STATE(FunctionSignatureFinish)
  287. // Finishes a function definition.
  288. //
  289. // Always:
  290. // (state done)
  291. CARBON_PARSER_STATE(FunctionDefinitionFinish)
  292. // Handles `package`.
  293. //
  294. // Always:
  295. // (state done)
  296. CARBON_PARSER_STATE(Package)
  297. // Starts deduced parameter processing.
  298. //
  299. // Always:
  300. // 1. PatternAs(DeducedParameter|Parameter)
  301. // 2. ParameterFinishAs(Deduced|Regular)
  302. CARBON_PARSER_STATE_VARIANTS2(Parameter, Deduced, Regular)
  303. // Finishes deduced parameter processing, including `,`. If there are more
  304. // parameters, enqueues another parameter processing state.
  305. //
  306. // If `Comma` without the list close token:
  307. // 1. ParameterAs(Deduced|Regular)
  308. // Else:
  309. // (state done)
  310. CARBON_PARSER_STATE_VARIANTS2(ParameterFinish, Deduced, Regular)
  311. // Handles processing of a parameter list `[` or `(`.
  312. //
  313. // If the list close token:
  314. // 1. ParameterListFinishAs(Deduced|Regular)
  315. // Else:
  316. // 1. ParameterAs(Deduced|Regular)
  317. // 2. ParameterListFinishAs(Deduced|Regular)
  318. CARBON_PARSER_STATE_VARIANTS2(ParameterList, Deduced, Regular)
  319. // Handles processing of a parameter list `]` or `)`.
  320. //
  321. // Always:
  322. // (state done)
  323. CARBON_PARSER_STATE_VARIANTS2(ParameterListFinish, Deduced, Regular)
  324. // Handles the processing of a `(condition)` up through the expression.
  325. //
  326. // Always:
  327. // 1. Expression
  328. // 2. ParenConditionAs(If|While)Finish
  329. CARBON_PARSER_STATE_VARIANTS2(ParenCondition, If, While)
  330. // Finishes the processing of a `(condition)` after the expression.
  331. //
  332. // Always:
  333. // (state done)
  334. CARBON_PARSER_STATE_VARIANTS2(ParenConditionFinish, If, While)
  335. // Handles the `(` of a parenthesized expression.
  336. //
  337. // If `CloseParen`:
  338. // 1. ParenExpressionFinishAsTuple
  339. // Else:
  340. // 1. Expression
  341. // 2. ParenExpressionParameterFinishAsUnknown
  342. // 3. ParenExpressionFinish
  343. CARBON_PARSER_STATE(ParenExpression)
  344. // Handles the end of a parenthesized expression's parameter. This will start as
  345. // AsUnknown on the first parameter; if there are more, it switches to AsTuple
  346. // processing.
  347. //
  348. // If `Comma` without `CloseParen`:
  349. // 1. Expression
  350. // 2. ParenExpressionParameterFinishAsTuple
  351. // SPECIAL: Parent becomes ParenExpressionFinishAsTuple
  352. // If `Comma` with `CloseParen`:
  353. // (state done)
  354. // SPECIAL: Parent becomes ParenExpressionFinishAsTuple
  355. // Else `CloseParen`:
  356. // (state done)
  357. CARBON_PARSER_STATE_VARIANTS2(ParenExpressionParameterFinish, Unknown, Tuple)
  358. // Handles the `)` of a parenthesized expression.
  359. //
  360. // Always:
  361. // (state done)
  362. CARBON_PARSER_STATE_VARIANTS2(ParenExpressionFinish, Normal, Tuple)
  363. // Handles pattern parsing for a pattern, enqueuing type expression processing.
  364. // This covers parameter and `var` support.
  365. //
  366. // If valid:
  367. // 1. Expression
  368. // 2. PatternFinishAs(Generic|Regular)
  369. // Else:
  370. // 1. PatternFinish
  371. CARBON_PARSER_STATE_VARIANTS3(Pattern, DeducedParameter, Parameter, Variable)
  372. // Handles `addr` in a pattern.
  373. //
  374. // Always:
  375. // (state done)
  376. CARBON_PARSER_STATE(PatternAddress)
  377. // Handles `template` in a pattern.
  378. //
  379. // Always:
  380. // (state done)
  381. CARBON_PARSER_STATE(PatternTemplate)
  382. // Finishes pattern processing.
  383. //
  384. // Always:
  385. // (state done)
  386. CARBON_PARSER_STATE_VARIANTS2(PatternFinish, Generic, Regular)
  387. // Handles a single statement. While typically within a statement block, this
  388. // can also be used for error recovery where we expect a statement block and
  389. // are missing braces.
  390. //
  391. // If `Break`:
  392. // 1. StatementBreakFinish
  393. // (state done)
  394. // If `Continue`:
  395. // 1. StatementContinueFinish
  396. // (state done)
  397. // If `For`:
  398. // 1. StatementForHeader
  399. // 2. StatementForFinish
  400. // If `If`:
  401. // 1. StatementIf
  402. // If `Return`:
  403. // 1. StatementReturn
  404. // If `Var`:
  405. // 1. VarAsSemicolon
  406. // If `While`:
  407. // 1. StatementWhile
  408. // Else:
  409. // 1. Expression
  410. // 2. ExpressionStatementFinish
  411. CARBON_PARSER_STATE(Statement)
  412. // Handles `break` processing at the `;`.
  413. //
  414. // Always:
  415. // (state done)
  416. CARBON_PARSER_STATE(StatementBreakFinish)
  417. // Handles `continue` processing at the `;`.
  418. //
  419. // Always:
  420. // (state done)
  421. CARBON_PARSER_STATE(StatementContinueFinish)
  422. // Handles `for` processing of `(var`, proceeding to a pattern before
  423. // continuing.
  424. //
  425. // If no `OpenParen`:
  426. // 1. CodeBlock
  427. // If `Var`:
  428. // 1. VarAsFor
  429. // 2. StatementForHeaderIn
  430. // Else:
  431. // 1. StatementForHeaderIn
  432. CARBON_PARSER_STATE(StatementForHeader)
  433. // Handles `for` procesisng of `in`, proceeding to an expression before
  434. // continuing.
  435. //
  436. // If `In` or `Colon`:
  437. // 1. Expression
  438. // 2. StatementForHeaderFinish
  439. // Else:
  440. // 1. StatementForHeaderFinish
  441. CARBON_PARSER_STATE(StatementForHeaderIn)
  442. // Handles `for` processing of `)`, proceeding to the statement block.
  443. //
  444. // Always:
  445. // 1. CodeBlock
  446. CARBON_PARSER_STATE(StatementForHeaderFinish)
  447. // Handles `for` processing at the final `}`.
  448. //
  449. // Always:
  450. // (state done)
  451. CARBON_PARSER_STATE(StatementForFinish)
  452. // Handles `if` processing at the start.
  453. //
  454. // Always:
  455. // 1. ParenConditionAsIf
  456. // 2. StatementIfConditionFinish
  457. CARBON_PARSER_STATE(StatementIf)
  458. // Handles `if` processing between the condition and start of the first code
  459. // block.
  460. //
  461. // Always:
  462. // 1. CodeBlock
  463. // 2. StatementIfThenBlockFinish
  464. CARBON_PARSER_STATE(StatementIfConditionFinish)
  465. // Handles `if` processing after the end of the first code block, with the
  466. // optional `else`.
  467. //
  468. // If `Else` then `If`:
  469. // 1. CodeBlock
  470. // 2. StatementIfElseBlockFinish
  471. // If `Else`:
  472. // 1. StatementIf
  473. // 2. StatementIfElseBlockFinish
  474. // Else:
  475. // (state done)
  476. CARBON_PARSER_STATE(StatementIfThenBlockFinish)
  477. // Handles `if` processing after a provided `else` code block.
  478. //
  479. // Always:
  480. // (state done)
  481. CARBON_PARSER_STATE(StatementIfElseBlockFinish)
  482. // Handles `return` processing.
  483. //
  484. // If `Semi`:
  485. // 1. StatementReturnFinish
  486. // Else:
  487. // 1. Expression
  488. // 2. StatementReturnFinish
  489. CARBON_PARSER_STATE(StatementReturn)
  490. // Handles `return` processing at the `;` when there's an expression.
  491. //
  492. // Always:
  493. // (state done)
  494. CARBON_PARSER_STATE(StatementReturnFinish)
  495. // Handles processing of statements within a scope.
  496. //
  497. // If `CloseCurlyBrace`:
  498. // (state done)
  499. // Else:
  500. // 1. Statement
  501. // 2. StatementScopeLoop
  502. CARBON_PARSER_STATE(StatementScopeLoop)
  503. // Handles `while` processing.
  504. //
  505. // Always:
  506. // 1. ParenConditionAsWhile
  507. // 2. StatementWhileConditionFinish
  508. CARBON_PARSER_STATE(StatementWhile)
  509. // Handles `while` processing between the condition and start of the code block.
  510. //
  511. // Always:
  512. // 1. CodeBlock
  513. // 2. StatementWhileBlockFinish
  514. CARBON_PARSER_STATE(StatementWhileConditionFinish)
  515. // Handles `while` processing after the end of the code block.
  516. //
  517. // Always:
  518. // (state done)
  519. CARBON_PARSER_STATE(StatementWhileBlockFinish)
  520. // Handles parsing after the declaration scope of a type.
  521. //
  522. // Always:
  523. // (state done)
  524. CARBON_PARSER_STATE_VARIANTS3(TypeDefinitionFinish, Class, Interface,
  525. NamedConstraint)
  526. // Handles processing of a type's introducer.
  527. //
  528. // Always:
  529. // 1. DeclarationNameAndParamsAsOptional
  530. // 2. TypeAfterParamsAs(Class|Interface|NamedConstraint)
  531. CARBON_PARSER_STATE_VARIANTS3(TypeIntroducer, Class, Interface, NamedConstraint)
  532. // Handles processing of a type after its optional parameters.
  533. //
  534. // If `Semi`:
  535. // (state done)
  536. // If `OpenCurlyBrace`:
  537. // 1. DeclarationScopeLoop
  538. // 2. TypeDefinitionFinishAs(Class|Interface|NamedConstraint)
  539. // Else:
  540. // (state done)
  541. CARBON_PARSER_STATE_VARIANTS3(TypeAfterParams, Class, Interface,
  542. NamedConstraint)
  543. // Handles the start of a `var`.
  544. //
  545. // Always:
  546. // 1. PatternAsVariable
  547. // 2. VarAfterPattern
  548. // 3. VarFinishAs(Semicolon|For)
  549. CARBON_PARSER_STATE_VARIANTS2(Var, Semicolon, For)
  550. // Handles `var` after the pattern, either followed by an initializer or the
  551. // semicolon.
  552. //
  553. // If `Equal`:
  554. // 1. Expression
  555. // Else:
  556. // (state done)
  557. CARBON_PARSER_STATE(VarAfterPattern)
  558. // Handles `var` parsing at the end.
  559. //
  560. // Always:
  561. // (state done)
  562. CARBON_PARSER_STATE_VARIANTS2(VarFinish, Semicolon, For)
  563. #undef CARBON_PARSER_STATE