source: trunk/orbital/mesh-parser.y @ 1428

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

orbital: allow hexadecimal colour values.

File size: 5.3 KB
Line 
1%{
2//
3// Orbital
4//
5// Copyright: (c) 2009-2012 Cédric Lecacheur <jordx@free.fr>
6//            (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com>
7//            (c) 2012 Sam Hocevar <sam@hocevar.net>
8//
9
10#if defined HAVE_CONFIG_H
11#   include "config.h"
12#endif
13
14#include "core.h"
15#include "loldebug.h"
16
17using namespace lol;
18
19#include "../mesh.h"
20
21#include <string>
22%}
23
24%require "2.3"
25%debug
26%defines
27%skeleton "lalr1.cc"
28%name-prefix="orbital"
29%define parser_class_name "MeshParser"
30%locations
31%parse-param { class MeshCompiler& mc }
32%error-verbose
33
34%union
35{
36    float fval;
37    struct { float f0, f1, f2, f3, f4, f5, f6, f7; } args;
38}
39
40%start mesh_description
41
42%token T_FLUSH T_INITRB T_FREERB
43
44%token T_COLOR T_BGCOLOR
45
46%token T_TRANSLATEX T_ROTATEX T_TAPERX T_SCALEX T_MIRRORX
47%token T_TRANSLATEY T_ROTATEY T_TAPERY T_SCALEY T_MIRRORY
48%token T_TRANSLATEZ T_ROTATEZ T_TAPERZ T_SCALEZ T_MIRRORZ
49%token T_TRANSLATE T_SCALE
50
51%token T_CYLINDER T_BOX T_SMOOTHCHAMFBOX T_FLATCHAMFBOX T_SPHERE T_STAR
52%token T_EXPANDEDSTAR T_DISC T_TRIANGLE T_QUAD T_COG
53
54%token T_END 0
55
56%token <fval> NUMBER
57%token <fval> COLOR
58
59%type <fval> number
60%type <args> args1 args2 args3 args4 args5 args6 args7 args8
61
62%{
63#include "../mesh-compiler.h"
64
65#undef yylex
66#define yylex mc.m_lexer->lex
67%}
68
69%%
70
71mesh_description:
72    mesh_command_list T_END
73    ;
74
75mesh_command_list:
76    mesh_command
77  | mesh_command_list ',' mesh_command
78    ;
79
80mesh_command:
81    rb_command
82  | color_command
83  | transform_command
84  | primitive_command
85    ;
86
87rb_command:
88    T_FLUSH    { mc.m_mesh.Flush(); }
89  | T_INITRB   { mc.m_mesh.MeshConvert(); }
90  | T_FREERB   { /* TODO */ }
91    ;
92
93color_command:
94    T_COLOR args4   { mc.m_mesh.SetCurColor(vec4($2.f0, $2.f1, $2.f2, $2.f3)); }
95  | T_COLOR COLOR   { uint32_t x = (uint32_t)$2;
96                      u8vec4 v;
97                      memcpy(&v, &x, sizeof(x));
98                      mc.m_mesh.SetCurColor(vec4(v) * (0.1 / 15)); }
99  | T_BGCOLOR args4 { mc.m_mesh.SetCurColor2(vec4($2.f0, $2.f1, $2.f2, $2.f3)); }
100  | T_BGCOLOR COLOR { uint32_t x = (uint32_t)$2;
101                      u8vec4 v;
102                      memcpy(&v, &x, sizeof(x));
103                      mc.m_mesh.SetCurColor(vec4(v) * (0.1 / 15)); }
104    ;
105
106transform_command:
107    T_TRANSLATEX args1  { mc.m_mesh.Translate(vec3($2.f0, 0, 0)); }
108  | T_TRANSLATEY args1  { mc.m_mesh.Translate(vec3(0, $2.f0, 0)); }
109  | T_TRANSLATEZ args1  { mc.m_mesh.Translate(vec3(0, 0, $2.f0)); }
110  | T_TRANSLATE args3   { mc.m_mesh.Translate(vec3($2.f0, $2.f1, $2.f2)); }
111  | T_ROTATEX args1     { mc.m_mesh.RotateX($2.f0); }
112  | T_ROTATEY args1     { mc.m_mesh.RotateY($2.f0); }
113  | T_ROTATEZ args1     { mc.m_mesh.RotateZ($2.f0); }
114  | T_TAPERX args3      { mc.m_mesh.TaperX($2.f0, $2.f1, $2.f2); }
115  | T_TAPERY args3      { mc.m_mesh.TaperY($2.f0, $2.f1, $2.f2); }
116  | T_TAPERZ args3      { mc.m_mesh.TaperZ($2.f0, $2.f1, $2.f2); }
117  | T_SCALEX args1      { mc.m_mesh.Scale(vec3($2.f0, 0, 0)); }
118  | T_SCALEY args1      { mc.m_mesh.Scale(vec3(0, $2.f0, 0)); }
119  | T_SCALEZ args1      { mc.m_mesh.Scale(vec3(0, 0, $2.f0)); }
120  | T_SCALE args3       { mc.m_mesh.Scale(vec3($2.f0, $2.f1, $2.f2)); }
121  | T_MIRRORX           { mc.m_mesh.MirrorX(); }
122  | T_MIRRORY           { mc.m_mesh.MirrorY(); }
123  | T_MIRRORZ           { mc.m_mesh.MirrorZ(); }
124    ;
125
126primitive_command:
127    T_CYLINDER args6       { mc.m_mesh.AppendCylinder((int)$2.f0, $2.f1,
128                                                 $2.f2, $2.f3,
129                                                 (int)$2.f4, (int)$2.f5); }
130  | T_BOX args3            { mc.m_mesh.AppendBox(vec3($2.f0, $2.f1, $2.f2)); }
131  | T_SMOOTHCHAMFBOX args4 { mc.m_mesh.AppendSmoothChamfBox(vec3($2.f0, $2.f1,
132                                                            $2.f2), $2.f3); }
133  | T_FLATCHAMFBOX args4   { mc.m_mesh.AppendFlatChamfBox(vec3($2.f0, $2.f1,
134                                                          $2.f2), $2.f3); }
135  | T_SPHERE args4         { mc.m_mesh.AppendSphere($2.f0,
136                                               vec3($2.f1, $2.f2, $2.f3)); }
137  | T_STAR args5           { mc.m_mesh.AppendStar((int)$2.f0, $2.f1, $2.f2,
138                                             (int)$2.f3, (int)$2.f4); }
139  | T_EXPANDEDSTAR args4   { mc.m_mesh.AppendExpandedStar((int)$2.f0, $2.f1,
140                                                     $2.f2, $2.f3); }
141  | T_DISC args3           { mc.m_mesh.AppendDisc((int)$2.f0, $2.f1, (int)$2.f2); }
142  | T_TRIANGLE args2       { mc.m_mesh.AppendSimpleTriangle($2.f0, (int)$2.f1); }
143  | T_QUAD args2           { mc.m_mesh.AppendSimpleQuad($2.f0, (int)$2.f1); }
144  | T_COG args8            { mc.m_mesh.AppendCog((int)$2.f0, $2.f1, $2.f2, $2.f3,
145                                            $2.f4, $2.f5, $2.f6, (int)$2.f7); }
146    ;
147
148args1: number { $$.f0 = $1; } ;
149args2: args1 ',' number { $$ = $1; $$.f1 = $3; } ;
150args3: args2 ',' number { $$ = $1; $$.f2 = $3; } ;
151args4: args3 ',' number { $$ = $1; $$.f3 = $3; } ;
152args5: args4 ',' number { $$ = $1; $$.f4 = $3; } ;
153args6: args5 ',' number { $$ = $1; $$.f5 = $3; } ;
154args7: args6 ',' number { $$ = $1; $$.f6 = $3; } ;
155args8: args7 ',' number { $$ = $1; $$.f7 = $3; } ;
156
157number:
158    NUMBER       { $$ = $1; }
159  | '-' number   { $$ = -$2; }
160    ;
161
162%%
163
164void orbital::MeshParser::error(const MeshParser::location_type& l,
165                                const std::string& m)
166{
167    mc.Error(l, m);
168}
169
Note: See TracBrowser for help on using the repository browser.