summaryrefslogtreecommitdiff
path: root/examples/manual/pas_include.lex
blob: 58cf5903201435ed36c707346be94645cd016b3e (plain)
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
/*
 * eof_rules.lex : An example of using multiple buffers
 *                 EOF rules, and start states
 */

%{
                              
#define MAX_NEST 10                   

YY_BUFFER_STATE include_stack[MAX_NEST];
int             include_count = -1;

%}


%x INCLUDE
%x COMMENT


%%

"{"                          BEGIN(COMMENT);

<COMMENT>"}"                 BEGIN(INITIAL); 
<COMMENT>"$include"[ \t]*"(" BEGIN(INCLUDE);
<COMMENT>[ \t]*              /* skip whitespace */

<INCLUDE>")"                 BEGIN(COMMENT); 
<INCLUDE>[ \t]*              /* skip whitespace */
<INCLUDE>[^ \t\n() ]+ {      /* get the include file name */
          if ( include_count >= MAX_NEST){
             fprintf( stderr, "Too many include files" );
             exit( 1 );
          }

          include_stack[++include_count] = YY_CURRENT_BUFFER;

          yyin = fopen( yytext, "r" );
          if ( ! yyin ){
             fprintf( stderr, "Unable to open %s",yytext);
             exit( 1 );
          }

          yy_switch_to_buffer(yy_create_buffer(yyin,YY_BUF_SIZE));

          BEGIN(INITIAL);
        }
<INCLUDE><<EOF>> 
        {
            fprintf( stderr, "EOF in include" );
            yyterminate();
        }
<COMMENT><<EOF>> 
        {
            fprintf( stderr, "EOF in comment" );
            yyterminate();
        }
<<EOF>> {
          if ( include_count <= 0 ){
            yyterminate();
          } else {
            yy_delete_buffer(include_stack[include_count--] );
            yy_switch_to_buffer(include_stack[include_count] );
            BEGIN(INCLUDE);
          }
        }
[a-z]+               ECHO;
.|\n                 ECHO;