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