parser_state.def 16 KB

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