blitz_query_cpp

Форк
0
/
parser.cpp 
968 строк · 26.5 Кб
1
#include <parser/parser.hpp>
2

3
using namespace blitz_query_cpp;
4

5
bool parser_t::unexpected_token()
6
{
7
    return report_error(error_code_t::UnexpectedToken,
8
                        "UnexpectedToken {}",
9
                        current_token.value);
10
}
11

12
bool parser_t::create_new_node(syntax_node_type type, bool is_leaf)
13
{
14
    syntax_node &parent = *nodes_stack.top();
15
    syntax_node &node = doc.all_nodes.emplace_back();
16
    node.parent = &parent;
17
    node.pos = current_token.pos;
18
    node.type = type;
19

20
    if (current_description.size() > 0)
21
    {
22
        node.description = current_description;
23
        current_description = std::string_view{};
24
    }
25

26
    if (!parent.add_child(&node))
27
        return false;
28
    if (!is_leaf)
29
        nodes_stack.push(&node);
30
    return true;
31
}
32

33
bool parser_t::parse()
34
{
35
    index_t token_count = count_tokens();
36
    // reserve node storage to avoid future reallocations
37
    doc.all_nodes.reserve(token_count);
38

39
    while (current_token.type != token_type::End && current_token.type != token_type::InvalidToken)
40
    {
41
        if (!parse_definitions())
42
        {
43
            return false;
44
        }
45
    }
46
    if (current_token.type == token_type::InvalidToken)
47
    {
48
        return unexpected_token();
49
    }
50
    return true;
51
}
52

53
bool parser_t::expect_token(token_type expected_types)
54
{
55
    if (!current_token.of_type(expected_types))
56
    {
57
        return unexpected_token();
58
    }
59
    return next_token();
60
}
61

62
bool parser_t::parse_keyword_token(syntax_node_type type, std::string_view keyword)
63
{
64
    if (!current_token.of_type(token_type::Name) || current_token.value != keyword)
65
    {
66
        return report_error(error_code_t::UnexpectedToken,
67
                            "Expected keyword '{}' got: '{}'",
68
                            keyword,
69
                            current_token.value);
70
    }
71
    return parse_node(type, token_type::Name);
72
}
73

74
bool parser_t::expect_keyword_token(std::string_view keyword, bool optional)
75
{
76
    if (!current_token.of_type(token_type::Name) || current_token.value != keyword)
77
    {
78
        if (optional)
79
            return false;
80
        return report_error(error_code_t::UnexpectedToken,
81
                            "Expected keyword '{}' got: '{}'",
82
                            keyword,
83
                            current_token.value);
84
    }
85
    return next_token();
86
}
87

88
index_t parser_t::count_tokens()
89
{
90
    tokenizer_t local_tokenizer{doc.doc_value};
91
    token_t t;
92
    index_t count = 0;
93
    do
94
    {
95
        t = local_tokenizer.next_token();
96
        count++;
97
    } while (!t.of_type(token_type::End | token_type::InvalidToken));
98
    return count;
99
}
100

101
bool parser_t::next_token()
102
{
103
    last_token_end = current_token.pos + current_token.size;
104
    current_token = tokenizer.next_token();
105
    return true;
106
}
107

108
void parser_t::update_node_size_and_content()
109
{
110
    syntax_node &node = current_node();
111
    node.size = last_token_end - node.pos;
112
    std::string_view doc_view = doc.doc_value;
113
    node.content = doc_view.substr(node.pos, node.size);
114
}
115

116
bool parser_t::parse_description()
117
{
118
    if (current_token.of_type(token_type::StringBlock | token_type::StringLiteral))
119
    {
120
        current_description = current_token.value;
121
        return next_token();
122
    }
123
    return true;
124
}
125

