token_kind.def 9.3 KB

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