parse_tree_test.cpp 4.2 KB

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