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

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

orbital: another bug in the hex colour parser; now also support 3-component
values which will automatically set alpha to 1.

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    uint32_t u32val;
38    struct { float f0, f1, f2, f3, f4, f5, f6, f7; } args;
39}
40
41%start mesh_description
42
43%token T_FLUSH T_INITRB T_FREERB
44
45%token T_COLOR T_BGCOLOR
46
47%token T_TRANSLATEX T_ROTATEX T_TAPERX T_SCALEX T_MIRRORX
48%token T_TRANSLATEY T_ROTATEY T_TAPERY T_SCALEY T_MIRRORY
49%token T_TRANSLATEZ T_ROTATEZ T_TAPERZ T_SCALEZ T_MIRRORZ
50%token T_TRANSLATE T_SCALE
51
52%token T_CYLINDER T_BOX T_SMOOTHCHAMFBOX T_FLATCHAMFBOX T_SPHERE T_STAR
53%token T_EXPANDEDSTAR T_DISC T_TRIANGLE T_QUAD T_COG
54
55%token T_END 0
56
57%token <fval> NUMBER
58%token <u32val> COLOR
59
60%type <fval> number
61%type <args> args1 args2 args3 args4 args5 args6 args7 args8
62
63%{
64#include "../mesh-compiler.h"
65
66#undef yylex
67#define yylex mc.m_lexer->lex
68%}
69
70%%
71
72mesh_description:
73    mesh_command_list T_END
74    ;
75
76mesh_command_list:
77    mesh_command
78  | mesh_command_list ',' mesh_command
79    ;
80
81mesh_command:
82    rb_command
83  | color_command
84  | transform_command
85  | primitive_command
86    ;
87
88rb_command:
89    T_FLUSH    { mc.m_mesh.Flush(); }
90  | T_INITRB   { mc.m_mesh.MeshConvert(); }
91  | T_FREERB   { /* TODO */ }
92    ;
93
94color_command:
95    T_COLOR args4   { mc.m_mesh.SetCurColor(vec4($2.f0, $2.f1, $2.f2, $2.f3)); }
96  | T_COLOR COLOR   { uint16_t x = (uint16_t)$2;
97                      vec4 v(x >> 12, (x >> 8) & 0xf, (x >> 4) & 0xf, x & 0xf);
98                      mc.m_mesh.SetCurColor(vec4(v) * (1. / 15)); }
99  | T_BGCOLOR args4 { mc.m_mesh.SetCurColor2(vec4($2.f0, $2.f1, $2.f2, $2.f3)); }
100  | T_BGCOLOR COLOR { uint16_t x = (uint16_t)$2;
101                      vec4 v(x >> 12, (x >> 8) & 0xf, (x >> 4) & 0xf, x & 0xf);
102                      mc.m_mesh.SetCurColor(vec4(v) * (1. / 15)); }
103    ;
104
105transform_command:
106    T_TRANSLATEX args1  { mc.m_mesh.Translate(vec3($2.f0, 0, 0)); }
107  | T_TRANSLATEY args1  { mc.m_mesh.Translate(vec3(0, $2.f0, 0)); }
108  | T_TRANSLATEZ args1  { mc.m_mesh.Translate(vec3(0, 0, $2.f0)); }
109  | T_TRANSLATE args3   { mc.m_mesh.Translate(vec3($2.f0, $2.f1, $2.f2)); }
110  | T_ROTATEX args1     { mc.m_mesh.RotateX($2.f0); }
111  | T_ROTATEY args1     { mc.m_mesh.RotateY($2.f0); }
112  | T_ROTATEZ args1     { mc.m_mesh.RotateZ($2.f0); }
113  | T_TAPERX args3      { mc.m_mesh.TaperX($2.f0, $2.f1, $2.f2); }
114  | T_TAPERY args3      { mc.m_mesh.TaperY($2.f0, $2.f1, $2.f2); }
115  | T_TAPERZ args3      { mc.m_mesh.TaperZ($2.f0, $2.f1, $2.f2); }
116  | T_SCALEX args1      { mc.m_mesh.Scale(vec3($2.f0, 0, 0)); }
117  | T_SCALEY args1      { mc.m_mesh.Scale(vec3(0, $2.f0, 0)); }
118  | T_SCALEZ args1      { mc.m_mesh.Scale(vec3(0, 0, $2.f0)); }
119  | T_SCALE args3       { mc.m_mesh.Scale(vec3($2.f0, $2.f1, $2.f2)); }
120  | T_MIRRORX           { mc.m_mesh.MirrorX(); }
121  | T_MIRRORY           { mc.m_mesh.MirrorY(); }
122  | T_MIRRORZ           { mc.m_mesh.MirrorZ(); }
123    ;
124
125primitive_command:
126    T_CYLINDER args6       { mc.m_mesh.AppendCylinder((int)$2.f0, $2.f1,
127                                                 $2.f2, $2.f3,
128                                                 (int)$2.f4, (int)$2.f5); }
129  | T_BOX args3            { mc.m_mesh.AppendBox(vec3($2.f0, $2.f1, $2.f2)); }
130  | T_SMOOTHCHAMFBOX args4 { mc.m_mesh.AppendSmoothChamfBox(vec3($2.f0, $2.f1,
131                                                            $2.f2), $2.f3); }
132  | T_FLATCHAMFBOX args4   { mc.m_mesh.AppendFlatChamfBox(vec3($2.f0, $2.f1,
133                                                          $2.f2), $2.f3); }
134  | T_SPHERE args4         { mc.m_mesh.AppendSphere($2.f0,
135                                               vec3($2.f1, $2.f2, $2.f3)); }
136  | T_STAR args5           { mc.m_mesh.AppendStar((int)$2.f0, $2.f1, $2.f2,
137                                             (int)$2.f3, (int)$2.f4); }
138  | T_EXPANDEDSTAR args4   { mc.m_mesh.AppendExpandedStar((int)$2.f0, $2.f1,
139                                                     $2.f2, $2.f3); }
140  | T_DISC args3           { mc.m_mesh.AppendDisc((int)$2.f0, $2.f1, (int)$2.f2); }
141  | T_TRIANGLE args2       { mc.m_mesh.AppendSimpleTriangle($2.f0, (int)$2.f1); }
142  | T_QUAD args2           { mc.m_mesh.AppendSimpleQuad($2.f0, (int)$2.f1); }
143  | T_COG args8            { mc.m_mesh.AppendCog((int)$2.f0, $2.f1, $2.f2, $2.f3,
144                                            $2.f4, $2.f5, $2.f6, (int)$2.f7); }
145    ;
146
147args1: number { $$.f0 = $1; } ;
148args2: args1 ',' number { $$ = $1; $$.f1 = $3; } ;
149args3: args2 ',' number { $$ = $1; $$.f2 = $3; } ;
150args4: args3 ',' number { $$ = $1; $$.f3 = $3; } ;
151args5: args4 ',' number { $$ = $1; $$.f4 = $3; } ;
152args6: args5 ',' number { $$ = $1; $$.f5 = $3; } ;
153args7: args6 ',' number { $$ = $1; $$.f6 = $3; } ;
154args8: args7 ',' number { $$ = $1; $$.f7 = $3; } ;
155
156number:
157    NUMBER       { $$ = $1; }
158  | '-' number   { $$ = -$2; }
159    ;
160
161%%
162
163void orbital::MeshParser::error(const MeshParser::location_type& l,
164                                const std::string& m)
165{
166    mc.Error(l, m);
167}
168
Note: See TracBrowser for help on using the repository browser.