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.
 
 
 

212 rivejä
5.4 KiB

  1. %{
  2. //
  3. // Lol Engine
  4. //
  5. // Copyright: (c) 2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  6. // (c) 2013 Sam Hocevar <sam@hocevar.net>
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the Do What The Fuck You Want To
  9. // Public License, Version 2, as published by Sam Hocevar. See
  10. // http://www.wtfpl.net/ for more details.
  11. //
  12. #if defined HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include "core.h"
  16. #include "../scenesetup.h"
  17. %}
  18. %require "2.3"
  19. %debug
  20. %defines
  21. %skeleton "lalr1.cc"
  22. %name-prefix="lol"
  23. %define parser_class_name "SceneSetupParser"
  24. %locations
  25. %parse-param { class SceneSetupCompiler& uc }
  26. %error-verbose
  27. %union
  28. {
  29. float fval;
  30. int ival;
  31. bool bval;
  32. float vval[4];
  33. int ivval[4];
  34. char* sval;
  35. char* svval;
  36. /* Can't use uin32_t here for some reason */
  37. unsigned u32val;
  38. }
  39. %start sstp_description
  40. %token T_COLOR
  41. %token T_ADDLIGHT T_OBJPOSITION T_OBJLOOKAT T_OBJCOLOR T_CLEARCOLOR
  42. %token T_SHOWGIZMO T_SHOWLIGHT
  43. %token T_CUSTOMCMD
  44. %token T_END 0
  45. %token T_ERROR
  46. %token <fval> F_NUMBER
  47. %token <ival> I_NUMBER
  48. %token <sval> STRING
  49. %token <svval> STRING_VAR
  50. %token <bval> BOOLEAN
  51. %token <u32val> COLOR
  52. /* Base Number types */
  53. %type <fval> fv
  54. %type <ival> iv
  55. /* Vector types */
  56. %type <vval> v3
  57. %type <ivval> iv3
  58. %type <vval> v4
  59. /* Special types */
  60. %type <bval> bv
  61. %type <sval> sv
  62. %type <svval> svv
  63. %{
  64. #include "../scenesetup-compiler.h"
  65. #undef yylex
  66. #define yylex uc.m_lexer->lex
  67. /* HACK: workaround for Bison who insists on using exceptions */
  68. #define try if (true)
  69. #define catch(...) if (false)
  70. #define throw (void)0
  71. %}
  72. %%
  73. sstp_description:
  74. sstp_expression_list T_END
  75. ;
  76. sstp_expression_list:
  77. sstp_expression
  78. | sstp_expression sstp_expression_list
  79. ;
  80. sstp_expression:
  81. sstp_command_list
  82. ;
  83. sstp_command_list:
  84. sstp_command
  85. | sstp_command_list sstp_command
  86. ;
  87. sstp_command:
  88. light_command
  89. | setup_command
  90. | scene_command
  91. | custom_command
  92. ;
  93. light_command:
  94. T_ADDLIGHT { uc.m_sstp.m_lights << new Light(); uc.m_last_cmd = "ADDLIGHT"; }
  95. | T_ADDLIGHT fv { uc.m_sstp.m_lights << new Light(); uc.m_last_cmd = "ADDLIGHT";
  96. uc.m_sstp.m_lights.Last()->SetPosition(vec4(vec3::zero, $2)); }
  97. ;
  98. setup_command:
  99. T_OBJPOSITION v3 { if (uc.m_last_cmd == "ADDLIGHT")
  100. uc.m_sstp.m_lights.Last()->SetPosition(vec4(vec3($2[0], $2[1], $2[2]), uc.m_sstp.m_lights.Last()->GetPosition().w)); }
  101. | T_OBJLOOKAT v3 { if (uc.m_last_cmd == "ADDLIGHT")
  102. { /* */ } }
  103. | T_OBJCOLOR v4 { if (uc.m_last_cmd == "ADDLIGHT")
  104. uc.m_sstp.m_lights.Last()->SetColor(vec4($2[0], $2[1], $2[2], $2[3])); }
  105. | T_OBJCOLOR COLOR { uint32_t x = $2;
  106. ivec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
  107. vec4 vv = vec4(v) * (1.f / 255.f);
  108. if (uc.m_last_cmd == "ADDLIGHT")
  109. uc.m_sstp.m_lights.Last()->SetColor(vv); }
  110. ;
  111. scene_command:
  112. T_CLEARCOLOR v4 { uc.m_sstp.m_clear_color = vec4($2[0], $2[1], $2[2], $2[3]); }
  113. T_CLEARCOLOR v3 { uc.m_sstp.m_clear_color = vec4($2[0], $2[1], $2[2], 1.f); }
  114. | T_CLEARCOLOR COLOR { uint32_t x = $2; ivec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
  115. uc.m_sstp.m_clear_color = vec4(v) * (1.f / 255.f); }
  116. | T_SHOWGIZMO bv { uc.m_sstp.m_show_gizmo = $2; }
  117. | T_SHOWLIGHT bv { uc.m_sstp.m_show_lights = $2; }
  118. ;
  119. custom_command:
  120. T_CUSTOMCMD svv sv { uc.m_sstp.m_custom_cmd.Push($2, $3); }
  121. ;
  122. /* Base Number types */
  123. fv:
  124. F_NUMBER { $$ = $1; }
  125. | '-' fv { $$ = -$2; }
  126. | I_NUMBER { $$ = (float)$1; }
  127. | '-' iv { $$ = -(float)$2; }
  128. ;
  129. iv:
  130. I_NUMBER { $$ = $1; }
  131. | '-' iv { $$ = -$2; }
  132. | F_NUMBER { $$ = (int)$1; }
  133. | '-' fv { $$ = -(int)$2; }
  134. ;
  135. /* Vector types */
  136. v3:
  137. '('fv')' { $$[0] = $2; $$[1] = $2; $$[2] = $2; }
  138. | '('fv fv fv')' { $$[0] = $2; $$[1] = $3; $$[2] = $4; }
  139. ;
  140. iv3:
  141. '('iv')' { $$[0] = $2; $$[1] = $2; $$[2] = $2; }
  142. | '('iv iv iv')' { $$[0] = $2; $$[1] = $3; $$[2] = $4; }
  143. ;
  144. v4:
  145. '('fv')' { $$[0] = $2; $$[1] = $2; $$[2] = $2; $$[3] = $2; }
  146. | '('fv fv fv fv')' { $$[0] = $2; $$[1] = $3; $$[2] = $4; $$[3] = $5; }
  147. ;
  148. /* Special types */
  149. bv:
  150. BOOLEAN { $$ = $1; }
  151. | I_NUMBER { $$ = !!$1; }
  152. | F_NUMBER { $$ = !!$1; }
  153. ;
  154. svv:
  155. STRING_VAR { $$ = $1; }
  156. ;
  157. sv:
  158. STRING { String t = $1;
  159. t.Replace('"', ' ', true);
  160. free($1);
  161. $$ = strdup((const char *)t.C()); }
  162. | STRING sv { String t = $1;
  163. t += $2;
  164. t.Replace('"', ' ', true);
  165. free($1);
  166. free($2);
  167. $$ = strdup((const char *)t.C()); }
  168. ;
  169. %%
  170. void lol::SceneSetupParser::error(const SceneSetupParser::location_type& l,
  171. const std::string& m)
  172. {
  173. uc.Error(l, m);
  174. }