Skip to content

Commit

Permalink
Rewrote HLSL grammar, mostly from scratch.
Browse files Browse the repository at this point in the history
I kept most of Jeff Lee's work for expressions, but the rest I rewrote,
 following the docs at MSDN for grammar details. I found this was both easier
 than weeding out the C-specific bits and wedging in the HLSL parts, and it
 should give me a richer understanding of how the guts of the parser work.

This isn't quite complete yet. Notably, it doesn't handle HLSL-style
 constructors for vector types: float4 x = float4(1,2,3,4);" for example.
  • Loading branch information
icculus committed Mar 7, 2009
1 parent 86a3040 commit 54d48ab
Show file tree
Hide file tree
Showing 2 changed files with 555 additions and 200 deletions.
187 changes: 164 additions & 23 deletions mojoshader_compiler.c
Expand Up @@ -60,8 +60,8 @@ static int ConvertToLemonToken(const Context *ctx)
case ((Token) ']'): return TOKEN_HLSL_RBRACKET;
case ((Token) '('): return TOKEN_HLSL_LPAREN;
case ((Token) ')'): return TOKEN_HLSL_RPAREN;
case ((Token) TOKEN_INT_LITERAL): return TOKEN_HLSL_CONSTANT;
case ((Token) TOKEN_FLOAT_LITERAL): return TOKEN_HLSL_CONSTANT;
case ((Token) TOKEN_INT_LITERAL): return TOKEN_HLSL_INT_CONSTANT;
case ((Token) TOKEN_FLOAT_LITERAL): return TOKEN_HLSL_FLOAT_CONSTANT;
case ((Token) TOKEN_STRING_LITERAL): return TOKEN_HLSL_STRING_LITERAL;
case ((Token) ':'): return TOKEN_HLSL_COLON;
case ((Token) ';'): return TOKEN_HLSL_SEMICOLON;
Expand All @@ -74,36 +74,177 @@ static int ConvertToLemonToken(const Context *ctx)
//if (tokencmp("")) return TOKEN_HLSL_TYPE_NAME
//if (tokencmp("...")) return TOKEN_HLSL_ELIPSIS
if (tokencmp("else")) return TOKEN_HLSL_ELSE;
if (tokencmp("sizeof")) return TOKEN_HLSL_SIZEOF;
if (tokencmp("inline")) return TOKEN_HLSL_INLINE;
if (tokencmp("void")) return TOKEN_HLSL_VOID;
if (tokencmp("in")) return TOKEN_HLSL_IN;
if (tokencmp("inout")) return TOKEN_HLSL_INOUT;
if (tokencmp("out")) return TOKEN_HLSL_OUT;
if (tokencmp("uniform")) return TOKEN_HLSL_UNIFORM;
if (tokencmp("linear")) return TOKEN_HLSL_LINEAR;
if (tokencmp("centroid")) return TOKEN_HLSL_CENTROID;
if (tokencmp("nointerpolation")) return TOKEN_HLSL_NOINTERPOLATION;
if (tokencmp("noperspective")) return TOKEN_HLSL_NOPERSPECTIVE;
if (tokencmp("sample")) return TOKEN_HLSL_SAMPLE;
if (tokencmp("struct")) return TOKEN_HLSL_STRUCT;
if (tokencmp("typedef")) return TOKEN_HLSL_TYPEDEF;
if (tokencmp("const")) return TOKEN_HLSL_CONST;
if (tokencmp("packoffset")) return TOKEN_HLSL_PACKOFFSET;
if (tokencmp("register")) return TOKEN_HLSL_REGISTER;
if (tokencmp("extern")) return TOKEN_HLSL_EXTERN;
if (tokencmp("shared")) return TOKEN_HLSL_SHARED;
if (tokencmp("static")) return TOKEN_HLSL_STATIC;
if (tokencmp("auto")) return TOKEN_HLSL_AUTO;
if (tokencmp("register")) return TOKEN_HLSL_REGISTER;
if (tokencmp("char")) return TOKEN_HLSL_CHAR;
if (tokencmp("short")) return TOKEN_HLSL_SHORT;
if (tokencmp("volatile")) return TOKEN_HLSL_VOLATILE;
if (tokencmp("row_major")) return TOKEN_HLSL_ROWMAJOR;
if (tokencmp("column_major")) return TOKEN_HLSL_COLUMNMAJOR;
if (tokencmp("bool")) return TOKEN_HLSL_BOOL;
if (tokencmp("int")) return TOKEN_HLSL_INT;
if (tokencmp("long")) return TOKEN_HLSL_LONG;
if (tokencmp("signed")) return TOKEN_HLSL_SIGNED;
if (tokencmp("unsigned")) return TOKEN_HLSL_UNSIGNED;
if (tokencmp("uint")) return TOKEN_HLSL_UINT;
if (tokencmp("half")) return TOKEN_HLSL_HALF;
if (tokencmp("float")) return TOKEN_HLSL_FLOAT;
if (tokencmp("double")) return TOKEN_HLSL_DOUBLE;
if (tokencmp("const")) return TOKEN_HLSL_CONST;
if (tokencmp("volatile")) return TOKEN_HLSL_VOLATILE;
if (tokencmp("void")) return TOKEN_HLSL_VOID;
if (tokencmp("struct")) return TOKEN_HLSL_STRUCT;
if (tokencmp("union")) return TOKEN_HLSL_UNION;
if (tokencmp("case")) return TOKEN_HLSL_CASE;
if (tokencmp("default")) return TOKEN_HLSL_DEFAULT;
if (tokencmp("string")) return TOKEN_HLSL_STRING;
if (tokencmp("snorm")) return TOKEN_HLSL_SNORM;
if (tokencmp("unorm")) return TOKEN_HLSL_UNORM;
if (tokencmp("buffer")) return TOKEN_HLSL_BUFFER;
if (tokencmp("vector")) return TOKEN_HLSL_VECTOR;
if (tokencmp("bool1")) return TOKEN_HLSL_BOOL1;
if (tokencmp("bool2")) return TOKEN_HLSL_BOOL2;
if (tokencmp("bool3")) return TOKEN_HLSL_BOOL3;
if (tokencmp("bool4")) return TOKEN_HLSL_BOOL4;
if (tokencmp("int1")) return TOKEN_HLSL_INT1;
if (tokencmp("int2")) return TOKEN_HLSL_INT2;
if (tokencmp("int3")) return TOKEN_HLSL_INT3;
if (tokencmp("int4")) return TOKEN_HLSL_INT4;
if (tokencmp("uint1")) return TOKEN_HLSL_UINT1;
if (tokencmp("uint2")) return TOKEN_HLSL_UINT2;
if (tokencmp("uint3")) return TOKEN_HLSL_UINT3;
if (tokencmp("uint4")) return TOKEN_HLSL_UINT4;
if (tokencmp("half1")) return TOKEN_HLSL_HALF1;
if (tokencmp("half2")) return TOKEN_HLSL_HALF2;
if (tokencmp("half3")) return TOKEN_HLSL_HALF3;
if (tokencmp("half4")) return TOKEN_HLSL_HALF4;
if (tokencmp("float1")) return TOKEN_HLSL_FLOAT1;
if (tokencmp("float2")) return TOKEN_HLSL_FLOAT2;
if (tokencmp("float3")) return TOKEN_HLSL_FLOAT3;
if (tokencmp("float4")) return TOKEN_HLSL_FLOAT4;
if (tokencmp("double1")) return TOKEN_HLSL_DOUBLE1;
if (tokencmp("double2")) return TOKEN_HLSL_DOUBLE2;
if (tokencmp("double3")) return TOKEN_HLSL_DOUBLE3;
if (tokencmp("double4")) return TOKEN_HLSL_DOUBLE4;
if (tokencmp("matrix")) return TOKEN_HLSL_MATRIX;
if (tokencmp("bool1x1")) return TOKEN_HLSL_BOOL1X1;
if (tokencmp("bool1x2")) return TOKEN_HLSL_BOOL1X2;
if (tokencmp("bool1x3")) return TOKEN_HLSL_BOOL1X3;
if (tokencmp("bool1x4")) return TOKEN_HLSL_BOOL1X4;
if (tokencmp("bool2x1")) return TOKEN_HLSL_BOOL2X1;
if (tokencmp("bool2x2")) return TOKEN_HLSL_BOOL2X2;
if (tokencmp("bool2x3")) return TOKEN_HLSL_BOOL2X3;
if (tokencmp("bool2x4")) return TOKEN_HLSL_BOOL2X4;
if (tokencmp("bool3x1")) return TOKEN_HLSL_BOOL3X1;
if (tokencmp("bool3x2")) return TOKEN_HLSL_BOOL3X2;
if (tokencmp("bool3x3")) return TOKEN_HLSL_BOOL3X3;
if (tokencmp("bool3x4")) return TOKEN_HLSL_BOOL3X4;
if (tokencmp("bool4x1")) return TOKEN_HLSL_BOOL4X1;
if (tokencmp("bool4x2")) return TOKEN_HLSL_BOOL4X2;
if (tokencmp("bool4x3")) return TOKEN_HLSL_BOOL4X3;
if (tokencmp("bool4x4")) return TOKEN_HLSL_BOOL4X4;
if (tokencmp("int1x1")) return TOKEN_HLSL_INT1X1;
if (tokencmp("int1x2")) return TOKEN_HLSL_INT1X2;
if (tokencmp("int1x3")) return TOKEN_HLSL_INT1X3;
if (tokencmp("int1x4")) return TOKEN_HLSL_INT1X4;
if (tokencmp("int2x1")) return TOKEN_HLSL_INT2X1;
if (tokencmp("int2x2")) return TOKEN_HLSL_INT2X2;
if (tokencmp("int2x3")) return TOKEN_HLSL_INT2X3;
if (tokencmp("int2x4")) return TOKEN_HLSL_INT2X4;
if (tokencmp("int3x1")) return TOKEN_HLSL_INT3X1;
if (tokencmp("int3x2")) return TOKEN_HLSL_INT3X2;
if (tokencmp("int3x3")) return TOKEN_HLSL_INT3X3;
if (tokencmp("int3x4")) return TOKEN_HLSL_INT3X4;
if (tokencmp("int4x1")) return TOKEN_HLSL_INT4X1;
if (tokencmp("int4x2")) return TOKEN_HLSL_INT4X2;
if (tokencmp("int4x3")) return TOKEN_HLSL_INT4X3;
if (tokencmp("int4x4")) return TOKEN_HLSL_INT4X4;
if (tokencmp("uint1x1")) return TOKEN_HLSL_UINT1X1;
if (tokencmp("uint1x2")) return TOKEN_HLSL_UINT1X2;
if (tokencmp("uint1x3")) return TOKEN_HLSL_UINT1X3;
if (tokencmp("uint1x4")) return TOKEN_HLSL_UINT1X4;
if (tokencmp("uint2x1")) return TOKEN_HLSL_UINT2X1;
if (tokencmp("uint2x2")) return TOKEN_HLSL_UINT2X2;
if (tokencmp("uint2x3")) return TOKEN_HLSL_UINT2X3;
if (tokencmp("uint2x4")) return TOKEN_HLSL_UINT2X4;
if (tokencmp("uint3x1")) return TOKEN_HLSL_UINT3X1;
if (tokencmp("uint3x2")) return TOKEN_HLSL_UINT3X2;
if (tokencmp("uint3x3")) return TOKEN_HLSL_UINT3X3;
if (tokencmp("uint3x4")) return TOKEN_HLSL_UINT3X4;
if (tokencmp("uint4x1")) return TOKEN_HLSL_UINT4X1;
if (tokencmp("uint4x2")) return TOKEN_HLSL_UINT4X2;
if (tokencmp("uint4x3")) return TOKEN_HLSL_UINT4X3;
if (tokencmp("uint4x4")) return TOKEN_HLSL_UINT4X4;
if (tokencmp("half1x1")) return TOKEN_HLSL_HALF1X1;
if (tokencmp("half1x2")) return TOKEN_HLSL_HALF1X2;
if (tokencmp("half1x3")) return TOKEN_HLSL_HALF1X3;
if (tokencmp("half1x4")) return TOKEN_HLSL_HALF1X4;
if (tokencmp("half2x1")) return TOKEN_HLSL_HALF2X1;
if (tokencmp("half2x2")) return TOKEN_HLSL_HALF2X2;
if (tokencmp("half2x3")) return TOKEN_HLSL_HALF2X3;
if (tokencmp("half2x4")) return TOKEN_HLSL_HALF2X4;
if (tokencmp("half3x1")) return TOKEN_HLSL_HALF3X1;
if (tokencmp("half3x2")) return TOKEN_HLSL_HALF3X2;
if (tokencmp("half3x3")) return TOKEN_HLSL_HALF3X3;
if (tokencmp("half3x4")) return TOKEN_HLSL_HALF3X4;
if (tokencmp("half4x1")) return TOKEN_HLSL_HALF4X1;
if (tokencmp("half4x2")) return TOKEN_HLSL_HALF4X2;
if (tokencmp("half4x3")) return TOKEN_HLSL_HALF4X3;
if (tokencmp("half4x4")) return TOKEN_HLSL_HALF4X4;
if (tokencmp("float1x1")) return TOKEN_HLSL_FLOAT1X1;
if (tokencmp("float1x2")) return TOKEN_HLSL_FLOAT1X2;
if (tokencmp("float1x3")) return TOKEN_HLSL_FLOAT1X3;
if (tokencmp("float1x4")) return TOKEN_HLSL_FLOAT1X4;
if (tokencmp("float2x1")) return TOKEN_HLSL_FLOAT2X1;
if (tokencmp("float2x2")) return TOKEN_HLSL_FLOAT2X2;
if (tokencmp("float2x3")) return TOKEN_HLSL_FLOAT2X3;
if (tokencmp("float2x4")) return TOKEN_HLSL_FLOAT2X4;
if (tokencmp("float3x1")) return TOKEN_HLSL_FLOAT3X1;
if (tokencmp("float3x2")) return TOKEN_HLSL_FLOAT3X2;
if (tokencmp("float3x3")) return TOKEN_HLSL_FLOAT3X3;
if (tokencmp("float3x4")) return TOKEN_HLSL_FLOAT3X4;
if (tokencmp("float4x1")) return TOKEN_HLSL_FLOAT4X1;
if (tokencmp("float4x2")) return TOKEN_HLSL_FLOAT4X2;
if (tokencmp("float4x3")) return TOKEN_HLSL_FLOAT4X3;
if (tokencmp("float4x4")) return TOKEN_HLSL_FLOAT4X4;
if (tokencmp("double1x1")) return TOKEN_HLSL_DOUBLE1X1;
if (tokencmp("double1x2")) return TOKEN_HLSL_DOUBLE1X2;
if (tokencmp("double1x3")) return TOKEN_HLSL_DOUBLE1X3;
if (tokencmp("double1x4")) return TOKEN_HLSL_DOUBLE1X4;
if (tokencmp("double2x1")) return TOKEN_HLSL_DOUBLE2X1;
if (tokencmp("double2x2")) return TOKEN_HLSL_DOUBLE2X2;
if (tokencmp("double2x3")) return TOKEN_HLSL_DOUBLE2X3;
if (tokencmp("double2x4")) return TOKEN_HLSL_DOUBLE2X4;
if (tokencmp("double3x1")) return TOKEN_HLSL_DOUBLE3X1;
if (tokencmp("double3x2")) return TOKEN_HLSL_DOUBLE3X2;
if (tokencmp("double3x3")) return TOKEN_HLSL_DOUBLE3X3;
if (tokencmp("double3x4")) return TOKEN_HLSL_DOUBLE3X4;
if (tokencmp("double4x1")) return TOKEN_HLSL_DOUBLE4X1;
if (tokencmp("double4x2")) return TOKEN_HLSL_DOUBLE4X2;
if (tokencmp("double4x3")) return TOKEN_HLSL_DOUBLE4X3;
if (tokencmp("double4x4")) return TOKEN_HLSL_DOUBLE4X4;
if (tokencmp("break")) return TOKEN_HLSL_BREAK;
if (tokencmp("continue")) return TOKEN_HLSL_CONTINUE;
if (tokencmp("discard")) return TOKEN_HLSL_DISCARD;
if (tokencmp("if")) return TOKEN_HLSL_IF;
if (tokencmp("switch")) return TOKEN_HLSL_SWITCH;
if (tokencmp("return")) return TOKEN_HLSL_RETURN;
if (tokencmp("while")) return TOKEN_HLSL_WHILE;
if (tokencmp("do")) return TOKEN_HLSL_DO;
if (tokencmp("for")) return TOKEN_HLSL_FOR;
if (tokencmp("continue")) return TOKEN_HLSL_CONTINUE;
if (tokencmp("break")) return TOKEN_HLSL_BREAK;
if (tokencmp("return")) return TOKEN_HLSL_RETURN;
if (tokencmp("unroll")) return TOKEN_HLSL_UNROLL;
if (tokencmp("loop")) return TOKEN_HLSL_LOOP;
if (tokencmp("do")) return TOKEN_HLSL_DO;
if (tokencmp("if")) return TOKEN_HLSL_IF;
if (tokencmp("branch")) return TOKEN_HLSL_BRANCH;
if (tokencmp("flatten")) return TOKEN_HLSL_FLATTEN;
if (tokencmp("switch")) return TOKEN_HLSL_SWITCH;
if (tokencmp("forcecase")) return TOKEN_HLSL_FORCECASE;
if (tokencmp("call")) return TOKEN_HLSL_CALL;
if (tokencmp("case")) return TOKEN_HLSL_CASE;
if (tokencmp("default")) return TOKEN_HLSL_DEFAULT;
#undef tokencmp
return TOKEN_HLSL_IDENTIFIER;

Expand Down

0 comments on commit 54d48ab

Please sign in to comment.