126
bool parser_t::parse_definitions()
127
{
128
    if (!parse_description())
129
        return false;
130

131
    if (current_token.type == token_type::Name)
132
    {
133
        switch (hash_crc32(current_token.value))
134
        {
135
        case "query"_crc32:
136
        case "mutation"_crc32:
137
        case "subscription"_crc32:
138
            return parse_operation_definition();
139
        case "fragment"_crc32:
140
            return parse_fragment_definition();
141
        case "directive"_crc32:
142
            return parse_directive_definition();
143
        case "schema"_crc32:
144
            return parse_schema_definition(syntax_node_type::SchemaDefinition);
145
        case "scalar"_crc32:
146
            return parse_scalar_type_definition(syntax_node_type::ScalarTypeDefinition);
147
        case "type"_crc32:
148
            return parse_object_type_definition(syntax_node_type::ObjectTypeDefinition, "type");
149
        case "interface"_crc32:
150
            return parse_object_type_definition(syntax_node_type::ObjectTypeDefinition, "interface");
151
        case "union"_crc32:
152
            return parse_union_type_definition(syntax_node_type::UnionTypeDefinition);
153
        case "enum"_crc32:
154
            return parse_enum_type_definition(syntax_node_type::EnumTypeDefinition);
155
        case "input"_crc32:
156
            return parse_input_object_type_definition(syntax_node_type::InputObjectTypeDefinition);
157
        case "extend"_crc32:
158
            return parse_type_extension();
159
        }
160
    }
161
    if (current_token.type == token_type::LBrace)
162
    {
163
        return parse_short_operation_definition();
164
    }
165
    return unexpected_token();
166
}
167

168
bool parser_t::parse_node(syntax_node_type type, token_type expected_types, NodeParseOptions opts)
169
{
170
    if (!current_token.of_type(expected_types))
171
    {
172
        if (has_any_flag(opts, ParseNodeIfMatch))
173
            return false;
174
        return unexpected_token();
175
    }
176

177
    if (!create_new_node(type, has_any_flag(opts, NodeIsLeaf)))
178
        return false;
179
    syntax_node &node = last_node();
180
    node.pos = current_token.pos;
181
    node.size = current_token.size;
182
    node.content = current_token.value;
183

184
    return next_token();
185
}
186

187
bool parser_t::parse_operation_definition()
188
{
189
    operation_type_t op_type = parse_operation_type(current_token.value);
190
    if (op_type == operation_type_t::None)
191
    {
192
        report_error(error_code_t::InvalidOperationType, "Invalid operation type {}", last_node().content);
193
        return false;
194
    }
195

196
    if (!parse_node(syntax_node_type::OperationDefinition, token_type::Name))
197
        return false;
198
    syntax_node &operation_node = current_node();
199
    operation_node.operation_type = op_type;
200
    doc.operation = &operation_node;
201

202
    if (current_token.type == token_type::Name)
203
    {
204
        if (!parse_name())
205
            return false;
206
    }
207

208
    if (!parse_variable_definitions())
209
        return false;
210
    if (!parse_directives(false))
211
        return false;
212
    if (!parse_selection_set())
213
        return false;
214

215
   return pop_node();
216
}
217

218
bool parser_t::parse_short_operation_definition()
219
{
220
    if (!create_new_node(syntax_node_type::OperationDefinition, false))
221
        return false;
222
    auto &node = current_node();
223
    doc.operation = &node;
224
    node.operation_type = operation_type_t::Query;
225
    node.size = doc.doc_value.size() - node.pos;
226
    if (!parse_selection_set())
227
        return false;
228
   return pop_node();
229
}
230

231
bool parser_t::parse_variable_definitions()
232
{
233
    size_t child_count = current_node().children.size();
234
    if (!current_token.of_type(token_type::LParen))
235
        return true;
236

237
    if (!next_token())
238
        return false;
239

240
    while (!current_token.of_type(token_type::RParen))
241
    {
242
        if (!parse_variable_definition())
243
        {
244
            return false;
245
        }
246
    }
247
    auto &node = current_node();
248
    node.variables = node.children.subspan(child_count);
249
    return expect_token(token_type::RParen);
250
}
251

