parse_tree_test.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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, IsValid) {
  33. TokenizedBuffer tokens = GetTokenizedBuffer("");
  34. ParseTree tree = ParseTree::Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  35. EXPECT_TRUE((*tree.postorder().begin()).is_valid());
  36. }
  37. TEST_F(ParseTreeTest, PrintPostorderAsYAML) {
  38. TokenizedBuffer tokens = GetTokenizedBuffer("fn F();");
  39. ParseTree tree = ParseTree::Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  40. EXPECT_FALSE(tree.has_errors());
  41. std::string print_output;
  42. llvm::raw_string_ostream print_stream(print_output);
  43. tree.Print(print_stream);
  44. print_stream.flush();
  45. auto file = Yaml::SequenceValue{
  46. Yaml::MappingValue{{"kind", "FunctionIntroducer"}, {"text", "fn"}},
  47. Yaml::MappingValue{{"kind", "DeclaredName"}, {"text", "F"}},
  48. Yaml::MappingValue{{"kind", "ParameterListStart"}, {"text", "("}},
  49. Yaml::MappingValue{
  50. {"kind", "ParameterList"}, {"text", ")"}, {"subtree_size", "2"}},
  51. Yaml::MappingValue{{"kind", "FunctionDeclaration"},
  52. {"text", ";"},
  53. {"subtree_size", "5"}},
  54. Yaml::MappingValue{{"kind", "FileEnd"}, {"text", ""}},
  55. };
  56. EXPECT_THAT(Yaml::Value::FromText(print_output), ElementsAre(file));
  57. }
  58. TEST_F(ParseTreeTest, PrintPreorderAsYAML) {
  59. TokenizedBuffer tokens = GetTokenizedBuffer("fn F();");
  60. ParseTree tree = ParseTree::Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  61. EXPECT_FALSE(tree.has_errors());
  62. std::string print_output;
  63. llvm::raw_string_ostream print_stream(print_output);
  64. tree.Print(print_stream, /*preorder=*/true);
  65. print_stream.flush();
  66. auto parameter_list = Yaml::SequenceValue{
  67. Yaml::MappingValue{
  68. {"node_index", "2"}, {"kind", "ParameterListStart"}, {"text", "("}},
  69. };
  70. auto function_decl = Yaml::SequenceValue{
  71. Yaml::MappingValue{
  72. {"node_index", "0"}, {"kind", "FunctionIntroducer"}, {"text", "fn"}},
  73. Yaml::MappingValue{
  74. {"node_index", "1"}, {"kind", "DeclaredName"}, {"text", "F"}},
  75. Yaml::MappingValue{{"node_index", "3"},
  76. {"kind", "ParameterList"},
  77. {"text", ")"},
  78. {"subtree_size", "2"},
  79. {"children", parameter_list}},
  80. };
  81. auto file = Yaml::SequenceValue{
  82. Yaml::MappingValue{{"node_index", "4"},
  83. {"kind", "FunctionDeclaration"},
  84. {"text", ";"},
  85. {"subtree_size", "5"},
  86. {"children", function_decl}},
  87. Yaml::MappingValue{
  88. {"node_index", "5"}, {"kind", "FileEnd"}, {"text", ""}},
  89. };
  90. EXPECT_THAT(Yaml::Value::FromText(print_output), ElementsAre(file));
  91. }
  92. TEST_F(ParseTreeTest, HighRecursion) {
  93. std::string code = "fn Foo() { return ";
  94. code.append(10000, '(');
  95. code.append(10000, ')');
  96. code += "; }";
  97. TokenizedBuffer tokens = GetTokenizedBuffer(code);
  98. ASSERT_FALSE(tokens.has_errors());
  99. Testing::MockDiagnosticConsumer consumer;
  100. ParseTree tree = ParseTree::Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  101. EXPECT_FALSE(tree.has_errors());
  102. }
  103. } // namespace
  104. } // namespace Carbon::Testing