parser_state.def 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. IfExpressionCondition
  170. // 3. IfExpressionFinish
  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 the condition of an `if` expression and handles the `then` token.
  227. //
  228. // If `Then`:
  229. // 1. Expression
  230. // 2. IfExpressionFinishThen
  231. // Else:
  232. // (state done)
  233. CARBON_PARSER_STATE(IfExpressionFinishCondition)
  234. // Completes the first alternative in an `if` expression and handles the `else`
  235. // token.
  236. //
  237. // If `Else`:
  238. // 1. Expression
  239. // 2. IfExpressionFinishElse
  240. // Else:
  241. // (state done)
  242. CARBON_PARSER_STATE(IfExpressionFinishThen)
  243. // Completes the second alternative in an `if` expression.
  244. //
  245. // Always:
  246. // (state done)
  247. CARBON_PARSER_STATE(IfExpressionFinishElse)
  248. // Completes an IfExpression.
  249. //
  250. // Always:
  251. // (state done)
  252. CARBON_PARSER_STATE(IfExpressionFinish)
  253. // Handles the `;` for an expression statement, which is different from most
  254. // keyword statements.
  255. //
  256. // Always:
  257. // (state done)
  258. CARBON_PARSER_STATE(ExpressionStatementFinish)
  259. // Handles a function's introducer.
  260. //
  261. // If invalid:
  262. // (state done)
  263. // Else:
  264. // 1. DeclarationNameAndParamsAsRequired
  265. // 2. FunctionAfterParameters
  266. CARBON_PARSER_STATE(FunctionIntroducer)
  267. // Handles processing of a function's syntax after `)`, primarily the
  268. // possibility a `->` return type is there. Always enqueues signature finish
  269. // handling.
  270. //
  271. // If `MinusGreater`:
  272. // 1. Expression
  273. // 2. FunctionReturnTypeFinish
  274. // 3. FunctionSignatureFinish
  275. // Else:
  276. // 1. FunctionSignatureFinish
  277. CARBON_PARSER_STATE(FunctionAfterParameters)
  278. // Finishes a function return type.
  279. //
  280. // Always:
  281. // (state done)
  282. CARBON_PARSER_STATE(FunctionReturnTypeFinish)
  283. // Finishes a function signature. If it's a declaration, the function is done;
  284. // otherwise, this also starts definition processing.
  285. //
  286. // If `Semi`:
  287. // (state done)
  288. // If `OpenCurlyBrace`:
  289. // 1. StatementScopeLoop
  290. // 2. FunctionDefinitionFinish
  291. // Else:
  292. // (state done)
  293. CARBON_PARSER_STATE(FunctionSignatureFinish)
  294. // Finishes a function definition.
  295. //
  296. // Always:
  297. // (state done)
  298. CARBON_PARSER_STATE(FunctionDefinitionFinish)
  299. // Handles `package`.
  300. //
  301. // Always:
  302. // (state done)
  303. CARBON_PARSER_STATE(Package)
  304. // Starts deduced parameter processing.
  305. //
  306. // Always:
  307. // 1. PatternAs(DeducedParameter|Parameter)
  308. // 2. ParameterFinishAs(Deduced|Regular)
  309. CARBON_PARSER_STATE_VARIANTS2(Parameter, Deduced, Regular)
  310. // Finishes deduced parameter processing, including `,`. If there are more
  311. // parameters, enqueues another parameter processing state.
  312. //
  313. // If `Comma` without the list close token:
  314. // 1. ParameterAs(Deduced|Regular)
  315. // Else:
  316. // (state done)
  317. CARBON_PARSER_STATE_VARIANTS2(ParameterFinish, Deduced, Regular)
  318. // Handles processing of a parameter list `[` or `(`.
  319. //
  320. // If the list close token:
  321. // 1. ParameterListFinishAs(Deduced|Regular)
  322. // Else:
  323. // 1. ParameterAs(Deduced|Regular)
  324. // 2. ParameterListFinishAs(Deduced|Regular)
  325. CARBON_PARSER_STATE_VARIANTS2(ParameterList, Deduced, Regular)
  326. // Handles processing of a parameter list `]` or `)`.
  327. //
  328. // Always:
  329. // (state done)
  330. CARBON_PARSER_STATE_VARIANTS2(ParameterListFinish, Deduced, Regular)
  331. // Handles the processing of a `(condition)` up through the expression.
  332. //
  333. // Always:
  334. // 1. Expression
  335. // 2. ParenConditionAs(If|While)Finish
  336. CARBON_PARSER_STATE_VARIANTS2(ParenCondition, If, While)
  337. // Finishes the processing of a `(condition)` after the expression.
  338. //
  339. // Always:
  340. // (state done)
  341. CARBON_PARSER_STATE_VARIANTS2(ParenConditionFinish, If, While)
  342. // Handles the `(` of a parenthesized expression.
  343. //
  344. // If `CloseParen`:
  345. // 1. ParenExpressionFinishAsTuple
  346. // Else:
  347. // 1. Expression
  348. // 2. ParenExpressionParameterFinishAsUnknown
  349. // 3. ParenExpressionFinish
  350. CARBON_PARSER_STATE(ParenExpression)
  351. // Handles the end of a parenthesized expression's parameter. This will start as
  352. // AsUnknown on the first parameter; if there are more, it switches to AsTuple
  353. // processing.
  354. //
  355. // If `Comma` without `CloseParen`:
  356. // 1. Expression
  357. // 2. ParenExpressionParameterFinishAsTuple
  358. // SPECIAL: Parent becomes ParenExpressionFinishAsTuple
  359. // If `Comma` with `CloseParen`:
  360. // (state done)
  361. // SPECIAL: Parent becomes ParenExpressionFinishAsTuple
  362. // Else `CloseParen`:
  363. // (state done)
  364. CARBON_PARSER_STATE_VARIANTS2(ParenExpressionParameterFinish, Unknown, Tuple)
  365. // Handles the `)` of a parenthesized expression.
  366. //
  367. // Always:
  368. // (state done)
  369. CARBON_PARSER_STATE_VARIANTS2(ParenExpressionFinish, Normal, Tuple)
  370. // Handles pattern parsing for a pattern, enqueuing type expression processing.
  371. // This covers parameter and `var` support.
  372. //
  373. // If valid:
  374. // 1. Expression
  375. // 2. PatternFinishAs(Generic|Regular)
  376. // Else:
  377. // 1. PatternFinish
  378. CARBON_PARSER_STATE_VARIANTS3(Pattern, DeducedParameter, Parameter, Variable)
  379. // Handles `addr` in a pattern.
  380. //
  381. // Always:
  382. // (state done)
  383. CARBON_PARSER_STATE(PatternAddress)
  384. // Handles `template` in a pattern.
  385. //
  386. // Always:
  387. // (state done)
  388. CARBON_PARSER_STATE(PatternTemplate)
  389. // Finishes pattern processing.
  390. //
  391. // Always:
  392. // (state done)
  393. CARBON_PARSER_STATE_VARIANTS2(PatternFinish, Generic, Regular)
  394. // Handles a single statement. While typically within a statement block, this
  395. // can also be used for error recovery where we expect a statement block and
  396. // are missing braces.
  397. //
  398. // If `Break`:
  399. // 1. StatementBreakFinish
  400. // (state done)
  401. // If `Continue`:
  402. // 1. StatementContinueFinish
  403. // (state done)
  404. // If `For`:
  405. // 1. StatementForHeader
  406. // 2. StatementForFinish
  407. // If `If`:
  408. // 1. StatementIf
  409. // If `Return`:
  410. // 1. StatementReturn
  411. // If `Var`:
  412. // 1. VarAsSemicolon
  413. // If `While`:
  414. // 1. StatementWhile
  415. // Else:
  416. // 1. Expression
  417. // 2. ExpressionStatementFinish
  418. CARBON_PARSER_STATE(Statement)
  419. // Handles `break` processing at the `;`.
  420. //
  421. // Always:
  422. // (state done)
  423. CARBON_PARSER_STATE(StatementBreakFinish)
  424. // Handles `continue` processing at the `;`.
  425. //
  426. // Always:
  427. // (state done)
  428. CARBON_PARSER_STATE(StatementContinueFinish)
  429. // Handles `for` processing of `(var`, proceeding to a pattern before
  430. // continuing.
  431. //
  432. // If no `OpenParen`:
  433. // 1. CodeBlock
  434. // If `Var`:
  435. // 1. VarAsFor
  436. // 2. StatementForHeaderIn
  437. // Else:
  438. // 1. StatementForHeaderIn
  439. CARBON_PARSER_STATE(StatementForHeader)
  440. // Handles `for` procesisng of `in`, proceeding to an expression before
  441. // continuing.
  442. //
  443. // If `In` or `Colon`:
  444. // 1. Expression
  445. // 2. StatementForHeaderFinish
  446. // Else:
  447. // 1. StatementForHeaderFinish
  448. CARBON_PARSER_STATE(StatementForHeaderIn)
  449. // Handles `for` processing of `)`, proceeding to the statement block.
  450. //
  451. // Always:
  452. // 1. CodeBlock
  453. CARBON_PARSER_STATE(StatementForHeaderFinish)
  454. // Handles `for` processing at the final `}`.
  455. //
  456. // Always:
  457. // (state done)
  458. CARBON_PARSER_STATE(StatementForFinish)
  459. // Handles `if` processing at the start.
  460. //
  461. // Always:
  462. // 1. ParenConditionAsIf
  463. // 2. StatementIfConditionFinish
  464. CARBON_PARSER_STATE(StatementIf)
  465. // Handles `if` processing between the condition and start of the first code
  466. // block.
  467. //
  468. // Always:
  469. // 1. CodeBlock
  470. // 2. StatementIfThenBlockFinish
  471. CARBON_PARSER_STATE(StatementIfConditionFinish)
  472. // Handles `if` processing after the end of the first code block, with the
  473. // optional `else`.
  474. //
  475. // If `Else` then `If`:
  476. // 1. CodeBlock
  477. // 2. StatementIfElseBlockFinish
  478. // If `Else`:
  479. // 1. StatementIf
  480. // 2. StatementIfElseBlockFinish
  481. // Else:
  482. // (state done)
  483. CARBON_PARSER_STATE(StatementIfThenBlockFinish)
  484. // Handles `if` processing after a provided `else` code block.
  485. //
  486. // Always:
  487. // (state done)
  488. CARBON_PARSER_STATE(StatementIfElseBlockFinish)
  489. // Handles `return` processing.
  490. //
  491. // If `Semi`:
  492. // 1. StatementReturnFinish
  493. // Else:
  494. // 1. Expression
  495. // 2. StatementReturnFinish
  496. CARBON_PARSER_STATE(StatementReturn)
  497. // Handles `return` processing at the `;` when there's an expression.
  498. //
  499. // Always:
  500. // (state done)
  501. CARBON_PARSER_STATE(StatementReturnFinish)
  502. // Handles processing of statements within a scope.
  503. //
  504. // If `CloseCurlyBrace`:
  505. // (state done)
  506. // Else:
  507. // 1. Statement
  508. // 2. StatementScopeLoop
  509. CARBON_PARSER_STATE(StatementScopeLoop)
  510. // Handles `while` processing.
  511. //
  512. // Always:
  513. // 1. ParenConditionAsWhile
  514. // 2. StatementWhileConditionFinish
  515. CARBON_PARSER_STATE(StatementWhile)
  516. // Handles `while` processing between the condition and start of the code block.
  517. //
  518. // Always:
  519. // 1. CodeBlock
  520. // 2. StatementWhileBlockFinish
  521. CARBON_PARSER_STATE(StatementWhileConditionFinish)
  522. // Handles `while` processing after the end of the code block.
  523. //
  524. // Always:
  525. // (state done)
  526. CARBON_PARSER_STATE(StatementWhileBlockFinish)
  527. // Handles parsing after the declaration scope of a type.
  528. //
  529. // Always:
  530. // (state done)
  531. CARBON_PARSER_STATE_VARIANTS3(TypeDefinitionFinish, Class, Interface,
  532. NamedConstraint)
  533. // Handles processing of a type's introducer.
  534. //
  535. // Always:
  536. // 1. DeclarationNameAndParamsAsOptional
  537. // 2. TypeAfterParamsAs(Class|Interface|NamedConstraint)
  538. CARBON_PARSER_STATE_VARIANTS3(TypeIntroducer, Class, Interface, NamedConstraint)
  539. // Handles processing of a type after its optional parameters.
  540. //
  541. // If `Semi`:
  542. // (state done)
  543. // If `OpenCurlyBrace`:
  544. // 1. DeclarationScopeLoop
  545. // 2. TypeDefinitionFinishAs(Class|Interface|NamedConstraint)
  546. // Else:
  547. // (state done)
  548. CARBON_PARSER_STATE_VARIANTS3(TypeAfterParams, Class, Interface,
  549. NamedConstraint)
  550. // Handles the start of a `var`.
  551. //
  552. // Always:
  553. // 1. PatternAsVariable
  554. // 2. VarAfterPattern
  555. // 3. VarFinishAs(Semicolon|For)
  556. CARBON_PARSER_STATE_VARIANTS2(Var, Semicolon, For)
  557. // Handles `var` after the pattern, either followed by an initializer or the
  558. // semicolon.
  559. //
  560. // If `Equal`:
  561. // 1. Expression
  562. // Else:
  563. // (state done)
  564. CARBON_PARSER_STATE(VarAfterPattern)
  565. // Handles `var` parsing at the end.
  566. //
  567. // Always:
  568. // (state done)
  569. CARBON_PARSER_STATE_VARIANTS2(VarFinish, Semicolon, For)
  570. #undef CARBON_PARSER_STATE