parse_tree.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 "parser/parse_tree.h"
  5. #include <cstdlib>
  6. #include "lexer/token_kind.h"
  7. #include "llvm/ADT/ArrayRef.h"
  8. #include "llvm/ADT/Optional.h"
  9. #include "llvm/ADT/Sequence.h"
  10. #include "llvm/ADT/SmallSet.h"
  11. #include "llvm/ADT/SmallVector.h"
  12. #include "llvm/ADT/iterator.h"
  13. #include "llvm/Support/raw_ostream.h"
  14. #include "parser/parse_node_kind.h"
  15. #include "parser/parser_impl.h"
  16. namespace Carbon {
  17. auto ParseTree::Parse(TokenizedBuffer& tokens, DiagnosticEmitter& emitter)
  18. -> ParseTree {
  19. // Delegate to the parser.
  20. return Parser::Parse(tokens, emitter);
  21. }
  22. auto ParseTree::Postorder() const -> llvm::iterator_range<PostorderIterator> {
  23. return {PostorderIterator(Node(0)),
  24. PostorderIterator(Node(node_impls.size()))};
  25. }
  26. auto ParseTree::Postorder(Node n) const
  27. -> llvm::iterator_range<PostorderIterator> {
  28. // The postorder ends after this node, the root, and begins at the start of
  29. // its subtree.
  30. int end_index = n.index + 1;
  31. int start_index = end_index - node_impls[n.index].subtree_size;
  32. return {PostorderIterator(Node(start_index)),
  33. PostorderIterator(Node(end_index))};
  34. }
  35. auto ParseTree::Children(Node n) const
  36. -> llvm::iterator_range<SiblingIterator> {
  37. int end_index = n.index - node_impls[n.index].subtree_size;
  38. return {SiblingIterator(*this, Node(n.index - 1)),
  39. SiblingIterator(*this, Node(end_index))};
  40. }
  41. auto ParseTree::Roots() const -> llvm::iterator_range<SiblingIterator> {
  42. return {SiblingIterator(*this, Node(static_cast<int>(node_impls.size()) - 1)),
  43. SiblingIterator(*this, Node(-1))};
  44. }
  45. auto ParseTree::HasErrorInNode(Node n) const -> bool {
  46. return node_impls[n.index].has_error;
  47. }
  48. auto ParseTree::GetNodeKind(Node n) const -> ParseNodeKind {
  49. return node_impls[n.index].kind;
  50. }
  51. auto ParseTree::GetNodeToken(Node n) const -> TokenizedBuffer::Token {
  52. return node_impls[n.index].token;
  53. }
  54. auto ParseTree::GetNodeText(Node n) const -> llvm::StringRef {
  55. return tokens->GetTokenText(node_impls[n.index].token);
  56. }
  57. auto ParseTree::Print(llvm::raw_ostream& output) const -> void {
  58. output << "[\n";
  59. // The parse tree is stored in postorder, but the most natural order to
  60. // visualize is preorder. This is a tree, so the preorder can be constructed
  61. // by reversing the order of each level of siblings within an RPO. The sibling
  62. // iterators are directly built around RPO and so can be used with a stack to
  63. // produce preorder.
  64. // The roots, like siblings, are in RPO (so reversed), but we add them in
  65. // order here because we'll pop off the stack effectively reversing then.
  66. llvm::SmallVector<std::pair<Node, int>, 16> node_stack;
  67. for (Node n : Roots()) {
  68. node_stack.push_back({n, 0});
  69. }
  70. while (!node_stack.empty()) {
  71. Node n;
  72. int depth;
  73. std::tie(n, depth) = node_stack.pop_back_val();
  74. auto& n_impl = node_impls[n.GetIndex()];
  75. for (int unused_indent : llvm::seq(0, depth)) {
  76. (void)unused_indent;
  77. output << " ";
  78. }
  79. output << "{node_index: " << n.index << ", kind: '" << n_impl.kind.GetName()
  80. << "', text: '" << tokens->GetTokenText(n_impl.token) << "'";
  81. if (n_impl.has_error) {
  82. output << ", has_error: yes";
  83. }
  84. if (n_impl.subtree_size > 1) {
  85. output << ", subtree_size: " << n_impl.subtree_size;
  86. // Has children, so we descend.
  87. output << ", children: [\n";
  88. // We append the children in order here as well because they will get
  89. // reversed when popped off the stack.
  90. for (Node sibling_n : Children(n)) {
  91. node_stack.push_back({sibling_n, depth + 1});
  92. }
  93. continue;
  94. }
  95. // This node is finished, so close it up.
  96. assert(n_impl.subtree_size == 1 &&
  97. "Subtree size must always be a positive integer!");
  98. output << "}";
  99. int next_depth = node_stack.empty() ? 0 : node_stack.back().second;
  100. assert(next_depth <= depth && "Cannot have the next depth increase!");
  101. for (int close_children_count : llvm::seq(0, depth - next_depth)) {
  102. (void)close_children_count;
  103. output << "]}";
  104. }
  105. // We always end with a comma and a new line as we'll move to the next node
  106. // at whatever the current level ends up being.
  107. output << ",\n";
  108. }
  109. output << "]\n";
  110. }
  111. auto ParseTree::Verify() const -> bool {
  112. // Verify basic tree structure invariants.
  113. llvm::SmallVector<ParseTree::Node, 16> ancestors;
  114. for (Node n : llvm::reverse(Postorder())) {
  115. auto& n_impl = node_impls[n.GetIndex()];
  116. if (n_impl.has_error && !has_errors) {
  117. llvm::errs()
  118. << "Node #" << n.GetIndex()
  119. << " has errors, but the tree is not marked as having any.\n";
  120. return false;
  121. }
  122. if (n_impl.subtree_size > 1) {
  123. if (!ancestors.empty()) {
  124. auto parent_n = ancestors.back();
  125. auto& parent_n_impl = node_impls[parent_n.GetIndex()];
  126. int end_index = n.GetIndex() - n_impl.subtree_size;
  127. int parent_end_index = parent_n.GetIndex() - parent_n_impl.subtree_size;
  128. if (parent_end_index > end_index) {
  129. llvm::errs() << "Node #" << n.GetIndex() << " has a subtree size of "
  130. << n_impl.subtree_size
  131. << " which extends beyond its parent's (node #"
  132. << parent_n.GetIndex() << ") subtree (size "
  133. << parent_n_impl.subtree_size << ")\n";
  134. return false;
  135. }
  136. }
  137. // Has children, so we descend.
  138. ancestors.push_back(n);
  139. continue;
  140. }
  141. if (n_impl.subtree_size < 1) {
  142. llvm::errs() << "Node #" << n.GetIndex()
  143. << " has an invalid subtree size of " << n_impl.subtree_size
  144. << "!\n";
  145. return false;
  146. }
  147. // We're going to pop off some levels of the tree. Check each ancestor to
  148. // make sure the offsets are correct.
  149. int next_index = n.GetIndex() - 1;
  150. while (!ancestors.empty()) {
  151. ParseTree::Node parent_n = ancestors.back();
  152. if ((parent_n.GetIndex() -
  153. node_impls[parent_n.GetIndex()].subtree_size) != next_index) {
  154. break;
  155. }
  156. ancestors.pop_back();
  157. }
  158. }
  159. if (!ancestors.empty()) {
  160. llvm::errs()
  161. << "Finished walking the parse tree and there are still ancestors:\n";
  162. for (Node ancestor_n : ancestors) {
  163. llvm::errs() << " Node #" << ancestor_n.GetIndex() << "\n";
  164. }
  165. return false;
  166. }
  167. return true;
  168. }
  169. } // namespace Carbon