packcr 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/exe/packcr +3 -0
- data/lib/packcr/broadcast.rb +17 -0
- data/lib/packcr/cli.rb +25 -0
- data/lib/packcr/code_block.rb +2 -3
- data/lib/packcr/context.rb +59 -56
- data/lib/packcr/generator.rb +13 -11
- data/lib/packcr/node/action_node.rb +14 -5
- data/lib/packcr/node/alternate_node.rb +20 -12
- data/lib/packcr/node/capture_node.rb +11 -11
- data/lib/packcr/node/charclass_node.rb +37 -13
- data/lib/packcr/node/eof_node.rb +23 -0
- data/lib/packcr/node/error_node.rb +17 -12
- data/lib/packcr/node/expand_node.rb +11 -5
- data/lib/packcr/node/predicate_node.rb +15 -24
- data/lib/packcr/node/quantity_node.rb +23 -18
- data/lib/packcr/node/reference_node.rb +21 -5
- data/lib/packcr/node/root_node.rb +60 -0
- data/lib/packcr/node/rule_node.rb +26 -3
- data/lib/packcr/node/sequence_node.rb +46 -31
- data/lib/packcr/node/string_node.rb +12 -12
- data/lib/packcr/node.rb +35 -4
- data/lib/packcr/parser.rb +2939 -2661
- data/lib/packcr/stream.rb +8 -13
- data/lib/packcr/templates/context/header.c.erb +29 -0
- data/lib/packcr/templates/context/source.c.erb +1292 -0
- data/lib/packcr/templates/context/source.rb.erb +406 -0
- data/lib/packcr/templates/node/action.c.erb +16 -0
- data/lib/packcr/templates/node/action.rb.erb +21 -0
- data/lib/packcr/templates/node/alternate.c.erb +34 -0
- data/lib/packcr/templates/node/alternate.rb.erb +40 -0
- data/lib/packcr/templates/node/capture.c.erb +17 -0
- data/lib/packcr/templates/node/capture.rb.erb +14 -0
- data/lib/packcr/templates/node/charclass.c.erb +47 -0
- data/lib/packcr/templates/node/charclass.rb.erb +49 -0
- data/lib/packcr/templates/node/charclass_any.c.erb +5 -0
- data/lib/packcr/templates/node/charclass_any.rb.erb +7 -0
- data/lib/packcr/templates/node/charclass_fail.c.erb +1 -0
- data/lib/packcr/templates/node/charclass_fail.rb.erb +1 -0
- data/lib/packcr/templates/node/charclass_one.c.erb +19 -0
- data/lib/packcr/templates/node/charclass_one.rb.erb +23 -0
- data/lib/packcr/templates/node/charclass_utf8.c.erb +49 -0
- data/lib/packcr/templates/node/charclass_utf8.rb.erb +50 -0
- data/lib/packcr/templates/node/charclass_utf8_reverse.rb.erb +51 -0
- data/lib/packcr/templates/node/eof.c.erb +1 -0
- data/lib/packcr/templates/node/eof.rb.erb +3 -0
- data/lib/packcr/templates/node/error.c.erb +28 -0
- data/lib/packcr/templates/node/error.rb.erb +34 -0
- data/lib/packcr/templates/node/expand.c.erb +16 -0
- data/lib/packcr/templates/node/expand.rb.erb +16 -0
- data/lib/packcr/templates/node/predicate.c.erb +30 -0
- data/lib/packcr/templates/node/predicate.rb.erb +28 -0
- data/lib/packcr/templates/node/predicate_neg.c.erb +23 -0
- data/lib/packcr/templates/node/predicate_neg.rb.erb +22 -0
- data/lib/packcr/templates/node/quantify_many.c.erb +45 -0
- data/lib/packcr/templates/node/quantify_many.rb.erb +47 -0
- data/lib/packcr/templates/node/quantify_one.c.erb +21 -0
- data/lib/packcr/templates/node/quantify_one.rb.erb +23 -0
- data/lib/packcr/templates/node/reference.c.erb +5 -0
- data/lib/packcr/templates/node/reference.rb.erb +9 -0
- data/lib/packcr/templates/node/reference_reverse.rb.erb +9 -0
- data/lib/packcr/templates/node/rule.c.erb +19 -0
- data/lib/packcr/templates/node/rule.rb.erb +23 -0
- data/lib/packcr/templates/node/sequence.c.erb +12 -0
- data/lib/packcr/templates/node/sequence.rb.erb +12 -0
- data/lib/packcr/templates/node/string_many.c.erb +11 -0
- data/lib/packcr/templates/node/string_many.rb.erb +10 -0
- data/lib/packcr/templates/node/string_one.c.erb +8 -0
- data/lib/packcr/templates/node/string_one.rb.erb +10 -0
- data/lib/packcr/tokenizer.rb +2948 -0
- data/lib/packcr/util.rb +0 -8
- data/lib/packcr/version.rb +1 -1
- data/lib/packcr.rb +1 -2
- metadata +87 -8
- data/lib/packcr/buffer.rb +0 -47
@@ -0,0 +1,1292 @@
|
|
1
|
+
/* A packrat parser generated by PackCR <%= Packcr::VERSION %> */
|
2
|
+
|
3
|
+
<%- code(:esource).each do |code| -%>
|
4
|
+
<%= stream.get_code_block(code, 0, @iname) -%>
|
5
|
+
<%- end -%>
|
6
|
+
<%- if !code(:esource).empty? -%>
|
7
|
+
|
8
|
+
<%- end -%>
|
9
|
+
#ifdef _MSC_VER
|
10
|
+
#undef _CRT_SECURE_NO_WARNINGS
|
11
|
+
#define _CRT_SECURE_NO_WARNINGS
|
12
|
+
#endif /* _MSC_VER */
|
13
|
+
#include <stdio.h>
|
14
|
+
#include <stdlib.h>
|
15
|
+
#include <string.h>
|
16
|
+
|
17
|
+
#ifndef _MSC_VER
|
18
|
+
#if defined __GNUC__ && defined _WIN32 /* MinGW */
|
19
|
+
#ifndef PCC_USE_SYSTEM_STRNLEN
|
20
|
+
#define strnlen(str, maxlen) pcc_strnlen(str, maxlen)
|
21
|
+
static size_t pcc_strnlen(const char *str, size_t maxlen) {
|
22
|
+
size_t i;
|
23
|
+
for (i = 0; i < maxlen && str[i]; i++);
|
24
|
+
return i;
|
25
|
+
}
|
26
|
+
#endif /* !PCC_USE_SYSTEM_STRNLEN */
|
27
|
+
#endif /* defined __GNUC__ && defined _WIN32 */
|
28
|
+
#endif /* !_MSC_VER */
|
29
|
+
|
30
|
+
#include "<%= @hname %>"
|
31
|
+
<%- if !code(:location).empty? -%>
|
32
|
+
|
33
|
+
<%- code(:location).each do |code| -%>
|
34
|
+
<%= stream.get_code_block(code, 4, @iname) -%>
|
35
|
+
<%- end -%>
|
36
|
+
<%- end -%>
|
37
|
+
|
38
|
+
<%- code(:source).each do |code| -%>
|
39
|
+
<%= stream.get_code_block(code, 0, @iname) -%>
|
40
|
+
<%- end -%>
|
41
|
+
#if !defined __has_attribute || defined _MSC_VER
|
42
|
+
#define __attribute__(x)
|
43
|
+
#endif
|
44
|
+
|
45
|
+
#ifdef _MSC_VER
|
46
|
+
#define MARK_FUNC_AS_USED __pragma(warning(suppress:4505))
|
47
|
+
#else
|
48
|
+
#define MARK_FUNC_AS_USED __attribute__((__unused__))
|
49
|
+
#endif
|
50
|
+
|
51
|
+
#ifndef PCC_BUFFER_MIN_SIZE
|
52
|
+
#define PCC_BUFFER_MIN_SIZE 256
|
53
|
+
#endif /* !PCC_BUFFER_MIN_SIZE */
|
54
|
+
|
55
|
+
#ifndef PCC_ARRAY_MIN_SIZE
|
56
|
+
#define PCC_ARRAY_MIN_SIZE 2
|
57
|
+
#endif /* !PCC_ARRAY_MIN_SIZE */
|
58
|
+
|
59
|
+
#ifndef PCC_POOL_MIN_SIZE
|
60
|
+
#define PCC_POOL_MIN_SIZE 65536
|
61
|
+
#endif /* !PCC_POOL_MIN_SIZE */
|
62
|
+
|
63
|
+
#define PCC_DBG_EVALUATE 0
|
64
|
+
#define PCC_DBG_MATCH 1
|
65
|
+
#define PCC_DBG_NOMATCH 2
|
66
|
+
|
67
|
+
#define PCC_VOID_VALUE (~(size_t)0)
|
68
|
+
|
69
|
+
typedef enum pcc_bool_tag {
|
70
|
+
PCC_FALSE = 0,
|
71
|
+
PCC_TRUE
|
72
|
+
} pcc_bool_t;
|
73
|
+
|
74
|
+
typedef struct pcc_char_array_tag {
|
75
|
+
char *buf;
|
76
|
+
size_t max;
|
77
|
+
size_t len;
|
78
|
+
} pcc_char_array_t;
|
79
|
+
|
80
|
+
typedef struct pcc_range_tag {
|
81
|
+
size_t start;
|
82
|
+
size_t end;
|
83
|
+
<%- if @location -%>
|
84
|
+
pcc_location_t *start_loc_ptr;
|
85
|
+
pcc_location_t *end_loc_ptr;
|
86
|
+
<%- end -%>
|
87
|
+
} pcc_range_t;
|
88
|
+
|
89
|
+
typedef <%= value_def %>pcc_value_t;
|
90
|
+
|
91
|
+
typedef <%= auxil_def %>pcc_auxil_t;
|
92
|
+
|
93
|
+
<% if prefix != "pcc" -%>
|
94
|
+
typedef <%= prefix%>_context_t pcc_context_t;
|
95
|
+
|
96
|
+
<% end -%>
|
97
|
+
typedef struct pcc_value_table_tag {
|
98
|
+
pcc_value_t *buf;
|
99
|
+
size_t max;
|
100
|
+
size_t len;
|
101
|
+
} pcc_value_table_t;
|
102
|
+
|
103
|
+
typedef struct pcc_value_refer_table_tag {
|
104
|
+
pcc_value_t **buf;
|
105
|
+
size_t max;
|
106
|
+
size_t len;
|
107
|
+
} pcc_value_refer_table_t;
|
108
|
+
|
109
|
+
typedef struct pcc_capture_tag {
|
110
|
+
pcc_range_t range;
|
111
|
+
char *string; /* mutable */
|
112
|
+
} pcc_capture_t;
|
113
|
+
|
114
|
+
typedef struct pcc_capture_table_tag {
|
115
|
+
pcc_capture_t *buf;
|
116
|
+
size_t max;
|
117
|
+
size_t len;
|
118
|
+
} pcc_capture_table_t;
|
119
|
+
|
120
|
+
typedef struct pcc_capture_const_table_tag {
|
121
|
+
const pcc_capture_t **buf;
|
122
|
+
size_t max;
|
123
|
+
size_t len;
|
124
|
+
} pcc_capture_const_table_t;
|
125
|
+
|
126
|
+
typedef struct pcc_thunk_tag pcc_thunk_t;
|
127
|
+
typedef struct pcc_thunk_array_tag pcc_thunk_array_t;
|
128
|
+
|
129
|
+
typedef void (*pcc_action_t)(pcc_context_t *, pcc_thunk_t *, pcc_value_t *);
|
130
|
+
|
131
|
+
typedef enum pcc_thunk_type_tag {
|
132
|
+
PCC_THUNK_LEAF,
|
133
|
+
PCC_THUNK_NODE
|
134
|
+
} pcc_thunk_type_t;
|
135
|
+
|
136
|
+
typedef struct pcc_thunk_leaf_tag {
|
137
|
+
pcc_value_refer_table_t values;
|
138
|
+
pcc_capture_const_table_t capts;
|
139
|
+
pcc_capture_t capt0;
|
140
|
+
pcc_action_t action;
|
141
|
+
} pcc_thunk_leaf_t;
|
142
|
+
|
143
|
+
typedef struct pcc_thunk_node_tag {
|
144
|
+
const pcc_thunk_array_t *thunks; /* just a reference */
|
145
|
+
pcc_value_t *value; /* just a reference */
|
146
|
+
} pcc_thunk_node_t;
|
147
|
+
|
148
|
+
typedef union pcc_thunk_data_tag {
|
149
|
+
pcc_thunk_leaf_t leaf;
|
150
|
+
pcc_thunk_node_t node;
|
151
|
+
} pcc_thunk_data_t;
|
152
|
+
|
153
|
+
struct pcc_thunk_tag {
|
154
|
+
pcc_thunk_type_t type;
|
155
|
+
pcc_thunk_data_t data;
|
156
|
+
};
|
157
|
+
|
158
|
+
struct pcc_thunk_array_tag {
|
159
|
+
pcc_thunk_t **buf;
|
160
|
+
size_t max;
|
161
|
+
size_t len;
|
162
|
+
};
|
163
|
+
|
164
|
+
typedef struct pcc_thunk_chunk_tag {
|
165
|
+
pcc_value_table_t values;
|
166
|
+
pcc_capture_table_t capts;
|
167
|
+
pcc_thunk_array_t thunks;
|
168
|
+
size_t pos; /* the starting position in the character buffer */
|
169
|
+
<%- if @location -%>
|
170
|
+
pcc_location_t pos_loc;
|
171
|
+
<%- end -%>
|
172
|
+
} pcc_thunk_chunk_t;
|
173
|
+
|
174
|
+
typedef struct pcc_lr_entry_tag pcc_lr_entry_t;
|
175
|
+
|
176
|
+
typedef enum pcc_lr_answer_type_tag {
|
177
|
+
PCC_LR_ANSWER_LR,
|
178
|
+
PCC_LR_ANSWER_CHUNK
|
179
|
+
} pcc_lr_answer_type_t;
|
180
|
+
|
181
|
+
typedef union pcc_lr_answer_data_tag {
|
182
|
+
pcc_lr_entry_t *lr;
|
183
|
+
pcc_thunk_chunk_t *chunk;
|
184
|
+
} pcc_lr_answer_data_t;
|
185
|
+
|
186
|
+
typedef struct pcc_lr_answer_tag pcc_lr_answer_t;
|
187
|
+
|
188
|
+
struct pcc_lr_answer_tag {
|
189
|
+
pcc_lr_answer_type_t type;
|
190
|
+
pcc_lr_answer_data_t data;
|
191
|
+
size_t pos; /* the absolute position in the input */
|
192
|
+
<%- if @location -%>
|
193
|
+
pcc_location_t pos_loc;
|
194
|
+
<%- end -%>
|
195
|
+
pcc_lr_answer_t *hold;
|
196
|
+
};
|
197
|
+
|
198
|
+
typedef pcc_thunk_chunk_t *(*pcc_rule_t)(pcc_context_t *);
|
199
|
+
|
200
|
+
typedef struct pcc_rule_set_tag {
|
201
|
+
pcc_rule_t *buf;
|
202
|
+
size_t max;
|
203
|
+
size_t len;
|
204
|
+
} pcc_rule_set_t;
|
205
|
+
|
206
|
+
typedef struct pcc_lr_head_tag pcc_lr_head_t;
|
207
|
+
|
208
|
+
struct pcc_lr_head_tag {
|
209
|
+
pcc_rule_t rule;
|
210
|
+
pcc_rule_set_t invol;
|
211
|
+
pcc_rule_set_t eval;
|
212
|
+
pcc_lr_head_t *hold;
|
213
|
+
};
|
214
|
+
|
215
|
+
typedef struct pcc_lr_memo_tag {
|
216
|
+
pcc_rule_t rule;
|
217
|
+
pcc_lr_answer_t *answer;
|
218
|
+
} pcc_lr_memo_t;
|
219
|
+
|
220
|
+
typedef struct pcc_lr_memo_map_tag {
|
221
|
+
pcc_lr_memo_t *buf;
|
222
|
+
size_t max;
|
223
|
+
size_t len;
|
224
|
+
} pcc_lr_memo_map_t;
|
225
|
+
|
226
|
+
typedef struct pcc_lr_table_entry_tag {
|
227
|
+
pcc_lr_head_t *head; /* just a reference */
|
228
|
+
pcc_lr_memo_map_t memos;
|
229
|
+
pcc_lr_answer_t *hold_a;
|
230
|
+
pcc_lr_head_t *hold_h;
|
231
|
+
} pcc_lr_table_entry_t;
|
232
|
+
|
233
|
+
typedef struct pcc_lr_table_tag {
|
234
|
+
pcc_lr_table_entry_t **buf;
|
235
|
+
size_t max;
|
236
|
+
size_t len;
|
237
|
+
size_t ofs;
|
238
|
+
} pcc_lr_table_t;
|
239
|
+
|
240
|
+
struct pcc_lr_entry_tag {
|
241
|
+
pcc_rule_t rule;
|
242
|
+
pcc_thunk_chunk_t *seed; /* just a reference */
|
243
|
+
pcc_lr_head_t *head; /* just a reference */
|
244
|
+
};
|
245
|
+
|
246
|
+
typedef struct pcc_lr_stack_tag {
|
247
|
+
pcc_lr_entry_t **buf;
|
248
|
+
size_t max;
|
249
|
+
size_t len;
|
250
|
+
} pcc_lr_stack_t;
|
251
|
+
|
252
|
+
typedef struct pcc_memory_entry_tag pcc_memory_entry_t;
|
253
|
+
typedef struct pcc_memory_pool_tag pcc_memory_pool_t;
|
254
|
+
|
255
|
+
struct pcc_memory_entry_tag {
|
256
|
+
pcc_memory_entry_t *next;
|
257
|
+
};
|
258
|
+
|
259
|
+
struct pcc_memory_pool_tag {
|
260
|
+
pcc_memory_pool_t *next;
|
261
|
+
size_t allocated;
|
262
|
+
size_t unused;
|
263
|
+
};
|
264
|
+
|
265
|
+
typedef struct pcc_memory_recycler_tag {
|
266
|
+
pcc_memory_pool_t *pool_list;
|
267
|
+
pcc_memory_entry_t *entry_list;
|
268
|
+
size_t element_size;
|
269
|
+
} pcc_memory_recycler_t;
|
270
|
+
|
271
|
+
struct <%= prefix %>_context_tag {
|
272
|
+
size_t pos; /* the position in the input of the first character currently buffered */
|
273
|
+
size_t cur; /* the current parsing position in the character buffer */
|
274
|
+
<%- if @location -%>
|
275
|
+
pcc_location_t pos_loc;
|
276
|
+
pcc_location_t cur_loc;
|
277
|
+
<%- end -%>
|
278
|
+
size_t level;
|
279
|
+
pcc_char_array_t buffer;
|
280
|
+
pcc_lr_table_t lrtable;
|
281
|
+
pcc_lr_stack_t lrstack;
|
282
|
+
pcc_thunk_array_t thunks;
|
283
|
+
pcc_auxil_t auxil;
|
284
|
+
pcc_memory_recycler_t thunk_chunk_recycler;
|
285
|
+
pcc_memory_recycler_t lr_head_recycler;
|
286
|
+
pcc_memory_recycler_t lr_answer_recycler;
|
287
|
+
};
|
288
|
+
|
289
|
+
#ifndef PCC_ERROR
|
290
|
+
#define PCC_ERROR(auxil) pcc_error()
|
291
|
+
MARK_FUNC_AS_USED
|
292
|
+
static void pcc_error(void) {
|
293
|
+
fprintf(stderr, "Syntax error\n");
|
294
|
+
exit(1);
|
295
|
+
}
|
296
|
+
#endif /* !PCC_ERROR */
|
297
|
+
|
298
|
+
#ifndef PCC_GETCHAR
|
299
|
+
#define PCC_GETCHAR(auxil) getchar()
|
300
|
+
#endif /* !PCC_GETCHAR */
|
301
|
+
|
302
|
+
#ifndef PCC_MALLOC
|
303
|
+
#define PCC_MALLOC(auxil, size) pcc_malloc_e(size)
|
304
|
+
static void *pcc_malloc_e(size_t size) {
|
305
|
+
void *const p = malloc(size);
|
306
|
+
if (p == NULL) {
|
307
|
+
fprintf(stderr, "Out of memory\n");
|
308
|
+
exit(1);
|
309
|
+
}
|
310
|
+
return p;
|
311
|
+
}
|
312
|
+
#endif /* !PCC_MALLOC */
|
313
|
+
|
314
|
+
#ifndef PCC_REALLOC
|
315
|
+
#define PCC_REALLOC(auxil, ptr, size) pcc_realloc_e(ptr, size)
|
316
|
+
static void *pcc_realloc_e(void *ptr, size_t size) {
|
317
|
+
void *const p = realloc(ptr, size);
|
318
|
+
if (p == NULL) {
|
319
|
+
fprintf(stderr, "Out of memory\n");
|
320
|
+
exit(1);
|
321
|
+
}
|
322
|
+
return p;
|
323
|
+
}
|
324
|
+
#endif /* !PCC_REALLOC */
|
325
|
+
|
326
|
+
#ifndef PCC_FREE
|
327
|
+
#define PCC_FREE(auxil, ptr) free(ptr)
|
328
|
+
#endif /* !PCC_FREE */
|
329
|
+
|
330
|
+
#ifndef PCC_DEBUG
|
331
|
+
#define PCC_DEBUG(auxil, event, rule, level, pos, buffer, length) ((void)0)
|
332
|
+
#endif /* !PCC_DEBUG */
|
333
|
+
|
334
|
+
static char *pcc_strndup_e(pcc_auxil_t auxil, const char *str, size_t len) {
|
335
|
+
const size_t m = strnlen(str, len);
|
336
|
+
char *const s = (char *)PCC_MALLOC(auxil, m + 1);
|
337
|
+
memcpy(s, str, m);
|
338
|
+
s[m] = '\0';
|
339
|
+
return s;
|
340
|
+
}
|
341
|
+
|
342
|
+
static void pcc_char_array__init(pcc_auxil_t auxil, pcc_char_array_t *array) {
|
343
|
+
array->len = 0;
|
344
|
+
array->max = 0;
|
345
|
+
array->buf = NULL;
|
346
|
+
}
|
347
|
+
|
348
|
+
static void pcc_char_array__add(pcc_auxil_t auxil, pcc_char_array_t *array, char ch) {
|
349
|
+
if (array->max <= array->len) {
|
350
|
+
const size_t n = array->len + 1;
|
351
|
+
size_t m = array->max;
|
352
|
+
if (m == 0) m = PCC_BUFFER_MIN_SIZE;
|
353
|
+
while (m < n && m != 0) m <<= 1;
|
354
|
+
if (m == 0) m = n;
|
355
|
+
array->buf = (char *)PCC_REALLOC(auxil, array->buf, m);
|
356
|
+
array->max = m;
|
357
|
+
}
|
358
|
+
array->buf[array->len++] = ch;
|
359
|
+
}
|
360
|
+
|
361
|
+
static void pcc_char_array__term(pcc_auxil_t auxil, pcc_char_array_t *array) {
|
362
|
+
PCC_FREE(auxil, array->buf);
|
363
|
+
}
|
364
|
+
|
365
|
+
static void pcc_value_table__init(pcc_auxil_t auxil, pcc_value_table_t *table) {
|
366
|
+
table->len = 0;
|
367
|
+
table->max = 0;
|
368
|
+
table->buf = NULL;
|
369
|
+
}
|
370
|
+
|
371
|
+
MARK_FUNC_AS_USED
|
372
|
+
static void pcc_value_table__resize(pcc_auxil_t auxil, pcc_value_table_t *table, size_t len) {
|
373
|
+
if (table->max < len) {
|
374
|
+
size_t m = table->max;
|
375
|
+
if (m == 0) m = PCC_ARRAY_MIN_SIZE;
|
376
|
+
while (m < len && m != 0) m <<= 1;
|
377
|
+
if (m == 0) m = len;
|
378
|
+
table->buf = (pcc_value_t *)PCC_REALLOC(auxil, table->buf, sizeof(pcc_value_t) * m);
|
379
|
+
table->max = m;
|
380
|
+
}
|
381
|
+
table->len = len;
|
382
|
+
}
|
383
|
+
|
384
|
+
MARK_FUNC_AS_USED
|
385
|
+
static void pcc_value_table__clear(pcc_auxil_t auxil, pcc_value_table_t *table) {
|
386
|
+
memset(table->buf, 0, sizeof(pcc_value_t) * table->len);
|
387
|
+
}
|
388
|
+
|
389
|
+
static void pcc_value_table__term(pcc_auxil_t auxil, pcc_value_table_t *table) {
|
390
|
+
PCC_FREE(auxil, table->buf);
|
391
|
+
}
|
392
|
+
|
393
|
+
static void pcc_value_refer_table__init(pcc_auxil_t auxil, pcc_value_refer_table_t *table) {
|
394
|
+
table->len = 0;
|
395
|
+
table->max = 0;
|
396
|
+
table->buf = NULL;
|
397
|
+
}
|
398
|
+
|
399
|
+
static void pcc_value_refer_table__resize(pcc_auxil_t auxil, pcc_value_refer_table_t *table, size_t len) {
|
400
|
+
size_t i;
|
401
|
+
if (table->max < len) {
|
402
|
+
size_t m = table->max;
|
403
|
+
if (m == 0) m = PCC_ARRAY_MIN_SIZE;
|
404
|
+
while (m < len && m != 0) m <<= 1;
|
405
|
+
if (m == 0) m = len;
|
406
|
+
table->buf = (pcc_value_t **)PCC_REALLOC(auxil, table->buf, sizeof(pcc_value_t *) * m);
|
407
|
+
table->max = m;
|
408
|
+
}
|
409
|
+
for (i = table->len; i < len; i++) table->buf[i] = NULL;
|
410
|
+
table->len = len;
|
411
|
+
}
|
412
|
+
|
413
|
+
static void pcc_value_refer_table__term(pcc_auxil_t auxil, pcc_value_refer_table_t *table) {
|
414
|
+
PCC_FREE(auxil, table->buf);
|
415
|
+
}
|
416
|
+
|
417
|
+
static void pcc_capture_table__init(pcc_auxil_t auxil, pcc_capture_table_t *table) {
|
418
|
+
table->len = 0;
|
419
|
+
table->max = 0;
|
420
|
+
table->buf = NULL;
|
421
|
+
}
|
422
|
+
|
423
|
+
MARK_FUNC_AS_USED
|
424
|
+
static void pcc_capture_table__resize(pcc_auxil_t auxil, pcc_capture_table_t *table, size_t len) {
|
425
|
+
size_t i;
|
426
|
+
for (i = len; i < table->len; i++) PCC_FREE(auxil, table->buf[i].string);
|
427
|
+
<%- if @location -%>
|
428
|
+
for (i = len; i < table->len; i++) {
|
429
|
+
PCC_FREE(auxil, table->buf[i].range.start_loc_ptr);
|
430
|
+
PCC_FREE(auxil, table->buf[i].range.end_loc_ptr);
|
431
|
+
}
|
432
|
+
<%- end -%>
|
433
|
+
if (table->max < len) {
|
434
|
+
size_t m = table->max;
|
435
|
+
if (m == 0) m = PCC_ARRAY_MIN_SIZE;
|
436
|
+
while (m < len && m != 0) m <<= 1;
|
437
|
+
if (m == 0) m = len;
|
438
|
+
table->buf = (pcc_capture_t *)PCC_REALLOC(auxil, table->buf, sizeof(pcc_capture_t) * m);
|
439
|
+
table->max = m;
|
440
|
+
}
|
441
|
+
for (i = table->len; i < len; i++) {
|
442
|
+
table->buf[i].range.start = 0;
|
443
|
+
table->buf[i].range.end = 0;
|
444
|
+
<%- if @location -%>
|
445
|
+
table->buf[i].range.start_loc_ptr = (pcc_location_t *)PCC_MALLOC(auxil, sizeof(pcc_location_t));
|
446
|
+
table->buf[i].range.end_loc_ptr = (pcc_location_t *)PCC_MALLOC(auxil, sizeof(pcc_location_t));
|
447
|
+
pcc_location_init(table->buf[i].range.start_loc_ptr);
|
448
|
+
pcc_location_init(table->buf[i].range.end_loc_ptr);
|
449
|
+
<%- end -%>
|
450
|
+
table->buf[i].string = NULL;
|
451
|
+
}
|
452
|
+
table->len = len;
|
453
|
+
}
|
454
|
+
|
455
|
+
static void pcc_capture_table__term(pcc_auxil_t auxil, pcc_capture_table_t *table) {
|
456
|
+
while (table->len > 0) {
|
457
|
+
table->len--;
|
458
|
+
PCC_FREE(auxil, table->buf[table->len].string);
|
459
|
+
<%- if @location -%>
|
460
|
+
PCC_FREE(auxil, table->buf[table->len].range.start_loc_ptr);
|
461
|
+
PCC_FREE(auxil, table->buf[table->len].range.end_loc_ptr);
|
462
|
+
<%- end -%>
|
463
|
+
}
|
464
|
+
PCC_FREE(auxil, table->buf);
|
465
|
+
}
|
466
|
+
|
467
|
+
static void pcc_capture_const_table__init(pcc_auxil_t auxil, pcc_capture_const_table_t *table) {
|
468
|
+
table->len = 0;
|
469
|
+
table->max = 0;
|
470
|
+
table->buf = NULL;
|
471
|
+
}
|
472
|
+
|
473
|
+
static void pcc_capture_const_table__resize(pcc_auxil_t auxil, pcc_capture_const_table_t *table, size_t len) {
|
474
|
+
size_t i;
|
475
|
+
if (table->max < len) {
|
476
|
+
size_t m = table->max;
|
477
|
+
if (m == 0) m = PCC_ARRAY_MIN_SIZE;
|
478
|
+
while (m < len && m != 0) m <<= 1;
|
479
|
+
if (m == 0) m = len;
|
480
|
+
table->buf = (const pcc_capture_t **)PCC_REALLOC(auxil, (pcc_capture_t **)table->buf, sizeof(const pcc_capture_t *) * m);
|
481
|
+
table->max = m;
|
482
|
+
}
|
483
|
+
for (i = table->len; i < len; i++) table->buf[i] = NULL;
|
484
|
+
table->len = len;
|
485
|
+
}
|
486
|
+
|
487
|
+
static void pcc_capture_const_table__term(pcc_auxil_t auxil, pcc_capture_const_table_t *table) {
|
488
|
+
PCC_FREE(auxil, (void *)table->buf);
|
489
|
+
}
|
490
|
+
|
491
|
+
MARK_FUNC_AS_USED
|
492
|
+
static pcc_thunk_t *pcc_thunk__create_leaf(pcc_auxil_t auxil, pcc_action_t action, size_t valuec, size_t captc) {
|
493
|
+
pcc_thunk_t *const thunk = (pcc_thunk_t *)PCC_MALLOC(auxil, sizeof(pcc_thunk_t));
|
494
|
+
thunk->type = PCC_THUNK_LEAF;
|
495
|
+
pcc_value_refer_table__init(auxil, &thunk->data.leaf.values);
|
496
|
+
pcc_value_refer_table__resize(auxil, &thunk->data.leaf.values, valuec);
|
497
|
+
pcc_capture_const_table__init(auxil, &thunk->data.leaf.capts);
|
498
|
+
pcc_capture_const_table__resize(auxil, &thunk->data.leaf.capts, captc);
|
499
|
+
thunk->data.leaf.capt0.range.start = 0;
|
500
|
+
thunk->data.leaf.capt0.range.end = 0;
|
501
|
+
thunk->data.leaf.capt0.string = NULL;
|
502
|
+
thunk->data.leaf.action = action;
|
503
|
+
return thunk;
|
504
|
+
}
|
505
|
+
|
506
|
+
static pcc_thunk_t *pcc_thunk__create_node(pcc_auxil_t auxil, const pcc_thunk_array_t *thunks, pcc_value_t *value) {
|
507
|
+
pcc_thunk_t *const thunk = (pcc_thunk_t *)PCC_MALLOC(auxil, sizeof(pcc_thunk_t));
|
508
|
+
thunk->type = PCC_THUNK_NODE;
|
509
|
+
thunk->data.node.thunks = thunks;
|
510
|
+
thunk->data.node.value = value;
|
511
|
+
return thunk;
|
512
|
+
}
|
513
|
+
|
514
|
+
static void pcc_thunk__destroy(pcc_auxil_t auxil, pcc_thunk_t *thunk) {
|
515
|
+
if (thunk == NULL) return;
|
516
|
+
switch (thunk->type) {
|
517
|
+
case PCC_THUNK_LEAF:
|
518
|
+
PCC_FREE(auxil, thunk->data.leaf.capt0.string);
|
519
|
+
pcc_capture_const_table__term(auxil, &thunk->data.leaf.capts);
|
520
|
+
pcc_value_refer_table__term(auxil, &thunk->data.leaf.values);
|
521
|
+
break;
|
522
|
+
case PCC_THUNK_NODE:
|
523
|
+
break;
|
524
|
+
default: /* unknown */
|
525
|
+
break;
|
526
|
+
}
|
527
|
+
PCC_FREE(auxil, thunk);
|
528
|
+
}
|
529
|
+
|
530
|
+
static void pcc_thunk_array__init(pcc_auxil_t auxil, pcc_thunk_array_t *array) {
|
531
|
+
array->len = 0;
|
532
|
+
array->max = 0;
|
533
|
+
array->buf = NULL;
|
534
|
+
}
|
535
|
+
|
536
|
+
static void pcc_thunk_array__add(pcc_auxil_t auxil, pcc_thunk_array_t *array, pcc_thunk_t *thunk) {
|
537
|
+
if (array->max <= array->len) {
|
538
|
+
const size_t n = array->len + 1;
|
539
|
+
size_t m = array->max;
|
540
|
+
if (m == 0) m = PCC_ARRAY_MIN_SIZE;
|
541
|
+
while (m < n && m != 0) m <<= 1;
|
542
|
+
if (m == 0) m = n;
|
543
|
+
array->buf = (pcc_thunk_t **)PCC_REALLOC(auxil, array->buf, sizeof(pcc_thunk_t *) * m);
|
544
|
+
array->max = m;
|
545
|
+
}
|
546
|
+
array->buf[array->len++] = thunk;
|
547
|
+
}
|
548
|
+
|
549
|
+
static void pcc_thunk_array__revert(pcc_auxil_t auxil, pcc_thunk_array_t *array, size_t len) {
|
550
|
+
while (array->len > len) {
|
551
|
+
array->len--;
|
552
|
+
pcc_thunk__destroy(auxil, array->buf[array->len]);
|
553
|
+
}
|
554
|
+
}
|
555
|
+
|
556
|
+
static void pcc_thunk_array__term(pcc_auxil_t auxil, pcc_thunk_array_t *array) {
|
557
|
+
while (array->len > 0) {
|
558
|
+
array->len--;
|
559
|
+
pcc_thunk__destroy(auxil, array->buf[array->len]);
|
560
|
+
}
|
561
|
+
PCC_FREE(auxil, array->buf);
|
562
|
+
}
|
563
|
+
|
564
|
+
static void pcc_memory_recycler__init(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler, size_t element_size) {
|
565
|
+
recycler->pool_list = NULL;
|
566
|
+
recycler->entry_list = NULL;
|
567
|
+
recycler->element_size = element_size;
|
568
|
+
}
|
569
|
+
|
570
|
+
static void *pcc_memory_recycler__supply(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler) {
|
571
|
+
if (recycler->entry_list) {
|
572
|
+
pcc_memory_entry_t *const tmp = recycler->entry_list;
|
573
|
+
recycler->entry_list = tmp->next;
|
574
|
+
return tmp;
|
575
|
+
}
|
576
|
+
if (!recycler->pool_list || recycler->pool_list->unused == 0) {
|
577
|
+
size_t size = PCC_POOL_MIN_SIZE;
|
578
|
+
if (recycler->pool_list) {
|
579
|
+
size = recycler->pool_list->allocated << 1;
|
580
|
+
if (size == 0) size = recycler->pool_list->allocated;
|
581
|
+
}
|
582
|
+
{
|
583
|
+
pcc_memory_pool_t *const pool = (pcc_memory_pool_t *)PCC_MALLOC(
|
584
|
+
auxil, sizeof(pcc_memory_pool_t) + recycler->element_size * size
|
585
|
+
);
|
586
|
+
pool->allocated = size;
|
587
|
+
pool->unused = size;
|
588
|
+
pool->next = recycler->pool_list;
|
589
|
+
recycler->pool_list = pool;
|
590
|
+
}
|
591
|
+
}
|
592
|
+
recycler->pool_list->unused--;
|
593
|
+
return (char *)recycler->pool_list + sizeof(pcc_memory_pool_t) + recycler->element_size * recycler->pool_list->unused;
|
594
|
+
}
|
595
|
+
|
596
|
+
static void pcc_memory_recycler__recycle(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler, void *ptr) {
|
597
|
+
pcc_memory_entry_t *const tmp = (pcc_memory_entry_t *)ptr;
|
598
|
+
tmp->next = recycler->entry_list;
|
599
|
+
recycler->entry_list = tmp;
|
600
|
+
}
|
601
|
+
|
602
|
+
static void pcc_memory_recycler__term(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler) {
|
603
|
+
while (recycler->pool_list) {
|
604
|
+
pcc_memory_pool_t *const tmp = recycler->pool_list;
|
605
|
+
recycler->pool_list = tmp->next;
|
606
|
+
PCC_FREE(auxil, tmp);
|
607
|
+
}
|
608
|
+
}
|
609
|
+
|
610
|
+
MARK_FUNC_AS_USED
|
611
|
+
static pcc_thunk_chunk_t *pcc_thunk_chunk__create(pcc_context_t *ctx) {
|
612
|
+
pcc_thunk_chunk_t *const chunk = (pcc_thunk_chunk_t *)pcc_memory_recycler__supply(ctx->auxil, &ctx->thunk_chunk_recycler);
|
613
|
+
pcc_value_table__init(ctx->auxil, &chunk->values);
|
614
|
+
pcc_capture_table__init(ctx->auxil, &chunk->capts);
|
615
|
+
pcc_thunk_array__init(ctx->auxil, &chunk->thunks);
|
616
|
+
chunk->pos = 0;
|
617
|
+
return chunk;
|
618
|
+
}
|
619
|
+
|
620
|
+
static void pcc_thunk_chunk__destroy(pcc_context_t *ctx, pcc_thunk_chunk_t *chunk) {
|
621
|
+
if (chunk == NULL) return;
|
622
|
+
pcc_thunk_array__term(ctx->auxil, &chunk->thunks);
|
623
|
+
pcc_capture_table__term(ctx->auxil, &chunk->capts);
|
624
|
+
pcc_value_table__term(ctx->auxil, &chunk->values);
|
625
|
+
pcc_memory_recycler__recycle(ctx->auxil, &ctx->thunk_chunk_recycler, chunk);
|
626
|
+
}
|
627
|
+
|
628
|
+
static void pcc_rule_set__init(pcc_auxil_t auxil, pcc_rule_set_t *set) {
|
629
|
+
set->len = 0;
|
630
|
+
set->max = 0;
|
631
|
+
set->buf = NULL;
|
632
|
+
}
|
633
|
+
|
634
|
+
static size_t pcc_rule_set__index(pcc_auxil_t auxil, const pcc_rule_set_t *set, pcc_rule_t rule) {
|
635
|
+
size_t i;
|
636
|
+
for (i = 0; i < set->len; i++) {
|
637
|
+
if (set->buf[i] == rule) return i;
|
638
|
+
}
|
639
|
+
return PCC_VOID_VALUE;
|
640
|
+
}
|
641
|
+
|
642
|
+
static pcc_bool_t pcc_rule_set__add(pcc_auxil_t auxil, pcc_rule_set_t *set, pcc_rule_t rule) {
|
643
|
+
const size_t i = pcc_rule_set__index(auxil, set, rule);
|
644
|
+
if (i != PCC_VOID_VALUE) return PCC_FALSE;
|
645
|
+
if (set->max <= set->len) {
|
646
|
+
const size_t n = set->len + 1;
|
647
|
+
size_t m = set->max;
|
648
|
+
if (m == 0) m = PCC_ARRAY_MIN_SIZE;
|
649
|
+
while (m < n && m != 0) m <<= 1;
|
650
|
+
if (m == 0) m = n;
|
651
|
+
set->buf = (pcc_rule_t *)PCC_REALLOC(auxil, set->buf, sizeof(pcc_rule_t) * m);
|
652
|
+
set->max = m;
|
653
|
+
}
|
654
|
+
set->buf[set->len++] = rule;
|
655
|
+
return PCC_TRUE;
|
656
|
+
}
|
657
|
+
|
658
|
+
static pcc_bool_t pcc_rule_set__remove(pcc_auxil_t auxil, pcc_rule_set_t *set, pcc_rule_t rule) {
|
659
|
+
const size_t i = pcc_rule_set__index(auxil, set, rule);
|
660
|
+
if (i == PCC_VOID_VALUE) return PCC_FALSE;
|
661
|
+
memmove(set->buf + i, set->buf + (i + 1), sizeof(pcc_rule_t) * (set->len - (i + 1)));
|
662
|
+
return PCC_TRUE;
|
663
|
+
}
|
664
|
+
|
665
|
+
static void pcc_rule_set__clear(pcc_auxil_t auxil, pcc_rule_set_t *set) {
|
666
|
+
set->len = 0;
|
667
|
+
}
|
668
|
+
|
669
|
+
static void pcc_rule_set__copy(pcc_auxil_t auxil, pcc_rule_set_t *set, const pcc_rule_set_t *src) {
|
670
|
+
size_t i;
|
671
|
+
pcc_rule_set__clear(auxil, set);
|
672
|
+
for (i = 0; i < src->len; i++) {
|
673
|
+
pcc_rule_set__add(auxil, set, src->buf[i]);
|
674
|
+
}
|
675
|
+
}
|
676
|
+
|
677
|
+
static void pcc_rule_set__term(pcc_auxil_t auxil, pcc_rule_set_t *set) {
|
678
|
+
PCC_FREE(auxil, set->buf);
|
679
|
+
}
|
680
|
+
|
681
|
+
static pcc_lr_head_t *pcc_lr_head__create(pcc_context_t *ctx, pcc_rule_t rule) {
|
682
|
+
pcc_lr_head_t *const head = (pcc_lr_head_t *)pcc_memory_recycler__supply(ctx->auxil, &ctx->lr_head_recycler);
|
683
|
+
head->rule = rule;
|
684
|
+
pcc_rule_set__init(ctx->auxil, &head->invol);
|
685
|
+
pcc_rule_set__init(ctx->auxil, &head->eval);
|
686
|
+
head->hold = NULL;
|
687
|
+
return head;
|
688
|
+
}
|
689
|
+
|
690
|
+
static void pcc_lr_head__destroy(pcc_context_t *ctx, pcc_lr_head_t *head) {
|
691
|
+
if (head == NULL) return;
|
692
|
+
pcc_lr_head__destroy(ctx, head->hold);
|
693
|
+
pcc_rule_set__term(ctx->auxil, &head->eval);
|
694
|
+
pcc_rule_set__term(ctx->auxil, &head->invol);
|
695
|
+
pcc_memory_recycler__recycle(ctx->auxil, &ctx->lr_head_recycler, head);
|
696
|
+
}
|
697
|
+
|
698
|
+
static void pcc_lr_entry__destroy(pcc_auxil_t auxil, pcc_lr_entry_t *lr);
|
699
|
+
|
700
|
+
static pcc_lr_answer_t *pcc_lr_answer__create(pcc_context_t *ctx, pcc_lr_answer_type_t type, size_t pos<% if @location %>, pcc_location_t pos_loc<% end %>) {
|
701
|
+
pcc_lr_answer_t *answer = (pcc_lr_answer_t *)pcc_memory_recycler__supply(ctx->auxil, &ctx->lr_answer_recycler);
|
702
|
+
answer->type = type;
|
703
|
+
answer->pos = pos;
|
704
|
+
<%- if @location -%>
|
705
|
+
answer->pos_loc = pos_loc;
|
706
|
+
<%- end -%>
|
707
|
+
answer->hold = NULL;
|
708
|
+
switch (answer->type) {
|
709
|
+
case PCC_LR_ANSWER_LR:
|
710
|
+
answer->data.lr = NULL;
|
711
|
+
break;
|
712
|
+
case PCC_LR_ANSWER_CHUNK:
|
713
|
+
answer->data.chunk = NULL;
|
714
|
+
break;
|
715
|
+
default: /* unknown */
|
716
|
+
PCC_FREE(ctx->auxil, answer);
|
717
|
+
answer = NULL;
|
718
|
+
}
|
719
|
+
return answer;
|
720
|
+
}
|
721
|
+
|
722
|
+
static void pcc_lr_answer__set_chunk(pcc_context_t *ctx, pcc_lr_answer_t *answer, pcc_thunk_chunk_t *chunk) {
|
723
|
+
pcc_lr_answer_t *const a = pcc_lr_answer__create(ctx, answer->type, answer->pos<% if @location %>, answer->pos_loc<% end %>);
|
724
|
+
switch (answer->type) {
|
725
|
+
case PCC_LR_ANSWER_LR:
|
726
|
+
a->data.lr = answer->data.lr;
|
727
|
+
break;
|
728
|
+
case PCC_LR_ANSWER_CHUNK:
|
729
|
+
a->data.chunk = answer->data.chunk;
|
730
|
+
break;
|
731
|
+
default: /* unknown */
|
732
|
+
break;
|
733
|
+
}
|
734
|
+
a->hold = answer->hold;
|
735
|
+
answer->hold = a;
|
736
|
+
answer->type = PCC_LR_ANSWER_CHUNK;
|
737
|
+
answer->data.chunk = chunk;
|
738
|
+
}
|
739
|
+
|
740
|
+
static void pcc_lr_answer__destroy(pcc_context_t *ctx, pcc_lr_answer_t *answer) {
|
741
|
+
while (answer != NULL) {
|
742
|
+
pcc_lr_answer_t *const a = answer->hold;
|
743
|
+
switch (answer->type) {
|
744
|
+
case PCC_LR_ANSWER_LR:
|
745
|
+
pcc_lr_entry__destroy(ctx->auxil, answer->data.lr);
|
746
|
+
break;
|
747
|
+
case PCC_LR_ANSWER_CHUNK:
|
748
|
+
pcc_thunk_chunk__destroy(ctx, answer->data.chunk);
|
749
|
+
break;
|
750
|
+
default: /* unknown */
|
751
|
+
break;
|
752
|
+
}
|
753
|
+
pcc_memory_recycler__recycle(ctx->auxil, &ctx->lr_answer_recycler, answer);
|
754
|
+
answer = a;
|
755
|
+
}
|
756
|
+
}
|
757
|
+
|
758
|
+
static void pcc_lr_memo_map__init(pcc_auxil_t auxil, pcc_lr_memo_map_t *map) {
|
759
|
+
map->len = 0;
|
760
|
+
map->max = 0;
|
761
|
+
map->buf = NULL;
|
762
|
+
}
|
763
|
+
|
764
|
+
static size_t pcc_lr_memo_map__index(pcc_context_t *ctx, pcc_lr_memo_map_t *map, pcc_rule_t rule) {
|
765
|
+
size_t i;
|
766
|
+
for (i = 0; i < map->len; i++) {
|
767
|
+
if (map->buf[i].rule == rule) return i;
|
768
|
+
}
|
769
|
+
return PCC_VOID_VALUE;
|
770
|
+
}
|
771
|
+
|
772
|
+
static void pcc_lr_memo_map__put(pcc_context_t *ctx, pcc_lr_memo_map_t *map, pcc_rule_t rule, pcc_lr_answer_t *answer) {
|
773
|
+
const size_t i = pcc_lr_memo_map__index(ctx, map, rule);
|
774
|
+
if (i != PCC_VOID_VALUE) {
|
775
|
+
pcc_lr_answer__destroy(ctx, map->buf[i].answer);
|
776
|
+
map->buf[i].answer = answer;
|
777
|
+
}
|
778
|
+
else {
|
779
|
+
if (map->max <= map->len) {
|
780
|
+
const size_t n = map->len + 1;
|
781
|
+
size_t m = map->max;
|
782
|
+
if (m == 0) m = PCC_ARRAY_MIN_SIZE;
|
783
|
+
while (m < n && m != 0) m <<= 1;
|
784
|
+
if (m == 0) m = n;
|
785
|
+
map->buf = (pcc_lr_memo_t *)PCC_REALLOC(ctx->auxil, map->buf, sizeof(pcc_lr_memo_t) * m);
|
786
|
+
map->max = m;
|
787
|
+
}
|
788
|
+
map->buf[map->len].rule = rule;
|
789
|
+
map->buf[map->len].answer = answer;
|
790
|
+
map->len++;
|
791
|
+
}
|
792
|
+
}
|
793
|
+
|
794
|
+
static pcc_lr_answer_t *pcc_lr_memo_map__get(pcc_context_t *ctx, pcc_lr_memo_map_t *map, pcc_rule_t rule) {
|
795
|
+
const size_t i = pcc_lr_memo_map__index(ctx, map, rule);
|
796
|
+
return (i != PCC_VOID_VALUE) ? map->buf[i].answer : NULL;
|
797
|
+
}
|
798
|
+
|
799
|
+
static void pcc_lr_memo_map__term(pcc_context_t *ctx, pcc_lr_memo_map_t *map) {
|
800
|
+
while (map->len > 0) {
|
801
|
+
map->len--;
|
802
|
+
pcc_lr_answer__destroy(ctx, map->buf[map->len].answer);
|
803
|
+
}
|
804
|
+
PCC_FREE(ctx->auxil, map->buf);
|
805
|
+
}
|
806
|
+
|
807
|
+
static pcc_lr_table_entry_t *pcc_lr_table_entry__create(pcc_context_t *ctx) {
|
808
|
+
pcc_lr_table_entry_t *const entry = (pcc_lr_table_entry_t *)PCC_MALLOC(ctx->auxil, sizeof(pcc_lr_table_entry_t));
|
809
|
+
entry->head = NULL;
|
810
|
+
pcc_lr_memo_map__init(ctx->auxil, &entry->memos);
|
811
|
+
entry->hold_a = NULL;
|
812
|
+
entry->hold_h = NULL;
|
813
|
+
return entry;
|
814
|
+
}
|
815
|
+
|
816
|
+
static void pcc_lr_table_entry__destroy(pcc_context_t *ctx, pcc_lr_table_entry_t *entry) {
|
817
|
+
if (entry == NULL) return;
|
818
|
+
pcc_lr_head__destroy(ctx, entry->hold_h);
|
819
|
+
pcc_lr_answer__destroy(ctx, entry->hold_a);
|
820
|
+
pcc_lr_memo_map__term(ctx, &entry->memos);
|
821
|
+
PCC_FREE(ctx->auxil, entry);
|
822
|
+
}
|
823
|
+
|
824
|
+
static void pcc_lr_table__init(pcc_auxil_t auxil, pcc_lr_table_t *table) {
|
825
|
+
table->ofs = 0;
|
826
|
+
table->len = 0;
|
827
|
+
table->max = 0;
|
828
|
+
table->buf = NULL;
|
829
|
+
}
|
830
|
+
|
831
|
+
static void pcc_lr_table__resize(pcc_context_t *ctx, pcc_lr_table_t *table, size_t len) {
|
832
|
+
size_t i;
|
833
|
+
for (i = len; i < table->len; i++) pcc_lr_table_entry__destroy(ctx, table->buf[i]);
|
834
|
+
if (table->max < len) {
|
835
|
+
size_t m = table->max;
|
836
|
+
if (m == 0) m = PCC_ARRAY_MIN_SIZE;
|
837
|
+
while (m < len && m != 0) m <<= 1;
|
838
|
+
if (m == 0) m = len;
|
839
|
+
table->buf = (pcc_lr_table_entry_t **)PCC_REALLOC(ctx->auxil, table->buf, sizeof(pcc_lr_table_entry_t *) * m);
|
840
|
+
table->max = m;
|
841
|
+
}
|
842
|
+
for (i = table->len; i < len; i++) table->buf[i] = NULL;
|
843
|
+
table->len = len;
|
844
|
+
}
|
845
|
+
|
846
|
+
static void pcc_lr_table__set_head(pcc_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {
|
847
|
+
index += table->ofs;
|
848
|
+
if (index >= table->len) pcc_lr_table__resize(ctx, table, index + 1);
|
849
|
+
if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx);
|
850
|
+
table->buf[index]->head = head;
|
851
|
+
}
|
852
|
+
|
853
|
+
static void pcc_lr_table__hold_head(pcc_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_lr_head_t *head) {
|
854
|
+
index += table->ofs;
|
855
|
+
if (index >= table->len) pcc_lr_table__resize(ctx, table, index + 1);
|
856
|
+
if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx);
|
857
|
+
head->hold = table->buf[index]->hold_h;
|
858
|
+
table->buf[index]->hold_h = head;
|
859
|
+
}
|
860
|
+
|
861
|
+
static void pcc_lr_table__set_answer(pcc_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_rule_t rule, pcc_lr_answer_t *answer) {
|
862
|
+
index += table->ofs;
|
863
|
+
if (index >= table->len) pcc_lr_table__resize(ctx, table, index + 1);
|
864
|
+
if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx);
|
865
|
+
pcc_lr_memo_map__put(ctx, &table->buf[index]->memos, rule, answer);
|
866
|
+
}
|
867
|
+
|
868
|
+
static void pcc_lr_table__hold_answer(pcc_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_lr_answer_t *answer) {
|
869
|
+
index += table->ofs;
|
870
|
+
if (index >= table->len) pcc_lr_table__resize(ctx, table, index + 1);
|
871
|
+
if (table->buf[index] == NULL) table->buf[index] = pcc_lr_table_entry__create(ctx);
|
872
|
+
answer->hold = table->buf[index]->hold_a;
|
873
|
+
table->buf[index]->hold_a = answer;
|
874
|
+
}
|
875
|
+
|
876
|
+
static pcc_lr_head_t *pcc_lr_table__get_head(pcc_context_t *ctx, pcc_lr_table_t *table, size_t index) {
|
877
|
+
index += table->ofs;
|
878
|
+
if (index >= table->len || table->buf[index] == NULL) return NULL;
|
879
|
+
return table->buf[index]->head;
|
880
|
+
}
|
881
|
+
|
882
|
+
static pcc_lr_answer_t *pcc_lr_table__get_answer(pcc_context_t *ctx, pcc_lr_table_t *table, size_t index, pcc_rule_t rule) {
|
883
|
+
index += table->ofs;
|
884
|
+
if (index >= table->len || table->buf[index] == NULL) return NULL;
|
885
|
+
return pcc_lr_memo_map__get(ctx, &table->buf[index]->memos, rule);
|
886
|
+
}
|
887
|
+
|
888
|
+
static void pcc_lr_table__shift(pcc_context_t *ctx, pcc_lr_table_t *table, size_t count) {
|
889
|
+
size_t i;
|
890
|
+
if (count > table->len - table->ofs) count = table->len - table->ofs;
|
891
|
+
for (i = 0; i < count; i++) pcc_lr_table_entry__destroy(ctx, table->buf[table->ofs++]);
|
892
|
+
if (table->ofs > (table->max >> 1)) {
|
893
|
+
memmove(table->buf, table->buf + table->ofs, sizeof(pcc_lr_table_entry_t *) * (table->len - table->ofs));
|
894
|
+
table->len -= table->ofs;
|
895
|
+
table->ofs = 0;
|
896
|
+
}
|
897
|
+
}
|
898
|
+
|
899
|
+
static void pcc_lr_table__term(pcc_context_t *ctx, pcc_lr_table_t *table) {
|
900
|
+
while (table->len > table->ofs) {
|
901
|
+
table->len--;
|
902
|
+
pcc_lr_table_entry__destroy(ctx, table->buf[table->len]);
|
903
|
+
}
|
904
|
+
PCC_FREE(ctx->auxil, table->buf);
|
905
|
+
}
|
906
|
+
|
907
|
+
static pcc_lr_entry_t *pcc_lr_entry__create(pcc_auxil_t auxil, pcc_rule_t rule) {
|
908
|
+
pcc_lr_entry_t *const lr = (pcc_lr_entry_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_entry_t));
|
909
|
+
lr->rule = rule;
|
910
|
+
lr->seed = NULL;
|
911
|
+
lr->head = NULL;
|
912
|
+
return lr;
|
913
|
+
}
|
914
|
+
|
915
|
+
static void pcc_lr_entry__destroy(pcc_auxil_t auxil, pcc_lr_entry_t *lr) {
|
916
|
+
PCC_FREE(auxil, lr);
|
917
|
+
}
|
918
|
+
|
919
|
+
static void pcc_lr_stack__init(pcc_auxil_t auxil, pcc_lr_stack_t *stack) {
|
920
|
+
stack->len = 0;
|
921
|
+
stack->max = 0;
|
922
|
+
stack->buf = NULL;
|
923
|
+
}
|
924
|
+
|
925
|
+
static void pcc_lr_stack__push(pcc_auxil_t auxil, pcc_lr_stack_t *stack, pcc_lr_entry_t *lr) {
|
926
|
+
if (stack->max <= stack->len) {
|
927
|
+
const size_t n = stack->len + 1;
|
928
|
+
size_t m = stack->max;
|
929
|
+
if (m == 0) m = PCC_ARRAY_MIN_SIZE;
|
930
|
+
while (m < n && m != 0) m <<= 1;
|
931
|
+
if (m == 0) m = n;
|
932
|
+
stack->buf = (pcc_lr_entry_t **)PCC_REALLOC(auxil, stack->buf, sizeof(pcc_lr_entry_t *) * m);
|
933
|
+
stack->max = m;
|
934
|
+
}
|
935
|
+
stack->buf[stack->len++] = lr;
|
936
|
+
}
|
937
|
+
|
938
|
+
static pcc_lr_entry_t *pcc_lr_stack__pop(pcc_auxil_t auxil, pcc_lr_stack_t *stack) {
|
939
|
+
return stack->buf[--stack->len];
|
940
|
+
}
|
941
|
+
|
942
|
+
static void pcc_lr_stack__term(pcc_auxil_t auxil, pcc_lr_stack_t *stack) {
|
943
|
+
PCC_FREE(auxil, stack->buf);
|
944
|
+
}
|
945
|
+
|
946
|
+
static pcc_context_t *pcc_context__create(pcc_auxil_t auxil) {
|
947
|
+
pcc_context_t *const ctx = (pcc_context_t *)PCC_MALLOC(auxil, sizeof(pcc_context_t));
|
948
|
+
ctx->pos = 0;
|
949
|
+
ctx->cur = 0;
|
950
|
+
<%- if @location -%>
|
951
|
+
pcc_location_init(&ctx->pos_loc);
|
952
|
+
pcc_location_init(&ctx->cur_loc);
|
953
|
+
<%- end -%>
|
954
|
+
ctx->level = 0;
|
955
|
+
pcc_char_array__init(auxil, &ctx->buffer);
|
956
|
+
pcc_lr_table__init(auxil, &ctx->lrtable);
|
957
|
+
pcc_lr_stack__init(auxil, &ctx->lrstack);
|
958
|
+
pcc_thunk_array__init(auxil, &ctx->thunks);
|
959
|
+
pcc_memory_recycler__init(auxil, &ctx->thunk_chunk_recycler, sizeof(pcc_thunk_chunk_t));
|
960
|
+
pcc_memory_recycler__init(auxil, &ctx->lr_head_recycler, sizeof(pcc_lr_head_t));
|
961
|
+
pcc_memory_recycler__init(auxil, &ctx->lr_answer_recycler, sizeof(pcc_lr_answer_t));
|
962
|
+
ctx->auxil = auxil;
|
963
|
+
return ctx;
|
964
|
+
}
|
965
|
+
|
966
|
+
static void pcc_context__destroy(pcc_context_t *ctx) {
|
967
|
+
if (ctx == NULL) return;
|
968
|
+
pcc_thunk_array__term(ctx->auxil, &ctx->thunks);
|
969
|
+
pcc_lr_stack__term(ctx->auxil, &ctx->lrstack);
|
970
|
+
pcc_lr_table__term(ctx, &ctx->lrtable);
|
971
|
+
pcc_char_array__term(ctx->auxil, &ctx->buffer);
|
972
|
+
pcc_memory_recycler__term(ctx->auxil, &ctx->thunk_chunk_recycler);
|
973
|
+
pcc_memory_recycler__term(ctx->auxil, &ctx->lr_head_recycler);
|
974
|
+
pcc_memory_recycler__term(ctx->auxil, &ctx->lr_answer_recycler);
|
975
|
+
PCC_FREE(ctx->auxil, ctx);
|
976
|
+
}
|
977
|
+
|
978
|
+
static size_t pcc_refill_buffer(pcc_context_t *ctx, size_t num) {
|
979
|
+
if (ctx->buffer.len >= ctx->cur + num) return ctx->buffer.len - ctx->cur;
|
980
|
+
while (ctx->buffer.len < ctx->cur + num) {
|
981
|
+
const int c = PCC_GETCHAR(ctx->auxil);
|
982
|
+
if (c < 0) break;
|
983
|
+
pcc_char_array__add(ctx->auxil, &ctx->buffer, (char)c);
|
984
|
+
}
|
985
|
+
return ctx->buffer.len - ctx->cur;
|
986
|
+
}
|
987
|
+
|
988
|
+
MARK_FUNC_AS_USED
|
989
|
+
static void pcc_commit_buffer(pcc_context_t *ctx) {
|
990
|
+
memmove(ctx->buffer.buf, ctx->buffer.buf + ctx->cur, ctx->buffer.len - ctx->cur);
|
991
|
+
ctx->buffer.len -= ctx->cur;
|
992
|
+
ctx->pos += ctx->cur;
|
993
|
+
pcc_lr_table__shift(ctx, &ctx->lrtable, ctx->cur);
|
994
|
+
ctx->cur = 0;
|
995
|
+
<%- if @location -%>
|
996
|
+
ctx->pos_loc = pcc_location_add(ctx->pos_loc, ctx->cur_loc);
|
997
|
+
pcc_location_init(&ctx->cur_loc);
|
998
|
+
<%- end -%>
|
999
|
+
}
|
1000
|
+
|
1001
|
+
MARK_FUNC_AS_USED
|
1002
|
+
static const char *pcc_get_capture_string(pcc_context_t *ctx, const pcc_capture_t *capt) {
|
1003
|
+
if (capt->string == NULL)
|
1004
|
+
((pcc_capture_t *)capt)->string =
|
1005
|
+
pcc_strndup_e(ctx->auxil, ctx->buffer.buf + capt->range.start, capt->range.end - capt->range.start);
|
1006
|
+
return capt->string;
|
1007
|
+
}
|
1008
|
+
|
1009
|
+
<% if @utf8 -%>
|
1010
|
+
static size_t pcc_get_char_as_utf32(pcc_context_t *ctx, int *out) { /* with checking UTF-8 validity */
|
1011
|
+
int c, u;
|
1012
|
+
size_t n;
|
1013
|
+
if (pcc_refill_buffer(ctx, 1) < 1) return 0;
|
1014
|
+
c = (int)(unsigned char)ctx->buffer.buf[ctx->cur];
|
1015
|
+
n = (c < 0x80) ? 1 :
|
1016
|
+
((c & 0xe0) == 0xc0) ? 2 :
|
1017
|
+
((c & 0xf0) == 0xe0) ? 3 :
|
1018
|
+
((c & 0xf8) == 0xf0) ? 4 : 0;
|
1019
|
+
if (n < 1) return 0;
|
1020
|
+
if (pcc_refill_buffer(ctx, n) < n) return 0;
|
1021
|
+
switch (n) {
|
1022
|
+
case 1:
|
1023
|
+
u = c;
|
1024
|
+
break;
|
1025
|
+
case 2:
|
1026
|
+
u = c & 0x1f;
|
1027
|
+
c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 1];
|
1028
|
+
if ((c & 0xc0) != 0x80) return 0;
|
1029
|
+
u <<= 6; u |= c & 0x3f;
|
1030
|
+
if (u < 0x80) return 0;
|
1031
|
+
break;
|
1032
|
+
case 3:
|
1033
|
+
u = c & 0x0f;
|
1034
|
+
c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 1];
|
1035
|
+
if ((c & 0xc0) != 0x80) return 0;
|
1036
|
+
u <<= 6; u |= c & 0x3f;
|
1037
|
+
c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 2];
|
1038
|
+
if ((c & 0xc0) != 0x80) return 0;
|
1039
|
+
u <<= 6; u |= c & 0x3f;
|
1040
|
+
if (u < 0x800) return 0;
|
1041
|
+
break;
|
1042
|
+
case 4:
|
1043
|
+
u = c & 0x07;
|
1044
|
+
c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 1];
|
1045
|
+
if ((c & 0xc0) != 0x80) return 0;
|
1046
|
+
u <<= 6; u |= c & 0x3f;
|
1047
|
+
c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 2];
|
1048
|
+
if ((c & 0xc0) != 0x80) return 0;
|
1049
|
+
u <<= 6; u |= c & 0x3f;
|
1050
|
+
c = (int)(unsigned char)ctx->buffer.buf[ctx->cur + 3];
|
1051
|
+
if ((c & 0xc0) != 0x80) return 0;
|
1052
|
+
u <<= 6; u |= c & 0x3f;
|
1053
|
+
if (u < 0x10000 || u > 0x10ffff) return 0;
|
1054
|
+
break;
|
1055
|
+
default:
|
1056
|
+
return 0;
|
1057
|
+
}
|
1058
|
+
if (out) *out = u;
|
1059
|
+
return n;
|
1060
|
+
}
|
1061
|
+
|
1062
|
+
<% end -%>
|
1063
|
+
MARK_FUNC_AS_USED
|
1064
|
+
static pcc_bool_t pcc_apply_rule(pcc_context_t *ctx, pcc_rule_t rule, pcc_thunk_array_t *thunks, pcc_value_t *value) {
|
1065
|
+
static pcc_value_t null;
|
1066
|
+
pcc_thunk_chunk_t *c = NULL;
|
1067
|
+
const size_t p = ctx->pos + ctx->cur;
|
1068
|
+
<%- if @location -%>
|
1069
|
+
const pcc_location_t p_loc = pcc_location_add(ctx->pos_loc, ctx->cur_loc);
|
1070
|
+
<%- end -%>
|
1071
|
+
pcc_bool_t b = PCC_TRUE;
|
1072
|
+
pcc_lr_answer_t *a = pcc_lr_table__get_answer(ctx, &ctx->lrtable, p, rule);
|
1073
|
+
pcc_lr_head_t *h = pcc_lr_table__get_head(ctx, &ctx->lrtable, p);
|
1074
|
+
if (h != NULL) {
|
1075
|
+
if (a == NULL && rule != h->rule && pcc_rule_set__index(ctx->auxil, &h->invol, rule) == PCC_VOID_VALUE) {
|
1076
|
+
b = PCC_FALSE;
|
1077
|
+
c = NULL;
|
1078
|
+
}
|
1079
|
+
else if (pcc_rule_set__remove(ctx->auxil, &h->eval, rule)) {
|
1080
|
+
b = PCC_FALSE;
|
1081
|
+
c = rule(ctx);
|
1082
|
+
a = pcc_lr_answer__create(ctx, PCC_LR_ANSWER_CHUNK, ctx->pos + ctx->cur<% if @location %>, pcc_location_add(ctx->pos_loc, ctx->cur_loc)<% end %>);
|
1083
|
+
a->data.chunk = c;
|
1084
|
+
pcc_lr_table__hold_answer(ctx, &ctx->lrtable, p, a);
|
1085
|
+
}
|
1086
|
+
}
|
1087
|
+
if (b) {
|
1088
|
+
if (a != NULL) {
|
1089
|
+
ctx->cur = a->pos - ctx->pos;
|
1090
|
+
<%- if @location -%>
|
1091
|
+
ctx->cur_loc = pcc_location_sub(a->pos_loc, ctx->pos_loc);
|
1092
|
+
<%- end -%>
|
1093
|
+
switch (a->type) {
|
1094
|
+
case PCC_LR_ANSWER_LR:
|
1095
|
+
if (a->data.lr->head == NULL) {
|
1096
|
+
a->data.lr->head = pcc_lr_head__create(ctx, rule);
|
1097
|
+
pcc_lr_table__hold_head(ctx, &ctx->lrtable, p, a->data.lr->head);
|
1098
|
+
}
|
1099
|
+
{
|
1100
|
+
size_t i = ctx->lrstack.len;
|
1101
|
+
while (i > 0) {
|
1102
|
+
i--;
|
1103
|
+
if (ctx->lrstack.buf[i]->head == a->data.lr->head) break;
|
1104
|
+
ctx->lrstack.buf[i]->head = a->data.lr->head;
|
1105
|
+
pcc_rule_set__add(ctx->auxil, &a->data.lr->head->invol, ctx->lrstack.buf[i]->rule);
|
1106
|
+
}
|
1107
|
+
}
|
1108
|
+
c = a->data.lr->seed;
|
1109
|
+
break;
|
1110
|
+
case PCC_LR_ANSWER_CHUNK:
|
1111
|
+
c = a->data.chunk;
|
1112
|
+
break;
|
1113
|
+
default: /* unknown */
|
1114
|
+
break;
|
1115
|
+
}
|
1116
|
+
}
|
1117
|
+
else {
|
1118
|
+
pcc_lr_entry_t *const e = pcc_lr_entry__create(ctx->auxil, rule);
|
1119
|
+
pcc_lr_stack__push(ctx->auxil, &ctx->lrstack, e);
|
1120
|
+
a = pcc_lr_answer__create(ctx, PCC_LR_ANSWER_LR, p<% if @location %>, p_loc<% end %>);
|
1121
|
+
a->data.lr = e;
|
1122
|
+
pcc_lr_table__set_answer(ctx, &ctx->lrtable, p, rule, a);
|
1123
|
+
c = rule(ctx);
|
1124
|
+
pcc_lr_stack__pop(ctx->auxil, &ctx->lrstack);
|
1125
|
+
a->pos = ctx->pos + ctx->cur;
|
1126
|
+
<%- if @location -%>
|
1127
|
+
a->pos_loc = pcc_location_add(ctx->pos_loc, ctx->cur_loc);
|
1128
|
+
<%- end -%>
|
1129
|
+
if (e->head == NULL) {
|
1130
|
+
pcc_lr_answer__set_chunk(ctx, a, c);
|
1131
|
+
}
|
1132
|
+
else {
|
1133
|
+
e->seed = c;
|
1134
|
+
h = a->data.lr->head;
|
1135
|
+
if (h->rule != rule) {
|
1136
|
+
c = a->data.lr->seed;
|
1137
|
+
a = pcc_lr_answer__create(ctx, PCC_LR_ANSWER_CHUNK, ctx->pos + ctx->cur<% if @location %>, pcc_location_add(ctx->pos_loc, ctx->cur_loc)<% end %>);
|
1138
|
+
a->data.chunk = c;
|
1139
|
+
pcc_lr_table__hold_answer(ctx, &ctx->lrtable, p, a);
|
1140
|
+
}
|
1141
|
+
else {
|
1142
|
+
pcc_lr_answer__set_chunk(ctx, a, a->data.lr->seed);
|
1143
|
+
if (a->data.chunk == NULL) {
|
1144
|
+
c = NULL;
|
1145
|
+
}
|
1146
|
+
else {
|
1147
|
+
pcc_lr_table__set_head(ctx, &ctx->lrtable, p, h);
|
1148
|
+
for (;;) {
|
1149
|
+
ctx->cur = p - ctx->pos;
|
1150
|
+
<%- if @location -%>
|
1151
|
+
ctx->cur_loc = pcc_location_sub(p_loc, ctx->pos_loc);
|
1152
|
+
<%- end -%>
|
1153
|
+
pcc_rule_set__copy(ctx->auxil, &h->eval, &h->invol);
|
1154
|
+
c = rule(ctx);
|
1155
|
+
if (c == NULL || ctx->pos + ctx->cur <= a->pos) break;
|
1156
|
+
pcc_lr_answer__set_chunk(ctx, a, c);
|
1157
|
+
a->pos = ctx->pos + ctx->cur;
|
1158
|
+
<%- if @location -%>
|
1159
|
+
a->pos_loc = pcc_location_add(ctx->pos_loc, ctx->cur_loc);
|
1160
|
+
<%- end -%>
|
1161
|
+
}
|
1162
|
+
pcc_thunk_chunk__destroy(ctx, c);
|
1163
|
+
pcc_lr_table__set_head(ctx, &ctx->lrtable, p, NULL);
|
1164
|
+
ctx->cur = a->pos - ctx->pos;
|
1165
|
+
<%- if @location -%>
|
1166
|
+
ctx->cur_loc = pcc_location_sub(a->pos_loc, ctx->pos_loc);
|
1167
|
+
<%- end -%>
|
1168
|
+
c = a->data.chunk;
|
1169
|
+
}
|
1170
|
+
}
|
1171
|
+
}
|
1172
|
+
}
|
1173
|
+
}
|
1174
|
+
if (c == NULL) return PCC_FALSE;
|
1175
|
+
if (value == NULL) value = &null;
|
1176
|
+
memset(value, 0, sizeof(pcc_value_t)); /* in case */
|
1177
|
+
pcc_thunk_array__add(ctx->auxil, thunks, pcc_thunk__create_node(ctx->auxil, &c->thunks, value));
|
1178
|
+
return PCC_TRUE;
|
1179
|
+
}
|
1180
|
+
|
1181
|
+
MARK_FUNC_AS_USED
|
1182
|
+
static void pcc_do_action(pcc_context_t *ctx, const pcc_thunk_array_t *thunks, pcc_value_t *value) {
|
1183
|
+
size_t i;
|
1184
|
+
for (i = 0; i < thunks->len; i++) {
|
1185
|
+
pcc_thunk_t *const thunk = thunks->buf[i];
|
1186
|
+
switch (thunk->type) {
|
1187
|
+
case PCC_THUNK_LEAF:
|
1188
|
+
thunk->data.leaf.action(ctx, thunk, value);
|
1189
|
+
break;
|
1190
|
+
case PCC_THUNK_NODE:
|
1191
|
+
pcc_do_action(ctx, thunk->data.node.thunks, thunk->data.node.value);
|
1192
|
+
break;
|
1193
|
+
default: /* unknown */
|
1194
|
+
break;
|
1195
|
+
}
|
1196
|
+
}
|
1197
|
+
}
|
1198
|
+
|
1199
|
+
<%- @root.rules.each do |rule| -%>
|
1200
|
+
<%- rule.codes.each do |code| -%>
|
1201
|
+
static void pcc_action_<%= rule.name %>_<%= code.index %>(<%= prefix %>_context_t *__pcc_ctx, pcc_thunk_t *__pcc_in, pcc_value_t *__pcc_out) {
|
1202
|
+
#define auxil (__pcc_ctx->auxil)
|
1203
|
+
#define __ (*__pcc_out)
|
1204
|
+
<%- code.vars.each do |ref| -%>
|
1205
|
+
#define <%= ref.var %> (*__pcc_in->data.leaf.values.buf[<%= ref.index %>])
|
1206
|
+
<% end -%>
|
1207
|
+
#define _0 pcc_get_capture_string(__pcc_ctx, &__pcc_in->data.leaf.capt0)
|
1208
|
+
#define _0s ((const size_t)(__pcc_ctx->pos + __pcc_in->data.leaf.capt0.range.start))
|
1209
|
+
#define _0e ((const size_t)(__pcc_ctx->pos + __pcc_in->data.leaf.capt0.range.end))
|
1210
|
+
<%- if @location -%>
|
1211
|
+
#define _0sl ((const pcc_location_t)(pcc_location_add(__pcc_ctx->pos_loc, *__pcc_in->data.leaf.capt0.range.start_loc_ptr)))
|
1212
|
+
#define _0el ((const pcc_location_t)(pcc_location_add(__pcc_ctx->pos_loc, *__pcc_in->data.leaf.capt0.range.end_loc_ptr)))
|
1213
|
+
<%- end -%>
|
1214
|
+
<%- if @capture_in_code -%>
|
1215
|
+
#define _0c __pcc_in->data.leaf.capt0
|
1216
|
+
<%- end -%>
|
1217
|
+
<% code.capts.each do |capture| -%>
|
1218
|
+
#define _<%= capture.index + 1 %> pcc_get_capture_string(__pcc_ctx, __pcc_in->data.leaf.capts.buf[<%= capture.index %>])
|
1219
|
+
#define _<%= capture.index + 1 %>s ((const size_t)(__pcc_ctx->pos + __pcc_in->data.leaf.capts.buf[<%= capture.index %>]->range.start))
|
1220
|
+
#define _<%= capture.index + 1 %>e ((const size_t)(__pcc_ctx->pos + __pcc_in->data.leaf.capts.buf[<%= capture.index %>]->range.end))
|
1221
|
+
<%- if @location -%>
|
1222
|
+
#define _<%= capture.index + 1 %>sl ((const pcc_location_t)(pcc_location_add(__pcc_ctx->pos_loc, *__pcc_in->data.leaf.capts.buf[<%= capture.index %>]->range.start_loc_ptr)))
|
1223
|
+
#define _<%= capture.index + 1 %>el ((const pcc_location_t)(pcc_location_add(__pcc_ctx->pos_loc, *__pcc_in->data.leaf.capts.buf[<%= capture.index %>]->range.end_loc_ptr)))
|
1224
|
+
<%- end -%>
|
1225
|
+
<%- if @capture_in_code -%>
|
1226
|
+
#define _<%= capture.index + 1 %>c (*__pcc_in->data.leaf.capts.buf[<%= capture.index %>])
|
1227
|
+
<%- end -%>
|
1228
|
+
<%- end -%>
|
1229
|
+
<%= stream.get_code_block(code.code, 4, @iname) -%>
|
1230
|
+
<%- code.capts.reverse_each do |capture| -%>
|
1231
|
+
<%- if @location -%>
|
1232
|
+
#undef _<%= capture.index + 1 %>el
|
1233
|
+
#undef _<%= capture.index + 1 %>sl
|
1234
|
+
<%- end -%>
|
1235
|
+
#undef _<%= capture.index + 1 %>e
|
1236
|
+
#undef _<%= capture.index + 1 %>s
|
1237
|
+
#undef _<%= capture.index + 1 %>
|
1238
|
+
<%- end -%>
|
1239
|
+
#undef _0e
|
1240
|
+
#undef _0s
|
1241
|
+
#undef _0
|
1242
|
+
<%- code.vars.reverse_each do |ref| -%>
|
1243
|
+
#undef <%= ref.var %>
|
1244
|
+
<%- end -%>
|
1245
|
+
#undef __
|
1246
|
+
#undef auxil
|
1247
|
+
}
|
1248
|
+
|
1249
|
+
<%- end -%>
|
1250
|
+
<%- end -%>
|
1251
|
+
<%- @root.rules.each do |rule| -%>
|
1252
|
+
static pcc_thunk_chunk_t *pcc_evaluate_rule_<%= rule.name %>(pcc_context_t *ctx);
|
1253
|
+
<%- end -%>
|
1254
|
+
|
1255
|
+
<%- @root.rules.each do |rule| -%>
|
1256
|
+
static pcc_thunk_chunk_t *pcc_evaluate_rule_<%= rule.name %>(pcc_context_t *ctx) {
|
1257
|
+
pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx);
|
1258
|
+
chunk->pos = ctx->cur;
|
1259
|
+
<%- if @location -%>
|
1260
|
+
chunk->pos_loc = ctx->cur_loc;
|
1261
|
+
<%- end -%>
|
1262
|
+
<%- gen = ::Packcr::Generator.new(rule, @ascii, @location) -%>
|
1263
|
+
<%= gen.generate_code(rule, 0, 0, false) -%>
|
1264
|
+
}
|
1265
|
+
|
1266
|
+
<%- end -%>
|
1267
|
+
<%= prefix %>_context_t *<%= prefix %>_create(<%= auxil_def %>auxil) {
|
1268
|
+
return pcc_context__create(auxil);
|
1269
|
+
}
|
1270
|
+
|
1271
|
+
int <%= prefix %>_parse(<%= prefix %>_context_t *ctx, <%= value_def %>*ret) {
|
1272
|
+
size_t pos = ctx->pos;
|
1273
|
+
<%- if !@root.rules.empty? -%>
|
1274
|
+
if (pcc_apply_rule(ctx, pcc_evaluate_rule_<%= @root.rules[0].name %>, &ctx->thunks, ret))
|
1275
|
+
pcc_do_action(ctx, &ctx->thunks, ret);
|
1276
|
+
else
|
1277
|
+
PCC_ERROR(ctx->auxil);
|
1278
|
+
pcc_commit_buffer(ctx);
|
1279
|
+
<%- end -%>
|
1280
|
+
pcc_thunk_array__revert(ctx->auxil, &ctx->thunks, 0);
|
1281
|
+
return pos != ctx->pos && pcc_refill_buffer(ctx, 1) >= 1;
|
1282
|
+
}
|
1283
|
+
|
1284
|
+
void <%= prefix %>_destroy(<%= prefix %>_context_t *ctx) {
|
1285
|
+
pcc_context__destroy(ctx);
|
1286
|
+
}
|
1287
|
+
<%- if !code(:lsource).empty? -%>
|
1288
|
+
|
1289
|
+
<%- code(:lsource).each do |code| -%>
|
1290
|
+
<%= stream.get_code_block(code, 0, @iname) -%>
|
1291
|
+
<%- end -%>
|
1292
|
+
<%- end -%>
|