252
bool parser_t::parse_variable_definition()
253
{
254
    if (!current_token.of_type(token_type::ParameterLiteral))
255
    {
256
        return unexpected_token();
257
    }
258
    if (!parse_node(syntax_node_type::VariableDefinition, token_type::ParameterLiteral))
259
        return false;
260
    auto &var_node = current_node();
261
    var_node.name = var_node.content;
262

263
    if (!expect_token(token_type::Colon))
264
        return false;
265
    if (!parse_type_reference())
266
        return false;
267

268
    if (current_token.of_type(token_type::Equal))
269
    {
270
        if (!next_token())
271
            return false;
272
        if (!parse_value_literal(true))
273
            return false;
274
    }
275
    size_t child_count = var_node.children.size();
276
    if (!parse_directives(true))
277
        return false;
278

279
    var_node.directives = var_node.children.subspan(child_count);
280

281
   return pop_node();
282
}
283

284
bool parser_t::parse_value_literal(bool is_constant)
285
{
286
    if (current_token.of_type(token_type::LBracket))
287
        return parse_list(is_constant);
288

289
    if (current_token.of_type(token_type::LBrace))
290
        return parse_object(is_constant);
291

292
    if (parse_node(syntax_node_type::StringValue, token_type::StringLiteral | token_type::StringBlock, NodeIsLeaf | ParseNodeIfMatch))
293
        return true;
294

295
    if (parse_node(syntax_node_type::IntValue, token_type::IntLiteral, NodeIsLeaf | ParseNodeIfMatch))
296
    {
297
        syntax_node &node = last_node();
298
        long long value = 0; 
299
        auto res = std::from_chars(node.content.begin(), node.content.end(), value);
300
        if(res.ec != std::errc{})
301
            return report_error(error_code_t::SyntaxError, "Failed to parse int value. Error: {}", (int)res.ec);
302
        node.intValue = value;
303
        return true;
304
    }
305

306
    if (parse_node(syntax_node_type::FloatValue, token_type::FloatLiteral, NodeIsLeaf | ParseNodeIfMatch))
307
    {
308
        syntax_node &node = last_node();
309
        node.floatValue = std::stod(node.content.data());
310
        return true;
311
    }
312

313
    if (parse_node(syntax_node_type::EnumValue, token_type::Name, NodeIsLeaf | ParseNodeIfMatch))
314
    {
315
        syntax_node &node = last_node();
316
        if (node.content == "true")
317
        {
318
            node.type = syntax_node_type::BoolValue;
319
            node.boolValue = true;
320
        }
321
        if (node.content == "false")
322
        {
323
            node.type = syntax_node_type::BoolValue;
324
            node.boolValue = false;
325
        }
326
        if (node.content == "null")
327
            node.type = syntax_node_type::NullValue;
328
        return true;
329
    }
330

331
    if (!is_constant && parse_node(syntax_node_type::Variable, token_type::ParameterLiteral, NodeIsLeaf | ParseNodeIfMatch))
332
    {
333
        syntax_node &node = last_node();
334
        node.name = node.content;
335
        return true;
336
    }
337

338
    return unexpected_token();
339
}
340

341
bool parser_t::parse_list(bool is_constant)
342
{
343
    if (!parse_node(syntax_node_type::ListValue, token_type::LBracket))
344
        return false;
345

346
    while (!current_token.of_type(token_type::RBracket))
347
    {
348
        if (!parse_value_literal(is_constant))
349
            return false;
350
    }
351

352
    if (!expect_token(token_type::RBrace))
353
        return false;
354

355
    return pop_node();
356
}
357

358
bool parser_t::parse_object(bool is_constant)
359
{
360
    if (!parse_node(syntax_node_type::ObjectValue, token_type::LBrace))
361
        return false;
362

363
    while (!current_token.of_type(token_type::RBrace))
364
    {
365
        if (!create_new_node(syntax_node_type::ObjectField, false))
366
            return false;
367
        if (!parse_name())
368
            return false;
369
        if (!expect_token(token_type::Colon))
370
            return false;
371
        if (!parse_value_literal(is_constant))
372
            return false;
373

374
        pop_node();
375
    }
376

377
    if (!expect_token(token_type::RBrace))
378
        return false;
379

380
    return pop_node();
381
}
382

