parser_state.def 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. // Note that this is an X-macro header.
  6. //
  7. // It does not use `#include` guards, and instead is designed to be `#include`ed
  8. // after the x-macro is defined in order for its inclusion to expand to the
  9. // desired output. The x-macro for this header is `CARBON_PARSE_NODE_KIND`. The
  10. // definition provided will be removed at the end of this file to clean up.
  11. #ifndef CARBON_PARSER_STATE
  12. #error "Must define the x-macro to use this file."
  13. #endif
  14. // Handles the `{` of a brace expression.
  15. //
  16. // If `CloseCurlyBrace`:
  17. // 1. BraceExpressionFinishAsUnknown
  18. // Else:
  19. // 1. BraceExpressionParameterAsUnknown
  20. // 2. BraceExpressionFinishAsUnknown
  21. CARBON_PARSER_STATE(BraceExpression)
  22. // Handles a brace expression parameter. Note this will always start as unknown,
  23. // but should be known after the first valid parameter. All later inconsistent
  24. // parameters are invalid.
  25. //
  26. // If valid:
  27. // 1. DesignatorExpressionAsStruct
  28. // 2. BraceExpressionParameterAfterDesignatorAs(Type|Value|Unknown)
  29. // Else:
  30. // 1. BraceExpressionParameterFinishAs(Type|Value|Unknown)
  31. CARBON_PARSER_STATE(BraceExpressionParameterAsType)
  32. CARBON_PARSER_STATE(BraceExpressionParameterAsValue)
  33. CARBON_PARSER_STATE(BraceExpressionParameterAsUnknown)
  34. // Handles a brace expression parameter after the initial designator. This
  35. // should be at a `:` or `=`, depending on whether it's a type or value literal.
  36. //
  37. // If valid:
  38. // 1. Expression
  39. // 2. BraceExpressionParameterFinishAs(Type|Value|Unknown)
  40. // Else:
  41. // 1. BraceExpressionParameterFinishAs(Type|Value|Unknown)
  42. CARBON_PARSER_STATE(BraceExpressionParameterAfterDesignatorAsType)
  43. CARBON_PARSER_STATE(BraceExpressionParameterAfterDesignatorAsValue)
  44. CARBON_PARSER_STATE(BraceExpressionParameterAfterDesignatorAsUnknown)
  45. // Handles the end of a brace expression parameter.
  46. //
  47. // If `Comma`:
  48. // 1. BraceExpressionParameterAsUnknown
  49. // Else:
  50. // (state done)
  51. CARBON_PARSER_STATE(BraceExpressionParameterFinishAsType)
  52. CARBON_PARSER_STATE(BraceExpressionParameterFinishAsValue)
  53. CARBON_PARSER_STATE(BraceExpressionParameterFinishAsUnknown)
  54. // Handles the `}` of a brace expression.
  55. //
  56. // Always:
  57. // (state done)
  58. CARBON_PARSER_STATE(BraceExpressionFinishAsType)
  59. CARBON_PARSER_STATE(BraceExpressionFinishAsValue)
  60. CARBON_PARSER_STATE(BraceExpressionFinishAsUnknown)
  61. // Handles a call expression `(...)`.
  62. //
  63. // If `CloseParen`:
  64. // 1. CallExpressionFinish
  65. // Else:
  66. // 1. Expression
  67. // 2. CallExpressionParameterFinish
  68. // 3. CallExpressionFinish
  69. CARBON_PARSER_STATE(CallExpression)
  70. // Handles the `,` or `)` after a call parameter.
  71. //
  72. // If `Comma`:
  73. // 1. Expression
  74. // 2. CallExpressionParameterFinish
  75. // Else:
  76. // (state done)
  77. CARBON_PARSER_STATE(CallExpressionParameterFinish)
  78. // Handles finishing the call expression.
  79. //
  80. // Always:
  81. // (state done)
  82. CARBON_PARSER_STATE(CallExpressionFinish)
  83. // Handles processing at the `{` on a typical code block.
  84. //
  85. // If `OpenCurlyBrace`:
  86. // 1. StatementScopeLoop
  87. // 2. CodeBlockFinish
  88. // Else:
  89. // 1. Statement
  90. // 2. CodeBlockFinish
  91. CARBON_PARSER_STATE(CodeBlock)
  92. // Handles processing at the `}` on a typical code block, after a statement
  93. // scope is done.
  94. //
  95. // Always:
  96. // (state done)
  97. CARBON_PARSER_STATE(CodeBlockFinish)
  98. // Handles processing of a declaration scope. Things like fn, class, interface,
  99. // and so on.
  100. //
  101. // If `EndOfFile`:
  102. // (state done)
  103. // If `Fn`:
  104. // 1. FunctionIntroducer
  105. // 2. DeclarationLoop
  106. // If `Package`:
  107. // 1. Package
  108. // 2. DeclarationLoop
  109. // If `Semi`:
  110. // 1. DeclarationLoop
  111. // If `Var`:
  112. // 1. Var
  113. // 2. DeclarationLoop
  114. // If `interface`:
  115. // 1. InterfaceIntroducer
  116. // 2. DeclarationLoop
  117. // Else:
  118. // 1. DeclarationLoop
  119. CARBON_PARSER_STATE(DeclarationLoop)
  120. // To handle parsing deduced parameter list at the end.
  121. //
  122. // Always:
  123. // (state done)
  124. CARBON_PARSER_STATE(DeducedParameterListFinish)
  125. // Handles a designator expression, such as `.z` in `x.(y.z)`.
  126. //
  127. // Always:
  128. // (state done)
  129. CARBON_PARSER_STATE(DesignatorAsExpression)
  130. CARBON_PARSER_STATE(DesignatorAsStruct)
  131. // Handles processing of an expression.
  132. //
  133. // If valid prefix operator:
  134. // 1. Expression
  135. // 2. ExpressionLoopForPrefix
  136. // Else:
  137. // 1. ExpressionInPostfix
  138. // 2. ExpressionLoop
  139. CARBON_PARSER_STATE(Expression)
  140. // Handles the initial part of postfix expressions, such as an identifier or
  141. // literal value, then proceeds to the loop.
  142. //
  143. // If `Identifier` or literal (including type literals):
  144. // 1. ExpressionInPostfixLoop
  145. // If `OpenCurlyBrace`:
  146. // 1. BraceExpression
  147. // 2. ExpressionInPostfixLoop
  148. // If `OpenParen`:
  149. // 1. ParenExpression
  150. // 2. ExpressionInPostfixLoop
  151. // Else:
  152. // (state done)
  153. CARBON_PARSER_STATE(ExpressionInPostfix)
  154. // Handles looping through elements following the initial postfix expression,
  155. // such as designators or parenthesized parameters.
  156. //
  157. // If `Period`:
  158. // 1. DesignatorAsExpression
  159. // 2. ExpressionInPostfixLoop
  160. // If `OpenParen`:
  161. // 1. CallExpression
  162. // 2. ExpressionInPostfixLoop
  163. // Else:
  164. // (state done)
  165. CARBON_PARSER_STATE(ExpressionInPostfixLoop)
  166. // Handles processing of an expression.
  167. //
  168. // If binary operator:
  169. // 1. Expression
  170. // 2. ExpressionLoopForBinary
  171. // If postfix operator:
  172. // 1. ExpressionLoop
  173. // Else:
  174. // (state done)
  175. CARBON_PARSER_STATE(ExpressionLoop)
  176. // Completes an ExpressionLoop pass by adding an infix operator, then goes back
  177. // to ExpressionLoop.
  178. //
  179. // Always:
  180. // 1. ExpressionLoop
  181. CARBON_PARSER_STATE(ExpressionLoopForBinary)
  182. // Completes an ExpressionLoop pass by adding a prefix operator, then goes back
  183. // to ExpressionLoop.
  184. //
  185. // Always:
  186. // 1. ExpressionLoop
  187. CARBON_PARSER_STATE(ExpressionLoopForPrefix)
  188. // Handles the `;` for an expression statement, which is different from most
  189. // keyword statements.
  190. //
  191. // Always:
  192. // (state done)
  193. CARBON_PARSER_STATE(ExpressionStatementFinish)
  194. // Handles processing of a function's `fn <name>(`, and enqueues parameter list
  195. // handling.
  196. //
  197. // If invalid:
  198. // (state done)
  199. // If parenthesized parameters:
  200. // 1. FunctionParameter
  201. // 2. FunctionParameterListFinish
  202. // 3. FunctionAfterParameterList
  203. // Else:
  204. // 1. FunctionParameterListFinish
  205. // 2. FunctionAfterParameterList
  206. CARBON_PARSER_STATE(FunctionIntroducer)
  207. // Starts function parameter processing.
  208. //
  209. // Always:
  210. // 1. PatternAsFunctionParameter
  211. // 2. FunctionParameterFinish
  212. CARBON_PARSER_STATE(FunctionParameter)
  213. // Finishes function parameter processing, including `,`. If there are more
  214. // parameters, enqueues another parameter processing state.
  215. //
  216. // If `Comma` without `CloseParen`:
  217. // 1. FunctionParameter
  218. // Else:
  219. // (state done)
  220. CARBON_PARSER_STATE(FunctionParameterFinish)
  221. // Handles processing of a function's parameter list `)`.
  222. //
  223. // Always:
  224. // (state done)
  225. CARBON_PARSER_STATE(FunctionParameterListFinish)
  226. // Handles processing of a function's syntax after `)`, primarily the
  227. // possibility a `->` return type is there. Always enqueues signature finish
  228. // handling.
  229. //
  230. // If `MinusGreater`:
  231. // 1. Expression
  232. // 2. FunctionReturnTypeFinish
  233. // 3. FunctionSignatureFinish
  234. // Else:
  235. // 1. FunctionSignatureFinish
  236. CARBON_PARSER_STATE(FunctionAfterParameterList)
  237. // Finishes a function return type.
  238. //
  239. // Always:
  240. // (state done)
  241. CARBON_PARSER_STATE(FunctionReturnTypeFinish)
  242. // Finishes a function signature. If it's a declaration, the function is done;
  243. // otherwise, this also starts definition processing.
  244. //
  245. // If `Semi`:
  246. // (state done)
  247. // If `OpenCurlyBrace`:
  248. // 1. StatementScopeLoop
  249. // 2. FunctionDefinitionFinish
  250. // Else:
  251. // (state done)
  252. CARBON_PARSER_STATE(FunctionSignatureFinish)
  253. // Finishes a function definition.
  254. //
  255. // Always:
  256. // (state done)
  257. CARBON_PARSER_STATE(FunctionDefinitionFinish)
  258. // Finishes an interface definition.
  259. //
  260. // Always:
  261. // (state done)
  262. CARBON_PARSER_STATE(InterfaceDefinitionFinish)
  263. // Handles parsing the body of an interface.
  264. //
  265. // If `}`:
  266. // (state done)
  267. // Else:
  268. // 1. InterfaceDefinitionLoop
  269. CARBON_PARSER_STATE(InterfaceDefinitionLoop)
  270. // Handles processing of a intefaces's `interface <name> {`.
  271. //
  272. // If invalid:
  273. // 1. InterfaceDefinitionFinish
  274. // If `{` is missing:
  275. // 1. InterfaceDefinitionFinish
  276. // Else:
  277. // 1. InterfaceDefinitionLoop
  278. // 2. InterfaceDefinitionFinish
  279. CARBON_PARSER_STATE(InterfaceIntroducer)
  280. // Handles `package`.
  281. //
  282. // Always:
  283. // (state done)
  284. CARBON_PARSER_STATE(Package)
  285. // Handles the processing of a `(condition)` up through the expression.
  286. //
  287. // Always:
  288. // 1. Expression
  289. // 2. ParenConditionAs(If|While)Finish
  290. CARBON_PARSER_STATE(ParenConditionAsIf)
  291. CARBON_PARSER_STATE(ParenConditionAsWhile)
  292. // Finishes the processing of a `(condition)` after the expression.
  293. //
  294. // Always:
  295. // (state done)
  296. CARBON_PARSER_STATE(ParenConditionFinishAsIf)
  297. CARBON_PARSER_STATE(ParenConditionFinishAsWhile)
  298. // Handles the `(` of a parenthesized expression.
  299. //
  300. // If `CloseParen`:
  301. // 1. ParenExpressionFinishAsTuple
  302. // Else:
  303. // 1. Expression
  304. // 2. ParenExpressionParameterFinishAsUnknown
  305. // 3. ParenExpressionFinish
  306. CARBON_PARSER_STATE(ParenExpression)
  307. // Handles the end of a parenthesized expression's parameter. This will start as
  308. // AsUnknown on the first parameter; if there are more, it switches to AsTuple
  309. // processing.
  310. //
  311. // If `Comma` without `CloseParen`:
  312. // 1. Expression
  313. // 2. ParenExpressionParameterFinishAsTuple
  314. // SPECIAL: Parent becomes ParenExpressionFinishAsTuple
  315. // If `Comma` with `CloseParen`:
  316. // (state done)
  317. // SPECIAL: Parent becomes ParenExpressionFinishAsTuple
  318. // Else `CloseParen`:
  319. // (state done)
  320. CARBON_PARSER_STATE(ParenExpressionParameterFinishAsUnknown)
  321. CARBON_PARSER_STATE(ParenExpressionParameterFinishAsTuple)
  322. // Handles the `)` of a parenthesized expression.
  323. //
  324. // Always:
  325. // (state done)
  326. CARBON_PARSER_STATE(ParenExpressionFinish)
  327. CARBON_PARSER_STATE(ParenExpressionFinishAsTuple)
  328. // Handles pattern parsing for a pattern, enqueuing type expression processing.
  329. // This covers function parameter and `var` support.
  330. //
  331. // If valid:
  332. // 1. Expression
  333. // 2. PatternFinish
  334. // Else:
  335. // 1. PatternFinish
  336. CARBON_PARSER_STATE(PatternAsFunctionParameter)
  337. CARBON_PARSER_STATE(PatternAsVariable)
  338. // Handles `addr` in a pattern.
  339. //
  340. // Always:
  341. // (state done)
  342. CARBON_PARSER_STATE(PatternAddress)
  343. // Finishes pattern processing.
  344. //
  345. // Always:
  346. // (state done)
  347. CARBON_PARSER_STATE(PatternFinish)
  348. // To start parsing `self` deduced parameter.
  349. //
  350. // If valid:
  351. // 1. Expression
  352. // 2. PatternFinish
  353. // Else:
  354. // 1. PatternFinish
  355. CARBON_PARSER_STATE(SelfPattern)
  356. // Handles a single statement. While typically within a statement block, this
  357. // can also be used for error recovery where we expect a statement block and
  358. // are missing braces.
  359. //
  360. // If `Break`:
  361. // 1. StatementBreakFinish
  362. // (state done)
  363. // If `Continue`:
  364. // 1. StatementContinueFinish
  365. // (state done)
  366. // If `For`:
  367. // 1. StatementForHeader
  368. // 2. StatementForFinish
  369. // If `If`:
  370. // 1. StatementIf
  371. // If `Return`:
  372. // 1. StatementReturn
  373. // If `Var`:
  374. // 1. VarAsSemicolon
  375. // If `While`:
  376. // 1. StatementWhile
  377. // Else:
  378. // 1. Expression
  379. // 2. ExpressionStatementFinish
  380. CARBON_PARSER_STATE(Statement)
  381. // Handles `break` processing at the `;`.
  382. //
  383. // Always:
  384. // (state done)
  385. CARBON_PARSER_STATE(StatementBreakFinish)
  386. // Handles `continue` processing at the `;`.
  387. //
  388. // Always:
  389. // (state done)
  390. CARBON_PARSER_STATE(StatementContinueFinish)
  391. // Handles `for` processing of `(var`, proceeding to a pattern before
  392. // continuing.
  393. //
  394. // If no `OpenParen`:
  395. // 1. CodeBlock
  396. // If `Var`:
  397. // 1. VarAsFor
  398. // 2. StatementForHeaderIn
  399. // Else:
  400. // 1. StatementForHeaderIn
  401. CARBON_PARSER_STATE(StatementForHeader)
  402. // Handles `for` procesisng of `in`, proceeding to an expression before
  403. // continuing.
  404. //
  405. // If `In` or `Colon`:
  406. // 1. Expression
  407. // 2. StatementForHeaderFinish
  408. // Else:
  409. // 1. StatementForHeaderFinish
  410. CARBON_PARSER_STATE(StatementForHeaderIn)
  411. // Handles `for` processing of `)`, proceeding to the statement block.
  412. //
  413. // Always:
  414. // 1. CodeBlock
  415. CARBON_PARSER_STATE(StatementForHeaderFinish)
  416. // Handles `for` processing at the final `}`.
  417. //
  418. // Always:
  419. // (state done)
  420. CARBON_PARSER_STATE(StatementForFinish)
  421. // Handles `if` processing at the start.
  422. //
  423. // Always:
  424. // 1. ParenConditionAsIf
  425. // 2. StatementIfConditionFinish
  426. CARBON_PARSER_STATE(StatementIf)
  427. // Handles `if` processing between the condition and start of the first code
  428. // block.
  429. //
  430. // Always:
  431. // 1. CodeBlock
  432. // 2. StatementIfThenBlockFinish
  433. CARBON_PARSER_STATE(StatementIfConditionFinish)
  434. // Handles `if` processing after the end of the first code block, with the
  435. // optional `else`.
  436. //
  437. // If `Else` then `If`:
  438. // 1. CodeBlock
  439. // 2. StatementIfElseBlockFinish
  440. // If `Else`:
  441. // 1. StatementIf
  442. // 2. StatementIfElseBlockFinish
  443. // Else:
  444. // (state done)
  445. CARBON_PARSER_STATE(StatementIfThenBlockFinish)
  446. // Handles `if` processing after a provided `else` code block.
  447. //
  448. // Always:
  449. // (state done)
  450. CARBON_PARSER_STATE(StatementIfElseBlockFinish)
  451. // Handles `return` processing.
  452. //
  453. // If `Semi`:
  454. // 1. StatementReturnFinish
  455. // Else:
  456. // 1. Expression
  457. // 2. StatementReturnFinish
  458. CARBON_PARSER_STATE(StatementReturn)
  459. // Handles `return` processing at the `;` when there's an expression.
  460. //
  461. // Always:
  462. // (state done)
  463. CARBON_PARSER_STATE(StatementReturnFinish)
  464. // Handles processing of statements within a scope.
  465. //
  466. // If `CloseCurlyBrace`:
  467. // (state done)
  468. // Else:
  469. // 1. Statement
  470. // 2. StatementScopeLoop
  471. CARBON_PARSER_STATE(StatementScopeLoop)
  472. // Handles `while` processing.
  473. //
  474. // Always:
  475. // 1. ParenConditionAsWhile
  476. // 2. StatementWhileConditionFinish
  477. CARBON_PARSER_STATE(StatementWhile)
  478. // Handles `while` processing between the condition and start of the code block.
  479. //
  480. // Always:
  481. // 1. CodeBlock
  482. // 2. StatementWhileBlockFinish
  483. CARBON_PARSER_STATE(StatementWhileConditionFinish)
  484. // Handles `while` processing after the end of the code block.
  485. //
  486. // Always:
  487. // (state done)
  488. CARBON_PARSER_STATE(StatementWhileBlockFinish)
  489. // Handles the start of a `var`.
  490. //
  491. // Always:
  492. // 1. PatternAsVariable
  493. // 2. VarAfterPattern
  494. // 3. VarFinishAs(Semicolon|For)
  495. CARBON_PARSER_STATE(VarAsSemicolon)
  496. CARBON_PARSER_STATE(VarAsFor)
  497. // Handles `var` after the pattern, either followed by an initializer or the
  498. // semicolon.
  499. //
  500. // If `Equal`:
  501. // 1. Expression
  502. // Else:
  503. // (state done)
  504. CARBON_PARSER_STATE(VarAfterPattern)
  505. // Handles `var` parsing at the end.
  506. //
  507. // Always:
  508. // (state done)
  509. CARBON_PARSER_STATE(VarFinishAsSemicolon)
  510. CARBON_PARSER_STATE(VarFinishAsFor)
  511. // Handles processing of a functions' syntax after the deduced parameter lists's
  512. // `]`. This applies only to interfaces and classes.
  513. //
  514. // If invalid:
  515. // (state done)
  516. // If parenthesized parameters:
  517. // 1. FunctionParameter
  518. // 2. FunctionParameterListFinish
  519. // 3. FunctionAfterParameterList
  520. // Else:
  521. // 1. FunctionParameterListFinish
  522. // 2. FunctionAfterParameterList
  523. CARBON_PARSER_STATE(FunctionAfterDeducedParameterList)
  524. #undef CARBON_PARSER_STATE