source: trunk/src/easymesh/easymesh-parser.y @ 2786

Last change on this file since 2786 was 2786, checked in by sam, 9 years ago

build: fix PS3 and Android builds.

File size: 8.6 KB
Line 
1%{
2//
3// Lol Engine
4//
5// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
6//            (c) 2009-2013 Cédric Lecacheur <jordx@free.fr>
7//            (c) 2009-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
8//   This program is free software; you can redistribute it and/or
9//   modify it under the terms of the Do What The Fuck You Want To
10//   Public License, Version 2, as published by Sam Hocevar. See
11//   http://www.wtfpl.net/ for more details.
12//
13
14#if defined HAVE_CONFIG_H
15#   include "config.h"
16#endif
17
18#include "core.h"
19#include "easymesh/easymesh.h"
20
21#include <string>
22%}
23
24%require "2.3"
25%debug
26%defines
27%skeleton "lalr1.cc"
28%name-prefix="lol"
29%define parser_class_name "EasyMeshParser"
30%locations
31%parse-param { class EasyMeshCompiler& mc }
32%error-verbose
33
34%union
35{
36    float fval;
37    /* Can't use uin32_t here for some reason */
38    unsigned u32val;
39    struct { float f0, f1, f2, f3, f4, f5, f6, f7, f8, f9; } args;
40}
41
42%start mesh_description
43
44%token T_COLOR T_BGCOLOR
45
46%token T_TRANSLATEX T_ROTATEX T_TAPERX T_TWISTX T_SHEARX T_STRETCHX T_BENDXY T_BENDXZ T_SCALEX T_MIRRORX
47%token T_TRANSLATEY T_ROTATEY T_TAPERY T_TWISTY T_SHEARY T_STRETCHY T_BENDYX T_BENDYZ T_SCALEY T_MIRRORY
48%token T_TRANSLATEZ T_ROTATEZ T_TAPERZ T_TWISTZ T_SHEARZ T_STRETCHZ T_BENDZX T_BENDZY T_SCALEZ T_MIRRORZ
49%token T_TRANSLATE T_SCALE T_TOGGLESCALEWINDING T_RADIALJITTER T_SPLITTRIANGLE T_SMOOTHMESH
50%token T_CSGUNION T_CSGSUBSTRACT T_CSGSUBSTRACTLOSS T_CSGAND T_CSGXOR
51%token T_CHAMFER
52
53%token T_CYLINDER T_BOX T_SMOOTHCHAMFBOX T_FLATCHAMFBOX T_SPHERE T_CAPSULE
54%token T_STAR T_EXPANDEDSTAR T_DISC T_TRIANGLE T_QUAD T_COG T_TORUS
55
56%token T_END 0
57%token T_ERROR
58
59%token <fval> NUMBER
60%token <u32val> COLOR
61
62%type <fval> number
63%type <args> args1 args2 args3 args4 args5 args6 args7 args8 args9 args10
64
65%{
66#include "easymesh/easymesh-compiler.h"
67
68#undef yylex
69#define yylex mc.m_lexer->lex
70
71/* HACK: workaround for Bison who insists on using exceptions */
72#define try if (true)
73#define catch(...) if (false)
74#define throw (void)0
75%}
76
77%%
78
79mesh_description:
80    mesh_expression_list T_END
81    ;
82
83mesh_expression_list:
84    mesh_expression
85  | mesh_expression mesh_expression_list
86  ;
87
88mesh_expression:
89    mesh_command_list
90  | mesh_open mesh_expression_list mesh_close
91    ;
92
93mesh_open:
94    '['       { mc.m_mesh.OpenBrace(); }
95    ;
96
97mesh_close:
98    ']'       { mc.m_mesh.CloseBrace(); }
99    ;
100
101mesh_command_list:
102    mesh_command
103  | mesh_command_list mesh_command
104    ;
105
106mesh_command:
107    color_command
108  | transform_command
109  | primitive_command
110    ;
111
112color_command:
113    T_COLOR args4   { mc.m_mesh.SetCurColor(vec4($2.f0, $2.f1, $2.f2, $2.f3)); }
114  | T_COLOR COLOR   { uint32_t x = $2;
115                      vec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
116                      mc.m_mesh.SetCurColor(vec4(v) * (1.f / 255)); }
117  | T_BGCOLOR args4 { mc.m_mesh.SetCurColor2(vec4($2.f0, $2.f1, $2.f2, $2.f3)); }
118  | T_BGCOLOR COLOR { uint32_t x = $2;
119                      vec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
120                      mc.m_mesh.SetCurColor2(vec4(v) * (1.f / 255)); }
121    ;
122
123transform_command:
124    T_CHAMFER args1        { mc.m_mesh.Chamfer($2.f0); }
125  | T_TRANSLATEX args1     { mc.m_mesh.Translate(vec3($2.f0, 0, 0)); }
126  | T_TRANSLATEY args1     { mc.m_mesh.Translate(vec3(0, $2.f0, 0)); }
127  | T_TRANSLATEZ args1     { mc.m_mesh.Translate(vec3(0, 0, $2.f0)); }
128  | T_TRANSLATE args3      { mc.m_mesh.Translate(vec3($2.f0, $2.f1, $2.f2)); }
129  | T_ROTATEX args1        { mc.m_mesh.RotateX($2.f0); }
130  | T_ROTATEY args1        { mc.m_mesh.RotateY($2.f0); }
131  | T_ROTATEZ args1        { mc.m_mesh.RotateZ($2.f0); }
132  | T_TAPERX args3         { mc.m_mesh.TaperX($2.f0, $2.f1, $2.f2); }
133  |  T_TAPERX args4        { mc.m_mesh.TaperX($2.f0, $2.f1, $2.f2, $2.f3); }
134  | T_TAPERY args3         { mc.m_mesh.TaperY($2.f0, $2.f1, $2.f2); }
135  |  T_TAPERY args4        { mc.m_mesh.TaperY($2.f0, $2.f1, $2.f2, $2.f3); }
136  | T_TAPERZ args3         { mc.m_mesh.TaperZ($2.f0, $2.f1, $2.f2); }
137  |  T_TAPERZ args4        { mc.m_mesh.TaperZ($2.f0, $2.f1, $2.f2, $2.f3); }
138  | T_TWISTX args2         { mc.m_mesh.TwistX($2.f0, $2.f1); }
139  | T_TWISTY args2         { mc.m_mesh.TwistY($2.f0, $2.f1); }
140  | T_TWISTZ args2         { mc.m_mesh.TwistZ($2.f0, $2.f1); }
141  | T_SHEARX args3         { mc.m_mesh.ShearX($2.f0, $2.f1, $2.f2); }
142  |  T_SHEARX args4        { mc.m_mesh.ShearX($2.f0, $2.f1, $2.f2, $2.f3); }
143  | T_SHEARY args3         { mc.m_mesh.ShearY($2.f0, $2.f1, $2.f2); }
144  |  T_SHEARY args4        { mc.m_mesh.ShearY($2.f0, $2.f1, $2.f2, $2.f3); }
145  | T_SHEARZ args3         { mc.m_mesh.ShearZ($2.f0, $2.f1, $2.f2); }
146  |  T_SHEARZ args4        { mc.m_mesh.ShearZ($2.f0, $2.f1, $2.f2, $2.f3); }
147  | T_STRETCHX args3       { mc.m_mesh.StretchX($2.f0, $2.f1, $2.f2); }
148  | T_STRETCHY args3       { mc.m_mesh.StretchY($2.f0, $2.f1, $2.f2); }
149  | T_STRETCHZ args3       { mc.m_mesh.StretchZ($2.f0, $2.f1, $2.f2); }
150  | T_BENDXY args2         { mc.m_mesh.BendXY($2.f0, $2.f1); }
151  | T_BENDXZ args2         { mc.m_mesh.BendXZ($2.f0, $2.f1); }
152  | T_BENDYX args2         { mc.m_mesh.BendYX($2.f0, $2.f1); }
153  | T_BENDYZ args2         { mc.m_mesh.BendYZ($2.f0, $2.f1); }
154  | T_BENDZX args2         { mc.m_mesh.BendZX($2.f0, $2.f1); }
155  | T_BENDZY args2         { mc.m_mesh.BendZY($2.f0, $2.f1); }
156  | T_SCALEX args1         { mc.m_mesh.Scale(vec3($2.f0, 1.0, 1.0)); }
157  | T_SCALEY args1         { mc.m_mesh.Scale(vec3(1.0, $2.f0, 1.0)); }
158  | T_SCALEZ args1         { mc.m_mesh.Scale(vec3(1.0, 1.0, $2.f0)); }
159  | T_SCALE args3          { mc.m_mesh.Scale(vec3($2.f0, $2.f1, $2.f2)); }
160  | T_MIRRORX              { mc.m_mesh.MirrorX(); }
161  | T_MIRRORY              { mc.m_mesh.MirrorY(); }
162  | T_MIRRORZ              { mc.m_mesh.MirrorZ(); }
163  | T_RADIALJITTER args1   { mc.m_mesh.RadialJitter($2.f0); }
164  | T_SPLITTRIANGLE args1  { mc.m_mesh.SplitTriangles($2.f0); }
165  | T_SMOOTHMESH args3     { mc.m_mesh.SmoothMesh($2.f0, $2.f1, $2.f2); }
166  | T_TOGGLESCALEWINDING   { mc.m_mesh.ToggleScaleWinding(); }
167  | T_CSGUNION             { mc.m_mesh.CsgUnion(); }
168  | T_CSGSUBSTRACT         { mc.m_mesh.CsgSubstract(); }
169  | T_CSGSUBSTRACTLOSS     { mc.m_mesh.CsgSubstractLoss(); }
170  | T_CSGAND               { mc.m_mesh.CsgAnd(); }
171  | T_CSGXOR               { mc.m_mesh.CsgXor(); }
172    ;
173
174primitive_command:
175    T_CYLINDER args6       { mc.m_mesh.AppendCylinder((int)$2.f0, $2.f1,
176                                                 $2.f2, $2.f3,
177                                                 (int)$2.f4, (int)$2.f5, 0); }
178  | T_CYLINDER args7       { mc.m_mesh.AppendCylinder((int)$2.f0, $2.f1,
179                                                 $2.f2, $2.f3,
180                                                 (int)$2.f4, (int)$2.f5, (int)$2.f6); }
181  | T_BOX args3            { mc.m_mesh.AppendBox(vec3($2.f0, $2.f1, $2.f2)); }
182  | T_SMOOTHCHAMFBOX args4 { mc.m_mesh.AppendSmoothChamfBox(vec3($2.f0, $2.f1,
183                                                            $2.f2), $2.f3); }
184  | T_FLATCHAMFBOX args4   { mc.m_mesh.AppendFlatChamfBox(vec3($2.f0, $2.f1,
185                                                          $2.f2), $2.f3); }
186  | T_SPHERE args2         { mc.m_mesh.AppendSphere($2.f0, $2.f1); }
187  | T_CAPSULE args3        { mc.m_mesh.AppendCapsule($2.f0, $2.f1, $2.f2); }
188  | T_TORUS args3          { mc.m_mesh.AppendTorus((int)$2.f0, $2.f1, $2.f2); }
189  | T_STAR args5           { mc.m_mesh.AppendStar((int)$2.f0, $2.f1, $2.f2,
190                                             (int)$2.f3, (int)$2.f4); }
191  | T_EXPANDEDSTAR args4   { mc.m_mesh.AppendExpandedStar((int)$2.f0, $2.f1,
192                                                     $2.f2, $2.f3); }
193  | T_DISC args3           { mc.m_mesh.AppendDisc((int)$2.f0, $2.f1, (int)$2.f2); }
194  | T_TRIANGLE args2       { mc.m_mesh.AppendSimpleTriangle($2.f0, (int)$2.f1); }
195  | T_QUAD args2           { mc.m_mesh.AppendSimpleQuad($2.f0, (int)$2.f1); }
196  | T_COG args10           { mc.m_mesh.AppendCog((int)$2.f0, $2.f1,
197                                 $2.f2, $2.f3, $2.f4, $2.f5, $2.f6,
198                                 $2.f7, $2.f8, (int)$2.f9); }
199    ;
200
201args1: number { $$.f0 = $1; } ;
202args2: args1 number { $$ = $1; $$.f1 = $2; } ;
203args3: args2 number { $$ = $1; $$.f2 = $2; } ;
204args4: args3 number { $$ = $1; $$.f3 = $2; } ;
205args5: args4 number { $$ = $1; $$.f4 = $2; } ;
206args6: args5 number { $$ = $1; $$.f5 = $2; } ;
207args7: args6 number { $$ = $1; $$.f6 = $2; } ;
208args8: args7 number { $$ = $1; $$.f7 = $2; } ;
209args9: args8 number { $$ = $1; $$.f8 = $2; } ;
210args10: args9 number { $$ = $1; $$.f9 = $2; } ;
211
212number:
213    NUMBER       { $$ = $1; }
214  | '-' number   { $$ = -$2; }
215    ;
216
217%%
218
219void lol::EasyMeshParser::error(const EasyMeshParser::location_type& l,
220                                const std::string& m)
221{
222    mc.Error(l, m);
223}
224
Note: See TracBrowser for help on using the repository browser.