yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
1// slang-token.h 2#ifndef SLANG_TOKEN_H_INCLUDED 3#define SLANG_TOKEN_H_INCLUDED 4 5#include "../core/slang-basic.h" 6#include "slang-name.h" 7#include "slang-source-loc.h" 8 9namespace Slang 10{ 11 12class Name ; 13 14enum class TokenType :uint8_t 15{ 16#define TOKEN (NAME ,DESC ) NAME, 17#include "slang-token-defs.h" 18}; 19 20char const * TokenTypeToString (TokenType type ); 21 22typedef uint8_t TokenFlags ; 23struct TokenFlag 24{ 25enum Enum :TokenFlags 26 { 27AtStartOfLine = 1 <<0 , 28AfterWhitespace = 1 <<1 , 29ScrubbingNeeded = 1 <<2 , 30Name = 1 <<3 ,///< Determines if 'name' is set or 'chars' in the charsNameUnion 31 }; 32}; 33 34class Token 35{ 36public : 37TokenType type = TokenType ::Unknown ; 38TokenFlags flags = 0 ; 39 40SourceLoc loc ; 41uint32_t charsCount = 0 ;///< Amount of characters. Is set if name or not. 42 43union CharsNameUnion 44 { 45const char * chars ; 46Name * name ; 47 }; 48 49CharsNameUnion charsNameUnion ; 50 51bool hasContent ()const {return charsCount > 0 ; } 52Index getContentLength ()const {return charsCount ; } 53 54UnownedStringSlice getContent ()const ; 55/// Set content 56void setContent (const UnownedStringSlice & content ); 57 58Name * getName ()const ; 59 60Name * getNameOrNull ()const ; 61 62SourceLoc getLoc ()const {return loc ; } 63 64/// Set the name 65SLANG_FORCE_INLINE void setName (Name * inName ); 66 67Token () {charsNameUnion .chars = nullptr ; } 68 69Token ( 70TokenType inType , 71const UnownedStringSlice & inContent , 72SourceLoc inLoc , 73TokenFlags inFlags = 0 ) 74 :flags (inFlags ) 75 { 76SLANG_ASSERT ((inFlags & TokenFlag ::Name )== 0 ); 77type = inType ; 78charsNameUnion .chars = inContent .begin (); 79charsCount = uint32_t (inContent .getLength ()); 80loc = inLoc ; 81 } 82Token (TokenType inType ,Name * name ,SourceLoc inLoc ,TokenFlags inFlags = 0 ) 83 { 84SLANG_ASSERT (name ); 85type = inType ; 86flags = inFlags |TokenFlag ::Name ; 87charsNameUnion .name = name ; 88charsCount = uint32_t (name -> text .getLength ()); 89loc = inLoc ; 90 } 91}; 92 93// --------------------------------------------------------------------------- 94SLANG_FORCE_INLINE UnownedStringSlice Token ::getContent ()const 95{ 96return (flags & TokenFlag ::Name ) ?charsNameUnion .name -> text .getUnownedSlice () 97 :UnownedStringSlice (charsNameUnion .chars ,charsCount ); 98} 99 100// --------------------------------------------------------------------------- 101SLANG_FORCE_INLINE Name * Token ::getName ()const 102{ 103return getNameOrNull (); 104} 105 106// --------------------------------------------------------------------------- 107SLANG_FORCE_INLINE Name * Token ::getNameOrNull ()const 108{ 109return (flags & TokenFlag ::Name ) ?charsNameUnion .name :nullptr ; 110} 111 112// --------------------------------------------------------------------------- 113SLANG_FORCE_INLINE void Token ::setContent (const UnownedStringSlice & content ) 114{ 115flags &= ~TokenFlag ::Name ; 116charsNameUnion .chars = content .begin (); 117charsCount = uint32_t (content .getLength ()); 118} 119 120// --------------------------------------------------------------------------- 121SLANG_FORCE_INLINE void Token ::setName (Name * inName ) 122{ 123SLANG_ASSERT (inName ); 124flags |=TokenFlag ::Name ; 125charsNameUnion .name = inName ; 126charsCount = uint32_t (inName -> text .getLength ()); 127} 128 129 130}// namespace Slang 131 132#endif