xref: /minix3/external/bsd/flex/dist/tests/test-include-by-buffer/scanner.l (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*357f1050SThomas Veerman /*
2*357f1050SThomas Veerman  * This file is part of flex.
3*357f1050SThomas Veerman  *
4*357f1050SThomas Veerman  * Redistribution and use in source and binary forms, with or without
5*357f1050SThomas Veerman  * modification, are permitted provided that the following conditions
6*357f1050SThomas Veerman  * are met:
7*357f1050SThomas Veerman  *
8*357f1050SThomas Veerman  * 1. Redistributions of source code must retain the above copyright
9*357f1050SThomas Veerman  *    notice, this list of conditions and the following disclaimer.
10*357f1050SThomas Veerman  * 2. Redistributions in binary form must reproduce the above copyright
11*357f1050SThomas Veerman  *    notice, this list of conditions and the following disclaimer in the
12*357f1050SThomas Veerman  *    documentation and/or other materials provided with the distribution.
13*357f1050SThomas Veerman  *
14*357f1050SThomas Veerman  * Neither the name of the University nor the names of its contributors
15*357f1050SThomas Veerman  * may be used to endorse or promote products derived from this software
16*357f1050SThomas Veerman  * without specific prior written permission.
17*357f1050SThomas Veerman  *
18*357f1050SThomas Veerman  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19*357f1050SThomas Veerman  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20*357f1050SThomas Veerman  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21*357f1050SThomas Veerman  * PURPOSE.
22*357f1050SThomas Veerman  */
23*357f1050SThomas Veerman 
24*357f1050SThomas Veerman %{
25*357f1050SThomas Veerman /* A scanner file to build "scanner.c".
26*357f1050SThomas Veerman    Input language is any text.
27*357f1050SThomas Veerman    "#include <filename>" causes a buffer switch.
28*357f1050SThomas Veerman  */
29*357f1050SThomas Veerman #include <stdio.h>
30*357f1050SThomas Veerman #include <stdlib.h>
31*357f1050SThomas Veerman #include "config.h"
32*357f1050SThomas Veerman %}
33*357f1050SThomas Veerman 
34*357f1050SThomas Veerman %option 8bit outfile="scanner.c" prefix="test"
35*357f1050SThomas Veerman %option nounput nomain noyywrap
36*357f1050SThomas Veerman %option warn
37*357f1050SThomas Veerman 
38*357f1050SThomas Veerman %x GET_FILENAME
39*357f1050SThomas Veerman %{
40*357f1050SThomas Veerman 
41*357f1050SThomas Veerman #define MAX_INCLUDE_DEPTH 10
42*357f1050SThomas Veerman YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
43*357f1050SThomas Veerman int include_stack_ptr = 0;
44*357f1050SThomas Veerman %}
45*357f1050SThomas Veerman %%
46*357f1050SThomas Veerman 
47*357f1050SThomas Veerman <INITIAL>{
48*357f1050SThomas Veerman ^"#include"[[:blank:]]+"<"  { BEGIN(GET_FILENAME); }
49*357f1050SThomas Veerman .|\n      { ECHO; }
50*357f1050SThomas Veerman }
51*357f1050SThomas Veerman 
52*357f1050SThomas Veerman <GET_FILENAME>{
53*357f1050SThomas Veerman [[:alnum:]_.-]+>  {
54*357f1050SThomas Veerman      /* recurse */
55*357f1050SThomas Veerman     yytext[yyleng-1]='\0';
56*357f1050SThomas Veerman     include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
57*357f1050SThomas Veerman     if((yyin=fopen(yytext,"r"))==NULL) {
58*357f1050SThomas Veerman         fprintf(stderr,"*** Error: Could not open include file \"%s\".\n",yytext);
59*357f1050SThomas Veerman         yyterminate();
60*357f1050SThomas Veerman     }
61*357f1050SThomas Veerman     yy_switch_to_buffer( yy_create_buffer( yyin, YY_BUF_SIZE ));
62*357f1050SThomas Veerman     BEGIN(0);
63*357f1050SThomas Veerman     }
64*357f1050SThomas Veerman .|\n  {
65*357f1050SThomas Veerman     fprintf(stderr,"Invalid input \"%s\".\n", yytext);
66*357f1050SThomas Veerman     yyterminate();
67*357f1050SThomas Veerman    }
68*357f1050SThomas Veerman }
69*357f1050SThomas Veerman 
70*357f1050SThomas Veerman <<EOF>> {
71*357f1050SThomas Veerman        if ( --include_stack_ptr < 0 ) {
72*357f1050SThomas Veerman            yyterminate();
73*357f1050SThomas Veerman        }
74*357f1050SThomas Veerman        else {
75*357f1050SThomas Veerman             fclose(yyin);
76*357f1050SThomas Veerman            yy_delete_buffer( YY_CURRENT_BUFFER );
77*357f1050SThomas Veerman            yy_switch_to_buffer( include_stack[include_stack_ptr] );
78*357f1050SThomas Veerman        }
79*357f1050SThomas Veerman    }
80*357f1050SThomas Veerman 
81*357f1050SThomas Veerman %%
82*357f1050SThomas Veerman 
83*357f1050SThomas Veerman int main(int argc, char** argv);
84*357f1050SThomas Veerman 
85*357f1050SThomas Veerman int
main(int argc,char ** argv)86*357f1050SThomas Veerman main ( int argc, char** argv )
87*357f1050SThomas Veerman {
88*357f1050SThomas Veerman     FILE * fp;
89*357f1050SThomas Veerman     if( argc != 2 ) {
90*357f1050SThomas Veerman         fprintf(stderr,"*** Error: Must specifiy one filename.\n");
91*357f1050SThomas Veerman         exit(-1);
92*357f1050SThomas Veerman     }
93*357f1050SThomas Veerman     if((fp=fopen(argv[1],"r"))==NULL) {
94*357f1050SThomas Veerman         fprintf(stderr,"*** Error: fopen(%s) failed.\n",argv[1]);
95*357f1050SThomas Veerman         exit(-1);
96*357f1050SThomas Veerman     }
97*357f1050SThomas Veerman     yyin = fp;
98*357f1050SThomas Veerman     yyout = stdout;
99*357f1050SThomas Veerman     yylex();
100*357f1050SThomas Veerman     printf("TEST RETURNING OK.\n");
101*357f1050SThomas Veerman     return 0;
102*357f1050SThomas Veerman }
103