forked from crosire/reshade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.hpp
244 lines (226 loc) · 4.62 KB
/
lexer.hpp
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
* Copyright (C) 2014 Patrick Mours. All rights reserved.
* License: https://github.com/crosire/reshade#license
*/
#pragma once
#include "source_location.hpp"
namespace reshadefx
{
class lexer
{
lexer &operator=(const lexer &) = delete;
public:
/// <summary>
/// A collection of identifiers for various possible tokens.
/// </summary>
enum class tokenid
{
unknown = -1,
end_of_file = 0,
end_of_line = '\n',
// operators
space = ' ',
exclaim = '!',
hash = '#',
dollar = '$',
percent = '%',
ampersand = '&',
parenthesis_open = '(',
parenthesis_close = ')',
star = '*',
plus = '+',
comma = ',',
minus = '-',
dot = '.',
slash = '/',
colon = ':',
semicolon = ';',
less = '<',
equal = '=',
greater = '>',
question = '?',
at = '@',
bracket_open = '[',
backslash = '\\',
bracket_close = ']',
caret = '^',
brace_open = '{',
pipe = '|',
brace_close = '}',
tilde = '~',
exclaim_equal = 256 /* != */,
percent_equal /* %= */,
ampersand_ampersand /* && */,
ampersand_equal /* &= */,
star_equal /* *= */,
plus_plus /* ++*/,
plus_equal /* += */,
minus_minus /* -- */,
minus_equal /* -= */,
arrow /* -> */,
ellipsis /* ... */,
slash_equal /* /= */,
colon_colon /* :: */,
less_less_equal /* <<= */,
less_less /* << */,
less_equal /* <= */,
equal_equal /* == */,
greater_greater_equal /* >>= */,
greater_greater /* >> */,
greater_equal /* >= */,
caret_equal /* ^= */,
pipe_equal /* |= */,
pipe_pipe /* || */,
// identifiers
reserved,
identifier,
// literals
true_literal,
false_literal,
int_literal,
uint_literal,
float_literal,
double_literal,
string_literal,
// keywords
namespace_,
struct_,
technique,
pass,
for_,
while_,
do_,
if_,
else_,
switch_,
case_,
default_,
break_,
continue_,
return_,
discard_,
extern_,
static_,
uniform_,
volatile_,
precise,
in,
out,
inout,
const_,
linear,
noperspective,
centroid,
nointerpolation,
void_,
bool_,
bool2,
bool2x2,
bool3,
bool3x3,
bool4,
bool4x4,
int_,
int2,
int2x2,
int3,
int3x3,
int4,
int4x4,
uint_,
uint2,
uint2x2,
uint3,
uint3x3,
uint4,
uint4x4,
float_,
float2,
float2x2,
float3,
float3x3,
float4,
float4x4,
vector,
matrix,
string_,
texture,
sampler,
// preprocessor directives
hash_def,
hash_undef,
hash_if,
hash_ifdef,
hash_ifndef,
hash_else,
hash_elif,
hash_endif,
hash_error,
hash_warning,
hash_pragma,
hash_include,
hash_unknown,
};
/// <summary>
/// A structure describing a single token in the input string.
/// </summary>
struct token
{
tokenid id;
location location;
size_t offset, length;
union
{
int literal_as_int;
unsigned int literal_as_uint;
float literal_as_float;
double literal_as_double;
};
std::string literal_as_string;
inline operator tokenid() const { return id; }
};
/// <summary>
/// Construct a copy of an existing instance.
/// </summary>
/// <param name="lexer">The instance to copy.</param>
lexer(const lexer &lexer);
/// <summary>
/// Construct a new lexical analyzer for an input string.
/// </summary>
/// <param name="source">The string to analyze.</param>
explicit lexer(
const std::string &input,
bool ignore_whitespace = true,
bool ignore_pp_directives = true,
bool ignore_keywords = false,
bool escape_string_literals = true);
/// <summary>
/// Get the input string this lexical analyzer works on.
/// </summary>
/// <returns>A constant reference to the input string.</returns>
inline const std::string &input_string() const { return _input; }
/// <summary>
/// Perform lexical analysis on the input string and return the next token in sequence.
/// </summary>
/// <returns>The next token from the input string.</returns>
token lex();
/// <summary>
/// Advances to the next token that is not whitespace.
/// </summary>
void skip_space();
/// <summary>
/// Advances to the next new line, ignoring all tokens.
/// </summary>
void skip_to_next_line();
private:
void skip(size_t length);
void parse_identifier(token &tok) const;
bool parse_pp_directive(token &tok);
void parse_string_literal(token &tok, bool escape) const;
void parse_numeric_literal(token &tok) const;
std::string _input;
location _cur_location;
const std::string::value_type *_cur, *_end;
bool _ignore_whitespace, _ignore_pp_directives, _ignore_keywords, _escape_string_literals;
};
}