ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libBASS/BASSlex.l
Revision: 377
Committed: Fri Mar 21 17:42:12 2003 UTC (21 years, 3 months ago) by mmeineke
Original Path: branches/mmeineke/OOPSE/libBASS/BASSlex.l
File size: 9225 byte(s)
Log Message:
New OOPSE Tree

File Contents

# User Rev Content
1 mmeineke 377
2     /* define some search patterns */
3    
4     digit [0-9]
5     letter [A-Za-z]
6     natural {digit}+
7     signedNat ("+"|"-")?{natural}
8     number {signedNat}("."{natural})?([Ee]{signedNat})?
9     identifier ("_"|{letter})({letter}|{digit}|"-"|"_")*
10    
11     /* define start states */
12    
13     %x PRE_P
14     %x INCL
15     %x DEF
16     %x IFDEF
17     %x IFNDEF
18     %x ESCAPE
19     %x CHECK_ESCAPE
20    
21     /* what to put at the top of the lex code */
22    
23     %{
24     #include <stdio.h>
25     #include <string.h>
26    
27     #include <BASSyacc.h>
28     #include <BASS_parse.h>
29     #include <simError.h>
30     #ifdef IS_MPI
31     #define __is_lex__
32     #include <mpiBASS.h>
33     #endif
34    
35     typedef unsigned short int r_short;
36    
37     extern void change_in_file( FILE* in_file );
38    
39     // the following is used by the include start state
40    
41     #define MAX_BUFFER_DEPTH 10
42     YY_BUFFER_STATE buffer_stack[MAX_BUFFER_DEPTH]; // a stack of the include buffers
43     int buffer_stack_ptr = 0;
44     struct filename_list{
45     char my_name[300];
46     struct filename_list* next;
47     };
48     struct filename_list* yyfile_name;
49     struct filename_list* temp_yyfile_name;
50     int yylineno_stack[MAX_BUFFER_DEPTH];
51    
52    
53     // the following is a check against the define buffer length
54    
55     void check_def_buff( char* defined, int index );
56    
57     //these are used by the ifdef and ifndef statements
58    
59     int escape_stack_ptr = 0; //keeps track of the escape stack
60    
61    
62     %}
63    
64     %option yylineno
65    
66     %%
67    
68     {signedNat} {
69     yylval.i_val = atoi( yytext );
70     return INTEGER;
71     }
72    
73     {number} {
74     yylval.d_val = atof( yytext );
75     return DOUBLE;
76     }
77    
78     {identifier} {
79     int token;
80     token = res_word( yytext );
81    
82     if( token == DEFINED ){
83    
84     if( buffer_stack_ptr >= MAX_BUFFER_DEPTH ){
85     fprintf( stderr,
86     "Maximum buffer depth exceeded for %s.\n",
87     yytext );
88     exit(1);
89     }
90    
91     buffer_stack[buffer_stack_ptr] = YY_CURRENT_BUFFER;
92     buffer_stack_ptr++;
93    
94     yy_scan_string( get_definition( yytext ) );
95     }
96     else if( token ){
97     return token;
98     }
99     else{
100     yylval.s_ptr = strdup( yytext );
101     return IDENTIFIER;
102     }
103     }
104    
105     \".*\" {
106     /* little routine to strip off the quotes */
107    
108     u_short i;
109     i = 0; // index
110     while( yytext[i+1] != '\"' ){
111    
112     yytext[i] = yytext[i+1];
113     i++;
114     }
115     yytext[i] = '\0';
116     yylval.s_ptr = strdup( yytext );
117     return QUOTED_STRING;
118     }
119    
120     \[{natural}\] {
121     int index;
122     sscanf(yytext, "[%d]", &index);
123     yylval.i_val = index;
124     return ARRAY_INDEX;
125     }
126    
127     [ \t\n]+ /* ignore whitespace */;
128    
129     "/*" {
130     /* ignore comments */
131     u_short done;
132     char c;
133    
134     done = 0;
135     while( !done ){
136    
137     c = input();
138     while( c != '*' ){
139    
140     c = input();
141     }
142     while( c == '*' ){
143    
144     c = input();
145     }
146     if( c == '/' ) done = 1;
147     }
148     }
149    
150     "//".*\n /* ignore comments */;
151    
152     "#" BEGIN(PRE_P);
153    
154     . {
155     // pass everything else to yacc
156     return yytext[0];
157     }
158    
159     <PRE_P>"include" BEGIN(INCL);
160     <PRE_P>"define" BEGIN(DEF);
161     <PRE_P>"ifdef" BEGIN(IFDEF);
162     <PRE_P>"ifndef" BEGIN(IFNDEF);
163     <PRE_P>"endif" /* do nothing */;
164     <PRE_P>\n BEGIN(INITIAL);
165    
166     <INCL>[ \t]* /* eat white space */
167     <INCL>\".*\" {
168     char foo_name[300];
169    
170     // little routine to strip off the quotes
171    
172     u_short i;
173     i = 0; // index
174     while( yytext[i+1] != '\"' ){
175    
176     yytext[i] = yytext[i+1];
177     i++;
178     }
179    
180     yytext[i] = '\0';
181     strcpy( foo_name, yytext );
182    
183     // now we have the include file name
184    
185     if( buffer_stack_ptr >= MAX_BUFFER_DEPTH ){
186    
187     fprintf( stderr, "Includes nested too deeply\n" );
188     exit(1);
189     }
190    
191     buffer_stack[buffer_stack_ptr] = YY_CURRENT_BUFFER;
192     yylineno_stack[buffer_stack_ptr] = yylineno;
193     buffer_stack_ptr++;
194    
195     yyin = fopen( yytext, "r" );
196     if( yyin == NULL ){
197     fprintf( stderr, "Unable to include file %s\n", yytext );
198     exit(1);
199     }
200    
201     yy_switch_to_buffer( yy_create_buffer( yyin, YY_BUF_SIZE ) );
202     yylineno = 0;
203    
204     temp_yyfile_name = (struct filename_list* )malloc( sizeof( struct filename_list ) );
205     temp_yyfile_name->next = yyfile_name;
206     yyfile_name = temp_yyfile_name;
207     strcpy( yyfile_name->my_name, foo_name );
208    
209    
210     BEGIN(INITIAL);
211     }
212     <INCL>\n BEGIN(INITIAL);
213     <INCL>. /* ignore everything else */
214    
215     <DEF>[ \t]* /* eat white space */;
216     <DEF>{identifier} {
217     char c;
218     char definition[ DEFINED_BUFFER_SIZE ];
219     short int done;
220     short int c_done; // a done marker for the comments
221     int def_ptr;
222    
223     // initialize the definition buffer
224    
225     for( def_ptr = 0; def_ptr < DEFINED_BUFFER_SIZE; def_ptr++ ){
226     definition[def_ptr] = '\0';
227     }
228    
229     def_ptr =0;
230     done = 0;
231     c_done = 0;
232    
233     while( !done ){
234    
235     c = input();
236     if( c == '\"' ){
237    
238     // shove the whole quoted string into the macro
239    
240     definition[def_ptr] = c;
241     //fprintf( stderr, "%c", c );
242     def_ptr++;
243     check_def_buff( yytext, def_ptr );
244    
245     c = input();
246     while( c != '\"' ){
247    
248     definition[def_ptr] = c;
249     //fprintf( stderr, "%c", c );
250     def_ptr++;
251     check_def_buff( yytext, def_ptr );
252    
253     c = input();
254     }
255     definition[def_ptr] = c;
256     //fprintf( stderr, "%c", c );
257     def_ptr++;
258     check_def_buff( yytext, def_ptr );
259     c = input();
260     }
261    
262     // handle comments
263    
264     if( c == '/' ){
265     c = input();
266     switch( c ){
267    
268     case '/':
269     while( c != '\n' && c != '\\' ){
270     c = input();
271     }
272     break;
273    
274     case '*':
275     c_done = 0;
276     while( !c_done ){
277     c = input();
278     while( c != '*' ){
279     c = input();
280     }
281     while( c == '*' ){
282     c = input();
283     }
284     if( c == '/' ) c_done = 1;
285     }
286     c = input();
287     break;
288    
289     default:
290     // the '/' char was a normal symbol
291     definition[def_ptr] = '/';
292     //fprintf( stderr, "%c", c );
293     def_ptr++;
294     check_def_buff( yytext, def_ptr );
295     break;
296     }
297     }
298    
299    
300     if( c == '\n' ){
301     done = 1;
302     }
303    
304     else{
305    
306     // check for the line wrap character '\'
307    
308     if( c == '\\' ){
309    
310     // skip the rest of the line until the line return
311    
312     c = input();
313     while( c != '\n' ){
314     c = input();
315     }
316     }
317    
318     else{
319    
320     // we now know the character is a good one
321    
322     definition[def_ptr] = c;
323     //fprintf( stderr, "%c", c );
324     def_ptr++;
325     check_def_buff( yytext, def_ptr );
326     }
327     }
328     }
329    
330     insert_define( yytext, definition );
331     BEGIN(INITIAL);
332     }
333     <DEF>\n BEGIN(INITIAL);
334     <DEF>. /* ignore everything else */;
335    
336     <IFDEF>[ \t]* /* eat white space */;
337     <IFDEF>{identifier} {
338     if( !is_defined( yytext ) ){
339     escape_stack_ptr++;
340     BEGIN(ESCAPE);
341     }
342     }
343     <IFDEF>\n BEGIN(INITIAL);
344     <IFDEF>. /* ignore everything else */;
345    
346     <IFNDEF>[ \t]* /* eat the white space */;
347     <IFNDEF>{identifier} {
348     if( is_defined( yytext ) ){
349     escape_stack_ptr++;
350     BEGIN(ESCAPE);
351     }
352     }
353     <IFNDEF>\n BEGIN(INITIAL);
354     <IFNDEF>. /* ignore everything else */;
355    
356     <ESCAPE>\".*\" /* do nothing */;
357     <ESCAPE>"//".*\n /* ignore comments */;
358     <ESCAPE>"/*" {
359     /* ignore comments */
360     u_short done;
361     char c;
362    
363     done = 0;
364     while( !done ){
365    
366     c = input();
367     while( c != '*' ){
368    
369     c = input();
370     }
371    
372     while( c == '*' ){
373    
374     c = input();
375     }
376    
377     if( c == '/' ) done = 1;
378     }
379     }
380     <ESCAPE>"#" BEGIN(CHECK_ESCAPE);
381     <ESCAPE>. /* ignore everything else */;
382    
383     <CHECK_ESCAPE>[ \t] /* ignore whitespace */;
384     <CHECK_ESCAPE>"ifdef" { escape_stack_ptr++; BEGIN(ESCAPE); }
385     <CHECK_ESCAPE>"ifndef" { escape_stack_ptr++; BEGIN(ESCAPE); }
386     <CHECK_ESCAPE>"endif" {
387     escape_stack_ptr--;
388     if( escape_stack_ptr <= 0){
389     escape_stack_ptr = 0; // just in case something flubbed
390     BEGIN(INITIAL);
391     }
392     }
393     <CHECK_ESCAPE>\n BEGIN(ESCAPE);
394     <CHECK_ESCAPE>. /* ignore everything else */;
395    
396     <<EOF>> {
397     buffer_stack_ptr--;
398     if( buffer_stack_ptr < 0 ){
399    
400     yyterminate();
401     }
402    
403     else{
404    
405     yy_delete_buffer( YY_CURRENT_BUFFER );
406     yy_switch_to_buffer( buffer_stack[buffer_stack_ptr] );
407     yylineno = yylineno_stack[buffer_stack_ptr];
408    
409     temp_yyfile_name = yyfile_name;
410     yyfile_name = temp_yyfile_name->next;
411     free( temp_yyfile_name );
412     }
413     }
414    
415     %%
416    
417     void check_def_buff( char* defined, int index ){
418    
419     if( index >= DEFINED_BUFFER_SIZE ){
420    
421     sprintf( painCave.errMsg, "Define buffer size exceeded for %s\n", defined );
422     painCave.isFatal = 1;
423     simError();
424     }
425     }
426    
427     int yywrap(void){
428     return 1;
429     }
430    
431     void change_in_file( FILE* in_file ){
432    
433     yyin = in_file;
434     }