You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

329 rivejä
14 KiB

  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. #if defined HAVE_CONFIG_H
  14. # include "config.h"
  15. #endif
  16. #include "core.h"
  17. #include "easymesh/easymesh.h"
  18. #include <string>
  19. %}
  20. %require "2.3"
  21. %debug
  22. %defines
  23. %skeleton "lalr1.cc"
  24. %name-prefix="lol"
  25. %define parser_class_name "EasyMeshParser"
  26. %locations
  27. %parse-param { class EasyMeshCompiler& mc }
  28. %error-verbose
  29. %union
  30. {
  31. float fval;
  32. int ival;
  33. bool bval;
  34. float vval[4];
  35. int ivval[4];
  36. char* lval;
  37. /* Can't use uin32_t here for some reason */
  38. unsigned u32val;
  39. }
  40. %start mesh_description
  41. %token T_LOOP T_COLOR T_ACOLOR T_BCOLOR T_VCOLOR
  42. %token T_TOGGLESCALEWINDING T_TOGGLEQUADWEIGHTING T_TOGGLEPOSTBUILDNORMAL T_TOGGLEVERTNOCLEANUP
  43. %token T_VERTMERGE T_VERTSEPARATE
  44. %token T_TRANSLATEX T_ROTATEX T_TAPERX T_TWISTX T_SHEARX T_STRETCHX T_BENDXY T_BENDXZ T_SCALEX T_MIRRORX
  45. %token T_TRANSLATEY T_ROTATEY T_TAPERY T_TWISTY T_SHEARY T_STRETCHY T_BENDYX T_BENDYZ T_SCALEY T_MIRRORY
  46. %token T_TRANSLATEZ T_ROTATEZ T_TAPERZ T_TWISTZ T_SHEARZ T_STRETCHZ T_BENDZX T_BENDZY T_SCALEZ T_MIRRORZ
  47. %token T_TRANSLATE T_ROTATE T_SCALE T_RADIALJITTER T_SPLITTRIANGLE T_SMOOTHMESH
  48. %token T_DUPLICATE
  49. %token T_CSGUNION T_CSGSUBSTRACT T_CSGSUBSTRACTLOSS T_CSGAND T_CSGXOR
  50. %token T_CHAMFER
  51. %token T_CYLINDER T_BOX T_SMOOTHCHAMFBOX T_FLATCHAMFBOX T_SPHERE T_CAPSULE
  52. %token T_STAR T_EXPANDEDSTAR T_DISC T_TRIANGLE T_QUAD T_COG T_TORUS
  53. %token T_END 0
  54. %token T_ERROR
  55. %token <fval> F_NUMBER
  56. %token <ival> I_NUMBER
  57. %token <bval> BOOLEAN
  58. %token <u32val> COLOR
  59. /* Base Number types */
  60. %type <fval> fv
  61. %type <ival> iv
  62. /* Vector types */
  63. %type <vval> v2
  64. %type <vval> v3
  65. %type <vval> v4
  66. /* Special types */
  67. %type <bval> bv
  68. %{
  69. #include "easymesh/easymesh-compiler.h"
  70. #undef yylex
  71. #define yylex mc.m_lexer->lex
  72. /* HACK: workaround for Bison who insists on using exceptions */
  73. #define try if (true)
  74. #define catch(...) if (false)
  75. #define throw (void)0
  76. %}
  77. %%
  78. mesh_description:
  79. mesh_expression_list T_END
  80. ;
  81. mesh_expression_list:
  82. mesh_expression
  83. | mesh_expression mesh_expression_list
  84. ;
  85. mesh_expression:
  86. mesh_command_list
  87. | mesh_open mesh_expression_list mesh_close
  88. ;
  89. mesh_command_list:
  90. mesh_command
  91. | mesh_command mesh_command_list
  92. ;
  93. mesh_command:
  94. color_command
  95. | transform_command
  96. | primitive_command
  97. | post_brace_command
  98. | pre_brace_command '[' mesh_expression_list mesh_close
  99. | doloop '[' mesh_expression_list ']' { mc.m_mesh.LoopEnd(); }
  100. ;
  101. post_brace_command:
  102. T_CSGUNION mesh_open mesh_expression_list ']' { mc.m_mesh.CsgUnion(); mc.m_mesh.CloseBrace(); }
  103. | T_CSGSUBSTRACT mesh_open mesh_expression_list ']' { mc.m_mesh.CsgSub(); mc.m_mesh.CloseBrace(); }
  104. | T_CSGSUBSTRACTLOSS mesh_open mesh_expression_list ']' { mc.m_mesh.CsgSubL(); mc.m_mesh.CloseBrace(); }
  105. | T_CSGAND mesh_open mesh_expression_list ']' { mc.m_mesh.CsgAnd(); mc.m_mesh.CloseBrace(); }
  106. | T_CSGXOR mesh_open mesh_expression_list ']' { mc.m_mesh.CsgXor(); mc.m_mesh.CloseBrace(); }
  107. ;
  108. doloop:
  109. T_LOOP iv { mc.m_mesh.LoopStart($2); }
  110. ;
  111. pre_brace_command:
  112. T_DUPLICATE { mc.m_mesh.DupAndScale(vec3(1.f), true); }
  113. ;
  114. mesh_open:
  115. '[' { mc.m_mesh.OpenBrace(); }
  116. ;
  117. mesh_close:
  118. ']' { mc.m_mesh.CloseBrace(); }
  119. ;
  120. color_command:
  121. T_COLOR fv fv fv fv { mc.m_mesh.SetCurColor(vec4($2, $3, $4, $5)); }
  122. | T_COLOR v4 { mc.m_mesh.SetCurColor(vec4($2[0], $2[1], $2[2], $2[3])); }
  123. | T_COLOR COLOR { uint32_t x = $2;
  124. ivec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
  125. mc.m_mesh.SetCurColor(vec4(v) * (1.f / 255.f)); }
  126. | T_ACOLOR fv fv fv fv { mc.m_mesh.SetCurColorA(vec4($2, $3, $4, $5)); }
  127. | T_ACOLOR v4 { mc.m_mesh.SetCurColorA(vec4($2[0], $2[1], $2[2], $2[3])); }
  128. | T_ACOLOR COLOR { uint32_t x = $2;
  129. ivec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
  130. mc.m_mesh.SetCurColorA(vec4(v) * (1.f / 255.f)); }
  131. | T_BCOLOR fv fv fv fv { mc.m_mesh.SetCurColorB(vec4($2, $3, $4, $5)); }
  132. | T_BCOLOR v4 { mc.m_mesh.SetCurColorB(vec4($2[0], $2[1], $2[2], $2[3])); }
  133. | T_BCOLOR COLOR { uint32_t x = $2;
  134. ivec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
  135. mc.m_mesh.SetCurColorB(vec4(v) * (1.f / 255.f)); }
  136. | T_VCOLOR fv fv fv fv { mc.m_mesh.SetVertColor(vec4($2, $3, $4, $5)); }
  137. | T_VCOLOR v4 { mc.m_mesh.SetVertColor(vec4($2[0], $2[1], $2[2], $2[3])); }
  138. | T_VCOLOR COLOR { uint32_t x = $2;
  139. ivec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
  140. mc.m_mesh.SetVertColor(vec4(v) * (1.f / 255.f)); }
  141. ;
  142. transform_command:
  143. T_TRANSLATEX fv { mc.m_mesh.Translate(vec3($2, 0.f, 0.f)); }
  144. | T_TRANSLATEY fv { mc.m_mesh.Translate(vec3(0.f, $2, 0.f)); }
  145. | T_TRANSLATEZ fv { mc.m_mesh.Translate(vec3(0.f, 0.f, $2)); }
  146. | T_TRANSLATE fv fv fv { mc.m_mesh.Translate(vec3($2, $3, $4)); }
  147. | T_TRANSLATE v3 { mc.m_mesh.Translate(vec3($2[0], $2[1], $2[2])); }
  148. | T_ROTATEX fv { mc.m_mesh.RotateX($2); }
  149. | T_ROTATEY fv { mc.m_mesh.RotateY($2); }
  150. | T_ROTATEZ fv { mc.m_mesh.RotateZ($2); }
  151. | T_ROTATE fv fv fv fv { mc.m_mesh.Rotate($2, vec3($3, $4, $5)); }
  152. | T_ROTATE fv v3 { mc.m_mesh.Rotate($2, vec3($3[0], $3[1], $3[2])); }
  153. | T_RADIALJITTER fv { mc.m_mesh.RadialJitter($2); }
  154. | T_TAPERX fv fv fv bv { mc.m_mesh.TaperX($2, $3, $4, $5); }
  155. | T_TAPERX fv fv fv { mc.m_mesh.TaperX($2, $3, $4); }
  156. | T_TAPERX fv fv { mc.m_mesh.TaperX($2, $3); }
  157. | T_TAPERY fv fv fv bv { mc.m_mesh.TaperY($2, $3, $4, $5); }
  158. | T_TAPERY fv fv fv { mc.m_mesh.TaperY($2, $3, $4); }
  159. | T_TAPERY fv fv { mc.m_mesh.TaperY($2, $3); }
  160. | T_TAPERZ fv fv fv bv { mc.m_mesh.TaperZ($2, $3, $4, $5); }
  161. | T_TAPERZ fv fv fv { mc.m_mesh.TaperZ($2, $3, $4); }
  162. | T_TAPERZ fv fv { mc.m_mesh.TaperZ($2, $3); }
  163. | T_TWISTX fv fv { mc.m_mesh.TwistX($2, $3); }
  164. | T_TWISTX fv { mc.m_mesh.TwistX($2); }
  165. | T_TWISTY fv fv { mc.m_mesh.TwistY($2, $3); }
  166. | T_TWISTY fv { mc.m_mesh.TwistY($2); }
  167. | T_TWISTZ fv fv { mc.m_mesh.TwistZ($2, $3); }
  168. | T_TWISTZ fv { mc.m_mesh.TwistZ($2); }
  169. | T_SHEARX fv fv fv bv { mc.m_mesh.ShearX($2, $3, $4, $5); }
  170. | T_SHEARX fv fv fv { mc.m_mesh.ShearX($2, $3, $4); }
  171. | T_SHEARX fv fv { mc.m_mesh.ShearX($2, $3); }
  172. | T_SHEARY fv fv fv bv { mc.m_mesh.ShearY($2, $3, $4, $5); }
  173. | T_SHEARY fv fv fv { mc.m_mesh.ShearY($2, $3, $4); }
  174. | T_SHEARY fv fv { mc.m_mesh.ShearY($2, $3); }
  175. | T_SHEARZ fv fv fv bv { mc.m_mesh.ShearZ($2, $3, $4, $5); }
  176. | T_SHEARZ fv fv fv { mc.m_mesh.ShearZ($2, $3, $4); }
  177. | T_SHEARZ fv fv { mc.m_mesh.ShearZ($2, $3); }
  178. | T_STRETCHX fv fv fv { mc.m_mesh.StretchX($2, $3, $4); }
  179. | T_STRETCHX fv fv { mc.m_mesh.StretchX($2, $3); }
  180. | T_STRETCHY fv fv fv { mc.m_mesh.StretchY($2, $3, $4); }
  181. | T_STRETCHY fv fv { mc.m_mesh.StretchY($2, $3); }
  182. | T_STRETCHZ fv fv fv { mc.m_mesh.StretchZ($2, $3, $4); }
  183. | T_STRETCHZ fv fv { mc.m_mesh.StretchZ($2, $3); }
  184. | T_BENDXY fv fv { mc.m_mesh.BendXY($2, $3); }
  185. | T_BENDXY fv { mc.m_mesh.BendXY($2); }
  186. | T_BENDXZ fv fv { mc.m_mesh.BendXZ($2, $3); }
  187. | T_BENDXZ fv { mc.m_mesh.BendXZ($2); }
  188. | T_BENDYX fv fv { mc.m_mesh.BendYX($2, $3); }
  189. | T_BENDYX fv { mc.m_mesh.BendYX($2); }
  190. | T_BENDYZ fv fv { mc.m_mesh.BendYZ($2, $3); }
  191. | T_BENDYZ fv { mc.m_mesh.BendYZ($2); }
  192. | T_BENDZX fv fv { mc.m_mesh.BendZX($2, $3); }
  193. | T_BENDZX fv { mc.m_mesh.BendZX($2); }
  194. | T_BENDZY fv fv { mc.m_mesh.BendZY($2, $3); }
  195. | T_BENDZY fv { mc.m_mesh.BendZY($2); }
  196. | T_SCALEX fv { mc.m_mesh.Scale(vec3($2, 1.f, 1.f)); }
  197. | T_SCALEY fv { mc.m_mesh.Scale(vec3(1.f, $2, 1.f)); }
  198. | T_SCALEZ fv { mc.m_mesh.Scale(vec3(1.f, 1.f, $2)); }
  199. | T_SCALE fv fv fv { mc.m_mesh.Scale(vec3($2, $3, $4)); }
  200. | T_SCALE v3 { mc.m_mesh.Scale(vec3($2[0], $2[1], $2[2])); }
  201. | T_SCALE fv { mc.m_mesh.Scale(vec3($2, $2, $2)); }
  202. | T_MIRRORX { mc.m_mesh.MirrorX(); }
  203. | T_MIRRORY { mc.m_mesh.MirrorY(); }
  204. | T_MIRRORZ { mc.m_mesh.MirrorZ(); }
  205. | T_CHAMFER fv { mc.m_mesh.Chamfer($2); }
  206. | T_SPLITTRIANGLE iv { mc.m_mesh.SplitTriangles($2); }
  207. | T_SMOOTHMESH iv iv iv { mc.m_mesh.SmoothMesh($2, $3, $4); }
  208. | T_TOGGLESCALEWINDING { mc.m_mesh.ToggleScaleWinding(); }
  209. | T_TOGGLEQUADWEIGHTING { mc.m_mesh.ToggleQuadWeighting(); }
  210. | T_TOGGLEPOSTBUILDNORMAL { mc.m_mesh.TogglePostBuildNormal(); }
  211. | T_TOGGLEVERTNOCLEANUP { mc.m_mesh.ToggleVerticeNoCleanup(); }
  212. | T_VERTMERGE { mc.m_mesh.VerticesMerge(); }
  213. | T_VERTSEPARATE { mc.m_mesh.VerticesSeparate(); }
  214. ;
  215. primitive_command:
  216. T_CYLINDER iv fv fv fv bv bv bv { mc.m_mesh.AppendCylinder($2, $3, $4, $5, $6, $7, $8); }
  217. | T_CYLINDER iv fv fv fv bv bv { mc.m_mesh.AppendCylinder($2, $3, $4, $5, $6, $7); }
  218. | T_CYLINDER iv fv fv fv bv { mc.m_mesh.AppendCylinder($2, $3, $4, $5, $6); }
  219. | T_CYLINDER iv fv fv fv { mc.m_mesh.AppendCylinder($2, $3, $4, $5); }
  220. | T_CYLINDER iv fv fv { mc.m_mesh.AppendCylinder($2, $3, $4, $4); }
  221. | T_SPHERE iv fv { mc.m_mesh.AppendSphere($2, $3); }
  222. | T_CAPSULE iv fv fv { mc.m_mesh.AppendCapsule($2, $3, $4); }
  223. | T_TORUS iv fv fv { mc.m_mesh.AppendTorus($2, $3, $4); }
  224. | T_BOX fv fv fv fv { mc.m_mesh.AppendBox(vec3($2, $3, $4), $5); }
  225. | T_BOX fv fv fv { mc.m_mesh.AppendBox(vec3($2, $3, $4)); }
  226. | T_BOX fv { mc.m_mesh.AppendBox(vec3($2, $2, $2)); }
  227. | T_BOX v3 fv { mc.m_mesh.AppendBox(vec3($2[0], $2[1], $2[2]), $3); }
  228. | T_BOX v3 { mc.m_mesh.AppendBox(vec3($2[0], $2[1], $2[2])); }
  229. | T_SMOOTHCHAMFBOX fv fv fv fv { mc.m_mesh.AppendSmoothChamfBox(vec3($2, $3, $4), $5); }
  230. | T_SMOOTHCHAMFBOX fv fv { mc.m_mesh.AppendSmoothChamfBox(vec3($2, $2, $2), $3); }
  231. | T_SMOOTHCHAMFBOX v3 fv { mc.m_mesh.AppendSmoothChamfBox(vec3($2[0], $2[1], $2[2]), $3); }
  232. | T_FLATCHAMFBOX fv fv fv fv { mc.m_mesh.AppendFlatChamfBox(vec3($2, $3, $4), $5); }
  233. | T_FLATCHAMFBOX fv fv { mc.m_mesh.AppendFlatChamfBox(vec3($2, $2, $2), $3); }
  234. | T_FLATCHAMFBOX v3 fv { mc.m_mesh.AppendFlatChamfBox(vec3($2[0], $2[1], $2[2]), $3); }
  235. | T_STAR iv fv fv bv bv { mc.m_mesh.AppendStar($2, $3, $4, $5, $6); }
  236. | T_STAR iv fv fv bv { mc.m_mesh.AppendStar($2, $3, $4, $5); }
  237. | T_STAR iv fv fv { mc.m_mesh.AppendStar($2, $3, $4); }
  238. | T_EXPANDEDSTAR iv fv fv fv { mc.m_mesh.AppendExpandedStar($2, $3, $4, $5); }
  239. | T_EXPANDEDSTAR iv fv fv { mc.m_mesh.AppendExpandedStar($2, $3, $4); }
  240. | T_DISC iv fv bv { mc.m_mesh.AppendDisc($2, $3, $4); }
  241. | T_DISC iv fv { mc.m_mesh.AppendDisc($2, $3); }
  242. | T_TRIANGLE fv bv { mc.m_mesh.AppendSimpleTriangle($2, $3); }
  243. | T_TRIANGLE fv { mc.m_mesh.AppendSimpleTriangle($2); }
  244. | T_QUAD fv bv { mc.m_mesh.AppendSimpleQuad($2, $3); }
  245. | T_QUAD fv { mc.m_mesh.AppendSimpleQuad($2); }
  246. | T_COG iv fv fv fv fv fv fv fv fv bv { mc.m_mesh.AppendCog($2, $3, $4, $5, $6, $7, $8, $9, $10, $11); }
  247. | T_COG iv fv fv fv fv fv fv fv fv { mc.m_mesh.AppendCog($2, $3, $4, $5, $6, $7, $8, $9, $10); }
  248. | T_COG iv fv fv fv fv fv fv fv { mc.m_mesh.AppendCog($2, $3, $4, $5, $6, $7, $8, $9); }
  249. | T_COG iv fv v2 v2 v2 fv bv { mc.m_mesh.AppendCog($2, $3, $4[0], $4[1], $5[0], $5[1], $6[0], $6[1], $7, $8); }
  250. | T_COG iv fv v2 v2 v2 fv { mc.m_mesh.AppendCog($2, $3, $4[0], $4[1], $5[0], $5[1], $6[0], $6[1], $7); }
  251. | T_COG iv fv v2 v2 v2 { mc.m_mesh.AppendCog($2, $3, $4[0], $4[1], $5[0], $5[1], $6[0], $6[1]); }
  252. ;
  253. /* Base Number types */
  254. fv:
  255. F_NUMBER { $$ = $1; }
  256. | '-' fv { $$ = -$2; }
  257. | I_NUMBER { $$ = (float)$1; }
  258. | '-' iv { $$ = -(float)$2; }
  259. ;
  260. iv:
  261. I_NUMBER { $$ = $1; }
  262. | '-' iv { $$ = -$2; }
  263. | F_NUMBER { $$ = (int)$1; }
  264. | '-' fv { $$ = -(int)$2; }
  265. ;
  266. /* Vector types */
  267. v2:
  268. '('fv')' { $$[0] = $2; $$[1] = $2; }
  269. | '('fv fv')' { $$[0] = $2; $$[1] = $3; }
  270. ;
  271. v3:
  272. '('fv')' { $$[0] = $2; $$[1] = $2; $$[2] = $2; }
  273. | '('fv fv fv')' { $$[0] = $2; $$[1] = $3; $$[2] = $4; }
  274. ;
  275. v4:
  276. '('fv')' { $$[0] = $2; $$[1] = $2; $$[2] = $2; $$[3] = $2; }
  277. | '('fv fv fv fv')' { $$[0] = $2; $$[1] = $3; $$[2] = $4; $$[3] = $5; }
  278. ;
  279. /* Special types */
  280. bv:
  281. BOOLEAN { $$ = $1; }
  282. | I_NUMBER { $$ = !!$1; }
  283. | F_NUMBER { $$ = !!$1; }
  284. ;
  285. %%
  286. void lol::EasyMeshParser::error(const EasyMeshParser::location_type& l,
  287. const std::string& m)
  288. {
  289. mc.Error(l, m);
  290. }