1 /* MIT (BSD) license - see LICENSE file for details */
3 #define __GRAPHQL_H__ 1
7 #include <ccan/list/list.h>
8 #include <ccan/tal/tal.h>
11 #define GRAPHQL_SUCCESS ((const char *)NULL)
13 // The following structures constitute the AST returned by the parser.
15 struct graphql_directive {
16 struct graphql_directive *next;
17 struct graphql_token *name;
18 struct graphql_arguments *args;
19 void *data; // for application use
22 struct graphql_directives {
23 struct graphql_directive *first;
24 void *data; // for application use
27 struct graphql_named_type {
28 struct graphql_token *name;
29 void *data; // for application use
33 struct graphql_named_type *named;
34 // struct graphql_list_type *list;
35 // struct graphql_non_null_type *non_null;
36 void *data; // for application use
39 struct graphql_default_value {
40 struct graphql_value *val;
41 void *data; // for application use
44 struct graphql_variable_definition {
45 struct graphql_variable_definition *next;
46 struct graphql_variable *var;
47 struct graphql_type *type;
48 struct graphql_default_value *default_val;
49 struct graphql_directives *directives;
50 void *data; // for application use
53 struct graphql_variable_definitions {
54 struct graphql_variable_definition *first;
55 void *data; // for application use
58 struct graphql_variable {
59 struct graphql_token *name;
60 void *data; // for application use
63 struct graphql_object_field {
64 struct graphql_object_field *next;
65 struct graphql_token *name;
66 struct graphql_value *val;
67 void *data; // for application use
70 struct graphql_object_value {
71 struct graphql_object_field *first;
72 void *data; // for application use
75 struct graphql_list_value {
76 struct graphql_token *val;
77 void *data; // for application use
80 struct graphql_enum_value {
81 struct graphql_token *val;
82 void *data; // for application use
85 struct graphql_null_value {
86 struct graphql_token *val;
87 void *data; // for application use
90 struct graphql_string_value {
91 struct graphql_token *val;
92 void *data; // for application use
95 struct graphql_boolean_value {
96 struct graphql_token *val;
97 void *data; // for application use
100 struct graphql_float_value {
101 struct graphql_token *val;
102 void *data; // for application use
105 struct graphql_int_value {
106 struct graphql_token *val;
107 void *data; // for application use
110 struct graphql_value {
111 struct graphql_variable *var;
112 struct graphql_int_value *int_val;
113 struct graphql_float_value *float_val;
114 struct graphql_boolean_value *bool_val;
115 struct graphql_string_value *str_val;
116 struct graphql_null_value *null_val;
117 struct graphql_enum_value *enum_val;
118 struct graphql_list_value *list_val;
119 struct graphql_object_value *obj_val;
120 void *data; // for application use
123 struct graphql_inline_fragment {
124 struct graphql_type_condition *type_cond;
125 struct graphql_directives *directives;
126 struct graphql_selection_set *sel_set;
127 void *data; // for application use
130 struct graphql_type_condition {
131 struct graphql_named_type *named_type;
132 void *data; // for application use
135 struct graphql_fragment_name {
136 struct graphql_token *name;
137 void *data; // for application use
140 struct graphql_fragment_definition {
141 struct graphql_fragment_name *name;
142 struct graphql_type_condition *type_cond;
143 struct graphql_directives *directives;
144 struct graphql_selection_set *sel_set;
145 void *data; // for application use
148 struct graphql_fragment_spread {
149 struct graphql_fragment_name *name;
150 struct graphql_directives *directives;
151 void *data; // for application use
154 struct graphql_alias {
155 struct graphql_token *name;
156 void *data; // for application use
159 struct graphql_argument {
160 struct graphql_argument *next;
161 struct graphql_token *name;
162 struct graphql_value *val;
163 void *data; // for application use
166 struct graphql_arguments {
167 struct graphql_argument *first;
168 void *data; // for application use
171 struct graphql_field {
172 struct graphql_alias *alias;
173 struct graphql_token *name;
174 struct graphql_arguments *args;
175 struct graphql_directives *directives;
176 struct graphql_selection_set *sel_set;
177 void *data; // for application use
180 struct graphql_selection {
181 struct graphql_selection *next;
182 struct graphql_field *field;
183 struct graphql_fragment_spread *frag_spread;
184 struct graphql_inline_fragment *inline_frag;
185 void *data; // for application use
188 struct graphql_selection_set {
189 struct graphql_selection *first;
190 void *data; // for application use
193 struct graphql_operation_type {
194 struct graphql_token *op_type;
195 void *data; // for application use
198 struct graphql_operation_definition {
199 struct graphql_operation_type *op_type;
200 struct graphql_token *op_name;
201 struct graphql_variable_definitions *vars;
202 struct graphql_directives *directives;
203 struct graphql_selection_set *sel_set;
204 void *data; // for application use
207 struct graphql_executable_definition {
208 struct graphql_executable_definition *next_def;
209 struct graphql_operation_definition *op_def;
210 struct graphql_fragment_definition *frag_def;
211 void *data; // for application use
214 struct graphql_executable_document {
215 struct graphql_executable_definition *first_def;
216 void *data; // for application use
219 struct graphql_definition {
220 struct graphql_definition *next_def;
221 struct graphql_executable_definition *executable_def;
222 struct graphql_type_system_definition_or_extension *type_system_def;
223 void *data; // for application use
226 struct graphql_document {
227 struct graphql_definition *first_def;
228 void *data; // for application use
231 enum token_type_enum {
244 PUNCT_LBRACKET = '[',
245 PUNCT_RBRACKET = ']',
249 PUNCT_SPREAD = 0x2026, // spread operator (triple dot)
252 struct graphql_token {
253 struct list_node node;
254 enum token_type_enum token_type;
256 unsigned int source_line;
257 unsigned int source_column;
258 unsigned int source_offset;
259 unsigned int source_len;
260 void *data; // for application use
265 * input - string to parse
266 * ctx - parent tal context or NULL
267 * tokens - a variable to receive the resulting token list
269 * GRAPHQL_SUCCESS or an error string.
271 const char *graphql_lex(const tal_t *ctx, const char *input, struct list_head **tokens);
275 * tokens - the list produced by the lexer
276 * doc - a variable to receive the resulting abstract syntax tree (AST)
278 * The token list is emptied during parsing, so far as the parsing
279 * succeeds. This allows the caller to inspect the line/char position
280 * of the next token (where the error likely is) and report that hint to
281 * the user in the form of an error message.
283 * GRAPHQL_SUCCESS or an error string.
285 const char *graphql_parse(struct list_head *tokens, struct graphql_executable_document **doc);
287 /* The lexer and parser in one function, for convenience. */
288 const char *graphql_lexparse(const tal_t *ctx, const char *input, struct list_head **tokens, struct graphql_executable_document **doc);