383
bool parser_t::parse_type_reference()
384
{
385
    if (parse_node(syntax_node_type::ListType, token_type::LBracket, ParseNodeIfMatch))
386
    {
387
        if (!parse_type_reference())
388
            return false;
389
        if (!expect_token(token_type::RBracket))
390
            return false;
391
    }
392
    else
393
    {
394
        if (!parse_named_type())
395
            return false;
396
    }
397

398
    auto &node = current_node();
399
    index_t size = last_token_end - node.pos;
400
    node.name = std::string_view(doc.doc_value).substr(node.pos, size);
401

402
    if (current_token.of_type(token_type::NotNull))
403
    {
404
        current_node().nullability = nullability_t::Required;
405
        if (!next_token())
406
            return false;
407
    }
408
    else
409
    {
410
        current_node().nullability = nullability_t::Optional;
411
    }
412

413
    current_node().parent->definition_type = &current_node();
414
   return pop_node();
415
}
416

417
bool parser_t::parse_directives(bool is_constant)
418
{
419
    size_t child_count = current_node().children.size();
420
    while (current_token.of_type(token_type::Directive))
421
    {
422
        if (!parse_directive(is_constant))
423
            return false;
424
    }
425
    auto &node = current_node();
426
    node.directives = node.children.subspan(child_count);
427
    return true;
428
}
429

430
bool parser_t::parse_directive(bool is_constant)
431
{
432
    if (!parse_node(syntax_node_type::Directive, token_type::Directive))
433
        return false;
434
    syntax_node &directive_node = current_node();
435
    directive_node.name = directive_node.content;
436

437
    if (!parse_arguments(is_constant))
438
        return false;
439

440
    return pop_node();
441
}
442

443
bool parser_t::parse_arguments(bool is_constant)
444
{
445
    if (!current_token.of_type(token_type::LParen))
446
        return true;
447

448
    size_t child_count = current_node().children.size();
449

450
    if (!next_token())
451
        return false;
452

453
    while (!current_token.of_type(token_type::RParen))
454
    {
455
        if (!create_new_node(syntax_node_type::Argument, false))
456
            return false;
457
        if (!parse_name())
458
            return false;
459
        if (!expect_token(token_type::Colon))
460
            return false;
461
        if (!parse_value_literal(is_constant))
462
            return false;
463

464
        pop_node();
465
    }
466

467
    auto &node = current_node();
468
    node.variables = node.children.subspan(child_count);
469

470
    return expect_token(token_type::RParen);
471
}
472

473
bool parser_t::parse_selection_set()
474
{
475
    if (!parse_node(syntax_node_type::SelectionSet, token_type::LBrace))
476
        return false;
477
    syntax_node &selection_set_node = current_node();
478
    selection_set_node.parent->selection_set = &selection_set_node;
479

480
    while (!current_token.of_type(token_type::RBrace))
481
    {
482
        if (!parse_selection())
483
            return false;
484
    }
485
    if (!expect_token(token_type::RBrace))
486
        return false;
487

488
   return pop_node();
489
}
490

491
bool parser_t::parse_selection()
492
{
493
    if (parse_fragment())
494
        return true;
495
    if (parse_field())
496
        return true;
497
    return false;
498
}
499

500
bool parser_t::parse_field()
501
{
502
    if (parse_node(syntax_node_type::Comment, token_type::Comment, ParseNodeIfMatch | NodeIsLeaf))
503
        return true;
504

505
    if (!parse_node(syntax_node_type::Field, token_type::Name))
506
    {
507
        report_error(error_code_t::NameExpected, "Field name expected at {}", current_token.pos);
508
        return false;
509
    }
510
    syntax_node &selection_node = current_node();
511
    selection_node.alias = selection_node.name = selection_node.content;
512

513
    if (current_token.of_type(token_type::Colon))
514
    {
515
        if (!expect_token(token_type::Colon))
516
            return false;
517
        if (!parse_name())
518
        {
519
            report_error(error_code_t::NameExpected, "Field name expected at {}", current_token.pos);
520
            return false;
521
        }
522

523
        selection_node.name = current_token.value;
524
        selection_node.alias = selection_node.content;
525
        selection_node.size = selection_node.pos;
526
    }
527

528
    if (!parse_arguments(false))
529
        return false;
530

531
    if (!parse_directives(false))
532
        return false;
533

534
    if (current_token.of_type(token_type::LBrace))
535
    {
536
        if (!parse_selection_set())
537
            return false;
538
    }
539

540
    return pop_node();
541
}
542

