File: DefBlockSyntaxParser.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (683 lines) | stat: -rw-r--r-- 25,953 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
#include "RadiantTest.h"

#include "parser/DefBlockSyntaxParser.h"
#include "algorithm/FileUtils.h"
#include "string/split.h"

namespace test
{


namespace
{

inline parser::DefSyntaxTree::Ptr parseText(const std::string& text)
{
    parser::DefBlockSyntaxParser<const std::string> parser(text);
    return parser.parse();
}

inline void expectToken(const parser::DefSyntaxToken& token, parser::DefSyntaxToken::Type type, const std::string& value)
{
    EXPECT_EQ(token.type, type) << "Expected token type " << (int)type;
    EXPECT_EQ(token.value, value) << "Expected token value " << value;
}

inline parser::DefBlockSyntaxParser<const std::string>::Tokeniser createTokeniser(const std::string& source)
{
    return parser::DefBlockSyntaxParser<const std::string>::Tokeniser(
        parser::detail::SyntaxParserTraits<const std::string>::GetStartIterator(source),
        parser::detail::SyntaxParserTraits<const std::string>::GetEndIterator(source), 
        parser::DefBlockSyntaxTokeniserFunc()
    );
}

inline void expectSingleToken(const std::string& source, parser::DefSyntaxToken::Type type, const std::string& value)
{
    auto tokeniser = createTokeniser(source);

    auto it = tokeniser.getIterator();
    expectToken(*it++, type, value);
}

}

TEST(DefBlockSyntaxTokeniser, EmptyText)
{
    auto tokeniser = createTokeniser("");
    auto it = tokeniser.getIterator();
    
    EXPECT_TRUE(it.isExhausted());
}

TEST(DefBlockSyntaxTokeniser, Whitespace)
{
    expectSingleToken(" ", parser::DefSyntaxToken::Type::Whitespace, " ");
    expectSingleToken("\t", parser::DefSyntaxToken::Type::Whitespace, "\t");
    expectSingleToken("\n", parser::DefSyntaxToken::Type::Whitespace, "\n");
    expectSingleToken("\r\n", parser::DefSyntaxToken::Type::Whitespace, "\r\n");
    expectSingleToken("\t\t\r\n", parser::DefSyntaxToken::Type::Whitespace, "\t\t\r\n");
    expectSingleToken("\t \t \r\n  \t\n  \n", parser::DefSyntaxToken::Type::Whitespace, "\t \t \r\n  \t\n  \n");
}

TEST(DefBlockSyntaxTokeniser, SingleTokens)
{
    expectSingleToken("test", parser::DefSyntaxToken::Type::Token, "test");
    expectSingleToken("textures/common", parser::DefSyntaxToken::Type::Token, "textures/common");
    expectSingleToken("m/is/leading*/token/", parser::DefSyntaxToken::Type::Token, "m/is/leading*/token/");
    
    expectSingleToken("// EOL comment ", parser::DefSyntaxToken::Type::EolComment, "// EOL comment ");
    expectSingleToken("//", parser::DefSyntaxToken::Type::EolComment, "//");
    expectSingleToken("//EOLcomment", parser::DefSyntaxToken::Type::EolComment, "//EOLcomment");
    expectSingleToken("/* block comment */", parser::DefSyntaxToken::Type::BlockComment, "/* block comment */");
    expectSingleToken("/* bl/ock * * * comment */", parser::DefSyntaxToken::Type::BlockComment, "/* bl/ock * * * comment */");
    expectSingleToken("/* blk \n test test\n\ncomment */", parser::DefSyntaxToken::Type::BlockComment, "/* blk \n test test\n\ncomment */");
    expectSingleToken("/* this should not crash *", parser::DefSyntaxToken::Type::BlockComment, "/* this should not crash *");
}

void expectTokenSequence(const std::string& source, const std::vector<std::pair<parser::DefSyntaxToken::Type, std::string>>& sequence)
{
    auto tokeniser = createTokeniser(source);
    auto it = tokeniser.getIterator();

    for (const auto& [type, value] : sequence)
    {
        expectToken(*it++, type, value);
    }
}

TEST(DefBlockSyntaxTokeniser, TokenSequences)
{
    expectTokenSequence(" test{}",
    {
        { parser::DefSyntaxToken::Type::Whitespace, " " },
        { parser::DefSyntaxToken::Type::Token, "test" },
        { parser::DefSyntaxToken::Type::BracedBlock, "{}" },
    });

    expectTokenSequence(" test//comment\n{\n{\r\n   TESt \n}\n}",
    {
        { parser::DefSyntaxToken::Type::Whitespace, " " },
        { parser::DefSyntaxToken::Type::Token, "test" },
        { parser::DefSyntaxToken::Type::EolComment, "//comment" },
        { parser::DefSyntaxToken::Type::Whitespace, "\n" },
        { parser::DefSyntaxToken::Type::BracedBlock, "{\n{\r\n   TESt \n}\n}" },
    });

    expectTokenSequence("/*comment*/\ntest/* comment */{{//",
    {
        { parser::DefSyntaxToken::Type::BlockComment, "/*comment*/" },
        { parser::DefSyntaxToken::Type::Whitespace, "\n" },
        { parser::DefSyntaxToken::Type::Token, "test" },
        { parser::DefSyntaxToken::Type::BlockComment, "/* comment */" },
        { parser::DefSyntaxToken::Type::BracedBlock, "{{//" },
    });

    expectTokenSequence("test\n{\n    \"some to//kens {{\" \"containing /* control characters */\" // test\n}\r\n\r\n",
    {
        { parser::DefSyntaxToken::Type::Token, "test" },
        { parser::DefSyntaxToken::Type::Whitespace, "\n" },
        { parser::DefSyntaxToken::Type::BracedBlock, "{\n    \"some to//kens {{\" \"containing /* control characters */\" // test\n}" },
        { parser::DefSyntaxToken::Type::Whitespace, "\r\n\r\n" },
    });

    expectTokenSequence("test\n{\n\"some\" \"keyvalue\" // line comment\n\n}\r\n\r\ntest2\n{\n\"some\" \"keyvalue\"\n\n}\n",
    {
        { parser::DefSyntaxToken::Type::Token, "test" },
        { parser::DefSyntaxToken::Type::Whitespace, "\n" },
        { parser::DefSyntaxToken::Type::BracedBlock, "{\n\"some\" \"keyvalue\" // line comment\n\n}" },
        { parser::DefSyntaxToken::Type::Whitespace, "\r\n\r\n" },
        { parser::DefSyntaxToken::Type::Token, "test2" },
        { parser::DefSyntaxToken::Type::Whitespace, "\n" },
        { parser::DefSyntaxToken::Type::BracedBlock, "{\n\"some\" \"keyvalue\"\n\n}" },
        { parser::DefSyntaxToken::Type::Whitespace, "\n" },
    });

    expectTokenSequence(R"(// Test declarations used for some DeclarationManager unit tests

decl/exporttest/guisurf1
{
    guiSurf	guis/lvlmaps/genericmap.gui
}

testdecl2 decltable2 { { 0, 0, 0, 0, 1, 1 } }
testdecl2 decltable3 { { 0, 0, 0, 0, 1, 1 } }
)",
    {
        { parser::DefSyntaxToken::Type::EolComment, "// Test declarations used for some DeclarationManager unit tests" },
        { parser::DefSyntaxToken::Type::Whitespace, "\n\n" },
        { parser::DefSyntaxToken::Type::Token, "decl/exporttest/guisurf1" },
        { parser::DefSyntaxToken::Type::Whitespace, "\n" },
        { parser::DefSyntaxToken::Type::BracedBlock, R"({
    guiSurf	guis/lvlmaps/genericmap.gui
})" },
        { parser::DefSyntaxToken::Type::Whitespace, "\n\n" },
        { parser::DefSyntaxToken::Type::Token, "testdecl2" },
        { parser::DefSyntaxToken::Type::Whitespace, " " },
        { parser::DefSyntaxToken::Type::Token, "decltable2" },
        { parser::DefSyntaxToken::Type::Whitespace, " " },
        { parser::DefSyntaxToken::Type::BracedBlock, "{ { 0, 0, 0, 0, 1, 1 } }" },
        { parser::DefSyntaxToken::Type::Whitespace, "\n" },
        { parser::DefSyntaxToken::Type::Token, "testdecl2" },
        { parser::DefSyntaxToken::Type::Whitespace, " " },
        { parser::DefSyntaxToken::Type::Token, "decltable3" },
        { parser::DefSyntaxToken::Type::Whitespace, " " },
        { parser::DefSyntaxToken::Type::BracedBlock, "{ { 0, 0, 0, 0, 1, 1 } }" },
        { parser::DefSyntaxToken::Type::Whitespace, "\n" },
    });
}

