]> git.ozlabs.org Git - ccan/blob - ccan/graphql/graphql.h
94f7fa4d44f89a089a322bf52f73b78f04ad56ea
[ccan] / ccan / graphql / graphql.h
1 #ifndef __GRAPHQL_H__
2 #define __GRAPHQL_H__ 1
3
4 #include <stdio.h>
5
6 #include <ccan/list/list.h>
7 #include <ccan/tal/tal.h>
8
9 // Coding constants
10 #define GRAPHQL_SUCCESS 0
11
12 // The following structures constitute the AST returned by the parser.
13
14 struct graphql_directive {
15         struct graphql_directive *next;
16         struct graphql_token *name;
17         struct graphql_arguments *args;
18 };
19
20 struct graphql_directives {
21         struct graphql_directive *first;
22 };
23
24 struct graphql_named_type {
25         struct graphql_token *name;
26 };
27
28 struct graphql_type {
29         struct graphql_named_type *named;
30 //      struct graphql_list_type *list;
31 //      struct graphql_non_null_type *non_null;
32 };
33
34 struct graphql_default_value {
35         struct graphql_value *val;
36 };
37
38 struct graphql_variable_definition {
39         struct graphql_variable_definition *next;
40         struct graphql_variable *var;
41         struct graphql_type *type;
42         struct graphql_default_value *default_val;
43         struct graphql_directives *directives;
44 };
45
46 struct graphql_variable_definitions {
47         struct graphql_variable_definition *first;
48 };
49
50 struct graphql_variable {
51         struct graphql_token *name;
52 };
53
54 struct graphql_object_field {
55         struct graphql_object_field *next;
56         struct graphql_token *name;
57         struct graphql_value *val;
58 };
59
60 struct graphql_object_value {
61         struct graphql_object_field *first;
62 };
63
64 struct graphql_list_value {
65         struct graphql_token *val;
66 };
67
68 struct graphql_enum_value {
69         struct graphql_token *val;
70 };
71
72 struct graphql_null_value {
73         struct graphql_token *val;
74 };
75
76 struct graphql_string_value {
77         struct graphql_token *val;
78 };
79
80 struct graphql_boolean_value {
81         struct graphql_token *val;
82 };
83
84 struct graphql_float_value {
85         struct graphql_token *val;
86 };
87
88 struct graphql_int_value {
89         struct graphql_token *val;
90 };
91
92 struct graphql_value {
93         struct graphql_variable *var;
94         struct graphql_int_value *int_val;
95         struct graphql_float_value *float_val;
96         struct graphql_boolean_value *bool_val;
97         struct graphql_string_value *str_val;
98         struct graphql_null_value *null_val;
99         struct graphql_enum_value *enum_val;
100         struct graphql_list_value *list_val;
101         struct graphql_object_value *obj_val;
102 };
103
104 struct graphql_inline_fragment {
105         struct graphql_type_condition *type_cond;
106         struct graphql_directives *directives;
107         struct graphql_selection_set *sel_set;
108 };
109
110 struct graphql_type_condition {
111         struct graphql_named_type *named_type;
112 };
113
114 struct graphql_fragment_name {
115         struct graphql_token *name;
116 };
117
118 struct graphql_fragment_definition {
119         struct graphql_fragment_name *name;
120         struct graphql_type_condition *type_cond;
121         struct graphql_directives *directives;
122         struct graphql_selection_set *sel_set;
123 };
124
125 struct graphql_fragment_spread {
126         struct graphql_fragment_name *name;
127         struct graphql_directives *directives;
128 };
129
130 struct graphql_alias {
131         struct graphql_token *name;
132 };
133
134 struct graphql_argument {
135         struct graphql_argument *next;
136         struct graphql_token *name;
137         struct graphql_value *val;
138 };
139
140 struct graphql_arguments {
141         struct graphql_argument *first;
142 };
143
144 struct graphql_field {
145         struct graphql_alias *alias;
146         struct graphql_token *name;
147         struct graphql_arguments *args;
148         struct graphql_directives *directives;
149         struct graphql_selection_set *sel_set;
150 };
151
152 struct graphql_selection {
153         struct graphql_selection *next;
154         struct graphql_field *field;
155         struct graphql_fragment_spread *frag_spread;
156         struct graphql_inline_fragment *inline_frag;
157 };
158
159 struct graphql_selection_set {
160         struct graphql_selection *first;
161 };
162
163 struct graphql_operation_type {
164         struct graphql_token *op_type;
165 };
166
167 struct graphql_operation_definition {
168         struct graphql_operation_type *op_type;
169         struct graphql_token *op_name;
170         struct graphql_variable_definitions *vars;
171         struct graphql_directives *directives;
172         struct graphql_selection_set *sel_set;
173 };
174
175 struct graphql_executable_definition {
176         struct graphql_executable_definition *next_def;
177         struct graphql_operation_definition *op_def;
178         struct graphql_fragment_definition *frag_def;
179 };
180
181 struct graphql_executable_document {
182         struct graphql_executable_definition *first_def;
183 };
184
185 struct graphql_definition {
186         struct graphql_definition *next_def;
187         struct graphql_executable_definition *executable_def;
188         struct graphql_type_system_definition_or_extension *type_system_def;
189 };
190
191 struct graphql_document {
192         struct graphql_definition *first_def;
193 };
194
195
196 struct graphql_token {
197         struct list_node list;
198         unsigned int token_type;
199         unsigned int token_specific;
200         char *token_string;
201         unsigned int source_line;
202         unsigned int source_column;
203         unsigned int source_len;
204 };
205
206 /* The lexer.
207  * INPUTS:
208  *      input - string to parse
209  *      ctx - parent tal context or NULL
210  *      tokens - a variable to receive the resulting token list
211  * RETURN:
212  *      GRAPHQL_SUCCESS or an error string.
213  */
214 const char *graphql_lex(const char *input, const tal_t *ctx, struct list_head **tokens);
215
216 /* The parser.
217  * INPUTS:
218  *      tokens - the list produced by the lexer
219  *      doc - a variable to receive the resulting abstract syntax tree (AST)
220  * RETURN:
221  *      GRAPHQL_SUCCESS or an error string.
222  */
223 const char *graphql_parse(struct list_head *tokens, struct graphql_executable_document **doc);
224
225 /* The lexer and parser in one function, for convenience. */
226 const char *graphql_lexparse(const char *input, const tal_t *ctx, struct list_head **tokens, struct graphql_executable_document **doc);
227
228 #endif
229