1 /**************************************************************************
2 * scenery.l -- Lexical Analyzer for scenery files
4 * Written by Curtis Olson, started May 1997.
6 * NOTE: Compiles with flex and gcc.
9 **************************************************************************/
20 /* custom print routine */
21 static int scanner_debug = 0;
22 static int scanner_msg_len;
23 static char scanner_msg[1024];
25 static void scanner_print(char *s) {
26 if ( scanner_debug ) {
31 /* Routines to manage a stack of nested input buffers (for
32 processing the #include directive */
33 #define MAX_INCLUDE_DEPTH 10
34 static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
35 static int include_stack_ptr = 0;
37 int push_input_stream ( char *input ) {
39 if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) {
40 fprintf( stderr, "Scanner says: Includes nested too deeply\n" );
44 /* save yyin in case the following fails */
47 yyin = fopen( input, "r" );
49 fprintf( stderr, "Scanner says: cannot open '%s'\n", input );
51 /* The failed attempt destroyed yyin, so restore it */
57 include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
58 yy_switch_to_buffer( yy_create_buffer(yyin, YY_BUF_SIZE) );
63 int pop_input_stream () {
64 if ( --include_stack_ptr < 1 ) {
65 /* end of last input stream */
68 /* end of current input stream, restore previous and continue */
69 yy_delete_buffer( YY_CURRENT_BUFFER );
70 yy_switch_to_buffer( include_stack[include_stack_ptr] );
81 ident {letter}({letter}|{digit}|{connecter})*
85 plain_real {integer}"."{integer}
86 short_exp_real {integer}[Ee][+-]?{integer}
87 exp_real {plain_real}[Ee][+-]?{integer}
88 real [+-]?{plain_real}|{short_exp_real}|{exp_real}
90 number {real}|{integer}
94 bad_string \"([^"\n]|\n)+\"
103 include { scanner_print("return IncludeSym\n");
107 mesh { scanner_print("return MeshSym\n");
111 row { scanner_print("return RowSym\n");
115 chunk { scanner_print("return ChunkSym\n");
119 bounds { scanner_print("return BoundsSym\n");
123 place { scanner_print("return PlaceSym\n");
127 {ident} { scanner_msg_len = snprintf(scanner_msg, 1024,
128 "return Identifier = %s\n", yytext);
129 scanner_msg[scanner_msg_len] = '\0';
130 scanner_print(scanner_msg);
134 {number} { scanner_print("return Number\n");
138 {string} { scanner_msg_len = snprintf(scanner_msg, 1024,
139 "return StringLiteral = %s\n", yytext);
140 scanner_msg[scanner_msg_len] = '\0';
141 scanner_print(scanner_msg);
142 return StringLiteral;
145 {bad_string} { scanner_msg_len = snprintf(scanner_msg, 1024,
146 "return BadStringLiteral = %s\n", yytext);
147 scanner_msg[scanner_msg_len] = '\0';
148 scanner_print(scanner_msg);
149 return BadStringLiteral;
153 scanner_msg_len = snprintf(scanner_msg, 1024,
154 "Line number = %d\n", line_num);
155 scanner_msg[scanner_msg_len] = '\0';
156 scanner_print(scanner_msg);
159 "#" { scanner_print("return HashSym\n");
163 "=" { scanner_print("return EqualSym\n");
167 "," { scanner_print("return CommaSym\n");
171 "{" { scanner_print("return LBraceSym\n");
175 "}" { scanner_print("return RBraceSym\n");
179 "(" { scanner_print("return LParenSym\n");
183 ")" { scanner_print("return RParenSym\n");
187 "//" { /* burn through "#" comment */
189 while ( (c != '\n') ) {
191 /* scanner_print("%c", c); */
199 {other} { scanner_msg_len = snprintf(scanner_msg, 1024,
200 "Scanned some unexpected text = `%s'\n", yytext);
201 scanner_msg[scanner_msg_len] = '\0';
202 scanner_print(scanner_msg);
206 <<EOF>> { if ( pop_input_stream() == 0 ) {
207 /* End of last input stream */