token_kind.def 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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_TOKEN_WITH_VIRTUAL_NODE(Token)
  25. // Wrapped around one of the abokve _TOKEN macros, indicates that this
  26. // token has one additional virtual node in the parse tree.
  27. //
  28. // This tree represents the subset relationship between these macros, where if a
  29. // specific x-macro isn't defined, it'll fall back to the parent macro.
  30. #ifndef CARBON_TOKEN
  31. #define CARBON_TOKEN(Name)
  32. #endif
  33. #ifndef CARBON_SYMBOL_TOKEN
  34. #define CARBON_SYMBOL_TOKEN(Name, Spelling) CARBON_TOKEN(Name)
  35. #endif
  36. #ifndef CARBON_TOKEN_WITH_VIRTUAL_NODE
  37. #define CARBON_TOKEN_WITH_VIRTUAL_NODE(Name) Name
  38. #endif
  39. // Note that symbols need to be ordered from longest to shortest to effectively
  40. // provide max-munch lexing.
  41. // clang-format off
  42. CARBON_SYMBOL_TOKEN(GreaterGreaterEqual, ">>=")
  43. CARBON_SYMBOL_TOKEN(LessEqualGreater, "<=>")
  44. CARBON_SYMBOL_TOKEN(LessLessEqual, "<<=")
  45. CARBON_SYMBOL_TOKEN(AmpEqual, "&=")
  46. CARBON_SYMBOL_TOKEN(CaretEqual, "^=")
  47. CARBON_SYMBOL_TOKEN(ColonEqual, ":=")
  48. CARBON_SYMBOL_TOKEN(ColonExclaim, ":!")
  49. CARBON_SYMBOL_TOKEN(EqualEqual, "==")
  50. CARBON_SYMBOL_TOKEN(EqualGreater, "=>")
  51. CARBON_SYMBOL_TOKEN(ExclaimEqual, "!=")
  52. CARBON_SYMBOL_TOKEN(GreaterEqual, ">=")
  53. CARBON_SYMBOL_TOKEN(GreaterGreater, ">>")
  54. CARBON_SYMBOL_TOKEN(LessEqual, "<=")
  55. CARBON_SYMBOL_TOKEN(LessGreater, "<>")
  56. CARBON_SYMBOL_TOKEN(LessLess, "<<")
  57. CARBON_SYMBOL_TOKEN(LessMinus, "<-")
  58. CARBON_SYMBOL_TOKEN(MinusEqual, "-=")
  59. CARBON_SYMBOL_TOKEN(MinusGreater, "->")
  60. CARBON_SYMBOL_TOKEN(MinusMinus, "--")
  61. CARBON_SYMBOL_TOKEN(PercentEqual, "%=")
  62. CARBON_SYMBOL_TOKEN(PipeEqual, "|=")
  63. CARBON_SYMBOL_TOKEN(PlusEqual, "+=")
  64. CARBON_SYMBOL_TOKEN(PlusPlus, "++")
  65. CARBON_SYMBOL_TOKEN(SlashEqual, "/=")
  66. CARBON_SYMBOL_TOKEN(StarEqual, "*=")
  67. CARBON_SYMBOL_TOKEN(TildeEqual, "~=")
  68. CARBON_SYMBOL_TOKEN(Amp, "&")
  69. CARBON_SYMBOL_TOKEN(At, "@")
  70. CARBON_SYMBOL_TOKEN(Backslash, "\\")
  71. CARBON_SYMBOL_TOKEN(Caret, "^")
  72. CARBON_SYMBOL_TOKEN(Colon, ":")
  73. CARBON_SYMBOL_TOKEN(Comma, ",")
  74. CARBON_SYMBOL_TOKEN(Equal, "=")
  75. CARBON_SYMBOL_TOKEN(Exclaim, "!")
  76. CARBON_SYMBOL_TOKEN(Greater, ">")
  77. CARBON_SYMBOL_TOKEN(Less, "<")
  78. CARBON_SYMBOL_TOKEN(Minus, "-")
  79. CARBON_SYMBOL_TOKEN(Percent, "%")
  80. CARBON_SYMBOL_TOKEN(Period, ".")
  81. CARBON_SYMBOL_TOKEN(Pipe, "|")
  82. CARBON_SYMBOL_TOKEN(Plus, "+")
  83. CARBON_SYMBOL_TOKEN(Question, "?")
  84. CARBON_SYMBOL_TOKEN(Semi, ";")
  85. CARBON_SYMBOL_TOKEN(Slash, "/")
  86. CARBON_SYMBOL_TOKEN(Star, "*")
  87. CARBON_SYMBOL_TOKEN(Tilde, "~")
  88. // clang-format on
  89. #ifndef CARBON_OPENING_GROUP_SYMBOL_TOKEN
  90. #define CARBON_OPENING_GROUP_SYMBOL_TOKEN(Name, Spelling, ClosingName) \
  91. CARBON_SYMBOL_TOKEN(Name, Spelling)
  92. #endif
  93. CARBON_OPENING_GROUP_SYMBOL_TOKEN(OpenParen, "(", CloseParen)
  94. CARBON_OPENING_GROUP_SYMBOL_TOKEN(OpenCurlyBrace, "{", CloseCurlyBrace)
  95. CARBON_OPENING_GROUP_SYMBOL_TOKEN(OpenSquareBracket, "[", CloseSquareBracket)
  96. #undef CARBON_OPENING_GROUP_SYMBOL_TOKEN
  97. #ifndef CARBON_CLOSING_GROUP_SYMBOL_TOKEN
  98. #define CARBON_CLOSING_GROUP_SYMBOL_TOKEN(Name, Spelling, OpeningName) \
  99. CARBON_SYMBOL_TOKEN(Name, Spelling)
  100. #endif
  101. CARBON_CLOSING_GROUP_SYMBOL_TOKEN(CloseParen, ")", OpenParen)
  102. CARBON_CLOSING_GROUP_SYMBOL_TOKEN(CloseCurlyBrace, "}", OpenCurlyBrace)
  103. CARBON_CLOSING_GROUP_SYMBOL_TOKEN(CloseSquareBracket, "]", OpenSquareBracket)
  104. #undef CARBON_CLOSING_GROUP_SYMBOL_TOKEN
  105. #undef CARBON_SYMBOL_TOKEN
  106. #ifndef CARBON_KEYWORD_TOKEN
  107. #define CARBON_KEYWORD_TOKEN(Name, Spelling) CARBON_TOKEN(Name)
  108. #endif
  109. // clang-format off
  110. CARBON_KEYWORD_TOKEN(Abstract, "abstract")
  111. CARBON_KEYWORD_TOKEN(Adapt, "adapt")
  112. CARBON_KEYWORD_TOKEN(Addr, "addr")
  113. CARBON_KEYWORD_TOKEN(Alias, "alias")
  114. CARBON_TOKEN_WITH_VIRTUAL_NODE(
  115. CARBON_KEYWORD_TOKEN(And, "and"))
  116. CARBON_KEYWORD_TOKEN(Api, "api")
  117. CARBON_KEYWORD_TOKEN(As, "as")
  118. CARBON_KEYWORD_TOKEN(Auto, "auto")
  119. CARBON_KEYWORD_TOKEN(Base, "base")
  120. CARBON_KEYWORD_TOKEN(Bool, "bool")
  121. CARBON_KEYWORD_TOKEN(Break, "break")
  122. CARBON_KEYWORD_TOKEN(Case, "case")
  123. CARBON_KEYWORD_TOKEN(Choice, "choice")
  124. CARBON_KEYWORD_TOKEN(Class, "class")
  125. CARBON_KEYWORD_TOKEN(Const, "const")
  126. CARBON_KEYWORD_TOKEN(Constraint, "constraint")
  127. CARBON_KEYWORD_TOKEN(Continue, "continue")
  128. CARBON_KEYWORD_TOKEN(Default, "default")
  129. CARBON_KEYWORD_TOKEN(Destructor, "destructor")
  130. CARBON_KEYWORD_TOKEN(Else, "else")
  131. CARBON_KEYWORD_TOKEN(Extend, "extend")
  132. CARBON_KEYWORD_TOKEN(False, "false")
  133. CARBON_KEYWORD_TOKEN(Final, "final")
  134. CARBON_KEYWORD_TOKEN(Fn, "fn")
  135. CARBON_KEYWORD_TOKEN(For, "for")
  136. CARBON_KEYWORD_TOKEN(Forall, "forall")
  137. CARBON_KEYWORD_TOKEN(Friend, "friend")
  138. CARBON_KEYWORD_TOKEN(If, "if")
  139. CARBON_KEYWORD_TOKEN(Impl, "impl")
  140. CARBON_KEYWORD_TOKEN(Impls, "impls")
  141. CARBON_KEYWORD_TOKEN(Import, "import")
  142. CARBON_KEYWORD_TOKEN(In, "in")
  143. CARBON_KEYWORD_TOKEN(Interface, "interface")
  144. CARBON_KEYWORD_TOKEN(Let, "let")
  145. CARBON_KEYWORD_TOKEN(Library, "library")
  146. CARBON_KEYWORD_TOKEN(Like, "like")
  147. CARBON_KEYWORD_TOKEN(Match, "match")
  148. CARBON_KEYWORD_TOKEN(Namespace, "namespace")
  149. CARBON_KEYWORD_TOKEN(Not, "not")
  150. CARBON_KEYWORD_TOKEN(Observe, "observe")
  151. CARBON_TOKEN_WITH_VIRTUAL_NODE(
  152. CARBON_KEYWORD_TOKEN(Or, "or"))
  153. CARBON_KEYWORD_TOKEN(Override, "override")
  154. CARBON_KEYWORD_TOKEN(Package, "package")
  155. CARBON_KEYWORD_TOKEN(Partial, "partial")
  156. CARBON_KEYWORD_TOKEN(Private, "private")
  157. CARBON_KEYWORD_TOKEN(Protected, "protected")
  158. CARBON_KEYWORD_TOKEN(Require, "require")
  159. CARBON_KEYWORD_TOKEN(Return, "return")
  160. CARBON_KEYWORD_TOKEN(Returned, "returned")
  161. CARBON_KEYWORD_TOKEN(SelfTypeIdentifier, "Self")
  162. CARBON_KEYWORD_TOKEN(SelfValueIdentifier, "self")
  163. // TODO: Although we provide a String type literal, it's not standardized.
  164. CARBON_KEYWORD_TOKEN(StringTypeLiteral, "String")
  165. CARBON_KEYWORD_TOKEN(Template, "template")
  166. CARBON_KEYWORD_TOKEN(Then, "then")
  167. CARBON_KEYWORD_TOKEN(True, "true")
  168. CARBON_KEYWORD_TOKEN(Type, "type")
  169. // Underscore is tokenized as a keyword because it's part of identifiers.
  170. CARBON_KEYWORD_TOKEN(Underscore, "_")
  171. CARBON_KEYWORD_TOKEN(Var, "var")
  172. CARBON_KEYWORD_TOKEN(Virtual, "virtual")
  173. CARBON_KEYWORD_TOKEN(Where, "where")
  174. CARBON_KEYWORD_TOKEN(While, "while")
  175. // clang-format on
  176. #undef CARBON_KEYWORD_TOKEN
  177. CARBON_TOKEN(Identifier)
  178. CARBON_TOKEN(IntegerLiteral)
  179. CARBON_TOKEN(RealLiteral)
  180. CARBON_TOKEN(StringLiteral)
  181. CARBON_TOKEN(IntegerTypeLiteral)
  182. CARBON_TOKEN(UnsignedIntegerTypeLiteral)
  183. CARBON_TOKEN(FloatingPointTypeLiteral)
  184. CARBON_TOKEN(Error)
  185. CARBON_TOKEN(EndOfFile)
  186. #undef CARBON_TOKEN
  187. #undef CARBON_TOKEN_WITH_VIRTUAL_NODE