yum-mirror/slang

Making it easier to work with shaders

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

Ellie Hermaszewskaformatf65d756bf

master
3.0 KiB117 linesraw
1// slang-json-lexer.h
2#ifndef SLANG_JSON_LEXER_H
3#define SLANG_JSON_LEXER_H
4
5#include "../core/slang-basic.h"
6#include "slang-diagnostic-sink.h"
7#include "slang-source-loc.h"
8
9namespace Slang
10{
11
12enum class JSONTokenType
13{
14    Invalid,
15    IntegerLiteral,
16    FloatLiteral,
17    StringLiteral,
18    LBracket,
19    RBracket,
20    LBrace,
21    RBrace,
22    Comma,
23    Colon,
24    True,
25    False,
26    Null,
27    EndOfFile,
28    CountOf,
29};
30
31struct JSONToken
32{
33    JSONTokenType type; ///< The token type
34    SourceLoc loc;      ///< Location in the source file
35    uint32_t length;    ///< The length of the token in bytes
36};
37
38UnownedStringSlice getJSONTokenAsText(JSONTokenType type);
39
40class JSONLexer
41{
42public:
43    /// Peek the current token
44    JSONToken& peekToken() { return m_token; }
45    /// Peek the current type
46    JSONTokenType peekType() { return m_token.type; }
47    /// Peek the current SourceLoc
48    SourceLoc peekLoc() { return m_token.loc; }
49
50    /// Get the lexeme of JSONToken
51    UnownedStringSlice getLexeme(const JSONToken& tok) const;
52    /// Peek the lexeme at the current position
53    UnownedStringSlice peekLexeme() const { return getLexeme(m_token); }
54
55    JSONTokenType advance();
56
57    /// Expects a token of type type. If found advances, if not returns an error and outputs to
58    /// diagnostic sink
59    SlangResult expect(JSONTokenType type);
60    /// Same as expect except out will hold the token.
61    SlangResult expect(JSONTokenType type, JSONToken& out);
62
63    /// Returns true and advances if current token is type
64    bool advanceIf(JSONTokenType type);
65    bool advanceIf(JSONTokenType type, JSONToken& out);
66
67    /// Must be called before use
68    SlangResult init(SourceView* sourceView, DiagnosticSink* sink);
69
70    /// Determines the first token from text. Useful for diagnostics on DiagnosticSink
71    static UnownedStringSlice calcLexemeLocation(const UnownedStringSlice& text);
72
73protected:
74    struct LexResult
75    {
76        JSONTokenType type;
77        const char* cursor;
78    };
79
80    /// Get the location of the cursor
81    SLANG_FORCE_INLINE SourceLoc _getLoc(const char* cursor) const
82    {
83        return m_startLoc + (cursor - m_contentStart);
84    }
85    const char* _lexLineComment(const char* cursor);
86    const char* _lexBlockComment(const char* cursor);
87    const char* _lexWhitespace(const char* cursor);
88    const char* _lexString(const char* cursor);
89    LexResult _lexNumber(const char* cursor);
90
91    SLANG_FORCE_INLINE JSONTokenType _setToken(JSONTokenType type, const char* cursor)
92    {
93        SLANG_ASSERT(cursor >= m_lexemeStart);
94        m_token.type = type;
95        m_token.loc = m_startLoc + (m_lexemeStart - m_contentStart);
96        m_token.length = uint32_t(cursor - m_lexemeStart);
97        m_cursor = cursor;
98        return type;
99    }
100    JSONTokenType _setInvalidToken();
101
102    JSONToken m_token;
103
104    const char* m_cursor;
105    const char* m_lexemeStart;
106
107    const char* m_contentStart;
108
109    SourceLoc m_startLoc;
110
111    SourceView* m_sourceView;
112    DiagnosticSink* m_sink;
113};
114
115} // namespace Slang
116
117#endif