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