[toolchain] Add dump-parse-tree command to driver. (#584)

Also stop producing a redundant additional error message if lexing fails in dump-tokens. The lexer will have produced a diagnostic already.
This commit is contained in:
Richard Smith
2021-06-17 19:07:14 -07:00
committed by GitHub
parent 6a4c8a5186
commit d6b47ba10f
5 changed files with 102 additions and 5 deletions
+55
View File
@@ -179,5 +179,60 @@ TEST(DriverTest, DumpTokenErrors) {
EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
}
TEST(DriverTest, DumpParseTree) {
RawTestOstream test_output_stream;
RawTestOstream test_error_stream;
Driver driver = Driver(test_output_stream, test_error_stream);
auto test_file_path = CreateTestFile("var v: Int = 42;");
EXPECT_TRUE(driver.RunDumpParseTreeSubcommand({test_file_path}));
EXPECT_THAT(test_error_stream.TakeStr(), StrEq(""));
auto tokenized_text = test_output_stream.TakeStr();
EXPECT_THAT(
Yaml::Value::FromText(tokenized_text),
ElementsAre(Yaml::SequenceValue{
Yaml::MappingValue{
{"node_index", "6"},
{"kind", "VariableDeclaration"},
{"text", "var"},
{"subtree_size", "7"},
{"children",
Yaml::SequenceValue{
Yaml::MappingValue{
{"node_index", "2"},
{"kind", "PatternBinding"},
{"text", ":"},
{"subtree_size", "3"},
{"children",
Yaml::SequenceValue{
Yaml::MappingValue{{"node_index", "0"},
{"kind", "DeclaredName"},
{"text", "v"}},
Yaml::MappingValue{{"node_index", "1"},
{"kind", "NameReference"},
{"text", "Int"}}}}},
Yaml::MappingValue{{"node_index", "4"},
{"kind", "VariableInitializer"},
{"text", "="},
{"subtree_size", "2"},
{"children", //
Yaml::SequenceValue{Yaml::MappingValue{
{"node_index", "3"},
{"kind", "Literal"},
{"text", "42"}}}}},
Yaml::MappingValue{{"node_index", "5"},
{"kind", "DeclarationEnd"},
{"text", ";"}}}}},
Yaml::MappingValue{{"node_index", "7"}, //
{"kind", "FileEnd"},
{"text", ""}}}));
// Check that the subcommand dispatch works.
EXPECT_TRUE(driver.RunFullCommand({"dump-parse-tree", test_file_path}));
EXPECT_THAT(test_error_stream.TakeStr(), StrEq(""));
EXPECT_THAT(test_output_stream.TakeStr(), StrEq(tokenized_text));
}
} // namespace
} // namespace Carbon