-
Notifications
You must be signed in to change notification settings - Fork 2
/
cfunctions.fl
438 lines (287 loc) · 11.1 KB
/
cfunctions.fl
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/* Lexer rules for cfunctions. */
%{
#include "error-msg.h"
#include "argument.h"
#include "wt.h"
struct pf pf;
#define FLEX
#include "flex.h"
#undef FLEX
const char * state_message (void);
void parser_push_state (int);
void parser_pop_state ();
/* Defining this macro stops a compiler warning about an unused
function called "input" or "yyinput". */
#define YY_NO_INPUT
%}
/* See the Flex manual */
%option noyywrap
/* The following options save us from getting some warning messages
from compiling with `gcc -Wall'. */
%option nounput noyy_top_state
/* Cfunctions uses a stack for various things - see the instances of
`parser_push_state' and `parser_pop_state' below. */
%option stack
/* ____ _ _
/ ___|| |_ __ _| |_ ___ ___
\___ \| __/ _` | __/ _ \/ __|
___) | || (_| | || __/\__ \
|____/ \__\__,_|\__\___||___/ */
/* state "in a C comment" */
%x comment
/* State "in a C function". Because the only thing the "function"
state does is skips strings and comments, and counts "{" and "}",
and then exits when there have been enough }'s, the "function"
state can also be used for such things as "inside a struct
definition" and "inside an initialiser". */
%x function
/* State "in a C function's argument list". */
%x arguments
/* State "parsing a pointer to a function". */
%x func_ptr
/* State "parsing arguments to a pointer to a function". */
%x func_ptr_arg
/* state "parsing a print format instruction" */
%x print_format
/* State "in a C string". Although Cfunctions is not interested in
strings, this state is necessary in order to jump over any curly
braces in strings that might confuse the curly braces depth
counter, and to prevent clashes with comment-like objects in
strings. */
%x c_string
/* State "in a C preprocessor statement". This includes multiline
statements which are matched with an appropriate regular
expression. */
%x in_cpp
/* State "in a struct definition". For example `a' in
`struct a { int x, int y };'. */
%x in_struct
/* State "in an initialiser". For example `10' in `int x = 10;'. */
%x initialiser
/* State "in an enum definition". For example `b' in
`enum b { y, z };'. */
%x in_enum
/* State "is an enum", For example `c' in `enum x { c };'. */
%x is_enum
/* State in "traditional C variable declarations". For example
`int x' in `char * func (x) int x {'. */
%x traditional
/* Macros for C preprocessor. */
endline \r?\n
/* The `\\{endline}' is to deal with multiline cpp statements. */
cpp_space ([ \t\f\r]|\\{endline})+
cpp_opt_space ([ \t\f\r]|\\{endline})*
cpp_any (.|\\{endline})
cpp ^{cpp_opt_space}#{cpp_opt_space}
/* ____
/ ___| _ __ ___ __ _ ___ _ __ ___ ___
| | | '_ ` _ \ / _` |/ __| '__/ _ \/ __|
| |___ | | | | | | (_| | (__| | | (_) \__ \
\____| |_| |_| |_|\__,_|\___|_| \___/|___/ */
/* The macro c_space has been set to match only a single `\n' in order
to prevent collisions with {cpp}. I don't know if there is
anywhere in the rules that expects {c_space} to match more than one
`\n' - this might be a problem. */
c_space ([ \t\f\r]+|\n)
c_opt_space [ \t\f\r\n]*
c_word [A-Za-z_][A-Za-z_0-9]*
c_array \[[^\][]*\]
c_var (\*{c_opt_space})*{c_word}({c_opt_space}{c_array})*
c_var_no_pointer {c_word}({c_opt_space}{c_array})*
c_func_ptr \({c_opt_space}\**{c_opt_space}{c_word}{c_opt_space}\)
braces \{[^{}]*\}
line_directive line{cpp_opt_space}[0-9]+{cpp_opt_space}\"([^\"]|\\\")+\"
%%
/* C preprocessor conditional statements. */
<*>{cpp}if{cpp_any}*$ cpp_add (cfp, yytext, CPP_IF);
<*>{cpp}else{cpp_any}*$ cpp_add (cfp, yytext, CPP_ELSE);
<*>{cpp}elif{cpp_any}*$ cpp_add (cfp, yytext, CPP_ELIF);
<*>{cpp}endif{cpp_any}*$ cpp_add (cfp, yytext, CPP_ENDIF);
/* Other C preprocessor statements. */
<*>{cpp}define{cpp_space}{c_word} { do_define (ARGS); }
<*>{cpp}{line_directive} line_change (ARGS);
{cpp} do_start_cpp (ARGS);
<in_cpp>{cpp_any} inline_print (ARGS);
<in_cpp>{endline} { inline_print (ARGS); parser_pop_state (); }
/* Comments. */
<*>"/"(\\{endline})?"*" {
/* One important exception: "/ *" in strings is a
part of a string, not a comment. This only
occurs for the case that the string is exactly
"/ *". */
if (YY_START != c_string) {
parser_push_state (comment);
}
else {
/* Don't need a warning. */
inline_print (ARGS);
}
}
<comment>\*+\/ parser_pop_state ();
<comment>(([^\*]|\n)*|\*+([^/\*]|\n)) ;
<INITIAL,function>"/"(\\{endline})?"/".* {
/* This could foul up on some cases. For example,
"a // * comment * /b" (a space was added between
* and / here for obvious reasons). */;
}
typedef(\{(\{(\{({braces}|[^}{])*\}|{braces}|[^}{])*\}|{braces}|[^}{])*\}|{braces}|[^}{;])*; {
do_copy_typedef (ARGS);
}
/* Type definitions. */
typedef { do_typedef (ARGS); }
/* Enumerations. */
enum {
parser_push_state (in_enum);
function_save (ARGS);
}
<in_enum>{c_word} { do_word (ARGS); }
<in_enum>\{ {
external_clear (cfp);
BEGIN (is_enum);
}
<in_enum>{c_space} count_lines (ARGS);
<is_enum>{c_word}[^\},;]* {
inline_print (ARGS);
}
<is_enum>(,|{c_space}) inline_print (ARGS);
<is_enum>\} {
inline_print (ARGS);
parser_pop_state ();
}
<is_enum>; line_error("`;' in enum list");
/* Structures and unions. */
(struct|union) { /* A `struct' is just the same
syntactically as a `union'. */
function_save (ARGS);
parser_push_state (in_struct);
}
<in_struct>{c_word} { do_word (ARGS); }
<in_struct>\* {
function_save (ARGS);
parser_pop_state (cfp);
}
<in_struct>; {
/* This state occurs in incomplete forward
declaration of a struct. */
forward_print (cfp, ";\n");
BEGIN (INITIAL);
}
<in_struct>{c_space} ;
<in_struct>\{ {
brace_open (cfp);
external_clear (cfp);
BEGIN (function);
}
/* Initialisers (these begin with `='). Cfunctions skips over
initialisers, because they don't need to be copied into header files. */
<initialiser>; {
external_print (cfp, ";\n");
parser_pop_state (cfp);
}
<initialiser>, {
current_arg = arg_share (current_arg);
parser_pop_state (cfp);
}
<initialiser>\{ {
brace_open (cfp);
parser_push_state (function);
}
<initialiser>\([^\)]*\) {
/* This is to jump comma operators. In practice it is very unlikely
that we ever see comma operators. Comma operators are for using
side effects. There can be no side effects in constant
expressions, which are the only expressions allowed outside
functions. But that is the only place Cfunctions will ever
encounter them. */
}
<initialiser>([^\"]|\n) {
/* Discard contents of initialiser. */
}
\({c_opt_space}{c_opt_space}\) do_arguments (ARGS);
\({c_opt_space}void{c_opt_space}\) do_void_arguments (ARGS);
/* Function pointers. At the moment this works for inline versions
of them but not for several other cases. */
\({c_opt_space}\*{c_opt_space} {
inline_print (ARGS);
parser_push_state (func_ptr);
}
<func_ptr>\){c_opt_space}\( {
inline_print (ARGS);
BEGIN (func_ptr_arg);
}
<func_ptr>\) {
inline_print (ARGS);
parser_pop_state ();
}
<func_ptr>{c_word} do_function_pointer (ARGS);
<func_ptr>[^\)]+ do_function_pointer (ARGS);
<func_ptr_arg>\) {
do_function_pointer_argument (ARGS);
parser_pop_state ();
}
<func_ptr_arg>\( {
do_function_pointer_argument (ARGS);
parser_push_state (func_ptr_arg);
}
<func_ptr_arg>[^\(\)]+ {
do_function_pointer_argument (ARGS);
}
/* Beginning of a C argument. */
\( {
do_start_arguments (cfp);
BEGIN(arguments);
}
<arguments>\) do_arguments_close_bracket (ARGS);
<arguments>\( do_arguments_open_bracket (ARGS);
<arguments>({c_var}|\*+) argument_save (ARGS);
<arguments>, argument_next (cfp);
<arguments>{c_space} ;
<arguments>"..." argument_save (ARGS);
<arguments>([0-9]*|\"([^\"]|\\\")*\") line_error ("unparseable macro");
<INITIAL>\{ {
start_function (cfp);
}
<INITIAL>__attribute__\(\(.*\)\) {
}
\} line_warning ("isolated }");
<function>\{ {
inline_print (ARGS);
brace_open (cfp);
}
<function>\} do_brace_close (cfp);
<*>\" start_quote (ARGS, YY_START);
<c_string>(\\[^\"])* inline_print (ARGS);
<c_string>(\\.|[^\\"])* inline_print (ARGS);
<function>([^\}\{\"]|\n|\'\\\"\'|\'\"\') inline_print (ARGS);
<function>(\'\{\'|\'\}\') do_escaped_brace (ARGS);
extern do_extern (ARGS);
\[[^]]*\] do_array_arguments (ARGS);
\; do_semicolon (cfp);
= do_equals (cfp);
\, do_comma (cfp);
main{c_opt_space}\([^\{]+\{ do_main (ARGS);
static do_static (ARGS);
void do_void (ARGS);
NO_RETURN do_NO_RETURN (ARGS);
/* Parse our print format declaration. */
PRINT_FORMAT {
check_extensions (cfp);
BEGIN (print_format);
}
<print_format>{c_opt_space} ;
<print_format>\( pf.index = 0;
<print_format>, pf.index++;
<print_format>\) do_PRINT_FORMAT (cfp);
<print_format>[1-9][0-9]* do_print_format_argument (ARGS, & pf);
void{c_opt_space}\*+ do_void_pointer (ARGS);
{c_word} function_save (ARGS);
\* function_save (ARGS);
{c_space} {
count_lines (ARGS);
}
<*>. {
line_warning ("unknown char `%c' while in %s state",
yytext[0], state_message ());
}
%%
#include "flex.c"