tools: Only allow the absolute difference in bracket pairs to be less than 128, since...
[wine/multimedia.git] / dlls / d3dcompiler_43 / hlsl.l
blobf40e3c9a04981871120b107ab46eeeacf639ebec
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  */
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wine/debug.h"
27 #define YY_NO_UNISTD_H
28 #include "d3dcompiler_private.h"
29 #include "hlsl.tab.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(hlsl_parser);
33 #define YY_USER_ACTION                               \
34    do {                                              \
35       hlsl_lloc.first_column = hlsl_ctx.column;      \
36       hlsl_lloc.first_line = hlsl_ctx.line_no;       \
37       hlsl_ctx.column += yyleng;                     \
38    } while(0);
42 %option noyywrap nounput noinput never-interactive
43 %option prefix="hlsl_"
45 %x pp pp_line pp_pragma pp_ignore
47 RESERVED1               auto|case|catch|char|class|const_cast|default|delete|dynamic_cast|enum
48 RESERVED2               explicit|friend|goto|long|mutable|new|operator|private|protected|public
49 RESERVED3               reinterpret_cast|short|signed|sizeof|static_cast|template|this|throw|try
50 RESERVED4               typename|union|unsigned|using|virtual
52 WS                      [ \t]
53 NEWLINE                 (\n)|(\r\n)
54 DOUBLESLASHCOMMENT      "//"[^\n]*
55 STRING                  \"[^\"]*\"
56 IDENTIFIER              [A-Za-z_][A-Za-z0-9_]*
58 ANY                     (.)
61 {RESERVED1}             {
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 {RESERVED2}             {
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 {RESERVED3}             {
70                             hlsl_message("Line %u: Reserved keyword \"%s\" used.\n", hlsl_ctx.line_no, yytext);
71                             set_parse_status(&hlsl_ctx.status, PARSE_ERR);
72                         }
73 {RESERVED4}             {
74                             hlsl_message("Line %u: Reserved keyword \"%s\" used.\n", hlsl_ctx.line_no, yytext);
75                             set_parse_status(&hlsl_ctx.status, PARSE_ERR);
76                         }
78 BlendState              {return KW_BLENDSTATE;          }
79 break                   {return KW_BREAK;               }
80 Buffer                  {return KW_BUFFER;              }
81 cbuffer                 {return KW_CBUFFER;             }
82 compile                 {return KW_COMPILE;             }
83 const                   {return KW_CONST;               }
84 continue                {return KW_CONTINUE;            }
85 DepthStencilState       {return KW_DEPTHSTENCILSTATE;   }
86 DepthStencilView        {return KW_DEPTHSTENCILVIEW;    }
87 discard                 {return KW_DISCARD;             }
88 do                      {return KW_DO;                  }
89 double                  {return KW_DOUBLE;              }
90 else                    {return KW_ELSE;                }
91 extern                  {return KW_EXTERN;              }
92 false                   {return KW_FALSE;               }
93 for                     {return KW_FOR;                 }
94 GeometryShader          {return KW_GEOMETRYSHADER;      }
95 groupshared             {return KW_GROUPSHARED;         }
96 if                      {return KW_IF;                  }
97 in                      {return KW_IN;                  }
98 inline                  {return KW_INLINE;              }
99 inout                   {return KW_INOUT;               }
100 matrix                  {return KW_MATRIX;              }
101 namespace               {return KW_NAMESPACE;           }
102 nointerpolation         {return KW_NOINTERPOLATION;     }
103 out                     {return KW_OUT;                 }
104 pass                    {return KW_PASS;                }
105 PixelShader             {return KW_PIXELSHADER;         }
106 precise                 {return KW_PRECISE;             }
107 RasterizerState         {return KW_RASTERIZERSTATE;     }
108 RenderTargetView        {return KW_RENDERTARGETVIEW;    }
109 return                  {return KW_RETURN;              }
110 register                {return KW_REGISTER;            }
111 sampler                 {return KW_SAMPLER;             }
112 sampler1D               {return KW_SAMPLER1D;           }
113 sampler2D               {return KW_SAMPLER2D;           }
114 sampler3D               {return KW_SAMPLER3D;           }
115 samplerCUBE             {return KW_SAMPLERCUBE;         }
116 sampler_state           {return KW_SAMPLER_STATE;       }
117 SamplerComparisonState  {return KW_SAMPLERCOMPARISONSTATE;}
118 shared                  {return KW_SHARED;              }
119 stateblock              {return KW_STATEBLOCK;          }
120 stateblock_state        {return KW_STATEBLOCK_STATE;    }
121 static                  {return KW_STATIC;              }
122 string                  {return KW_STRING;              }
123 struct                  {return KW_STRUCT;              }
124 switch                  {return KW_SWITCH;              }
125 tbuffer                 {return KW_TBUFFER;             }
126 technique               {return KW_TECHNIQUE;           }
127 technique10             {return KW_TECHNIQUE10;         }
128 texture                 {return KW_TEXTURE;             }
129 texture1D               {return KW_TEXTURE1D;           }
130 Texture1DArray          {return KW_TEXTURE1DARRAY;      }
131 texture2D               {return KW_TEXTURE2D;           }
132 Texture2DArray          {return KW_TEXTURE2DARRAY;      }
133 Texture2DMS             {return KW_TEXTURE2DMS;         }
134 Texture2DMSArray        {return KW_TEXTURE2DMSARRAY;    }
135 texture3D               {return KW_TEXTURE3D;           }
136 Texture3DArray          {return KW_TEXTURE3DARRAY;      }
137 textureCUBE             {return KW_TEXTURECUBE;         }
138 true                    {return KW_TRUE;                }
139 typedef                 {return KW_TYPEDEF;             }
140 uniform                 {return KW_UNIFORM;             }
141 vector                  {return KW_VECTOR;              }
142 VertexShader            {return KW_VERTEXSHADER;        }
143 void                    {return KW_VOID;                }
144 volatile                {return KW_VOLATILE;            }
145 while                   {return KW_WHILE;               }
147 \+\+                    {return OP_INC;                 }
148 \-\-                    {return OP_DEC;                 }
149 &&                      {return OP_AND;                 }
150 \|\|                    {return OP_OR;                  }
151 ==                      {return OP_EQ;                  }
152 \<\<                    {return OP_LEFTSHIFT;           }
153 \<\<=                   {return OP_LEFTSHIFTASSIGN;     }
154 \>\>                    {return OP_RIGHTSHIFT;          }
155 \>\>=                   {return OP_RIGHTSHIFTASSIGN;    }
156 \.\.\.                  {return OP_ELLIPSIS;            }
157 \<=                     {return OP_LE;                  }
158 \>=                     {return OP_GE;                  }
159 !=                      {return OP_NE;                  }
160 \+=                     {return OP_ADDASSIGN;           }
161 \-=                     {return OP_SUBASSIGN;           }
162 \*=                     {return OP_MULASSIGN;           }
163 \/=                     {return OP_DIVASSIGN;           }
164 %=                      {return OP_MODASSIGN;           }
165 &=                      {return OP_ANDASSIGN;           }
166 \|=                     {return OP_ORASSIGN;            }
167 ^=                      {return OP_XORASSIGN;           }
168 ##                      {return OP_UNKNOWN1;            }
169 #@                      {return OP_UNKNOWN2;            }
170 ::                      {return OP_UNKNOWN3;            }
171 \-\>                    {return OP_UNKNOWN4;            }
173 column_major            {return KW_COLUMN_MAJOR;        }
174 row_major               {return KW_ROW_MAJOR;           }
176 {IDENTIFIER}            {
177                             hlsl_lval.name = d3dcompiler_strdup(yytext);
178                             if (get_variable(hlsl_ctx.cur_scope, yytext)
179                                     || find_function(yytext))
180                                 return VAR_IDENTIFIER;
181                             else if (get_type(hlsl_ctx.cur_scope, yytext, TRUE))
182                                 return TYPE_IDENTIFIER;
183                             else
184                                 return NEW_IDENTIFIER;
185                         }
187 [0-9]*\.[0-9]+([eE][+-]?[0-9]+)?[h|H|f|F]? {
188                             hlsl_lval.floatval = atof(yytext);
189                             return C_FLOAT;
190                         }
191 [0-9]+\.([eE][+-]?[0-9]+)?[h|H|f|F]? {
192                             hlsl_lval.floatval = atof(yytext);
193                             return C_FLOAT;
194                         }
195 [0-9]+([eE][+-]?[0-9]+)?[h|H|f|F] {
196                             hlsl_lval.floatval = atof(yytext);
197                             return C_FLOAT;
198                         }
199 0x[0-9a-fA-F]+          {
200                             sscanf(yytext, "0x%x", &hlsl_lval.intval);
201                             return C_INTEGER;
202                         }
203 0[0-7]+                 {
204                             sscanf(yytext, "0%o", &hlsl_lval.intval);
205                             return C_INTEGER;
206                         }
207 [0-9]+                  {
208                             hlsl_lval.intval = (atoi(yytext));
209                             return C_INTEGER;
210                         }
212 {DOUBLESLASHCOMMENT}    {}
214 {WS}+                   {}
215 {NEWLINE}               {
216                             hlsl_ctx.line_no++;
217                             hlsl_ctx.column = 1;
218                         }
220 ^#                      {
221                             BEGIN pp;
222                         }
224 <pp>pragma{WS}+         {
225                             TRACE("Got a #pragma.\n");
226                             BEGIN pp_pragma;
227                         }
228 <pp_pragma>pack_matrix{WS}*\({WS}*row_major{WS}*\) {
229                             TRACE("#pragma setting row_major mode.\n");
230                             hlsl_ctx.matrix_majority = HLSL_ROW_MAJOR;
231                             BEGIN pp_ignore;
232                         }
233 <pp_pragma>pack_matrix{WS}*\({WS}*column_major{WS}*\) {
234                             TRACE("#pragma setting column_major mode.\n");
235                             hlsl_ctx.matrix_majority = HLSL_COLUMN_MAJOR;
236                             BEGIN pp_ignore;
237                         }
238 <pp_pragma>{NEWLINE}    {
239                             FIXME("Unsupported preprocessor #pragma directive at line %u.\n", hlsl_ctx.line_no);
240                             BEGIN INITIAL;
241                         }
242 <pp_pragma>{ANY}        {}
243 <pp>[0-9]+              {
244                             TRACE("Preprocessor line info.\n");
245                             BEGIN pp_line;
246                             hlsl_lval.intval = (atoi(yytext));
247                             return PRE_LINE;
248                         }
249 <pp_line>{STRING}       {
250                             char *string = d3dcompiler_strdup(yytext + 1);
252                             BEGIN pp_ignore;
253                             string[strlen(string) - 1] = 0;
254                             hlsl_lval.name = string;
255                             return STRING;
256                         }
257 <pp_line>{WS}+          {}
258 <pp_line>{NEWLINE}      {
259                             FIXME("Malformed preprocessor line directive?\n");
260                             BEGIN INITIAL;
261                         }
262 <pp_ignore>{NEWLINE}    {
263                             BEGIN INITIAL;
264                         }
265 <pp_ignore>{ANY}        {}
266 <pp>{NEWLINE}           {
267                             FIXME("Unexpected preprocessor directive.\n");
268                             BEGIN INITIAL;
269                         }
270 <pp>{ANY}               {}
272 {ANY}                   {
273                             return yytext[0];
274                         }
278 struct bwriter_shader *parse_hlsl(enum shader_type type, DWORD major, DWORD minor,
279         const char *entrypoint, char **messages);
281 struct bwriter_shader *parse_hlsl_shader(const char *text, enum shader_type type, DWORD major, DWORD minor,
282         const char *entrypoint, char **messages)
284     struct bwriter_shader *ret = NULL;
285     YY_BUFFER_STATE buffer;
287     buffer = hlsl__scan_string(text);
288     hlsl__switch_to_buffer(buffer);
290     ret = parse_hlsl(type, major, minor, entrypoint, messages);
292     hlsl__delete_buffer(buffer);
293     return ret;