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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
/*
* Copyright (C) 2020 Jo-Philipp Wich <jo@mein.io>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef __AST_H_
#define __AST_H_
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdarg.h>
#include <string.h>
#ifdef JSONC
#include <json.h>
#else
#include <json-c/json.h>
#endif
#define ALIGN(x) (((x) + sizeof(size_t) - 1) & -sizeof(size_t))
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#endif
#define JSON_C_TO_STRING_STRICT (1<<31)
enum uc_lex_state {
UT_LEX_IDENTIFY_BLOCK,
UT_LEX_BLOCK_COMMENT_START,
UT_LEX_BLOCK_EXPRESSION_START,
UT_LEX_BLOCK_EXPRESSION_EMIT_TAG,
UT_LEX_BLOCK_STATEMENT_START,
UT_LEX_BLOCK_COMMENT,
UT_LEX_IDENTIFY_TOKEN,
UT_LEX_PARSE_TOKEN,
UT_LEX_EOF
};
struct uc_op {
uint16_t type;
uint16_t is_first:1;
uint16_t is_op:1;
uint16_t is_overflow:1;
uint16_t is_postfix:1;
uint16_t is_for_in:1;
uint16_t is_list:1;
uint16_t is_reg_icase:1;
uint16_t is_reg_newline:1;
uint16_t is_reg_global:1;
uint16_t is_ellip:1;
uint16_t is_arrow:1;
uint32_t off;
struct json_object *val;
union {
struct {
struct json_object *proto;
size_t type;
void *data;
uint32_t off;
} tag;
struct {
uint32_t next;
uint32_t operand[4];
} tree;
};
};
struct uc_scope {
struct uc_scope *next;
struct json_object *scope, *parent;
size_t refs;
};
struct uc_source {
struct uc_source *next;
char *filename;
uint32_t off;
FILE *fp;
};
struct uc_function {
char *name;
union {
struct json_object *args;
void *cfn;
};
struct uc_scope *parent_scope;
struct uc_source *source;
uint32_t entry;
};
struct uc_callstack {
struct uc_callstack *next;
struct uc_function *function;
struct uc_scope *scope;
struct json_object *ctx;
uint32_t off;
};
struct uc_state {
struct uc_op *pool;
uint32_t poolsize;
uint32_t main;
uint8_t srand_called:1;
uint8_t trim_blocks:1;
uint8_t lstrip_blocks:1;
uint8_t strict_declarations:1;
struct {
enum uc_lex_state state;
uint8_t eof:1;
uint8_t skip_leading_whitespace:1;
uint8_t skip_leading_newline:1;
uint8_t within_expression_block:1;
uint8_t within_statement_block:1;
uint8_t semicolon_emitted:1;
uint8_t expect_div:1;
uint8_t is_escape:1;
size_t buflen;
char *buf, *bufstart, *bufend;
size_t lookbehindlen;
char *lookbehind;
const void *tok;
char esc[5];
uint8_t esclen;
int lead_surrogate;
size_t lastoff;
} lex;
struct json_object *ctx, *rval, *exception;
struct uc_scope *scopelist, *scope;
struct uc_source *sources, *source;
struct uc_callstack *callstack;
struct uc_function *function;
size_t calldepth;
};
struct uc_extended_type {
const char *name;
struct json_object *proto;
void (*free)(void *);
};
static inline bool uc_is_type(struct json_object *val, int type) {
struct uc_op *tag = json_object_get_userdata(val);
return (tag && tag->type == type);
};
#define OP(idx) (&state->pool[idx])
#define OP_POS(idx) OP(idx)->off
#define OP_VAL(idx) OP(idx)->val
#define OP_TYPE(idx) OP(idx)->type
#define OP_NEXT(idx) OP(idx)->tree.next
#define OP_IS_LIST(idx) OP(idx)->is_list
#define OP_IS_ELLIP(idx) OP(idx)->is_ellip
#define OP_IS_FOR_IN(idx) OP(idx)->is_for_in
#define OP_IS_POSTFIX(idx) OP(idx)->is_postfix
#define OPn_NUM ARRAY_SIZE(((struct uc_op *)NULL)->tree.operand)
#define OPn(idx, n) OP(idx)->tree.operand[n]
#define OPn_POS(idx, n) OP(OPn(idx, n))->off
#define OPn_VAL(idx, n) OP(OPn(idx, n))->val
#define OPn_TYPE(idx, n) OP(OPn(idx, n))->type
#define OPn_IS_LIST(idx, n) OP(OPn(idx, n))->is_list
#define OPn_IS_OVERFLOW(idx, n) OP(OPn(idx, n))->is_overflow
uint32_t uc_new_op(struct uc_state *s, int type, struct json_object *val, ...);
uint32_t uc_wrap_op(struct uc_state *s, uint32_t parent, ...);
uint32_t uc_append_op(struct uc_state *s, uint32_t a, uint32_t b);
struct json_object *uc_parse(struct uc_state *s, FILE *fp);
void uc_free(struct uc_state *s);
struct json_object *uc_new_func(struct uc_state *s, uint32_t decl, struct uc_scope *scope);
struct json_object *uc_new_object(struct json_object *proto);
struct json_object *uc_new_double(double v);
struct json_object *uc_new_null(void);
struct json_object *uc_new_regexp(const char *source, bool icase, bool newline, bool global, char **err);
__attribute__((format(printf, 3, 0)))
struct json_object *uc_new_exception(struct uc_state *s, uint32_t off, const char *fmt, ...);
struct uc_scope *uc_new_scope(struct uc_state *s, struct uc_scope *parent);
struct uc_scope *uc_parent_scope(struct uc_scope *scope);
struct uc_scope *uc_acquire_scope(struct uc_scope *scope);
void uc_release_scope(struct uc_scope *scope);
bool uc_register_extended_type(const char *name, struct json_object *proto, void (*freefn)(void *));
struct json_object *uc_set_extended_type(struct json_object *v, const char *name, void *data);
void **uc_get_extended_type(struct json_object *val, const char *name);
void *ParseAlloc(void *(*mfunc)(size_t));
void Parse(void *pParser, int type, uint32_t off, struct uc_state *s);
void ParseFree(void *pParser, void (*ffunc)(void *));
static inline uint32_t getrefcnt(struct json_object *v) {
struct {
enum json_type o_type;
uint32_t _ref_count;
} *spy = (void *)v;
return spy ? spy->_ref_count : 0;
}
static inline void *xalloc(size_t size) {
void *ptr = calloc(1, size);
if (!ptr) {
fprintf(stderr, "Out of memory\n");
abort();
}
return ptr;
}
static inline void *xrealloc(void *ptr, size_t size) {
ptr = realloc(ptr, size);
if (!ptr) {
fprintf(stderr, "Out of memory\n");
abort();
}
return ptr;
}
static inline char *xstrdup(const char *s) {
char *ptr = strdup(s);
if (!ptr) {
fprintf(stderr, "Out of memory\n");
abort();
}
return ptr;
}
static inline struct json_object *xjs_new_object(void) {
struct json_object *ptr = json_object_new_object();
if (!ptr) {
fprintf(stderr, "Out of memory\n");
abort();
}
return ptr;
}
static inline struct json_object *xjs_new_array(void) {
struct json_object *ptr = json_object_new_array();
if (!ptr) {
fprintf(stderr, "Out of memory\n");
abort();
}
return ptr;
}
static inline struct json_object *xjs_new_int64(int64_t n) {
struct json_object *ptr = json_object_new_int64(n);
if (!ptr) {
fprintf(stderr, "Out of memory\n");
abort();
}
return ptr;
}
static inline struct json_object *xjs_new_string(const char *s) {
struct json_object *ptr = json_object_new_string(s);
if (!ptr) {
fprintf(stderr, "Out of memory\n");
abort();
}
return ptr;
}
static inline struct json_object *xjs_new_string_len(const char *s, size_t len) {
struct json_object *ptr = json_object_new_string_len(s, len);
if (!ptr) {
fprintf(stderr, "Out of memory\n");
abort();
}
return ptr;
}
static inline struct json_object *xjs_new_boolean(bool v) {
struct json_object *ptr = json_object_new_boolean(v);
if (!ptr) {
fprintf(stderr, "Out of memory\n");
abort();
}
return ptr;
}
static inline struct json_tokener *xjs_new_tokener(void) {
struct json_tokener *tok = json_tokener_new();
if (!tok) {
fprintf(stderr, "Out of memory\n");
abort();
}
return tok;
}
static inline int xasprintf(char **strp, const char *fmt, ...) {
va_list ap;
int len;
va_start(ap, fmt);
len = vasprintf(strp, fmt, ap);
va_end(ap);
if (len == -1) {
fprintf(stderr, "Out of memory\n");
abort();
}
return len;
}
static inline int xvasprintf(char **strp, const char *fmt, va_list ap) {
int len = vasprintf(strp, fmt, ap);
if (len == -1) {
fprintf(stderr, "Out of memory\n");
abort();
}
return len;
}
#endif /* __AST_H_ */
|