d3dcompiler: Carry around both shader major and minor version.
[wine] / dlls / d3dcompiler_43 / hlsl.l
1 /*
2  * HLSL parser
3  *
4  * Copyright 2008 Stefan Dösinger
5  * Copyright 2012 Matteo Bruni for CodeWeavers
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 %{
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wine/debug.h"
26
27 #define YY_NO_UNISTD_H
28 #include "d3dcompiler_private.h"
29 #include "hlsl.tab.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(hlsl_parser);
32 %}
33
34 %option noyywrap nounput noinput
35 %option prefix="hlsl_"
36
37 %x pp pp_line pp_pragma pp_ignore
38
39 RESERVED1               auto|case|catch|char|class|const_cast|default|delete|dynamic_cast|enum
40 RESERVED2               explicit|friend|goto|long|mutable|new|operator|private|protected|public
41 RESERVED3               reinterpret_cast|short|signed|sizeof|static_cast|template|this|throw|try
42 RESERVED4               typename|union|unsigned|using|virtual
43
44 WS                      [ \t]
45 NEWLINE                 (\n)|(\r\n)
46 DOUBLESLASHCOMMENT      "//"[^\n]*
47 STRING                  \"[^\"]*\"
48 IDENTIFIER              [A-Za-z_][A-Za-z0-9_]*
49
50 ANY                     (.)
51
52 %%
53 {RESERVED1}             {
54                             hlsl_message("Line %u: Reserved keyword \"%s\" used.\n", hlsl_ctx.line_no, yytext);
55                             set_parse_status(&hlsl_ctx.status, PARSE_ERR);
56                         }
57 {RESERVED2}             {
58                             hlsl_message("Line %u: Reserved keyword \"%s\" used.\n", hlsl_ctx.line_no, yytext);
59                             set_parse_status(&hlsl_ctx.status, PARSE_ERR);
60                         }
61 {RESERVED3}             {
62                             hlsl_message("Line %u: Reserved keyword \"%s\" used.\n", hlsl_ctx.line_no, yytext);
63                             set_parse_status(&hlsl_ctx.status, PARSE_ERR);
64                         }
65 {RESERVED4}             {
66                             hlsl_message("Line %u: Reserved keyword \"%s\" used.\n", hlsl_ctx.line_no, yytext);
67                             set_parse_status(&hlsl_ctx.status, PARSE_ERR);
68                         }
69
70 BlendState              {return KW_BLENDSTATE;          }
71 break                   {return KW_BREAK;               }
72 Buffer                  {return KW_BUFFER;              }
73 cbuffer                 {return KW_CBUFFER;             }
74 compile                 {return KW_COMPILE;             }
75 const                   {return KW_CONST;               }
76 continue                {return KW_CONTINUE;            }
77 DepthStencilState       {return KW_DEPTHSTENCILSTATE;   }
78 DepthStencilView        {return KW_DEPTHSTENCILVIEW;    }
79 discard                 {return KW_DISCARD;             }
80 do                      {return KW_DO;                  }
81 double                  {return KW_DOUBLE;              }
82 else                    {return KW_ELSE;                }
83 extern                  {return KW_EXTERN;              }
84 false                   {return KW_FALSE;               }
85 for                     {return KW_FOR;                 }
86 GeometryShader          {return KW_GEOMETRYSHADER;      }
87 groupshared             {return KW_GROUPSHARED;         }
88 if                      {return KW_IF;                  }
89 in                      {return KW_IN;                  }
90 inline                  {return KW_INLINE;              }
91 inout                   {return KW_INOUT;               }
92 matrix                  {return KW_MATRIX;              }
93 namespace               {return KW_NAMESPACE;           }
94 nointerpolation         {return KW_NOINTERPOLATION;     }
95 out                     {return KW_OUT;                 }
96 pass                    {return KW_PASS;                }
97 PixelShader             {return KW_PIXELSHADER;         }
98 precise                 {return KW_PRECISE;             }
99 RasterizerState         {return KW_RASTERIZERSTATE;     }
100 RenderTargetView        {return KW_RENDERTARGETVIEW;    }
101 return                  {return KW_RETURN;              }
102 register                {return KW_REGISTER;            }
103 sampler                 {return KW_SAMPLER;             }
104 sampler1D               {return KW_SAMPLER1D;           }
105 sampler2D               {return KW_SAMPLER2D;           }
106 sampler3D               {return KW_SAMPLER3D;           }
107 samplerCUBE             {return KW_SAMPLERCUBE;         }
108 sampler_state           {return KW_SAMPLER_STATE;       }
109 SamplerComparisonState  {return KW_SAMPLERCOMPARISONSTATE;}
110 shared                  {return KW_SHARED;              }
111 stateblock              {return KW_STATEBLOCK;          }
112 stateblock_state        {return KW_STATEBLOCK_STATE;    }
113 static                  {return KW_STATIC;              }
114 string                  {return KW_STRING;              }
115 struct                  {return KW_STRUCT;              }
116 switch                  {return KW_SWITCH;              }
117 tbuffer                 {return KW_TBUFFER;             }
118 technique               {return KW_TECHNIQUE;           }
119 technique10             {return KW_TECHNIQUE10;         }
120 texture                 {return KW_TEXTURE;             }
121 texture1D               {return KW_TEXTURE1D;           }
122 Texture1DArray          {return KW_TEXTURE1DARRAY;      }
123 texture2D               {return KW_TEXTURE2D;           }
124 Texture2DArray          {return KW_TEXTURE2DARRAY;      }
125 Texture2DMS             {return KW_TEXTURE2DMS;         }
126 Texture2DMSArray        {return KW_TEXTURE2DMSARRAY;    }
127 texture3D               {return KW_TEXTURE3D;           }
128 Texture3DArray          {return KW_TEXTURE3DARRAY;      }
129 textureCUBE             {return KW_TEXTURECUBE;         }
130 true                    {return KW_TRUE;                }
131 typedef                 {return KW_TYPEDEF;             }
132 uniform                 {return KW_UNIFORM;             }
133 vector                  {return KW_VECTOR;              }
134 VertexShader            {return KW_VERTEXSHADER;        }
135 void                    {return KW_VOID;                }
136 volatile                {return KW_VOLATILE;            }
137 while                   {return KW_WHILE;               }
138
139 \+\+                    {return OP_INC;                 }
140 \-\-                    {return OP_DEC;                 }
141 &&                      {return OP_AND;                 }
142 \|\|                    {return OP_OR;                  }
143 ==                      {return OP_EQ;                  }
144 \<\<                    {return OP_LEFTSHIFT;           }
145 \<\<=                   {return OP_LEFTSHIFTASSIGN;     }
146 \>\>                    {return OP_RIGHTSHIFT;          }
147 \>\>=                   {return OP_RIGHTSHIFTASSIGN;    }
148 \.\.\.                  {return OP_ELLIPSIS;            }
149 \<=                     {return OP_LE;                  }
150 \>=                     {return OP_GE;                  }
151 !=                      {return OP_NE;                  }
152 \+=                     {return OP_ADDASSIGN;           }
153 \-=                     {return OP_SUBASSIGN;           }
154 \*=                     {return OP_MULASSIGN;           }
155 \/=                     {return OP_DIVASSIGN;           }
156 %=                      {return OP_MODASSIGN;           }
157 &=                      {return OP_ANDASSIGN;           }
158 \|=                     {return OP_ORASSIGN;            }
159 ^=                      {return OP_XORASSIGN;           }
160 ##                      {return OP_UNKNOWN1;            }
161 #@                      {return OP_UNKNOWN2;            }
162 ::                      {return OP_UNKNOWN3;            }
163 \-\>                    {return OP_UNKNOWN4;            }
164
165 column_major            {return KW_COLUMN_MAJOR;        }
166 row_major               {return KW_ROW_MAJOR;           }
167
168 {IDENTIFIER}            {
169                             hlsl_lval.name = d3dcompiler_strdup(yytext);
170                             if (get_variable(hlsl_ctx.cur_scope, yytext)
171                                     || find_function(yytext))
172                                 return VAR_IDENTIFIER;
173                             else if (get_type(hlsl_ctx.cur_scope, yytext, TRUE))
174                                 return TYPE_IDENTIFIER;
175                             else
176                                 return NEW_IDENTIFIER;
177                         }
178
179 [+-]?[0-9]*\.[0-9]+([eE][+-]?[0-9]+)?[h|H|f|F]? {
180                             hlsl_lval.floatval = atof(yytext);
181                             return C_FLOAT;
182                         }
183 [+-]?[0-9]+\.([eE][+-]?[0-9]+)?[h|H|f|F]? {
184                             hlsl_lval.floatval = atof(yytext);
185                             return C_FLOAT;
186                         }
187 [+-]?[0-9]+([eE][+-]?[0-9]+)?[h|H|f|F] {
188                             hlsl_lval.floatval = atof(yytext);
189                             return C_FLOAT;
190                         }
191 0x[0-9a-fA-F]+          {
192                             sscanf(yytext, "0x%x", &hlsl_lval.intval);
193                             return C_INTEGER;
194                         }
195 0[0-7]+                 {
196                             sscanf(yytext, "0%o", &hlsl_lval.intval);
197                             return C_INTEGER;
198                         }
199 \-?[0-9]+               {
200                             hlsl_lval.intval = (atoi(yytext));
201                             return C_INTEGER;
202                         }
203
204 {DOUBLESLASHCOMMENT}    {}
205
206 {WS}+                   {}
207 {NEWLINE}               {
208                             hlsl_ctx.line_no++;
209                         }
210
211 ^#                      {
212                             BEGIN pp;
213                         }
214
215 <pp>pragma{WS}+         {
216                             TRACE("Got a #pragma.\n");
217                             BEGIN pp_pragma;
218                         }
219 <pp_pragma>pack_matrix{WS}*\({WS}*row_major{WS}*\) {
220                             TRACE("#pragma setting row_major mode.\n");
221                             hlsl_ctx.matrix_majority = HLSL_ROW_MAJOR;
222                             BEGIN pp_ignore;
223                         }
224 <pp_pragma>pack_matrix{WS}*\({WS}*column_major{WS}*\) {
225                             TRACE("#pragma setting column_major mode.\n");
226                             hlsl_ctx.matrix_majority = HLSL_COLUMN_MAJOR;
227                             BEGIN pp_ignore;
228                         }
229 <pp_pragma>{NEWLINE}    {
230                             FIXME("Unsupported preprocessor #pragma directive at line %u.\n", hlsl_ctx.line_no);
231                             BEGIN INITIAL;
232                         }
233 <pp_pragma>{ANY}        {}
234 <pp>[0-9]+              {
235                             TRACE("Preprocessor line info.\n");
236                             BEGIN pp_line;
237                             hlsl_lval.intval = (atoi(yytext));
238                             return PRE_LINE;
239                         }
240 <pp_line>{STRING}       {
241                             char *string = d3dcompiler_strdup(yytext + 1);
242
243                             BEGIN pp_ignore;
244                             string[strlen(string) - 1] = 0;
245                             hlsl_lval.name = string;
246                             return STRING;
247                         }
248 <pp_line>{WS}+          {}
249 <pp_line>{NEWLINE}      {
250                             FIXME("Malformed preprocessor line directive?\n");
251                             BEGIN INITIAL;
252                         }
253 <pp_ignore>{NEWLINE}    {
254                             BEGIN INITIAL;
255                         }
256 <pp_ignore>{ANY}        {}
257 <pp>{NEWLINE}           {
258                             FIXME("Unexpected preprocessor directive.\n");
259                             BEGIN INITIAL;
260                         }
261 <pp>{ANY}               {}
262
263 {ANY}                   {
264                             return yytext[0];
265                         }
266
267 %%
268
269 struct bwriter_shader *parse_hlsl(enum shader_type type, DWORD major, DWORD minor,
270         const char *entrypoint, char **messages);
271
272 struct bwriter_shader *parse_hlsl_shader(const char *text, enum shader_type type, DWORD major, DWORD minor,
273         const char *entrypoint, char **messages)
274 {
275     struct bwriter_shader *ret = NULL;
276     YY_BUFFER_STATE buffer;
277
278     buffer = hlsl__scan_string(text);
279     hlsl__switch_to_buffer(buffer);
280
281     ret = parse_hlsl(type, major, minor, entrypoint, messages);
282
283     hlsl__delete_buffer(buffer);
284     return ret;
285 }