yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
3.3 KiB132 linesraw
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{
25    enum Enum : TokenFlags
26    {
27        AtStartOfLine = 1 << 0,
28        AfterWhitespace = 1 << 1,
29        ScrubbingNeeded = 1 << 2,
30        Name = 1 << 3, ///< Determines if 'name' is set or 'chars' in the charsNameUnion
31    };
32};
33
34class Token
35{
36public:
37    TokenType type = TokenType::Unknown;
38    TokenFlags flags = 0;
39
40    SourceLoc loc;
41    uint32_t charsCount = 0; ///< Amount of characters. Is set if name or not.
42
43    union CharsNameUnion
44    {
45        const char* chars;
46        Name* name;
47    };
48
49    CharsNameUnion charsNameUnion;
50
51    bool hasContent() const { return charsCount > 0; }
52    Index getContentLength() const { return charsCount; }
53
54    UnownedStringSlice getContent() const;
55    /// Set content
56    void setContent(const UnownedStringSlice& content);
57
58    Name* getName() const;
59
60    Name* getNameOrNull() const;
61
62    SourceLoc getLoc() const { return loc; }
63
64    /// Set the name
65    SLANG_FORCE_INLINE void setName(Name* inName);
66
67    Token() { charsNameUnion.chars = nullptr; }
68
69    Token(
70        TokenType inType,
71        const UnownedStringSlice& inContent,
72        SourceLoc inLoc,
73        TokenFlags inFlags = 0)
74        : flags(inFlags)
75    {
76        SLANG_ASSERT((inFlags & TokenFlag::Name) == 0);
77        type = inType;
78        charsNameUnion.chars = inContent.begin();
79        charsCount = uint32_t(inContent.getLength());
80        loc = inLoc;
81    }
82    Token(TokenType inType, Name* name, SourceLoc inLoc, TokenFlags inFlags = 0)
83    {
84        SLANG_ASSERT(name);
85        type = inType;
86        flags = inFlags | TokenFlag::Name;
87        charsNameUnion.name = name;
88        charsCount = uint32_t(name->text.getLength());
89        loc = inLoc;
90    }
91};
92
93// ---------------------------------------------------------------------------
94SLANG_FORCE_INLINE UnownedStringSlice Token::getContent() const
95{
96    return (flags & TokenFlag::Name) ? charsNameUnion.name->text.getUnownedSlice()
97                                     : UnownedStringSlice(charsNameUnion.chars, charsCount);
98}
99
100// ---------------------------------------------------------------------------
101SLANG_FORCE_INLINE Name* Token::getName() const
102{
103    return getNameOrNull();
104}
105
106// ---------------------------------------------------------------------------
107SLANG_FORCE_INLINE Name* Token::getNameOrNull() const
108{
109    return (flags & TokenFlag::Name) ? charsNameUnion.name : nullptr;
110}
111
112// ---------------------------------------------------------------------------
113SLANG_FORCE_INLINE void Token::setContent(const UnownedStringSlice& content)
114{
115    flags &= ~TokenFlag::Name;
116    charsNameUnion.chars = content.begin();
117    charsCount = uint32_t(content.getLength());
118}
119
120// ---------------------------------------------------------------------------
121SLANG_FORCE_INLINE void Token::setName(Name* inName)
122{
123    SLANG_ASSERT(inName);
124    flags |= TokenFlag::Name;
125    charsNameUnion.name = inName;
126    charsCount = uint32_t(inName->text.getLength());
127}
128
129
130} // namespace Slang
131
132#endif