543
bool parser_t::parse_fragment()
544
{
545
    if (!parse_node(syntax_node_type::FragmentSpread, token_type::FragmentSpread, ParseNodeIfMatch))
546
        return false;
547

548
    if (!current_token.of_type(token_type::Name))
549
        return report_error(error_code_t::SyntaxError, "fragment name or inline fragment expected");
550

551
    auto &node = current_node();
552
    // inline fragment
553
    if (current_token.value == "on")
554
    {
555
        if (!next_token())
556
            return false;
557
        if (!parse_node(syntax_node_type::NamedType, token_type::Name, NodeIsLeaf))
558
            return false;
559
        node.definition_type = &last_node();
560

561
        if (!parse_directives(false))
562
            return false;
563

564
        if (!parse_selection_set())
565
            return false;
566
    }
567
    else
568
    {
569
        node.name = current_token.value;
570
        if (!parse_directives(false))
571
            return false;
572
    }
573
    return pop_node();
574
}
575

576
bool parser_t::parse_type_extension()
577
{
578
    if (!next_token())
579
        return report_error(error_code_t::UnexpectedEndOfDocument, "Type extension expected");
580
    switch (hash_crc32(current_token.value))
581
    {
582
    case "scalar"_crc32:
583
        return parse_scalar_type_definition(syntax_node_type::ScalarTypeExtension);
584
    case "union"_crc32:
585
        return parse_union_type_definition(syntax_node_type::UnionTypeExtension);
586
    case "enum"_crc32:
587
        return parse_enum_type_definition(syntax_node_type::EnumTypeExtension);
588
    case "type"_crc32:
589
        return parse_object_type_definition(syntax_node_type::ObjectTypeExtension, "type");
590
    case "interface"_crc32:
591
        return parse_object_type_definition(syntax_node_type::InterfaceTypeDefinition, "interface");
592
    case "imput"_crc32:
593
        return parse_input_object_type_definition(syntax_node_type::InterfaceTypeDefinition);
594
    }
595
    return report_error(error_code_t::InvalidToken, "Expected type, scalar, union, enum, interface or union. Got: {}", current_token.value);
596
}
597

598
bool parser_t::parse_input_object_type_definition(syntax_node_type node_type)
599
{
600
    if (!parse_keyword_token(node_type, "input"))
601
        return false;
602

603
    if (!parse_name())
604
        return false;
605

606
    if (!parse_directives(false))
607
        return false;
608

609
    if(node_type == syntax_node_type::InputObjectTypeExtension && !current_token.of_type(token_type::LBrace))
610
        return pop_node();
611

612
    if (!parse_argument_definitions(token_type::LBrace, token_type::RBrace))
613
        return false;
614

615
    return pop_node();
616
}
617

618
bool parser_t::parse_enum_type_definition(syntax_node_type node_type)
619
{
620
    if (!parse_keyword_token(node_type, "enum"))
621
        return false;
622

623
    if (!parse_name())
624
        return false;
625

626
    if (!parse_directives(false))
627
        return false;
628

629
    if (node_type == syntax_node_type::EnumTypeExtension && !current_token.of_type(token_type::LBrace))
630
        return pop_node();
631

632
    if (!expect_token(token_type::LBrace))
633
        return false;
634

635
    if (!parse_enum_values())
636
        return false;
637

638
    if (!expect_token(token_type::RBrace))
639
        return false;
640

641
    return pop_node();
642
}
643

644
bool parser_t::parse_enum_values()
645
{
646
    while (!current_token.of_type(token_type::RBrace))
647
    {
648
        if (!parse_enum_value())
649
        {
650
            return false;
651
        }
652
    }
653
    return true;
654
}
655

656
bool parser_t::parse_enum_value()
657
{
658
    if (!create_new_node(syntax_node_type::EnumValueDefinition, false))
659
        return false;
660
    if (!parse_name())
661
        return false;
662
    if (!parse_directives(true))
663
        return false;
664
    return pop_node();
665
}
666

