node_kind.cpp 2.8 KB

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