1 | /* $Id: parser.yy 48 2009-09-05 08:07:10Z tb $ -*- mode: c++ -*- */ |
---|
2 | /** \file parser.yy Contains the example Bison parser source */ |
---|
3 | |
---|
4 | %{ /*** C/C++ Declarations ***/ |
---|
5 | #if defined HAVE_CONFIG_H |
---|
6 | # include "config.h" |
---|
7 | #endif |
---|
8 | |
---|
9 | #include "core.h" |
---|
10 | #include "loldebug.h" |
---|
11 | |
---|
12 | using namespace lol; |
---|
13 | |
---|
14 | #include "mesh.h" |
---|
15 | |
---|
16 | #include <stdio.h> |
---|
17 | #include <string> |
---|
18 | #include <vector> |
---|
19 | |
---|
20 | %} |
---|
21 | |
---|
22 | /*** yacc/bison Declarations ***/ |
---|
23 | |
---|
24 | /* Require bison 2.3 or later */ |
---|
25 | %require "2.3" |
---|
26 | |
---|
27 | /* add debug output code to generated parser. disable this for release |
---|
28 | * versions. */ |
---|
29 | %debug |
---|
30 | |
---|
31 | /* start symbol */ |
---|
32 | %start mesh_description |
---|
33 | |
---|
34 | /* write out a header file containing the token defines */ |
---|
35 | %defines |
---|
36 | |
---|
37 | /* use newer C++ skeleton file */ |
---|
38 | %skeleton "lalr1.cc" |
---|
39 | |
---|
40 | /* namespace to enclose parser in */ |
---|
41 | %name-prefix="orbital" |
---|
42 | |
---|
43 | /* set the parser's class identifier */ |
---|
44 | %define parser_class_name "MeshParser" |
---|
45 | |
---|
46 | /* keep track of the current position within the input */ |
---|
47 | %locations |
---|
48 | %initial-action |
---|
49 | { |
---|
50 | // initialize the initial location object |
---|
51 | @$.begin.filename = @$.end.filename = &driver.streamname; |
---|
52 | }; |
---|
53 | |
---|
54 | /* The driver is passed by reference to the parser and to the scanner. This |
---|
55 | * provides a simple but effective pure interface, not relying on global |
---|
56 | * variables. */ |
---|
57 | %parse-param { class Driver& driver } |
---|
58 | |
---|
59 | /* verbose error messages */ |
---|
60 | %error-verbose |
---|
61 | |
---|
62 | %union |
---|
63 | { |
---|
64 | float fval; |
---|
65 | struct { float f0, f1, f2, f3, f4, f5, f6, f7; } args; |
---|
66 | } |
---|
67 | |
---|
68 | %token T_FLUSH T_INITRB T_FREERB |
---|
69 | |
---|
70 | %token T_COLOR T_BGCOLOR |
---|
71 | |
---|
72 | %token T_TRANSLATEX T_ROTATEX T_TAPERX T_SCALEX T_MIRRORX |
---|
73 | %token T_TRANSLATEY T_ROTATEY T_TAPERY T_SCALEY T_MIRRORY |
---|
74 | %token T_TRANSLATEZ T_ROTATEZ T_TAPERZ T_SCALEZ T_MIRRORZ |
---|
75 | %token T_TRANSLATE T_SCALE |
---|
76 | |
---|
77 | %token T_CYLINDER T_BOX T_SMOOTHCHAMFBOX T_FLATCHAMFBOX T_SPHERE T_STAR |
---|
78 | %token T_EXPANDEDSTAR T_DISC T_TRIANGLE T_QUAD T_COG |
---|
79 | |
---|
80 | %token T_END 0 |
---|
81 | |
---|
82 | %token <fval> NUMBER |
---|
83 | |
---|
84 | %type <fval> number |
---|
85 | |
---|
86 | %type <args> args1 args2 args3 args4 args5 args6 args7 args8 |
---|
87 | |
---|
88 | %{ |
---|
89 | |
---|
90 | #include "mesh-driver.h" |
---|
91 | #include "mesh-scanner.h" |
---|
92 | |
---|
93 | /* this "connects" the bison parser in the driver to the flex scanner class |
---|
94 | * object. it defines the yylex() function call to pull the next token from the |
---|
95 | * current lexer object of the driver context. */ |
---|
96 | #undef yylex |
---|
97 | #define yylex driver.lexer->lex |
---|
98 | |
---|
99 | %} |
---|
100 | |
---|
101 | %% /*** Grammar Rules ***/ |
---|
102 | |
---|
103 | mesh_description: |
---|
104 | mesh_command_list T_END |
---|
105 | ; |
---|
106 | |
---|
107 | mesh_command_list: |
---|
108 | mesh_command |
---|
109 | | mesh_command_list ',' mesh_command |
---|
110 | ; |
---|
111 | |
---|
112 | mesh_command: |
---|
113 | rb_command |
---|
114 | | color_command |
---|
115 | | transform_command |
---|
116 | | primitive_command |
---|
117 | ; |
---|
118 | |
---|
119 | rb_command: |
---|
120 | T_FLUSH { driver.mesh.Flush(); } |
---|
121 | | T_INITRB { driver.mesh.MeshConvert(); } |
---|
122 | | T_FREERB { /* TODO */ } |
---|
123 | ; |
---|
124 | |
---|
125 | color_command: |
---|
126 | T_COLOR args4 { driver.mesh.SetCurColor(vec4($2.f0, $2.f1, $2.f2, $2.f3)); } |
---|
127 | | T_BGCOLOR args4 { driver.mesh.SetCurColor2(vec4($2.f0, $2.f1, $2.f2, $2.f3)); } |
---|
128 | ; |
---|
129 | |
---|
130 | transform_command: |
---|
131 | T_TRANSLATEX args1 { driver.mesh.Translate(vec3($2.f0, 0, 0)); } |
---|
132 | | T_TRANSLATEY args1 { driver.mesh.Translate(vec3(0, $2.f0, 0)); } |
---|
133 | | T_TRANSLATEZ args1 { driver.mesh.Translate(vec3(0, 0, $2.f0)); } |
---|
134 | | T_TRANSLATE args3 { driver.mesh.Translate(vec3($2.f0, $2.f1, $2.f2)); } |
---|
135 | | T_ROTATEX args1 { driver.mesh.RotateX($2.f0); } |
---|
136 | | T_ROTATEY args1 { driver.mesh.RotateY($2.f0); } |
---|
137 | | T_ROTATEZ args1 { driver.mesh.RotateZ($2.f0); } |
---|
138 | | T_TAPERX args3 { driver.mesh.TaperX($2.f0, $2.f1, $2.f2); } |
---|
139 | | T_TAPERY args3 { driver.mesh.TaperY($2.f0, $2.f1, $2.f2); } |
---|
140 | | T_TAPERZ args3 { driver.mesh.TaperZ($2.f0, $2.f1, $2.f2); } |
---|
141 | | T_SCALEX args1 { driver.mesh.Scale(vec3($2.f0, 0, 0)); } |
---|
142 | | T_SCALEY args1 { driver.mesh.Scale(vec3(0, $2.f0, 0)); } |
---|
143 | | T_SCALEZ args1 { driver.mesh.Scale(vec3(0, 0, $2.f0)); } |
---|
144 | | T_SCALE args3 { driver.mesh.Scale(vec3($2.f0, $2.f1, $2.f2)); } |
---|
145 | | T_MIRRORX { driver.mesh.MirrorX(); } |
---|
146 | | T_MIRRORY { driver.mesh.MirrorY(); } |
---|
147 | | T_MIRRORZ { driver.mesh.MirrorZ(); } |
---|
148 | ; |
---|
149 | |
---|
150 | primitive_command: |
---|
151 | T_CYLINDER args6 { driver.mesh.AppendCylinder((int)$2.f0, $2.f1, |
---|
152 | $2.f2, $2.f3, |
---|
153 | (int)$2.f4, (int)$2.f5); } |
---|
154 | | T_BOX args3 { driver.mesh.AppendBox(vec3($2.f0, $2.f1, $2.f2)); } |
---|
155 | | T_SMOOTHCHAMFBOX args4 { driver.mesh.AppendSmoothChamfBox(vec3($2.f0, $2.f1, |
---|
156 | $2.f2), $2.f3); } |
---|
157 | | T_FLATCHAMFBOX args4 { driver.mesh.AppendFlatChamfBox(vec3($2.f0, $2.f1, |
---|
158 | $2.f2), $2.f3); } |
---|
159 | | T_SPHERE args4 { driver.mesh.AppendSphere($2.f0, |
---|
160 | vec3($2.f1, $2.f2, $2.f3)); } |
---|
161 | | T_STAR args5 { driver.mesh.AppendStar((int)$2.f0, $2.f1, $2.f2, |
---|
162 | (int)$2.f3, (int)$2.f4); } |
---|
163 | | T_EXPANDEDSTAR args4 { driver.mesh.AppendExpandedStar((int)$2.f0, $2.f1, |
---|
164 | $2.f2, $2.f3); } |
---|
165 | | T_DISC args3 { driver.mesh.AppendDisc((int)$2.f0, $2.f1, (int)$2.f2); } |
---|
166 | | T_TRIANGLE args2 { driver.mesh.AppendSimpleTriangle($2.f0, (int)$2.f1); } |
---|
167 | | T_QUAD args2 { driver.mesh.AppendSimpleQuad($2.f0, (int)$2.f1); } |
---|
168 | | T_COG args8 { driver.mesh.AppendCog((int)$2.f0, $2.f1, $2.f2, $2.f3, |
---|
169 | $2.f4, $2.f5, $2.f6, (int)$2.f7); } |
---|
170 | ; |
---|
171 | |
---|
172 | args1: number { $$.f0 = $1; } ; |
---|
173 | args2: args1 ',' number { $$ = $1; $$.f1 = $3; } ; |
---|
174 | args3: args2 ',' number { $$ = $1; $$.f2 = $3; } ; |
---|
175 | args4: args3 ',' number { $$ = $1; $$.f3 = $3; } ; |
---|
176 | args5: args4 ',' number { $$ = $1; $$.f4 = $3; } ; |
---|
177 | args6: args5 ',' number { $$ = $1; $$.f5 = $3; } ; |
---|
178 | args7: args6 ',' number { $$ = $1; $$.f6 = $3; } ; |
---|
179 | args8: args7 ',' number { $$ = $1; $$.f7 = $3; } ; |
---|
180 | |
---|
181 | number: |
---|
182 | NUMBER { $$ = $1; } |
---|
183 | | '-' number { $$ = -$2; } |
---|
184 | ; |
---|
185 | |
---|
186 | %% /*** Additional Code ***/ |
---|
187 | |
---|
188 | void orbital::MeshParser::error(const MeshParser::location_type& l, |
---|
189 | const std::string& m) |
---|
190 | { |
---|
191 | driver.error(l, m); |
---|
192 | } |
---|