parse_tree.cpp 7.0 KB

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