-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.c
266 lines (246 loc) · 7.05 KB
/
grammar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#define _POSIX_C_SOURCE 200809L // strndup
#include <stdint.h>
#include <string.h>
#include "grammar.h"
symbol **symbolmap = NULL;
const char* start_key = "__start__";
const char ***build_grammar(const char *const filename){
const char ***map = calloc(mapsize, sizeof(char**));
if(!map){
fputs("Insufficient memory for grammar.\n", stderr);
exit(1);
}
json_error_t json_err = {0};
json_t *root = json_load_file(filename, 0, &json_err);
for(size_t i=mapbase; i<mapsize; i++){
map[i] = calloc(linewidth, sizeof(char*));
if(!map[i]){
fputs("Insufficient memory for grammar.\n", stderr);
exit(1);
}
}
const char *key;
json_t *value = NULL;
unsigned mapidx = mapbase;
size_t symidx = 1, line_len = 0;
json_object_foreach(root, key, value){
json_t *data = NULL;
size_t linesz = json_array_size(value);
if(strcmp(key, start_key) == 0){
for(size_t i=0; i<linesz; i++){
data = json_array_get(value, i);
map[mapbase][i] = strndup(json_string_value(data), max_textlen);
}
}else{
symbolmap_add(key, symidx++);
for(size_t i=0; i<linesz; i++){
data = json_array_get(value, i);
map[mapidx][i] = strndup(json_string_value(data), max_textlen);
}
}
mapidx++;
}
json_decref(root);
for(unsigned mapidx=mapbase; mapidx<mapsize; mapidx++){
line_len = linelen(map[mapidx]);
if(line_len == 0){
continue;
}
for(size_t idx=0; idx<line_len; idx++){
char *addrdata = patch_symbol_addresses(map[mapidx][idx]);
free((char*)map[mapidx][idx]);
map[mapidx][idx] = addrdata;
}
}
return map;
}
void print_grammar(const char ***grammar){
size_t c_len;
for(unsigned mapidx=mapbase; mapidx<mapsize; mapidx++){
c_len = linelen(grammar[mapidx]);
if(c_len == 0){
continue;
}
printf("%x:\n", mapidx);
for(size_t idx=0; idx<c_len; idx++){
printf(" %s\n", grammar[mapidx][idx]);
}
}
}
void free_grammar(const char ***grammar){
for(size_t midx=mapbase; midx<mapsize; midx++){
for(size_t lidx=0; lidx<linewidth; lidx++){
free((char*)grammar[midx][lidx]);
}
}
for(size_t midx=mapbase; midx<mapsize; midx++){
free(grammar[midx]);
}
free(grammar);
}
size_t symbolmap_init(symbol ***map){
*map = calloc(mapsize, sizeof(symbol **));
if(!map){
fputs("Insufficient memory for symbolmap.\n", stderr);
exit(1);
}
return mapsize;
}
void symbolmap_free(symbol **symbolmap){
for(size_t idx=0; idx<mapsize; idx++){
symbol *list = symbolmap[idx], *prev = NULL;
while(list){
prev = list;
list = list->next;
free(prev->symbol);
prev->index = 0;
prev->next = NULL;
free(prev);
}
}
free(symbolmap);
}
void symbolmap_add(const char *symtxt, size_t symidx){
symbol *s = calloc(1, sizeof(symbol));
if(!s){
fputs("Insufficient memory for symbol.\n", stderr);
exit(1);
}
s->symbol = strndup(symtxt, max_symbollen);
s->index = symidx;
s->next = NULL;
uint32_t hash = fnv1a32_hashstr(s->symbol);
uint8_t bucket = hash & 0xFF;
symbol **list = &symbolmap[bucket];
while(*list){
//if(strcmp(symtxt, *list->symbol) == 0){
// ; // err already in list
//}
list = &(*list)->next;
}
*list = s;
}
size_t symbolmap_getidx(const char *symtxt){
uint32_t hash = fnv1a32_hashstr(symtxt);
uint8_t bucket = hash & 0xFF;
symbol *list = symbolmap[bucket];
while(list){
if(strcmp(symtxt, list->symbol) == 0){
return list->index;
}
list = list->next;
}
return 0; // Only __start__ can have a zero symbol-index
}
char **extract_symbols(const char *str){
const char* start = "{";
const char* end = "}";
size_t bufsz = 32;
if(strlen(str) == 0){
return NULL;
}
char *tokstr = strdup(str), *tok = NULL;
char **tokens = calloc(bufsz, sizeof(char *));
if(!tokens){
fputs("Insufficient memory for symbol buffer.\n", stderr);
exit(1);
}
size_t idx = 0, endidx=0;
tok = strtok(tokstr, start);
if((strlen(tok) == strlen(str)) || !tok){
goto tok_tidy_exit;
}
endidx = strcspn(tok, end);
if(endidx < strlen(tok)){
tokens[idx++] = strndup(tok, endidx);
}
while((tok = strtok(NULL, start))){
endidx = strcspn(tok, end);
if(endidx < strlen(tok)){
tokens[idx++] = strndup(tok, endidx);
}
if(idx == bufsz){
bufsz *= 2;
char **np = realloc(tokens, bufsz*sizeof(char *));
if(!np){
fputs("Insufficient memory to grow symbol buffer.\n", stderr);
free(tokens);
exit(1);
}
tokens = np;
}
}
if(idx == 0){
goto tok_tidy_exit;
}
tokens[idx++] = NULL;
// Shrink the buffer
char **np = realloc(tokens, idx*sizeof(char *));
if(!np){
fputs("Insufficient memory to shrink symbol buffer.\n", stderr);
free(tokens);
exit(1);
}
tokens = np;
free(tokstr);
return tokens;
tok_tidy_exit:
free(tokstr);
free(tokens);
return NULL;
}
size_t linelen(const char **line){
size_t len = 0;
while(line[len++]){
if(len == linewidth){
return linewidth;
}
}
return len-1;
}
char *patch_symbol_addresses(const char *text){
char **symbols = extract_symbols(text);
if(!symbols){
return strdup(text);
}
size_t symidx = 0;
size_t txtlen = strlen(text);
char *patched = calloc(txtlen, sizeof(char));
if(!patched){
fputs("Insufficient memory for address buffer.\n", stderr);
exit(1);
}
char *pnext = patched;
char *str = (char *)text;
/* Note that symbols are extracted above in
* the order and multiplicity they are encountered
* in *text. As such, we're certain to encounter
* them again here in the same order.
*/
while(symbols[symidx]){
size_t slen = strlen(str);
size_t symlen = strlen(symbols[symidx]);
size_t mapidx = symbolmap_getidx(symbols[symidx]);
(void)mapidx;
char *sidx = strstr(str, symbols[symidx]);
if(!sidx){
continue;
}
if(sidx - str > 0 && sidx + symlen - str <= (long)slen){
if(sidx[-1] == '{' && sidx[symlen] == '}'){
strncpy(pnext, str, sidx-str-1);
pnext += (sidx-str-1);
*pnext = (char)(mapbase + mapidx);
pnext++;
}
}
str = sidx + symlen + 1;
symidx++;
}
strcpy(pnext, str);
for(size_t idx=0; symbols[idx]; idx++){
free(symbols[idx]);
}
free(symbols);
return patched;
}