]> git.mxchange.org Git - flightgear.git/blob - Scenery/scanner.l
48f56fa28957c3bcd02ddaa7a396a624a23a232b
[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  * NOTE: Compiles with flex and gcc.
7  *
8  * $Id$
9  **************************************************************************/
10
11
12 /* C Pass Through */
13 %{
14     #include <stdio.h>
15     #include "parser.h"
16
17     int line_num = 1;
18     char c;
19
20     /* custom print routine */
21     static int scanner_debug = 0;
22
23     /* Routines to manage a stack of nested input buffers (for
24        processing the #include directive */
25     #define MAX_INCLUDE_DEPTH 10
26     static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
27     static int include_stack_ptr = 0;
28
29     int push_input_stream ( char *input ) { 
30         FILE *yyin_save;
31         if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) {
32             fprintf( stderr, "Scanner says:  Includes nested too deeply\n" );
33             return(0);
34         }
35
36         /* save yyin in case the following fails */
37         yyin_save = yyin;
38
39         yyin = fopen( input, "r" );
40         if ( ! yyin ) {
41             fprintf( stderr, "Scanner says:  cannot open '%s'\n", input );
42
43             /* The failed attempt destroyed yyin, so restore it */
44             yyin = yyin_save;
45
46             return(0);
47         }
48  
49         include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
50         yy_switch_to_buffer( yy_create_buffer(yyin, YY_BUF_SIZE) );
51
52         return(1);
53     }
54
55     int pop_input_stream () { 
56         if ( --include_stack_ptr < 1 ) {
57             /* end of last input stream */
58             return(0);
59         } else {
60             /* end of current input stream, restore previous and continue */
61             yy_delete_buffer( YY_CURRENT_BUFFER );
62             yy_switch_to_buffer( include_stack[include_stack_ptr] );
63             return(1);
64         }
65     }
66 %}
67
68
69 /* Definitions */
70 letter          [A-Za-z]
71 digit           [0-9]
72 connecter       [_-]
73 ident           {letter}({letter}|{digit}|{connecter})*
74
75 integer         [+-]?{digit}+
76
77 plain_real      {integer}"."{integer}
78 short_exp_real  {integer}[Ee][+-]?{integer}
79 exp_real        {plain_real}[Ee][+-]?{integer}
80 real            [+-]?{plain_real}|{short_exp_real}|{exp_real}
81
82 number          {real}|{integer}
83
84 string          \"[^"\n]+\"
85
86 bad_string      \"([^"\n]|\n)+\"
87
88 ws              [ \t]+
89 other           .
90
91
92 /* Rules */ 
93 %%
94
95 include         { if ( scanner_debug ) {
96                       printf("return IncludeSym\n");
97                   }
98                   return IncludeSym;
99                 }
100
101 mesh            { if ( scanner_debug ) {
102                       printf("return MeshSym\n");
103                   }
104                   return MeshSym;
105                 }
106
107 row             { if ( scanner_debug ) {
108                       printf("return RowSym\n");
109                   }
110                   return RowSym;
111                 }
112
113 chunk           { if ( scanner_debug ) {
114                       printf("return ChunkSym\n");
115                   }
116                   return ChunkSym;
117                 }
118
119 bounds          { if ( scanner_debug ) {
120                       printf("return BoundsSym\n");
121                   }
122                   return BoundsSym;
123                 }
124
125 place           { if ( scanner_debug ) {
126                       printf("return PlaceSym\n");
127                   }
128                   return PlaceSym;
129                 }
130
131 {ident}         { if ( scanner_debug ) {
132                       printf("return Identifier = %s\n", yytext);
133                   }
134                   return Identifier;
135                 }
136
137 {number}        { if ( scanner_debug ) {
138                       printf("return Number\n");
139                   }
140                   return Number;
141                 }
142
143 {string}        { if ( scanner_debug ) {
144                       printf("return StringLiteral = %s\n", yytext);
145                   }
146                   return StringLiteral;
147                 }
148
149 {bad_string}    { if ( scanner_debug ) {
150                       printf("return BadStringLiteral = %s\n", yytext);
151                   }
152                   return BadStringLiteral; 
153                 }
154
155 "\n"            { line_num++; 
156                   if ( scanner_debug ) {
157                       printf("Line number = %d\n", line_num);
158                   }
159                }
160
161 "#"             { if ( scanner_debug ) {
162                       printf("return HashSym\n");
163                   }
164                   return HashSym; 
165                 }
166
167 "="             { if ( scanner_debug ) {
168                       printf("return EqualSym\n");
169                   }
170                   return EqualSym; 
171                 }
172
173 ","             { if ( scanner_debug ) {
174                       printf("return CommaSym\n");
175                   }
176                   return CommaSym; 
177                 }
178
179 "{"             { if ( scanner_debug ) {
180                       printf("return LBraceSym\n");
181                   }
182                   return LBraceSym; 
183                 }
184
185 "}"             { if ( scanner_debug ) {
186                       printf("return RBraceSym\n");
187                   }
188                   return RBraceSym;
189                 }
190
191 "("             { if ( scanner_debug ) {
192                       printf("return LParenSym\n");
193                   }
194                   return LParenSym; 
195                 }
196
197 ")"             { if ( scanner_debug ) {
198                       printf("return RParenSym\n");
199                   }
200                   return RParenSym;
201                 }
202
203 "//"            { /* burn through "#" comment */ 
204                   c = input();
205                   while ( (c != '\n') ) {
206                       c = input();
207                       /* scanner_print("%c", c); */
208                   }
209                   unput(c);
210                   /* line_num++; */
211                 }
212
213 {ws}            { ; }
214
215 {other}         { if ( scanner_debug ) {
216                       printf("Scanned some unexpected text = `%s'\n", yytext);
217                   }
218                   return ErrorMisc; 
219                 }
220
221 <<EOF>>         { if ( pop_input_stream() == 0 ) {
222                       /* End of last input stream */
223                       yyterminate();
224                   }
225                 }