packcr 0.0.6 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +21 -0
- data/README.md +73 -0
- data/lib/packcr/cli.rb +18 -16
- data/lib/packcr/code_block.rb +0 -1
- data/lib/packcr/context.rb +20 -18
- data/lib/packcr/generated/context.rb +483 -0
- data/lib/packcr/generated/node/action_node.rb +60 -0
- data/lib/packcr/generated/node/alternate_node.rb +98 -0
- data/lib/packcr/generated/node/capture_node.rb +39 -0
- data/lib/packcr/generated/node/charclass_node.rb +372 -0
- data/lib/packcr/generated/node/eof_node.rb +20 -0
- data/lib/packcr/generated/node/error_node.rb +67 -0
- data/lib/packcr/generated/node/expand_node.rb +30 -0
- data/lib/packcr/generated/node/predicate_node.rb +140 -0
- data/lib/packcr/generated/node/quantity_node.rb +166 -0
- data/lib/packcr/generated/node/reference_node.rb +70 -0
- data/lib/packcr/generated/node/rule_node.rb +63 -0
- data/lib/packcr/generated/node/sequence_node.rb +42 -0
- data/lib/packcr/generated/node/string_node.rb +60 -0
- data/lib/packcr/generator.rb +2 -1
- data/lib/packcr/node/action_node.rb +4 -2
- data/lib/packcr/node/alternate_node.rb +3 -1
- data/lib/packcr/node/capture_node.rb +3 -1
- data/lib/packcr/node/charclass_node.rb +24 -28
- data/lib/packcr/node/eof_node.rb +4 -2
- data/lib/packcr/node/error_node.rb +3 -1
- data/lib/packcr/node/expand_node.rb +8 -5
- data/lib/packcr/node/predicate_node.rb +4 -2
- data/lib/packcr/node/quantity_node.rb +12 -10
- data/lib/packcr/node/reference_node.rb +11 -5
- data/lib/packcr/node/root_node.rb +1 -0
- data/lib/packcr/node/rule_node.rb +7 -4
- data/lib/packcr/node/sequence_node.rb +3 -1
- data/lib/packcr/node/string_node.rb +9 -6
- data/lib/packcr/node.rb +3 -5
- data/lib/packcr/parser.rb +4389 -4064
- data/lib/packcr/stream.rb +17 -12
- data/lib/packcr/templates/context/header.c.erb +3 -3
- data/lib/packcr/templates/context/source.c.erb +481 -690
- data/lib/packcr/templates/context/source.rb.erb +123 -171
- data/lib/packcr/templates/node/action.c.erb +5 -5
- data/lib/packcr/templates/node/action.rb.erb +2 -2
- data/lib/packcr/templates/node/alternate.c.erb +9 -9
- data/lib/packcr/templates/node/alternate.rb.erb +4 -5
- data/lib/packcr/templates/node/capture.c.erb +7 -7
- data/lib/packcr/templates/node/capture.rb.erb +4 -4
- data/lib/packcr/templates/node/charclass.c.erb +12 -12
- data/lib/packcr/templates/node/charclass.rb.erb +6 -6
- data/lib/packcr/templates/node/charclass_any.c.erb +3 -3
- data/lib/packcr/templates/node/charclass_any.rb.erb +2 -2
- data/lib/packcr/templates/node/charclass_fail.c.erb +1 -1
- data/lib/packcr/templates/node/charclass_one.c.erb +10 -10
- data/lib/packcr/templates/node/charclass_one.rb.erb +6 -6
- data/lib/packcr/templates/node/charclass_utf8.c.erb +8 -8
- data/lib/packcr/templates/node/charclass_utf8.rb.erb +3 -3
- data/lib/packcr/templates/node/charclass_utf8_reverse.rb.erb +5 -5
- data/lib/packcr/templates/node/eof.c.erb +1 -1
- data/lib/packcr/templates/node/error.c.erb +11 -11
- data/lib/packcr/templates/node/error.rb.erb +2 -2
- data/lib/packcr/templates/node/expand.c.erb +5 -5
- data/lib/packcr/templates/node/expand.rb.erb +3 -3
- data/lib/packcr/templates/node/predicate.c.erb +10 -10
- data/lib/packcr/templates/node/predicate.rb.erb +6 -6
- data/lib/packcr/templates/node/predicate_neg.c.erb +8 -8
- data/lib/packcr/templates/node/predicate_neg.rb.erb +6 -6
- data/lib/packcr/templates/node/quantity_many.c.erb +47 -0
- data/lib/packcr/templates/node/{quantify_many.rb.erb → quantity_many.rb.erb} +9 -9
- data/lib/packcr/templates/node/quantity_one.c.erb +23 -0
- data/lib/packcr/templates/node/{quantify_one.rb.erb → quantity_one.rb.erb} +8 -8
- data/lib/packcr/templates/node/reference.c.erb +14 -2
- data/lib/packcr/templates/node/reference.rb.erb +16 -4
- data/lib/packcr/templates/node/reference_reverse.rb.erb +16 -4
- data/lib/packcr/templates/node/rule.c.erb +14 -7
- data/lib/packcr/templates/node/rule.rb.erb +26 -19
- data/lib/packcr/templates/node/string_many.c.erb +6 -6
- data/lib/packcr/templates/node/string_many.rb.erb +3 -3
- data/lib/packcr/templates/node/string_one.c.erb +5 -5
- data/lib/packcr/templates/node/string_one.rb.erb +3 -3
- data/lib/packcr/util.rb +21 -16
- data/lib/packcr/version.rb +1 -1
- data/lib/packcr.rb +8 -11
- metadata +40 -10
- data/lib/packcr/templates/node/quantify_many.c.erb +0 -45
- data/lib/packcr/templates/node/quantify_one.c.erb +0 -21
- data/lib/packcr/tokenizer.rb +0 -2948
@@ -16,14 +16,14 @@
|
|
16
16
|
|
17
17
|
#ifndef _MSC_VER
|
18
18
|
#if defined __GNUC__ && defined _WIN32 /* MinGW */
|
19
|
-
#ifndef
|
20
|
-
#define strnlen(str, maxlen)
|
21
|
-
static size_t
|
19
|
+
#ifndef PACKCR_USE_SYSTEM_STRNLEN
|
20
|
+
#define strnlen(str, maxlen) packcr_strnlen(str, maxlen)
|
21
|
+
static size_t packcr_strnlen(const char *str, size_t maxlen) {
|
22
22
|
size_t i;
|
23
23
|
for (i = 0; i < maxlen && str[i]; i++);
|
24
24
|
return i;
|
25
25
|
}
|
26
|
-
#endif /* !
|
26
|
+
#endif /* !PACKCR_USE_SYSTEM_STRNLEN */
|
27
27
|
#endif /* defined __GNUC__ && defined _WIN32 */
|
28
28
|
#endif /* !_MSC_VER */
|
29
29
|
|
@@ -48,260 +48,221 @@ static size_t pcc_strnlen(const char *str, size_t maxlen) {
|
|
48
48
|
#define MARK_FUNC_AS_USED __attribute__((__unused__))
|
49
49
|
#endif
|
50
50
|
|
51
|
-
#ifndef
|
52
|
-
#define
|
53
|
-
#endif /* !
|
51
|
+
#ifndef PACKCR_BUFFER_MIN_SIZE
|
52
|
+
#define PACKCR_BUFFER_MIN_SIZE 256
|
53
|
+
#endif /* !PACKCR_BUFFER_MIN_SIZE */
|
54
54
|
|
55
|
-
#ifndef
|
56
|
-
#define
|
57
|
-
#endif /* !
|
55
|
+
#ifndef PACKCR_ARRAY_MIN_SIZE
|
56
|
+
#define PACKCR_ARRAY_MIN_SIZE 2
|
57
|
+
#endif /* !PACKCR_ARRAY_MIN_SIZE */
|
58
58
|
|
59
|
-
#ifndef
|
60
|
-
#define
|
61
|
-
#endif /* !
|
59
|
+
#ifndef PACKCR_POOL_MIN_SIZE
|
60
|
+
#define PACKCR_POOL_MIN_SIZE 65536
|
61
|
+
#endif /* !PACKCR_POOL_MIN_SIZE */
|
62
62
|
|
63
|
-
#define
|
64
|
-
#define
|
65
|
-
#define
|
63
|
+
#define PACKCR_DBG_EVALUATE 0
|
64
|
+
#define PACKCR_DBG_MATCH 1
|
65
|
+
#define PACKCR_DBG_NOMATCH 2
|
66
66
|
|
67
|
-
#define
|
67
|
+
#define PACKCR_VOID_VALUE (~(size_t)0)
|
68
68
|
|
69
|
-
typedef enum
|
70
|
-
|
71
|
-
|
72
|
-
}
|
69
|
+
typedef enum packcr_bool_tag {
|
70
|
+
PACKCR_FALSE = 0,
|
71
|
+
PACKCR_TRUE
|
72
|
+
} packcr_bool_t;
|
73
73
|
|
74
|
-
typedef struct
|
74
|
+
typedef struct packcr_char_array_tag {
|
75
75
|
char *buf;
|
76
76
|
size_t max;
|
77
77
|
size_t len;
|
78
|
-
}
|
78
|
+
} packcr_char_array_t;
|
79
79
|
|
80
|
-
typedef struct
|
80
|
+
typedef struct packcr_range_tag {
|
81
81
|
size_t start;
|
82
82
|
size_t end;
|
83
83
|
<%- if @location -%>
|
84
|
-
|
85
|
-
|
84
|
+
packcr_location_t start_loc;
|
85
|
+
packcr_location_t end_loc;
|
86
86
|
<%- end -%>
|
87
|
-
}
|
87
|
+
} packcr_range_t;
|
88
88
|
|
89
|
-
typedef <%= value_def %>
|
89
|
+
typedef <%= value_def %>packcr_value_t;
|
90
90
|
|
91
|
-
typedef <%= auxil_def %>
|
91
|
+
typedef <%= auxil_def %>packcr_auxil_t;
|
92
92
|
|
93
|
-
<% if prefix != "
|
94
|
-
typedef <%= prefix%>_context_t
|
93
|
+
<% if prefix != "packcr" -%>
|
94
|
+
typedef <%= prefix%>_context_t packcr_context_t;
|
95
95
|
|
96
96
|
<% end -%>
|
97
|
-
typedef struct
|
98
|
-
|
97
|
+
typedef struct packcr_value_table_tag {
|
98
|
+
packcr_value_t *buf;
|
99
99
|
size_t max;
|
100
100
|
size_t len;
|
101
|
-
}
|
101
|
+
} packcr_value_table_t;
|
102
102
|
|
103
|
-
typedef struct
|
104
|
-
|
103
|
+
typedef struct packcr_value_refer_table_tag {
|
104
|
+
packcr_value_t **buf;
|
105
105
|
size_t max;
|
106
106
|
size_t len;
|
107
|
-
}
|
107
|
+
} packcr_value_refer_table_t;
|
108
108
|
|
109
|
-
typedef struct
|
110
|
-
|
109
|
+
typedef struct packcr_capture_tag {
|
110
|
+
packcr_range_t range;
|
111
111
|
char *string; /* mutable */
|
112
|
-
}
|
112
|
+
} packcr_capture_t;
|
113
113
|
|
114
|
-
typedef struct
|
115
|
-
|
114
|
+
typedef struct packcr_capture_table_tag {
|
115
|
+
packcr_capture_t *buf;
|
116
116
|
size_t max;
|
117
117
|
size_t len;
|
118
|
-
}
|
118
|
+
} packcr_capture_table_t;
|
119
119
|
|
120
|
-
typedef struct
|
121
|
-
const
|
120
|
+
typedef struct packcr_capture_const_table_tag {
|
121
|
+
const packcr_capture_t **buf;
|
122
122
|
size_t max;
|
123
123
|
size_t len;
|
124
|
-
}
|
125
|
-
|
126
|
-
typedef struct
|
127
|
-
typedef struct
|
128
|
-
|
129
|
-
typedef void (*
|
130
|
-
|
131
|
-
typedef enum
|
132
|
-
|
133
|
-
|
134
|
-
}
|
135
|
-
|
136
|
-
typedef struct
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
}
|
142
|
-
|
143
|
-
typedef struct
|
144
|
-
const
|
145
|
-
|
146
|
-
}
|
147
|
-
|
148
|
-
typedef union
|
149
|
-
|
150
|
-
|
151
|
-
}
|
152
|
-
|
153
|
-
struct
|
154
|
-
|
155
|
-
|
124
|
+
} packcr_capture_const_table_t;
|
125
|
+
|
126
|
+
typedef struct packcr_thunk_tag packcr_thunk_t;
|
127
|
+
typedef struct packcr_thunk_array_tag packcr_thunk_array_t;
|
128
|
+
|
129
|
+
typedef void (*packcr_action_t)(packcr_context_t *, packcr_thunk_t *, packcr_value_t *);
|
130
|
+
|
131
|
+
typedef enum packcr_thunk_type_tag {
|
132
|
+
PACKCR_THUNK_LEAF,
|
133
|
+
PACKCR_THUNK_NODE
|
134
|
+
} packcr_thunk_type_t;
|
135
|
+
|
136
|
+
typedef struct packcr_thunk_leaf_tag {
|
137
|
+
packcr_value_refer_table_t values;
|
138
|
+
packcr_capture_const_table_t capts;
|
139
|
+
packcr_capture_t capt0;
|
140
|
+
packcr_action_t action;
|
141
|
+
} packcr_thunk_leaf_t;
|
142
|
+
|
143
|
+
typedef struct packcr_thunk_node_tag {
|
144
|
+
const packcr_thunk_array_t *thunks; /* just a reference */
|
145
|
+
packcr_value_t *value; /* just a reference */
|
146
|
+
} packcr_thunk_node_t;
|
147
|
+
|
148
|
+
typedef union packcr_thunk_data_tag {
|
149
|
+
packcr_thunk_leaf_t leaf;
|
150
|
+
packcr_thunk_node_t node;
|
151
|
+
} packcr_thunk_data_t;
|
152
|
+
|
153
|
+
struct packcr_thunk_tag {
|
154
|
+
packcr_thunk_type_t type;
|
155
|
+
packcr_thunk_data_t data;
|
156
156
|
};
|
157
157
|
|
158
|
-
struct
|
159
|
-
|
158
|
+
struct packcr_thunk_array_tag {
|
159
|
+
packcr_thunk_t **buf;
|
160
160
|
size_t max;
|
161
161
|
size_t len;
|
162
162
|
};
|
163
163
|
|
164
|
-
typedef struct
|
165
|
-
|
166
|
-
|
167
|
-
|
164
|
+
typedef struct packcr_thunk_chunk_tag {
|
165
|
+
packcr_value_table_t values;
|
166
|
+
packcr_capture_table_t capts;
|
167
|
+
packcr_thunk_array_t thunks;
|
168
168
|
size_t pos; /* the starting position in the character buffer */
|
169
169
|
<%- if @location -%>
|
170
|
-
|
170
|
+
packcr_location_t pos_loc;
|
171
171
|
<%- end -%>
|
172
|
-
}
|
172
|
+
} packcr_thunk_chunk_t;
|
173
173
|
|
174
|
-
typedef struct
|
174
|
+
typedef struct packcr_lr_memo_tag packcr_lr_memo_t;
|
175
175
|
|
176
|
-
|
177
|
-
|
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 */
|
176
|
+
struct packcr_lr_memo_tag {
|
177
|
+
size_t offset;
|
192
178
|
<%- if @location -%>
|
193
|
-
|
179
|
+
packcr_location_t offset_loc;
|
194
180
|
<%- end -%>
|
195
|
-
|
181
|
+
packcr_lr_memo_t *hold;
|
182
|
+
packcr_thunk_chunk_t *chunk;
|
183
|
+
packcr_bool_t fail;
|
184
|
+
packcr_bool_t grow;
|
196
185
|
};
|
197
186
|
|
198
|
-
typedef
|
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
|
-
};
|
187
|
+
typedef struct packcr_rule_set_tag packcr_rule_set_t;
|
214
188
|
|
215
|
-
typedef
|
216
|
-
pcc_rule_t rule;
|
217
|
-
pcc_lr_answer_t *answer;
|
218
|
-
} pcc_lr_memo_t;
|
189
|
+
typedef packcr_thunk_chunk_t *(*packcr_rule_t)(packcr_context_t *, size_t<% if @location %>, packcr_location_t<% end %>, packcr_rule_set_t*);
|
219
190
|
|
220
|
-
typedef struct
|
221
|
-
|
191
|
+
typedef struct packcr_rule_set_tag {
|
192
|
+
packcr_rule_t *buf;
|
222
193
|
size_t max;
|
223
194
|
size_t len;
|
224
|
-
}
|
195
|
+
} packcr_rule_set_t;
|
225
196
|
|
226
|
-
typedef struct
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
pcc_lr_head_t *hold_h;
|
231
|
-
} pcc_lr_table_entry_t;
|
197
|
+
typedef struct packcr_lr_memo_map_entry_tag {
|
198
|
+
packcr_rule_t rule;
|
199
|
+
packcr_lr_memo_t *memo;
|
200
|
+
} packcr_lr_memo_map_entry_t;
|
232
201
|
|
233
|
-
typedef struct
|
234
|
-
|
202
|
+
typedef struct packcr_lr_memo_map_tag {
|
203
|
+
packcr_lr_memo_map_entry_t *buf;
|
235
204
|
size_t max;
|
236
205
|
size_t len;
|
237
|
-
|
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
|
-
};
|
206
|
+
} packcr_lr_memo_map_t;
|
245
207
|
|
246
|
-
typedef struct
|
247
|
-
|
208
|
+
typedef struct packcr_lr_table_tag {
|
209
|
+
packcr_lr_memo_map_t **buf;
|
248
210
|
size_t max;
|
249
211
|
size_t len;
|
250
|
-
|
212
|
+
size_t ofs;
|
213
|
+
} packcr_lr_table_t;
|
251
214
|
|
252
|
-
typedef struct
|
253
|
-
typedef struct
|
215
|
+
typedef struct packcr_memory_entry_tag packcr_memory_entry_t;
|
216
|
+
typedef struct packcr_memory_pool_tag packcr_memory_pool_t;
|
254
217
|
|
255
|
-
struct
|
256
|
-
|
218
|
+
struct packcr_memory_entry_tag {
|
219
|
+
packcr_memory_entry_t *next;
|
257
220
|
};
|
258
221
|
|
259
|
-
struct
|
260
|
-
|
222
|
+
struct packcr_memory_pool_tag {
|
223
|
+
packcr_memory_pool_t *next;
|
261
224
|
size_t allocated;
|
262
225
|
size_t unused;
|
263
226
|
};
|
264
227
|
|
265
|
-
typedef struct
|
266
|
-
|
267
|
-
|
228
|
+
typedef struct packcr_memory_recycler_tag {
|
229
|
+
packcr_memory_pool_t *pool_list;
|
230
|
+
packcr_memory_entry_t *entry_list;
|
268
231
|
size_t element_size;
|
269
|
-
}
|
232
|
+
} packcr_memory_recycler_t;
|
270
233
|
|
271
234
|
struct <%= prefix %>_context_tag {
|
272
|
-
size_t
|
273
|
-
size_t
|
235
|
+
size_t buffer_start_position; /* the position in the input of the first character currently buffered */
|
236
|
+
size_t position_offset; /* the current parsing position in the character buffer */
|
274
237
|
<%- if @location -%>
|
275
|
-
|
276
|
-
|
238
|
+
packcr_location_t buffer_start_position_loc;
|
239
|
+
packcr_location_t position_offset_loc;
|
277
240
|
<%- end -%>
|
278
241
|
size_t level;
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
pcc_memory_recycler_t lr_head_recycler;
|
286
|
-
pcc_memory_recycler_t lr_answer_recycler;
|
242
|
+
packcr_char_array_t buffer;
|
243
|
+
packcr_lr_table_t lrtable;
|
244
|
+
packcr_thunk_array_t thunks;
|
245
|
+
packcr_auxil_t auxil;
|
246
|
+
packcr_memory_recycler_t thunk_chunk_recycler;
|
247
|
+
packcr_memory_recycler_t lr_memo_recycler;
|
287
248
|
};
|
288
249
|
|
289
|
-
#ifndef
|
290
|
-
#define
|
250
|
+
#ifndef PACKCR_ERROR
|
251
|
+
#define PACKCR_ERROR(auxil) packcr_error()
|
291
252
|
MARK_FUNC_AS_USED
|
292
|
-
static void
|
253
|
+
static void packcr_error(void) {
|
293
254
|
fprintf(stderr, "Syntax error\n");
|
294
255
|
exit(1);
|
295
256
|
}
|
296
|
-
#endif /* !
|
257
|
+
#endif /* !PACKCR_ERROR */
|
297
258
|
|
298
|
-
#ifndef
|
299
|
-
#define
|
300
|
-
#endif /* !
|
259
|
+
#ifndef PACKCR_GETCHAR
|
260
|
+
#define PACKCR_GETCHAR(auxil) getchar()
|
261
|
+
#endif /* !PACKCR_GETCHAR */
|
301
262
|
|
302
|
-
#ifndef
|
303
|
-
#define
|
304
|
-
static void *
|
263
|
+
#ifndef PACKCR_MALLOC
|
264
|
+
#define PACKCR_MALLOC(auxil, size) packcr_malloc_e(size)
|
265
|
+
static void *packcr_malloc_e(size_t size) {
|
305
266
|
void *const p = malloc(size);
|
306
267
|
if (p == NULL) {
|
307
268
|
fprintf(stderr, "Out of memory\n");
|
@@ -309,11 +270,11 @@ static void *pcc_malloc_e(size_t size) {
|
|
309
270
|
}
|
310
271
|
return p;
|
311
272
|
}
|
312
|
-
#endif /* !
|
273
|
+
#endif /* !PACKCR_MALLOC */
|
313
274
|
|
314
|
-
#ifndef
|
315
|
-
#define
|
316
|
-
static void *
|
275
|
+
#ifndef PACKCR_REALLOC
|
276
|
+
#define PACKCR_REALLOC(auxil, ptr, size) packcr_realloc_e(ptr, size)
|
277
|
+
static void *packcr_realloc_e(void *ptr, size_t size) {
|
317
278
|
void *const p = realloc(ptr, size);
|
318
279
|
if (p == NULL) {
|
319
280
|
fprintf(stderr, "Out of memory\n");
|
@@ -321,267 +282,259 @@ static void *pcc_realloc_e(void *ptr, size_t size) {
|
|
321
282
|
}
|
322
283
|
return p;
|
323
284
|
}
|
324
|
-
#endif /* !
|
285
|
+
#endif /* !PACKCR_REALLOC */
|
325
286
|
|
326
|
-
#ifndef
|
327
|
-
#define
|
328
|
-
#endif /* !
|
287
|
+
#ifndef PACKCR_FREE
|
288
|
+
#define PACKCR_FREE(auxil, ptr) free(ptr)
|
289
|
+
#endif /* !PACKCR_FREE */
|
329
290
|
|
330
|
-
#ifndef
|
331
|
-
#define
|
332
|
-
#endif /* !
|
291
|
+
#ifndef PACKCR_DEBUG
|
292
|
+
#define PACKCR_DEBUG(auxil, event, rule, level, pos, buffer, length) ((void)0)
|
293
|
+
#endif /* !PACKCR_DEBUG */
|
333
294
|
|
334
|
-
static char *
|
295
|
+
static char *packcr_strndup_e(packcr_auxil_t auxil, const char *str, size_t len) {
|
335
296
|
const size_t m = strnlen(str, len);
|
336
|
-
char *const s = (char *)
|
297
|
+
char *const s = (char *)PACKCR_MALLOC(auxil, m + 1);
|
337
298
|
memcpy(s, str, m);
|
338
299
|
s[m] = '\0';
|
339
300
|
return s;
|
340
301
|
}
|
341
302
|
|
342
|
-
static void
|
303
|
+
static void packcr_char_array__init(packcr_auxil_t auxil, packcr_char_array_t *array) {
|
343
304
|
array->len = 0;
|
344
305
|
array->max = 0;
|
345
306
|
array->buf = NULL;
|
346
307
|
}
|
347
308
|
|
348
|
-
static void
|
309
|
+
static void packcr_char_array__add(packcr_auxil_t auxil, packcr_char_array_t *array, char ch) {
|
349
310
|
if (array->max <= array->len) {
|
350
311
|
const size_t n = array->len + 1;
|
351
312
|
size_t m = array->max;
|
352
|
-
if (m == 0) m =
|
313
|
+
if (m == 0) m = PACKCR_BUFFER_MIN_SIZE;
|
353
314
|
while (m < n && m != 0) m <<= 1;
|
354
315
|
if (m == 0) m = n;
|
355
|
-
array->buf = (char *)
|
316
|
+
array->buf = (char *)PACKCR_REALLOC(auxil, array->buf, m);
|
356
317
|
array->max = m;
|
357
318
|
}
|
358
319
|
array->buf[array->len++] = ch;
|
359
320
|
}
|
360
321
|
|
361
|
-
static void
|
362
|
-
|
322
|
+
static void packcr_char_array__term(packcr_auxil_t auxil, packcr_char_array_t *array) {
|
323
|
+
PACKCR_FREE(auxil, array->buf);
|
363
324
|
}
|
364
325
|
|
365
|
-
static void
|
326
|
+
static void packcr_value_table__init(packcr_auxil_t auxil, packcr_value_table_t *table) {
|
366
327
|
table->len = 0;
|
367
328
|
table->max = 0;
|
368
329
|
table->buf = NULL;
|
369
330
|
}
|
370
331
|
|
371
332
|
MARK_FUNC_AS_USED
|
372
|
-
static void
|
333
|
+
static void packcr_value_table__resize(packcr_auxil_t auxil, packcr_value_table_t *table, size_t len) {
|
373
334
|
if (table->max < len) {
|
374
335
|
size_t m = table->max;
|
375
|
-
if (m == 0) m =
|
336
|
+
if (m == 0) m = PACKCR_ARRAY_MIN_SIZE;
|
376
337
|
while (m < len && m != 0) m <<= 1;
|
377
338
|
if (m == 0) m = len;
|
378
|
-
table->buf = (
|
339
|
+
table->buf = (packcr_value_t *)PACKCR_REALLOC(auxil, table->buf, sizeof(packcr_value_t) * m);
|
379
340
|
table->max = m;
|
380
341
|
}
|
381
342
|
table->len = len;
|
382
343
|
}
|
383
344
|
|
384
345
|
MARK_FUNC_AS_USED
|
385
|
-
static void
|
386
|
-
memset(table->buf, 0, sizeof(
|
346
|
+
static void packcr_value_table__clear(packcr_auxil_t auxil, packcr_value_table_t *table) {
|
347
|
+
memset(table->buf, 0, sizeof(packcr_value_t) * table->len);
|
387
348
|
}
|
388
349
|
|
389
|
-
static void
|
390
|
-
|
350
|
+
static void packcr_value_table__term(packcr_auxil_t auxil, packcr_value_table_t *table) {
|
351
|
+
PACKCR_FREE(auxil, table->buf);
|
391
352
|
}
|
392
353
|
|
393
|
-
static void
|
354
|
+
static void packcr_value_refer_table__init(packcr_auxil_t auxil, packcr_value_refer_table_t *table) {
|
394
355
|
table->len = 0;
|
395
356
|
table->max = 0;
|
396
357
|
table->buf = NULL;
|
397
358
|
}
|
398
359
|
|
399
|
-
static void
|
360
|
+
static void packcr_value_refer_table__resize(packcr_auxil_t auxil, packcr_value_refer_table_t *table, size_t len) {
|
400
361
|
size_t i;
|
401
362
|
if (table->max < len) {
|
402
363
|
size_t m = table->max;
|
403
|
-
if (m == 0) m =
|
364
|
+
if (m == 0) m = PACKCR_ARRAY_MIN_SIZE;
|
404
365
|
while (m < len && m != 0) m <<= 1;
|
405
366
|
if (m == 0) m = len;
|
406
|
-
table->buf = (
|
367
|
+
table->buf = (packcr_value_t **)PACKCR_REALLOC(auxil, table->buf, sizeof(packcr_value_t *) * m);
|
407
368
|
table->max = m;
|
408
369
|
}
|
409
370
|
for (i = table->len; i < len; i++) table->buf[i] = NULL;
|
410
371
|
table->len = len;
|
411
372
|
}
|
412
373
|
|
413
|
-
static void
|
414
|
-
|
374
|
+
static void packcr_value_refer_table__term(packcr_auxil_t auxil, packcr_value_refer_table_t *table) {
|
375
|
+
PACKCR_FREE(auxil, table->buf);
|
415
376
|
}
|
416
377
|
|
417
|
-
static void
|
378
|
+
static void packcr_capture_table__init(packcr_auxil_t auxil, packcr_capture_table_t *table) {
|
418
379
|
table->len = 0;
|
419
380
|
table->max = 0;
|
420
381
|
table->buf = NULL;
|
421
382
|
}
|
422
383
|
|
423
384
|
MARK_FUNC_AS_USED
|
424
|
-
static void
|
385
|
+
static void packcr_capture_table__resize(packcr_auxil_t auxil, packcr_capture_table_t *table, size_t len) {
|
425
386
|
size_t i;
|
426
|
-
for (i = len; i < table->len; i++)
|
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 -%>
|
387
|
+
for (i = len; i < table->len; i++) PACKCR_FREE(auxil, table->buf[i].string);
|
433
388
|
if (table->max < len) {
|
434
389
|
size_t m = table->max;
|
435
|
-
if (m == 0) m =
|
390
|
+
if (m == 0) m = PACKCR_ARRAY_MIN_SIZE;
|
436
391
|
while (m < len && m != 0) m <<= 1;
|
437
392
|
if (m == 0) m = len;
|
438
|
-
table->buf = (
|
393
|
+
table->buf = (packcr_capture_t *)PACKCR_REALLOC(auxil, table->buf, sizeof(packcr_capture_t) * m);
|
439
394
|
table->max = m;
|
440
395
|
}
|
441
396
|
for (i = table->len; i < len; i++) {
|
442
397
|
table->buf[i].range.start = 0;
|
443
398
|
table->buf[i].range.end = 0;
|
444
399
|
<%- if @location -%>
|
445
|
-
table->buf[i].range.
|
446
|
-
table->buf[i].range.
|
447
|
-
pcc_location_init(table->buf[i].range.start_loc_ptr);
|
448
|
-
pcc_location_init(table->buf[i].range.end_loc_ptr);
|
400
|
+
packcr_location_init(&table->buf[i].range.start_loc);
|
401
|
+
packcr_location_init(&table->buf[i].range.end_loc);
|
449
402
|
<%- end -%>
|
450
403
|
table->buf[i].string = NULL;
|
451
404
|
}
|
452
405
|
table->len = len;
|
453
406
|
}
|
454
407
|
|
455
|
-
static void
|
408
|
+
static void packcr_capture_table__term(packcr_auxil_t auxil, packcr_capture_table_t *table) {
|
456
409
|
while (table->len > 0) {
|
457
410
|
table->len--;
|
458
|
-
|
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 -%>
|
411
|
+
PACKCR_FREE(auxil, table->buf[table->len].string);
|
463
412
|
}
|
464
|
-
|
413
|
+
PACKCR_FREE(auxil, table->buf);
|
465
414
|
}
|
466
415
|
|
467
|
-
static void
|
416
|
+
static void packcr_capture_const_table__init(packcr_auxil_t auxil, packcr_capture_const_table_t *table) {
|
468
417
|
table->len = 0;
|
469
418
|
table->max = 0;
|
470
419
|
table->buf = NULL;
|
471
420
|
}
|
472
421
|
|
473
|
-
static void
|
422
|
+
static void packcr_capture_const_table__resize(packcr_auxil_t auxil, packcr_capture_const_table_t *table, size_t len) {
|
474
423
|
size_t i;
|
475
424
|
if (table->max < len) {
|
476
425
|
size_t m = table->max;
|
477
|
-
if (m == 0) m =
|
426
|
+
if (m == 0) m = PACKCR_ARRAY_MIN_SIZE;
|
478
427
|
while (m < len && m != 0) m <<= 1;
|
479
428
|
if (m == 0) m = len;
|
480
|
-
table->buf = (const
|
429
|
+
table->buf = (const packcr_capture_t **)PACKCR_REALLOC(auxil, (packcr_capture_t **)table->buf, sizeof(const packcr_capture_t *) * m);
|
481
430
|
table->max = m;
|
482
431
|
}
|
483
432
|
for (i = table->len; i < len; i++) table->buf[i] = NULL;
|
484
433
|
table->len = len;
|
485
434
|
}
|
486
435
|
|
487
|
-
static void
|
488
|
-
|
436
|
+
static void packcr_capture_const_table__term(packcr_auxil_t auxil, packcr_capture_const_table_t *table) {
|
437
|
+
PACKCR_FREE(auxil, (void *)table->buf);
|
489
438
|
}
|
490
439
|
|
491
440
|
MARK_FUNC_AS_USED
|
492
|
-
static
|
493
|
-
|
494
|
-
thunk->type =
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
441
|
+
static packcr_thunk_t *packcr_thunk__create_leaf(packcr_auxil_t auxil, packcr_action_t action, size_t valuec, size_t captc) {
|
442
|
+
packcr_thunk_t *const thunk = (packcr_thunk_t *)PACKCR_MALLOC(auxil, sizeof(packcr_thunk_t));
|
443
|
+
thunk->type = PACKCR_THUNK_LEAF;
|
444
|
+
packcr_value_refer_table__init(auxil, &thunk->data.leaf.values);
|
445
|
+
packcr_value_refer_table__resize(auxil, &thunk->data.leaf.values, valuec);
|
446
|
+
packcr_capture_const_table__init(auxil, &thunk->data.leaf.capts);
|
447
|
+
packcr_capture_const_table__resize(auxil, &thunk->data.leaf.capts, captc);
|
499
448
|
thunk->data.leaf.capt0.range.start = 0;
|
500
449
|
thunk->data.leaf.capt0.range.end = 0;
|
450
|
+
<%- if @location -%>
|
451
|
+
packcr_location_init(&thunk->data.leaf.capt0.range.start_loc);
|
452
|
+
packcr_location_init(&thunk->data.leaf.capt0.range.end_loc);
|
453
|
+
<%- end -%>
|
501
454
|
thunk->data.leaf.capt0.string = NULL;
|
502
455
|
thunk->data.leaf.action = action;
|
503
456
|
return thunk;
|
504
457
|
}
|
505
458
|
|
506
|
-
static
|
507
|
-
|
508
|
-
thunk->type =
|
459
|
+
static packcr_thunk_t *packcr_thunk__create_node(packcr_auxil_t auxil, const packcr_thunk_array_t *thunks, packcr_value_t *value) {
|
460
|
+
packcr_thunk_t *const thunk = (packcr_thunk_t *)PACKCR_MALLOC(auxil, sizeof(packcr_thunk_t));
|
461
|
+
thunk->type = PACKCR_THUNK_NODE;
|
509
462
|
thunk->data.node.thunks = thunks;
|
510
463
|
thunk->data.node.value = value;
|
511
464
|
return thunk;
|
512
465
|
}
|
513
466
|
|
514
|
-
static void
|
467
|
+
static void packcr_thunk__destroy(packcr_auxil_t auxil, packcr_thunk_t *thunk) {
|
515
468
|
if (thunk == NULL) return;
|
516
469
|
switch (thunk->type) {
|
517
|
-
case
|
518
|
-
|
519
|
-
|
520
|
-
|
470
|
+
case PACKCR_THUNK_LEAF:
|
471
|
+
PACKCR_FREE(auxil, thunk->data.leaf.capt0.string);
|
472
|
+
packcr_capture_const_table__term(auxil, &thunk->data.leaf.capts);
|
473
|
+
packcr_value_refer_table__term(auxil, &thunk->data.leaf.values);
|
521
474
|
break;
|
522
|
-
case
|
475
|
+
case PACKCR_THUNK_NODE:
|
523
476
|
break;
|
524
477
|
default: /* unknown */
|
525
478
|
break;
|
526
479
|
}
|
527
|
-
|
480
|
+
PACKCR_FREE(auxil, thunk);
|
528
481
|
}
|
529
482
|
|
530
|
-
static void
|
483
|
+
static void packcr_thunk_array__init(packcr_auxil_t auxil, packcr_thunk_array_t *array) {
|
531
484
|
array->len = 0;
|
532
485
|
array->max = 0;
|
533
486
|
array->buf = NULL;
|
534
487
|
}
|
535
488
|
|
536
|
-
static void
|
489
|
+
static void packcr_thunk_array__add(packcr_auxil_t auxil, packcr_thunk_array_t *array, packcr_thunk_t *thunk) {
|
537
490
|
if (array->max <= array->len) {
|
538
491
|
const size_t n = array->len + 1;
|
539
492
|
size_t m = array->max;
|
540
|
-
if (m == 0) m =
|
493
|
+
if (m == 0) m = PACKCR_ARRAY_MIN_SIZE;
|
541
494
|
while (m < n && m != 0) m <<= 1;
|
542
495
|
if (m == 0) m = n;
|
543
|
-
array->buf = (
|
496
|
+
array->buf = (packcr_thunk_t **)PACKCR_REALLOC(auxil, array->buf, sizeof(packcr_thunk_t *) * m);
|
544
497
|
array->max = m;
|
545
498
|
}
|
546
499
|
array->buf[array->len++] = thunk;
|
547
500
|
}
|
548
501
|
|
549
|
-
static void
|
502
|
+
static void packcr_thunk_array__revert(packcr_auxil_t auxil, packcr_thunk_array_t *array, size_t len) {
|
550
503
|
while (array->len > len) {
|
551
504
|
array->len--;
|
552
|
-
|
505
|
+
packcr_thunk__destroy(auxil, array->buf[array->len]);
|
553
506
|
}
|
554
507
|
}
|
555
508
|
|
556
|
-
static void
|
509
|
+
static void packcr_thunk_array__term(packcr_auxil_t auxil, packcr_thunk_array_t *array) {
|
557
510
|
while (array->len > 0) {
|
558
511
|
array->len--;
|
559
|
-
|
512
|
+
packcr_thunk__destroy(auxil, array->buf[array->len]);
|
560
513
|
}
|
561
|
-
|
514
|
+
PACKCR_FREE(auxil, array->buf);
|
562
515
|
}
|
563
516
|
|
564
|
-
static void
|
517
|
+
static void packcr_memory_recycler__init(packcr_auxil_t auxil, packcr_memory_recycler_t *recycler, size_t element_size) {
|
565
518
|
recycler->pool_list = NULL;
|
566
519
|
recycler->entry_list = NULL;
|
567
520
|
recycler->element_size = element_size;
|
568
521
|
}
|
569
522
|
|
570
|
-
static void *
|
523
|
+
static void *packcr_memory_recycler__supply(packcr_auxil_t auxil, packcr_memory_recycler_t *recycler) {
|
571
524
|
if (recycler->entry_list) {
|
572
|
-
|
525
|
+
packcr_memory_entry_t *const tmp = recycler->entry_list;
|
573
526
|
recycler->entry_list = tmp->next;
|
574
527
|
return tmp;
|
575
528
|
}
|
576
529
|
if (!recycler->pool_list || recycler->pool_list->unused == 0) {
|
577
|
-
size_t size =
|
530
|
+
size_t size = PACKCR_POOL_MIN_SIZE;
|
578
531
|
if (recycler->pool_list) {
|
579
532
|
size = recycler->pool_list->allocated << 1;
|
580
533
|
if (size == 0) size = recycler->pool_list->allocated;
|
581
534
|
}
|
582
535
|
{
|
583
|
-
|
584
|
-
auxil, sizeof(
|
536
|
+
packcr_memory_pool_t *const pool = (packcr_memory_pool_t *)PACKCR_MALLOC(
|
537
|
+
auxil, sizeof(packcr_memory_pool_t) + recycler->element_size * size
|
585
538
|
);
|
586
539
|
pool->allocated = size;
|
587
540
|
pool->unused = size;
|
@@ -590,464 +543,324 @@ static void *pcc_memory_recycler__supply(pcc_auxil_t auxil, pcc_memory_recycler_
|
|
590
543
|
}
|
591
544
|
}
|
592
545
|
recycler->pool_list->unused--;
|
593
|
-
return (char *)recycler->pool_list + sizeof(
|
546
|
+
return (char *)recycler->pool_list + sizeof(packcr_memory_pool_t) + recycler->element_size * recycler->pool_list->unused;
|
594
547
|
}
|
595
548
|
|
596
|
-
static void
|
597
|
-
|
549
|
+
static void packcr_memory_recycler__recycle(packcr_auxil_t auxil, packcr_memory_recycler_t *recycler, void *ptr) {
|
550
|
+
packcr_memory_entry_t *const tmp = (packcr_memory_entry_t *)ptr;
|
598
551
|
tmp->next = recycler->entry_list;
|
599
552
|
recycler->entry_list = tmp;
|
600
553
|
}
|
601
554
|
|
602
|
-
static void
|
555
|
+
static void packcr_memory_recycler__term(packcr_auxil_t auxil, packcr_memory_recycler_t *recycler) {
|
603
556
|
while (recycler->pool_list) {
|
604
|
-
|
557
|
+
packcr_memory_pool_t *const tmp = recycler->pool_list;
|
605
558
|
recycler->pool_list = tmp->next;
|
606
|
-
|
559
|
+
PACKCR_FREE(auxil, tmp);
|
607
560
|
}
|
608
561
|
}
|
609
562
|
|
610
563
|
MARK_FUNC_AS_USED
|
611
|
-
static
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
564
|
+
static packcr_thunk_chunk_t *packcr_thunk_chunk__create(packcr_context_t *ctx) {
|
565
|
+
packcr_thunk_chunk_t *const chunk = (packcr_thunk_chunk_t *)packcr_memory_recycler__supply(ctx->auxil, &ctx->thunk_chunk_recycler);
|
566
|
+
packcr_value_table__init(ctx->auxil, &chunk->values);
|
567
|
+
packcr_capture_table__init(ctx->auxil, &chunk->capts);
|
568
|
+
packcr_thunk_array__init(ctx->auxil, &chunk->thunks);
|
616
569
|
chunk->pos = 0;
|
617
570
|
return chunk;
|
618
571
|
}
|
619
572
|
|
620
|
-
static void
|
573
|
+
static void packcr_thunk_chunk__destroy(packcr_context_t *ctx, packcr_thunk_chunk_t *chunk) {
|
621
574
|
if (chunk == NULL) return;
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
575
|
+
packcr_thunk_array__term(ctx->auxil, &chunk->thunks);
|
576
|
+
packcr_capture_table__term(ctx->auxil, &chunk->capts);
|
577
|
+
packcr_value_table__term(ctx->auxil, &chunk->values);
|
578
|
+
packcr_memory_recycler__recycle(ctx->auxil, &ctx->thunk_chunk_recycler, chunk);
|
626
579
|
}
|
627
580
|
|
628
|
-
static void
|
581
|
+
static void packcr_rule_set__init(packcr_auxil_t auxil, packcr_rule_set_t *set) {
|
629
582
|
set->len = 0;
|
630
583
|
set->max = 0;
|
631
584
|
set->buf = NULL;
|
632
585
|
}
|
633
586
|
|
634
|
-
static size_t
|
587
|
+
static size_t packcr_rule_set__index(packcr_auxil_t auxil, const packcr_rule_set_t *set, packcr_rule_t rule) {
|
635
588
|
size_t i;
|
636
589
|
for (i = 0; i < set->len; i++) {
|
637
590
|
if (set->buf[i] == rule) return i;
|
638
591
|
}
|
639
|
-
return
|
592
|
+
return PACKCR_VOID_VALUE;
|
640
593
|
}
|
641
594
|
|
642
|
-
static
|
643
|
-
const size_t i =
|
644
|
-
if (i !=
|
595
|
+
static packcr_bool_t packcr_rule_set__add(packcr_auxil_t auxil, packcr_rule_set_t *set, packcr_rule_t rule) {
|
596
|
+
const size_t i = packcr_rule_set__index(auxil, set, rule);
|
597
|
+
if (i != PACKCR_VOID_VALUE) return PACKCR_FALSE;
|
645
598
|
if (set->max <= set->len) {
|
646
599
|
const size_t n = set->len + 1;
|
647
600
|
size_t m = set->max;
|
648
|
-
if (m == 0) m =
|
601
|
+
if (m == 0) m = PACKCR_ARRAY_MIN_SIZE;
|
649
602
|
while (m < n && m != 0) m <<= 1;
|
650
603
|
if (m == 0) m = n;
|
651
|
-
set->buf = (
|
604
|
+
set->buf = (packcr_rule_t *)PACKCR_REALLOC(auxil, set->buf, sizeof(packcr_rule_t) * m);
|
652
605
|
set->max = m;
|
653
606
|
}
|
654
607
|
set->buf[set->len++] = rule;
|
655
|
-
return
|
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;
|
608
|
+
return PACKCR_TRUE;
|
663
609
|
}
|
664
610
|
|
665
|
-
static void
|
666
|
-
set->
|
611
|
+
static void packcr_rule_set__term(packcr_auxil_t auxil, packcr_rule_set_t *set) {
|
612
|
+
PACKCR_FREE(auxil, set->buf);
|
667
613
|
}
|
668
614
|
|
669
|
-
static
|
670
|
-
|
671
|
-
|
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;
|
615
|
+
static packcr_lr_memo_t *packcr_lr_memo__create(packcr_context_t *ctx, size_t offset<% if @location %>, packcr_location_t offset_loc<% end %>) {
|
616
|
+
packcr_lr_memo_t *memo = (packcr_lr_memo_t *)packcr_memory_recycler__supply(ctx->auxil, &ctx->lr_memo_recycler);
|
617
|
+
memo->offset = offset;
|
704
618
|
<%- if @location -%>
|
705
|
-
|
619
|
+
memo->offset_loc = offset_loc;
|
706
620
|
<%- end -%>
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
621
|
+
memo->chunk = NULL;
|
622
|
+
memo->fail = PACKCR_TRUE;
|
623
|
+
memo->grow = PACKCR_FALSE;
|
624
|
+
memo->hold = NULL;
|
625
|
+
return memo;
|
626
|
+
}
|
627
|
+
|
628
|
+
static void packcr_lr_memo__set_chunk(packcr_context_t *ctx, packcr_lr_memo_t *memo, packcr_thunk_chunk_t *chunk) {
|
629
|
+
if (memo->chunk) {
|
630
|
+
packcr_lr_memo_t *const new_hold_memo = packcr_lr_memo__create(ctx, memo->offset<% if @location %>, memo->offset_loc<% end %>);
|
631
|
+
new_hold_memo->chunk = memo->chunk;
|
632
|
+
new_hold_memo->hold = memo->hold;
|
633
|
+
memo->hold = new_hold_memo;
|
718
634
|
}
|
719
|
-
|
635
|
+
memo->chunk = chunk;
|
636
|
+
memo->fail = PACKCR_FALSE;
|
720
637
|
}
|
721
638
|
|
722
|
-
static void
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
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;
|
639
|
+
static void packcr_lr_memo__destroy(packcr_context_t *ctx, packcr_lr_memo_t *memo) {
|
640
|
+
while (memo != NULL) {
|
641
|
+
packcr_lr_memo_t *const hold_memo = memo->hold;
|
642
|
+
packcr_thunk_chunk__destroy(ctx, memo->chunk);
|
643
|
+
packcr_memory_recycler__recycle(ctx->auxil, &ctx->lr_memo_recycler, memo);
|
644
|
+
memo = hold_memo;
|
755
645
|
}
|
756
646
|
}
|
757
647
|
|
758
|
-
static void
|
648
|
+
static void packcr_lr_memo_map__init(packcr_auxil_t auxil, packcr_lr_memo_map_t *map) {
|
759
649
|
map->len = 0;
|
760
650
|
map->max = 0;
|
761
651
|
map->buf = NULL;
|
762
652
|
}
|
763
653
|
|
764
|
-
static size_t
|
654
|
+
static size_t packcr_lr_memo_map__index(packcr_context_t *ctx, packcr_lr_memo_map_t *map, packcr_rule_t rule) {
|
765
655
|
size_t i;
|
766
656
|
for (i = 0; i < map->len; i++) {
|
767
657
|
if (map->buf[i].rule == rule) return i;
|
768
658
|
}
|
769
|
-
return
|
659
|
+
return PACKCR_VOID_VALUE;
|
770
660
|
}
|
771
661
|
|
772
|
-
static void
|
773
|
-
const size_t i =
|
774
|
-
if (i !=
|
775
|
-
|
776
|
-
map->buf[i].
|
662
|
+
static void packcr_lr_memo_map__put(packcr_context_t *ctx, packcr_lr_memo_map_t *map, packcr_rule_t rule, packcr_lr_memo_t *memo) {
|
663
|
+
const size_t i = packcr_lr_memo_map__index(ctx, map, rule);
|
664
|
+
if (i != PACKCR_VOID_VALUE) {
|
665
|
+
packcr_lr_memo__destroy(ctx, map->buf[i].memo);
|
666
|
+
map->buf[i].memo = memo;
|
777
667
|
}
|
778
668
|
else {
|
779
669
|
if (map->max <= map->len) {
|
780
670
|
const size_t n = map->len + 1;
|
781
671
|
size_t m = map->max;
|
782
|
-
if (m == 0) m =
|
672
|
+
if (m == 0) m = PACKCR_ARRAY_MIN_SIZE;
|
783
673
|
while (m < n && m != 0) m <<= 1;
|
784
674
|
if (m == 0) m = n;
|
785
|
-
map->buf = (
|
675
|
+
map->buf = (packcr_lr_memo_map_entry_t *)PACKCR_REALLOC(ctx->auxil, map->buf, sizeof(packcr_lr_memo_map_entry_t) * m);
|
786
676
|
map->max = m;
|
787
677
|
}
|
788
678
|
map->buf[map->len].rule = rule;
|
789
|
-
map->buf[map->len].
|
679
|
+
map->buf[map->len].memo = memo;
|
790
680
|
map->len++;
|
791
681
|
}
|
792
682
|
}
|
793
683
|
|
794
|
-
static
|
795
|
-
const size_t i =
|
796
|
-
return (i !=
|
684
|
+
static packcr_lr_memo_t *packcr_lr_memo_map__get(packcr_context_t *ctx, packcr_lr_memo_map_t *map, packcr_rule_t rule) {
|
685
|
+
const size_t i = packcr_lr_memo_map__index(ctx, map, rule);
|
686
|
+
return (i != PACKCR_VOID_VALUE) ? map->buf[i].memo : NULL;
|
797
687
|
}
|
798
688
|
|
799
|
-
static void
|
689
|
+
static void packcr_lr_memo_map__term(packcr_context_t *ctx, packcr_lr_memo_map_t *map) {
|
800
690
|
while (map->len > 0) {
|
801
691
|
map->len--;
|
802
|
-
|
692
|
+
packcr_lr_memo__destroy(ctx, map->buf[map->len].memo);
|
803
693
|
}
|
804
|
-
|
694
|
+
PACKCR_FREE(ctx->auxil, map->buf);
|
805
695
|
}
|
806
696
|
|
807
|
-
static
|
808
|
-
|
809
|
-
|
810
|
-
|
811
|
-
entry->hold_a = NULL;
|
812
|
-
entry->hold_h = NULL;
|
813
|
-
return entry;
|
697
|
+
static packcr_lr_memo_map_t *packcr_lr_memo_map__create(packcr_context_t *ctx) {
|
698
|
+
packcr_lr_memo_map_t *const memo = (packcr_lr_memo_map_t *)PACKCR_MALLOC(ctx->auxil, sizeof(packcr_lr_memo_map_t));
|
699
|
+
packcr_lr_memo_map__init(ctx->auxil, memo);
|
700
|
+
return memo;
|
814
701
|
}
|
815
702
|
|
816
|
-
static void
|
817
|
-
if (
|
818
|
-
|
819
|
-
|
820
|
-
pcc_lr_memo_map__term(ctx, &entry->memos);
|
821
|
-
PCC_FREE(ctx->auxil, entry);
|
703
|
+
static void packcr_lr_memo_map__destroy(packcr_context_t *ctx, packcr_lr_memo_map_t *memo) {
|
704
|
+
if (memo == NULL) return;
|
705
|
+
packcr_lr_memo_map__term(ctx, memo);
|
706
|
+
PACKCR_FREE(ctx->auxil, memo);
|
822
707
|
}
|
823
708
|
|
824
|
-
static void
|
709
|
+
static void packcr_lr_table__init(packcr_auxil_t auxil, packcr_lr_table_t *table) {
|
825
710
|
table->ofs = 0;
|
826
711
|
table->len = 0;
|
827
712
|
table->max = 0;
|
828
713
|
table->buf = NULL;
|
829
714
|
}
|
830
715
|
|
831
|
-
static void
|
716
|
+
static void packcr_lr_table__resize(packcr_context_t *ctx, packcr_lr_table_t *table, size_t len) {
|
832
717
|
size_t i;
|
833
|
-
for (i = len; i < table->len; i++)
|
718
|
+
for (i = len; i < table->len; i++) packcr_lr_memo_map__destroy(ctx, table->buf[i]);
|
834
719
|
if (table->max < len) {
|
835
720
|
size_t m = table->max;
|
836
|
-
if (m == 0) m =
|
721
|
+
if (m == 0) m = PACKCR_ARRAY_MIN_SIZE;
|
837
722
|
while (m < len && m != 0) m <<= 1;
|
838
723
|
if (m == 0) m = len;
|
839
|
-
table->buf = (
|
724
|
+
table->buf = (packcr_lr_memo_map_t **)PACKCR_REALLOC(ctx->auxil, table->buf, sizeof(packcr_lr_memo_map_t *) * m);
|
840
725
|
table->max = m;
|
841
726
|
}
|
842
727
|
for (i = table->len; i < len; i++) table->buf[i] = NULL;
|
843
728
|
table->len = len;
|
844
729
|
}
|
845
730
|
|
846
|
-
static void
|
731
|
+
static void packcr_lr_table__set_memo(packcr_context_t *ctx, packcr_lr_table_t *table, size_t index, packcr_rule_t rule, packcr_lr_memo_t *memo) {
|
847
732
|
index += table->ofs;
|
848
|
-
if (index >= table->len)
|
849
|
-
if (table->buf[index] == NULL) table->buf[index] =
|
850
|
-
table->buf[index]
|
733
|
+
if (index >= table->len) packcr_lr_table__resize(ctx, table, index + 1);
|
734
|
+
if (table->buf[index] == NULL) table->buf[index] = packcr_lr_memo_map__create(ctx);
|
735
|
+
packcr_lr_memo_map__put(ctx, table->buf[index], rule, memo);
|
851
736
|
}
|
852
737
|
|
853
|
-
static
|
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) {
|
738
|
+
static packcr_lr_memo_t *packcr_lr_table__get_memo(packcr_context_t *ctx, packcr_lr_table_t *table, size_t index, packcr_rule_t rule) {
|
883
739
|
index += table->ofs;
|
884
740
|
if (index >= table->len || table->buf[index] == NULL) return NULL;
|
885
|
-
return
|
741
|
+
return packcr_lr_memo_map__get(ctx, table->buf[index], rule);
|
886
742
|
}
|
887
743
|
|
888
|
-
static void
|
744
|
+
static void packcr_lr_table__shift(packcr_context_t *ctx, packcr_lr_table_t *table, size_t count) {
|
889
745
|
size_t i;
|
890
746
|
if (count > table->len - table->ofs) count = table->len - table->ofs;
|
891
|
-
for (i = 0; i < count; i++)
|
747
|
+
for (i = 0; i < count; i++) packcr_lr_memo_map__destroy(ctx, table->buf[table->ofs++]);
|
892
748
|
if (table->ofs > (table->max >> 1)) {
|
893
|
-
memmove(table->buf, table->buf + table->ofs, sizeof(
|
749
|
+
memmove(table->buf, table->buf + table->ofs, sizeof(packcr_lr_memo_map_t *) * (table->len - table->ofs));
|
894
750
|
table->len -= table->ofs;
|
895
751
|
table->ofs = 0;
|
896
752
|
}
|
897
753
|
}
|
898
754
|
|
899
|
-
static void
|
755
|
+
static void packcr_lr_table__term(packcr_context_t *ctx, packcr_lr_table_t *table) {
|
900
756
|
while (table->len > table->ofs) {
|
901
757
|
table->len--;
|
902
|
-
|
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;
|
758
|
+
packcr_lr_memo_map__destroy(ctx, table->buf[table->len]);
|
934
759
|
}
|
935
|
-
|
760
|
+
PACKCR_FREE(ctx->auxil, table->buf);
|
936
761
|
}
|
937
762
|
|
938
|
-
static
|
939
|
-
|
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;
|
763
|
+
static packcr_context_t *packcr_context__create(packcr_auxil_t auxil) {
|
764
|
+
packcr_context_t *const ctx = (packcr_context_t *)PACKCR_MALLOC(auxil, sizeof(packcr_context_t));
|
765
|
+
ctx->buffer_start_position = 0;
|
766
|
+
ctx->position_offset = 0;
|
950
767
|
<%- if @location -%>
|
951
|
-
|
952
|
-
|
768
|
+
packcr_location_init(&ctx->buffer_start_position_loc);
|
769
|
+
packcr_location_init(&ctx->position_offset_loc);
|
953
770
|
<%- end -%>
|
954
771
|
ctx->level = 0;
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
|
959
|
-
|
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));
|
772
|
+
packcr_char_array__init(auxil, &ctx->buffer);
|
773
|
+
packcr_lr_table__init(auxil, &ctx->lrtable);
|
774
|
+
packcr_thunk_array__init(auxil, &ctx->thunks);
|
775
|
+
packcr_memory_recycler__init(auxil, &ctx->thunk_chunk_recycler, sizeof(packcr_thunk_chunk_t));
|
776
|
+
packcr_memory_recycler__init(auxil, &ctx->lr_memo_recycler, sizeof(packcr_lr_memo_t));
|
962
777
|
ctx->auxil = auxil;
|
963
778
|
return ctx;
|
964
779
|
}
|
965
780
|
|
966
|
-
static void
|
781
|
+
static void packcr_context__destroy(packcr_context_t *ctx) {
|
967
782
|
if (ctx == NULL) return;
|
968
|
-
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
973
|
-
|
974
|
-
|
975
|
-
|
976
|
-
|
977
|
-
|
978
|
-
|
979
|
-
|
980
|
-
while (ctx->buffer.len < ctx->cur + num) {
|
981
|
-
const int c = PCC_GETCHAR(ctx->auxil);
|
783
|
+
packcr_thunk_array__term(ctx->auxil, &ctx->thunks);
|
784
|
+
packcr_lr_table__term(ctx, &ctx->lrtable);
|
785
|
+
packcr_char_array__term(ctx->auxil, &ctx->buffer);
|
786
|
+
packcr_memory_recycler__term(ctx->auxil, &ctx->thunk_chunk_recycler);
|
787
|
+
packcr_memory_recycler__term(ctx->auxil, &ctx->lr_memo_recycler);
|
788
|
+
PACKCR_FREE(ctx->auxil, ctx);
|
789
|
+
}
|
790
|
+
|
791
|
+
static size_t packcr_refill_buffer(packcr_context_t *ctx, size_t num) {
|
792
|
+
if (ctx->buffer.len >= ctx->position_offset + num) return ctx->buffer.len - ctx->position_offset;
|
793
|
+
while (ctx->buffer.len < ctx->position_offset + num) {
|
794
|
+
const int c = PACKCR_GETCHAR(ctx->auxil);
|
982
795
|
if (c < 0) break;
|
983
|
-
|
796
|
+
packcr_char_array__add(ctx->auxil, &ctx->buffer, (char)c);
|
984
797
|
}
|
985
|
-
return ctx->buffer.len - ctx->
|
798
|
+
return ctx->buffer.len - ctx->position_offset;
|
986
799
|
}
|
987
800
|
|
988
801
|
MARK_FUNC_AS_USED
|
989
|
-
static void
|
990
|
-
memmove(ctx->buffer.buf, ctx->buffer.buf + ctx->
|
991
|
-
ctx->buffer.len -= ctx->
|
992
|
-
ctx->
|
993
|
-
|
994
|
-
ctx->
|
802
|
+
static void packcr_commit_buffer(packcr_context_t *ctx) {
|
803
|
+
memmove(ctx->buffer.buf, ctx->buffer.buf + ctx->position_offset, ctx->buffer.len - ctx->position_offset);
|
804
|
+
ctx->buffer.len -= ctx->position_offset;
|
805
|
+
ctx->buffer_start_position += ctx->position_offset;
|
806
|
+
packcr_lr_table__shift(ctx, &ctx->lrtable, ctx->position_offset);
|
807
|
+
ctx->position_offset = 0;
|
995
808
|
<%- if @location -%>
|
996
|
-
ctx->
|
997
|
-
|
809
|
+
ctx->buffer_start_position_loc = packcr_location_add(ctx->buffer_start_position_loc, ctx->position_offset_loc);
|
810
|
+
packcr_location_init(&ctx->position_offset_loc);
|
998
811
|
<%- end -%>
|
999
812
|
}
|
1000
813
|
|
1001
814
|
MARK_FUNC_AS_USED
|
1002
|
-
static const char *
|
815
|
+
static const char *packcr_get_capture_string(packcr_context_t *ctx, const packcr_capture_t *capt) {
|
1003
816
|
if (capt->string == NULL)
|
1004
|
-
((
|
1005
|
-
|
817
|
+
((packcr_capture_t *)capt)->string =
|
818
|
+
packcr_strndup_e(ctx->auxil, ctx->buffer.buf + capt->range.start, capt->range.end - capt->range.start);
|
1006
819
|
return capt->string;
|
1007
820
|
}
|
1008
821
|
|
1009
822
|
<% if @utf8 -%>
|
1010
|
-
static size_t
|
823
|
+
static size_t packcr_get_char_as_utf32(packcr_context_t *ctx, int *out) { /* with checking UTF-8 validity */
|
1011
824
|
int c, u;
|
1012
825
|
size_t n;
|
1013
|
-
if (
|
1014
|
-
c = (int)(unsigned char)ctx->buffer.buf[ctx->
|
826
|
+
if (packcr_refill_buffer(ctx, 1) < 1) return 0;
|
827
|
+
c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset];
|
1015
828
|
n = (c < 0x80) ? 1 :
|
1016
829
|
((c & 0xe0) == 0xc0) ? 2 :
|
1017
830
|
((c & 0xf0) == 0xe0) ? 3 :
|
1018
831
|
((c & 0xf8) == 0xf0) ? 4 : 0;
|
1019
832
|
if (n < 1) return 0;
|
1020
|
-
if (
|
833
|
+
if (packcr_refill_buffer(ctx, n) < n) return 0;
|
1021
834
|
switch (n) {
|
1022
835
|
case 1:
|
1023
836
|
u = c;
|
1024
837
|
break;
|
1025
838
|
case 2:
|
1026
839
|
u = c & 0x1f;
|
1027
|
-
c = (int)(unsigned char)ctx->buffer.buf[ctx->
|
840
|
+
c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset + 1];
|
1028
841
|
if ((c & 0xc0) != 0x80) return 0;
|
1029
842
|
u <<= 6; u |= c & 0x3f;
|
1030
843
|
if (u < 0x80) return 0;
|
1031
844
|
break;
|
1032
845
|
case 3:
|
1033
846
|
u = c & 0x0f;
|
1034
|
-
c = (int)(unsigned char)ctx->buffer.buf[ctx->
|
847
|
+
c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset + 1];
|
1035
848
|
if ((c & 0xc0) != 0x80) return 0;
|
1036
849
|
u <<= 6; u |= c & 0x3f;
|
1037
|
-
c = (int)(unsigned char)ctx->buffer.buf[ctx->
|
850
|
+
c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset + 2];
|
1038
851
|
if ((c & 0xc0) != 0x80) return 0;
|
1039
852
|
u <<= 6; u |= c & 0x3f;
|
1040
853
|
if (u < 0x800) return 0;
|
1041
854
|
break;
|
1042
855
|
case 4:
|
1043
856
|
u = c & 0x07;
|
1044
|
-
c = (int)(unsigned char)ctx->buffer.buf[ctx->
|
857
|
+
c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset + 1];
|
1045
858
|
if ((c & 0xc0) != 0x80) return 0;
|
1046
859
|
u <<= 6; u |= c & 0x3f;
|
1047
|
-
c = (int)(unsigned char)ctx->buffer.buf[ctx->
|
860
|
+
c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset + 2];
|
1048
861
|
if ((c & 0xc0) != 0x80) return 0;
|
1049
862
|
u <<= 6; u |= c & 0x3f;
|
1050
|
-
c = (int)(unsigned char)ctx->buffer.buf[ctx->
|
863
|
+
c = (int)(unsigned char)ctx->buffer.buf[ctx->position_offset + 3];
|
1051
864
|
if ((c & 0xc0) != 0x80) return 0;
|
1052
865
|
u <<= 6; u |= c & 0x3f;
|
1053
866
|
if (u < 0x10000 || u > 0x10ffff) return 0;
|
@@ -1060,135 +873,120 @@ static size_t pcc_get_char_as_utf32(pcc_context_t *ctx, int *out) { /* with chec
|
|
1060
873
|
}
|
1061
874
|
|
1062
875
|
<% end -%>
|
1063
|
-
|
1064
|
-
|
1065
|
-
|
1066
|
-
|
1067
|
-
|
876
|
+
static void packcr_grow_lr(packcr_context_t *ctx, packcr_rule_t rule, size_t offset<% if @location %>, packcr_location_t offset_loc<% end %>) {
|
877
|
+
while(1) {
|
878
|
+
const size_t old_offset = ctx->position_offset;
|
879
|
+
packcr_thunk_chunk_t *chunk;
|
880
|
+
packcr_lr_memo_t *memo;
|
881
|
+
packcr_rule_set_t limits;
|
882
|
+
ctx->position_offset = offset;
|
1068
883
|
<%- if @location -%>
|
1069
|
-
|
884
|
+
ctx->position_offset_loc = offset_loc;
|
1070
885
|
<%- end -%>
|
1071
|
-
|
1072
|
-
|
1073
|
-
|
1074
|
-
|
1075
|
-
if (
|
1076
|
-
|
1077
|
-
|
1078
|
-
|
1079
|
-
|
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);
|
886
|
+
packcr_rule_set__init(ctx->auxil, &limits);
|
887
|
+
packcr_rule_set__add(ctx->auxil, &limits, rule);
|
888
|
+
chunk = rule(ctx, offset<% if @location %>, offset_loc<% end %>, &limits);
|
889
|
+
packcr_rule_set__term(ctx->auxil, &limits);
|
890
|
+
if (!chunk)
|
891
|
+
break;
|
892
|
+
if (ctx->position_offset <= old_offset) {
|
893
|
+
packcr_thunk_chunk__destroy(ctx, chunk);
|
894
|
+
break;
|
1085
895
|
}
|
896
|
+
memo = packcr_lr_table__get_memo(ctx, &ctx->lrtable, offset, rule);
|
897
|
+
packcr_lr_memo__set_chunk(ctx, memo, chunk);
|
898
|
+
memo->offset = ctx->position_offset;
|
899
|
+
<%- if @location -%>
|
900
|
+
memo->offset_loc = ctx->position_offset_loc;
|
901
|
+
<%- end -%>
|
1086
902
|
}
|
1087
|
-
|
1088
|
-
|
1089
|
-
|
1090
|
-
|
1091
|
-
|
1092
|
-
|
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;
|
903
|
+
}
|
904
|
+
|
905
|
+
MARK_FUNC_AS_USED
|
906
|
+
static packcr_thunk_chunk_t *packcr_get_rule_thunk_chunk(packcr_context_t *ctx, packcr_rule_t rule) {
|
907
|
+
packcr_thunk_chunk_t *c = NULL;
|
908
|
+
size_t offset = ctx->position_offset;
|
1126
909
|
<%- if @location -%>
|
1127
|
-
|
910
|
+
packcr_location_t offset_loc = ctx->position_offset_loc;
|
1128
911
|
<%- end -%>
|
1129
|
-
|
1130
|
-
|
1131
|
-
|
1132
|
-
|
1133
|
-
|
1134
|
-
|
1135
|
-
|
1136
|
-
|
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;
|
912
|
+
packcr_lr_memo_t *memo = packcr_lr_table__get_memo(ctx, &ctx->lrtable, offset, rule);
|
913
|
+
|
914
|
+
if (memo == NULL) {
|
915
|
+
memo = packcr_lr_memo__create(ctx, offset<% if @location %>, offset_loc<% end %>);
|
916
|
+
packcr_lr_table__set_memo(ctx, &ctx->lrtable, offset, rule, memo);
|
917
|
+
c = rule(ctx, offset<% if @location %>, offset_loc<% end %>, NULL);
|
918
|
+
packcr_lr_memo__set_chunk(ctx, memo, c);
|
919
|
+
memo->offset = ctx->position_offset;
|
1150
920
|
<%- if @location -%>
|
1151
|
-
|
921
|
+
memo->offset_loc = ctx->position_offset_loc;
|
1152
922
|
<%- end -%>
|
1153
|
-
|
1154
|
-
|
1155
|
-
|
1156
|
-
|
1157
|
-
a->pos = ctx->pos + ctx->cur;
|
923
|
+
if (memo->grow) {
|
924
|
+
packcr_grow_lr(ctx, rule, offset<% if @location %>, offset_loc<% end %>);
|
925
|
+
memo->grow = PACKCR_FALSE;
|
926
|
+
ctx->position_offset = memo->offset;
|
1158
927
|
<%- if @location -%>
|
1159
|
-
|
928
|
+
ctx->position_offset_loc = memo->offset_loc;
|
1160
929
|
<%- end -%>
|
1161
|
-
|
1162
|
-
|
1163
|
-
|
1164
|
-
|
930
|
+
return memo->chunk;
|
931
|
+
}
|
932
|
+
return c;
|
933
|
+
} else if (memo->fail) {
|
934
|
+
packcr_lr_memo__set_chunk(ctx, memo, NULL);
|
935
|
+
memo->grow = PACKCR_TRUE;
|
936
|
+
return NULL;
|
937
|
+
}
|
938
|
+
ctx->position_offset = memo->offset;
|
1165
939
|
<%- if @location -%>
|
1166
|
-
|
940
|
+
ctx->position_offset_loc = memo->offset_loc;
|
1167
941
|
<%- end -%>
|
1168
|
-
|
1169
|
-
|
1170
|
-
|
942
|
+
return memo->chunk;
|
943
|
+
}
|
944
|
+
|
945
|
+
MARK_FUNC_AS_USED
|
946
|
+
static packcr_bool_t packcr_apply_rule(packcr_context_t *ctx, packcr_rule_t rule, packcr_thunk_array_t *thunks, packcr_value_t *value, size_t offset<% if @location %>, packcr_location_t offset_loc<% end %>, packcr_rule_set_t *limits) {
|
947
|
+
static packcr_value_t null;
|
948
|
+
packcr_thunk_chunk_t *c;
|
949
|
+
if (limits != NULL) {
|
950
|
+
packcr_lr_memo_t *memo;
|
951
|
+
packcr_rule_set__add(ctx->auxil, limits, rule);
|
952
|
+
c = rule(ctx, offset<% if @location %>, offset_loc<% end %>, limits);
|
953
|
+
memo = packcr_lr_table__get_memo(ctx, &ctx->lrtable, offset, rule);
|
954
|
+
if (memo == NULL || ctx->position_offset <= memo->offset) {
|
955
|
+
if (memo) {
|
956
|
+
c = memo->chunk;
|
957
|
+
ctx->position_offset = memo->offset;
|
958
|
+
<%- if @location -%>
|
959
|
+
ctx->position_offset_loc = memo->offset_loc;
|
960
|
+
<%- end -%>
|
1171
961
|
}
|
962
|
+
} else {
|
963
|
+
packcr_lr_memo__set_chunk(ctx, memo, c);
|
964
|
+
memo->offset = ctx->position_offset;
|
965
|
+
<%- if @location -%>
|
966
|
+
memo->offset_loc = ctx->position_offset_loc;
|
967
|
+
<%- end -%>
|
1172
968
|
}
|
969
|
+
} else {
|
970
|
+
c = packcr_get_rule_thunk_chunk(ctx, rule);
|
1173
971
|
}
|
1174
|
-
if (c == NULL) return
|
972
|
+
if (c == NULL) return PACKCR_FALSE;
|
1175
973
|
if (value == NULL) value = &null;
|
1176
|
-
memset(value, 0, sizeof(
|
1177
|
-
|
1178
|
-
return
|
974
|
+
memset(value, 0, sizeof(packcr_value_t)); /* in case */
|
975
|
+
packcr_thunk_array__add(ctx->auxil, thunks, packcr_thunk__create_node(ctx->auxil, &c->thunks, value));
|
976
|
+
return PACKCR_TRUE;
|
1179
977
|
}
|
1180
978
|
|
1181
979
|
MARK_FUNC_AS_USED
|
1182
|
-
static void
|
980
|
+
static void packcr_do_action(packcr_context_t *ctx, const packcr_thunk_array_t *thunks, packcr_value_t *value) {
|
1183
981
|
size_t i;
|
1184
982
|
for (i = 0; i < thunks->len; i++) {
|
1185
|
-
|
983
|
+
packcr_thunk_t *const thunk = thunks->buf[i];
|
1186
984
|
switch (thunk->type) {
|
1187
|
-
case
|
985
|
+
case PACKCR_THUNK_LEAF:
|
1188
986
|
thunk->data.leaf.action(ctx, thunk, value);
|
1189
987
|
break;
|
1190
|
-
case
|
1191
|
-
|
988
|
+
case PACKCR_THUNK_NODE:
|
989
|
+
packcr_do_action(ctx, thunk->data.node.thunks, thunk->data.node.value);
|
1192
990
|
break;
|
1193
991
|
default: /* unknown */
|
1194
992
|
break;
|
@@ -1198,32 +996,32 @@ static void pcc_do_action(pcc_context_t *ctx, const pcc_thunk_array_t *thunks, p
|
|
1198
996
|
|
1199
997
|
<%- @root.rules.each do |rule| -%>
|
1200
998
|
<%- rule.codes.each do |code| -%>
|
1201
|
-
static void
|
1202
|
-
#define auxil (
|
1203
|
-
#define __ (*
|
999
|
+
static void packcr_action_<%= rule.name %>_<%= code.index %>(<%= prefix %>_context_t *__packcr_ctx, packcr_thunk_t *__packcr_in, packcr_value_t *__packcr_out) {
|
1000
|
+
#define auxil (__packcr_ctx->auxil)
|
1001
|
+
#define __ (*__packcr_out)
|
1204
1002
|
<%- code.vars.each do |ref| -%>
|
1205
|
-
#define <%= ref.var %> (*
|
1003
|
+
#define <%= ref.var %> (*__packcr_in->data.leaf.values.buf[<%= ref.index %>])
|
1206
1004
|
<% end -%>
|
1207
|
-
#define _0
|
1208
|
-
#define _0s ((const size_t)(
|
1209
|
-
#define _0e ((const size_t)(
|
1005
|
+
#define _0 packcr_get_capture_string(__packcr_ctx, &__packcr_in->data.leaf.capt0)
|
1006
|
+
#define _0s ((const size_t)(__packcr_ctx->buffer_start_position + __packcr_in->data.leaf.capt0.range.start))
|
1007
|
+
#define _0e ((const size_t)(__packcr_ctx->buffer_start_position + __packcr_in->data.leaf.capt0.range.end))
|
1210
1008
|
<%- if @location -%>
|
1211
|
-
#define _0sl ((const
|
1212
|
-
#define _0el ((const
|
1009
|
+
#define _0sl ((const packcr_location_t)(packcr_location_add(__packcr_ctx->buffer_start_position_loc, __packcr_in->data.leaf.capt0.range.start_loc)))
|
1010
|
+
#define _0el ((const packcr_location_t)(packcr_location_add(__packcr_ctx->buffer_start_position_loc, __packcr_in->data.leaf.capt0.range.end_loc)))
|
1213
1011
|
<%- end -%>
|
1214
1012
|
<%- if @capture_in_code -%>
|
1215
|
-
#define _0c
|
1013
|
+
#define _0c __packcr_in->data.leaf.capt0
|
1216
1014
|
<%- end -%>
|
1217
1015
|
<% code.capts.each do |capture| -%>
|
1218
|
-
#define _<%= capture.index + 1 %>
|
1219
|
-
#define _<%= capture.index + 1 %>s ((const size_t)(
|
1220
|
-
#define _<%= capture.index + 1 %>e ((const size_t)(
|
1016
|
+
#define _<%= capture.index + 1 %> packcr_get_capture_string(__packcr_ctx, __packcr_in->data.leaf.capts.buf[<%= capture.index %>])
|
1017
|
+
#define _<%= capture.index + 1 %>s ((const size_t)(__packcr_ctx->buffer_start_position + __packcr_in->data.leaf.capts.buf[<%= capture.index %>]->range.start))
|
1018
|
+
#define _<%= capture.index + 1 %>e ((const size_t)(__packcr_ctx->buffer_start_position + __packcr_in->data.leaf.capts.buf[<%= capture.index %>]->range.end))
|
1221
1019
|
<%- if @location -%>
|
1222
|
-
#define _<%= capture.index + 1 %>sl ((const
|
1223
|
-
#define _<%= capture.index + 1 %>el ((const
|
1020
|
+
#define _<%= capture.index + 1 %>sl ((const packcr_location_t)(packcr_location_add(__packcr_ctx->buffer_start_position_loc, __packcr_in->data.leaf.capts.buf[<%= capture.index %>]->range.start_loc)))
|
1021
|
+
#define _<%= capture.index + 1 %>el ((const packcr_location_t)(packcr_location_add(__packcr_ctx->buffer_start_position_loc, __packcr_in->data.leaf.capts.buf[<%= capture.index %>]->range.end_loc)))
|
1224
1022
|
<%- end -%>
|
1225
1023
|
<%- if @capture_in_code -%>
|
1226
|
-
#define _<%= capture.index + 1 %>c (*
|
1024
|
+
#define _<%= capture.index + 1 %>c (*__packcr_in->data.leaf.capts.buf[<%= capture.index %>])
|
1227
1025
|
<%- end -%>
|
1228
1026
|
<%- end -%>
|
1229
1027
|
<%= stream.get_code_block(code.code, 4, @iname) -%>
|
@@ -1249,40 +1047,33 @@ static void pcc_action_<%= rule.name %>_<%= code.index %>(<%= prefix %>_context_
|
|
1249
1047
|
<%- end -%>
|
1250
1048
|
<%- end -%>
|
1251
1049
|
<%- @root.rules.each do |rule| -%>
|
1252
|
-
static
|
1050
|
+
static packcr_thunk_chunk_t *packcr_evaluate_rule_<%= rule.name %>(packcr_context_t *ctx, size_t offset<% if @location %>, packcr_location_t offset_loc<% end %>, packcr_rule_set_t *limits);
|
1253
1051
|
<%- end -%>
|
1254
1052
|
|
1255
1053
|
<%- @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
1054
|
<%- gen = ::Packcr::Generator.new(rule, @ascii, @location) -%>
|
1263
1055
|
<%= gen.generate_code(rule, 0, 0, false) -%>
|
1264
|
-
}
|
1265
1056
|
|
1266
1057
|
<%- end -%>
|
1267
1058
|
<%= prefix %>_context_t *<%= prefix %>_create(<%= auxil_def %>auxil) {
|
1268
|
-
return
|
1059
|
+
return packcr_context__create(auxil);
|
1269
1060
|
}
|
1270
1061
|
|
1271
1062
|
int <%= prefix %>_parse(<%= prefix %>_context_t *ctx, <%= value_def %>*ret) {
|
1272
|
-
size_t pos = ctx->
|
1063
|
+
size_t pos = ctx->buffer_start_position;
|
1273
1064
|
<%- if !@root.rules.empty? -%>
|
1274
|
-
if (
|
1275
|
-
|
1065
|
+
if (packcr_apply_rule(ctx, packcr_evaluate_rule_<%= @root.rules[0].name %>, &ctx->thunks, ret, ctx->position_offset<% if @location %>, ctx->position_offset_loc<% end %>, NULL))
|
1066
|
+
packcr_do_action(ctx, &ctx->thunks, ret);
|
1276
1067
|
else
|
1277
|
-
|
1278
|
-
|
1068
|
+
PACKCR_ERROR(ctx->auxil);
|
1069
|
+
packcr_commit_buffer(ctx);
|
1279
1070
|
<%- end -%>
|
1280
|
-
|
1281
|
-
return pos != ctx->
|
1071
|
+
packcr_thunk_array__revert(ctx->auxil, &ctx->thunks, 0);
|
1072
|
+
return pos != ctx->buffer_start_position && packcr_refill_buffer(ctx, 1) >= 1;
|
1282
1073
|
}
|
1283
1074
|
|
1284
1075
|
void <%= prefix %>_destroy(<%= prefix %>_context_t *ctx) {
|
1285
|
-
|
1076
|
+
packcr_context__destroy(ctx);
|
1286
1077
|
}
|
1287
1078
|
<%- if !code(:lsource).empty? -%>
|
1288
1079
|
|