parse_tree_test.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 "parser/parse_tree.h"
  5. #include <forward_list>
  6. #include "diagnostics/diagnostic_emitter.h"
  7. #include "gmock/gmock.h"
  8. #include "gtest/gtest.h"
  9. #include "lexer/tokenized_buffer.h"
  10. #include "lexer/tokenized_buffer_test_helpers.h"
  11. #include "llvm/ADT/Sequence.h"
  12. #include "llvm/Support/SourceMgr.h"
  13. #include "llvm/Support/YAMLParser.h"
  14. #include "parser/parse_node_kind.h"
  15. #include "parser/parse_test_helpers.h"
  16. namespace Carbon {
  17. namespace {
  18. using Carbon::Testing::IsKeyValueScalars;
  19. using Carbon::Testing::MatchParseTreeNodes;
  20. using ::testing::Eq;
  21. using ::testing::Ne;
  22. using ::testing::NotNull;
  23. using ::testing::StrEq;
  24. struct ParseTreeTest : ::testing::Test {
  25. std::forward_list<SourceBuffer> source_storage;
  26. std::forward_list<TokenizedBuffer> token_storage;
  27. DiagnosticConsumer& consumer = ConsoleDiagnosticConsumer();
  28. auto GetSourceBuffer(llvm::Twine t) -> SourceBuffer& {
  29. source_storage.push_front(SourceBuffer::CreateFromText(t.str()));
  30. return source_storage.front();
  31. }
  32. auto GetTokenizedBuffer(llvm::Twine t) -> TokenizedBuffer& {
  33. token_storage.push_front(
  34. TokenizedBuffer::Lex(GetSourceBuffer(t), consumer));
  35. return token_storage.front();
  36. }
  37. };
  38. TEST_F(ParseTreeTest, Empty) {
  39. TokenizedBuffer tokens = GetTokenizedBuffer("");
  40. ParseTree tree = ParseTree::Parse(tokens, consumer);
  41. EXPECT_FALSE(tree.HasErrors());
  42. EXPECT_THAT(tree, MatchParseTreeNodes({{.kind = ParseNodeKind::FileEnd()}}));
  43. }
  44. TEST_F(ParseTreeTest, EmptyDeclaration) {
  45. TokenizedBuffer tokens = GetTokenizedBuffer(";");
  46. ParseTree tree = ParseTree::Parse(tokens, consumer);
  47. EXPECT_FALSE(tree.HasErrors());
  48. auto it = tree.Postorder().begin();
  49. auto end = tree.Postorder().end();
  50. ASSERT_THAT(it, Ne(end));
  51. ParseTree::Node n = *it++;
  52. ASSERT_THAT(it, Ne(end));
  53. ParseTree::Node eof = *it++;
  54. EXPECT_THAT(it, Eq(end));
  55. // Directly test the main API so that we get easier to understand errors in
  56. // simple cases than what the custom matcher will produce.
  57. EXPECT_FALSE(tree.HasErrorInNode(n));
  58. EXPECT_FALSE(tree.HasErrorInNode(eof));
  59. EXPECT_THAT(tree.GetNodeKind(n), Eq(ParseNodeKind::EmptyDeclaration()));
  60. EXPECT_THAT(tree.GetNodeKind(eof), Eq(ParseNodeKind::FileEnd()));
  61. auto t = tree.GetNodeToken(n);
  62. ASSERT_THAT(tokens.Tokens().begin(), Ne(tokens.Tokens().end()));
  63. EXPECT_THAT(t, Eq(*tokens.Tokens().begin()));
  64. EXPECT_THAT(tokens.GetTokenText(t), Eq(";"));
  65. EXPECT_THAT(tree.Children(n).begin(), Eq(tree.Children(n).end()));
  66. EXPECT_THAT(tree.Children(eof).begin(), Eq(tree.Children(eof).end()));
  67. EXPECT_THAT(tree.Postorder().begin(), Eq(tree.Postorder(n).begin()));
  68. EXPECT_THAT(tree.Postorder(n).end(), Eq(tree.Postorder(eof).begin()));
  69. EXPECT_THAT(tree.Postorder(eof).end(), Eq(tree.Postorder().end()));
  70. }
  71. TEST_F(ParseTreeTest, BasicFunctionDeclaration) {
  72. TokenizedBuffer tokens = GetTokenizedBuffer("fn F();");
  73. ParseTree tree = ParseTree::Parse(tokens, consumer);
  74. EXPECT_FALSE(tree.HasErrors());
  75. EXPECT_THAT(
  76. tree, MatchParseTreeNodes(
  77. {{.kind = ParseNodeKind::FunctionDeclaration(),
  78. .text = "fn",
  79. .children = {{ParseNodeKind::Identifier(), "F"},
  80. {.kind = ParseNodeKind::ParameterList(),
  81. .text = "(",
  82. .children = {{ParseNodeKind::ParameterListEnd(),
  83. ")"}}},
  84. {ParseNodeKind::DeclarationEnd(), ";"}}},
  85. {.kind = ParseNodeKind::FileEnd()}}));
  86. }
  87. TEST_F(ParseTreeTest, NoDeclarationIntroducerOrSemi) {
  88. TokenizedBuffer tokens = GetTokenizedBuffer("foo bar baz");
  89. ParseTree tree = ParseTree::Parse(tokens, consumer);
  90. EXPECT_TRUE(tree.HasErrors());
  91. EXPECT_THAT(tree, MatchParseTreeNodes({{.kind = ParseNodeKind::FileEnd()}}));
  92. }
  93. TEST_F(ParseTreeTest, NoDeclarationIntroducerWithSemi) {
  94. TokenizedBuffer tokens = GetTokenizedBuffer("foo;");
  95. ParseTree tree = ParseTree::Parse(tokens, consumer);
  96. EXPECT_TRUE(tree.HasErrors());
  97. EXPECT_THAT(tree,
  98. MatchParseTreeNodes({{.kind = ParseNodeKind::EmptyDeclaration(),
  99. .text = ";",
  100. .has_error = true},
  101. {.kind = ParseNodeKind::FileEnd()}}));
  102. }
  103. TEST_F(ParseTreeTest, JustFunctionIntroducerAndSemi) {
  104. TokenizedBuffer tokens = GetTokenizedBuffer("fn;");
  105. ParseTree tree = ParseTree::Parse(tokens, consumer);
  106. EXPECT_TRUE(tree.HasErrors());
  107. EXPECT_THAT(tree, MatchParseTreeNodes(
  108. {{.kind = ParseNodeKind::FunctionDeclaration(),
  109. .has_error = true,
  110. .children = {{ParseNodeKind::DeclarationEnd()}}},
  111. {.kind = ParseNodeKind::FileEnd()}}));
  112. }
  113. TEST_F(ParseTreeTest, RepeatedFunctionIntroducerAndSemi) {
  114. TokenizedBuffer tokens = GetTokenizedBuffer("fn fn;");
  115. ParseTree tree = ParseTree::Parse(tokens, consumer);
  116. EXPECT_TRUE(tree.HasErrors());
  117. EXPECT_THAT(tree, MatchParseTreeNodes(
  118. {{.kind = ParseNodeKind::FunctionDeclaration(),
  119. .has_error = true,
  120. .children = {{ParseNodeKind::DeclarationEnd()}}},
  121. {.kind = ParseNodeKind::FileEnd()}}));
  122. }
  123. TEST_F(ParseTreeTest, FunctionDeclarationWithNoSignatureOrSemi) {
  124. TokenizedBuffer tokens = GetTokenizedBuffer("fn foo");
  125. ParseTree tree = ParseTree::Parse(tokens, consumer);
  126. EXPECT_TRUE(tree.HasErrors());
  127. EXPECT_THAT(tree, MatchParseTreeNodes(
  128. {{.kind = ParseNodeKind::FunctionDeclaration(),
  129. .has_error = true,
  130. .children = {{ParseNodeKind::Identifier(), "foo"}}},
  131. {.kind = ParseNodeKind::FileEnd()}}));
  132. }
  133. TEST_F(ParseTreeTest,
  134. FunctionDeclarationWithIdentifierInsteadOfSignatureAndSemi) {
  135. TokenizedBuffer tokens = GetTokenizedBuffer("fn foo bar;");
  136. ParseTree tree = ParseTree::Parse(tokens, consumer);
  137. EXPECT_TRUE(tree.HasErrors());
  138. EXPECT_THAT(tree, MatchParseTreeNodes(
  139. {{.kind = ParseNodeKind::FunctionDeclaration(),
  140. .has_error = true,
  141. .children = {{ParseNodeKind::Identifier(), "foo"},
  142. {ParseNodeKind::DeclarationEnd()}}},
  143. {.kind = ParseNodeKind::FileEnd()}}));
  144. }
  145. TEST_F(ParseTreeTest, FunctionDeclarationWithSingleIdentifierParameterList) {
  146. TokenizedBuffer tokens = GetTokenizedBuffer("fn foo(bar);");
  147. ParseTree tree = ParseTree::Parse(tokens, consumer);
  148. // Note: this might become valid depending on the parameter syntax, this test
  149. // shouldn't be taken as a sign it should remain invalid.
  150. EXPECT_TRUE(tree.HasErrors());
  151. EXPECT_THAT(
  152. tree,
  153. MatchParseTreeNodes(
  154. {{.kind = ParseNodeKind::FunctionDeclaration(),
  155. .has_error = true,
  156. .children = {{ParseNodeKind::Identifier(), "foo"},
  157. {.kind = ParseNodeKind::ParameterList(),
  158. .has_error = true,
  159. .children = {{ParseNodeKind::ParameterListEnd()}}},
  160. {ParseNodeKind::DeclarationEnd()}}},
  161. {.kind = ParseNodeKind::FileEnd()}}));
  162. }
  163. TEST_F(ParseTreeTest, FunctionDeclarationWithoutName) {
  164. TokenizedBuffer tokens = GetTokenizedBuffer("fn ();");
  165. ParseTree tree = ParseTree::Parse(tokens, consumer);
  166. EXPECT_TRUE(tree.HasErrors());
  167. EXPECT_THAT(tree, MatchParseTreeNodes(
  168. {{.kind = ParseNodeKind::FunctionDeclaration(),
  169. .has_error = true,
  170. .children = {{ParseNodeKind::DeclarationEnd()}}},
  171. {.kind = ParseNodeKind::FileEnd()}}));
  172. }
  173. TEST_F(ParseTreeTest,
  174. FunctionDeclarationWithoutNameAndManyTokensToSkipInGroupedSymbols) {
  175. TokenizedBuffer tokens = GetTokenizedBuffer(
  176. "fn (a tokens c d e f g h i j k l m n o p q r s t u v w x y z);");
  177. ParseTree tree = ParseTree::Parse(tokens, consumer);
  178. EXPECT_TRUE(tree.HasErrors());
  179. EXPECT_THAT(tree, MatchParseTreeNodes(
  180. {{.kind = ParseNodeKind::FunctionDeclaration(),
  181. .has_error = true,
  182. .children = {{ParseNodeKind::DeclarationEnd()}}},
  183. {.kind = ParseNodeKind::FileEnd()}}));
  184. }
  185. TEST_F(ParseTreeTest, FunctionDeclarationSkipToNewlineWithoutSemi) {
  186. TokenizedBuffer tokens = GetTokenizedBuffer(
  187. "fn ()\n"
  188. "fn F();");
  189. ParseTree tree = ParseTree::Parse(tokens, consumer);
  190. EXPECT_TRUE(tree.HasErrors());
  191. EXPECT_THAT(
  192. tree,
  193. MatchParseTreeNodes(
  194. {{.kind = ParseNodeKind::FunctionDeclaration(), .has_error = true},
  195. {.kind = ParseNodeKind::FunctionDeclaration(),
  196. .children = {{ParseNodeKind::Identifier(), "F"},
  197. {.kind = ParseNodeKind::ParameterList(),
  198. .children = {{ParseNodeKind::ParameterListEnd()}}},
  199. {ParseNodeKind::DeclarationEnd()}}},
  200. {.kind = ParseNodeKind::FileEnd()}}));
  201. }
  202. TEST_F(ParseTreeTest, FunctionDeclarationSkipIndentedNewlineWithSemi) {
  203. TokenizedBuffer tokens = GetTokenizedBuffer(
  204. "fn (x,\n"
  205. " y,\n"
  206. " z);\n"
  207. "fn F();");
  208. ParseTree tree = ParseTree::Parse(tokens, consumer);
  209. EXPECT_TRUE(tree.HasErrors());
  210. EXPECT_THAT(
  211. tree,
  212. MatchParseTreeNodes(
  213. {{.kind = ParseNodeKind::FunctionDeclaration(),
  214. .has_error = true,
  215. .children = {{ParseNodeKind::DeclarationEnd()}}},
  216. {.kind = ParseNodeKind::FunctionDeclaration(),
  217. .children = {{ParseNodeKind::Identifier(), "F"},
  218. {.kind = ParseNodeKind::ParameterList(),
  219. .children = {{ParseNodeKind::ParameterListEnd()}}},
  220. {ParseNodeKind::DeclarationEnd()}}},
  221. {.kind = ParseNodeKind::FileEnd()}}));
  222. }
  223. TEST_F(ParseTreeTest, FunctionDeclarationSkipIndentedNewlineWithoutSemi) {
  224. TokenizedBuffer tokens = GetTokenizedBuffer(
  225. "fn (x,\n"
  226. " y,\n"
  227. " z)\n"
  228. "fn F();");
  229. ParseTree tree = ParseTree::Parse(tokens, consumer);
  230. EXPECT_TRUE(tree.HasErrors());
  231. EXPECT_THAT(
  232. tree,
  233. MatchParseTreeNodes(
  234. {{.kind = ParseNodeKind::FunctionDeclaration(), .has_error = true},
  235. {.kind = ParseNodeKind::FunctionDeclaration(),
  236. .children = {{ParseNodeKind::Identifier(), "F"},
  237. {.kind = ParseNodeKind::ParameterList(),
  238. .children = {{ParseNodeKind::ParameterListEnd()}}},
  239. {ParseNodeKind::DeclarationEnd()}}},
  240. {.kind = ParseNodeKind::FileEnd()}}));
  241. }
  242. TEST_F(ParseTreeTest, FunctionDeclarationSkipIndentedNewlineUntilOutdent) {
  243. TokenizedBuffer tokens = GetTokenizedBuffer(
  244. " fn (x,\n"
  245. " y,\n"
  246. " z)\n"
  247. "fn F();");
  248. ParseTree tree = ParseTree::Parse(tokens, consumer);
  249. EXPECT_TRUE(tree.HasErrors());
  250. EXPECT_THAT(
  251. tree,
  252. MatchParseTreeNodes(
  253. {{.kind = ParseNodeKind::FunctionDeclaration(), .has_error = true},
  254. {.kind = ParseNodeKind::FunctionDeclaration(),
  255. .children = {{ParseNodeKind::Identifier(), "F"},
  256. {.kind = ParseNodeKind::ParameterList(),
  257. .children = {{ParseNodeKind::ParameterListEnd()}}},
  258. {ParseNodeKind::DeclarationEnd()}}},
  259. {.kind = ParseNodeKind::FileEnd()}}));
  260. }
  261. TEST_F(ParseTreeTest, FunctionDeclarationSkipWithoutSemiToCurly) {
  262. // FIXME: We don't have a grammar construct that uses curlies yet so this just
  263. // won't parse at all. Once it does, we should ensure that the close brace
  264. // gets properly parsed for the struct (or whatever other curly-braced syntax
  265. // we have grouping function declarations) despite the invalid function
  266. // declaration missing a semicolon.
  267. TokenizedBuffer tokens = GetTokenizedBuffer(
  268. "struct X { fn () }\n"
  269. "fn F();");
  270. ParseTree tree = ParseTree::Parse(tokens, consumer);
  271. EXPECT_TRUE(tree.HasErrors());
  272. }
  273. TEST_F(ParseTreeTest, BasicFunctionDefinition) {
  274. TokenizedBuffer tokens = GetTokenizedBuffer(
  275. "fn F() {\n"
  276. "}");
  277. ParseTree tree = ParseTree::Parse(tokens, consumer);
  278. EXPECT_FALSE(tree.HasErrors());
  279. EXPECT_THAT(
  280. tree,
  281. MatchParseTreeNodes(
  282. {{.kind = ParseNodeKind::FunctionDeclaration(),
  283. .children = {{ParseNodeKind::Identifier(), "F"},
  284. {.kind = ParseNodeKind::ParameterList(),
  285. .children = {{ParseNodeKind::ParameterListEnd()}}},
  286. {.kind = ParseNodeKind::CodeBlock(),
  287. .text = "{",
  288. .children = {{ParseNodeKind::CodeBlockEnd(), "}"}}}}},
  289. {.kind = ParseNodeKind::FileEnd()}}));
  290. }
  291. TEST_F(ParseTreeTest, FunctionDefinitionWithNestedBlocks) {
  292. TokenizedBuffer tokens = GetTokenizedBuffer(
  293. "fn F() {\n"
  294. " {\n"
  295. " {{}}\n"
  296. " }\n"
  297. "}");
  298. ParseTree tree = ParseTree::Parse(tokens, consumer);
  299. EXPECT_FALSE(tree.HasErrors());
  300. EXPECT_THAT(
  301. tree,
  302. MatchParseTreeNodes(
  303. {{.kind = ParseNodeKind::FunctionDeclaration(),
  304. .children =
  305. {{ParseNodeKind::Identifier(), "F"},
  306. {.kind = ParseNodeKind::ParameterList(),
  307. .children = {{ParseNodeKind::ParameterListEnd()}}},
  308. {.kind = ParseNodeKind::CodeBlock(),
  309. .children =
  310. {{.kind = ParseNodeKind::CodeBlock(),
  311. .children =
  312. {{.kind = ParseNodeKind::CodeBlock(),
  313. .children = {{.kind = ParseNodeKind::CodeBlock(),
  314. .children = {{ParseNodeKind::
  315. CodeBlockEnd()}}},
  316. {ParseNodeKind::CodeBlockEnd()}}},
  317. {ParseNodeKind::CodeBlockEnd()}}},
  318. {ParseNodeKind::CodeBlockEnd()}}}}},
  319. {.kind = ParseNodeKind::FileEnd()}}));
  320. }
  321. TEST_F(ParseTreeTest, FunctionDefinitionWithIdenifierInStatements) {
  322. TokenizedBuffer tokens = GetTokenizedBuffer(
  323. "fn F() {\n"
  324. " bar\n"
  325. "}");
  326. ParseTree tree = ParseTree::Parse(tokens, consumer);
  327. // Note: this might become valid depending on the expression syntax. This test
  328. // shouldn't be taken as a sign it should remain invalid.
  329. EXPECT_TRUE(tree.HasErrors());
  330. EXPECT_THAT(
  331. tree,
  332. MatchParseTreeNodes(
  333. {{.kind = ParseNodeKind::FunctionDeclaration(),
  334. .children = {{ParseNodeKind::Identifier(), "F"},
  335. {.kind = ParseNodeKind::ParameterList(),
  336. .children = {{ParseNodeKind::ParameterListEnd()}}},
  337. {.kind = ParseNodeKind::CodeBlock(),
  338. .has_error = true,
  339. .children = {{ParseNodeKind::CodeBlockEnd()}}}}},
  340. {.kind = ParseNodeKind::FileEnd()}}));
  341. }
  342. TEST_F(ParseTreeTest, FunctionDefinitionWithIdenifierInNestedBlock) {
  343. TokenizedBuffer tokens = GetTokenizedBuffer(
  344. "fn F() {\n"
  345. " {bar}\n"
  346. "}");
  347. ParseTree tree = ParseTree::Parse(tokens, consumer);
  348. // Note: this might become valid depending on the expression syntax. This test
  349. // shouldn't be taken as a sign it should remain invalid.
  350. EXPECT_TRUE(tree.HasErrors());
  351. EXPECT_THAT(
  352. tree,
  353. MatchParseTreeNodes(
  354. {{.kind = ParseNodeKind::FunctionDeclaration(),
  355. .children =
  356. {{ParseNodeKind::Identifier(), "F"},
  357. {.kind = ParseNodeKind::ParameterList(),
  358. .children = {{ParseNodeKind::ParameterListEnd()}}},
  359. {.kind = ParseNodeKind::CodeBlock(),
  360. .children = {{.kind = ParseNodeKind::CodeBlock(),
  361. .has_error = true,
  362. .children = {{ParseNodeKind::CodeBlockEnd()}}},
  363. {ParseNodeKind::CodeBlockEnd()}}}}},
  364. {.kind = ParseNodeKind::FileEnd()}}));
  365. }
  366. auto GetAndDropLine(llvm::StringRef& s) -> std::string {
  367. auto newline_offset = s.find_first_of('\n');
  368. llvm::StringRef line = s.slice(0, newline_offset);
  369. if (newline_offset != llvm::StringRef::npos) {
  370. s = s.substr(newline_offset + 1);
  371. } else {
  372. s = "";
  373. }
  374. return line.str();
  375. }
  376. TEST_F(ParseTreeTest, Printing) {
  377. TokenizedBuffer tokens = GetTokenizedBuffer("fn F();");
  378. ParseTree tree = ParseTree::Parse(tokens, consumer);
  379. EXPECT_FALSE(tree.HasErrors());
  380. std::string print_storage;
  381. llvm::raw_string_ostream print_stream(print_storage);
  382. tree.Print(print_stream);
  383. llvm::StringRef print = print_stream.str();
  384. EXPECT_THAT(GetAndDropLine(print), StrEq("["));
  385. EXPECT_THAT(GetAndDropLine(print),
  386. StrEq("{node_index: 4, kind: 'FunctionDeclaration', text: 'fn', "
  387. "subtree_size: 5, children: ["));
  388. EXPECT_THAT(GetAndDropLine(print),
  389. StrEq(" {node_index: 0, kind: 'Identifier', text: 'F'},"));
  390. EXPECT_THAT(GetAndDropLine(print),
  391. StrEq(" {node_index: 2, kind: 'ParameterList', text: '(', "
  392. "subtree_size: 2, children: ["));
  393. EXPECT_THAT(GetAndDropLine(print),
  394. StrEq(" {node_index: 1, kind: 'ParameterListEnd', "
  395. "text: ')'}]},"));
  396. EXPECT_THAT(GetAndDropLine(print),
  397. StrEq(" {node_index: 3, kind: 'DeclarationEnd', text: ';'}]},"));
  398. EXPECT_THAT(GetAndDropLine(print),
  399. StrEq("{node_index: 5, kind: 'FileEnd', text: ''},"));
  400. EXPECT_THAT(GetAndDropLine(print), StrEq("]"));
  401. EXPECT_TRUE(print.empty()) << print;
  402. }
  403. TEST_F(ParseTreeTest, PrintingAsYAML) {
  404. TokenizedBuffer tokens = GetTokenizedBuffer("fn F();");
  405. ParseTree tree = ParseTree::Parse(tokens, consumer);
  406. EXPECT_FALSE(tree.HasErrors());
  407. std::string print_output;
  408. llvm::raw_string_ostream print_stream(print_output);
  409. tree.Print(print_stream);
  410. print_stream.flush();
  411. // Parse the output into a YAML stream. This will print errors to stderr.
  412. llvm::SourceMgr source_manager;
  413. llvm::yaml::Stream yaml_stream(print_output, source_manager);
  414. auto di = yaml_stream.begin();
  415. auto* root_node = llvm::dyn_cast<llvm::yaml::SequenceNode>(di->getRoot());
  416. ASSERT_THAT(root_node, NotNull());
  417. // The root node is just an array of top-level parse nodes.
  418. auto ni = root_node->begin();
  419. auto ne = root_node->end();
  420. auto* node = llvm::dyn_cast<llvm::yaml::MappingNode>(&*ni);
  421. ASSERT_THAT(node, NotNull());
  422. auto nkvi = node->begin();
  423. auto nkve = node->end();
  424. EXPECT_THAT(&*nkvi, IsKeyValueScalars("node_index", "4"));
  425. ++nkvi;
  426. EXPECT_THAT(&*nkvi, IsKeyValueScalars("kind", "FunctionDeclaration"));
  427. ++nkvi;
  428. EXPECT_THAT(&*nkvi, IsKeyValueScalars("text", "fn"));
  429. ++nkvi;
  430. EXPECT_THAT(&*nkvi, IsKeyValueScalars("subtree_size", "5"));
  431. ++nkvi;
  432. auto* children_node = llvm::dyn_cast<llvm::yaml::KeyValueNode>(&*nkvi);
  433. ASSERT_THAT(children_node, NotNull());
  434. auto* children_key_node =
  435. llvm::dyn_cast<llvm::yaml::ScalarNode>(children_node->getKey());
  436. ASSERT_THAT(children_key_node, NotNull());
  437. EXPECT_THAT(children_key_node->getRawValue(), StrEq("children"));
  438. auto* children_value_node =
  439. llvm::dyn_cast<llvm::yaml::SequenceNode>(children_node->getValue());
  440. ASSERT_THAT(children_value_node, NotNull());
  441. auto ci = children_value_node->begin();
  442. auto ce = children_value_node->end();
  443. ASSERT_THAT(ci, Ne(ce));
  444. node = llvm::dyn_cast<llvm::yaml::MappingNode>(&*ci);
  445. ASSERT_THAT(node, NotNull());
  446. auto ckvi = node->begin();
  447. EXPECT_THAT(&*ckvi, IsKeyValueScalars("node_index", "0"));
  448. ++ckvi;
  449. EXPECT_THAT(&*ckvi, IsKeyValueScalars("kind", "Identifier"));
  450. ++ckvi;
  451. EXPECT_THAT(&*ckvi, IsKeyValueScalars("text", "F"));
  452. ++ckvi;
  453. EXPECT_THAT(ckvi, Eq(node->end()));
  454. ++ci;
  455. ASSERT_THAT(ci, Ne(ce));
  456. node = llvm::dyn_cast<llvm::yaml::MappingNode>(&*ci);
  457. ASSERT_THAT(node, NotNull());
  458. ckvi = node->begin();
  459. auto ckve = node->end();
  460. EXPECT_THAT(&*ckvi, IsKeyValueScalars("node_index", "2"));
  461. ++ckvi;
  462. EXPECT_THAT(&*ckvi, IsKeyValueScalars("kind", "ParameterList"));
  463. ++ckvi;
  464. EXPECT_THAT(&*ckvi, IsKeyValueScalars("text", "("));
  465. ++ckvi;
  466. EXPECT_THAT(&*ckvi, IsKeyValueScalars("subtree_size", "2"));
  467. ++ckvi;
  468. children_node = llvm::dyn_cast<llvm::yaml::KeyValueNode>(&*ckvi);
  469. ASSERT_THAT(children_node, NotNull());
  470. children_key_node =
  471. llvm::dyn_cast<llvm::yaml::ScalarNode>(children_node->getKey());
  472. ASSERT_THAT(children_key_node, NotNull());
  473. EXPECT_THAT(children_key_node->getRawValue(), StrEq("children"));
  474. children_value_node =
  475. llvm::dyn_cast<llvm::yaml::SequenceNode>(children_node->getValue());
  476. ASSERT_THAT(children_value_node, NotNull());
  477. auto c2_i = children_value_node->begin();
  478. auto c2_e = children_value_node->end();
  479. ASSERT_THAT(c2_i, Ne(c2_e));
  480. node = llvm::dyn_cast<llvm::yaml::MappingNode>(&*c2_i);
  481. ASSERT_THAT(node, NotNull());
  482. auto c2_kvi = node->begin();
  483. EXPECT_THAT(&*c2_kvi, IsKeyValueScalars("node_index", "1"));
  484. ++c2_kvi;
  485. EXPECT_THAT(&*c2_kvi, IsKeyValueScalars("kind", "ParameterListEnd"));
  486. ++c2_kvi;
  487. EXPECT_THAT(&*c2_kvi, IsKeyValueScalars("text", ")"));
  488. ++c2_kvi;
  489. EXPECT_THAT(c2_kvi, Eq(node->end()));
  490. ++c2_i;
  491. EXPECT_THAT(c2_i, Eq(c2_e));
  492. ++ckvi;
  493. EXPECT_THAT(ckvi, Eq(ckve));
  494. ++ci;
  495. ASSERT_THAT(ci, Ne(ce));
  496. node = llvm::dyn_cast<llvm::yaml::MappingNode>(&*ci);
  497. ASSERT_THAT(node, NotNull());
  498. ckvi = node->begin();
  499. EXPECT_THAT(&*ckvi, IsKeyValueScalars("node_index", "3"));
  500. ++ckvi;
  501. EXPECT_THAT(&*ckvi, IsKeyValueScalars("kind", "DeclarationEnd"));
  502. ++ckvi;
  503. EXPECT_THAT(&*ckvi, IsKeyValueScalars("text", ";"));
  504. ++ckvi;
  505. EXPECT_THAT(ckvi, Eq(node->end()));
  506. ++ci;
  507. EXPECT_THAT(ci, Eq(ce));
  508. ++nkvi;
  509. EXPECT_THAT(nkvi, Eq(nkve));
  510. ++ni;
  511. ASSERT_THAT(ni, Ne(ne));
  512. node = llvm::dyn_cast<llvm::yaml::MappingNode>(&*ni);
  513. ASSERT_THAT(node, NotNull());
  514. nkvi = node->begin();
  515. EXPECT_THAT(&*nkvi, IsKeyValueScalars("node_index", "5"));
  516. ++nkvi;
  517. EXPECT_THAT(&*nkvi, IsKeyValueScalars("kind", "FileEnd"));
  518. ++nkvi;
  519. EXPECT_THAT(&*nkvi, IsKeyValueScalars("text", ""));
  520. ++nkvi;
  521. EXPECT_THAT(nkvi, Eq(node->end()));
  522. ++ni;
  523. EXPECT_THAT(ni, Eq(ne));
  524. ++di;
  525. EXPECT_THAT(di, Eq(yaml_stream.end()));
  526. }
  527. } // namespace
  528. } // namespace Carbon