parse_tree_test.cpp 4.4 KB

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