1 /**************************************************************************
2 * scenery.l -- Lexical Analyzer for scenery files
4 * Written by Curtis Olson, started May 1997.
6 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
22 * NOTE: Compiles with flex and gcc.
25 **************************************************************************/
36 /* custom print routine */
37 static int scanner_debug = 0;
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;
45 int push_input_stream ( char *input ) {
47 if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) {
48 fprintf( stderr, "Scanner says: Includes nested too deeply\n" );
52 /* save yyin in case the following fails */
55 yyin = fopen( input, "r" );
57 fprintf( stderr, "Scanner says: cannot open '%s'\n", input );
59 /* The failed attempt destroyed yyin, so restore it */
65 include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
66 yy_switch_to_buffer( yy_create_buffer(yyin, YY_BUF_SIZE) );
71 int pop_input_stream () {
72 if ( --include_stack_ptr < 1 ) {
73 /* end of last input stream */
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] );
89 ident {letter}({letter}|{digit}|{connecter})*
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}
98 number {real}|{integer}
102 bad_string \"([^"\n]|\n)+\"
111 include { if ( scanner_debug ) {
112 printf("return IncludeSym\n");
117 mesh { if ( scanner_debug ) {
118 printf("return MeshSym\n");
123 row { if ( scanner_debug ) {
124 printf("return RowSym\n");
129 chunk { if ( scanner_debug ) {
130 printf("return ChunkSym\n");
135 bounds { if ( scanner_debug ) {
136 printf("return BoundsSym\n");
141 place { if ( scanner_debug ) {
142 printf("return PlaceSym\n");
147 {ident} { if ( scanner_debug ) {
148 printf("return Identifier = %s\n", yytext);
153 {number} { if ( scanner_debug ) {
154 printf("return Number\n");
159 {string} { if ( scanner_debug ) {
160 printf("return StringLiteral = %s\n", yytext);
162 return StringLiteral;
165 {bad_string} { if ( scanner_debug ) {
166 printf("return BadStringLiteral = %s\n", yytext);
168 return BadStringLiteral;
172 if ( scanner_debug ) {
173 printf("Line number = %d\n", line_num);
177 "#" { if ( scanner_debug ) {
178 printf("return HashSym\n");
183 "=" { if ( scanner_debug ) {
184 printf("return EqualSym\n");
189 "," { if ( scanner_debug ) {
190 printf("return CommaSym\n");
195 "{" { if ( scanner_debug ) {
196 printf("return LBraceSym\n");
201 "}" { if ( scanner_debug ) {
202 printf("return RBraceSym\n");
207 "(" { if ( scanner_debug ) {
208 printf("return LParenSym\n");
213 ")" { if ( scanner_debug ) {
214 printf("return RParenSym\n");
219 "//" { /* burn through "#" comment */
221 while ( (c != '\n') ) {
223 /* scanner_print("%c", c); */
231 {other} { if ( scanner_debug ) {
232 printf("Scanned some unexpected text = `%s'\n", yytext);
237 <<EOF>> { if ( pop_input_stream() == 0 ) {
238 /* End of last input stream */