parse_tree_test.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <forward_list>
  8. #include "llvm/Support/FormatVariadic.h"
  9. #include "toolchain/common/yaml_test_helpers.h"
  10. #include "toolchain/diagnostics/diagnostic_emitter.h"
  11. #include "toolchain/diagnostics/mocks.h"
  12. #include "toolchain/lexer/tokenized_buffer.h"
  13. namespace Carbon::Testing {
  14. namespace {
  15. using ::testing::AtLeast;
  16. using ::testing::ElementsAre;
  17. using ::testing::Eq;
  18. class ParseTreeTest : public ::testing::Test {
  19. protected:
  20. auto GetSourceBuffer(llvm::Twine t) -> SourceBuffer& {
  21. source_storage.push_front(
  22. std::move(*SourceBuffer::CreateFromText(t.str())));
  23. return source_storage.front();
  24. }
  25. auto GetTokenizedBuffer(llvm::Twine t) -> TokenizedBuffer& {
  26. token_storage.push_front(
  27. TokenizedBuffer::Lex(GetSourceBuffer(t), consumer));
  28. return token_storage.front();
  29. }
  30. std::forward_list<SourceBuffer> source_storage;
  31. std::forward_list<TokenizedBuffer> token_storage;
  32. DiagnosticConsumer& consumer = ConsoleDiagnosticConsumer();
  33. };
  34. TEST_F(ParseTreeTest, DefaultInvalid) {
  35. ParseTree::Node node;
  36. EXPECT_FALSE(node.is_valid());
  37. }
  38. TEST_F(ParseTreeTest, IsValid) {
  39. TokenizedBuffer tokens = GetTokenizedBuffer("");
  40. ParseTree tree = ParseTree::Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  41. EXPECT_TRUE((*tree.postorder().begin()).is_valid());
  42. }
  43. TEST_F(ParseTreeTest, PrintPostorderAsYAML) {
  44. TokenizedBuffer tokens = GetTokenizedBuffer("fn F();");
  45. ParseTree tree = ParseTree::Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  46. EXPECT_FALSE(tree.has_errors());
  47. std::string print_output;
  48. llvm::raw_string_ostream print_stream(print_output);
  49. tree.Print(print_stream);
  50. print_stream.flush();
  51. auto file = Yaml::SequenceValue{
  52. Yaml::MappingValue{{"kind", "FunctionIntroducer"}, {"text", "fn"}},
  53. Yaml::MappingValue{{"kind", "DeclaredName"}, {"text", "F"}},
  54. Yaml::MappingValue{{"kind", "ParameterListStart"}, {"text", "("}},
  55. Yaml::MappingValue{
  56. {"kind", "ParameterList"}, {"text", ")"}, {"subtree_size", "2"}},
  57. Yaml::MappingValue{{"kind", "FunctionDeclaration"},
  58. {"text", ";"},
  59. {"subtree_size", "5"}},
  60. Yaml::MappingValue{{"kind", "FileEnd"}, {"text", ""}},
  61. };
  62. EXPECT_THAT(Yaml::Value::FromText(print_output), ElementsAre(file));
  63. }
  64. TEST_F(ParseTreeTest, PrintPreorderAsYAML) {
  65. TokenizedBuffer tokens = GetTokenizedBuffer("fn F();");
  66. ParseTree tree = ParseTree::Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  67. EXPECT_FALSE(tree.has_errors());
  68. std::string print_output;
  69. llvm::raw_string_ostream print_stream(print_output);
  70. tree.Print(print_stream, /*preorder=*/true);
  71. print_stream.flush();
  72. auto parameter_list = Yaml::SequenceValue{
  73. Yaml::MappingValue{
  74. {"node_index", "2"}, {"kind", "ParameterListStart"}, {"text", "("}},
  75. };
  76. auto function_decl = Yaml::SequenceValue{
  77. Yaml::MappingValue{
  78. {"node_index", "0"}, {"kind", "FunctionIntroducer"}, {"text", "fn"}},
  79. Yaml::MappingValue{
  80. {"node_index", "1"}, {"kind", "DeclaredName"}, {"text", "F"}},
  81. Yaml::MappingValue{{"node_index", "3"},
  82. {"kind", "ParameterList"},
  83. {"text", ")"},
  84. {"subtree_size", "2"},
  85. {"children", parameter_list}},
  86. };
  87. auto file = Yaml::SequenceValue{
  88. Yaml::MappingValue{{"node_index", "4"},
  89. {"kind", "FunctionDeclaration"},
  90. {"text", ";"},
  91. {"subtree_size", "5"},
  92. {"children", function_decl}},
  93. Yaml::MappingValue{
  94. {"node_index", "5"}, {"kind", "FileEnd"}, {"text", ""}},
  95. };
  96. EXPECT_THAT(Yaml::Value::FromText(print_output), ElementsAre(file));
  97. }
  98. TEST_F(ParseTreeTest, HighRecursion) {
  99. std::string code = "fn Foo() { return ";
  100. code.append(10000, '(');
  101. code.append(10000, ')');
  102. code += "; }";
  103. TokenizedBuffer tokens = GetTokenizedBuffer(code);
  104. ASSERT_FALSE(tokens.has_errors());
  105. Testing::MockDiagnosticConsumer consumer;
  106. ParseTree tree = ParseTree::Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  107. EXPECT_FALSE(tree.has_errors());
  108. }
  109. } // namespace
  110. } // namespace Carbon::Testing