parse_tree_test.cpp 4.4 KB

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