]> git.mxchange.org Git - flightgear.git/blob - Scenery/scanner.l
Added GNU copyright headers.
[flightgear.git] / Scenery / scanner.l
1 /**************************************************************************
2  * scenery.l -- Lexical Analyzer for scenery files
3  *
4  * Written by Curtis Olson, started May 1997.
5  *
6  * Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * NOTE: Compiles with flex and gcc.
23  *
24  * $Id$
25  **************************************************************************/
26
27
28 /* C Pass Through */
29 %{
30     #include <stdio.h>
31     #include "parser.h"
32
33     int line_num = 1;
34     char c;
35
36     /* custom print routine */
37     static int scanner_debug = 0;
38
39     /* Routines to manage a stack of nested input buffers (for
40        processing the #include directive */
41     #define MAX_INCLUDE_DEPTH 10
42     static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
43     static int include_stack_ptr = 0;
44
45     int push_input_stream ( char *input ) { 
46         FILE *yyin_save;
47         if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) {
48             fprintf( stderr, "Scanner says:  Includes nested too deeply\n" );
49             return(0);
50         }
51
52         /* save yyin in case the following fails */
53         yyin_save = yyin;
54
55         yyin = fopen( input, "r" );
56         if ( ! yyin ) {
57             fprintf( stderr, "Scanner says:  cannot open '%s'\n", input );
58
59             /* The failed attempt destroyed yyin, so restore it */
60             yyin = yyin_save;
61
62             return(0);
63         }
64  
65         include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
66         yy_switch_to_buffer( yy_create_buffer(yyin, YY_BUF_SIZE) );
67
68         return(1);
69     }
70
71     int pop_input_stream () { 
72         if ( --include_stack_ptr < 1 ) {
73             /* end of last input stream */
74             return(0);
75         } else {
76             /* end of current input stream, restore previous and continue */
77             yy_delete_buffer( YY_CURRENT_BUFFER );
78             yy_switch_to_buffer( include_stack[include_stack_ptr] );
79             return(1);
80         }
81     }
82 %}
83
84
85 /* Definitions */
86 letter          [A-Za-z]
87 digit           [0-9]
88 connecter       [_-]
89 ident           {letter}({letter}|{digit}|{connecter})*
90
91 integer         [+-]?{digit}+
92
93 plain_real      {integer}"."{integer}
94 short_exp_real  {integer}[Ee][+-]?{integer}
95 exp_real        {plain_real}[Ee][+-]?{integer}
96 real            [+-]?{plain_real}|{short_exp_real}|{exp_real}
97
98 number          {real}|{integer}
99
100 string          \"[^"\n]+\"
101
102 bad_string      \"([^"\n]|\n)+\"
103
104 ws              [ \t]+
105 other           .
106
107
108 /* Rules */ 
109 %%
110
111 include         { if ( scanner_debug ) {
112                       printf("return IncludeSym\n");
113                   }
114                   return IncludeSym;
115                 }
116
117 mesh            { if ( scanner_debug ) {
118                       printf("return MeshSym\n");
119                   }
120                   return MeshSym;
121                 }
122
123 row             { if ( scanner_debug ) {
124                       printf("return RowSym\n");
125                   }
126                   return RowSym;
127                 }
128
129 chunk           { if ( scanner_debug ) {
130                       printf("return ChunkSym\n");
131                   }
132                   return ChunkSym;
133                 }
134
135 bounds          { if ( scanner_debug ) {
136                       printf("return BoundsSym\n");
137                   }
138                   return BoundsSym;
139                 }
140
141 place           { if ( scanner_debug ) {
142                       printf("return PlaceSym\n");
143                   }
144                   return PlaceSym;
145                 }
146
147 {ident}         { if ( scanner_debug ) {
148                       printf("return Identifier = %s\n", yytext);
149                   }
150                   return Identifier;
151                 }
152
153 {number}        { if ( scanner_debug ) {
154                       printf("return Number\n");
155                   }
156                   return Number;
157                 }
158
159 {string}        { if ( scanner_debug ) {
160                       printf("return StringLiteral = %s\n", yytext);
161                   }
162                   return StringLiteral;
163                 }
164
165 {bad_string}    { if ( scanner_debug ) {
166                       printf("return BadStringLiteral = %s\n", yytext);
167                   }
168                   return BadStringLiteral; 
169                 }
170
171 "\n"            { line_num++; 
172                   if ( scanner_debug ) {
173                       printf("Line number = %d\n", line_num);
174                   }
175                }
176
177 "#"             { if ( scanner_debug ) {
178                       printf("return HashSym\n");
179                   }
180                   return HashSym; 
181                 }
182
183 "="             { if ( scanner_debug ) {
184                       printf("return EqualSym\n");
185                   }
186                   return EqualSym; 
187                 }
188
189 ","             { if ( scanner_debug ) {
190                       printf("return CommaSym\n");
191                   }
192                   return CommaSym; 
193                 }
194
195 "{"             { if ( scanner_debug ) {
196                       printf("return LBraceSym\n");
197                   }
198                   return LBraceSym; 
199                 }
200
201 "}"             { if ( scanner_debug ) {
202                       printf("return RBraceSym\n");
203                   }
204                   return RBraceSym;
205                 }
206
207 "("             { if ( scanner_debug ) {
208                       printf("return LParenSym\n");
209                   }
210                   return LParenSym; 
211                 }
212
213 ")"             { if ( scanner_debug ) {
214                       printf("return RParenSym\n");
215                   }
216                   return RParenSym;
217                 }
218
219 "//"            { /* burn through "#" comment */ 
220                   c = input();
221                   while ( (c != '\n') ) {
222                       c = input();
223                       /* scanner_print("%c", c); */
224                   }
225                   unput(c);
226                   /* line_num++; */
227                 }
228
229 {ws}            { ; }
230
231 {other}         { if ( scanner_debug ) {
232                       printf("Scanned some unexpected text = `%s'\n", yytext);
233                   }
234                   return ErrorMisc; 
235                 }
236
237 <<EOF>>         { if ( pop_input_stream() == 0 ) {
238                       /* End of last input stream */
239                       yyterminate();
240                   }
241                 }