parse_tree_test.cpp 19 KB

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