parse_tree_test.cpp 4.3 KB

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