blob: 90ead2103a6cf5dc7894813901152592b7d67cba (
plain)
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
|
#include <fspec/bcode.h>
#include <fspec/validator.h>
#include "util/ragel/ragel.h"
#include "fspec/private/bcode-types.h"
#include <assert.h>
struct stack {
union {
fspec_num num;
fspec_off off;
fspec_var var;
fspec_strsz strsz;
unsigned char b[sizeof(fspec_num)];
} u;
uint8_t i;
};
struct state {
struct ragel ragel;
struct stack stack;
};
%%{
machine fspec_validator;
variable p state.ragel.p;
variable pe state.ragel.pe;
variable eof state.ragel.eof;
write data noerror nofinal;
main := any*;
}%%
bool
fspec_validator_parse(struct fspec_validator *validator, const char *name)
{
int cs;
%% write init;
(void)fspec_validator_en_main;
assert(validator);
assert(validator->ops.read);
assert(validator->mem.input.data && validator->mem.input.len);
assert(validator->mem.input.len <= (size_t)~0 && "input storage size exceeds size_t range");
struct state state = {
.ragel.name = name,
.ragel.lineno = 1,
};
static_assert(sizeof(state.stack.u) == sizeof(state.stack.u.b), "bytes doesn't represent the largest member in union");
struct fspec_mem input = validator->mem.input;
for (bool eof = false; !state.ragel.error && !eof;) {
const size_t bytes = validator->ops.read(validator, input.data, 1, input.len);
const struct ragel_mem rl = { .data = input.data, .end = (char*)input.data + bytes, .binary = true };
ragel_feed_input(&state.ragel, (eof = (bytes < input.len)), &rl);
%% write exec;
}
return !state.ragel.error;
}
|