TEST(DefBlockSyntaxTokeniser, QuoteWithinLineComment)
{
    auto input = string::join(std::vector<std::string>
    {
        "entityDef something",
        "{",
        "    \"key\" \"value\" // comment with quote\"", // has a quote at the end of the line
        "    \"key2\" \"value2\" // comment\" with quote", // has a quote in the middle
        "    \"key3\" \"value3\" // the third opening \" quote", // has a quote in the middle
        "}",
        "entityDef tork {}"
    }, "\n"); // use a specific line break to be platform independent here

    auto tokeniser = createTokeniser(input);
    auto it = tokeniser.getIterator();

    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::Token);
    EXPECT_EQ((*it).value, "entityDef");
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::Whitespace);
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::Token);
    EXPECT_EQ((*it).value, "something");
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::Whitespace);
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::BracedBlock);
    EXPECT_NE((*it).value.find("key2"), std::string::npos);
    EXPECT_NE((*it).value.find("value2"), std::string::npos);
    EXPECT_NE((*it).value.find("// comment with quote\""), std::string::npos);
    EXPECT_NE((*it).value.find("// comment\" with quote"), std::string::npos);
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::Whitespace);
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::Token);
    EXPECT_EQ((*it).value, "entityDef");
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::Whitespace);
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::Token);
    EXPECT_EQ((*it).value, "tork");
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::Whitespace);
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::BracedBlock);
}