667
bool parser_t::parse_union_type_definition(syntax_node_type node_type)
668
{
669
    if (!parse_keyword_token(node_type, "union"))
670
        return false;
671

672
    if (!parse_name())
673
        return false;
674

675
    if (!parse_directives(false))
676
        return false;
677

678
    if (node_type == syntax_node_type::UnionTypeExtension && !current_token.of_type(token_type::Equal))
679
       return pop_node();
680

681
    if (!expect_token(token_type::Equal))
682
        return false;
683

684
    if (current_token.of_type(token_type::Union))
685
    {
686
        if (!next_token())
687
            return false;
688
    }
689
    size_t child_count = current_node().children.size();
690
    do
691
    {
692
        if (!parse_named_type(NodeIsLeaf))
693
            return false;
694

695
        if (!current_token.of_type(token_type::Union))
696
            break;
697

698
        if (!next_token())
699
            break;
700

701
    } while (true);
702
    auto &node = current_node();
703
    node.implements = node.children.subspan(child_count);
704

705
    return pop_node();
706
}
707

708
bool parser_t::parse_object_type_definition(syntax_node_type type, std::string_view keyword)
709
{
710
    if (!parse_keyword_token(type, keyword))
711
        return false;
712

713
    if (!parse_name())
714
        return false;
715

716
    if (!parse_implements_interfaces())
717
        return false;
718

719
    if (!parse_directives(false))
720
        return false;
721

722
    if (!expect_token(token_type::LBrace))
723
        return false;
724

725
    while (!current_token.of_type(token_type::RBrace))
726
    {
727
        if (!parse_description())
728
            return false;
729
        if (!create_new_node(syntax_node_type::FieldDefinition, false))
730
            return false;
731
        if (!parse_name())
732
            return false;
733
        if (!parse_argument_definitions())
734
            return false;
735
        if (!expect_token(token_type::Colon))
736
            return false;
737
        if (!parse_type_reference())
738
            return false;
739
        if (!parse_directives(true))
740
            return false;
741
        pop_node();
742
    }
743
    pop_node();
744
    return expect_token(token_type::RBrace);
745
}
746

747
bool parser_t::parse_implements_interfaces()
748
{
749
    if (!expect_keyword_token("implements", true))
750
        return true;
751

752
    if (current_token.of_type(token_type::And))
753
    {
754
        if (!next_token())
755
            return false;
756
    }
757
    size_t child_count = current_node().children.size();
758

759
    while (current_token.of_type(token_type::Name))
760
    {
761
        if (!parse_named_type(NodeIsLeaf))
762
            return false;
763

764
        if (current_token.of_type(token_type::And))
765
        {
766
            if (!next_token())
767
                return false;
768
        }
769
    }
770
    auto &node = current_node();
771
    node.implements = node.children.subspan(child_count);
772

773
    return true;
774
}
775

776
bool parser_t::parse_argument_definitions(token_type start, token_type end)
777
{
778
    size_t child_count = current_node().children.size();
779
    if (!current_token.of_type(start))
780
        return true;
781

782
    if (!next_token())
783
        return false;
784

785
    while (!current_token.of_type(end))
786
    {
787
        if (!parse_argument_definition())
788
        {
789
            return false;
790
        }
791
    }
792
    auto &node = current_node();
793
    node.arguments = node.children.subspan(child_count);
794
    return expect_token(end);
795
}
796

797
bool parser_t::parse_argument_definition()
798
{
799
    if (!parse_description())
800
        return false;
801
    if (!create_new_node(syntax_node_type::InputValueDefinition, false))
802
        return false;
803
    if (!parse_name())
804
        return false;
805
    if (!expect_token(token_type::Colon))
806
        return false;
807
    if (!parse_type_reference())
808
        return false;
809
    if (current_token.of_type(token_type::Equal))
810
    {
811
        if (!next_token())
812
            return false;
813
        if (!parse_value_literal(true))
814
            return false;
815
    }
816
    if (!parse_directives(true))
817
        return false;
818
   return pop_node();
819
}
820

