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