-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsdlang.c
274 lines (223 loc) · 6 KB
/
sdlang.c
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
#include "sdlang.h"
#include <stdlib.h>
static struct sdlang_functions_t* sdlang_user_emit_functions = NULL;
void sdlang_set_emit_functions(struct sdlang_functions_t* emit_functions)
{
if (emit_functions != NULL)
{
emit_functions->node_name[0] = '\0';
emit_functions->attr_name[0] = '\0';
}
sdlang_user_emit_functions = emit_functions;
}
void sdlang_emit_token(const struct sdlang_token_t* token, void* user)
{
struct sdlang_functions_t* vtbl = sdlang_user_emit_functions;
if (vtbl == NULL)
{
return;
}
const char* value = token->string.from;
const int len = (const int)(token->string.to - value);
#define size_minus_one(dst) \
((int)sizeof(vtbl->dst) - 1)
#define safe_copy_value(dst) \
strncpy(vtbl->dst, value, len < size_minus_one(dst) ? len : size_minus_one(dst)); \
vtbl->dst[len < size_minus_one(dst) ? len : size_minus_one(dst)] = '\0'
#define safe_call(fn, params) \
if (vtbl->fn != NULL) vtbl->fn params
#define safe_emit_value(fn, ...) \
if (vtbl->value_##fn != NULL) vtbl->value_##fn(vtbl->node_name, vtbl->attr_name, __VA_ARGS__); \
vtbl->attr_name[0] = '\0';
switch (token->type)
{
case SDLANG_TOKEN_NODE:
{
safe_copy_value(node_name);
}
break;
case SDLANG_TOKEN_NODE_END:
{
vtbl->node_name[0] = '\0';
}
break;
case SDLANG_TOKEN_BLOCK:
{
safe_call(block_begin, (vtbl->node_name, user));
}
break;
case SDLANG_TOKEN_BLOCK_END:
{
safe_call(block_end, (user));
}
break;
case SDLANG_TOKEN_ATTRIBUTE:
{
/* store attribute name */
safe_copy_value(attr_name);
}
break;
case SDLANG_TOKEN_INT32:
{
const long l = strtol(value, NULL, 10);
safe_emit_value(i32, l, user);
}
break;
case SDLANG_TOKEN_INT64:
{
const long long ll = strtoll(value, NULL, 10);
safe_emit_value(i64, ll, user);
}
break;
case SDLANG_TOKEN_INT128:
{
int64_t hi = 0;
uint64_t lo = 0;
const char *s = value, *e = &value[len];
int64_t sign = 1;
if (*s == '-')
{
sign = -sign;
++s;
}
while (s != e)
{
const uint64_t digit = *s - '0';
if (lo * 10 + digit < lo)
{
hi = (hi << 4) | (lo >> 48);
lo >>= 4;
}
lo = lo * 10 + digit;
++s;
}
hi = sign * hi;
safe_emit_value(i128, hi, lo, user);
}
break;
case SDLANG_TOKEN_FLOAT32:
{
const float f = strtof(value, NULL);
safe_emit_value(f32, f, user);
}
break;
case SDLANG_TOKEN_FLOAT64:
{
const double d = strtod(value, NULL);
safe_emit_value(f64, d, user);
}
break;
case SDLANG_TOKEN_STRING:
{
safe_emit_value(string, value, len, user);
}
break;
case SDLANG_TOKEN_BASE64:
{
safe_emit_value(base64, value, len, user);
}
break;
case SDLANG_TOKEN_UINT32:
{
const unsigned long ul = strtoul(value, NULL, 16);
safe_emit_value(u32, ul, user);
}
break;
case SDLANG_TOKEN_UINT64:
{
const unsigned long long ull = strtoull(value, NULL, 16);
safe_emit_value(u64, ull, user);
}
break;
case SDLANG_TOKEN_TRUE:
{
safe_emit_value(bool, true, user);
}
break;
case SDLANG_TOKEN_FALSE:
{
safe_emit_value(bool, false, user);
}
break;
case SDLANG_TOKEN_NULL:
{
safe_emit_value(null, user);
}
break;
default:
break;
}
}
static void (*sdlang_user_emit_token)(const struct sdlang_token_t*, void*) = sdlang_emit_token;
void sdlang_set_emit_token(void (*emit_token)(const struct sdlang_token_t* token, void* user))
{
sdlang_user_emit_token = emit_token != NULL ? emit_token : sdlang_emit_token;
}
static void emit(enum sdlang_token_type_t type, const char* ts,
const char* te, int line, void* user)
{
switch (type)
{
case SDLANG_TOKEN_ATTRIBUTE:
/* strip trailing '=' */
--te;
break;
case SDLANG_TOKEN_INT32:
case SDLANG_TOKEN_INT64:
case SDLANG_TOKEN_FLOAT32:
case SDLANG_TOKEN_FLOAT64:
/* strip leading '+' */
if (*ts == '+')
{
++ts;
}
break;
case SDLANG_TOKEN_INT128:
/* strip leading '+' and suffix */
if (*ts == '+')
{
++ts;
}
te -= 2;
break;
case SDLANG_TOKEN_STRING:
case SDLANG_TOKEN_BASE64:
/* strip delimiters */
++ts;
--te;
break;
default:
break;
}
const struct sdlang_token_t token = {
.type = type,
.string = {
.from = ts,
.to = te
},
.line = line
};
(*sdlang_user_emit_token)(&token, user);
}
static void sdlang_report_error(enum sdlang_error_t error, int line)
{
(void)error;
(void)line;
}
static void (*sdlang_user_report_error)(enum sdlang_error_t error, int line) = sdlang_report_error;
void sdlang_set_report_error(void (*report_error)(enum sdlang_error_t error, int line))
{
sdlang_user_report_error = report_error != NULL ? report_error : sdlang_report_error;
}
static void check_stack_size(char** p, char* pe, int top, int line)
{
if (top == SDLANG_PARSE_STACKSIZE - 1)
{
/*
trick the FSM into completing the current iteration,
then the main loop checks for the stack pointer
*/
*p = pe - 1;
}
}
#include "sdlang.inl"