TEST(DefBlockSyntaxTokeniser, QuoteWithinBlockComment)
{
    auto input = string::join(std::vector<std::string>
    {
        "entityDef something",
        "{",
        "    \"key\" \"value\" /* comment with quote\" */", // comment has a quote in it
        "}",
        "entityDef tork {}"
    }, "\n"); // use a specific line break to be platform independent here

    auto tokeniser = createTokeniser(input);
    auto it = tokeniser.getIterator();

    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::Token);
    EXPECT_EQ((*it).value, "entityDef");
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::Whitespace);
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::Token);
    EXPECT_EQ((*it).value, "something");
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::Whitespace);
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::BracedBlock);
    EXPECT_NE((*it).value.find("key"), std::string::npos);
    EXPECT_NE((*it).value.find("value"), std::string::npos);
    EXPECT_NE((*it).value.find("/* comment with quote\" */"), std::string::npos);
    EXPECT_EQ((*it).value.find("tork"), std::string::npos);
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::Whitespace);
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::Token);
    EXPECT_EQ((*it).value, "entityDef");
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::Whitespace);
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::Token);
    EXPECT_EQ((*it).value, "tork");
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::Whitespace);
    ++it;
    EXPECT_EQ((*it).type, parser::DefSyntaxToken::Type::BracedBlock);
}

using DefBlockSyntaxParserTest = RadiantTest;

TEST_F(DefBlockSyntaxParserTest, ParseEmptyText)
{
    auto syntaxTree = parseText("");
    EXPECT_TRUE(syntaxTree) << "Syntax Root must not be null";
}

