forked from unknownworlds/hlslparser
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathHLSLTokenizer.h
172 lines (143 loc) · 4 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
#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_Texture,
HLSLToken_Sampler,
HLSLToken_Sampler2D,
HLSLToken_Sampler3D,
HLSLToken_SamplerCube,
HLSLToken_Sampler2DShadow,
HLSLToken_Sampler2DMS,
HLSLToken_Sampler2DArray,
// Reserved words.
HLSLToken_If,
HLSLToken_Else,
HLSLToken_For,
HLSLToken_While,
HLSLToken_Break,
HLSLToken_True,
HLSLToken_False,
HLSLToken_Void,
HLSLToken_Struct,
HLSLToken_CBuffer,
HLSLToken_TBuffer,
HLSLToken_Register,
HLSLToken_Return,
HLSLToken_Continue,
HLSLToken_Discard,
HLSLToken_Const,
HLSLToken_Static,
HLSLToken_Inline,
// Input modifiers.
HLSLToken_Uniform,
HLSLToken_In,
HLSLToken_Out,
HLSLToken_InOut,
// Effect keywords.
HLSLToken_SamplerState,
HLSLToken_Technique,
HLSLToken_Pass,
// 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, // ||
// 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(const char* fileName, const char* buffer, size_t length);
/** Advances to the next token in the stream. */
void Next();
/** 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:
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