token_kind.def 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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_TOKEN(Name)
  12. // Defines a token. Used directly when a token needs custom parsing, such as
  13. // integer literals.
  14. // - CARBON_SYMBOL_TOKEN(Name, Spelling)
  15. // Defines a symbol which has the provided spelling, such as `*`. Spellings
  16. // must be unique.
  17. // - CARBON_OPENING_GROUP_SYMBOL_TOKEN(Name, Spelling, ClosingName)
  18. // - CARBON_CLOSING_GROUP_SYMBOL_TOKEN(Name, Spelling, OpeningName)
  19. // These two macros together define matches opening and closing symbols,
  20. // such as `(` and `)`, and create an association between the two.
  21. // - CARBON_KEYWORD_TOKEN(Name, Spelling)
  22. // Defines a keyword which has the provided spelling, such as `if`.
  23. // Spellings must be unique.
  24. // - CARBON_DECL_INTRODUCER_TOKEN(Name, Spelling)
  25. // A declaration introducer keyword, such as `fn`.
  26. // - CARBON_TOKEN_WITH_VIRTUAL_NODE(TokenIndex)
  27. // Wrapped around one of the above _TOKEN macros, indicates that this
  28. // token has one additional virtual node in the parse tree.
  29. //
  30. // This tree represents the subset relationship between these macros, where if a
  31. // specific x-macro isn't defined, it'll fall back to the parent macro.
  32. #ifndef CARBON_TOKEN
  33. #define CARBON_TOKEN(Name)
  34. #endif
  35. // The error token comes first because we want it to get the zero value, which
  36. // will also be used in default initialization.
  37. CARBON_TOKEN(Error)
  38. #ifndef CARBON_SYMBOL_TOKEN
  39. #define CARBON_SYMBOL_TOKEN(Name, Spelling) CARBON_TOKEN(Name)
  40. #endif
  41. #ifndef CARBON_ONE_CHAR_SYMBOL_TOKEN
  42. #define CARBON_ONE_CHAR_SYMBOL_TOKEN(Name, Spelling) \
  43. CARBON_SYMBOL_TOKEN(Name, Spelling)
  44. #endif
  45. #ifndef CARBON_TOKEN_WITH_VIRTUAL_NODE
  46. #define CARBON_TOKEN_WITH_VIRTUAL_NODE(Name) Name
  47. #endif
  48. // Note that symbols need to be ordered from longest to shortest to effectively
  49. // provide max-munch lexing.
  50. // clang-format off
  51. CARBON_SYMBOL_TOKEN(GreaterGreaterEqual, ">>=")
  52. CARBON_SYMBOL_TOKEN(LessEqualGreater, "<=>")
  53. CARBON_SYMBOL_TOKEN(LessLessEqual, "<<=")
  54. CARBON_SYMBOL_TOKEN(MinusGreaterQuestion,"->?")
  55. CARBON_SYMBOL_TOKEN(AmpEqual, "&=")
  56. CARBON_SYMBOL_TOKEN(CaretEqual, "^=")
  57. CARBON_SYMBOL_TOKEN(ColonEqual, ":=")
  58. CARBON_TOKEN_WITH_VIRTUAL_NODE(
  59. CARBON_SYMBOL_TOKEN(ColonExclaim, ":!"))
  60. CARBON_SYMBOL_TOKEN(ColonQuestion, ":?")
  61. CARBON_SYMBOL_TOKEN(EqualEqual, "==")
  62. CARBON_SYMBOL_TOKEN(EqualGreater, "=>")
  63. CARBON_SYMBOL_TOKEN(ExclaimEqual, "!=")
  64. CARBON_SYMBOL_TOKEN(GreaterEqual, ">=")
  65. CARBON_SYMBOL_TOKEN(GreaterGreater, ">>")
  66. CARBON_SYMBOL_TOKEN(LessEqual, "<=")
  67. CARBON_SYMBOL_TOKEN(LessGreater, "<>")
  68. CARBON_SYMBOL_TOKEN(LessLess, "<<")
  69. CARBON_SYMBOL_TOKEN(LessMinus, "<-")
  70. CARBON_SYMBOL_TOKEN(MinusEqual, "-=")
  71. CARBON_SYMBOL_TOKEN(MinusGreater, "->")
  72. CARBON_SYMBOL_TOKEN(MinusMinus, "--")
  73. CARBON_SYMBOL_TOKEN(PercentEqual, "%=")
  74. CARBON_SYMBOL_TOKEN(PipeEqual, "|=")
  75. CARBON_SYMBOL_TOKEN(PlusEqual, "+=")
  76. CARBON_SYMBOL_TOKEN(PlusPlus, "++")
  77. CARBON_SYMBOL_TOKEN(SlashEqual, "/=")
  78. CARBON_SYMBOL_TOKEN(StarEqual, "*=")
  79. CARBON_SYMBOL_TOKEN(TildeEqual, "~=")
  80. CARBON_SYMBOL_TOKEN(Amp, "&")
  81. CARBON_SYMBOL_TOKEN(At, "@")
  82. CARBON_SYMBOL_TOKEN(Backslash, "\\")
  83. CARBON_SYMBOL_TOKEN(Caret, "^")
  84. CARBON_SYMBOL_TOKEN(Colon, ":")
  85. CARBON_SYMBOL_TOKEN(Equal, "=")
  86. CARBON_SYMBOL_TOKEN(Exclaim, "!")
  87. CARBON_SYMBOL_TOKEN(Greater, ">")
  88. CARBON_SYMBOL_TOKEN(Less, "<")
  89. CARBON_SYMBOL_TOKEN(Minus, "-")
  90. CARBON_SYMBOL_TOKEN(Percent, "%")
  91. CARBON_SYMBOL_TOKEN(Period, ".")
  92. CARBON_SYMBOL_TOKEN(Pipe, "|")
  93. CARBON_SYMBOL_TOKEN(Plus, "+")
  94. CARBON_SYMBOL_TOKEN(Question, "?")
  95. CARBON_SYMBOL_TOKEN(Slash, "/")
  96. CARBON_SYMBOL_TOKEN(Star, "*")
  97. CARBON_SYMBOL_TOKEN(Tilde, "~")
  98. // Some Carbon symbols are constructively exactly one character and cannot be
  99. // combined with any other characters to form new symbols. We can lex these
  100. // without needing to max-munch any other characters. These are typically
  101. // expected to be terminators or separators that need to compose with all other
  102. // parts of the grammar. Group symbols are also currently one-character symbols,
  103. // although we may choose to remove that if we need to add composite grouping
  104. // symbols in the future.
  105. CARBON_ONE_CHAR_SYMBOL_TOKEN(Comma, ",")
  106. CARBON_ONE_CHAR_SYMBOL_TOKEN(Semi, ";")
  107. // clang-format on
  108. #ifndef CARBON_OPENING_GROUP_SYMBOL_TOKEN
  109. #define CARBON_OPENING_GROUP_SYMBOL_TOKEN(Name, Spelling, ClosingName) \
  110. CARBON_ONE_CHAR_SYMBOL_TOKEN(Name, Spelling)
  111. #endif
  112. CARBON_OPENING_GROUP_SYMBOL_TOKEN(OpenParen, "(", CloseParen)
  113. CARBON_OPENING_GROUP_SYMBOL_TOKEN(OpenCurlyBrace, "{", CloseCurlyBrace)
  114. CARBON_OPENING_GROUP_SYMBOL_TOKEN(OpenSquareBracket, "[", CloseSquareBracket)
  115. #undef CARBON_OPENING_GROUP_SYMBOL_TOKEN
  116. #ifndef CARBON_CLOSING_GROUP_SYMBOL_TOKEN
  117. #define CARBON_CLOSING_GROUP_SYMBOL_TOKEN(Name, Spelling, OpeningName) \
  118. CARBON_ONE_CHAR_SYMBOL_TOKEN(Name, Spelling)
  119. #endif
  120. CARBON_CLOSING_GROUP_SYMBOL_TOKEN(CloseParen, ")", OpenParen)
  121. CARBON_CLOSING_GROUP_SYMBOL_TOKEN(CloseCurlyBrace, "}", OpenCurlyBrace)
  122. CARBON_CLOSING_GROUP_SYMBOL_TOKEN(CloseSquareBracket, "]", OpenSquareBracket)
  123. #undef CARBON_CLOSING_GROUP_SYMBOL_TOKEN
  124. #undef CARBON_ONE_CHAR_SYMBOL_TOKEN
  125. #undef CARBON_SYMBOL_TOKEN
  126. #ifndef CARBON_KEYWORD_TOKEN
  127. #define CARBON_KEYWORD_TOKEN(Name, Spelling) CARBON_TOKEN(Name)
  128. #endif
  129. #ifndef CARBON_DECL_INTRODUCER_TOKEN
  130. #define CARBON_DECL_INTRODUCER_TOKEN(Name, Spelling) \
  131. CARBON_KEYWORD_TOKEN(Name, Spelling)
  132. #endif
  133. // clang-format off
  134. CARBON_DECL_INTRODUCER_TOKEN(Adapt, "adapt")
  135. CARBON_DECL_INTRODUCER_TOKEN(Alias, "alias")
  136. CARBON_DECL_INTRODUCER_TOKEN(Base, "base")
  137. CARBON_DECL_INTRODUCER_TOKEN(Choice, "choice")
  138. CARBON_DECL_INTRODUCER_TOKEN(Class, "class")
  139. CARBON_DECL_INTRODUCER_TOKEN(Constraint, "constraint")
  140. CARBON_DECL_INTRODUCER_TOKEN(Export, "export")
  141. CARBON_TOKEN_WITH_VIRTUAL_NODE(
  142. CARBON_DECL_INTRODUCER_TOKEN(Fn, "fn"))
  143. CARBON_DECL_INTRODUCER_TOKEN(Impl, "impl")
  144. CARBON_DECL_INTRODUCER_TOKEN(Import, "import")
  145. CARBON_DECL_INTRODUCER_TOKEN(Interface, "interface")
  146. CARBON_DECL_INTRODUCER_TOKEN(Let, "let")
  147. CARBON_DECL_INTRODUCER_TOKEN(Library, "library")
  148. CARBON_DECL_INTRODUCER_TOKEN(Namespace, "namespace")
  149. CARBON_DECL_INTRODUCER_TOKEN(Package, "package")
  150. CARBON_DECL_INTRODUCER_TOKEN(Require, "require")
  151. CARBON_TOKEN_WITH_VIRTUAL_NODE(
  152. CARBON_DECL_INTRODUCER_TOKEN(Var, "var"))
  153. CARBON_KEYWORD_TOKEN(Abstract, "abstract")
  154. CARBON_TOKEN_WITH_VIRTUAL_NODE(
  155. CARBON_KEYWORD_TOKEN(And, "and"))
  156. CARBON_KEYWORD_TOKEN(Array, "array")
  157. CARBON_KEYWORD_TOKEN(As, "as")
  158. CARBON_KEYWORD_TOKEN(Auto, "auto")
  159. CARBON_KEYWORD_TOKEN(Bool, "bool")
  160. CARBON_KEYWORD_TOKEN(Break, "break")
  161. CARBON_KEYWORD_TOKEN(Case, "case")
  162. CARBON_KEYWORD_TOKEN(Char, "char")
  163. CARBON_KEYWORD_TOKEN(Const, "const")
  164. CARBON_KEYWORD_TOKEN(Continue, "continue")
  165. CARBON_KEYWORD_TOKEN(Core, "Core")
  166. CARBON_KEYWORD_TOKEN(Cpp, "Cpp")
  167. CARBON_KEYWORD_TOKEN(Default, "default")
  168. CARBON_KEYWORD_TOKEN(Else, "else")
  169. CARBON_KEYWORD_TOKEN(Eval, "eval")
  170. CARBON_KEYWORD_TOKEN(Extend, "extend")
  171. CARBON_KEYWORD_TOKEN(Extern, "extern")
  172. CARBON_KEYWORD_TOKEN(False, "false")
  173. CARBON_KEYWORD_TOKEN(Final, "final")
  174. CARBON_KEYWORD_TOKEN(For, "for")
  175. CARBON_KEYWORD_TOKEN(Form, "form")
  176. CARBON_KEYWORD_TOKEN(Forall, "forall")
  177. CARBON_KEYWORD_TOKEN(Friend, "friend")
  178. CARBON_KEYWORD_TOKEN(If, "if")
  179. CARBON_KEYWORD_TOKEN(Impls, "impls")
  180. CARBON_KEYWORD_TOKEN(In, "in")
  181. CARBON_KEYWORD_TOKEN(Inline, "inline")
  182. CARBON_KEYWORD_TOKEN(Like, "like")
  183. CARBON_KEYWORD_TOKEN(Match, "match")
  184. CARBON_KEYWORD_TOKEN(MustEval, "musteval")
  185. CARBON_KEYWORD_TOKEN(Not, "not")
  186. CARBON_KEYWORD_TOKEN(Observe, "observe")
  187. CARBON_TOKEN_WITH_VIRTUAL_NODE(
  188. CARBON_KEYWORD_TOKEN(Or, "or"))
  189. CARBON_KEYWORD_TOKEN(Override, "override")
  190. CARBON_KEYWORD_TOKEN(Partial, "partial")
  191. CARBON_KEYWORD_TOKEN(Private, "private")
  192. CARBON_KEYWORD_TOKEN(Protected, "protected")
  193. CARBON_KEYWORD_TOKEN(Ref, "ref")
  194. CARBON_KEYWORD_TOKEN(Return, "return")
  195. CARBON_KEYWORD_TOKEN(Returned, "returned")
  196. CARBON_KEYWORD_TOKEN(SelfTypeIdentifier, "Self")
  197. CARBON_KEYWORD_TOKEN(SelfValueIdentifier, "self")
  198. // TODO: Although we provide a `str` type literal, it's not standardized.
  199. CARBON_KEYWORD_TOKEN(Str, "str")
  200. CARBON_KEYWORD_TOKEN(Template, "template")
  201. CARBON_KEYWORD_TOKEN(Then, "then")
  202. CARBON_KEYWORD_TOKEN(True, "true")
  203. CARBON_KEYWORD_TOKEN(Type, "type")
  204. // Underscore is tokenized as a keyword because it's part of identifiers.
  205. CARBON_KEYWORD_TOKEN(Underscore, "_")
  206. CARBON_KEYWORD_TOKEN(Unsafe, "unsafe")
  207. CARBON_KEYWORD_TOKEN(Unused, "unused")
  208. CARBON_KEYWORD_TOKEN(Virtual, "virtual")
  209. CARBON_KEYWORD_TOKEN(Val, "val")
  210. CARBON_TOKEN_WITH_VIRTUAL_NODE(
  211. CARBON_KEYWORD_TOKEN(Where, "where"))
  212. CARBON_KEYWORD_TOKEN(While, "while")
  213. // clang-format on
  214. #undef CARBON_DECL_INTRODUCER_TOKEN
  215. #undef CARBON_KEYWORD_TOKEN
  216. CARBON_TOKEN(Identifier)
  217. CARBON_TOKEN(IntLiteral)
  218. CARBON_TOKEN(RealLiteral)
  219. CARBON_TOKEN(StringLiteral)
  220. CARBON_TOKEN(CharLiteral)
  221. CARBON_TOKEN(IntTypeLiteral)
  222. CARBON_TOKEN(UnsignedIntTypeLiteral)
  223. CARBON_TOKEN(FloatTypeLiteral)
  224. CARBON_TOKEN(FileStart)
  225. CARBON_TOKEN(FileEnd)
  226. #undef CARBON_TOKEN
  227. #undef CARBON_TOKEN_WITH_VIRTUAL_NODE