1 // Copyright 2020 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/reader/wgsl/token.h"
16
17 namespace tint {
18 namespace reader {
19 namespace wgsl {
20
21 // static
TypeToName(Type type)22 std::string Token::TypeToName(Type type) {
23 switch (type) {
24 case Token::Type::kError:
25 return "kError";
26 case Token::Type::kEOF:
27 return "kEOF";
28 case Token::Type::kIdentifier:
29 return "kIdentifier";
30 case Token::Type::kFloatLiteral:
31 return "kFloatLiteral";
32 case Token::Type::kSintLiteral:
33 return "kSintLiteral";
34 case Token::Type::kUintLiteral:
35 return "kUintLiteral";
36 case Token::Type::kUninitialized:
37 return "kUninitialized";
38
39 case Token::Type::kAnd:
40 return "&";
41 case Token::Type::kAndAnd:
42 return "&&";
43 case Token::Type::kArrow:
44 return "->";
45 case Token::Type::kAttrLeft:
46 return "[[";
47 case Token::Type::kAttrRight:
48 return "]]";
49 case Token::Type::kForwardSlash:
50 return "/";
51 case Token::Type::kBang:
52 return "!";
53 case Token::Type::kBracketLeft:
54 return "[";
55 case Token::Type::kBracketRight:
56 return "]";
57 case Token::Type::kBraceLeft:
58 return "{";
59 case Token::Type::kBraceRight:
60 return "}";
61 case Token::Type::kColon:
62 return ":";
63 case Token::Type::kComma:
64 return ",";
65 case Token::Type::kEqual:
66 return "=";
67 case Token::Type::kEqualEqual:
68 return "==";
69 case Token::Type::kGreaterThan:
70 return ">";
71 case Token::Type::kGreaterThanEqual:
72 return ">=";
73 case Token::Type::kShiftRight:
74 return ">>";
75 case Token::Type::kLessThan:
76 return "<";
77 case Token::Type::kLessThanEqual:
78 return "<=";
79 case Token::Type::kShiftLeft:
80 return "<<";
81 case Token::Type::kMod:
82 return "%";
83 case Token::Type::kNotEqual:
84 return "!=";
85 case Token::Type::kMinus:
86 return "-";
87 case Token::Type::kMinusMinus:
88 return "--";
89 case Token::Type::kPeriod:
90 return ".";
91 case Token::Type::kPlus:
92 return "+";
93 case Token::Type::kPlusPlus:
94 return "++";
95 case Token::Type::kOr:
96 return "|";
97 case Token::Type::kOrOr:
98 return "||";
99 case Token::Type::kParenLeft:
100 return "(";
101 case Token::Type::kParenRight:
102 return ")";
103 case Token::Type::kSemicolon:
104 return ";";
105 case Token::Type::kStar:
106 return "*";
107 case Token::Type::kTilde:
108 return "~";
109 case Token::Type::kUnderscore:
110 return "_";
111 case Token::Type::kXor:
112 return "^";
113
114 case Token::Type::kArray:
115 return "array";
116 case Token::Type::kAtomic:
117 return "atomic";
118 case Token::Type::kBitcast:
119 return "bitcast";
120 case Token::Type::kBool:
121 return "bool";
122 case Token::Type::kBreak:
123 return "break";
124 case Token::Type::kCase:
125 return "case";
126 case Token::Type::kContinue:
127 return "continue";
128 case Token::Type::kContinuing:
129 return "continuing";
130 case Token::Type::kDiscard:
131 return "discard";
132 case Token::Type::kDefault:
133 return "default";
134 case Token::Type::kElse:
135 return "else";
136 case Token::Type::kElseIf:
137 return "elseif";
138 case Token::Type::kF32:
139 return "f32";
140 case Token::Type::kFallthrough:
141 return "fallthrough";
142 case Token::Type::kFalse:
143 return "false";
144 case Token::Type::kFn:
145 return "fn";
146 case Token::Type::kFor:
147 return "for";
148 case Token::Type::kFormatBgra8Unorm:
149 return "bgra8unorm";
150 case Token::Type::kFormatBgra8UnormSrgb:
151 return "bgra8unorm_srgb";
152 case Token::Type::kFormatR16Float:
153 return "r16float";
154 case Token::Type::kFormatR16Sint:
155 return "r16sint";
156 case Token::Type::kFormatR16Uint:
157 return "r16uint";
158 case Token::Type::kFormatR32Float:
159 return "r32float";
160 case Token::Type::kFormatR32Sint:
161 return "r32sint";
162 case Token::Type::kFormatR32Uint:
163 return "r32uint";
164 case Token::Type::kFormatR8Sint:
165 return "r8sint";
166 case Token::Type::kFormatR8Snorm:
167 return "r8snorm";
168 case Token::Type::kFormatR8Uint:
169 return "r8uint";
170 case Token::Type::kFormatR8Unorm:
171 return "r8unorm";
172 case Token::Type::kFormatRg11B10Float:
173 return "rg11b10float";
174 case Token::Type::kFormatRg16Float:
175 return "rg16float";
176 case Token::Type::kFormatRg16Sint:
177 return "rg16sint";
178 case Token::Type::kFormatRg16Uint:
179 return "rg16uint";
180 case Token::Type::kFormatRg32Float:
181 return "rg32float";
182 case Token::Type::kFormatRg32Sint:
183 return "rg32sint";
184 case Token::Type::kFormatRg32Uint:
185 return "rg32uint";
186 case Token::Type::kFormatRg8Sint:
187 return "rg8sint";
188 case Token::Type::kFormatRg8Snorm:
189 return "rg8snorm";
190 case Token::Type::kFormatRg8Uint:
191 return "rg8uint";
192 case Token::Type::kFormatRg8Unorm:
193 return "rg8unorm";
194 case Token::Type::kFormatRgb10A2Unorm:
195 return "rgb10a2unorm";
196 case Token::Type::kFormatRgba16Float:
197 return "rgba16float";
198 case Token::Type::kFormatRgba16Sint:
199 return "rgba16sint";
200 case Token::Type::kFormatRgba16Uint:
201 return "rgba16uint";
202 case Token::Type::kFormatRgba32Float:
203 return "rgba32float";
204 case Token::Type::kFormatRgba32Sint:
205 return "rgba32sint";
206 case Token::Type::kFormatRgba32Uint:
207 return "rgba32uint";
208 case Token::Type::kFormatRgba8Sint:
209 return "rgba8sint";
210 case Token::Type::kFormatRgba8Snorm:
211 return "rgba8snorm";
212 case Token::Type::kFormatRgba8Uint:
213 return "rgba8uint";
214 case Token::Type::kFormatRgba8Unorm:
215 return "rgba8unorm";
216 case Token::Type::kFormatRgba8UnormSrgb:
217 return "rgba8unorm_srgb";
218 case Token::Type::kFunction:
219 return "function";
220 case Token::Type::kI32:
221 return "i32";
222 case Token::Type::kIf:
223 return "if";
224 case Token::Type::kImage:
225 return "image";
226 case Token::Type::kImport:
227 return "import";
228 case Token::Type::kLet:
229 return "let";
230 case Token::Type::kLoop:
231 return "loop";
232 case Token::Type::kMat2x2:
233 return "mat2x2";
234 case Token::Type::kMat2x3:
235 return "mat2x3";
236 case Token::Type::kMat2x4:
237 return "mat2x4";
238 case Token::Type::kMat3x2:
239 return "mat3x2";
240 case Token::Type::kMat3x3:
241 return "mat3x3";
242 case Token::Type::kMat3x4:
243 return "mat3x4";
244 case Token::Type::kMat4x2:
245 return "mat4x2";
246 case Token::Type::kMat4x3:
247 return "mat4x3";
248 case Token::Type::kMat4x4:
249 return "mat4x4";
250 case Token::Type::kPrivate:
251 return "private";
252 case Token::Type::kPtr:
253 return "ptr";
254 case Token::Type::kReturn:
255 return "return";
256 case Token::Type::kSampler:
257 return "sampler";
258 case Token::Type::kComparisonSampler:
259 return "sampler_comparison";
260 case Token::Type::kStorage:
261 return "storage";
262 case Token::Type::kStruct:
263 return "struct";
264 case Token::Type::kSwitch:
265 return "switch";
266 case Token::Type::kTextureDepth2d:
267 return "texture_depth_2d";
268 case Token::Type::kTextureDepth2dArray:
269 return "texture_depth_2d_array";
270 case Token::Type::kTextureDepthCube:
271 return "texture_depth_cube";
272 case Token::Type::kTextureDepthCubeArray:
273 return "texture_depth_cube_array";
274 case Token::Type::kTextureDepthMultisampled2d:
275 return "texture_depth_multisampled_2d";
276 case Token::Type::kTextureExternal:
277 return "texture_external";
278 case Token::Type::kTextureMultisampled2d:
279 return "texture_multisampled_2d";
280 case Token::Type::kTextureSampled1d:
281 return "texture_1d";
282 case Token::Type::kTextureSampled2d:
283 return "texture_2d";
284 case Token::Type::kTextureSampled2dArray:
285 return "texture_2d_array";
286 case Token::Type::kTextureSampled3d:
287 return "texture_3d";
288 case Token::Type::kTextureSampledCube:
289 return "texture_cube";
290 case Token::Type::kTextureSampledCubeArray:
291 return "texture_cube_array";
292 case Token::Type::kTextureStorage1d:
293 return "texture_storage_1d";
294 case Token::Type::kTextureStorage2d:
295 return "texture_storage_2d";
296 case Token::Type::kTextureStorage2dArray:
297 return "texture_storage_2d_array";
298 case Token::Type::kTextureStorage3d:
299 return "texture_storage_3d";
300 case Token::Type::kTrue:
301 return "true";
302 case Token::Type::kType:
303 return "type";
304 case Token::Type::kU32:
305 return "u32";
306 case Token::Type::kUniform:
307 return "uniform";
308 case Token::Type::kVar:
309 return "var";
310 case Token::Type::kVec2:
311 return "vec2";
312 case Token::Type::kVec3:
313 return "vec3";
314 case Token::Type::kVec4:
315 return "vec4";
316 case Token::Type::kWorkgroup:
317 return "workgroup";
318 }
319
320 return "<unknown>";
321 }
322
Token()323 Token::Token() : type_(Type::kUninitialized) {}
324
Token(Type type,const Source & source,const std::string & val)325 Token::Token(Type type, const Source& source, const std::string& val)
326 : type_(type), source_(source), val_str_(val) {}
327
Token(const Source & source,uint32_t val)328 Token::Token(const Source& source, uint32_t val)
329 : type_(Type::kUintLiteral), source_(source), val_uint_(val) {}
330
Token(const Source & source,int32_t val)331 Token::Token(const Source& source, int32_t val)
332 : type_(Type::kSintLiteral), source_(source), val_int_(val) {}
333
Token(const Source & source,float val)334 Token::Token(const Source& source, float val)
335 : type_(Type::kFloatLiteral), source_(source), val_float_(val) {}
336
Token(Type type,const Source & source)337 Token::Token(Type type, const Source& source) : Token(type, source, "") {}
338
339 Token::Token(Token&&) = default;
340
341 Token::Token(const Token&) = default;
342
343 Token::~Token() = default;
344
345 Token& Token::operator=(const Token&) = default;
346
to_str() const347 std::string Token::to_str() const {
348 if (type_ == Type::kFloatLiteral) {
349 return std::to_string(val_float_);
350 }
351 if (type_ == Type::kSintLiteral) {
352 return std::to_string(val_int_);
353 }
354 if (type_ == Type::kUintLiteral) {
355 return std::to_string(val_uint_);
356 }
357 return val_str_;
358 }
359
to_f32() const360 float Token::to_f32() const {
361 return val_float_;
362 }
363
to_u32() const364 uint32_t Token::to_u32() const {
365 return val_uint_;
366 }
367
to_i32() const368 int32_t Token::to_i32() const {
369 return val_int_;
370 }
371
372 } // namespace wgsl
373 } // namespace reader
374 } // namespace tint
375