Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

scenesetup-parser.y 4.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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
  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. ;
  109. custom_command:
  110. T_CUSTOMCMD svv sv { uc.m_sstp.m_custom_cmd.Push($2, $3); }
  111. ;
  112. /* Base Number types */
  113. fv:
  114. F_NUMBER { $$ = $1; }
  115. | '-' fv { $$ = -$2; }
  116. | I_NUMBER { $$ = (float)$1; }
  117. | '-' iv { $$ = -(float)$2; }
  118. ;
  119. iv:
  120. I_NUMBER { $$ = $1; }
  121. | '-' iv { $$ = -$2; }
  122. | F_NUMBER { $$ = (int)$1; }
  123. | '-' fv { $$ = -(int)$2; }
  124. ;
  125. /* Vector types */
  126. v3:
  127. '('fv')' { $$[0] = $2; $$[1] = $2; $$[2] = $2; }
  128. | '('fv fv fv')' { $$[0] = $2; $$[1] = $3; $$[2] = $4; }
  129. ;
  130. iv3:
  131. '('iv')' { $$[0] = $2; $$[1] = $2; $$[2] = $2; }
  132. | '('iv iv iv')' { $$[0] = $2; $$[1] = $3; $$[2] = $4; }
  133. ;
  134. v4:
  135. '('fv')' { $$[0] = $2; $$[1] = $2; $$[2] = $2; $$[3] = $2; }
  136. | '('fv fv fv fv')' { $$[0] = $2; $$[1] = $3; $$[2] = $4; $$[3] = $5; }
  137. ;
  138. /* Special types */
  139. bv:
  140. BOOLEAN { $$ = $1; }
  141. | I_NUMBER { $$ = !!$1; }
  142. | F_NUMBER { $$ = !!$1; }
  143. ;
  144. svv:
  145. STRING_VAR { $$ = $1; }
  146. ;
  147. sv:
  148. STRING { String t = $1;
  149. t.Replace('"', ' ', true);
  150. free($1);
  151. $$ = strdup((const char *)t.C()); }
  152. | STRING sv { String t = $1;
  153. t += $2;
  154. t.Replace('"', ' ', true);
  155. free($1);
  156. free($2);
  157. $$ = strdup((const char *)t.C()); }
  158. ;
  159. %%
  160. void lol::SceneSetupParser::error(const SceneSetupParser::location_type& l,
  161. const std::string& m)
  162. {
  163. uc.Error(l, m);
  164. }