TEST_F(DefBlockSyntaxParserTest, ParseWhitespace)
{
    auto syntaxTree = parseText(" ");
    EXPECT_EQ(syntaxTree->getRoot()->getChildren().size(), 1) << "Expected 1 whitespace node";
    EXPECT_EQ(syntaxTree->getRoot()->getChildren().front()->getType(), parser::DefSyntaxNode::Type::Whitespace);
    EXPECT_EQ(syntaxTree->getRoot()->getChildren().front()->getString(), " ");

    syntaxTree = parseText("\n\n");
    EXPECT_EQ(syntaxTree->getRoot()->getChildren().size(), 1) << "Expected 1 whitespace node";
    EXPECT_EQ(syntaxTree->getRoot()->getChildren().front()->getType(), parser::DefSyntaxNode::Type::Whitespace);
    EXPECT_EQ(syntaxTree->getRoot()->getChildren().front()->getString(), "\n\n");

    syntaxTree = parseText("\t \t");
    EXPECT_EQ(syntaxTree->getRoot()->getChildren().size(), 1) << "Expected 1 whitespace node";
    EXPECT_EQ(syntaxTree->getRoot()->getChildren().front()->getType(), parser::DefSyntaxNode::Type::Whitespace);
    EXPECT_EQ(syntaxTree->getRoot()->getChildren().front()->getString(), "\t \t");

    syntaxTree = parseText("\r\n \r\n");
    EXPECT_EQ(syntaxTree->getRoot()->getChildren().size(), 1) << "Expected 1 whitespace node";
    EXPECT_EQ(syntaxTree->getRoot()->getChildren().front()->getType(), parser::DefSyntaxNode::Type::Whitespace);
    EXPECT_EQ(syntaxTree->getRoot()->getChildren().front()->getString(), "\r\n \r\n");
}

namespace
{

    const std::string ExampleFileContent = R"(// Some comment

decl/exporttest/guisurf1
{
    guiSurf	guis/lvlmaps/genericmap.gui
}

testdecl2 decltable2 { { 0, 0, 0, 0, 1, 1 } }
testdecl2 decltable3 { { 0, 0, 0, 0, 1, 1 } }
)";

    void expectExampleFileSyntax(const parser::DefSyntaxTree::Ptr& syntaxTree)
    {
        EXPECT_EQ(syntaxTree->getRoot()->getChildren().size(), 8) << "Expected 8 nodes";

        EXPECT_EQ(syntaxTree->getRoot()->getChildren().at(0)->getType(), parser::DefSyntaxNode::Type::Comment);
        EXPECT_EQ(syntaxTree->getRoot()->getChildren().at(0)->getString(), "// Some comment");

        EXPECT_EQ(syntaxTree->getRoot()->getChildren().at(1)->getType(), parser::DefSyntaxNode::Type::Whitespace);
        EXPECT_EQ(syntaxTree->getRoot()->getChildren().at(1)->getString(), "\n\n");

        EXPECT_EQ(syntaxTree->getRoot()->getChildren().at(2)->getType(), parser::DefSyntaxNode::Type::DeclBlock);
        auto block = std::static_pointer_cast<parser::DefBlockSyntax>(syntaxTree->getRoot()->getChildren().at(2));
        EXPECT_FALSE(block->getType());
        EXPECT_EQ(block->getName()->getString(), "decl/exporttest/guisurf1");
        EXPECT_EQ(block->getBlockContents(), "\n    guiSurf	guis/lvlmaps/genericmap.gui\n");

        EXPECT_EQ(syntaxTree->getRoot()->getChildren().at(3)->getType(), parser::DefSyntaxNode::Type::Whitespace);
        EXPECT_EQ(syntaxTree->getRoot()->getChildren().at(3)->getString(), "\n\n");

        EXPECT_EQ(syntaxTree->getRoot()->getChildren().at(4)->getType(), parser::DefSyntaxNode::Type::DeclBlock);
        block = std::static_pointer_cast<parser::DefBlockSyntax>(syntaxTree->getRoot()->getChildren().at(4));
        EXPECT_EQ(block->getType()->getString(), "testdecl2");
        EXPECT_EQ(block->getName()->getString(), "decltable2");
        EXPECT_EQ(block->getBlockContents(), " { 0, 0, 0, 0, 1, 1 } ");

        EXPECT_EQ(syntaxTree->getRoot()->getChildren().at(5)->getType(), parser::DefSyntaxNode::Type::Whitespace);
        EXPECT_EQ(syntaxTree->getRoot()->getChildren().at(5)->getString(), "\n");

        EXPECT_EQ(syntaxTree->getRoot()->getChildren().at(6)->getType(), parser::DefSyntaxNode::Type::DeclBlock);
        block = std::static_pointer_cast<parser::DefBlockSyntax>(syntaxTree->getRoot()->getChildren().at(6));
        EXPECT_EQ(block->getType()->getString(), "testdecl2");
        EXPECT_EQ(block->getName()->getString(), "decltable3");
        EXPECT_EQ(block->getBlockContents(), " { 0, 0, 0, 0, 1, 1 } ");

        EXPECT_EQ(syntaxTree->getRoot()->getChildren().at(7)->getType(), parser::DefSyntaxNode::Type::Whitespace);
        EXPECT_EQ(syntaxTree->getRoot()->getChildren().at(7)->getString(), "\n");
    }
}

