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.

799 lines
32 KiB

  1. %{
  2. //
  3. // Lol Engine
  4. //
  5. // Copyright © 2010-2015 Sam Hocevar <sam@hocevar.net>
  6. //
  7. // This program is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #if defined HAVE_CONFIG_H
  14. # include "config.h"
  15. #endif
  16. #include <cstdlib>
  17. using std::exit;
  18. using std::malloc;
  19. using std::realloc;
  20. using std::free;
  21. #include <iostream>
  22. #include "core.h"
  23. #include "gpu/lolfx-compiler.h"
  24. typedef lol::LolFxParser::token token;
  25. typedef lol::LolFxParser::token_type token_type;
  26. #ifndef YY_DECL
  27. # define YY_DECL lol::LolFxParser::token_type \
  28. lol::LolFxScanner::lex(lol::LolFxParser::semantic_type* yylval, \
  29. lol::LolFxParser::location_type* yylloc)
  30. #endif
  31. #define yyterminate() return token::T_END
  32. #define YY_NO_UNISTD_H
  33. #define YY_USER_ACTION yylloc->columns(yyleng);
  34. %}
  35. %option c++ prefix="LolFx"
  36. %option batch yywrap nounput stack
  37. %x C_COMMENT
  38. %x CPP_COMMENT
  39. %x PREPROCESSOR_COMMENT
  40. %%
  41. %{
  42. /* reset location at the beginning of yylex() */
  43. yylloc->step();
  44. int language_guess = INITIAL;
  45. m_pp_stack.Push(BlockIsTrue);
  46. %}
  47. /*
  48. * GLSL keywords
  49. */
  50. "subroutine" { return token::GT_SUBROUTINE; }
  51. "patch" { return token::GT_PATCH; }
  52. "sample" { return token::GT_SAMPLE; }
  53. "flat" { return token::GT_FLAT; }
  54. "smooth" { return token::GT_SMOOTH; }
  55. "layout" { return token::GT_LAYOUT; }
  56. "sampler1dshadow" { return token::GT_SAMPLER1DSHADOW; }
  57. "sampler2dshadow" { return token::GT_SAMPLER2DSHADOW; }
  58. "samplercubeshadow" { return token::GT_SAMPLERCUBESHADOW; }
  59. "sampler1darray" { return token::GT_SAMPLER1DARRAY; }
  60. "sampler2darray" { return token::GT_SAMPLER2DARRAY; }
  61. "sampler1darrayshadow" { return token::GT_SAMPLER1DARRAYSHADOW; }
  62. "sampler2darrayshadow" { return token::GT_SAMPLER2DARRAYSHADOW; }
  63. "isampler1d" { return token::GT_ISAMPLER1D; }
  64. "isampler2d" { return token::GT_ISAMPLER2D; }
  65. "isampler3d" { return token::GT_ISAMPLER3D; }
  66. "isamplercube" { return token::GT_ISAMPLERCUBE; }
  67. "isampler1darray" { return token::GT_ISAMPLER1DARRAY; }
  68. "isampler2darray" { return token::GT_ISAMPLER2DARRAY; }
  69. "usampler1d" { return token::GT_USAMPLER1D; }
  70. "usampler2d" { return token::GT_USAMPLER2D; }
  71. "usampler3d" { return token::GT_USAMPLER3D; }
  72. "usamplercube" { return token::GT_USAMPLERCUBE; }
  73. "usampler1darray" { return token::GT_USAMPLER1DARRAY; }
  74. "usampler2darray" { return token::GT_USAMPLER2DARRAY; }
  75. "sampler2drect" { return token::GT_SAMPLER2DRECT; }
  76. "sampler2drectshadow" { return token::GT_SAMPLER2DRECTSHADOW; }
  77. "isampler2drect" { return token::GT_ISAMPLER2DRECT; }
  78. "usampler2drect" { return token::GT_USAMPLER2DRECT; }
  79. "samplerbuffer" { return token::GT_SAMPLERBUFFER; }
  80. "isamplerbuffer" { return token::GT_ISAMPLERBUFFER; }
  81. "usamplerbuffer" { return token::GT_USAMPLERBUFFER; }
  82. "samplercubearray" { return token::GT_SAMPLERCUBEARRAY; }
  83. "samplercubearrayshadow" { return token::GT_SAMPLERCUBEARRAYSHADOW; }
  84. "isamplercubearray" { return token::GT_ISAMPLERCUBEARRAY; }
  85. "usamplercubearray" { return token::GT_USAMPLERCUBEARRAY; }
  86. "sampler2dms" { return token::GT_SAMPLER2DMS; }
  87. "isampler2dms" { return token::GT_ISAMPLER2DMS; }
  88. "usampler2dms" { return token::GT_USAMPLER2DMS; }
  89. "sampler2dmsarray" { return token::GT_SAMPLER2DMSARRAY; }
  90. "isampler2dmsarray" { return token::GT_ISAMPLER2DMSARRAY; }
  91. "usampler2dmsarray" { return token::GT_USAMPLER2DMSARRAY; }
  92. "highp" { return token::GT_HIGHP; }
  93. "mediump" { return token::GT_MEDIUMP; }
  94. "lowp" { return token::GT_LOWP; }
  95. "precision" { return token::GT_PRECISION; }
  96. "invariant" { return token::GT_INVARIANT; }
  97. /*
  98. * Deprecated GLSL keywords
  99. */
  100. "attribute" { return token::GT_ATTRIBUTE; }
  101. "varying" { return token::GT_VARYING; }
  102. /*
  103. * GLSL vector types
  104. */
  105. "bvec2" { return token::GT_BVEC2; }
  106. "bvec3" { return token::GT_BVEC3; }
  107. "bvec4" { return token::GT_BVEC4; }
  108. "ivec2" { return token::GT_IVEC2; }
  109. "ivec3" { return token::GT_IVEC3; }
  110. "ivec4" { return token::GT_IVEC4; }
  111. "uvec2" { return token::GT_UVEC2; }
  112. "uvec3" { return token::GT_UVEC3; }
  113. "uvec4" { return token::GT_UVEC4; }
  114. "vec2" { return token::GT_VEC2; }
  115. "vec3" { return token::GT_VEC3; }
  116. "vec4" { return token::GT_VEC4; }
  117. "mat2" { return token::GT_MAT2; }
  118. "mat2x2" { return token::GT_MAT2X2; }
  119. "mat2x3" { return token::GT_MAT2X3; }
  120. "mat2x4" { return token::GT_MAT2X4; }
  121. "mat3" { return token::GT_MAT3; }
  122. "mat3x2" { return token::GT_MAT3X2; }
  123. "mat3x3" { return token::GT_MAT3X3; }
  124. "mat3x4" { return token::GT_MAT3X4; }
  125. "mat4" { return token::GT_MAT4; }
  126. "mat4x2" { return token::GT_MAT4X2; }
  127. "mat4x3" { return token::GT_MAT4X3; }
  128. "mat4x4" { return token::GT_MAT4X4; }
  129. "dvec2" { return token::GT_DVEC2; }
  130. "dvec3" { return token::GT_DVEC3; }
  131. "dvec4" { return token::GT_DVEC4; }
  132. "dmat2" { return token::GT_DMAT2; }
  133. "dmat2x2" { return token::GT_DMAT2X2; }
  134. "dmat2x3" { return token::GT_DMAT2X3; }
  135. "dmat2x4" { return token::GT_DMAT2X4; }
  136. "dmat3" { return token::GT_DMAT3; }
  137. "dmat3x2" { return token::GT_DMAT3X2; }
  138. "dmat3x3" { return token::GT_DMAT3X3; }
  139. "dmat3x4" { return token::GT_DMAT3X4; }
  140. "dmat4" { return token::GT_DMAT4; }
  141. "dmat4x2" { return token::GT_DMAT4X2; }
  142. "dmat4x3" { return token::GT_DMAT4X3; }
  143. "dmat4x4" { return token::GT_DMAT4X4; }
  144. /*
  145. * GLSL constant values
  146. */
  147. "true" { yylval->ival = 1; return token::BOOLCONSTANT; }
  148. "false" { yylval->ival = 0; return token::BOOLCONSTANT; }
  149. /*
  150. * GLSL keywords that are also valid or reserved in HLSL
  151. * and HLSL keywords that are also valid or reserved in GLSL.
  152. */
  153. "bool" { return token::GHT_BOOL; }
  154. "break" { return token::GHT_BREAK; }
  155. "case" { return token::GHT_CASE; }
  156. "centroid" { return token::GHT_CENTROID; }
  157. "const" { return token::GHT_CONST; }
  158. "continue" { return token::GHT_CONTINUE; }
  159. "default" { return token::GHT_DEFAULT; }
  160. "discard" { return token::GHT_DISCARD; }
  161. "do" { return token::GHT_DO; }
  162. "double" { return token::GHT_DOUBLE; }
  163. "else" { return token::GHT_ELSE; }
  164. "float" { return token::GHT_FLOAT; }
  165. "for" { return token::GHT_FOR; }
  166. "if" { return token::GHT_IF; }
  167. "in" { return token::GHT_IN; }
  168. "inout" { return token::GHT_INOUT; }
  169. "int" { return token::GHT_INT; }
  170. "noperspective" { return token::GHT_NOPERSPECTIVE; }
  171. "out" { return token::GHT_OUT; }
  172. "return" { return token::GHT_RETURN; }
  173. "sampler1d" { return token::GHT_SAMPLER1D; }
  174. "sampler2d" { return token::GHT_SAMPLER2D; }
  175. "sampler3d" { return token::GHT_SAMPLER3D; }
  176. "samplercube" { return token::GHT_SAMPLERCUBE; }
  177. "struct" { return token::GHT_STRUCT; }
  178. "switch" { return token::GHT_SWITCH; }
  179. "uint" { return token::GHT_UINT; }
  180. "uniform" { return token::GHT_UNIFORM; }
  181. "void" { return token::GHT_VOID; }
  182. "while" { return token::GHT_WHILE; }
  183. (?i:bool) { return token::HGT_BOOL; }
  184. (?i:break) { return token::HGT_BREAK; }
  185. (?i:case) { return token::HGT_CASE; }
  186. (?i:centroid) { return token::HGT_CENTROID; }
  187. (?i:const) { return token::HGT_CONST; }
  188. (?i:continue) { return token::HGT_CONTINUE; }
  189. (?i:default) { return token::HGT_DEFAULT; }
  190. (?i:discard) { return token::HGT_DISCARD; }
  191. (?i:do) { return token::HGT_DO; }
  192. (?i:double) { return token::HGT_DOUBLE; }
  193. (?i:else) { return token::HGT_ELSE; }
  194. (?i:float) { return token::HGT_FLOAT; }
  195. (?i:for) { return token::HGT_FOR; }
  196. (?i:if) { return token::HGT_IF; }
  197. (?i:in) { return token::HGT_IN; }
  198. (?i:inout) { return token::HGT_INOUT; }
  199. (?i:int) { return token::HGT_INT; }
  200. (?i:noperspective) { return token::HGT_NOPERSPECTIVE; }
  201. (?i:out) { return token::HGT_OUT; }
  202. (?i:return) { return token::HGT_RETURN; }
  203. (?i:sampler1d) { return token::HGT_SAMPLER1D; }
  204. (?i:sampler2d) { return token::HGT_SAMPLER2D; }
  205. (?i:sampler3d) { return token::HGT_SAMPLER3D; }
  206. (?i:samplercube) { return token::HGT_SAMPLERCUBE; }
  207. (?i:struct) { return token::HGT_STRUCT; }
  208. (?i:switch) { return token::HGT_SWITCH; }
  209. (?i:uint) { return token::HGT_UINT; }
  210. (?i:uniform) { return token::HGT_UNIFORM; }
  211. (?i:void) { return token::HGT_VOID; }
  212. (?i:while) { return token::HGT_WHILE; }
  213. /*
  214. * HLSL keywords
  215. * Gathered from http://msdn.microsoft.com/en-us/library/windows/desktop/bb509568%28v=vs.85%29.aspx
  216. */
  217. (?i:appendstructuredbuffer) { return token::HT_APPENDSTRUCTUREDBUFFER; }
  218. (?i:asm) { return token::HT_ASM; }
  219. (?i:asm_fragment) { return token::HT_ASM_FRAGMENT; }
  220. (?i:blendstate) { return token::HT_BLENDSTATE; }
  221. (?i:buffer) { return token::HT_BUFFER; }
  222. (?i:byteaddressbuffer) { return token::HT_BYTEADDRESSBUFFER; }
  223. (?i:cbuffer) { return token::HT_CBUFFER; }
  224. (?i:column_major) { return token::HT_COLUMN_MAJOR; }
  225. (?i:compile) { return token::HT_COMPILE; }
  226. (?i:compile_fragment) { return token::HT_COMPILE_FRAGMENT; }
  227. (?i:compileshader) { return token::HT_COMPILESHADER; }
  228. (?i:computeshader) { return token::HT_COMPUTESHADER; }
  229. (?i:consumestructuredbuffer) { return token::HT_CONSUMESTRUCTUREDBUFFER; }
  230. (?i:depthstencilstate) { return token::HT_DEPTHSTENCILSTATE; }
  231. (?i:depthstencilview) { return token::HT_DEPTHSTENCILVIEW; }
  232. (?i:domainshader) { return token::HT_DOMAINSHADER; }
  233. (?i:dword) { return token::HT_DWORD; }
  234. (?i:extern) { return token::HT_EXTERN; }
  235. (?i:false) { return token::HT_FALSE; }
  236. (?i:fxgroup) { return token::HT_FXGROUP; }
  237. (?i:geometryshader) { return token::HT_GEOMETRYSHADER; }
  238. (?i:groupshared) { return token::HT_GROUPSHARED; }
  239. (?i:half) { return token::HT_HALF; }
  240. (?i:hullshader) { return token::HT_HULLSHADER; }
  241. (?i:inline) { return token::HT_INLINE; }
  242. (?i:inputpatch) { return token::HT_INPUTPATCH; }
  243. (?i:interface) { return token::HT_INTERFACE; }
  244. (?i:line) { return token::HT_LINE; }
  245. (?i:lineadj) { return token::HT_LINEADJ; }
  246. (?i:linear) { return token::HT_LINEAR; }
  247. (?i:linestream) { return token::HT_LINESTREAM; }
  248. (?i:matrix) { return token::HT_MATRIX; }
  249. (?i:namespace) { return token::HT_NAMESPACE; }
  250. (?i:nointerpolation) { return token::HT_NOINTERPOLATION; }
  251. (?i:null) { return token::HT_NULL; }
  252. (?i:outputpatch) { return token::HT_OUTPUTPATCH; }
  253. (?i:packoffset) { return token::HT_PACKOFFSET; }
  254. (?i:pass) { return token::HT_PASS; }
  255. (?i:pixelfragment) { return token::HT_PIXELFRAGMENT; }
  256. (?i:pixelshader) { return token::HT_PIXELSHADER; }
  257. (?i:point) { return token::HT_POINT; }
  258. (?i:pointstream) { return token::HT_POINTSTREAM; }
  259. (?i:precise) { return token::HT_PRECISE; }
  260. (?i:rasterizerstate) { return token::HT_RASTERIZERSTATE; }
  261. (?i:rendertargetview) { return token::HT_RENDERTARGETVIEW; }
  262. (?i:register) { return token::HT_REGISTER; }
  263. (?i:row_major) { return token::HT_ROW_MAJOR; }
  264. (?i:rwbuffer) { return token::HT_RWBUFFER; }
  265. (?i:rwbyteaddressbuffer) { return token::HT_RWBYTEADDRESSBUFFER; }
  266. (?i:rwstructuredbuffer) { return token::HT_RWSTRUCTUREDBUFFER; }
  267. (?i:rwtexture1d) { return token::HT_RWTEXTURE1D; }
  268. (?i:rwtexture1darray) { return token::HT_RWTEXTURE1DARRAY; }
  269. (?i:rwtexture2d) { return token::HT_RWTEXTURE2D; }
  270. (?i:rwtexture2darray) { return token::HT_RWTEXTURE2DARRAY; }
  271. (?i:rwtexture3d) { return token::HT_RWTEXTURE3D; }
  272. (?i:sampler) { return token::HT_SAMPLER; }
  273. (?i:sampler_state) { return token::HT_SAMPLER_STATE; }
  274. (?i:samplerstate) { return token::HT_SAMPLERSTATE; }
  275. (?i:samplercomparisonstate) { return token::HT_SAMPLERCOMPARISONSTATE; }
  276. (?i:shared) { return token::HT_SHARED; }
  277. (?i:snorm) { return token::HT_SNORM; }
  278. (?i:stateblock) { return token::HT_STATEBLOCK; }
  279. (?i:stateblock_state) { return token::HT_STATEBLOCK_STATE; }
  280. (?i:static) { return token::HT_STATIC; }
  281. (?i:string) { return token::HT_STRING; }
  282. (?i:structuredbuffer) { return token::HT_STRUCTUREDBUFFER; }
  283. (?i:tbuffer) { return token::HT_TBUFFER; }
  284. (?i:technique) { return token::HT_TECHNIQUE; }
  285. (?i:technique10) { return token::HT_TECHNIQUE10; }
  286. (?i:technique11xz) { return token::HT_TECHNIQUE11XZ; }
  287. "texture" { return token::HT_TEXTURE; }
  288. (?i:texture1d) { return token::HT_TEXTURE1D; }
  289. (?i:texture1darray) { return token::HT_TEXTURE1DARRAY; }
  290. (?i:texture2d) { return token::HT_TEXTURE2D; }
  291. (?i:texture2darray) { return token::HT_TEXTURE2DARRAY; }
  292. (?i:texture2dms) { return token::HT_TEXTURE2DMS; }
  293. (?i:texture2dmsarray) { return token::HT_TEXTURE2DMSARRAY; }
  294. (?i:texture3d) { return token::HT_TEXTURE3D; }
  295. (?i:texturecube) { return token::HT_TEXTURECUBE; }
  296. (?i:texturecubearray) { return token::HT_TEXTURECUBEARRAY; }
  297. (?i:true) { return token::HT_TRUE; }
  298. (?i:typedef) { return token::HT_TYPEDEF; }
  299. (?i:triangle) { return token::HT_TRIANGLE; }
  300. (?i:triangleadj) { return token::HT_TRIANGLEADJ; }
  301. (?i:trianglestream) { return token::HT_TRIANGLESTREAM; }
  302. (?i:unorm) { return token::HT_UNORM; }
  303. (?i:vector) { return token::HT_VECTOR; }
  304. (?i:vertexfragment) { return token::HT_VERTEXFRAGMENT; }
  305. (?i:vertexshader) { return token::HT_VERTEXSHADER; }
  306. (?i:volatile) { return token::HT_VOLATILE; }
  307. /*
  308. * HLSL vector types
  309. * TODO: this can probably be factored somehow
  310. */
  311. (?i:bool1) { return token::HT_BOOL1; }
  312. (?i:bool1x1) { return token::HT_BOOL1x1; }
  313. (?i:bool2x1) { return token::HT_BOOL2x1; }
  314. (?i:bool3x1) { return token::HT_BOOL3x1; }
  315. (?i:bool4x1) { return token::HT_BOOL4x1; }
  316. (?i:bool2) { return token::HT_BOOL2; }
  317. (?i:bool1x2) { return token::HT_BOOL1x2; }
  318. (?i:bool2x2) { return token::HT_BOOL2x2; }
  319. (?i:bool3x2) { return token::HT_BOOL3x2; }
  320. (?i:bool4x2) { return token::HT_BOOL4x2; }
  321. (?i:bool3) { return token::HT_BOOL3; }
  322. (?i:bool1x3) { return token::HT_BOOL1x3; }
  323. (?i:bool2x3) { return token::HT_BOOL2x3; }
  324. (?i:bool3x3) { return token::HT_BOOL3x3; }
  325. (?i:bool4x3) { return token::HT_BOOL4x3; }
  326. (?i:bool4) { return token::HT_BOOL4; }
  327. (?i:bool1x4) { return token::HT_BOOL1x4; }
  328. (?i:bool2x4) { return token::HT_BOOL2x4; }
  329. (?i:bool3x4) { return token::HT_BOOL3x4; }
  330. (?i:bool4x4) { return token::HT_BOOL4x4; }
  331. (?i:float1) { return token::HT_FLOAT1; }
  332. (?i:float1x1) { return token::HT_FLOAT1x1; }
  333. (?i:float2x1) { return token::HT_FLOAT2x1; }
  334. (?i:float3x1) { return token::HT_FLOAT3x1; }
  335. (?i:float4x1) { return token::HT_FLOAT4x1; }
  336. (?i:float2) { return token::HT_FLOAT2; }
  337. (?i:float1x2) { return token::HT_FLOAT1x2; }
  338. (?i:float2x2) { return token::HT_FLOAT2x2; }
  339. (?i:float3x2) { return token::HT_FLOAT3x2; }
  340. (?i:float4x2) { return token::HT_FLOAT4x2; }
  341. (?i:float3) { return token::HT_FLOAT3; }
  342. (?i:float1x3) { return token::HT_FLOAT1x3; }
  343. (?i:float2x3) { return token::HT_FLOAT2x3; }
  344. (?i:float3x3) { return token::HT_FLOAT3x3; }
  345. (?i:float4x3) { return token::HT_FLOAT4x3; }
  346. (?i:float4) { return token::HT_FLOAT4; }
  347. (?i:float1x4) { return token::HT_FLOAT1x4; }
  348. (?i:float2x4) { return token::HT_FLOAT2x4; }
  349. (?i:float3x4) { return token::HT_FLOAT3x4; }
  350. (?i:float4x4) { return token::HT_FLOAT4x4; }
  351. (?i:double1) { return token::HT_DOUBLE1; }
  352. (?i:double1x1) { return token::HT_DOUBLE1x1; }
  353. (?i:double2x1) { return token::HT_DOUBLE2x1; }
  354. (?i:double3x1) { return token::HT_DOUBLE3x1; }
  355. (?i:double4x1) { return token::HT_DOUBLE4x1; }
  356. (?i:double2) { return token::HT_DOUBLE2; }
  357. (?i:double1x2) { return token::HT_DOUBLE1x2; }
  358. (?i:double2x2) { return token::HT_DOUBLE2x2; }
  359. (?i:double3x2) { return token::HT_DOUBLE3x2; }
  360. (?i:double4x2) { return token::HT_DOUBLE4x2; }
  361. (?i:double3) { return token::HT_DOUBLE3; }
  362. (?i:double1x3) { return token::HT_DOUBLE1x3; }
  363. (?i:double2x3) { return token::HT_DOUBLE2x3; }
  364. (?i:double3x3) { return token::HT_DOUBLE3x3; }
  365. (?i:double4x3) { return token::HT_DOUBLE4x3; }
  366. (?i:double4) { return token::HT_DOUBLE4; }
  367. (?i:double1x4) { return token::HT_DOUBLE1x4; }
  368. (?i:double2x4) { return token::HT_DOUBLE2x4; }
  369. (?i:double3x4) { return token::HT_DOUBLE3x4; }
  370. (?i:double4x4) { return token::HT_DOUBLE4x4; }
  371. (?i:dword1) { return token::HT_DWORD1; }
  372. (?i:dword1x1) { return token::HT_DWORD1x1; }
  373. (?i:dword2x1) { return token::HT_DWORD2x1; }
  374. (?i:dword3x1) { return token::HT_DWORD3x1; }
  375. (?i:dword4x1) { return token::HT_DWORD4x1; }
  376. (?i:dword2) { return token::HT_DWORD2; }
  377. (?i:dword1x2) { return token::HT_DWORD1x2; }
  378. (?i:dword2x2) { return token::HT_DWORD2x2; }
  379. (?i:dword3x2) { return token::HT_DWORD3x2; }
  380. (?i:dword4x2) { return token::HT_DWORD4x2; }
  381. (?i:dword3) { return token::HT_DWORD3; }
  382. (?i:dword1x3) { return token::HT_DWORD1x3; }
  383. (?i:dword2x3) { return token::HT_DWORD2x3; }
  384. (?i:dword3x3) { return token::HT_DWORD3x3; }
  385. (?i:dword4x3) { return token::HT_DWORD4x3; }
  386. (?i:dword4) { return token::HT_DWORD4; }
  387. (?i:dword1x4) { return token::HT_DWORD1x4; }
  388. (?i:dword2x4) { return token::HT_DWORD2x4; }
  389. (?i:dword3x4) { return token::HT_DWORD3x4; }
  390. (?i:dword4x4) { return token::HT_DWORD4x4; }
  391. (?i:int1) { return token::HT_INT1; }
  392. (?i:int1x1) { return token::HT_INT1x1; }
  393. (?i:int2x1) { return token::HT_INT2x1; }
  394. (?i:int3x1) { return token::HT_INT3x1; }
  395. (?i:int4x1) { return token::HT_INT4x1; }
  396. (?i:int2) { return token::HT_INT2; }
  397. (?i:int1x2) { return token::HT_INT1x2; }
  398. (?i:int2x2) { return token::HT_INT2x2; }
  399. (?i:int3x2) { return token::HT_INT3x2; }
  400. (?i:int4x2) { return token::HT_INT4x2; }
  401. (?i:int3) { return token::HT_INT3; }
  402. (?i:int1x3) { return token::HT_INT1x3; }
  403. (?i:int2x3) { return token::HT_INT2x3; }
  404. (?i:int3x3) { return token::HT_INT3x3; }
  405. (?i:int4x3) { return token::HT_INT4x3; }
  406. (?i:int4) { return token::HT_INT4; }
  407. (?i:int1x4) { return token::HT_INT1x4; }
  408. (?i:int2x4) { return token::HT_INT2x4; }
  409. (?i:int3x4) { return token::HT_INT3x4; }
  410. (?i:int4x4) { return token::HT_INT4x4; }
  411. (?i:uint1) { return token::HT_UINT1; }
  412. (?i:uint1x1) { return token::HT_UINT1x1; }
  413. (?i:uint2x1) { return token::HT_UINT2x1; }
  414. (?i:uint3x1) { return token::HT_UINT3x1; }
  415. (?i:uint4x1) { return token::HT_UINT4x1; }
  416. (?i:uint2) { return token::HT_UINT2; }
  417. (?i:uint1x2) { return token::HT_UINT1x2; }
  418. (?i:uint2x2) { return token::HT_UINT2x2; }
  419. (?i:uint3x2) { return token::HT_UINT3x2; }
  420. (?i:uint4x2) { return token::HT_UINT4x2; }
  421. (?i:uint3) { return token::HT_UINT3; }
  422. (?i:uint1x3) { return token::HT_UINT1x3; }
  423. (?i:uint2x3) { return token::HT_UINT2x3; }
  424. (?i:uint3x3) { return token::HT_UINT3x3; }
  425. (?i:uint4x3) { return token::HT_UINT4x3; }
  426. (?i:uint4) { return token::HT_UINT4; }
  427. (?i:uint1x4) { return token::HT_UINT1x4; }
  428. (?i:uint2x4) { return token::HT_UINT2x4; }
  429. (?i:uint3x4) { return token::HT_UINT3x4; }
  430. (?i:uint4x4) { return token::HT_UINT4x4; }
  431. /*
  432. * HLSL preprocessor directives
  433. * Gathered from http://msdn.microsoft.com/en-us/library/windows/desktop/bb943993%28v=vs.85%29.aspx
  434. */
  435. ^[ \t]*"#"[ \t]*"define".* {
  436. /* FIXME: this is very, very limited */
  437. char const *tmp = strstr(yytext, "define") + 6;
  438. while (*tmp == ' ' || *tmp == '\n')
  439. tmp++;
  440. if (*tmp == '\0')
  441. return token::PREPROCESSOR_DEFINE;
  442. char const *tmp2 = tmp;
  443. while (*tmp != ' ' && *tmp != '\n' && *tmp != '\0')
  444. tmp++;
  445. String key(tmp2, (int)(tmp - tmp2));
  446. while (*tmp == ' ' || *tmp == '\n')
  447. tmp++;
  448. String val(*tmp ? tmp : "1");
  449. Log::Debug("new macro: ‘%s’ = ‘%s’\n", key.C(), val.C());
  450. m_pp_defines[key] = val;
  451. }
  452. ^[ \t]*"#"[ \t]*"elif" {
  453. if (m_pp_stack.Count() <= 1)
  454. return token::PREPROCESSOR_ELIF;
  455. m_pp_stack.Last() = BlockIsForeverFalse;
  456. BEGIN(PREPROCESSOR_COMMENT);
  457. }
  458. ^[ \t]*"#"[ \t]*"else" {
  459. if (m_pp_stack.Count() <= 1)
  460. return token::PREPROCESSOR_ELSE;
  461. m_pp_stack.Last() = BlockIsForeverFalse;
  462. BEGIN(PREPROCESSOR_COMMENT);
  463. }
  464. ^[ \t]*"#"[ \t]*"endif" {
  465. if (m_pp_stack.Count() <= 1)
  466. return token::PREPROCESSOR_ENDIF;
  467. m_pp_stack.Pop();
  468. }
  469. ^[ \t]*"#"[ \t]*"error".* { return token::PREPROCESSOR_ERROR; }
  470. ^[ \t]*"#"[ \t]*"if".* {
  471. if (IsExpressionTrue(strstr(yytext, "if") + 2))
  472. {
  473. m_pp_stack.Push(BlockIsTrue);
  474. }
  475. else
  476. {
  477. m_pp_stack.Push(BlockIsFalse);
  478. BEGIN(PREPROCESSOR_COMMENT);
  479. }
  480. }
  481. ^[ \t]*"#"[ \t]*"ifdef" {
  482. /* FIXME: this is very, very limited */
  483. char const *tmp = strstr(yytext, "ifdef") + 5;
  484. while (*tmp == ' ' || *tmp == '\n')
  485. tmp++;
  486. if (*tmp == '\0')
  487. return token::PREPROCESSOR_IFDEF;
  488. char const *tmp2 = tmp;
  489. while (*tmp != ' ' && *tmp != '\n' && *tmp != '\0')
  490. tmp++;
  491. String key(tmp2, (int)(tmp - tmp2));
  492. if (m_pp_defines.has_key(key))
  493. {
  494. m_pp_stack.Push(BlockIsTrue);
  495. }
  496. else
  497. {
  498. m_pp_stack.Push(BlockIsFalse);
  499. BEGIN(PREPROCESSOR_COMMENT);
  500. }
  501. }
  502. ^[ \t]*"#"[ \t]*"ifndef" {
  503. /* FIXME: this is very, very limited */
  504. char const *tmp = strstr(yytext, "ifndef") + 6;
  505. while (*tmp == ' ' || *tmp == '\n')
  506. tmp++;
  507. if (*tmp == '\0')
  508. return token::PREPROCESSOR_IFDEF;
  509. char const *tmp2 = tmp;
  510. while (*tmp != ' ' && *tmp != '\n' && *tmp != '\0')
  511. tmp++;
  512. String key(tmp2, (int)(tmp - tmp2));
  513. if (m_pp_defines.has_key(key))
  514. {
  515. m_pp_stack.Push(BlockIsFalse);
  516. BEGIN(PREPROCESSOR_COMMENT);
  517. }
  518. else
  519. {
  520. m_pp_stack.Push(BlockIsTrue);
  521. }
  522. }
  523. ^[ \t]*"#"[ \t]*"include" { return token::PREPROCESSOR_INCLUDE; }
  524. ^[ \t]*"#"[ \t]*"line".* { /* ignore for now */ }
  525. ^[ \t]*"#"[ \t]*"pragma".* { /* ignore for now */ }
  526. ^[ \t]*"#"[ \t]*"undef" {
  527. /* FIXME: this is very, very limited */
  528. char const *tmp = strstr(yytext, "undef") + 5;
  529. while (*tmp == ' ' || *tmp == '\n')
  530. tmp++;
  531. if (*tmp == '\0')
  532. return token::PREPROCESSOR_UNDEF;
  533. char const *tmp2 = tmp;
  534. while (*tmp != ' ' && *tmp != '\n' && *tmp != '\0')
  535. tmp++;
  536. String key(tmp2, (int)(tmp - tmp2));
  537. Log::Debug("undef macro: ‘%s’\n", key.C());
  538. m_pp_defines.remove(key);
  539. }
  540. <PREPROCESSOR_COMMENT>^[ \t]*"#"[ \t]*(if|ifdef|ifndef)[ \t]+.*$ {
  541. m_pp_stack.Push(BlockIsForeverFalse);
  542. }
  543. <PREPROCESSOR_COMMENT>^[ \t]*"#"[ \t]*"else" {
  544. switch (m_pp_stack.Last())
  545. {
  546. case BlockIsFalse:
  547. m_pp_stack.Last() = BlockIsTrue;
  548. BEGIN(INITIAL);
  549. break;
  550. case BlockIsForeverFalse:
  551. break;
  552. case BlockIsTrue:
  553. m_pp_stack.Last() = BlockIsForeverFalse;
  554. break;
  555. }
  556. }
  557. <PREPROCESSOR_COMMENT>^[ \t]*"#"[ \t]*"elif" {
  558. switch (m_pp_stack.Last())
  559. {
  560. case BlockIsFalse:
  561. if (IsExpressionTrue(strstr(yytext, "elif") + 4))
  562. {
  563. m_pp_stack.Last() = BlockIsTrue;
  564. BEGIN(INITIAL);
  565. }
  566. break;
  567. case BlockIsForeverFalse:
  568. break;
  569. case BlockIsTrue:
  570. m_pp_stack.Last() = BlockIsForeverFalse;
  571. break;
  572. }
  573. }
  574. <PREPROCESSOR_COMMENT>^[ \t]*"#"[ \t]*"endif" {
  575. m_pp_stack.Pop();
  576. if (m_pp_stack.Last() == BlockIsTrue)
  577. BEGIN(INITIAL);
  578. }
  579. <PREPROCESSOR_COMMENT>\n { yylloc->lines(1); }
  580. <PREPROCESSOR_COMMENT>[^/#]* { }
  581. <PREPROCESSOR_COMMENT>"/*" { BEGIN(C_COMMENT); }
  582. <PREPROCESSOR_COMMENT>[/#] { }
  583. /*
  584. * GLSL preprocessor directives
  585. */
  586. ^[ \t]*"#"[ \t]*"version"[^\n]* { /* ignore for now */ }
  587. /*
  588. * HLSL reserved keywords
  589. * Gathered from http://msdn.microsoft.com/en-us/library/windows/desktop/bb509569%28v=vs.85%29.aspx
  590. */
  591. (?i:auto) { return token::HT_AUTO; }
  592. (?i:catch) { return token::HT_CATCH; }
  593. (?i:char) { return token::HT_CHAR; }
  594. (?i:class) { return token::HT_CLASS; }
  595. (?i:const_cast) { return token::HT_CONST_CAST; }
  596. (?i:delete) { return token::HT_DELETE; }
  597. (?i:dynamic_cast) { return token::HT_DYNAMIC_CAST; }
  598. (?i:enum) { return token::HT_ENUM; }
  599. (?i:explicit) { return token::HT_EXPLICIT; }
  600. (?i:friend) { return token::HT_FRIEND; }
  601. (?i:goto) { return token::HT_GOTO; }
  602. (?i:long) { return token::HT_LONG; }
  603. (?i:mutable) { return token::HT_MUTABLE; }
  604. (?i:new) { return token::HT_NEW; }
  605. (?i:operator) { return token::HT_OPERATOR; }
  606. (?i:private) { return token::HT_PRIVATE; }
  607. (?i:protected) { return token::HT_PROTECTED; }
  608. (?i:public) { return token::HT_PUBLIC; }
  609. (?i:reinterpret_cast) { return token::HT_REINTERPRET_CAST; }
  610. (?i:short) { return token::HT_SHORT; }
  611. (?i:signed) { return token::HT_SIGNED; }
  612. (?i:sizeof) { return token::HT_SIZEOF; }
  613. (?i:static_cast) { return token::HT_STATIC_CAST; }
  614. (?i:template) { return token::HT_TEMPLATE; }
  615. (?i:this) { return token::HT_THIS; }
  616. (?i:throw) { return token::HT_THROW; }
  617. (?i:try) { return token::HT_TRY; }
  618. (?i:typename) { return token::HT_TYPENAME; }
  619. (?i:union) { return token::HT_UNION; }
  620. (?i:unsigned) { return token::HT_UNSIGNED; }
  621. (?i:using) { return token::HT_USING; }
  622. (?i:virtual) { return token::HT_VIRTUAL; }
  623. /*
  624. * Various tokens
  625. */
  626. "++" { return token::T_INC; }
  627. "--" { return token::T_DEC; }
  628. "<=" { return token::T_LE; }
  629. ">=" { return token::T_GE; }
  630. "==" { return token::T_EQ; }
  631. "!=" { return token::T_NE; }
  632. "<<" { return token::T_LEFT; }
  633. ">>" { return token::T_RIGHT; }
  634. "&&" { return token::T_AND; }
  635. "||" { return token::T_OR; }
  636. "^^" { return token::T_XOR; }
  637. "*=" { return token::T_MULEQ; }
  638. "/=" { return token::T_DIVEQ; }
  639. "%=" { return token::T_MODEQ; }
  640. "+=" { return token::T_ADDEQ; }
  641. "-=" { return token::T_SUBEQ; }
  642. "<<=" { return token::T_LEFTEQ; }
  643. ">>=" { return token::T_RIGHTEQ; }
  644. "&=" { return token::T_ANDEQ; }
  645. "^=" { return token::T_XOREQ; }
  646. "|=" { return token::T_OREQ; }
  647. (0|[1-9][0-9]*)\.[0-9]+ {
  648. yylval->fval = std::atof(yytext);
  649. return token::FLOATCONSTANT;
  650. }
  651. (0|[1-9][0-9]*)[uU] {
  652. yylval->uval = std::atoi(yytext);
  653. return token::UINTCONSTANT;
  654. }
  655. (0|[1-9][0-9]*) {
  656. yylval->ival = std::atoi(yytext);
  657. return token::INTCONSTANT;
  658. }
  659. ([xyzw]{1,4}|[rgba]{1,4}|[stpq]{1,4}) {
  660. /* Copy token for now */
  661. yylval->sval = strdup(yytext);
  662. return token::FIELDSELECTION;
  663. }
  664. [a-zA-Z_][a-zA-Z0-9_]* {
  665. /* Copy token for now */
  666. yylval->sval = strdup(yytext);
  667. return token::IDENTIFIER;
  668. }
  669. [ \t\r]+ { /* ignore whitespace */ }
  670. [\n] { yylloc->lines(1); }
  671. . { return (token_type)*yytext; /* accept all characters */ }
  672. /*
  673. * Ignore C comments
  674. */
  675. "/*" { BEGIN(C_COMMENT); }
  676. <C_COMMENT>[^*\n]* { }
  677. <C_COMMENT>\n { yylloc->lines(1); }
  678. <C_COMMENT>. { }
  679. <C_COMMENT>"*/" {
  680. if (m_pp_stack.Last() == BlockIsTrue)
  681. BEGIN(INITIAL);
  682. else
  683. BEGIN(PREPROCESSOR_COMMENT);
  684. }
  685. /*
  686. * Ignore C++ comments
  687. */
  688. "//" { BEGIN(CPP_COMMENT); }
  689. <CPP_COMMENT>\n { yylloc->lines(1); BEGIN(INITIAL); }
  690. <CPP_COMMENT>.* { }
  691. %%
  692. lol::LolFxScanner::LolFxScanner(char const *command)
  693. : LolFxFlexLexer(0, 0),
  694. m_input(command)
  695. {
  696. }
  697. lol::LolFxScanner::~LolFxScanner()
  698. {
  699. }
  700. int lol::LolFxScanner::LexerInput(char* buf, int max_size)
  701. {
  702. buf[0] = m_input[0];
  703. if (buf[0])
  704. ++m_input;
  705. return buf[0] ? 1 : 0;
  706. }
  707. bool lol::LolFxScanner::IsExpressionTrue(char const *buf)
  708. {
  709. /* FIXME: this is really too limited */
  710. return std::atoi(buf) != 0;
  711. }
  712. #ifdef yylex
  713. #undef yylex
  714. #endif
  715. int LolFxFlexLexer::yylex()
  716. {
  717. std::cerr << "in LolFxFlexLexer::yylex() !" << std::endl;
  718. return 0;
  719. }
  720. int LolFxFlexLexer::yywrap()
  721. {
  722. return 1;
  723. }