forked from Nomoresleep/hlslparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HLSLTokenizer.h
222 lines (192 loc) · 5.72 KB
/
HLSLTokenizer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#ifndef HLSL_TOKENIZER_H
#define HLSL_TOKENIZER_H
namespace M4
{
/** In addition to the values in this enum, all of the ASCII characters are
valid tokens. */
enum HLSLToken
{
// Built-in types.
HLSLToken_Float = 256,
HLSLToken_Float2,
HLSLToken_Float3,
HLSLToken_Float4,
HLSLToken_Float2x2,
HLSLToken_Float3x3,
HLSLToken_Float4x4,
HLSLToken_Float4x3,
HLSLToken_Float4x2,
HLSLToken_Half,
HLSLToken_Half2,
HLSLToken_Half3,
HLSLToken_Half4,
HLSLToken_Half2x2,
HLSLToken_Half3x3,
HLSLToken_Half4x4,
HLSLToken_Half4x3,
HLSLToken_Half4x2,
HLSLToken_Bool,
HLSLToken_Bool2,
HLSLToken_Bool3,
HLSLToken_Bool4,
HLSLToken_Int,
HLSLToken_Int2,
HLSLToken_Int3,
HLSLToken_Int4,
HLSLToken_Uint,
HLSLToken_Uint2,
HLSLToken_Uint3,
HLSLToken_Uint4,
HLSLToken_Texture1D,
HLSLToken_Texture2D,
HLSLToken_Texture3D,
HLSLToken_TextureCube,
HLSLToken_TextureCubeArray,
HLSLToken_Texture2DMS,
HLSLToken_Texture1DArray,
HLSLToken_Texture2DArray,
HLSLToken_Texture2DMSArray,
HLSLToken_RWTexture1D,
HLSLToken_RWTexture2D,
HLSLToken_RWTexture3D,
HLSLToken_SamplerState,
HLSLToken_Sampler,
// Reserved words.
HLSLToken_If,
HLSLToken_Else,
HLSLToken_For,
HLSLToken_While,
HLSLToken_Break,
HLSLToken_True,
HLSLToken_False,
HLSLToken_Void,
HLSLToken_Struct,
HLSLToken_ConstantBuffer,
HLSLToken_TextureBuffer,
HLSLToken_Register,
HLSLToken_Return,
HLSLToken_Continue,
HLSLToken_Discard,
HLSLToken_Const,
HLSLToken_Static,
HLSLToken_Inline,
// Input modifiers.
HLSLToken_Uniform,
HLSLToken_In,
HLSLToken_Out,
HLSLToken_InOut,
//ImageType tokens
HLSLToken_ImageFormat_First,
HLSLToken_ImageFormat_RGBA32F = HLSLToken_ImageFormat_First,
HLSLToken_ImageFormat_RGBA16F,
HLSLToken_ImageFormat_RG32F,
HLSLToken_ImageFormat_RG16F,
HLSLToken_ImageFormat_R11G11B10F,
HLSLToken_ImageFormat_R32F,
HLSLToken_ImageFormat_R16F,
HLSLToken_ImageFormat_RGBA16Un,
HLSLToken_ImageFormat_RGB10A2Un,
HLSLToken_ImageFormat_RGBA8Un,
HLSLToken_ImageFormat_RG16Un,
HLSLToken_ImageFormat_RG8Un,
HLSLToken_ImageFormat_R16Un,
HLSLToken_ImageFormat_R8Un,
HLSLToken_ImageFormat_RGBA16Sn,
HLSLToken_ImageFormat_RGBA8Sn,
HLSLToken_ImageFormat_RG16Sn,
HLSLToken_ImageFormat_RG8Sn,
HLSLToken_ImageFormat_R16Sn,
HLSLToken_ImageFormat_R8Sn,
HLSLToken_ImageFormat_RGBA32I,
HLSLToken_ImageFormat_RGBA16I,
HLSLToken_ImageFormat_RGBA8I,
HLSLToken_ImageFormat_RG32I,
HLSLToken_ImageFormat_RG16I,
HLSLToken_ImageFormat_RG8I,
HLSLToken_ImageFormat_R32I,
HLSLToken_ImageFormat_R16I,
HLSLToken_ImageFormat_R8I,
HLSLToken_ImageFormat_RGBA32UI,
HLSLToken_ImageFormat_RGBA16UI,
HLSLToken_ImageFormat_RGB10A2UI,
HLSLToken_ImageFormat_RGBA8UI,
HLSLToken_ImageFormat_RG32UI,
HLSLToken_ImageFormat_RG16UI,
HLSLToken_ImageFormat_RG8UI,
HLSLToken_ImageFormat_R32UI,
HLSLToken_ImageFormat_R16UI,
HLSLToken_ImageFormat_R8UI,
HLSLToken_ImageFormat_Last = HLSLToken_ImageFormat_R8UI,
// Multi-character symbols.
HLSLToken_LessEqual,
HLSLToken_GreaterEqual,
HLSLToken_EqualEqual,
HLSLToken_NotEqual,
HLSLToken_PlusPlus,
HLSLToken_MinusMinus,
HLSLToken_PlusEqual,
HLSLToken_MinusEqual,
HLSLToken_TimesEqual,
HLSLToken_DivideEqual,
HLSLToken_AndAnd, // &&
HLSLToken_BarBar, // ||
HLSLToken_BitShiftLeft, // <<
HLSLToken_BitShiftRight,// >>
// Other token types.
HLSLToken_FloatLiteral,
HLSLToken_HalfLiteral,
HLSLToken_IntLiteral,
HLSLToken_Identifier,
HLSLToken_EndOfStream,
};
class HLSLTokenizer
{
public:
/// Maximum string length of an identifier.
static const int s_maxIdentifier = 255 + 1;
/** The file name is only used for error reporting. */
HLSLTokenizer(Logger* logger, const char* fileName, const char* buffer, size_t length);
/** Advances to the next token in the stream. */
void Next();
/** Get token ID from the string */
static int GetTokenID(const char* name);
/** Returns the current token in the stream. */
int GetToken() const;
/** Returns the number of the current token. */
float GetFloat() const;
int GetInt() const;
/** Returns the identifier for the current token. */
const char* GetIdentifier() const;
/** Returns the line number where the current token began. */
int GetLineNumber() const;
/** Returns the file name where the current token began. */
const char* GetFileName() const;
/** Gets a human readable text description of the current token. */
void GetTokenName(char buffer[s_maxIdentifier]) const;
/** Reports an error using printf style formatting. The current line number
is included. Only the first error reported will be output. */
void Error(const char* format, ...);
/** Gets a human readable text description of the specified token. */
static void GetTokenName(int token, char buffer[s_maxIdentifier]);
private:
bool SkipWhitespace();
bool SkipComment();
bool SkipPragmaDirective();
bool ScanNumber();
bool ScanLineDirective();
private:
Logger* m_logger;
const char* m_fileName;
const char* m_buffer;
const char* m_bufferEnd;
int m_lineNumber;
bool m_error;
int m_token;
float m_fValue;
int m_iValue;
char m_identifier[s_maxIdentifier];
char m_lineDirectiveFileName[s_maxIdentifier];
int m_tokenLineNumber;
};
}
#endif