// Parse the example decl file contents from a string and check the syntax tree contents
TEST_F(DefBlockSyntaxParserTest, SimpleDeclFileFromString)
{
    parser::DefBlockSyntaxParser<const std::string> parser(ExampleFileContent);
    auto tree = parser.parse();
    
    expectExampleFileSyntax(tree);
}

// Parse the example decl file contents from a stream and check the syntax tree contents
TEST_F(DefBlockSyntaxParserTest, SimpleDeclFileFromStream)
{
    std::stringstream stream(ExampleFileContent);

    parser::DefBlockSyntaxParser<std::istream> parser(stream);
    auto tree = parser.parse();
    
    expectExampleFileSyntax(tree);
}

void checkDeclFileReconstruction(const std::string& declFile)
{
    auto originalText = algorithm::loadTextFromVfsFile(declFile);
    auto syntaxTree = parseText(originalText);

    auto reconstructedText = syntaxTree->getString();
    EXPECT_EQ(reconstructedText, originalText) << "Parsed file " << declFile << " couldn't be reconstructed";
}
// Attempt to parse a whole file and reconstruct it from the syntax tree
TEST_F(DefBlockSyntaxParserTest, ReconstructFileFromSyntaxTree)
{
    checkDeclFileReconstruction("testdecls/exporttest.decl");
    checkDeclFileReconstruction("testdecls/numbers.decl");
    checkDeclFileReconstruction("testdecls/precedence_test1.decl");
    checkDeclFileReconstruction("testdecls/precedence_test2.decl");
    checkDeclFileReconstruction("testdecls/removal_tests.decl");
    checkDeclFileReconstruction("testdecls/removal_tests.decl");

    // These two contain malformed decls at the bottom of the file
    checkDeclFileReconstruction("testdecls/syntax_parser_test1.decl");
    checkDeclFileReconstruction("testdecls/syntax_parser_test2.decl");
    // Unfinished comment block at the end
    checkDeclFileReconstruction("testdecls/syntax_parser_test3.decl");

    checkDeclFileReconstruction("particles/testparticles.prt");
    
    checkDeclFileReconstruction("materials/parsertest.mtr");
    checkDeclFileReconstruction("materials/example.mtr");
    checkDeclFileReconstruction("materials/tdm_internal_engine.mtr");
    checkDeclFileReconstruction("materials/null_byte_at_the_end.mtr");

    checkDeclFileReconstruction("def/base.def");
    checkDeclFileReconstruction("def/tdm_ai.def");
    checkDeclFileReconstruction("def/mover_door.def");
}

inline void parseBlock(const std::string& testString,
    const std::vector<std::pair<std::string, std::string>>& expectedBlocks)
{
    // Use the stream parser variant (even though we could parse right from the string)
    std::istringstream stream{ testString };
    parser::DefBlockSyntaxParser<std::istream> parser(stream);
    auto syntaxTree = parser.parse();

    // Check that the expected blocks appear in the syntax tree, in the correct order
    auto expectedBlock = expectedBlocks.begin();

    syntaxTree->foreachBlock([&](const parser::DefBlockSyntax::Ptr& block)
    {
        if (expectedBlock->first.find(' ') != std::string::npos)
        {
            // Check name and type
            std::vector<std::string> parts;
            string::split(parts, expectedBlock->first, " \t");
            EXPECT_EQ(block->getType()->getString(), parts[0]);
            EXPECT_EQ(block->getName()->getString(), parts[1]);
        }
        else
        {
            if (expectedBlock->first.empty())
            {
                EXPECT_TRUE(!block->getName() || block->getName()->getString().empty());
            }
            else
            {
                EXPECT_EQ(block->getName()->getString(), expectedBlock->first);
            }
        }

        EXPECT_NE(block->getBlockContents().find(expectedBlock->second), std::string::npos);

        ++expectedBlock;
    });
}

