]> git.ozlabs.org Git - ccan/blob - ccan/graphql/graphql.h
rune: fix encoding of alternates which need escapes.
[ccan] / ccan / graphql / graphql.h
1 /* MIT (BSD) license - see LICENSE file for details */
2 #ifndef __GRAPHQL_H__
3 #define __GRAPHQL_H__ 1
4
5 #include <stdio.h>
6
7 #include <ccan/list/list.h>
8 #include <ccan/tal/tal.h>
9
10 // Coding constants
11 #define GRAPHQL_SUCCESS ((const char *)NULL)
12
13 // The following structures constitute the AST returned by the parser.
14
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
20 };
21
22 struct graphql_directives {
23         struct graphql_directive *first;
24         void *data; // for application use
25 };
26
27 struct graphql_named_type {
28         struct graphql_token *name;
29         void *data; // for application use
30 };
31
32 struct graphql_type {
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
37 };
38
39 struct graphql_default_value {
40         struct graphql_value *val;
41         void *data; // for application use
42 };
43
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
51 };
52
53 struct graphql_variable_definitions {
54         struct graphql_variable_definition *first;
55         void *data; // for application use
56 };
57
58 struct graphql_variable {
59         struct graphql_token *name;
60         void *data; // for application use
61 };
62
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
68 };
69
70 struct graphql_object_value {
71         struct graphql_object_field *first;
72         void *data; // for application use
73 };
74
75 struct graphql_list_value {
76         struct graphql_token *val;
77         void *data; // for application use
78 };
79
80 struct graphql_enum_value {
81         struct graphql_token *val;
82         void *data; // for application use
83 };
84
85 struct graphql_null_value {
86         struct graphql_token *val;
87         void *data; // for application use
88 };
89
90 struct graphql_string_value {
91         struct graphql_token *val;
92         void *data; // for application use
93 };
94
95 struct graphql_boolean_value {
96         struct graphql_token *val;
97         void *data; // for application use
98 };
99
100 struct graphql_float_value {
101         struct graphql_token *val;
102         void *data; // for application use
103 };
104
105 struct graphql_int_value {
106         struct graphql_token *val;
107         void *data; // for application use
108 };
109
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
121 };
122
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
128 };
129
130 struct graphql_type_condition {
131         struct graphql_named_type *named_type;
132         void *data; // for application use
133 };
134
135 struct graphql_fragment_name {
136         struct graphql_token *name;
137         void *data; // for application use
138 };
139
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
146 };
147
148 struct graphql_fragment_spread {
149         struct graphql_fragment_name *name;
150         struct graphql_directives *directives;
151         void *data; // for application use
152 };
153
154 struct graphql_alias {
155         struct graphql_token *name;
156         void *data; // for application use
157 };
158
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
164 };
165
166 struct graphql_arguments {
167         struct graphql_argument *first;
168         void *data; // for application use
169 };
170
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
178 };
179
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
186 };
187
188 struct graphql_selection_set {
189         struct graphql_selection *first;
190         void *data; // for application use
191 };
192
193 struct graphql_operation_type {
194         struct graphql_token *op_type;
195         void *data; // for application use
196 };
197
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
205 };
206
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
212 };
213
214 struct graphql_executable_document {
215         struct graphql_executable_definition *first_def;
216         void *data; // for application use
217 };
218
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
224 };
225
226 struct graphql_document {
227         struct graphql_definition *first_def;
228         void *data; // for application use
229 };
230
231 enum token_type_enum {
232         NAME            = 'a',
233         INTEGER         = 'i',
234         FLOAT           = 'f',
235         STRING          = 's',
236         PUNCT_BANG      = '!',
237         PUNCT_SH__      = '$',
238         PUNCT_AMP       = '&',
239         PUNCT_LPAR      = '(',
240         PUNCT_RPAR      = ')',
241         PUNCT_COLON     = ':',
242         PUNCT_EQ        = '=',
243         PUNCT_AT        = '@',
244         PUNCT_LBRACKET  = '[',
245         PUNCT_RBRACKET  = ']',
246         PUNCT_LBRACE    = '{',
247         PUNCT_PIPE      = '|',
248         PUNCT_RBRACE    = '}',
249         PUNCT_SPREAD    = 0x2026, // spread operator (triple dot)
250 };
251
252 struct graphql_token {
253         struct list_node node;
254         enum token_type_enum token_type;
255         char *token_string;
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
261 };
262
263 /* The lexer.
264  * INPUTS:
265  *      input - string to parse
266  *      ctx - parent tal context or NULL
267  *      tokens - a variable to receive the resulting token list
268  * RETURN:
269  *      GRAPHQL_SUCCESS or an error string.
270  */
271 const char *graphql_lex(const tal_t *ctx, const char *input, struct list_head **tokens);
272
273 /* The parser.
274  * INPUTS:
275  *      tokens - the list produced by the lexer
276  *      doc - a variable to receive the resulting abstract syntax tree (AST)
277  * OPERATION:
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.
282  * RETURN:
283  *      GRAPHQL_SUCCESS or an error string.
284  */
285 const char *graphql_parse(struct list_head *tokens, struct graphql_executable_document **doc);
286
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);
289
290 #endif
291