parser_state.def 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. // Handles a designator expression, such as `.z` in `x.(y.z)`.
  121. //
  122. // Always:
  123. // (state done)
  124. CARBON_PARSER_STATE(DesignatorAsExpression)
  125. CARBON_PARSER_STATE(DesignatorAsStruct)
  126. // Handles processing of an expression.
  127. //
  128. // If valid prefix operator:
  129. // 1. Expression
  130. // 2. ExpressionLoopForPrefix
  131. // Else:
  132. // 1. ExpressionInPostfix
  133. // 2. ExpressionLoop
  134. CARBON_PARSER_STATE(Expression)
  135. // Handles the initial part of postfix expressions, such as an identifier or
  136. // literal value, then proceeds to the loop.
  137. //
  138. // If `Identifier` or literal (including type literals):
  139. // 1. ExpressionInPostfixLoop
  140. // If `OpenCurlyBrace`:
  141. // 1. BraceExpression
  142. // 2. ExpressionInPostfixLoop
  143. // If `OpenParen`:
  144. // 1. ParenExpression
  145. // 2. ExpressionInPostfixLoop
  146. // Else:
  147. // (state done)
  148. CARBON_PARSER_STATE(ExpressionInPostfix)
  149. // Handles looping through elements following the initial postfix expression,
  150. // such as designators or parenthesized parameters.
  151. //
  152. // If `Period`:
  153. // 1. DesignatorAsExpression
  154. // 2. ExpressionInPostfixLoop
  155. // If `OpenParen`:
  156. // 1. CallExpression
  157. // 2. ExpressionInPostfixLoop
  158. // Else:
  159. // (state done)
  160. CARBON_PARSER_STATE(ExpressionInPostfixLoop)
  161. // Handles processing of an expression.
  162. //
  163. // If binary operator:
  164. // 1. Expression
  165. // 2. ExpressionLoopForBinary
  166. // If postfix operator:
  167. // 1. ExpressionLoop
  168. // Else:
  169. // (state done)
  170. CARBON_PARSER_STATE(ExpressionLoop)
  171. // Completes an ExpressionLoop pass by adding an infix operator, then goes back
  172. // to ExpressionLoop.
  173. //
  174. // Always:
  175. // 1. ExpressionLoop
  176. CARBON_PARSER_STATE(ExpressionLoopForBinary)
  177. // Completes an ExpressionLoop pass by adding a prefix operator, then goes back
  178. // to ExpressionLoop.
  179. //
  180. // Always:
  181. // 1. ExpressionLoop
  182. CARBON_PARSER_STATE(ExpressionLoopForPrefix)
  183. // Handles the `;` for an expression statement, which is different from most
  184. // keyword statements.
  185. //
  186. // Always:
  187. // (state done)
  188. CARBON_PARSER_STATE(ExpressionStatementFinish)
  189. // Handles processing of a function's `fn <name>(`, and enqueues parameter list
  190. // handling.
  191. //
  192. // If invalid:
  193. // (state done)
  194. // If parenthesized parameters:
  195. // 1. PatternAsFunctionParameter
  196. // 2. FunctionParameterListFinish
  197. // 3. FunctionAfterParameterList
  198. // Else:
  199. // 1. FunctionParameterListFinish
  200. // 2. FunctionAfterParameterList
  201. CARBON_PARSER_STATE(FunctionIntroducer)
  202. // Starts function parameter processing.
  203. //
  204. // Always:
  205. // 1. PatternAsFunctionParameter
  206. // 2. FunctionParameterFinish
  207. CARBON_PARSER_STATE(FunctionParameter)
  208. // Finishes function parameter processing, including `,`. If there are more
  209. // parameters, enqueues another parameter processing state.
  210. //
  211. // If `Comma` without `CloseParen`:
  212. // 1. FunctionParameter
  213. // Else:
  214. // (state done)
  215. CARBON_PARSER_STATE(FunctionParameterFinish)
  216. // Handles processing of a function's parameter list `)`.
  217. //
  218. // Always:
  219. // (state done)
  220. CARBON_PARSER_STATE(FunctionParameterListFinish)
  221. // Handles processing of a function's syntax after `)`, primarily the
  222. // possibility a `->` return type is there. Always enqueues signature finish
  223. // handling.
  224. //
  225. // If `MinusGreater`:
  226. // 1. Expression
  227. // 2. FunctionReturnTypeFinish
  228. // 3. FunctionSignatureFinish
  229. // Else:
  230. // 1. FunctionSignatureFinish
  231. CARBON_PARSER_STATE(FunctionAfterParameterList)
  232. // Finishes a function return type.
  233. //
  234. // Always:
  235. // (state done)
  236. CARBON_PARSER_STATE(FunctionReturnTypeFinish)
  237. // Finishes a function signature. If it's a declaration, the function is done;
  238. // otherwise, this also starts definition processing.
  239. //
  240. // If `Semi`:
  241. // (state done)
  242. // If `OpenCurlyBrace`:
  243. // 1. StatementScopeLoop
  244. // 2. FunctionDefinitionFinish
  245. // Else:
  246. // (state done)
  247. CARBON_PARSER_STATE(FunctionSignatureFinish)
  248. // Finishes a function definition.
  249. //
  250. // Always:
  251. // (state done)
  252. CARBON_PARSER_STATE(FunctionDefinitionFinish)
  253. // Finishes an interface definition.
  254. //
  255. // Always:
  256. // (state done)
  257. CARBON_PARSER_STATE(InterfaceDefinitionFinish)
  258. // Handles parsing the body of an interface.
  259. //
  260. // If `}`:
  261. // (state done)
  262. // Else:
  263. // 1. InterfaceDefinitionLoop
  264. CARBON_PARSER_STATE(InterfaceDefinitionLoop)
  265. // Handles processing of a intefaces's `interface <name> {`.
  266. //
  267. // If invalid:
  268. // 1. InterfaceDefinitionFinish
  269. // If `{` is missing:
  270. // 1. InterfaceDefinitionFinish
  271. // Else:
  272. // 1. InterfaceDefinitionLoop
  273. // 2. InterfaceDefinitionFinish
  274. CARBON_PARSER_STATE(InterfaceIntroducer)
  275. // Handles `package`.
  276. //
  277. // Always:
  278. // (state done)
  279. CARBON_PARSER_STATE(Package)
  280. // Handles the processing of a `(condition)` up through the expression.
  281. //
  282. // Always:
  283. // 1. Expression
  284. // 2. ParenConditionFinish
  285. CARBON_PARSER_STATE(ParenCondition)
  286. // Finishes the processing of a `(condition)` after the expression.
  287. //
  288. // Always:
  289. // (state done)
  290. CARBON_PARSER_STATE(ParenConditionFinish)
  291. // Handles the `(` of a parenthesized expression.
  292. //
  293. // If `CloseParen`:
  294. // 1. ParenExpressionFinishAsTuple
  295. // Else:
  296. // 1. Expression
  297. // 2. ParenExpressionParameterFinishAsUnknown
  298. // 3. ParenExpressionFinish
  299. CARBON_PARSER_STATE(ParenExpression)
  300. // Handles the end of a parenthesized expression's parameter. This will start as
  301. // AsUnknown on the first parameter; if there are more, it switches to AsTuple
  302. // processing.
  303. //
  304. // If `Comma` without `CloseParen`:
  305. // 1. Expression
  306. // 2. ParenExpressionParameterFinishAsTuple
  307. // SPECIAL: Parent becomes ParenExpressionFinishAsTuple
  308. // If `Comma` with `CloseParen`:
  309. // (state done)
  310. // SPECIAL: Parent becomes ParenExpressionFinishAsTuple
  311. // Else `CloseParen`:
  312. // (state done)
  313. CARBON_PARSER_STATE(ParenExpressionParameterFinishAsUnknown)
  314. CARBON_PARSER_STATE(ParenExpressionParameterFinishAsTuple)
  315. // Handles the `)` of a parenthesized expression.
  316. //
  317. // Always:
  318. // (state done)
  319. CARBON_PARSER_STATE(ParenExpressionFinish)
  320. CARBON_PARSER_STATE(ParenExpressionFinishAsTuple)
  321. // Handles pattern parsing for a pattern, enqueuing type expression processing.
  322. // This covers function parameter and `var` support.
  323. //
  324. // If valid:
  325. // 1. Expression
  326. // 2. PatternFinish
  327. // Else:
  328. // 1. PatternFinish
  329. CARBON_PARSER_STATE(PatternAsFunctionParameter)
  330. CARBON_PARSER_STATE(PatternAsVariable)
  331. // Finishes pattern processing.
  332. //
  333. // Always:
  334. // (state done)
  335. CARBON_PARSER_STATE(PatternFinish)
  336. // Handles a single statement. While typically within a statement block, this
  337. // can also be used for error recovery where we expect a statement block and
  338. // are missing braces.
  339. //
  340. // If `Break`:
  341. // 1. StatementBreakFinish
  342. // (state done)
  343. // If `Continue`:
  344. // 1. StatementContinueFinish
  345. // (state done)
  346. // If `For`:
  347. // 1. StatementForHeader
  348. // 2. StatementForFinish
  349. // If `If`:
  350. // 1. StatementIf
  351. // If `Return`:
  352. // 1. StatementReturn
  353. // If `Var`:
  354. // 1. VarAsRequireSemicolon
  355. // If `While`:
  356. // 1. StatementWhile
  357. // Else:
  358. // 1. Expression
  359. // 2. ExpressionStatementFinish
  360. CARBON_PARSER_STATE(Statement)
  361. // Handles `break` processing at the `;`.
  362. //
  363. // Always:
  364. // (state done)
  365. CARBON_PARSER_STATE(StatementBreakFinish)
  366. // Handles `continue` processing at the `;`.
  367. //
  368. // Always:
  369. // (state done)
  370. CARBON_PARSER_STATE(StatementContinueFinish)
  371. // Handles `for` processing of `(var`, proceeding to a pattern before
  372. // continuing.
  373. //
  374. // If no `OpenParen`:
  375. // 1. CodeBlock
  376. // If `Var`:
  377. // 1. VarAsNoSemicolon
  378. // 2. StatementForHeaderIn
  379. // Else:
  380. // 1. StatementForHeaderIn
  381. CARBON_PARSER_STATE(StatementForHeader)
  382. // Handles `for` procesisng of `in`, proceeding to an expression before
  383. // continuing.
  384. //
  385. // If `In` or `Colon`:
  386. // 1. Expression
  387. // 2. StatementForHeaderFinish
  388. // Else:
  389. // 1. StatementForHeaderFinish
  390. CARBON_PARSER_STATE(StatementForHeaderIn)
  391. // Handles `for` processing of `)`, proceeding to the statement block.
  392. //
  393. // Always:
  394. // 1. CodeBlock
  395. CARBON_PARSER_STATE(StatementForHeaderFinish)
  396. // Handles `for` processing at the final `}`.
  397. //
  398. // Always:
  399. // (state done)
  400. CARBON_PARSER_STATE(StatementForFinish)
  401. // Handles `if` processing at the start.
  402. //
  403. // Always:
  404. // 1. ParenCondition
  405. // 2. StatementIfConditionFinish
  406. CARBON_PARSER_STATE(StatementIf)
  407. // Handles `if` processing between the condition and start of the first code
  408. // block.
  409. //
  410. // Always:
  411. // 1. CodeBlock
  412. // 2. StatementIfThenBlockFinish
  413. CARBON_PARSER_STATE(StatementIfConditionFinish)
  414. // Handles `if` processing after the end of the first code block, with the
  415. // optional `else`.
  416. //
  417. // If `Else` then `If`:
  418. // 1. CodeBlock
  419. // 2. StatementIfElseBlockFinish
  420. // If `Else`:
  421. // 1. StatementIf
  422. // 2. StatementIfElseBlockFinish
  423. // Else:
  424. // (state done)
  425. CARBON_PARSER_STATE(StatementIfThenBlockFinish)
  426. // Handles `if` processing after a provided `else` code block.
  427. //
  428. // Always:
  429. // (state done)
  430. CARBON_PARSER_STATE(StatementIfElseBlockFinish)
  431. // Handles `return` processing.
  432. //
  433. // If `Semi`:
  434. // 1. StatementReturnFinish
  435. // Else:
  436. // 1. Expression
  437. // 2. StatementReturnFinish
  438. CARBON_PARSER_STATE(StatementReturn)
  439. // Handles `return` processing at the `;` when there's an expression.
  440. //
  441. // Always:
  442. // (state done)
  443. CARBON_PARSER_STATE(StatementReturnFinish)
  444. // Handles processing of statements within a scope.
  445. //
  446. // If `CloseCurlyBrace`:
  447. // (state done)
  448. // Else:
  449. // 1. Statement
  450. // 2. StatementScopeLoop
  451. CARBON_PARSER_STATE(StatementScopeLoop)
  452. // Handles `while` processing.
  453. //
  454. // Always:
  455. // 1. ParenCondition
  456. // 2. StatementWhileConditionFinish
  457. CARBON_PARSER_STATE(StatementWhile)
  458. // Handles `while` processing between the condition and start of the code block.
  459. //
  460. // Always:
  461. // 1. CodeBlock
  462. // 2. StatementWhileBlockFinish
  463. CARBON_PARSER_STATE(StatementWhileConditionFinish)
  464. // Handles `while` processing after the end of the code block.
  465. //
  466. // Always:
  467. // (state done)
  468. CARBON_PARSER_STATE(StatementWhileBlockFinish)
  469. // Handles the start of a `var`.
  470. //
  471. // Always:
  472. // 1. PatternAsVariable
  473. // 2. VarAfterPattern
  474. // 3. VarFinishAs(RequireSemicolon|NoSemicolon)
  475. CARBON_PARSER_STATE(VarAsRequireSemicolon)
  476. CARBON_PARSER_STATE(VarAsNoSemicolon)
  477. // Handles `var` after the pattern, either followed by an initializer or the
  478. // semicolon.
  479. //
  480. // If `Equal`:
  481. // 1. Expression
  482. // 2. VarAfterInitializer
  483. // Else:
  484. // (state done)
  485. CARBON_PARSER_STATE(VarAfterPattern)
  486. // Handles `var` after the initializer, wrapping up its subtree.
  487. //
  488. // Always:
  489. // (state done)
  490. CARBON_PARSER_STATE(VarAfterInitializer)
  491. // Handles `var` parsing at the end.
  492. //
  493. // Always:
  494. // (state done)
  495. CARBON_PARSER_STATE(VarFinishAsRequireSemicolon)
  496. CARBON_PARSER_STATE(VarFinishAsNoSemicolon)
  497. #undef CARBON_PARSER_STATE