forked from orangeduck/mpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpc.h
289 lines (228 loc) · 8.49 KB
/
mpc.h
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
/*
** mpc - Micro Parser Combinator library for C
** https://github.com/orangeduck/mpc
** Daniel Holden - [email protected]
** Licensed under BSD3
*/
#ifndef mpc_h
#define mpc_h
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdarg.h>
/*
** Error Type
*/
struct mpc_err_t;
typedef struct mpc_err_t mpc_err_t;
int mpc_err_line(mpc_err_t* x);
int mpc_err_column(mpc_err_t* x);
char mpc_err_unexpected(mpc_err_t* x);
char** mpc_err_expected(mpc_err_t* x, int* num);
char* mpc_err_filename(mpc_err_t* x);
void mpc_err_delete(mpc_err_t* x);
void mpc_err_print(mpc_err_t* x);
void mpc_err_print_to(mpc_err_t* x, FILE* f);
void mpc_err_msg(mpc_err_t* x, char* out, int* outn, int outmax);
/*
** Parsing
*/
typedef void mpc_val_t;
typedef union {
mpc_err_t* error;
mpc_val_t* output;
} mpc_result_t;
struct mpc_parser_t;
typedef struct mpc_parser_t mpc_parser_t;
bool mpc_parse(const char* filename, const char* s, mpc_parser_t* p, mpc_result_t* r);
bool mpc_parse_file(const char* filename, FILE* f, mpc_parser_t* p, mpc_result_t* r);
bool mpc_parse_filename(const char* filename, mpc_parser_t* p, mpc_result_t* r);
/*
** Function Types
*/
typedef void(*mpc_dtor_t)(mpc_val_t*);
typedef mpc_val_t*(*mpc_apply_t)(mpc_val_t*);
typedef mpc_val_t*(*mpc_apply_to_t)(mpc_val_t*,void*);
typedef mpc_val_t*(*mpc_fold_t)(mpc_val_t*,mpc_val_t*);
typedef mpc_val_t*(*mpc_afold_t)(int,mpc_val_t**);
typedef mpc_val_t*(*mpc_lift_t)(void);
/*
** Building a Parser
*/
void mpc_delete(mpc_parser_t* p);
mpc_parser_t* mpc_new(const char* name);
mpc_parser_t* mpc_define(mpc_parser_t* p, mpc_parser_t* a);
mpc_parser_t* mpc_undefine(mpc_parser_t* p);
void mpc_cleanup(int n, ...);
void mpc_cleanup_va(int n, va_list va);
/*
** Basic Parsers
*/
mpc_parser_t* mpc_pass(void);
mpc_parser_t* mpc_fail(void);
mpc_parser_t* mpc_lift(mpc_lift_t f);
mpc_parser_t* mpc_lift_val(mpc_val_t* x);
mpc_parser_t* mpc_any(void);
mpc_parser_t* mpc_char(char c);
mpc_parser_t* mpc_range(char s, char e);
mpc_parser_t* mpc_oneof(const char* s);
mpc_parser_t* mpc_noneof(const char* s);
mpc_parser_t* mpc_satisfy(bool(*f)(char));
mpc_parser_t* mpc_string(const char* s);
/*
** Core Parsers
*/
mpc_parser_t* mpc_expect(mpc_parser_t* a, const char* expected);
mpc_parser_t* mpc_apply(mpc_parser_t* a, mpc_apply_t f);
mpc_parser_t* mpc_apply_to(mpc_parser_t* a, mpc_apply_to_t f, void* x);
mpc_parser_t* mpc_not(mpc_parser_t* a, mpc_dtor_t da);
mpc_parser_t* mpc_not_else(mpc_parser_t* a, mpc_dtor_t da, mpc_lift_t lf);
mpc_parser_t* mpc_maybe(mpc_parser_t* a);
mpc_parser_t* mpc_maybe_else(mpc_parser_t* a, mpc_lift_t lf);
mpc_parser_t* mpc_many(mpc_parser_t* a, mpc_fold_t f);
mpc_parser_t* mpc_many_else(mpc_parser_t* a, mpc_fold_t f, mpc_lift_t lf);
mpc_parser_t* mpc_many1(mpc_parser_t* a, mpc_fold_t f);
mpc_parser_t* mpc_count(mpc_parser_t* a, mpc_dtor_t da, mpc_fold_t f, int n);
mpc_parser_t* mpc_count_else(mpc_parser_t* a, mpc_dtor_t da, mpc_fold_t f, int n, mpc_lift_t lf);
mpc_parser_t* mpc_else(mpc_parser_t* a, mpc_parser_t* b);
mpc_parser_t* mpc_also(mpc_parser_t* a, mpc_parser_t* b, mpc_dtor_t da, mpc_fold_t f);
mpc_parser_t* mpc_bind(mpc_parser_t* a, mpc_parser_t* b, mpc_dtor_t da, mpc_fold_t f);
mpc_parser_t* mpc_or(int n, ...);
mpc_parser_t* mpc_and(int n, mpc_afold_t f, ...);
mpc_parser_t* mpc_or_va(int n, va_list va);
mpc_parser_t* mpc_and_va(int n, mpc_afold_t f, va_list va);
/*
** Common Parsers
*/
mpc_parser_t* mpc_eoi(void);
mpc_parser_t* mpc_soi(void);
mpc_parser_t* mpc_space(void);
mpc_parser_t* mpc_spaces(void);
mpc_parser_t* mpc_whitespace(void);
mpc_parser_t* mpc_newline(void);
mpc_parser_t* mpc_tab(void);
mpc_parser_t* mpc_escape(void);
mpc_parser_t* mpc_digit(void);
mpc_parser_t* mpc_hexdigit(void);
mpc_parser_t* mpc_octdigit(void);
mpc_parser_t* mpc_digits(void);
mpc_parser_t* mpc_hexdigits(void);
mpc_parser_t* mpc_octdigits(void);
mpc_parser_t* mpc_lower(void);
mpc_parser_t* mpc_upper(void);
mpc_parser_t* mpc_alpha(void);
mpc_parser_t* mpc_underscore(void);
mpc_parser_t* mpc_alphanum(void);
mpc_parser_t* mpc_int(void);
mpc_parser_t* mpc_hex(void);
mpc_parser_t* mpc_oct(void);
mpc_parser_t* mpc_number(void);
mpc_parser_t* mpc_real(void);
mpc_parser_t* mpc_float(void);
mpc_parser_t* mpc_char_lit(void);
mpc_parser_t* mpc_string_lit(void);
mpc_parser_t* mpc_regex_lit(void);
mpc_parser_t* mpc_ident(void);
/*
** Useful Parsers
*/
mpc_parser_t* mpc_start(mpc_parser_t* a);
mpc_parser_t* mpc_end(mpc_parser_t* a, mpc_dtor_t da);
mpc_parser_t* mpc_enclose(mpc_parser_t* a, mpc_dtor_t da);
mpc_parser_t* mpc_strip(mpc_parser_t* a);
mpc_parser_t* mpc_tok(mpc_parser_t* a);
mpc_parser_t* mpc_sym(const char* s);
mpc_parser_t* mpc_total(mpc_parser_t* a, mpc_dtor_t da);
mpc_parser_t* mpc_between(mpc_parser_t* a, mpc_dtor_t ad, const char* o, const char* c);
mpc_parser_t* mpc_parens(mpc_parser_t* a, mpc_dtor_t ad);
mpc_parser_t* mpc_braces(mpc_parser_t* a, mpc_dtor_t ad);
mpc_parser_t* mpc_brackets(mpc_parser_t* a, mpc_dtor_t ad);
mpc_parser_t* mpc_squares(mpc_parser_t* a, mpc_dtor_t ad);
mpc_parser_t* mpc_tok_between(mpc_parser_t* a, mpc_dtor_t ad, const char* o, const char* c);
mpc_parser_t* mpc_tok_parens(mpc_parser_t* a, mpc_dtor_t ad);
mpc_parser_t* mpc_tok_braces(mpc_parser_t* a, mpc_dtor_t ad);
mpc_parser_t* mpc_tok_brackets(mpc_parser_t* a, mpc_dtor_t ad);
mpc_parser_t* mpc_tok_squares(mpc_parser_t* a, mpc_dtor_t ad);
/*
** Regular Expression Parsers
*/
mpc_parser_t* mpc_re(const char* re);
/*
** Common Fold Functions
*/
void mpcf_dtor_null(mpc_val_t* x);
mpc_val_t* mpcf_lift_null(void);
mpc_val_t* mpcf_lift_emptystr(void);
mpc_val_t* mpcf_free(mpc_val_t* x);
mpc_val_t* mpcf_int(mpc_val_t* x);
mpc_val_t* mpcf_hex(mpc_val_t* x);
mpc_val_t* mpcf_oct(mpc_val_t* x);
mpc_val_t* mpcf_float(mpc_val_t* x);
mpc_val_t* mpcf_escape(mpc_val_t* x);
mpc_val_t* mpcf_unescape(mpc_val_t* x);
mpc_val_t* mpcf_fst(mpc_val_t* x, mpc_val_t* y);
mpc_val_t* mpcf_snd(mpc_val_t* x, mpc_val_t* y);
mpc_val_t* mpcf_fst_free(mpc_val_t* x, mpc_val_t* y);
mpc_val_t* mpcf_snd_free(mpc_val_t* x, mpc_val_t* y);
mpc_val_t* mpcf_freefold(mpc_val_t* t, mpc_val_t* x);
mpc_val_t* mpcf_strfold(mpc_val_t* t, mpc_val_t* x);
mpc_val_t* mpcf_afst(int n, mpc_val_t** xs);
mpc_val_t* mpcf_asnd(int n, mpc_val_t** xs);
mpc_val_t* mpcf_atrd(int n, mpc_val_t** xs);
mpc_val_t* mpcf_astrfold(int n, mpc_val_t** xs);
mpc_val_t* mpcf_between_free(int n, mpc_val_t** xs);
mpc_val_t* mpcf_maths(int n, mpc_val_t** xs);
/*
** Printing
*/
void mpc_print(mpc_parser_t* p);
/*
** AST
*/
typedef struct mpc_ast_t {
char* tag;
char* contents;
int children_num;
struct mpc_ast_t** children;
} mpc_ast_t;
void mpc_ast_delete(mpc_ast_t* a);
mpc_ast_t* mpc_ast_new(const char* tag, const char* contents);
mpc_ast_t* mpc_ast_build(int n, const char* tag, ...);
mpc_ast_t* mpc_ast_insert_root(mpc_ast_t* a);
void mpc_ast_add_child(mpc_ast_t* r, mpc_ast_t* a);
void mpc_ast_tag(mpc_ast_t* a, const char* t);
void mpc_ast_print(mpc_ast_t* a);
bool mpc_ast_eq(mpc_ast_t* a, mpc_ast_t* b);
mpc_val_t* mpcf_fold_ast(mpc_val_t* a, mpc_val_t* b);
mpc_val_t* mpcf_afold_ast(int n, mpc_val_t** as);
mpc_val_t* mpcf_apply_str_ast(mpc_val_t* c);
mpc_parser_t* mpca_tag(mpc_parser_t* a, const char* t);
mpc_parser_t* mpca_total(mpc_parser_t* a);
mpc_parser_t* mpca_not(mpc_parser_t* a);
mpc_parser_t* mpca_maybe(mpc_parser_t* a);
mpc_parser_t* mpca_many(mpc_parser_t* a);
mpc_parser_t* mpca_many1(mpc_parser_t* a);
mpc_parser_t* mpca_count(mpc_parser_t* a, int n);
mpc_parser_t* mpca_else(mpc_parser_t* a, mpc_parser_t* b);
mpc_parser_t* mpca_also(mpc_parser_t* a, mpc_parser_t* b);
mpc_parser_t* mpca_bind(mpc_parser_t* a, mpc_parser_t* b);
mpc_parser_t* mpca_or(int n, ...);
mpc_parser_t* mpca_and(int n, ...);
mpc_parser_t* mpca_grammar(const char* grammar, ...);
mpc_parser_t* mpca_grammar_va(const char* grammar, va_list va);
void mpca_lang(const char* language, ...);
void mpca_lang_va(const char* language, va_list va);
void mpca_lang_file(const char* filename, ...);
void mpca_lang_file_va(const char* filename, va_list va);
/*
** Testing
*/
bool mpc_unmatch(mpc_parser_t* p, const char* s, void* d,
bool(*tester)(void*, void*),
mpc_dtor_t destructor,
void(*printer)(void*));
bool mpc_match(mpc_parser_t* p, const char* s, void* d,
bool(*tester)(void*, void*),
mpc_dtor_t destructor,
void(*printer)(void*));
#endif