node_kind.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/parse/node_kind.h"
  5. #include "common/check.h"
  6. #include "llvm/ADT/StringExtras.h"
  7. #include "toolchain/parse/typed_nodes.h"
  8. namespace Carbon::Parse {
  9. auto operator<<(llvm::raw_ostream& output, NodeCategory category)
  10. -> llvm::raw_ostream& {
  11. if (!category) {
  12. output << "<none>";
  13. } else {
  14. llvm::ListSeparator sep("|");
  15. #define CARBON_NODE_CATEGORY(Name) \
  16. if (!!(category & NodeCategory::Name)) { \
  17. output << sep << #Name; \
  18. }
  19. CARBON_NODE_CATEGORY(Decl);
  20. CARBON_NODE_CATEGORY(Expr);
  21. CARBON_NODE_CATEGORY(ImplAs);
  22. CARBON_NODE_CATEGORY(MemberExpr);
  23. CARBON_NODE_CATEGORY(MemberName);
  24. CARBON_NODE_CATEGORY(Modifier);
  25. CARBON_NODE_CATEGORY(Pattern);
  26. CARBON_NODE_CATEGORY(Statement);
  27. #undef CARBON_NODE_CATEGORY
  28. }
  29. return output;
  30. }
  31. CARBON_DEFINE_ENUM_CLASS_NAMES(NodeKind) = {
  32. #define CARBON_PARSE_NODE_KIND(Name) CARBON_ENUM_CLASS_NAME_STRING(Name)
  33. #include "toolchain/parse/node_kind.def"
  34. };
  35. auto NodeKind::CheckMatchesTokenKind(Lex::TokenKind token_kind, bool has_error)
  36. -> void {
  37. static constexpr Lex::TokenKind TokenIfValid[] = {
  38. #define CARBON_IF_VALID(LexTokenKind) LexTokenKind
  39. #define CARBON_PARSE_NODE_KIND_WITH_TOKEN(Name, LexTokenKind) \
  40. Lex::TokenKind::LexTokenKind,
  41. #include "toolchain/parse/node_kind.def"
  42. };
  43. static constexpr Lex::TokenKind TokenIfError[] = {
  44. #define CARBON_IF_VALID(LexTokenKind) Error
  45. #define CARBON_PARSE_NODE_KIND_WITH_TOKEN(Name, LexTokenKind) \
  46. Lex::TokenKind::LexTokenKind,
  47. #include "toolchain/parse/node_kind.def"
  48. };
  49. Lex::TokenKind expected_token_kind =
  50. has_error ? TokenIfError[AsInt()] : TokenIfValid[AsInt()];
  51. // Error indicates that the kind shouldn't be enforced.
  52. CARBON_CHECK(Lex::TokenKind::Error == expected_token_kind ||
  53. token_kind == expected_token_kind)
  54. << "Created parse node with NodeKind " << *this << " and has_error "
  55. << has_error << " for lexical token kind " << token_kind
  56. << ", but expected token kind " << expected_token_kind;
  57. }
  58. auto NodeKind::has_bracket() const -> bool {
  59. return definition().has_bracket();
  60. }
  61. auto NodeKind::bracket() const -> NodeKind { return definition().bracket(); }
  62. auto NodeKind::has_child_count() const -> bool {
  63. return definition().has_child_count();
  64. }
  65. auto NodeKind::child_count() const -> int32_t {
  66. return definition().child_count();
  67. }
  68. auto NodeKind::category() const -> NodeCategory {
  69. return definition().category();
  70. }
  71. auto NodeKind::definition() const -> const Definition& {
  72. static constexpr const Definition* Table[] = {
  73. #define CARBON_PARSE_NODE_KIND(Name) &Parse::Name::Kind,
  74. #include "toolchain/parse/node_kind.def"
  75. };
  76. return *Table[AsInt()];
  77. }
  78. } // namespace Carbon::Parse