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);
33 #define YY_USER_ACTION \
35 hlsl_lloc.first_column = hlsl_ctx.column; \
36 hlsl_lloc.first_line = hlsl_ctx.line_no; \
37 hlsl_ctx.column += yyleng; \
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
54 DOUBLESLASHCOMMENT "//"[^\n]*
56 IDENTIFIER [A-Za-z_][A-Za-z0-9_]*
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 hlsl_message("Line %u: Reserved keyword \"%s\" used.\n", hlsl_ctx.line_no, yytext);
71 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
74 hlsl_message("Line %u: Reserved keyword \"%s\" used.\n", hlsl_ctx.line_no, yytext);
75 set_parse_status(&hlsl_ctx.status, PARSE_ERR);
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; }
89 double {return KW_DOUBLE; }
90 else {return KW_ELSE; }
91 extern {return KW_EXTERN; }
92 false {return KW_FALSE; }
94 GeometryShader {return KW_GEOMETRYSHADER; }
95 groupshared {return KW_GROUPSHARED; }
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; }
150 \|\| {return OP_OR; }
152 \<\< {return OP_LEFTSHIFT; }
153 \<\<= {return OP_LEFTSHIFTASSIGN; }
154 \>\> {return OP_RIGHTSHIFT; }
155 \>\>= {return OP_RIGHTSHIFTASSIGN; }
156 \.\.\. {return OP_ELLIPSIS; }
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; }
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;
184 return NEW_IDENTIFIER;
187 [+-]?[0-9]*\.[0-9]+([eE][+-]?[0-9]+)?[h|H|f|F]? {
188 hlsl_lval.floatval = atof(yytext);
191 [+-]?[0-9]+\.([eE][+-]?[0-9]+)?[h|H|f|F]? {
192 hlsl_lval.floatval = atof(yytext);
195 [+-]?[0-9]+([eE][+-]?[0-9]+)?[h|H|f|F] {
196 hlsl_lval.floatval = atof(yytext);
200 sscanf(yytext, "0x%x", &hlsl_lval.intval);
204 sscanf(yytext, "0%o", &hlsl_lval.intval);
208 hlsl_lval.intval = (atoi(yytext));
212 {DOUBLESLASHCOMMENT} {}
225 TRACE("Got a #pragma.\n");
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;
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;
238 <pp_pragma>{NEWLINE} {
239 FIXME("Unsupported preprocessor #pragma directive at line %u.\n", hlsl_ctx.line_no);
244 TRACE("Preprocessor line info.\n");
246 hlsl_lval.intval = (atoi(yytext));
250 char *string = d3dcompiler_strdup(yytext + 1);
253 string[strlen(string) - 1] = 0;
254 hlsl_lval.name = string;
259 FIXME("Malformed preprocessor line directive?\n");
262 <pp_ignore>{NEWLINE} {
267 FIXME("Unexpected preprocessor directive.\n");
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);