inline void parseBlock(const std::string& testString,
    const std::string& expectedName, const std::string& needleToFindInBlockContents)
{
    parseBlock(testString, { std::make_pair(expectedName, needleToFindInBlockContents) });
}

TEST_F(DefBlockSyntaxParserTest, ParseTypedBlock)
{
    std::string testString = R"(entityDef atdm:target_base
{
	"inherit"				"atdm:entity_base"

	"editor_displayFolder"	"Targets"
	"nodraw"				"1"
})";

    parseBlock(testString, "entityDef atdm:target_base", "\"atdm:entity_base\"");
}

TEST_F(DefBlockSyntaxParserTest, ParseTypedBlockWithWhitespace)
{
    std::string testString = R"(	entityDef	atdm:target_base  		
{
	"inherit"				"atdm:entity_base"

	"editor_displayFolder"	"Inner opening { and closing } braces to confuse the parser"
	"nodraw"				"1"
})";

    parseBlock(testString, "entityDef atdm:target_base", "\"atdm:entity_base\"");
}

TEST_F(DefBlockSyntaxParserTest, ParseTypedBlockWithCurlyBracesInContent)
{
    std::string testString = R"(	entityDef	atdm:target_base  		
{
	"editor_displayFolder"	"Inner opening { and closing } braces to confuse the parser"
	"nodraw"				"1"
})";

    parseBlock(testString, "entityDef atdm:target_base", "Inner opening { and closing } braces to confuse the parser");
}

TEST_F(DefBlockSyntaxParserTest, ParseNestedBlock)
{
    std::string testString = R"(textures/darkmod/test  		
{
    {
        blend diffusemap
        map _white
    }
})";

    parseBlock(testString, "textures/darkmod/test", "blend diffusemap");
}

TEST_F(DefBlockSyntaxParserTest, ParseNameAndBlockInSeparateLines)
{
    std::string testString = R"(textures/parsing_test/variant1
    {
        diffusemap _white
    })";

    parseBlock(testString, "textures/parsing_test/variant1", "_white");
}

TEST_F(DefBlockSyntaxParserTest, ParseCommentAfterBlockName)
{
    std::string testString = R"(textures/parsing_test/commentAfterBlockName // comment separated by whitespace
    {
        diffusemap _white
    })";

    parseBlock(testString, "textures/parsing_test/commentAfterBlockName", "_white");
}

TEST_F(DefBlockSyntaxParserTest, ParseCommentAfterBlockNameNoWhitespace)
{
    std::string testString = R"(textures/parsing_test/CommentAfterBlockNameNoWhitespace// comment without whitespace
    {
        diffusemap _white
    })";

    parseBlock(testString, "textures/parsing_test/CommentAfterBlockNameNoWhitespace", "_white");
}

TEST_F(DefBlockSyntaxParserTest, ParseCommentInBetweenNameAndBlock)
{
    std::string testString = R"(textures/parsing_test/commentInBetweenNameAndBlock
// comment between name and block
{
    diffusemap _white
})";

    parseBlock(testString, "textures/parsing_test/commentInBetweenNameAndBlock", "_white");
}

TEST_F(DefBlockSyntaxParserTest, ParseNameAndBlockInSameLine)
{
    std::string testString = R"(textures/parsing_test/variant2 {
        diffusemap _white
    })";

    parseBlock(testString, "textures/parsing_test/variant2", "_white");
}

TEST_F(DefBlockSyntaxParserTest, ParseNameAndBlockInSameLineNoWhitespace)
{
    std::string testString = R"(textures/parsing_test/variant3{
        diffusemap _white
    })";

    parseBlock(testString, "textures/parsing_test/variant3", "_white");
}

