token_kind.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #include "toolchain/lexer/token_kind.h"
  5. #include "common/check.h"
  6. #include "llvm/ADT/StringRef.h"
  7. namespace Carbon {
  8. CARBON_DEFINE_ENUM_CLASS_NAMES(TokenKind) = {
  9. #define CARBON_TOKEN(TokenName) CARBON_ENUM_CLASS_NAME_STRING(TokenName)
  10. #include "toolchain/lexer/token_kind.def"
  11. };
  12. auto TokenKind::is_symbol() const -> bool {
  13. static constexpr bool Table[] = {
  14. #define CARBON_TOKEN(TokenName) false,
  15. #define CARBON_SYMBOL_TOKEN(TokenName, Spelling) true,
  16. #include "toolchain/lexer/token_kind.def"
  17. };
  18. return Table[AsInt()];
  19. }
  20. auto TokenKind::is_grouping_symbol() const -> bool {
  21. static constexpr bool Table[] = {
  22. #define CARBON_TOKEN(TokenName) false,
  23. #define CARBON_OPENING_GROUP_SYMBOL_TOKEN(TokenName, Spelling, ClosingName) \
  24. true,
  25. #define CARBON_CLOSING_GROUP_SYMBOL_TOKEN(TokenName, Spelling, OpeningName) \
  26. true,
  27. #include "toolchain/lexer/token_kind.def"
  28. };
  29. return Table[AsInt()];
  30. }
  31. auto TokenKind::is_opening_symbol() const -> bool {
  32. static constexpr bool Table[] = {
  33. #define CARBON_TOKEN(TokenName) false,
  34. #define CARBON_OPENING_GROUP_SYMBOL_TOKEN(TokenName, Spelling, ClosingName) \
  35. true,
  36. #include "toolchain/lexer/token_kind.def"
  37. };
  38. return Table[AsInt()];
  39. }
  40. auto TokenKind::closing_symbol() const -> TokenKind {
  41. static constexpr TokenKind Table[] = {
  42. #define CARBON_TOKEN(TokenName) Error,
  43. #define CARBON_OPENING_GROUP_SYMBOL_TOKEN(TokenName, Spelling, ClosingName) \
  44. ClosingName,
  45. #include "toolchain/lexer/token_kind.def"
  46. };
  47. auto result = Table[AsInt()];
  48. CARBON_CHECK(result != Error) << "Only opening symbols are valid!";
  49. return result;
  50. }
  51. auto TokenKind::is_closing_symbol() const -> bool {
  52. static constexpr bool Table[] = {
  53. #define CARBON_TOKEN(TokenName) false,
  54. #define CARBON_CLOSING_GROUP_SYMBOL_TOKEN(TokenName, Spelling, OpeningName) \
  55. true,
  56. #include "toolchain/lexer/token_kind.def"
  57. };
  58. return Table[AsInt()];
  59. }
  60. auto TokenKind::opening_symbol() const -> TokenKind {
  61. static constexpr TokenKind Table[] = {
  62. #define CARBON_TOKEN(TokenName) Error,
  63. #define CARBON_CLOSING_GROUP_SYMBOL_TOKEN(TokenName, Spelling, OpeningName) \
  64. OpeningName,
  65. #include "toolchain/lexer/token_kind.def"
  66. };
  67. auto result = Table[AsInt()];
  68. CARBON_CHECK(result != Error) << "Only closing symbols are valid!";
  69. return result;
  70. }
  71. auto TokenKind::is_one_char_symbol() const -> bool {
  72. static constexpr bool Table[] = {
  73. #define CARBON_TOKEN(TokenName) false,
  74. #define CARBON_ONE_CHAR_SYMBOL_TOKEN(TokenName, Spelling) true,
  75. #include "toolchain/lexer/token_kind.def"
  76. };
  77. return Table[AsInt()];
  78. }
  79. auto TokenKind::is_keyword() const -> bool {
  80. static constexpr bool Table[] = {
  81. #define CARBON_TOKEN(TokenName) false,
  82. #define CARBON_KEYWORD_TOKEN(TokenName, Spelling) true,
  83. #include "toolchain/lexer/token_kind.def"
  84. };
  85. return Table[AsInt()];
  86. }
  87. auto TokenKind::is_sized_type_literal() const -> bool {
  88. return *this == TokenKind::IntegerTypeLiteral ||
  89. *this == TokenKind::UnsignedIntegerTypeLiteral ||
  90. *this == TokenKind::FloatingPointTypeLiteral;
  91. }
  92. auto TokenKind::fixed_spelling() const -> llvm::StringRef {
  93. static constexpr llvm::StringLiteral Table[] = {
  94. #define CARBON_TOKEN(TokenName) "",
  95. #define CARBON_SYMBOL_TOKEN(TokenName, Spelling) Spelling,
  96. #define CARBON_KEYWORD_TOKEN(TokenName, Spelling) Spelling,
  97. #include "toolchain/lexer/token_kind.def"
  98. };
  99. return Table[AsInt()];
  100. }
  101. auto TokenKind::expected_parse_tree_size() const -> int {
  102. static constexpr int8_t Table[] = {
  103. #define CARBON_TOKEN(Name) 1,
  104. #define CARBON_TOKEN_WITH_VIRTUAL_NODE(size) 2,
  105. #include "toolchain/lexer/token_kind.def"
  106. };
  107. return Table[AsInt()];
  108. }
  109. } // namespace Carbon