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 #ifndef SRC_READER_WGSL_TOKEN_H_ 16 #define SRC_READER_WGSL_TOKEN_H_ 17 18 #include <string> 19 20 #include "src/source.h" 21 22 namespace tint { 23 namespace reader { 24 namespace wgsl { 25 26 /// Stores tokens generated by the Lexer 27 class Token { 28 public: 29 /// The type of the parsed token 30 enum class Type { 31 /// Error result 32 kError = -2, 33 /// Uninitialized token 34 kUninitialized = 0, 35 /// End of input string reached 36 kEOF, 37 38 /// An identifier 39 kIdentifier, 40 /// A float value 41 kFloatLiteral, 42 /// An signed int value 43 kSintLiteral, 44 /// A unsigned int value 45 kUintLiteral, 46 47 /// A '&' 48 kAnd, 49 /// A '&&' 50 kAndAnd, 51 /// A '->' 52 kArrow, 53 /// A '[[' 54 kAttrLeft, 55 /// A ']]' 56 kAttrRight, 57 /// A '/' 58 kForwardSlash, 59 /// A '!' 60 kBang, 61 /// A '[' 62 kBracketLeft, 63 /// A ']' 64 kBracketRight, 65 /// A '{' 66 kBraceLeft, 67 /// A '}' 68 kBraceRight, 69 /// A ':' 70 kColon, 71 /// A ',' 72 kComma, 73 /// A '=' 74 kEqual, 75 /// A '==' 76 kEqualEqual, 77 /// A '>' 78 kGreaterThan, 79 /// A '>=' 80 kGreaterThanEqual, 81 /// A '>>' 82 kShiftRight, 83 /// A '<' 84 kLessThan, 85 /// A '<=' 86 kLessThanEqual, 87 /// A '<<' 88 kShiftLeft, 89 /// A '%' 90 kMod, 91 /// A '-' 92 kMinus, 93 /// A '--' 94 kMinusMinus, 95 /// A '!=' 96 kNotEqual, 97 /// A '.' 98 kPeriod, 99 /// A '+' 100 kPlus, 101 /// A '++' 102 kPlusPlus, 103 /// A '|' 104 kOr, 105 /// A '||' 106 kOrOr, 107 /// A '(' 108 kParenLeft, 109 /// A ')' 110 kParenRight, 111 /// A ';' 112 kSemicolon, 113 /// A '*' 114 kStar, 115 /// A '~' 116 kTilde, 117 /// A '_' 118 kUnderscore, 119 /// A '^' 120 kXor, 121 122 /// A 'array' 123 kArray, 124 /// A 'atomic' 125 kAtomic, 126 /// A 'bitcast' 127 kBitcast, 128 /// A 'bool' 129 kBool, 130 /// A 'break' 131 kBreak, 132 /// A 'case' 133 kCase, 134 /// A 'continue' 135 kContinue, 136 /// A 'continuing' 137 kContinuing, 138 /// A 'discard' 139 kDiscard, 140 /// A 'default' 141 kDefault, 142 /// A 'else' 143 kElse, 144 /// A 'elseif' 145 kElseIf, 146 /// A 'f32' 147 kF32, 148 /// A 'fallthrough' 149 kFallthrough, 150 /// A 'false' 151 kFalse, 152 /// A 'fn' 153 kFn, 154 // A 'for' 155 kFor, 156 // A 'Bgra8Unorm' format 157 kFormatBgra8Unorm, 158 // A 'Bgra8UnormSrgb' format 159 kFormatBgra8UnormSrgb, 160 // A 'R16Float' format 161 kFormatR16Float, 162 // A 'R16Sint' format 163 kFormatR16Sint, 164 // A 'R16Uint' format 165 kFormatR16Uint, 166 // A 'R32Float' format 167 kFormatR32Float, 168 // A 'R32Sint' format 169 kFormatR32Sint, 170 // A 'R32Uint' format 171 kFormatR32Uint, 172 // A 'R8Sint' format 173 kFormatR8Sint, 174 // A 'R8Snorm' format 175 kFormatR8Snorm, 176 // A 'R8Uint' format 177 kFormatR8Uint, 178 // A 'R8Unorm' format 179 kFormatR8Unorm, 180 // A 'Rg11B10Float' format 181 kFormatRg11B10Float, 182 // A 'Rg16Float' format 183 kFormatRg16Float, 184 // A 'Rg16Sint' format 185 kFormatRg16Sint, 186 // A 'Rg16Uint' format 187 kFormatRg16Uint, 188 // A 'Rg32Float' format 189 kFormatRg32Float, 190 // A 'Rg32Sint' format 191 kFormatRg32Sint, 192 // A 'Rg32Uint' format 193 kFormatRg32Uint, 194 // A 'Rg8Sint' format 195 kFormatRg8Sint, 196 // A 'Rg8Snorm' format 197 kFormatRg8Snorm, 198 // A 'Rg8Uint' format 199 kFormatRg8Uint, 200 // A 'Rg8Unorm' format 201 kFormatRg8Unorm, 202 // A 'Rgb10A2Unorm' format 203 kFormatRgb10A2Unorm, 204 // A 'Rgba16Float' format 205 kFormatRgba16Float, 206 // A 'Rgba16Sint' format 207 kFormatRgba16Sint, 208 // A 'Rgba16Uint' format 209 kFormatRgba16Uint, 210 // A 'Rgba32Float' format 211 kFormatRgba32Float, 212 // A 'Rgba32Sint' format 213 kFormatRgba32Sint, 214 // A 'Rgba32Uint' format 215 kFormatRgba32Uint, 216 // A 'Rgba8Sint' format 217 kFormatRgba8Sint, 218 // A 'Rgba8Snorm' format 219 kFormatRgba8Snorm, 220 // A 'Rgba8Uint' format 221 kFormatRgba8Uint, 222 // A 'Rgba8Unorm' format 223 kFormatRgba8Unorm, 224 // A 'Rgba8UnormSrgb' format 225 kFormatRgba8UnormSrgb, 226 /// A 'function' 227 kFunction, 228 /// A 'i32' 229 kI32, 230 /// A 'if' 231 kIf, 232 /// A 'image' 233 kImage, 234 /// A 'import' 235 kImport, 236 /// A 'let' 237 kLet, 238 /// A 'loop' 239 kLoop, 240 /// A 'mat2x2' 241 kMat2x2, 242 /// A 'mat2x3' 243 kMat2x3, 244 /// A 'mat2x4' 245 kMat2x4, 246 /// A 'mat3x2' 247 kMat3x2, 248 /// A 'mat3x3' 249 kMat3x3, 250 /// A 'mat3x4' 251 kMat3x4, 252 /// A 'mat4x2' 253 kMat4x2, 254 /// A 'mat4x3' 255 kMat4x3, 256 /// A 'mat4x4' 257 kMat4x4, 258 /// A 'private' 259 kPrivate, 260 /// A 'ptr' 261 kPtr, 262 /// A 'return' 263 kReturn, 264 /// A 'sampler' 265 kSampler, 266 /// A 'sampler_comparison' 267 kComparisonSampler, 268 /// A 'storage' 269 kStorage, 270 /// A 'struct' 271 kStruct, 272 /// A 'switch' 273 kSwitch, 274 /// A 'texture_depth_2d' 275 kTextureDepth2d, 276 /// A 'texture_depth_2d_array' 277 kTextureDepth2dArray, 278 /// A 'texture_depth_cube' 279 kTextureDepthCube, 280 /// A 'texture_depth_cube_array' 281 kTextureDepthCubeArray, 282 /// A 'texture_depth_multisampled_2d' 283 kTextureDepthMultisampled2d, 284 /// A 'texture_external' 285 kTextureExternal, 286 /// A 'texture_multisampled_2d' 287 kTextureMultisampled2d, 288 /// A 'texture_1d' 289 kTextureSampled1d, 290 /// A 'texture_2d' 291 kTextureSampled2d, 292 /// A 'texture_2d_array' 293 kTextureSampled2dArray, 294 /// A 'texture_3d' 295 kTextureSampled3d, 296 /// A 'texture_cube' 297 kTextureSampledCube, 298 /// A 'texture_cube_array' 299 kTextureSampledCubeArray, 300 /// A 'texture_storage_1d' 301 kTextureStorage1d, 302 /// A 'texture_storage_2d' 303 kTextureStorage2d, 304 /// A 'texture_storage_2d_array' 305 kTextureStorage2dArray, 306 /// A 'texture_storage_3d' 307 kTextureStorage3d, 308 /// A 'true' 309 kTrue, 310 /// A 'type' 311 kType, 312 /// A 'u32' 313 kU32, 314 /// A 'uniform' 315 kUniform, 316 /// A 'var' 317 kVar, 318 /// A 'vec2' 319 kVec2, 320 /// A 'vec3' 321 kVec3, 322 /// A 'vec4' 323 kVec4, 324 /// A 'workgroup' 325 kWorkgroup, 326 }; 327 328 /// Converts a token type to a name 329 /// @param type the type to convert 330 /// @returns the token type as as string 331 static std::string TypeToName(Type type); 332 333 /// Creates an uninitialized token 334 Token(); 335 /// Create a Token 336 /// @param type the Token::Type of the token 337 /// @param source the source of the token 338 Token(Type type, const Source& source); 339 340 /// Create a string Token 341 /// @param type the Token::Type of the token 342 /// @param source the source of the token 343 /// @param val the source string for the token 344 Token(Type type, const Source& source, const std::string& val); 345 /// Create a unsigned integer Token 346 /// @param source the source of the token 347 /// @param val the source unsigned for the token 348 Token(const Source& source, uint32_t val); 349 /// Create a signed integer Token 350 /// @param source the source of the token 351 /// @param val the source integer for the token 352 Token(const Source& source, int32_t val); 353 /// Create a float Token 354 /// @param source the source of the token 355 /// @param val the source float for the token 356 Token(const Source& source, float val); 357 /// Move constructor 358 Token(Token&&); 359 /// Copy constructor 360 Token(const Token&); 361 ~Token(); 362 363 /// Assignment operator 364 /// @param b the token to copy 365 /// @return Token 366 Token& operator=(const Token& b); 367 368 /// Returns true if the token is of the given type 369 /// @param t the type to check against. 370 /// @returns true if the token is of type `t` Is(Type t)371 bool Is(Type t) const { return type_ == t; } 372 373 /// @returns true if the token is uninitialized IsUninitialized()374 bool IsUninitialized() const { return type_ == Type::kUninitialized; } 375 /// @returns true if the token is EOF IsEof()376 bool IsEof() const { return type_ == Type::kEOF; } 377 /// @returns true if the token is Error IsError()378 bool IsError() const { return type_ == Type::kError; } 379 /// @returns true if the token is an identifier IsIdentifier()380 bool IsIdentifier() const { return type_ == Type::kIdentifier; } 381 /// @returns true if the token is a literal IsLiteral()382 bool IsLiteral() const { 383 return type_ == Type::kSintLiteral || type_ == Type::kFalse || 384 type_ == Type::kUintLiteral || type_ == Type::kTrue || 385 type_ == Type::kFloatLiteral; 386 } 387 /// @returns true if token is a 'matNxM' IsMatrix()388 bool IsMatrix() const { 389 return type_ == Type::kMat2x2 || type_ == Type::kMat2x3 || 390 type_ == Type::kMat2x4 || type_ == Type::kMat3x2 || 391 type_ == Type::kMat3x3 || type_ == Type::kMat3x4 || 392 type_ == Type::kMat4x2 || type_ == Type::kMat4x3 || 393 type_ == Type::kMat4x4; 394 } 395 /// @returns true if token is a 'mat3xM' IsMat3xN()396 bool IsMat3xN() const { 397 return type_ == Type::kMat3x2 || type_ == Type::kMat3x3 || 398 type_ == Type::kMat3x4; 399 } 400 /// @returns true if token is a 'mat4xM' IsMat4xN()401 bool IsMat4xN() const { 402 return type_ == Type::kMat4x2 || type_ == Type::kMat4x3 || 403 type_ == Type::kMat4x4; 404 } 405 /// @returns true if token is a 'matNx3' IsMatNx3()406 bool IsMatNx3() const { 407 return type_ == Type::kMat2x3 || type_ == Type::kMat3x3 || 408 type_ == Type::kMat4x3; 409 } 410 /// @returns true if token is a 'matNx4' IsMatNx4()411 bool IsMatNx4() const { 412 return type_ == Type::kMat2x4 || type_ == Type::kMat3x4 || 413 type_ == Type::kMat4x4; 414 } 415 416 /// @returns true if token is a 'vecN' IsVector()417 bool IsVector() const { 418 return type_ == Type::kVec2 || type_ == Type::kVec3 || type_ == Type::kVec4; 419 } 420 421 /// @returns the source information for this token source()422 Source source() const { return source_; } 423 424 /// Returns the string value of the token 425 /// @return const std::string& 426 std::string to_str() const; 427 /// Returns the float value of the token. 0 is returned if the token does not 428 /// contain a float value. 429 /// @return float 430 float to_f32() const; 431 /// Returns the uint32 value of the token. 0 is returned if the token does not 432 /// contain a unsigned integer value. 433 /// @return uint32_t 434 uint32_t to_u32() const; 435 /// Returns the int32 value of the token. 0 is returned if the token does not 436 /// contain a signed integer value. 437 /// @return int32_t 438 int32_t to_i32() const; 439 440 /// @returns the token type as string to_name()441 std::string to_name() const { return Token::TypeToName(type_); } 442 443 private: 444 /// The Token::Type of the token 445 Type type_ = Type::kError; 446 /// The source where the token appeared 447 Source source_; 448 /// The string represented by the token 449 std::string val_str_; 450 /// The signed integer represented by the token 451 int32_t val_int_ = 0; 452 /// The unsigned integer represented by the token 453 uint32_t val_uint_ = 0; 454 /// The float value represented by the token 455 float val_float_ = 0.0; 456 }; 457 458 #ifndef NDEBUG 459 inline std::ostream& operator<<(std::ostream& out, Token::Type type) { 460 out << Token::TypeToName(type); 461 return out; 462 } 463 #endif // NDEBUG 464 465 } // namespace wgsl 466 } // namespace reader 467 } // namespace tint 468 469 #endif // SRC_READER_WGSL_TOKEN_H_ 470