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
|
#include <fspec/lexer.h>
#include <fspec/bcode.h>
#include "lexer-stack.h"
#include "util/ragel/ragel.h"
#include "fspec/private/bcode-types.h"
#include <assert.h>
%%{
machine fspec_lexer;
include fspec_stack "lexer-stack.rl";
variable p ragel.p;
variable pe ragel.pe;
variable eof ragel.eof;
write data noerror nofinal;
action arg_eof {
}
action arg_num {
}
action arg_str {
}
action arg_var {
}
action filter {
}
action goto {
}
action vnul {
}
action vdec {
}
action vhex {
}
action vstr {
}
action r8 {
}
action r16 {
}
action r32 {
}
action r64 {
}
action enum_member_end {
}
action enum_member_start {
}
action enum_end {
}
action enum_start {
}
action struct_member_end {
}
action struct_member_start {
}
action struct_end {
}
action struct_start {
}
action type_err {
ragel_throw_error(&ragel, "unknown type name");
}
action visual_err {
ragel_throw_error(&ragel, "unknown visualization");
}
action syntax_err {
ragel_throw_error(&ragel, "malformed input (machine failed here or in next expression)");
}
action line {
ragel_advance_line(&ragel);
}
newline = '\n';
valid = ^cntrl;
comment = '//' <: valid* :>> newline;
type = ('u8' | 's8') %r8 | ('u16' | 's16') %r16 | ('u32' | 's32') %r32 | ('u64' | 's32') %r64;
visual = 'nul' %vnul | 'dec' %vdec | 'hex' %vhex | 'str' %vstr;
catch_const_expr = stack_num %arg_num;
catch_struct = 'struct ' <: stack_name;
catch_enum = 'enum ' <: stack_name;
catch_type = (catch_struct %goto | type) $!type_err;
catch_args = stack_num %arg_num | stack_str %arg_str | stack_name %arg_var;
catch_array = '[' <: (catch_args | '$' %arg_eof) :>> ']';
catch_filter = ' | ' %filter <: stack_name %arg_str :>> ('(' <: catch_args? <: (', ' <: catch_args)* :>> ')')?;
catch_visual = ' ' <: visual $!visual_err;
struct_member = stack_name %struct_member_start :>> ': ' <: (catch_type <: catch_array* catch_filter* catch_visual?) :>> ';' %struct_member_end;
struct = catch_struct %struct_start :>> ' {' <: (space | comment | struct_member)* :>> '};' %struct_end;
enum_member = stack_name %enum_member_start :>> (': ' <: catch_const_expr)? :>> ';' %enum_member_end;
enum = catch_enum %enum_start :>> ' {' <: (space | comment | enum_member)* :>> '};' %enum_end;
line = valid* :>> newline %line;
main := ((space | comment | enum | struct)* & line*) $!syntax_err;
}%%
bool
fspec_lexer_parse(struct fspec_lexer *lexer, const char *name)
{
int cs;
%% write init;
(void)fspec_lexer_en_main;
assert(lexer);
assert(lexer->ops.read);
assert(lexer->mem.input.data && lexer->mem.input.len);
assert(lexer->mem.input.len <= (size_t)~0 && "input storage size exceeds size_t range");
char var[256];
struct stack stack = { .var.buf.mem = { .data = var, .len = sizeof(var) } };
struct ragel ragel = { .name = name, .lineno = 1 };
struct fspec_mem input = lexer->mem.input;
for (bool eof = false; !ragel.error && !eof;) {
const size_t bytes = lexer->ops.read(lexer, input.data, 1, input.len);
const struct ragel_mem rl = { .data = input.data, .end = (char*)input.data + bytes };
ragel_feed_input(&ragel, (eof = (bytes < input.len)), &rl);
%% write exec;
}
return !ragel.error;
}
|