TEST_F(DefBlockSyntaxParserTest, ParseTightBlockSequence)
{
    std::string testString = R"(textures/parsing_test/block1 {
        diffusemap _white
    }

    textures/parsing_test/block2 {
        diffusemap _white
    } textures/parsing_test/block3 {
        diffusemap _white
    }
    textures/parsing_test/block4 {
        diffusemap _white
    }textures/parsing_test/block5 {
        diffusemap _white
    })";

    parseBlock(testString, {
        std::make_pair("textures/parsing_test/block1", "_white"),
        std::make_pair("textures/parsing_test/block2", "_white"),
        std::make_pair("textures/parsing_test/block3", "_white"),
        std::make_pair("textures/parsing_test/block4", "_white"),
        std::make_pair("textures/parsing_test/block5", "_white"),
        });
}

// The tokeniser normalises the whitespace between type and name to a single space
TEST_F(DefBlockSyntaxParserTest, ParseWhitespaceAfterTypename)
{
    std::string testString = R"(textures/parsing_test/block1 {
        diffusemap _white
    }

    // Add a tab in between type name and block name
    sound	textures/parsing_test/block1a {
        diffusemap _white
    }

    // spaces after the typename, and some tabs after the name
    sound    textures/parsing_test/block1b		{
        diffusemap _white
    }

    // Tabs before and after the name
    sound	  	  	textures/parsing_test/block1c
    {
        diffusemap _white
    }

    textures/parsing_test/block2 {
        diffusemap _white
    } sound textures/parsing_test/block3 {
        diffusemap _white
    }
    sound textures/parsing_test/block4 {
        diffusemap _white
    }sound textures/parsing_test/block5 {
        diffusemap _white
    })";

    parseBlock(testString, {
        std::make_pair("textures/parsing_test/block1", "_white"),
        std::make_pair("sound textures/parsing_test/block1a", "_white"),
        std::make_pair("sound textures/parsing_test/block1b", "_white"),
        std::make_pair("sound textures/parsing_test/block1c", "_white"),
        std::make_pair("textures/parsing_test/block2", "_white"),
        std::make_pair("sound textures/parsing_test/block3", "_white"),
        std::make_pair("sound textures/parsing_test/block4", "_white"),
        std::make_pair("sound textures/parsing_test/block5", "_white"),
        });
}

TEST_F(DefBlockSyntaxParserTest, ParseUnnamedBlock)
{
    std::string testString = R"(/* just a comment */ {
        diffusemap _white
    })";

    parseBlock(testString, "", "_white");
}

TEST_F(DefBlockSyntaxParserTest, ParseBlockWithTooManyLeadingTokens)
{
    std::string testString = R"(testdecl blabla something {
        diffusemap _white
    })";

    parseBlock(testString, "testdecl blabla", "_white");
}

TEST_F(DefBlockSyntaxParserTest, ParseIncompleteBlock)
{
    std::string testString = R"(testdecl something {
        diffusemap _white)";

    parseBlock(testString, "something", "_white");
}

TEST_F(DefBlockSyntaxParserTest, ParseTrailingNullByte)
{
    std::string testString = "testdecl something {\n"
        "         diffusemap _white\n}\n\n\n";
    testString.append({ '\0' });

    parser::DefBlockSyntaxParser<const std::string> parser(testString);
    auto syntaxTree = parser.parse();

    // Ensure we don't have any empty nodes in the syntax tree
    for (const auto& syntaxNode : syntaxTree->getRoot()->getChildren())
    {
        EXPECT_TRUE(syntaxNode) << "Found an empty node in the syntax tree";
    }
}

TEST_F(DefBlockSyntaxParserTest, ParseTrailingNullByte2)
{
    std::ifstream stream(_context.getTestProjectPath() + "materials/null_byte_at_the_end.mtr");

    parser::DefBlockSyntaxParser<std::istream> parser(stream);
    auto syntaxTree = parser.parse();

    // Ensure we don't have any empty nodes in the syntax tree
    for (const auto& syntaxNode : syntaxTree->getRoot()->getChildren())
    {
        EXPECT_TRUE(syntaxNode);
    }
}
}