Skip to content

Commit 8fd351b

Browse files
authored
Merge pull request #45 from graphql/schema-parser
Schema parser
2 parents 2ae5c17 + af91437 commit 8fd351b

15 files changed

+2470
-974
lines changed

GraphQLParser.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,56 @@ namespace graphql {
2020

2121
// Given properly-configured yylex, run the parser and return the
2222
// result.
23-
static std::unique_ptr<ast::Node> doParse(const char **outError, yyscan_t scanner) {
23+
static std::unique_ptr<ast::Node> doParse(const char **outError, yyscan_t scanner, bool enableSchema) {
2424
Node *outAST;
25-
yy::GraphQLParserImpl parser(&outAST, outError, scanner);
25+
yy::GraphQLParserImpl parser(enableSchema, &outAST, outError, scanner);
2626
int failure = parser.parse();
2727
return !failure ? std::unique_ptr<ast::Node>(outAST) : nullptr;
2828
}
2929

30-
std::unique_ptr<ast::Node> parseString(const char *text, const char **error) {
30+
static std::unique_ptr<ast::Node> parseStringImpl(const char *text, const char **error, bool enableSchema) {
3131
yyscan_t scanner;
3232
struct LexerExtra extra;
3333
yylex_init_extra(&extra, &scanner);
3434
YY_BUFFER_STATE buffer = yy_scan_string(text, scanner);
3535
yy_switch_to_buffer(buffer, scanner);
3636

37-
auto result = doParse(error, scanner);
37+
auto result = doParse(error, scanner, enableSchema);
3838
yylex_destroy(scanner);
3939
return result;
4040
}
4141

42-
std::unique_ptr<ast::Node> parseFile(FILE *file, const char **error) {
42+
std::unique_ptr<ast::Node> parseString(const char *text, const char **error) {
43+
return parseStringImpl(text, error, false);
44+
}
45+
46+
std::unique_ptr<ast::Node> parseStringWithExperimentalSchemaSupport(
47+
const char *text, const char **error) {
48+
return parseStringImpl(text, error, true);
49+
}
50+
51+
static std::unique_ptr<ast::Node> parseFileImpl(
52+
FILE *file, const char **error, bool enableSchema) {
4353
yyscan_t scanner;
4454
struct LexerExtra extra;
4555
yylex_init_extra(&extra, &scanner);
4656
yyset_in(file, scanner);
4757

48-
auto result = doParse(error, scanner);
58+
auto result = doParse(error, scanner, enableSchema);
4959
yylex_destroy(scanner);
5060

5161
return result;
5262
}
5363

64+
std::unique_ptr<ast::Node> parseFile(FILE *file, const char **error) {
65+
return parseFileImpl(file, error, false);
66+
}
67+
68+
std::unique_ptr<ast::Node> parseFileWithExperimentalSchemaSupport(
69+
FILE *file, const char **error) {
70+
return parseFileImpl(file, error, true);
71+
}
72+
73+
5474
}
5575
}

GraphQLParser.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,26 @@ namespace ast {
3232
*/
3333
std::unique_ptr<ast::Node> parseString(const char *text, const char **error);
3434

35+
/**
36+
* Like parseString, but enables support for the experimental type
37+
* definition syntax from https://github.com/facebook/graphql/pull/90 .
38+
*/
39+
std::unique_ptr<ast::Node> parseStringWithExperimentalSchemaSupport(
40+
const char *text, const char **error);
41+
3542
/**
3643
* Read and parse GraphQL source from the given file, returning an
3744
* AST. Returns nullptr on error and, if error is not null, places an
3845
* error string in error that must be freed with free(3).
3946
*/
4047
std::unique_ptr<ast::Node> parseFile(FILE *file, const char **error);
4148

49+
/**
50+
* Like parseFile, but enables support for the experimental type
51+
* definition syntax from https://github.com/facebook/graphql/pull/90 .
52+
*/
53+
std::unique_ptr<ast::Node> parseFileWithExperimentalSchemaSupport(
54+
FILE *file, const char **error);
55+
4256
}
4357
}

ast/ast.ast

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,23 @@
77
# O for option in a union.
88
# Scalar type ontology: string, boolean
99

10-
# Location tracking for Identifier is probably useful for error messages.
10+
# Definitions other than OperationDefinition and FragmentDefinition
11+
# are experimental additions for schema parsing. (We don't support
12+
# nested unions in the AST mini-language, so I've flattened and elided
13+
# TypeSystemDefinition and TypeDefinition.)
1114
U Definition
1215
O OperationDefinition
1316
O FragmentDefinition
17+
O SchemaDefinition
18+
O ScalarTypeDefinition
19+
O ObjectTypeDefinition
20+
O InterfaceTypeDefinition
21+
O UnionTypeDefinition
22+
O EnumTypeDefinition
23+
O InputObjectTypeDefinition
24+
O TypeExtensionDefinition
25+
O DirectiveDefinition
26+
1427

1528
T Document
1629
P Definition definitions
@@ -124,3 +137,65 @@ S Type type
124137

125138
T Name
126139
S string value
140+
141+
T SchemaDefinition
142+
P? Directive directives
143+
P OperationTypeDefinition operationTypes
144+
145+
T OperationTypeDefinition
146+
S OperationKind operation
147+
S NamedType type
148+
149+
T ScalarTypeDefinition
150+
S Name name
151+
P? Directive directives
152+
153+
T ObjectTypeDefinition
154+
S Name name
155+
P? NamedType interfaces
156+
P? Directive directives
157+
P FieldDefinition fields
158+
159+
T FieldDefinition
160+
S Name name
161+
P? InputValueDefinition arguments
162+
S Type type
163+
P? Directive directives
164+
165+
T InputValueDefinition
166+
S Name name
167+
S Type type
168+
S? Value defaultValue
169+
P? Directive directives
170+
171+
T InterfaceTypeDefinition
172+
S Name name
173+
P? Directive directives
174+
P FieldDefinition fields
175+
176+
T UnionTypeDefinition
177+
S Name name
178+
P? Directive directives
179+
P NamedType types
180+
181+
T EnumTypeDefinition
182+
S Name name
183+
P? Directive directives
184+
P EnumValueDefinition values
185+
186+
T EnumValueDefinition
187+
S Name name
188+
P? Directive directives
189+
190+
T InputObjectTypeDefinition
191+
S Name name
192+
P? Directive directives
193+
P InputValueDefinition fields
194+
195+
T TypeExtensionDefinition
196+
S ObjectTypeDefinition definition
197+
198+
T DirectiveDefinition
199+
S Name name
200+
P? InputValueDefinition arguments
201+
P Name locations

c/GraphQLParser.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,21 @@ struct GraphQLAstNode *graphql_parse_string(const char *text, const char **error
1717
return (struct GraphQLAstNode *)facebook::graphql::parseString(text, error).release();
1818
}
1919

20+
struct GraphQLAstNode *graphql_parse_string_with_experimental_schema_support(
21+
const char *text, const char **error) {
22+
return (struct GraphQLAstNode *)facebook::graphql::parseStringWithExperimentalSchemaSupport(
23+
text, error).release();
24+
}
25+
2026
struct GraphQLAstNode *graphql_parse_file(FILE *file, const char **error) {
2127
return (struct GraphQLAstNode *)facebook::graphql::parseFile(file, error).release();
2228
}
2329

30+
struct GraphQLAstNode *graphql_parse_file_with_experimental_schema_support(
31+
FILE *file, const char **error) {
32+
return (struct GraphQLAstNode *)facebook::graphql::parseFileWithExperimentalSchemaSupport(file, error).release();
33+
}
34+
2435
void graphql_error_free(const char *error) {
2536
std::free((void *)error);
2637
}

c/GraphQLParser.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ struct GraphQLAstNode;
2828
* error message is placed in error and must be freed with
2929
* graphql_error_free().
3030
*/
31-
struct GraphQLAstNode *graphql_parse_string(const char *text,
32-
const char **error);
31+
struct GraphQLAstNode *graphql_parse_string(
32+
const char *text, const char **error);
33+
34+
struct GraphQLAstNode *graphql_parse_string_with_experimental_schema_support(
35+
const char *text, const char **error);
3336

3437
/**
3538
* Read and parse GraphQL source from the given file, returning an
@@ -40,6 +43,9 @@ struct GraphQLAstNode *graphql_parse_string(const char *text,
4043
*/
4144
struct GraphQLAstNode *graphql_parse_file(FILE *file, const char **error);
4245

46+
struct GraphQLAstNode *graphql_parse_file_with_experimental_schema_support(
47+
FILE *file, const char **error);
48+
4349
/**
4450
* Frees an error.
4551
*/

0 commit comments

Comments
 (0)