-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpeg-internal.h
272 lines (252 loc) · 12.6 KB
/
peg-internal.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/** @file Содержит определения внутренних типов и функций, используемых парсером.
Данный файл долен быть С89-совместимым, так, чтобы `gcc -pedantic -Wall -Wextra -ansi -std=c89`
не выдавал предупреждений.
*/
#include "peg.h"
/* Для memcmp, memcpy. */
#include <string.h>
/* Для malloc/calloc/free/bsearch. */
#include <stdlib.h>
/* Для va_*, используемых в функции wrap. */
#include <stdarg.h>
#include <assert.h>
struct Literal {
/** Длина литерала (массива data). */
unsigned int len;
/** Массив символов литерала. */
const char* data;
/** Сообщение об ожидаемом элементе, если сопоставление с литералом пройдет неудачно. */
/*struct Expected expected;*/
};
struct CharClass {
/** Старшая половина -- количество элементов в массиве single, младшая -- количество пар
в массиве range.
*/
unsigned int counts;
/** Массив одиночных символов, включенный в данный набор. */
const char* single;
/** Массив пар символов (т.е. длина массива всегда четная), представляющих в данном наборе
поддиапазоны от первого до посленего символа (обе границы включительно).
*/
const char* range;
/** Сообщение об ожидаемом элементе, если сопоставление с классом символов пройдет неудачно. */
/*struct Expected expected;*/
};
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* Вспомогательные структуры. */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
typedef struct Result* ResultPtr;
typedef struct Result* (*RuleFunc)(struct Context*);
struct ParseFunc {
/** Длина имени функции для разбора правила. */
unsigned int len;
/** Имя функции для разбора правила. */
const char* name;
/** Функция для разбора правила. */
RuleFunc func;
};
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* Процедуры сопоставления. */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
int matchLiteral(struct Context* context, const struct Literal* literal) {
const char* begin;
unsigned int inputLen;
begin = context->input.begin + context->current.offset;
inputLen = context->input.end - begin;
/* Если входная строка короче, то она точно не равна литералу. */
return inputLen < literal->len ? 0 : memcmp(begin, literal->data, literal->len) == 0;
}
/**
@param context Информация о разбираемом участке и текущей в нем позиции.
@param ranges Массив из @a count описателей диапазонов символов/списков символов.
@param count Количество элементов в массиве @a ranges.
*/
int matchCharClass(struct Context* context, struct CharClass* cls, int inverted) {
const char* begin;
assert(context);
assert(context->input.begin);
assert(cls);
assert(cls->single);
assert(cls->range);
begin = context->input.begin + context->current.offset;
/* Если мы еще не в конце входных данных, то пытаемся сматчить текущий символ. */
if (begin < context->input.end) {
/* Выделяем нижнюю половину бит числа с помощью маски. Верхнюю половину получаем, просто
отодвинув ненужную нижнюю половину. */
unsigned int countLO;
unsigned int countHI;
char ch;
unsigned int i;
countLO = cls->counts & ((~0u) >> (sizeof(cls->counts)*4));
countHI = cls->counts >> (sizeof(cls->counts)*4);
ch = begin[0];
/* Класс символов является списком пар, задающих диапазоны символов. */
for (i = 0; i < countLO; ++i) {
char b;
char e;
b = cls->range[i*2];
e = cls->range[i*2 + 1];
/* Строгая проверка, т.к. если начало и конец совпадают, то это единственный символ
и он должен быть в cls->single. */
assert(b < e);
if (b <= ch && ch <= e) {
/* Текущий символ принадлежит диапазону символов. */
return !inverted;
}
}
/* Класс символов является просто списком символов, которые нужно проверить. */
if (memchr(cls->single, ch, countHI) != 0) {
/* Если текущий символ строки имеется в списке допустимых символов, сопоставление успешно. */
return !inverted;
}
}
return inverted;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* Работа с памятью. */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
struct Result* allocResult(const char* begin, const char* end, unsigned int count) {
struct Result* r = (struct Result*)malloc(sizeof(struct Result));
assert(begin);
assert(end);
assert(r);
r->region.begin.data = begin;
r->region.end.data = end;
r->count = count;
r->childs = count == 0 ? 0 : calloc(count, sizeof(struct Result));
return r;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* Работа с ожидаемыми элементами. */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void clearExpected(struct FailInfo* info) {
assert(info);
free(info->expected);
info->count = 0;
info->expected = 0;
}
void pushExpected(struct FailInfo* info, struct Expected* expected) {
unsigned int count;
assert(info);
assert(expected);
count = ++info->count;
info->expected = (const struct Expected**)realloc(info->expected, count * sizeof(*info->expected));
info->expected[count-1] = expected;
}
struct Result* fail(struct Context* context, struct Expected* expected) {
assert(context);
assert(expected);
/* Если подавление запоминания позиций ошибки разбора не включено, то пытаемся
запомнить позицию, если она расположена во входных данных позже, чем уже имеющиеся.
*/
if (context->failInfo.silent == 0) {
if (context->current.offset < context->failInfo.pos.offset) { return &FAILED; }
if (context->current.offset > context->failInfo.pos.offset) {
memcpy(&context->failInfo.pos, &context->current, sizeof(struct Location));
clearExpected(&context->failInfo);
}
pushExpected(&context->failInfo, expected);
}
return &FAILED;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* Работа с позицией. */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void movePos(struct Context* context, unsigned int count) {
context->current.data += count;
context->current.offset += count;
/*TODO: Скорректировать line и column*/
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* Правила разбора примитивов. */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/** Если в разбираемых данных еще не достигнут конец, продвигает текущую позицию на один символ
вперед и возвращает результат с границами от текущей позиции до текущей позиции плюс 1.
В случае неудачи позиция остается неизменной и возвращается константа FAILED.
@param context Информация о разбираемом участке и текущей в нем позиции.
@return Результат разбора или константу FAILED, если разбор неудачен. Результат необходимо
освободить функцией freeResult, когда он больше не будет нужен.
*/
struct Result* parseAny(struct Context* context) {
#define MAKE_TYPEANDLEN(type, len) ((type << (sizeof(((struct Expected*)0)->typeAndLen)*8 - 3)) | len)
static struct Expected expected = {
MAKE_TYPEANDLEN(E_EX_TYPE_ANY, sizeof("any character") / sizeof(char)),
"any character"
};
#undef MAKE_TYPEANDLEN
const char* begin = context->input.begin + context->current.offset;
if (begin < context->input.end) {
movePos(context, 1);
return allocResult(begin, begin + 1, 0);
} else {
return fail(context, &expected);
}
}
/** Если указанная строка @a literal является подстрокой разбираемых данных, начиная с текущей
позиции разбора, то продвигает текущую позицию на величину @a len и возвращает результат с
границами от текущей позиции до текущей позиции плюс @a len.
В случае неудачи позиция остается неизменной и возвращается константа FAILED.
@param context Информация о разбираемом участке и текущей в нем позиции.
@param literal Литерал, с которым осуществляется сопоставление разбираемых данных.
@return Результат разбора или константу FAILED, если разбор неудачен. Результат необходимо
освободить функцией freeResult, когда он больше не будет нужен.
*/
struct Result* parseLiteral(struct Context* context, struct Literal* literal, struct Expected* expected) {
assert(context);
assert(literal);
assert(expected);
if (matchLiteral(context, literal)) {
const char* begin = context->input.begin + context->current.offset;
movePos(context, literal->len);
return allocResult(begin, begin + literal->len, 0);
} else {
return fail(context, expected);
}
}
struct Result* parseCharClass(struct Context* context, struct CharClass* cls, struct Expected* expected, int inverted) {
assert(context);
assert(cls);
assert(expected);
if (matchCharClass(context, cls, inverted)) {
const char* begin = context->input.begin + context->current.offset;
movePos(context, 1);
return allocResult(begin, begin + 1, 0);
} else {
return fail(context, expected);
}
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
struct Result* wrap(struct Context* context, unsigned int pos, unsigned int count, ...) {
struct Result* r;
unsigned int i;
va_list results;
va_start(results, count);
r = allocResult(context->input.begin + pos, context->input.begin + context->current.offset, count);
for (i = 0; i < count; ++i) {
assert(r->childs);
r->childs[i] = va_arg(results, struct Result*);
assert(r->childs[i]);
}
va_end(results);
return r;
}
static int findRuleCompatator(const struct Range* name, const struct ParseFunc* entry) {
unsigned int len;
assert(name);
assert(name->begin);
assert(name->end);
assert(entry);
len = name->end - name->begin;
if (len < entry->len) { return -1; }
if (len > entry->len) { return +1; }
return memcmp(name->begin, entry->name, len);
}
const struct ParseFunc* findRule(const struct ParseFunc* table, size_t count, const struct Range* name) {
typedef int (*Comparator)(const void*, const void*);
assert(table);
assert(name);
return (const struct ParseFunc*)bsearch(
name, table, count, sizeof(table[0]),
(Comparator)&findRuleCompatator
);
}