parser_state.def 14 KB

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