-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.l
296 lines (245 loc) · 4.84 KB
/
scan.l
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
/* Scanner for GOB2 Documentations */
%{
#include <stdbool.h>
#include <ctype.h>
#include <errno.h>
#include <error.h>
#define CHAR_BUFFER_LENGTH 255
//#define D foobar
#ifdef D
#define DEBUG(msg, ...) fprintf(stdout, msg"\n", ##__VA_ARGS__)
#else
#define DEBUG(msg, ...)
#endif
extern int errno;
static int depth = -1;
static char my_class[CHAR_BUFFER_LENGTH] = {0};
static char my_func_prefix[CHAR_BUFFER_LENGTH] = {0};
static char current_property[CHAR_BUFFER_LENGTH] = {0};
static void print_class_with_namespace(char *classname){
char copy[CHAR_BUFFER_LENGTH] = {0};
strncpy(copy, classname, CHAR_BUFFER_LENGTH);
char *p = strtok(copy, ":");
printf("%s", p);
while(p){
p = strtok(NULL, ":");
if( p )
printf("::%s", p);
}
}
#ifdef FLEX_SCANNER
int yylex(void);
int yywrap(void) { return 1; }
#endif
%}
%x IN_CLASS_DEF
%x IN_CLASS
%x IN_METHOD_DEF
%x IN_METHOD
%x IN_METHOD_ARGUMENTS
%x IN_METHOD_ARGUMENT_CHECK
%x AFTER_METHOD_ARGUMENTS
%x IN_PROPERTY_DEF
%x IN_PROPERTY
%x IN_PROPERTY_SETGET
%x IN_VARIABLE_DEF
%x IN_VARIABLE
%x IN_GLOBAL_COMMENT
%x IN_COMMENT
%x IN_C_CODE
%x IN_METHOD_OR_VARIABLE_START
WS [ \t\n]
TYPE [a-zA-Z0-9]+(:[a-zA-Z0-9]+)*
CLASSNAME [a-zA-Z0-9]+(:[a-zA-Z0-9]+)+
FUNCNAME [a-zA-Z0-9_]+
%%
^{WS}*\%[a-z]*{WS}*\{ {
BEGIN(IN_C_CODE);
}
<IN_C_CODE>^{WS}*\%[a-z]*{WS}*\} {
BEGIN(INITIAL);
}
<IN_C_CODE>.* { ; /*ignore*/ }
class {
DEBUG("FOUND CLASS DEFINITION");
printf("class");
BEGIN(IN_CLASS_DEF);
}
<IN_CLASS_DEF>from {
printf(":");
}
<IN_CLASS_DEF>{CLASSNAME} {
if( strlen(my_class) <= 0 ){
strncpy(my_class, yytext, CHAR_BUFFER_LENGTH);
DEBUG("FOUND MAIN CLASS '%s'", my_class);
}
print_class_with_namespace(yytext);
}
<IN_CLASS_DEF>\({WS}*abstract{WS}*\) {
; //ignore
}
<IN_CLASS_DEF>\{ {
printf("{\n");
DEBUG("FOUND CLASS");
BEGIN(IN_CLASS);
}
<IN_CLASS>(constructor|init|dispose|finalize){WS}*\({WS}*self{WS}*\){WS}*\{ {
DEBUG("FOUND INTERNAL METHOD: %s", yytext);
BEGIN(IN_METHOD);
depth = 0;
}
<IN_CLASS>property {
DEBUG("FOUND PROPERTY");
BEGIN(IN_PROPERTY);
depth = 0;
}
\/\*\* {
DEBUG("FOUND GLOBAL COMMENT");
printf("/**");
BEGIN(IN_GLOBAL_COMMENT);
}
<IN_CLASS>\/\*\* {
DEBUG("FOUND COMMENT");
printf("/**");
BEGIN(IN_COMMENT);
}
<IN_COMMENT,IN_GLOBAL_COMMENT>{CLASSNAME} {
DEBUG("FOUND CLASSNAME IN COMMENT '%s'", yytext);
print_class_with_namespace(yytext);
}
<IN_GLOBAL_COMMENT>\*\/ {
BEGIN(INITIAL);
printf("*/");
DEBUG("END GLOBAL COMMENT");
}
<IN_COMMENT>\*\/ {
BEGIN(IN_CLASS);
printf("*/");
DEBUG("END CLASS COMMENT");
}
<IN_PROPERTY>\( {
BEGIN(IN_PROPERTY_DEF);
DEBUG("FOUND PROPERTY DEFINITION");
depth = 0;
}
<IN_PROPERTY_DEF>\( {
DEBUG("FOUND PROPERTY DEFINITION");
depth++;
}
<IN_PROPERTY_DEF>\) {
depth--;
if( depth < 0 ){
DEBUG("END PROPERTY DEFINITION");
BEGIN(IN_PROPERTY);
}
}
<IN_PROPERTY_DEF>[^\(\)] { ; }
<IN_PROPERTY>\{ {
BEGIN(IN_PROPERTY_SETGET);
DEBUG("STARTING SETTER AND GETTER FOR PROPERTY");
depth = 0;
}
<IN_PROPERTY_SETGET>\{ {
depth++;
}
<IN_PROPERTY_SETGET>\} {
depth--;
if( depth < 0 ){
BEGIN(IN_PROPERTY);
DEBUG("END SETTER AND GETTER FOR PROPERTY");
}
}
<IN_PROPERTY_SETGET>[^\{\}]+ { ; };
<IN_PROPERTY>\; {
DEBUG("END PROPERTY");
BEGIN(IN_CLASS);
}
<IN_CLASS>(private|public|protected) {
printf("%s: ", yytext);
BEGIN(IN_METHOD_OR_VARIABLE_START);
}
<IN_METHOD_OR_VARIABLE_START>{TYPE} {
DEBUG("FOUND TYPE OF METHOD OR PROPERTY '%s'", yytext);
print_class_with_namespace(yytext);
}
<IN_METHOD_OR_VARIABLE_START>"=" {
BEGIN(IN_VARIABLE);
DEBUG("FOUND START OF VARIABLE");
depth = 0;
}
<IN_VARIABLE>\{ {
depth++;
}
<IN_VARIABLE>\} {
depth--;
}
<IN_VARIABLE>; {
if( depth <= 0 ){
printf(";");
BEGIN(IN_CLASS);
}
}
<IN_PROPERTY>\}{WS}*; {
DEBUG("FOUND END OF PROPERTY");
printf(";");
BEGIN(IN_CLASS);
}
<IN_PROPERTY,IN_VARIABLE>. {
; // Ignore
}
<IN_METHOD_OR_VARIABLE_START>{FUNCNAME} {
DEBUG("FOUND FUNCTION NAME '%s'", yytext);
printf(yytext);
}
<IN_METHOD_OR_VARIABLE_START>\( {
DEBUG("FOUND METHOD ARGUMENT START");
printf("(");
BEGIN(IN_METHOD_ARGUMENTS);
depth = 0;
}
<IN_METHOD_ARGUMENTS>self {
print_class_with_namespace(my_class);
printf(" *self");
}
<IN_METHOD_ARGUMENTS>{TYPE} {
print_class_with_namespace(yytext);
}
<IN_METHOD_ARGUMENTS>\({WS}*check {
BEGIN(IN_METHOD_ARGUMENT_CHECK);
}
<IN_METHOD_ARGUMENT_CHECK>\) {
BEGIN(IN_METHOD_ARGUMENTS);
}
<IN_METHOD_ARGUMENT_CHECK>[^\)]+ { ; }
<IN_METHOD_ARGUMENTS>\) {
printf(")");
BEGIN(AFTER_METHOD_ARGUMENTS);
}
<AFTER_METHOD_ARGUMENTS>\{ {
printf(";");
BEGIN(IN_METHOD);
depth = 0;
}
<AFTER_METHOD_ARGUMENTS>[^\{]+ {
;
}
<IN_METHOD>\{ {
depth++;
}
<IN_METHOD>\} {
depth--;
if( depth < 0 ){
DEBUG("FOUND END OF METHOD");
BEGIN(IN_CLASS);
}
}
<IN_METHOD>[^\{\}]+ { ; }
%%
int main( int argc, char **argv ){
++argv, --argc; /* skip over program name */
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;
yylex();
}