source: trunk/test/sandbox/lex/lolfx.y @ 1411

Last change on this file since 1411 was 1411, checked in by sam, 9 years ago

test: add more GLSL and HLSL keywords to our grammar.

File size: 8.6 KB
Line 
1%{
2#include <cstdio>
3#include <iostream>
4
5extern "C" int yylex();
6extern "C" int yyparse();
7extern "C" FILE *yyin;
8extern "C" int yylineno;
9
10void yyerror(const char *s);
11%}
12
13/* The classic Bison union trick */
14%union
15{
16    int ival;
17    float fval;
18    char *sval;
19}
20
21 /*
22  * GLSL keywords
23  * FIXME: unsorted
24  */
25
26%token GT_SUBROUTINE GT_PATCH GT_SAMPLE GT_FLAT GT_SMOOTH GT_LAYOUT
27%token GT_SAMPLER1DSHADOW GT_SAMPLER2DSHADOW GT_SAMPLERCUBESHADOW
28%token GT_SAMPLER1DARRAY GT_SAMPLER2DARRAY GT_SAMPLER1DARRAYSHADOW
29%token GT_SAMPLER2DARRAYSHADOW GT_ISAMPLER1D GT_ISAMPLER2D GT_ISAMPLER3D
30%token GT_ISAMPLERCUBE GT_ISAMPLER1DARRAY GT_ISAMPLER2DARRAY GT_USAMPLER1D
31%token GT_USAMPLER2D GT_USAMPLER3D GT_USAMPLERCUBE GT_USAMPLER1DARRAY
32%token GT_USAMPLER2DARRAY GT_SAMPLER2DRECT GT_SAMPLER2DRECTSHADOW
33%token GT_ISAMPLER2DRECT GT_USAMPLER2DRECT GT_SAMPLERBUFFER GT_ISAMPLERBUFFER
34%token GT_USAMPLERBUFFER GT_SAMPLERCUBEARRAY GT_SAMPLERCUBEARRAYSHADOW
35%token GT_ISAMPLERCUBEARRAY GT_USAMPLERCUBEARRAY GT_SAMPLER2DMS GT_ISAMPLER2DMS
36%token GT_USAMPLER2DMS GT_SAMPLER2DMSARRAY GT_ISAMPLER2DMSARRAY
37%token GT_USAMPLER2DMSARRAY GT_IDENTIFIER GT_TYPE_NAME GT_FLOATCONSTANT
38%token GT_INTCONSTANT GT_UINTCONSTANT GT_BOOLCONSTANT GT_FIELD_SELECTION
39%token GT_LEFT_OP GT_RIGHT_OP GT_INC_OP GT_DEC_OP GT_LE_OP GT_GE_OP GT_EQ_OP
40%token GT_NE_OP GT_AND_OP GT_OR_OP GT_XOR_OP GT_MUL_ASSIGN GT_DIV_ASSIGN
41%token GT_ADD_ASSIGN GT_MOD_ASSIGN GT_LEFT_ASSIGN GT_RIGHT_ASSIGN GT_AND_ASSIGN
42%token GT_XOR_ASSIGN GT_OR_ASSIGN GT_SUB_ASSIGN GT_LEFT_PAREN GT_RIGHT_PAREN
43%token GT_LEFT_BRACKET GT_RIGHT_BRACKET GT_LEFT_BRACE GT_RIGHT_BRACE GT_DOT
44%token GT_COMMA GT_COLON GT_EQUAL GT_SEMICOLON GT_BANG GT_DASH GT_TILDE GT_PLUS
45%token GT_STAR GT_SLASH GT_PERCENT GT_LEFT_ANGLE GT_RIGHT_ANGLE GT_VERTICAL_BAR
46%token GT_CARET GT_AMPERSAND GT_QUESTION GT_INVARIANT GT_HIGH_PRECISION
47%token GT_MEDIUM_PRECISION GT_LOW_PRECISION GT_PRECISION GT_BVEC2 GT_BVEC3
48%token GT_BVEC4 GT_IVEC2 GT_IVEC3 GT_IVEC4 GT_UVEC2 GT_UVEC3 GT_UVEC4 GT_VEC2
49%token GT_VEC3 GT_VEC4 GT_MAT2 GT_MAT2X2 GT_MAT2X3 GT_MAT2X4 GT_MAT3 GT_MAT3X2
50%token GT_MAT3X3 GT_MAT3X4 GT_MAT4 GT_MAT4X2 GT_MAT4X3 GT_MAT4X4 GT_DVEC2
51%token GT_DVEC3 GT_DVEC4 GT_DMAT2 GT_DMAT2X2 GT_DMAT2X3 GT_DMAT2X4 GT_DMAT3
52%token GT_DMAT3X2 GT_DMAT3X3 GT_DMAT3X4 GT_DMAT4 GT_DMAT4X2 GT_DMAT4X3
53%token GT_DMAT4X4
54
55 /*
56  * HLSL and GLSL keywords
57  */
58
59%token GHT_BOOL GHT_BREAK GHT_CASE GHT_CENTROID GHT_CONST GHT_CONTINUE
60%token GHT_DEFAULT GHT_DISCARD GHT_DO GHT_DOUBLE GHT_ELSE GHT_FLOAT GHT_FOR
61%token GHT_IF GHT_IN GHT_INOUT GHT_INT GHT_NOPERSPECTIVE GHT_OUT GHT_RETURN
62%token GHT_SAMPLER1D GHT_SAMPLER2D GHT_SAMPLER3D GHT_SAMPLERCUBE GHT_STRUCT
63%token GHT_SWITCH GHT_UINT GHT_UNIFORM GHT_VOID GHT_WHILE
64
65%token HGT_BOOL HGT_BREAK HGT_CASE HGT_CENTROID HGT_CONST HGT_CONTINUE
66%token HGT_DEFAULT HGT_DISCARD HGT_DO HGT_DOUBLE HGT_ELSE HGT_FLOAT HGT_FOR
67%token HGT_IF HGT_IN HGT_INOUT HGT_INT HGT_NOPERSPECTIVE HGT_OUT HGT_RETURN
68%token HGT_SAMPLER1D HGT_SAMPLER2D HGT_SAMPLER3D HGT_SAMPLERCUBE HGT_STRUCT
69%token HGT_SWITCH HGT_UINT HGT_UNIFORM HGT_VOID HGT_WHILE
70
71 /*
72  * HLSL keywords
73  */
74
75%token HT_APPENDSTRUCTUREDBUFFER HT_ASM HT_ASM_FRAGMENT HT_BLENDSTATE HT_BOOL
76%token HT_BREAK HT_BUFFER HT_BYTEADDRESSBUFFER HT_CBUFFER HT_CENTROID
77%token HT_COLUMN_MAJOR HT_COMPILE HT_COMPILE_FRAGMENT HT_COMPILESHADER HT_CONST
78%token HT_CONTINUE HT_COMPUTESHADER HT_CONSUMESTRUCTUREDBUFFER
79%token HT_DEPTHSTENCILSTATE HT_DEPTHSTENCILVIEW HT_DISCARD HT_DO HT_DOUBLE
80%token HT_DOMAINSHADER HT_DWORD HT_ELSE HT_EXTERN HT_FALSE HT_FLOAT HT_FOR
81%token HT_FXGROUP HT_GEOMETRYSHADER HT_GROUPSHARED HT_HALF HT_HULLSHADER HT_IF
82%token HT_IN HT_INLINE HT_INOUT HT_INPUTPATCH HT_INT HT_INTERFACE HT_LINE
83%token HT_LINEADJ HT_LINEAR HT_LINESTREAM HT_MATRIX HT_NAMESPACE
84%token HT_NOINTERPOLATION HT_NOPERSPECTIVE HT_NULL HT_OUT HT_OUTPUTPATCH
85%token HT_PACKOFFSET HT_PASS HT_PIXELFRAGMENT HT_PIXELSHADER HT_POINT
86%token HT_POINTSTREAM HT_PRECISE HT_RASTERIZERSTATE HT_RENDERTARGETVIEW
87%token HT_RETURN HT_REGISTER HT_ROW_MAJOR HT_RWBUFFER HT_RWBYTEADDRESSBUFFER
88%token HT_RWSTRUCTUREDBUFFER HT_RWTEXTURE1D HT_RWTEXTURE1DARRAY HT_RWTEXTURE2D
89%token HT_RWTEXTURE2DARRAY HT_RWTEXTURE3D HT_SAMPLER HT_SAMPLER1D HT_SAMPLER2D
90%token HT_SAMPLER3D HT_SAMPLERCUBE HT_SAMPLER_STATE HT_SAMPLERSTATE
91%token HT_SAMPLERCOMPARISONSTATE HT_SHARED HT_SNORM HT_STATEBLOCK
92%token HT_STATEBLOCK_STATE HT_STATIC HT_STRING HT_STRUCT HT_SWITCH
93%token HT_STRUCTUREDBUFFER HT_TBUFFER HT_TECHNIQUE HT_TECHNIQUE10
94%token HT_TECHNIQUE11XZ HT_TEXTURE HT_TEXTURE1D HT_TEXTURE1DARRAY HT_TEXTURE2D
95%token HT_TEXTURE2DARRAY HT_TEXTURE2DMS HT_TEXTURE2DMSARRAY HT_TEXTURE3D
96%token HT_TEXTURECUBE HT_TEXTURECUBEARRAY HT_TRUE HT_TYPEDEF HT_TRIANGLE
97%token HT_TRIANGLEADJ HT_TRIANGLESTREAM HT_UNIFORM HT_UNORM HT_UINT HT_VECTOR
98%token HT_VERTEXFRAGMENT HT_VERTEXSHADER HT_VOID HT_VOLATILE HT_WHILE
99
100 /*
101  * HLSL types
102  */
103
104%token HT_BOOL1 HT_BOOL1x1 HT_BOOL2x1 HT_BOOL3x1 HT_BOOL4x1
105%token HT_BOOL2 HT_BOOL1x2 HT_BOOL2x2 HT_BOOL3x2 HT_BOOL4x2
106%token HT_BOOL3 HT_BOOL1x3 HT_BOOL2x3 HT_BOOL3x3 HT_BOOL4x3
107%token HT_BOOL4 HT_BOOL1x4 HT_BOOL2x4 HT_BOOL3x4 HT_BOOL4x4
108
109%token HT_FLOAT1 HT_FLOAT1x1 HT_FLOAT2x1 HT_FLOAT3x1 HT_FLOAT4x1
110%token HT_FLOAT2 HT_FLOAT1x2 HT_FLOAT2x2 HT_FLOAT3x2 HT_FLOAT4x2
111%token HT_FLOAT3 HT_FLOAT1x3 HT_FLOAT2x3 HT_FLOAT3x3 HT_FLOAT4x3
112%token HT_FLOAT4 HT_FLOAT1x4 HT_FLOAT2x4 HT_FLOAT3x4 HT_FLOAT4x4
113
114%token HT_DOUBLE1 HT_DOUBLE1x1 HT_DOUBLE2x1 HT_DOUBLE3x1 HT_DOUBLE4x1
115%token HT_DOUBLE2 HT_DOUBLE1x2 HT_DOUBLE2x2 HT_DOUBLE3x2 HT_DOUBLE4x2
116%token HT_DOUBLE3 HT_DOUBLE1x3 HT_DOUBLE2x3 HT_DOUBLE3x3 HT_DOUBLE4x3
117%token HT_DOUBLE4 HT_DOUBLE1x4 HT_DOUBLE2x4 HT_DOUBLE3x4 HT_DOUBLE4x4
118
119%token HT_DWORD1 HT_DWORD1x1 HT_DWORD2x1 HT_DWORD3x1 HT_DWORD4x1
120%token HT_DWORD2 HT_DWORD1x2 HT_DWORD2x2 HT_DWORD3x2 HT_DWORD4x2
121%token HT_DWORD3 HT_DWORD1x3 HT_DWORD2x3 HT_DWORD3x3 HT_DWORD4x3
122%token HT_DWORD4 HT_DWORD1x4 HT_DWORD2x4 HT_DWORD3x4 HT_DWORD4x4
123
124%token HT_INT1 HT_INT1x1 HT_INT2x1 HT_INT3x1 HT_INT4x1
125%token HT_INT2 HT_INT1x2 HT_INT2x2 HT_INT3x2 HT_INT4x2
126%token HT_INT3 HT_INT1x3 HT_INT2x3 HT_INT3x3 HT_INT4x3
127%token HT_INT4 HT_INT1x4 HT_INT2x4 HT_INT3x4 HT_INT4x4
128
129%token HT_UINT1 HT_UINT1x1 HT_UINT2x1 HT_UINT3x1 HT_UINT4x1
130%token HT_UINT2 HT_UINT1x2 HT_UINT2x2 HT_UINT3x2 HT_UINT4x2
131%token HT_UINT3 HT_UINT1x3 HT_UINT2x3 HT_UINT3x3 HT_UINT4x3
132%token HT_UINT4 HT_UINT1x4 HT_UINT2x4 HT_UINT3x4 HT_UINT4x4
133
134 /*
135  * HLSL preprocessor directives
136  */
137
138%token PREPROCESSOR_DEFINE PREPROCESSOR_ELIF PREPROCESSOR_ELSE
139%token PREPROCESSOR_ENDIF PREPROCESSOR_ERROR PREPROCESSOR_IF
140%token PREPROCESSOR_IFDEF PREPROCESSOR_IFNDEF PREPROCESSOR_INCLUDE
141%token PREPROCESSOR_LINE PREPROCESSOR_PRAGMA PREPROCESSOR_UNDEF
142
143 /*
144  * HLSL reserved keywords
145  */
146
147%token HT_AUTO HT_CASE HT_CATCH HT_CHAR HT_CLASS HT_CONST_CAST HT_DEFAULT
148%token HT_DELETE HT_DYNAMIC_CAST HT_ENUM HT_EXPLICIT HT_FRIEND HT_GOTO HT_LONG
149%token HT_MUTABLE HT_NEW HT_OPERATOR HT_PRIVATE HT_PROTECTED HT_PUBLIC
150%token HT_REINTERPRET_CAST HT_SHORT HT_SIGNED HT_SIZEOF HT_STATIC_CAST
151%token HT_TEMPLATE HT_THIS HT_THROW HT_TRY HT_TYPENAME HT_UNION HT_UNSIGNED
152%token HT_USING HT_VIRTUAL
153
154 /*
155  * Temporary shit
156  */
157
158%token <ival> INT
159%token <fval> FLOAT
160%token <sval> STRING NAME
161
162%%
163
164fx:
165    section_list
166    ;
167
168section_list:
169    section
170    | section_list section
171    ;
172
173section:
174    technique
175    | shader
176    ;
177
178 /*
179  * Grammar for techniques
180  */
181
182technique:
183    HT_TECHNIQUE NAME '{' pass_list '}' { std::cout << "New tech" << std::endl; }
184    ;
185
186 /*
187  * Grammar for passes
188  */
189
190pass_list:
191    pass
192    | pass_list pass
193    ;
194
195pass:
196    HT_PASS NAME '{' pass_stmt_list '}' { std::cout << "New pass" << std::endl; }
197    ;
198
199pass_stmt_list:
200    pass_stmt
201    | pass_stmt_list pass_stmt
202    ;
203
204pass_stmt:
205    ';'
206    | NAME '=' NAME ';'
207    | NAME '=' INT ';'
208    | NAME '[' INT ']' '=' NAME ';'
209    | NAME '[' INT ']' '=' INT ';'
210    ;
211
212 /*
213  * Grammar for shaders
214  */
215
216shader:
217    shader_region shader_code
218    | shader_region
219    ;
220
221shader_region:
222    '#' NAME shader_name { std::cout << "new shader " << $2 << std::endl; }
223    ;
224
225shader_name:
226    NAME
227    | shader_name '.' NAME
228    ;
229
230shader_code:
231    INT shader_code      { std::cout << "int: " << $1 << std::endl; }
232    | FLOAT shader_code  { std::cout << "float: " << $1 << std::endl; }
233    | STRING shader_code { std::cout << "string: " << $1 << std::endl; }
234    | INT            { std::cout << "int: " << $1 << std::endl; }
235    | FLOAT          { std::cout << "float: " << $1 << std::endl; }
236    | STRING         { std::cout << "string: " << $1 << std::endl; }
237    ;
238
239%%
240
241main()
242{
243    yyin = fopen("test.lolfx", "r");
244    do
245    {
246        yyparse();
247    }
248    while (!feof(yyin));
249
250    fclose(yyin);
251}
252
253void yyerror(const char *s)
254{
255    std::cout << "Parse error line " << yylineno << ": " << s << std::endl;
256    exit(-1);
257}
258
Note: See TracBrowser for help on using the repository browser.