220 řádky
8.4 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. /* Can't use uin32_t here for some reason */
  33. unsigned u32val;
  34. struct { float f0, f1, f2, f3, f4, f5, f6, f7, f8, f9; } args;
  35. }
  36. %start mesh_description
  37. %token T_COLOR T_BGCOLOR
  38. %token T_TRANSLATEX T_ROTATEX T_TAPERX T_TWISTX T_SHEARX T_STRETCHX T_BENDXY T_BENDXZ T_SCALEX T_MIRRORX
  39. %token T_TRANSLATEY T_ROTATEY T_TAPERY T_TWISTY T_SHEARY T_STRETCHY T_BENDYX T_BENDYZ T_SCALEY T_MIRRORY
  40. %token T_TRANSLATEZ T_ROTATEZ T_TAPERZ T_TWISTZ T_SHEARZ T_STRETCHZ T_BENDZX T_BENDZY T_SCALEZ T_MIRRORZ
  41. %token T_TRANSLATE T_SCALE T_TOGGLESCALEWINDING T_RADIALJITTER T_SPLITTRIANGLE
  42. %token T_CSGUNION T_CSGSUBSTRACT T_CSGSUBSTRACTLOSS T_CSGAND T_CSGXOR
  43. %token T_CHAMFER
  44. %token T_CYLINDER T_BOX T_SMOOTHCHAMFBOX T_FLATCHAMFBOX T_SPHERE T_CAPSULE
  45. %token T_STAR T_EXPANDEDSTAR T_DISC T_TRIANGLE T_QUAD T_COG T_TORUS
  46. %token T_END 0
  47. %token T_ERROR
  48. %token <fval> NUMBER
  49. %token <u32val> COLOR
  50. %type <fval> number
  51. %type <args> args1 args2 args3 args4 args5 args6 args7 args8 args9 args10
  52. %{
  53. #include "easymesh/easymesh-compiler.h"
  54. #undef yylex
  55. #define yylex mc.m_lexer->lex
  56. %}
  57. %%
  58. mesh_description:
  59. mesh_expression_list T_END
  60. ;
  61. mesh_expression_list:
  62. mesh_expression
  63. | mesh_expression mesh_expression_list
  64. ;
  65. mesh_expression:
  66. mesh_command_list
  67. | mesh_open mesh_expression_list mesh_close
  68. ;
  69. mesh_open:
  70. '[' { mc.m_mesh.OpenBrace(); }
  71. ;
  72. mesh_close:
  73. ']' { mc.m_mesh.CloseBrace(); }
  74. ;
  75. mesh_command_list:
  76. mesh_command
  77. | mesh_command_list mesh_command
  78. ;
  79. mesh_command:
  80. color_command
  81. | transform_command
  82. | primitive_command
  83. ;
  84. color_command:
  85. T_COLOR args4 { mc.m_mesh.SetCurColor(vec4($2.f0, $2.f1, $2.f2, $2.f3)); }
  86. | T_COLOR COLOR { uint32_t x = $2;
  87. vec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
  88. mc.m_mesh.SetCurColor(vec4(v) * (1.f / 255)); }
  89. | T_BGCOLOR args4 { mc.m_mesh.SetCurColor2(vec4($2.f0, $2.f1, $2.f2, $2.f3)); }
  90. | T_BGCOLOR COLOR { uint32_t x = $2;
  91. vec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
  92. mc.m_mesh.SetCurColor2(vec4(v) * (1.f / 255)); }
  93. ;
  94. transform_command:
  95. T_CHAMFER args1 { mc.m_mesh.Chamfer($2.f0); }
  96. | T_TRANSLATEX args1 { mc.m_mesh.Translate(vec3($2.f0, 0, 0)); }
  97. | T_TRANSLATEY args1 { mc.m_mesh.Translate(vec3(0, $2.f0, 0)); }
  98. | T_TRANSLATEZ args1 { mc.m_mesh.Translate(vec3(0, 0, $2.f0)); }
  99. | T_TRANSLATE args3 { mc.m_mesh.Translate(vec3($2.f0, $2.f1, $2.f2)); }
  100. | T_ROTATEX args1 { mc.m_mesh.RotateX($2.f0); }
  101. | T_ROTATEY args1 { mc.m_mesh.RotateY($2.f0); }
  102. | T_ROTATEZ args1 { mc.m_mesh.RotateZ($2.f0); }
  103. | T_TAPERX args3 { mc.m_mesh.TaperX($2.f0, $2.f1, $2.f2); }
  104. | T_TAPERX args4 { mc.m_mesh.TaperX($2.f0, $2.f1, $2.f2, $2.f3); }
  105. | T_TAPERY args3 { mc.m_mesh.TaperY($2.f0, $2.f1, $2.f2); }
  106. | T_TAPERY args4 { mc.m_mesh.TaperY($2.f0, $2.f1, $2.f2, $2.f3); }
  107. | T_TAPERZ args3 { mc.m_mesh.TaperZ($2.f0, $2.f1, $2.f2); }
  108. | T_TAPERZ args4 { mc.m_mesh.TaperZ($2.f0, $2.f1, $2.f2, $2.f3); }
  109. | T_TWISTX args2 { mc.m_mesh.TwistX($2.f0, $2.f1); }
  110. | T_TWISTY args2 { mc.m_mesh.TwistY($2.f0, $2.f1); }
  111. | T_TWISTZ args2 { mc.m_mesh.TwistZ($2.f0, $2.f1); }
  112. | T_SHEARX args3 { mc.m_mesh.ShearX($2.f0, $2.f1, $2.f2); }
  113. | T_SHEARX args4 { mc.m_mesh.ShearX($2.f0, $2.f1, $2.f2, $2.f3); }
  114. | T_SHEARY args3 { mc.m_mesh.ShearY($2.f0, $2.f1, $2.f2); }
  115. | T_SHEARY args4 { mc.m_mesh.ShearY($2.f0, $2.f1, $2.f2, $2.f3); }
  116. | T_SHEARZ args3 { mc.m_mesh.ShearZ($2.f0, $2.f1, $2.f2); }
  117. | T_SHEARZ args4 { mc.m_mesh.ShearZ($2.f0, $2.f1, $2.f2, $2.f3); }
  118. | T_STRETCHX args3 { mc.m_mesh.StretchX($2.f0, $2.f1, $2.f2); }
  119. | T_STRETCHY args3 { mc.m_mesh.StretchY($2.f0, $2.f1, $2.f2); }
  120. | T_STRETCHZ args3 { mc.m_mesh.StretchZ($2.f0, $2.f1, $2.f2); }
  121. | T_BENDXY args2 { mc.m_mesh.BendXY($2.f0, $2.f1); }
  122. | T_BENDXZ args2 { mc.m_mesh.BendXZ($2.f0, $2.f1); }
  123. | T_BENDYX args2 { mc.m_mesh.BendYX($2.f0, $2.f1); }
  124. | T_BENDYZ args2 { mc.m_mesh.BendYZ($2.f0, $2.f1); }
  125. | T_BENDZX args2 { mc.m_mesh.BendZX($2.f0, $2.f1); }
  126. | T_BENDZY args2 { mc.m_mesh.BendZY($2.f0, $2.f1); }
  127. | T_SCALEX args1 { mc.m_mesh.Scale(vec3($2.f0, 1.0, 1.0)); }
  128. | T_SCALEY args1 { mc.m_mesh.Scale(vec3(1.0, $2.f0, 1.0)); }
  129. | T_SCALEZ args1 { mc.m_mesh.Scale(vec3(1.0, 1.0, $2.f0)); }
  130. | T_SCALE args3 { mc.m_mesh.Scale(vec3($2.f0, $2.f1, $2.f2)); }
  131. | T_MIRRORX { mc.m_mesh.MirrorX(); }
  132. | T_MIRRORY { mc.m_mesh.MirrorY(); }
  133. | T_MIRRORZ { mc.m_mesh.MirrorZ(); }
  134. | T_RADIALJITTER args1 { mc.m_mesh.RadialJitter($2.f0); }
  135. | T_SPLITTRIANGLE args1 { mc.m_mesh.SplitTriangles($2.f0); }
  136. | T_TOGGLESCALEWINDING { mc.m_mesh.ToggleScaleWinding(); }
  137. | T_CSGUNION { mc.m_mesh.CsgUnion(); }
  138. | T_CSGSUBSTRACT { mc.m_mesh.CsgSubstract(); }
  139. | T_CSGSUBSTRACTLOSS { mc.m_mesh.CsgSubstractLoss(); }
  140. | T_CSGAND { mc.m_mesh.CsgAnd(); }
  141. | T_CSGXOR { mc.m_mesh.CsgXor(); }
  142. ;
  143. primitive_command:
  144. T_CYLINDER args6 { mc.m_mesh.AppendCylinder((int)$2.f0, $2.f1,
  145. $2.f2, $2.f3,
  146. (int)$2.f4, (int)$2.f5, 0); }
  147. | T_CYLINDER args7 { mc.m_mesh.AppendCylinder((int)$2.f0, $2.f1,
  148. $2.f2, $2.f3,
  149. (int)$2.f4, (int)$2.f5, (int)$2.f6); }
  150. | T_BOX args3 { mc.m_mesh.AppendBox(vec3($2.f0, $2.f1, $2.f2)); }
  151. | T_SMOOTHCHAMFBOX args4 { mc.m_mesh.AppendSmoothChamfBox(vec3($2.f0, $2.f1,
  152. $2.f2), $2.f3); }
  153. | T_FLATCHAMFBOX args4 { mc.m_mesh.AppendFlatChamfBox(vec3($2.f0, $2.f1,
  154. $2.f2), $2.f3); }
  155. | T_SPHERE args2 { mc.m_mesh.AppendSphere($2.f0, $2.f1); }
  156. | T_CAPSULE args3 { mc.m_mesh.AppendCapsule($2.f0, $2.f1, $2.f2); }
  157. | T_TORUS args3 { mc.m_mesh.AppendTorus((int)$2.f0, $2.f1, $2.f2); }
  158. | T_STAR args5 { mc.m_mesh.AppendStar((int)$2.f0, $2.f1, $2.f2,
  159. (int)$2.f3, (int)$2.f4); }
  160. | T_EXPANDEDSTAR args4 { mc.m_mesh.AppendExpandedStar((int)$2.f0, $2.f1,
  161. $2.f2, $2.f3); }
  162. | T_DISC args3 { mc.m_mesh.AppendDisc((int)$2.f0, $2.f1, (int)$2.f2); }
  163. | T_TRIANGLE args2 { mc.m_mesh.AppendSimpleTriangle($2.f0, (int)$2.f1); }
  164. | T_QUAD args2 { mc.m_mesh.AppendSimpleQuad($2.f0, (int)$2.f1); }
  165. | T_COG args10 { mc.m_mesh.AppendCog((int)$2.f0, $2.f1,
  166. $2.f2, $2.f3, $2.f4, $2.f5, $2.f6,
  167. $2.f7, $2.f8, (int)$2.f9); }
  168. ;
  169. args1: number { $$.f0 = $1; } ;
  170. args2: args1 number { $$ = $1; $$.f1 = $2; } ;
  171. args3: args2 number { $$ = $1; $$.f2 = $2; } ;
  172. args4: args3 number { $$ = $1; $$.f3 = $2; } ;
  173. args5: args4 number { $$ = $1; $$.f4 = $2; } ;
  174. args6: args5 number { $$ = $1; $$.f5 = $2; } ;
  175. args7: args6 number { $$ = $1; $$.f6 = $2; } ;
  176. args8: args7 number { $$ = $1; $$.f7 = $2; } ;
  177. args9: args8 number { $$ = $1; $$.f8 = $2; } ;
  178. args10: args9 number { $$ = $1; $$.f9 = $2; } ;
  179. number:
  180. NUMBER { $$ = $1; }
  181. | '-' number { $$ = -$2; }
  182. ;
  183. %%
  184. void lol::EasyMeshParser::error(const EasyMeshParser::location_type& l,
  185. const std::string& m)
  186. {
  187. mc.Error(l, m);
  188. }