forked from graphql/libgraphqlparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.ypp
693 lines (576 loc) · 26.6 KB
/
parser.ypp
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
684
685
686
687
688
689
690
691
692
693
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
%require "3"
%skeleton "lalr1.cc"
%defines
%define parser_class_name {GraphQLParserImpl}
%define api.token.prefix {TOK_}
%define parse.error verbose
%code requires
{
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include "Ast.h"
using facebook::graphql::ast::Node;
using facebook::graphql::ast::Name;
using facebook::graphql::ast::Definition;
using facebook::graphql::ast::Document;
using facebook::graphql::ast::OperationDefinition;
using facebook::graphql::ast::VariableDefinition;
using facebook::graphql::ast::Variable;
using facebook::graphql::ast::SelectionSet;
using facebook::graphql::ast::Selection;
using facebook::graphql::ast::Field;
using facebook::graphql::ast::Argument;
using facebook::graphql::ast::FragmentSpread;
using facebook::graphql::ast::InlineFragment;
using facebook::graphql::ast::FragmentDefinition;
using facebook::graphql::ast::Value;
using facebook::graphql::ast::IntValue;
using facebook::graphql::ast::FloatValue;
using facebook::graphql::ast::StringValue;
using facebook::graphql::ast::BooleanValue;
using facebook::graphql::ast::NullValue;
using facebook::graphql::ast::EnumValue;
using facebook::graphql::ast::ListValue;
using facebook::graphql::ast::ObjectValue;
using facebook::graphql::ast::ObjectField;
using facebook::graphql::ast::Directive;
using facebook::graphql::ast::Type;
using facebook::graphql::ast::NamedType;
using facebook::graphql::ast::ListType;
using facebook::graphql::ast::NonNullType;
// Experimental schema support.
using facebook::graphql::ast::SchemaDefinition;
using facebook::graphql::ast::ScalarTypeDefinition;
using facebook::graphql::ast::ObjectTypeDefinition;
using facebook::graphql::ast::InterfaceTypeDefinition;
using facebook::graphql::ast::UnionTypeDefinition;
using facebook::graphql::ast::EnumTypeDefinition;
using facebook::graphql::ast::InputObjectTypeDefinition;
using facebook::graphql::ast::TypeExtensionDefinition;
using facebook::graphql::ast::DirectiveDefinition;
using facebook::graphql::ast::SchemaDefinition;
using facebook::graphql::ast::OperationTypeDefinition;
using facebook::graphql::ast::ScalarTypeDefinition;
using facebook::graphql::ast::ObjectTypeDefinition;
using facebook::graphql::ast::FieldDefinition;
using facebook::graphql::ast::InputValueDefinition;
using facebook::graphql::ast::InterfaceTypeDefinition;
using facebook::graphql::ast::UnionTypeDefinition;
using facebook::graphql::ast::EnumTypeDefinition;
using facebook::graphql::ast::EnumValueDefinition;
using facebook::graphql::ast::InputObjectTypeDefinition;
using facebook::graphql::ast::TypeExtensionDefinition;
using facebook::graphql::ast::DirectiveDefinition;
union yystype { \
const char *str; \
const char *heapStr; \
Name *name; \
Definition *definition; \
Document *document; \
OperationDefinition *operationDefinition; \
VariableDefinition *variableDefinition; \
Variable *variable; \
SelectionSet *selectionSet; \
Selection *selection; \
Field *field; \
Argument *argument; \
FragmentSpread *fragmentSpread; \
InlineFragment *inlineFragment; \
FragmentDefinition *fragmentDefinition; \
Value *value; \
IntValue *intValue; \
FloatValue *floatValue; \
StringValue *stringValue; \
BooleanValue *booleanValue; \
NullValue *nullValue; \
EnumValue *enumValue; \
ListValue *arrayValue; \
ObjectValue *objectValue; \
ObjectField *objectField; \
Directive *directive; \
Type *type; \
NamedType *namedType; \
ListType *listType; \
NonNullType *nonNullType; \
\
std::vector<std::unique_ptr<Definition>> *definitionList; \
std::vector<std::unique_ptr<VariableDefinition>> *variableDefinitionList; \
std::vector<std::unique_ptr<Selection>> *selectionList; \
std::vector<std::unique_ptr<Field>> *fieldList; \
std::vector<std::unique_ptr<Argument>> *argumentList; \
std::vector<std::unique_ptr<Value>> *valueList; \
std::vector<std::unique_ptr<ObjectField>> *objectFieldList; \
std::vector<std::unique_ptr<Directive>> *directiveList; \
\
SchemaDefinition *schemaDefinition; \
ScalarTypeDefinition *scalarTypeDefinition; \
ObjectTypeDefinition *objectTypeDefinition; \
InterfaceTypeDefinition *interfaceTypeDefinition; \
UnionTypeDefinition *unionTypeDefinition; \
EnumTypeDefinition *enumTypeDefinition; \
InputObjectTypeDefinition *inputObjectTypeDefinition; \
TypeExtensionDefinition *typeExtensionDefinition; \
DirectiveDefinition *directiveDefinition; \
OperationTypeDefinition *operationTypeDefinition; \
InputValueDefinition *inputValueDefinition; \
FieldDefinition *fieldDefinition; \
EnumValueDefinition *enumValueDefinition; \
\
std::vector<std::unique_ptr<OperationTypeDefinition>> *operationTypeDefinitionList; \
std::vector<std::unique_ptr<NamedType>> *typeNameList; \
std::vector<std::unique_ptr<InputValueDefinition>> *inputValueDefinitionList; \
std::vector<std::unique_ptr<FieldDefinition>> *fieldDefinitionList; \
std::vector<std::unique_ptr<Name>> *nameList; \
std::vector<std::unique_ptr<EnumValueDefinition>> *enumValueDefinitionList; \
};
#define YYSTYPE union yystype
#define YYLTYPE yy::location
}
%lex-param { void *scanner }
%parse-param { bool enableSchema } { Node **outAST } { const char **outError } { void *scanner }
%locations
%code
{
#include "lexer.h"
#include "syntaxdefs.h"
}
%token EOF 0
%token <str> DIRECTIVE "directive"
%token <str> ENUM "enum"
%token <str> EXTEND "extend"
%token <str> FALSE "false"
%token <str> FRAGMENT "fragment"
%token <str> IMPLEMENTS "implements"
%token <str> INPUT "input"
%token <str> INTERFACE "interface"
%token <str> MUTATION "mutation"
%token <str> NULL "null"
%token <str> QUERY "query"
%token <str> ON "on"
%token <str> SCALAR "scalar"
%token <str> SCHEMA "schema"
%token <str> SUBSCRIPTION "subscription"
%token <str> TRUE "true"
%token <str> TYPE "type"
%token <str> UNION "union"
%token BANG "!"
%token LPAREN "("
%token RPAREN ")"
%token ELLIPSIS "..."
%token COLON ":"
%token EQUAL "="
%token AT "@"
%token LBRACKET "["
%token RBRACKET "]"
%token LBRACE "{"
%token PIPE "|"
%token RBRACE "}"
%token <str> VARIABLE
%token <str> INTEGER
%token <str> FLOAT
%token <str> STRING
%token <str> IDENTIFIER
%type <variable> variable
%type <intValue> int_value
%type <floatValue> float_value
%type <stringValue> string_value
%type <document> start
%type <document> document
%type <name> fragment_name
%type <name> name
%type <name> name_opt
%type <definitionList> definition_list
%type <definition> definition
%type <definition> schema_gate
%type <operationDefinition> operation_definition
%type <variableDefinitionList> variable_definitions
%type <variableDefinitionList> variable_definition_list
%type <variableDefinition> variable_definition
%type <value> default_value_opt
%type <value> default_value
%type <selectionSet> selection_set
%type <selectionSet> selection_set_opt
%type <selectionList> selection_list
%type <selection> selection
%type <field> field
%type <argumentList> arguments_opt
%type <argumentList> arguments
%type <argumentList> argument_list
%type <argument> argument
%type <fragmentSpread> fragment_spread
%type <inlineFragment> inline_fragment
%type <fragmentDefinition> fragment_definition
%type <namedType> type_condition
%type <value> value
%type <value> value_const
%type <booleanValue> boolean_value
%type <nullValue> null_value
%type <enumValue> enum_value
%type <arrayValue> list_value
%type <arrayValue> list_value_const
%type <valueList> value_list
%type <valueList> value_const_list
%type <objectValue> object_value
%type <objectValue> object_value_const
%type <objectFieldList> object_field_list
%type <objectFieldList> object_field_const_list
%type <objectField> object_field
%type <objectField> object_field_const
%type <directiveList> directives
%type <directiveList> directives_opt
%type <directiveList> directive_list
%type <directive> directive
%type <type> type
%type <namedType> type_name
%type <listType> list_type
%type <nonNullType> non_null_type
%type <heapStr> operation_type
%type <schemaDefinition> schema_definition;
%type <scalarTypeDefinition> scalar_type_definition;
%type <objectTypeDefinition> object_type_definition;
%type <interfaceTypeDefinition> interface_type_definition;
%type <unionTypeDefinition> union_type_definition;
%type <enumTypeDefinition> enum_type_definition;
%type <inputObjectTypeDefinition> input_object_type_definition;
%type <typeExtensionDefinition> type_extension_definition;
%type <directiveDefinition> directive_definition;
%type <operationTypeDefinition> operation_type_definition;
%type <operationTypeDefinitionList> operation_type_definition_list;
%type <typeNameList> type_name_list;
%type <typeNameList> implements_interfaces_opt;
%type <typeNameList> union_members;
%type <fieldDefinition> field_definition;
%type <fieldDefinitionList> field_definition_list;
%type <inputValueDefinitionList> arguments_definition_opt;
%type <inputValueDefinitionList> arguments_definition;
%type <inputValueDefinitionList> input_value_definition_list;
%type <inputValueDefinition> input_value_definition;
%type <enumValueDefinition> enum_value_definition;
%type <nameList> directive_locations;
%type <enumValueDefinitionList> enum_value_definition_list;
%destructor { } <str>
%destructor { free((void *)$$); } <heapStr>
%destructor { } <document> /* we steal it and put it in outAST, don't free! */
%destructor { delete $$; } <*>
%printer { yyoutput << $$; } <str>
%%
start: document { *outAST = $1; }
;
/* All of the non-identifier tokens are to accommodate various flavors
of name that don't include those tokens. */
fragment_name: DIRECTIVE { $$ = new Name(@1, strdup($1)); }
| ENUM { $$ = new Name(@1, strdup($1)); }
| EXTEND { $$ = new Name(@1, strdup($1)); }
| FALSE { $$ = new Name(@1, strdup($1)); }
| FRAGMENT { $$ = new Name(@1, strdup($1)); }
| IDENTIFIER { $$ = new Name(@1, strdup($1)); }
| IMPLEMENTS { $$ = new Name(@1, strdup($1)); }
| INPUT { $$ = new Name(@1, strdup($1)); }
| INTERFACE { $$ = new Name(@1, strdup($1)); }
| MUTATION { $$ = new Name(@1, strdup($1)); }
| NULL { $$ = new Name(@1, strdup($1)); }
| QUERY { $$ = new Name(@1, strdup($1)); }
| SCALAR { $$ = new Name(@1, strdup($1)); }
| SCHEMA { $$ = new Name(@1, strdup($1)); }
| SUBSCRIPTION { $$ = new Name(@1, strdup($1)); }
| TRUE { $$ = new Name(@1, strdup($1)); }
| TYPE { $$ = new Name(@1, strdup($1)); }
| UNION { $$ = new Name(@1, strdup($1)); }
;
name: fragment_name
| ON { $$ = new Name(@1, strdup($1)); }
;
name_opt:
%empty {$$ = nullptr;}
| name
;
/* 2.2 Document */
document: definition_list { $$ = new Document(@$, $1); }
;
definition_list:definition { $$ = new std::vector<std::unique_ptr<Definition>>(); $$->emplace_back($1); }
| definition_list definition { $1->emplace_back($2); $$ = $1; }
;
definition: operation_definition { $$ = static_cast<Definition *>($1); }
| fragment_definition { $$ = static_cast<Definition *>($1); }
| schema_gate {
if (!enableSchema) {
error(@$, "schema support disabled");
// %destructor doesn't work with YYERROR. See
// https://www.gnu.org/software/bison/manual/html_node/Destructor-Decl.html
delete $$;
YYERROR;
}
$$ = static_cast<Definition *>($1);
}
;
schema_gate: schema_definition { $$ = static_cast<Definition *>($1); }
| scalar_type_definition { $$ = static_cast<Definition *>($1); }
| object_type_definition { $$ = static_cast<Definition *>($1); }
| interface_type_definition { $$ = static_cast<Definition *>($1); }
| union_type_definition { $$ = static_cast<Definition *>($1); }
| enum_type_definition { $$ = static_cast<Definition *>($1); }
| input_object_type_definition { $$ = static_cast<Definition *>($1); }
| type_extension_definition { $$ = static_cast<Definition *>($1); }
| directive_definition { $$ = static_cast<Definition *>($1); }
;
/* 2.2.1 Operations */
operation_definition:
selection_set { $$ = new OperationDefinition(@$, strdup("query"), nullptr, nullptr, nullptr, $1); }
| operation_type name_opt selection_set { $$ = new OperationDefinition(@$, $1, $2, nullptr, nullptr, $3); }
| operation_type name_opt variable_definitions selection_set { $$ = new OperationDefinition(@$, $1, $2, $3, nullptr, $4); }
| operation_type name_opt directives selection_set { $$ = new OperationDefinition(@$, $1, $2, nullptr, $3, $4); }
| operation_type name_opt variable_definitions directives selection_set { $$ = new OperationDefinition(@$, $1, $2, $3, $4, $5); }
;
operation_type: QUERY { $$ = strdup($1); }
| MUTATION { $$ = strdup($1); }
| SUBSCRIPTION { $$ = strdup($1); }
;
variable_definitions:
"(" variable_definition_list ")" { $$ = $2; }
;
variable_definition_list:
variable_definition { $$ = new std::vector<std::unique_ptr<VariableDefinition>>(); $$->emplace_back($1); }
| variable_definition_list variable_definition { $1->emplace_back($2); $$ = $1; }
;
variable: VARIABLE { $$ = new Variable(@$, new Name(@1, strdup($1))); }
;
variable_definition:
variable ":" type default_value_opt { $$ = new VariableDefinition(@$, $1, $3, $4); }
;
default_value_opt:
%empty { $$ = nullptr; }
| default_value
;
default_value: "=" value_const { $$ = $2; }
;
selection_set:
"{" selection_list "}" { $$ = new SelectionSet(@$, $2); }
;
selection_set_opt:
%empty { $$ = nullptr; }
| selection_set
;
selection_list: selection { $$ = new std::vector<std::unique_ptr<Selection>>(); $$->emplace_back($1); }
| selection_list selection { $1->emplace_back($2); $$ = $1; }
;
selection: field { $$ = static_cast<Selection *>($1); }
| fragment_spread { $$ = static_cast<Selection *>($1); }
| inline_fragment { $$ = static_cast<Selection *>($1); }
;
field: name arguments_opt directives_opt selection_set_opt { $$ = new Field(@$, nullptr, $1, $2, $3, $4); }
| name ":" name arguments_opt directives_opt selection_set_opt { $$ = new Field(@$, $1, $3, $4, $5, $6); }
;
arguments: "(" argument_list ")" { $$ = $2; }
;
arguments_opt: %empty { $$ = nullptr; }
| arguments { $$ = $1; }
;
argument_list: argument { $$ = new std::vector<std::unique_ptr<Argument>>(); $$->emplace_back($1); }
| argument_list argument { $1->emplace_back($2); $$ = $1; }
;
argument: name ":" value { $$ = new Argument(@$, $1, $3); }
;
/* 2.2.6 Fragments */
fragment_spread:
"..." fragment_name directives_opt { $$ = new FragmentSpread(@$, $2, $3); }
;
inline_fragment:
"..." "on" type_condition directives_opt selection_set { $$ = new InlineFragment(@$, $3, $4, $5); }
| "..." directives_opt selection_set { $$ = new InlineFragment(@$, nullptr, $2, $3); }
;
fragment_definition:
"fragment" fragment_name "on" type_condition directives_opt selection_set { $$ = new FragmentDefinition(@$, $2, $4, $5, $6); }
;
type_condition: type_name
;
/* 2.2.7 Input Values */
value: variable { $$ = static_cast<Value *>($1); }
| int_value { $$ = static_cast<Value *>($1); }
| float_value { $$ = static_cast<Value *>($1); }
| string_value { $$ = static_cast<Value *>($1); }
| boolean_value { $$ = static_cast<Value *>($1); }
| null_value { $$ = static_cast<Value *>($1); }
| enum_value { $$ = static_cast<Value *>($1); }
| list_value { $$ = static_cast<Value *>($1); }
| object_value { $$ = static_cast<Value *>($1); }
;
int_value: INTEGER { $$ = new IntValue(@$, strdup($1)); }
;
float_value: FLOAT { $$ = new FloatValue(@$, strdup($1)); }
;
string_value: STRING { $$ = new StringValue(@$, strdup($1)); }
;
value_const: int_value { $$ = static_cast<Value *>($1); }
| float_value { $$ = static_cast<Value *>($1); }
| string_value { $$ = static_cast<Value *>($1); }
| boolean_value { $$ = static_cast<Value *>($1); }
| null_value { $$ = static_cast<Value *>($1); }
| enum_value { $$ = static_cast<Value *>($1); }
| list_value_const { $$ = static_cast<Value *>($1); }
| object_value_const { $$ = static_cast<Value *>($1); }
;
boolean_value: TRUE { $$ = new BooleanValue(@$, true); }
| FALSE { $$ = new BooleanValue(@$, false); }
;
null_value: NULL { $$ = new NullValue(@$); }
;
enum_value: DIRECTIVE { $$ = new EnumValue(@$, strdup($1)); }
| ENUM { $$ = new EnumValue(@$, strdup($1)); }
| EXTEND { $$ = new EnumValue(@$, strdup($1)); }
| FRAGMENT { $$ = new EnumValue(@$, strdup($1)); }
| IDENTIFIER { $$ = new EnumValue(@$, strdup($1)); }
| IMPLEMENTS { $$ = new EnumValue(@$, strdup($1)); }
| INPUT { $$ = new EnumValue(@$, strdup($1)); }
| INTERFACE { $$ = new EnumValue(@$, strdup($1)); }
| MUTATION { $$ = new EnumValue(@$, strdup($1)); }
| ON { $$ = new EnumValue(@$, strdup($1)); }
| QUERY { $$ = new EnumValue(@$, strdup($1)); }
| SCALAR { $$ = new EnumValue(@$, strdup($1)); }
| SCHEMA { $$ = new EnumValue(@$, strdup($1)); }
| SUBSCRIPTION { $$ = new EnumValue(@$, strdup($1)); }
| TYPE { $$ = new EnumValue(@$, strdup($1)); }
| UNION { $$ = new EnumValue(@$, strdup($1)); }
;
/* 2.2.7.6 List Value */
/* REVIEW: the empty case is inefficient; consider implementing
ListValue manually. Don't forget to also do list_value_const. */
list_value: "[" "]" { $$ = new ListValue(@$, new std::vector<std::unique_ptr<Value>>()); }
| "[" value_list "]" { $$ = new ListValue(@$, $2); }
;
value_list: value { $$ = new std::vector<std::unique_ptr<Value>>(); $$->emplace_back($1); }
| value_list value { $1->emplace_back($2); $$ = $1; }
;
list_value_const:
"[" "]" { $$ = new ListValue(@$, new std::vector<std::unique_ptr<Value>>()); }
| "[" value_const_list "]" { $$ = new ListValue(@$, $2); }
;
value_const_list:
value_const { $$ = new std::vector<std::unique_ptr<Value>>(); $$->emplace_back($1); }
| value_const_list value_const { $1->emplace_back($2); $$ = $1; }
;
/* 2.2.7.7 Object Value */
/* REVIEW: Inefficient, like ListValue. */
object_value: "{" "}" { $$ = new ObjectValue(@$, new std::vector<std::unique_ptr<ObjectField>>()); }
| "{" object_field_list "}" { $$ = new ObjectValue(@$, $2); }
;
object_field_list:
object_field { $$ = new std::vector<std::unique_ptr<ObjectField>>(); $$->emplace_back($1); }
| object_field_list object_field { $1->emplace_back($2); $$ = $1; }
;
object_field: name ":" value { $$ = new ObjectField(@$, $1, $3); }
;
object_value_const:
"{" "}" { $$ = new ObjectValue(@$, new std::vector<std::unique_ptr<ObjectField>>()); }
| "{" object_field_const_list "}" { $$ = new ObjectValue(@$, $2); }
;
object_field_const_list:
object_field_const { $$ = new std::vector<std::unique_ptr<ObjectField>>(); $$->emplace_back($1); }
| object_field_const_list object_field_const { $1->emplace_back($2); $$ = $1; }
;
object_field_const: name ":" value_const { $$ = new ObjectField(@$, $1, $3); }
;
/* 2.2.10 Directives */
directives: directive_list
;
directives_opt: %empty { $$ = nullptr; }
| directives
;
directive_list: directive { $$ = new std::vector<std::unique_ptr<Directive>>(); $$->emplace_back($1); }
| directive_list directive { $1->emplace_back($2); $$ = $1; }
;
directive: "@" name arguments_opt { $$ = new Directive(@$, $2, $3); }
;
/* 2.2.9 Types */
type: type_name { $$ = static_cast<Type *>($1); }
| list_type { $$ = static_cast<Type *>($1); }
| non_null_type { $$ = static_cast<Type *>($1); }
;
type_name: name { $$ = new NamedType(@$, $1); }
;
list_type: "[" type "]" { $$ = new ListType(@$, $2); }
;
non_null_type: type_name "!" { $$ = new NonNullType(@$, $1); }
| list_type "!" { $$ = new NonNullType(@$, $1); }
;
/* Experimental schema parsing support. */
schema_definition: SCHEMA directives_opt "{" operation_type_definition_list "}" { $$ = new SchemaDefinition(@$, $2, $4); }
;
operation_type_definition_list:
operation_type_definition { $$ = new std::vector<std::unique_ptr<OperationTypeDefinition>>(); $$->emplace_back($1); }
| operation_type_definition_list operation_type_definition { $1->emplace_back($2); $$ = $1; }
;
operation_type_definition:
operation_type ":" type_name { $$ = new OperationTypeDefinition(@$, $1, $3); }
;
scalar_type_definition: SCALAR name directives_opt { $$ = new ScalarTypeDefinition(@$, $2, $3); }
;
object_type_definition: TYPE name implements_interfaces_opt directives_opt "{" field_definition_list "}" { $$ = new ObjectTypeDefinition(@$, $2, $3, $4, $6); }
;
implements_interfaces_opt: %empty { $$ = nullptr; }
| IMPLEMENTS type_name_list { $$ = $2; }
;
type_name_list: type_name { $$ = new std::vector<std::unique_ptr<NamedType>>(); $$->emplace_back($1); }
| type_name_list type_name { $1->emplace_back($2); $$ = $1; }
;
field_definition: name arguments_definition_opt ":" type directives_opt { $$ = new FieldDefinition(@$, $1, $2, $4, $5); }
;
field_definition_list:
field_definition { $$ = new std::vector<std::unique_ptr<FieldDefinition>>(); $$->emplace_back($1); }
| field_definition_list field_definition { $1->emplace_back($2); $$ = $1; }
;
arguments_definition_opt: %empty { $$ = nullptr; }
| arguments_definition { $$ = $1; }
;
arguments_definition: "(" input_value_definition_list ")" { $$ = $2; }
;
input_value_definition_list: input_value_definition { $$ = new std::vector<std::unique_ptr<InputValueDefinition>>(); $$->emplace_back($1); }
| input_value_definition_list input_value_definition { $1->emplace_back($2); $$ = $1; }
;
input_value_definition: name ":" type default_value_opt directives_opt { $$ = new InputValueDefinition(@$, $1, $3, $4, $5); }
interface_type_definition: INTERFACE name directives_opt "{" field_definition_list "}" { $$ = new InterfaceTypeDefinition(@$, $2, $3, $5); }
;
union_type_definition: UNION name directives_opt "=" union_members { $$ = new UnionTypeDefinition(@$, $2, $3, $5); }
;
union_members: type_name { $$ = new std::vector<std::unique_ptr<NamedType>>(); $$->emplace_back($1); }
| union_members "|" type_name { $1->emplace_back($3); $$ = $1; }
;
enum_type_definition: ENUM name directives_opt "{" enum_value_definition_list "}" { $$ = new EnumTypeDefinition(@$, $2, $3, $5); }
;
enum_value_definition: name directives_opt { $$ = new EnumValueDefinition(@$, $1, $2); }
;
enum_value_definition_list:
enum_value_definition { $$ = new std::vector<std::unique_ptr<EnumValueDefinition>>(); $$->emplace_back($1); }
| enum_value_definition_list enum_value_definition { $1->emplace_back($2); $$ = $1; }
;
input_object_type_definition: INPUT name directives_opt "{" input_value_definition_list "}" { $$ = new InputObjectTypeDefinition(@$, $2, $3, $5); }
;
type_extension_definition: EXTEND object_type_definition { $$ = new TypeExtensionDefinition(@$, $2); }
;
directive_definition: DIRECTIVE "@" name arguments_definition_opt ON directive_locations { $$ = new DirectiveDefinition(@$, $3, $4, $6); }
;
directive_locations:
name { $$ = new std::vector<std::unique_ptr<Name>>(); $$->emplace_back($1); }
| directive_locations "|" name { $1->emplace_back($3); $$ = $1; }
;
%%
void yy::GraphQLParserImpl::error(const yy::location &loc, const std::string &str) {
std::ostringstream out;
out << loc << ": " << str;
if (outError) {
*outError = strdup(out.str().c_str());
}
}
/* Workaround for syntax_error ctor being marked inline, which causes link
errors if used from lexer.lpp. */
yy::GraphQLParserImpl::syntax_error make_error(const yy::location &loc, const std::string &str) {
return yy::GraphQLParserImpl::syntax_error(loc, str);
}