821
bool parser_t::parse_name(token_type name_type)
822
{
823
    if (!current_token.of_type(name_type))
824
    {
825
        return unexpected_token();
826
    }
827
    syntax_node &parent = current_node();
828

829
    parent.name = current_token.value;
830
    return next_token();
831
}
832

833
bool parser_t::parse_scalar_type_definition(syntax_node_type node_type)
834
{
835
    if (!parse_node(node_type, token_type::Name))
836
        return false;
837

838
    if (!parse_name())
839
        return false;
840

841
    if (!parse_directives(true))
842
        return false;
843

844
   return pop_node();
845
}
846

847
bool parser_t::parse_schema_definition(syntax_node_type node_type)
848
{
849
    if (!parse_keyword_token(node_type, "schema"))
850
        return false;
851

852
    if (!parse_directives(true))
853
        return false;
854

855
    if(node_type == syntax_node_type::SchemaExtension && !current_token.of_type(token_type::LBrace))
856
        return pop_node();
857

858
    if (!expect_token(token_type::LBrace))
859
        return false;
860

861
    while (!current_token.of_type(token_type::RBrace))
862
    {
863
        if (!parse_operation_type_definition())
864
            return false;
865
    }
866

867
    if (!expect_token(token_type::RBrace))
868
        return false;
869

870
    return  pop_node();
871
}
872

873
bool parser_t::parse_operation_type_definition()
874
{
875
    if (!create_new_node(syntax_node_type::OperationTypeDefinition, false))
876
        return false;
877

878
    auto op_type = parse_operation_type(current_token.value);
879
    if (op_type == operation_type_t::None)
880
    {
881
        report_error(error_code_t::InvalidOperationType, "Invalid Operation Type: {}", current_token.value);
882
        return false;
883
    }
884
    current_node().operation_type = op_type;
885

886
    if (!expect_token(token_type::Name))
887
        return false;
888

889
    if (!expect_token(token_type::Colon))
890
        return false;
891
    if (!parse_named_type(NodeIsLeaf))
892
        return false;
893

894
    return pop_node();
895
}
896

897
bool parser_t::parse_named_type(NodeParseOptions opts)
898
{
899
    if (!parse_node(syntax_node_type::NamedType, token_type::Name, opts))
900
        return false;
901
    last_node().name = last_node().content;
902
    return true;
903
}
904

905
bool parser_t::parse_directive_definition()
906
{
907
    if (!parse_keyword_token(syntax_node_type::DirectiveDefinition, "directive"))
908
        return false;
909

910
    if (!parse_name(token_type::Directive))
911
        return false;
912

913
    if (!parse_argument_definitions())
914
        return false;
915

916
    if (!current_token.of_type(token_type::Name))
917
        return report_error(error_code_t::UnexpectedToken, "Expected to see 'on' or 'repeatable' keyword while parsing directive '{}' at {}", current_node().name, current_token.pos);
918

919
    if (expect_keyword_token("repeatable", true))
920
        current_node().directive_target = directive_target_t::IsRepeatable;
921

922
    if (!expect_keyword_token("on"))
923
        return false;
924

925
    directive_target_t target = directive_target_t::None;
926
    do
927
    {
928
        if (current_token.of_type(token_type::Union))
929
        {
930
            if (!next_token())
931
                return false;
932
        }
933
        if (!current_token.of_type(token_type::Name))
934
        {
935
            break;
936
        }
937
        target = parse_directive_target(current_token.value);
938
        if (target == directive_target_t::None)
939
        {
940
            break;
941
        }
942
        current_node().directive_target |= target;
943
        if (!next_token())
944
            return false;
945

946
    } while (target != directive_target_t::None);
947

948
    return pop_node();
949
}
950

951
bool parser_t::parse_fragment_definition()
952
{
953
    if (!parse_keyword_token(syntax_node_type::FragmentDefinition, "fragment"))
954
        return false;
955
    if (!parse_name())
956
        return false;
957
    if (!parse_variable_definitions())
958
        return false;
959
    if (!expect_keyword_token("on"))
960
        return false;
961
    if (!parse_named_type(NodeIsLeaf))
962
        return false;
963
    if (!parse_directives(false))
964
        return false;
965
    if (!parse_selection_set())
966
        return false;
967
   return pop_node();
968
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.