token_kind.def 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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(AmpEqual, "&=")
  55. CARBON_SYMBOL_TOKEN(CaretEqual, "^=")
  56. CARBON_SYMBOL_TOKEN(ColonEqual, ":=")
  57. CARBON_TOKEN_WITH_VIRTUAL_NODE(
  58. CARBON_SYMBOL_TOKEN(ColonExclaim, ":!"))
  59. CARBON_SYMBOL_TOKEN(EqualEqual, "==")
  60. CARBON_SYMBOL_TOKEN(EqualGreater, "=>")
  61. CARBON_SYMBOL_TOKEN(ExclaimEqual, "!=")
  62. CARBON_SYMBOL_TOKEN(GreaterEqual, ">=")
  63. CARBON_SYMBOL_TOKEN(GreaterGreater, ">>")
  64. CARBON_SYMBOL_TOKEN(LessEqual, "<=")
  65. CARBON_SYMBOL_TOKEN(LessGreater, "<>")
  66. CARBON_SYMBOL_TOKEN(LessLess, "<<")
  67. CARBON_SYMBOL_TOKEN(LessMinus, "<-")
  68. CARBON_SYMBOL_TOKEN(MinusEqual, "-=")
  69. CARBON_SYMBOL_TOKEN(MinusGreater, "->")
  70. CARBON_SYMBOL_TOKEN(MinusMinus, "--")
  71. CARBON_SYMBOL_TOKEN(PercentEqual, "%=")
  72. CARBON_SYMBOL_TOKEN(PipeEqual, "|=")
  73. CARBON_SYMBOL_TOKEN(PlusEqual, "+=")
  74. CARBON_SYMBOL_TOKEN(PlusPlus, "++")
  75. CARBON_SYMBOL_TOKEN(SlashEqual, "/=")
  76. CARBON_SYMBOL_TOKEN(StarEqual, "*=")
  77. CARBON_SYMBOL_TOKEN(TildeEqual, "~=")
  78. CARBON_SYMBOL_TOKEN(Amp, "&")
  79. CARBON_SYMBOL_TOKEN(At, "@")
  80. CARBON_SYMBOL_TOKEN(Backslash, "\\")
  81. CARBON_SYMBOL_TOKEN(Caret, "^")
  82. CARBON_SYMBOL_TOKEN(Colon, ":")
  83. CARBON_SYMBOL_TOKEN(Equal, "=")
  84. CARBON_SYMBOL_TOKEN(Exclaim, "!")
  85. CARBON_SYMBOL_TOKEN(Greater, ">")
  86. CARBON_SYMBOL_TOKEN(Less, "<")
  87. CARBON_SYMBOL_TOKEN(Minus, "-")
  88. CARBON_SYMBOL_TOKEN(Percent, "%")
  89. CARBON_SYMBOL_TOKEN(Period, ".")
  90. CARBON_SYMBOL_TOKEN(Pipe, "|")
  91. CARBON_SYMBOL_TOKEN(Plus, "+")
  92. CARBON_SYMBOL_TOKEN(Question, "?")
  93. CARBON_SYMBOL_TOKEN(Slash, "/")
  94. CARBON_SYMBOL_TOKEN(Star, "*")
  95. CARBON_SYMBOL_TOKEN(Tilde, "~")
  96. // Some Carbon symbols are constructively exactly one character and cannot be
  97. // combined with any other characters to form new symbols. We can lex these
  98. // without needing to max-munch any other characters. These are typically
  99. // expected to be terminators or separators that need to compose with all other
  100. // parts of the grammar. Group symbols are also currently one-character symbols,
  101. // although we may choose to remove that if we need to add composite grouping
  102. // symbols in the future.
  103. CARBON_ONE_CHAR_SYMBOL_TOKEN(Comma, ",")
  104. CARBON_ONE_CHAR_SYMBOL_TOKEN(Semi, ";")
  105. // clang-format on
  106. #ifndef CARBON_OPENING_GROUP_SYMBOL_TOKEN
  107. #define CARBON_OPENING_GROUP_SYMBOL_TOKEN(Name, Spelling, ClosingName) \
  108. CARBON_ONE_CHAR_SYMBOL_TOKEN(Name, Spelling)
  109. #endif
  110. CARBON_OPENING_GROUP_SYMBOL_TOKEN(OpenParen, "(", CloseParen)
  111. CARBON_OPENING_GROUP_SYMBOL_TOKEN(OpenCurlyBrace, "{", CloseCurlyBrace)
  112. CARBON_OPENING_GROUP_SYMBOL_TOKEN(OpenSquareBracket, "[", CloseSquareBracket)
  113. #undef CARBON_OPENING_GROUP_SYMBOL_TOKEN
  114. #ifndef CARBON_CLOSING_GROUP_SYMBOL_TOKEN
  115. #define CARBON_CLOSING_GROUP_SYMBOL_TOKEN(Name, Spelling, OpeningName) \
  116. CARBON_ONE_CHAR_SYMBOL_TOKEN(Name, Spelling)
  117. #endif
  118. CARBON_CLOSING_GROUP_SYMBOL_TOKEN(CloseParen, ")", OpenParen)
  119. CARBON_CLOSING_GROUP_SYMBOL_TOKEN(CloseCurlyBrace, "}", OpenCurlyBrace)
  120. CARBON_CLOSING_GROUP_SYMBOL_TOKEN(CloseSquareBracket, "]", OpenSquareBracket)
  121. #undef CARBON_CLOSING_GROUP_SYMBOL_TOKEN
  122. #undef CARBON_ONE_CHAR_SYMBOL_TOKEN
  123. #undef CARBON_SYMBOL_TOKEN
  124. #ifndef CARBON_KEYWORD_TOKEN
  125. #define CARBON_KEYWORD_TOKEN(Name, Spelling) CARBON_TOKEN(Name)
  126. #endif
  127. #ifndef CARBON_DECL_INTRODUCER_TOKEN
  128. #define CARBON_DECL_INTRODUCER_TOKEN(Name, Spelling) \
  129. CARBON_KEYWORD_TOKEN(Name, Spelling)
  130. #endif
  131. // clang-format off
  132. CARBON_DECL_INTRODUCER_TOKEN(Adapt, "adapt")
  133. CARBON_DECL_INTRODUCER_TOKEN(Alias, "alias")
  134. CARBON_DECL_INTRODUCER_TOKEN(Base, "base")
  135. CARBON_DECL_INTRODUCER_TOKEN(Choice, "choice")
  136. CARBON_DECL_INTRODUCER_TOKEN(Class, "class")
  137. CARBON_DECL_INTRODUCER_TOKEN(Constraint, "constraint")
  138. CARBON_DECL_INTRODUCER_TOKEN(Export, "export")
  139. CARBON_DECL_INTRODUCER_TOKEN(Fn, "fn")
  140. CARBON_DECL_INTRODUCER_TOKEN(Impl, "impl")
  141. CARBON_DECL_INTRODUCER_TOKEN(Import, "import")
  142. CARBON_DECL_INTRODUCER_TOKEN(Interface, "interface")
  143. CARBON_DECL_INTRODUCER_TOKEN(Let, "let")
  144. CARBON_DECL_INTRODUCER_TOKEN(Library, "library")
  145. CARBON_DECL_INTRODUCER_TOKEN(Namespace, "namespace")
  146. CARBON_DECL_INTRODUCER_TOKEN(Package, "package")
  147. CARBON_TOKEN_WITH_VIRTUAL_NODE(
  148. CARBON_DECL_INTRODUCER_TOKEN(Var, "var"))
  149. CARBON_KEYWORD_TOKEN(Abstract, "abstract")
  150. CARBON_KEYWORD_TOKEN(Addr, "addr")
  151. CARBON_TOKEN_WITH_VIRTUAL_NODE(
  152. CARBON_KEYWORD_TOKEN(And, "and"))
  153. CARBON_KEYWORD_TOKEN(Array, "array")
  154. CARBON_KEYWORD_TOKEN(As, "as")
  155. CARBON_KEYWORD_TOKEN(Auto, "auto")
  156. CARBON_KEYWORD_TOKEN(Bool, "bool")
  157. CARBON_KEYWORD_TOKEN(Break, "break")
  158. CARBON_KEYWORD_TOKEN(Case, "case")
  159. CARBON_KEYWORD_TOKEN(Const, "const")
  160. CARBON_KEYWORD_TOKEN(Continue, "continue")
  161. CARBON_KEYWORD_TOKEN(Core, "Core")
  162. CARBON_KEYWORD_TOKEN(Default, "default")
  163. CARBON_KEYWORD_TOKEN(Destroy, "destroy")
  164. CARBON_KEYWORD_TOKEN(Else, "else")
  165. CARBON_KEYWORD_TOKEN(Extend, "extend")
  166. CARBON_KEYWORD_TOKEN(Extern, "extern")
  167. CARBON_KEYWORD_TOKEN(False, "false")
  168. CARBON_KEYWORD_TOKEN(Final, "final")
  169. CARBON_KEYWORD_TOKEN(For, "for")
  170. CARBON_KEYWORD_TOKEN(Forall, "forall")
  171. CARBON_KEYWORD_TOKEN(Friend, "friend")
  172. CARBON_KEYWORD_TOKEN(If, "if")
  173. CARBON_KEYWORD_TOKEN(Impls, "impls")
  174. CARBON_KEYWORD_TOKEN(In, "in")
  175. CARBON_KEYWORD_TOKEN(Inline, "inline")
  176. CARBON_KEYWORD_TOKEN(Like, "like")
  177. CARBON_KEYWORD_TOKEN(Match, "match")
  178. CARBON_KEYWORD_TOKEN(Not, "not")
  179. CARBON_KEYWORD_TOKEN(Observe, "observe")
  180. CARBON_TOKEN_WITH_VIRTUAL_NODE(
  181. CARBON_KEYWORD_TOKEN(Or, "or"))
  182. CARBON_KEYWORD_TOKEN(Partial, "partial")
  183. CARBON_KEYWORD_TOKEN(Private, "private")
  184. CARBON_KEYWORD_TOKEN(Protected, "protected")
  185. CARBON_KEYWORD_TOKEN(Require, "require")
  186. CARBON_KEYWORD_TOKEN(Return, "return")
  187. CARBON_KEYWORD_TOKEN(Returned, "returned")
  188. CARBON_KEYWORD_TOKEN(SelfTypeIdentifier, "Self")
  189. CARBON_KEYWORD_TOKEN(SelfValueIdentifier, "self")
  190. // TODO: Although we provide a `str` type literal, it's not standardized.
  191. CARBON_KEYWORD_TOKEN(StringTypeLiteral, "str")
  192. CARBON_KEYWORD_TOKEN(Template, "template")
  193. CARBON_KEYWORD_TOKEN(Then, "then")
  194. CARBON_KEYWORD_TOKEN(True, "true")
  195. CARBON_KEYWORD_TOKEN(Type, "type")
  196. // Underscore is tokenized as a keyword because it's part of identifiers.
  197. CARBON_KEYWORD_TOKEN(Underscore, "_")
  198. CARBON_KEYWORD_TOKEN(Virtual, "virtual")
  199. CARBON_TOKEN_WITH_VIRTUAL_NODE(
  200. CARBON_KEYWORD_TOKEN(Where, "where"))
  201. CARBON_KEYWORD_TOKEN(While, "while")
  202. // clang-format on
  203. #undef CARBON_DECL_INTRODUCER_TOKEN
  204. #undef CARBON_KEYWORD_TOKEN
  205. CARBON_TOKEN(Identifier)
  206. CARBON_TOKEN(IntLiteral)
  207. CARBON_TOKEN(RealLiteral)
  208. CARBON_TOKEN(StringLiteral)
  209. CARBON_TOKEN(CharLiteral)
  210. CARBON_TOKEN(IntTypeLiteral)
  211. CARBON_TOKEN(UnsignedIntTypeLiteral)
  212. CARBON_TOKEN(FloatTypeLiteral)
  213. CARBON_TOKEN(FileStart)
  214. CARBON_TOKEN(FileEnd)
  215. #undef CARBON_TOKEN
  216. #undef CARBON_TOKEN_WITH_VIRTUAL_NODE