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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
#include <stdio.h>
#include <stdlib.h>
#include "eo_tokenizer.h"
static void
_eo_tokenizer_abort(Eo_Tokenizer *toknz,
const char *file, const char* fct, int line,
const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
eina_log_vprint(_eo_tokenizer_log_dom, EINA_LOG_LEVEL_ERR,
file, fct, line, fmt, ap);
va_end(ap);
fprintf(stderr, " toknz[%d] n:%d l:%d p:%d pe:%d ts:%d te:%d act:%d\n",
toknz->cs, toknz->current_nesting, toknz->current_line,
(toknz->p - toknz->buf), (toknz->pe - toknz->buf),
toknz->ts, toknz->te, toknz->act);
exit(EXIT_FAILURE);
}
#define ABORT(toknz, ...) \
_eo_tokenizer_abort(toknz, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__);
static const char*
_eo_tokenizer_token_get(Eo_Tokenizer *toknz, char *p)
{
if (toknz->saved.tok == NULL) ABORT(toknz, "toknz->saved.tok is NULL");
int l = (p - toknz->saved.tok);
char d[BUFSIZE];
memcpy(d, toknz->saved.tok, l);
d[l] = '\0';
toknz->saved.tok = NULL;
DBG("token : >%s<", d);
return eina_stringshare_add(d);
}
static Eo_Class_Def*
_eo_tokenizer_class_get(Eo_Tokenizer *toknz, char *p)
{
Eo_Class_Def *kls = calloc(1, sizeof(Eo_Class_Def));
if (kls == NULL) ABORT(toknz, "calloc Eo_Class_Def failure");
kls->name = _eo_tokenizer_token_get(toknz, p);
return kls;
}
%%{
machine common;
access toknz->;
variable p toknz->p;
variable pe toknz->pe;
variable eof toknz->eof;
action inc_line {
toknz->current_line += 1;
DBG("inc[%d] %d", toknz->cs, toknz->current_line);
}
action save_line {
toknz->saved.line = toknz->current_line;
DBG("save line[%d] %d", toknz->cs, toknz->current_line);
}
action save_fpc {
toknz->saved.tok = fpc;
DBG("save token[%d] %p %c", toknz->cs, fpc, *fpc);
}
action show_comment {
DBG("comment[%d] line%03d:%03d", toknz->cs,
toknz->saved.line, toknz->current_line);
}
action show_ignore {
DBG("ignore[%d] line:%d", toknz->cs, toknz->current_line);
}
action show_error {
DBG("error[%d]", toknz->cs);
char *s, *d;
char buf[BUFSIZE];
for (s = fpc, d = buf; (s <= toknz->pe); s++)
{
if ((*s == '\r') || (*s == '\n'))
break;
*d++ = *s;
}
*d = '\0';
ERR("error nesting:%d line:%d : %s",
toknz->current_nesting, toknz->current_line, buf);
toknz->cs = eo_tokenizer_error;
fbreak; /* necessary to stop scanners */
}
cr = '\n';
cr_neg = [^\n];
ws = [ \t\r];
newline = cr @inc_line;
ignore = (0x00..0x20 - cr)+ newline?;
eo_comment = "/*@" ignore* %save_fpc ( any | '\n' @inc_line )* :>> "*/";
c_comment = "/*" ( any | '\n' @inc_line )* :>> "*/";
cpp_comment = "//" (any - cr)* newline;
comment = ( c_comment | cpp_comment ) > save_line;
alnum_u = alnum | '_';
alpha_u = alpha | '_';
ident = alpha+ >save_fpc (alnum | '_' )+;
begin_def = '{';
end_def = '};';
begin_list = '(';
end_list = ')';
list_separator = ',';
}%%
%%{
machine eo_tokenizer;
include common;
write data;
# TOKENIZE CLASS
action end_class_comment {
toknz->tmp.kls->comment = _eo_tokenizer_token_get(toknz, fpc-1);
}
action end_inherit_name {
const char *base = _eo_tokenizer_token_get(toknz, fpc);
toknz->tmp.kls->inherits = eina_list_append(toknz->tmp.kls->inherits, base);
}
action begin_properties {
INF(" begin propeties");
toknz->current_nesting++;
/* fgoto tokenize_properties; */
}
action begin_methods {
INF(" begin methods");
toknz->current_nesting++;
/* fgoto tokenize_methods; */
}
action end_class {
INF("end class:%s", toknz->tmp.kls->name);
toknz->classes= eina_list_append(toknz->classes, toknz->tmp.kls);
toknz->tmp.kls = NULL;
toknz->current_nesting--;
fgoto main;
}
inherit_item = ident %end_inherit_name ignore*;
inherit_item_next = list_separator ignore* inherit_item;
inherits = 'inherits' ignore* begin_def ignore* (inherit_item inherit_item_next*)? end_def;
properties = 'properties' ignore* begin_def;
methods = 'methods' ignore* begin_def;
tokenize_class := |*
ignore+; #=> show_ignore;
eo_comment => end_class_comment;
comment => show_comment;
inherits;
properties => begin_properties;
methods => begin_methods;
end_def => end_class;
any => show_error;
*|;
# TOP LEVEL
action begin_class {
INF("begin class: %s", toknz->tmp.kls->name);
toknz->current_nesting++;
fgoto tokenize_class;
}
action end_class_name {
toknz->tmp.kls = _eo_tokenizer_class_get(toknz, fpc);
}
begin_class = ident %end_class_name ignore* begin_def;
main := |*
ignore+; #=> show_ignore;
comment => show_comment;
begin_class => begin_class;
any => show_error;
*|;
}%%
Eina_Bool
eo_tokenizer_walk(Eo_Tokenizer *toknz, const char *source)
{
INF("tokenize %s...", source);
toknz->source = eina_stringshare_add(source);
FILE *stream;
int done = 0;
int have = 0;
int offset = 0;
stream = fopen(toknz->source, "r");
if (!stream)
{
ERR("unable to read in %s", toknz->source);
return EINA_FALSE;
}
%% write init;
while (!done)
{
int len;
int space;
toknz->p = toknz->buf + have;
space = BUFSIZE - have;
if (space == 0)
{
fclose(stream);
ABORT(toknz, "out of buffer space");
}
len = fread(toknz->p, 1, space, stream);
if (len == 0) break;
toknz->pe = toknz->p + len;
if (len < space)
{
toknz->eof = toknz->pe;
done = 1;
}
%% write exec;
if ( toknz->cs == %%{ write error; }%% )
{
ERR("wrong termination");
break;
}
if ( toknz->ts == 0 )
have = 0;
else
{
DBG("move data and pointers before buffer feed");
have = toknz->pe - toknz->ts;
offset = toknz->ts - toknz->buf;
memmove(toknz->buf, toknz->ts, have);
toknz->te -= offset;
toknz->ts = toknz->buf;
}
if (toknz->saved.tok != NULL)
{
if ((have == 0) || ((toknz->saved.tok - offset) < toknz->buf))
{
WRN("reset lost saved token %p", toknz->saved.tok);
toknz->saved.tok = NULL;
}
else
toknz->saved.tok -= offset;
}
}
fclose(stream);
return EINA_TRUE;
}
Eo_Tokenizer*
eo_tokenizer_get(void)
{
Eo_Tokenizer *toknz = calloc(1, sizeof(Eo_Tokenizer));
if (!toknz) return NULL;
toknz->ts = NULL;
toknz->te = NULL;
/* toknz->top = 0; */
toknz->source = NULL;
toknz->max_nesting = 10;
toknz->current_line = 1;
toknz->current_nesting = 0;
toknz->saved.tok = NULL;
toknz->saved.line = 0;
toknz->classes = NULL;
return toknz;
}
void
eo_tokenizer_dump(Eo_Tokenizer *toknz)
{
const char *s;
Eina_List *k, *l;
Eo_Class_Def *kls;
EINA_LIST_FOREACH(toknz->classes, k, kls)
{
printf("Class: %s (%s)\n",
kls->name, (kls->comment ? kls->comment : "-"));
printf(" inherits from :");
EINA_LIST_FOREACH(kls->inherits, l, s)
printf(" %s", s);
printf("\n");
}
}
static void
_eo_tokenizer_ret_free(Eo_Ret_Def *ret)
{
if (ret->type) eina_stringshare_del(ret->type);
if (ret->comment) eina_stringshare_del(ret->comment);
/* do not free */
}
static void
_eo_tokenizer_param_free(Eo_Param_Def *param)
{
if (param->type) eina_stringshare_del(param->type);
if (param->name) eina_stringshare_del(param->name);
if (param->comment) eina_stringshare_del(param->comment);
free(param);
}
static void
_eo_tokenizer_property_def_free(Eo_Property_Def *prop)
{
Eo_Param_Def *param;
_eo_tokenizer_ret_free(&prop->ret);
if (prop->name)
eina_stringshare_del(prop->name);
if (prop->comment)
eina_stringshare_del(prop->comment);
EINA_LIST_FREE(prop->params, param)
_eo_tokenizer_param_free(param);
free(prop);
}
static void
_eo_tokenizer_method_def_free(Eo_Method_Def *meth)
{
Eo_Param_Def *param;
_eo_tokenizer_ret_free(&meth->ret);
if (meth->name)
eina_stringshare_del(meth->name);
if (meth->comment)
eina_stringshare_del(meth->comment);
EINA_LIST_FREE(meth->params, param)
_eo_tokenizer_param_free(param);
free(meth);
}
static void
_eo_tokenizer_class_def_free(Eo_Class_Def *kls)
{
const char *s;
Eina_List *l;
Eo_Property_Def *prop;
Eo_Method_Def *meth;
if (kls->name)
eina_stringshare_del(kls->name);
if (kls->comment)
eina_stringshare_del(kls->comment);
EINA_LIST_FOREACH(kls->inherits, l, s)
if (s) eina_stringshare_del(s);
EINA_LIST_FREE(kls->properties, prop)
_eo_tokenizer_property_def_free(prop);
EINA_LIST_FREE(kls->methods, meth)
_eo_tokenizer_method_def_free(meth);
free(kls);
}
void
eo_tokenizer_free(Eo_Tokenizer *toknz)
{
Eo_Class_Def *kls;
if (toknz->source)
eina_stringshare_del(toknz->source);
EINA_LIST_FREE(toknz->classes, kls)
_eo_tokenizer_class_def_free(kls);
free(toknz);
}
|