]> git.mxchange.org Git - flightgear.git/blob - Scenery/parser.y
Added GNU copyright headers.
[flightgear.git] / Scenery / parser.y
1 /**************************************************************************
2  * parser.y -- scenery file parser
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  * $Id$
23  **************************************************************************/
24
25
26 /* C pass through */
27 %{
28 #include <malloc.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "common.h"
34 #include "mesh.h"
35 #include "scenery.h"
36
37
38     /*#DEFINE YYDEBUG 1 */
39
40     /* interfacing with scanner.l (lex) */
41     extern int line_num;
42     extern char *yytext;
43     int push_input_stream ( char *input );
44
45     /* we must define this ourselves */
46     int yyerror(char *s);
47
48     /* handle for a mesh structure */
49     struct mesh *mesh_ptr;
50 %}
51
52
53 /* top level reserved words */
54 %token IncludeSym MeshSym RowSym ChunkSym BoundsSym PlaceSym
55
56 /* basic tokens */
57 %token Identifier Number StringLiteral 
58
59 /* symbol tokens  */
60 %token HashSym EqualSym LBraceSym RBraceSym LParenSym RParenSym CommaSym
61
62 /* error tokens */
63 %token BadStringLiteral ErrorMisc
64
65
66 /* Start Symbol */
67 %start  start
68
69
70 /* Rules Section */
71 %%
72
73 start : 
74     object_list
75 ;
76
77 object_list : 
78     object 
79     | object_list object
80 ;
81
82 object : 
83     include 
84     | mesh 
85     | chunk
86 ;
87
88 /* includes */
89 include : 
90     HashSym IncludeSym StringLiteral 
91     { 
92         yytext = strip_quotes(yytext);
93         printf("Need to include %s\n", yytext);
94         push_input_stream(yytext);
95     }
96 ;
97
98 /* terrain mesh rules */
99 mesh : 
100     MeshSym 
101     {
102         /* allocate a structure for this terrain mesh */
103         mesh_ptr = new_mesh();
104     }
105     Identifier LBraceSym mesh_body RBraceSym
106     {
107         /* The following two lines *SHOULD* be here, they are just temporarily
108            commented out until the scenery mngmt stuff gets hashed out. */
109
110         /* free up the mem used by this structure */
111         /* free(mesh_ptr->mesh_data); */
112         /* free(mesh_ptr); */
113     }
114 ;
115
116 mesh_body : 
117     mesh_header_list
118     {
119         /* We've read the headers, so allocate a pseudo 2d array */
120         mesh_ptr->mesh_data = new_mesh_data(mesh_ptr->rows, mesh_ptr->cols);
121         printf("Beginning to read mesh data\n");
122     }
123     mesh_row_list
124 ;
125
126 mesh_header_list :
127      mesh_header 
128      | mesh_header_list mesh_header
129 ;
130
131 mesh_header : 
132     Identifier 
133     {
134         mesh_set_option_name(mesh_ptr, yytext);
135     }
136     EqualSym Number
137     {
138         mesh_set_option_value(mesh_ptr, yytext);
139     }
140 ;
141
142 mesh_row_list :
143     mesh_row
144     | mesh_row_list mesh_row
145 ;
146
147 mesh_row : 
148     RowSym 
149     {
150         /* printf("Ready to read a row\n"); */
151         mesh_ptr->cur_col = 0;
152     }
153     EqualSym LParenSym mesh_row_items RParenSym
154     {
155         mesh_ptr->cur_row++;
156     }
157 ;
158
159 mesh_row_items : 
160     mesh_item 
161     | mesh_row_items mesh_item
162 ;
163
164 mesh_item :
165     Number
166     {
167         /* mesh data is a pseudo 2d array */
168         mesh_ptr->mesh_data[mesh_ptr->cur_row * mesh_ptr->rows + 
169                             mesh_ptr->cur_col] = atof(yytext);
170         mesh_ptr->cur_col++;
171     }
172 ;
173
174 /* chunk rules */
175 chunk           : ChunkSym Identifier LBraceSym chunk_body RBraceSym
176                 ;
177
178 chunk_body      : chunk_header_list chunk_bounds place_list
179                 ;
180
181 chunk_header_list : chunk_header
182                 | chunk_header_list chunk_header
183                 ;
184
185 chunk_header    : Identifier EqualSym StringLiteral
186                 ;
187
188 chunk_bounds    : BoundsSym EqualSym LBraceSym 
189                 Number CommaSym
190                 Number CommaSym
191                 Number CommaSym
192                 Number CommaSym
193                 Number CommaSym
194                 Number CommaSym
195                 Number CommaSym
196                 Number
197                 RBraceSym
198                 ;
199
200 /* place rules */
201 place_list      : place | place_list place
202                 ;
203
204 place           : PlaceSym Identifier LBraceSym place_options_list RBraceSym
205                 ;
206
207 place_options_list : place_option | place_options_list place_option
208                 ; 
209
210 place_option    : Identifier EqualSym place_value
211                 ;
212
213 place_value     : geodetic_coord | Number
214                 ;
215
216 geodetic_coord  : LParenSym Number CommaSym Number CommaSym 
217                   Number RParenSym
218                 ;
219
220
221 /* C Function Section */
222 %%
223
224
225 int yyerror(char *s) {
226     printf("Error: %s at line %d.\n", s, line_num);
227     return 0;
228 }
229
230
231 /* this is a simple main for testing the parser */
232
233 /*
234 int main(int argc, char **argv) {
235 #ifdef YYDEBUG
236     yydebug = 1;
237 #endif
238
239     printf("input file = %s\n", argv[1]);
240     push_input_stream(argv[1]);
241     yyparse();
242
243     return 0;
244 }
245 */
246
247
248 /* parse a scenery file */
249 int parse_scenery(char *file) {
250     int result;
251
252     printf("input file = %s\n", file);
253     push_input_stream(file);
254     result = yyparse();
255
256     /* return(result) */
257     return(mesh_ptr);
258 }