4 * Copyright 2008 Stefan Dösinger
5 * Copyright 2012 Matteo Bruni for CodeWeavers
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.
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.
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
24 #include "wine/port.h"
25 #include "wine/debug.h"
27 #define YY_NO_UNISTD_H
28 #include "d3dcompiler_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(hlsl_parser);
34 %option noyywrap nounput noinput
35 %option prefix="hlsl_"
37 %x pp pp_line pp_pragma pp_ignore
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
46 DOUBLESLASHCOMMENT "//"[^\n]*
48 IDENTIFIER [A-Za-z_][A-Za-z0-9_]*
54 hlsl_message("Line %u: Reserved keyword \"%s\" used.\n", hlsl_ctx.line_no, yytext);
55 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
58 hlsl_message("Line %u: Reserved keyword \"%s\" used.\n", hlsl_ctx.line_no, yytext);
59 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
62 hlsl_message("Line %u: Reserved keyword \"%s\" used.\n", hlsl_ctx.line_no, yytext);
63 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
66 hlsl_message("Line %u: Reserved keyword \"%s\" used.\n", hlsl_ctx.line_no, yytext);
67 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
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; }
81 double {return KW_DOUBLE; }
82 else {return KW_ELSE; }
83 extern {return KW_EXTERN; }
84 false {return KW_FALSE; }
86 GeometryShader {return KW_GEOMETRYSHADER; }
87 groupshared {return KW_GROUPSHARED; }
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; }
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; }
139 \+\+ {return OP_INC; }
140 \-\- {return OP_DEC; }
142 \|\| {return OP_OR; }
144 \<\< {return OP_LEFTSHIFT; }
145 \<\<= {return OP_LEFTSHIFTASSIGN; }
146 \>\> {return OP_RIGHTSHIFT; }
147 \>\>= {return OP_RIGHTSHIFTASSIGN; }
148 \.\.\. {return OP_ELLIPSIS; }
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; }
165 column_major {return KW_COLUMN_MAJOR; }
166 row_major {return KW_ROW_MAJOR; }
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;
176 return NEW_IDENTIFIER;
179 [+-]?[0-9]*\.[0-9]+([eE][+-]?[0-9]+)?[h|H|f|F]? {
180 hlsl_lval.floatval = atof(yytext);
183 [+-]?[0-9]+\.([eE][+-]?[0-9]+)?[h|H|f|F]? {
184 hlsl_lval.floatval = atof(yytext);
187 [+-]?[0-9]+([eE][+-]?[0-9]+)?[h|H|f|F] {
188 hlsl_lval.floatval = atof(yytext);
192 sscanf(yytext, "0x%x", &hlsl_lval.intval);
196 sscanf(yytext, "0%o", &hlsl_lval.intval);
200 hlsl_lval.intval = (atoi(yytext));
204 {DOUBLESLASHCOMMENT} {}
216 TRACE("Got a #pragma.\n");
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;
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;
229 <pp_pragma>{NEWLINE} {
230 FIXME("Unsupported preprocessor #pragma directive at line %u.\n", hlsl_ctx.line_no);
235 TRACE("Preprocessor line info.\n");
237 hlsl_lval.intval = (atoi(yytext));
241 char *string = d3dcompiler_strdup(yytext + 1);
244 string[strlen(string) - 1] = 0;
245 hlsl_lval.name = string;
250 FIXME("Malformed preprocessor line directive?\n");
253 <pp_ignore>{NEWLINE} {
258 FIXME("Unexpected preprocessor directive.\n");
269 struct bwriter_shader *parse_hlsl(enum shader_type type, DWORD major, DWORD minor,
270 const char *entrypoint, char **messages);
272 struct bwriter_shader *parse_hlsl_shader(const char *text, enum shader_type type, DWORD major, DWORD minor,
273 const char *entrypoint, char **messages)
275 struct bwriter_shader *ret = NULL;
276 YY_BUFFER_STATE buffer;
278 buffer = hlsl__scan_string(text);
279 hlsl__switch_to_buffer(buffer);
281 ret = parse_hlsl(type, major, minor, entrypoint, messages);
283 hlsl__delete_buffer(buffer);