25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

scenesetup-parser.y 5.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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_CUSTOMCMD
  43. %token T_END 0
  44. %token T_ERROR
  45. %token <fval> F_NUMBER
  46. %token <ival> I_NUMBER
  47. %token <sval> STRING
  48. %token <svval> STRING_VAR
  49. %token <bval> BOOLEAN
  50. %token <u32val> COLOR
  51. /* Base Number types */
  52. %type <fval> fv
  53. %type <ival> iv
  54. /* Vector types */
  55. %type <vval> v3
  56. %type <ivval> iv3
  57. %type <vval> v4
  58. /* Special types */
  59. %type <bval> bv
  60. %type <sval> sv
  61. %type <svval> svv
  62. %{
  63. #include "../scenesetup-compiler.h"
  64. #undef yylex
  65. #define yylex uc.m_lexer->lex
  66. /* HACK: workaround for Bison who insists on using exceptions */
  67. #define try if (true)
  68. #define catch(...) if (false)
  69. #define throw (void)0
  70. %}
  71. %%
  72. sstp_description:
  73. sstp_expression_list T_END
  74. ;
  75. sstp_expression_list:
  76. sstp_expression
  77. | sstp_expression sstp_expression_list
  78. ;
  79. sstp_expression:
  80. sstp_command_list
  81. ;
  82. sstp_command_list:
  83. sstp_command
  84. | sstp_command_list sstp_command
  85. ;
  86. sstp_command:
  87. light_command
  88. | setup_command
  89. | custom_command
  90. ;
  91. light_command:
  92. T_ADDLIGHT { uc.m_sstp.m_lights << new Light(); uc.m_last_cmd = "ADDLIGHT"; }
  93. | T_ADDLIGHT fv { uc.m_sstp.m_lights << new Light(); uc.m_last_cmd = "ADDLIGHT";
  94. uc.m_sstp.m_lights.Last()->SetPosition(vec4(vec3::zero, $2)); }
  95. ;
  96. setup_command:
  97. T_OBJPOSITION v3 { if (uc.m_last_cmd == "ADDLIGHT")
  98. uc.m_sstp.m_lights.Last()->SetPosition(vec4(vec3($2[0], $2[1], $2[2]), uc.m_sstp.m_lights.Last()->GetPosition().w)); }
  99. | T_OBJLOOKAT v3 { if (uc.m_last_cmd == "ADDLIGHT")
  100. { /* */ } }
  101. | T_OBJCOLOR v4 { if (uc.m_last_cmd == "ADDLIGHT")
  102. uc.m_sstp.m_lights.Last()->SetColor(vec4($2[0], $2[1], $2[2], $2[3])); }
  103. | T_OBJCOLOR COLOR { uint32_t x = $2;
  104. ivec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
  105. vec4 vv = vec4(v) * (1.f / 255.f);
  106. if (uc.m_last_cmd == "ADDLIGHT")
  107. uc.m_sstp.m_lights.Last()->SetColor(vv); }
  108. | T_CLEARCOLOR v4 { uc.m_sstp.m_clear_color = vec4($2[0], $2[1], $2[2], $2[3]); }
  109. | T_CLEARCOLOR COLOR { uint32_t x = $2; ivec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
  110. uc.m_sstp.m_clear_color = vec4(v) * (1.f / 255.f); }
  111. ;
  112. custom_command:
  113. T_CUSTOMCMD svv sv { uc.m_sstp.m_custom_cmd.Push($2, $3); }
  114. ;
  115. /* Base Number types */
  116. fv:
  117. F_NUMBER { $$ = $1; }
  118. | '-' fv { $$ = -$2; }
  119. | I_NUMBER { $$ = (float)$1; }
  120. | '-' iv { $$ = -(float)$2; }
  121. ;
  122. iv:
  123. I_NUMBER { $$ = $1; }
  124. | '-' iv { $$ = -$2; }
  125. | F_NUMBER { $$ = (int)$1; }
  126. | '-' fv { $$ = -(int)$2; }
  127. ;
  128. /* Vector types */
  129. v3:
  130. '('fv')' { $$[0] = $2; $$[1] = $2; $$[2] = $2; }
  131. | '('fv fv fv')' { $$[0] = $2; $$[1] = $3; $$[2] = $4; }
  132. ;
  133. iv3:
  134. '('iv')' { $$[0] = $2; $$[1] = $2; $$[2] = $2; }
  135. | '('iv iv iv')' { $$[0] = $2; $$[1] = $3; $$[2] = $4; }
  136. ;
  137. v4:
  138. '('fv')' { $$[0] = $2; $$[1] = $2; $$[2] = $2; $$[3] = $2; }
  139. | '('fv fv fv fv')' { $$[0] = $2; $$[1] = $3; $$[2] = $4; $$[3] = $5; }
  140. ;
  141. /* Special types */
  142. bv:
  143. BOOLEAN { $$ = $1; }
  144. | I_NUMBER { $$ = !!$1; }
  145. | F_NUMBER { $$ = !!$1; }
  146. ;
  147. svv:
  148. STRING_VAR { $$ = $1; }
  149. ;
  150. sv:
  151. STRING { String t = $1;
  152. t.Replace('"', ' ', true);
  153. free($1);
  154. $$ = strdup((const char *)t.C()); }
  155. | STRING sv { String t = $1;
  156. t += $2;
  157. t.Replace('"', ' ', true);
  158. free($1);
  159. free($2);
  160. $$ = strdup((const char *)t.C()); }
  161. ;
  162. %%
  163. void lol::SceneSetupParser::error(const SceneSetupParser::location_type& l,
  164. const std::string& m)
  165. {
  166. uc.Error(l, m);
  167. }