| 1 |
/* |
| 2 |
* Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2023 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 14 окт. 2019 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef LSP_PLUG_IN_FMT_JSON_TOKENIZER_H_ |
| 23 |
#define LSP_PLUG_IN_FMT_JSON_TOKENIZER_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/runtime/LSPString.h> |
| 27 |
#include <lsp-plug.in/io/IInSequence.h> |
| 28 |
#include <lsp-plug.in/fmt/json/token.h> |
| 29 |
|
| 30 |
namespace lsp |
| 31 |
{ |
| 32 |
namespace json |
| 33 |
{ |
| 34 |
|
| 35 |
class Tokenizer |
| 36 |
{ |
| 37 |
private: |
| 38 |
friend class Serializer; |
| 39 |
|
| 40 |
protected: |
| 41 |
io::IInSequence *pIn; |
| 42 |
lsp_swchar_t cCurrent; |
| 43 |
token_t enToken; |
| 44 |
LSPString sValue; |
| 45 |
union |
| 46 |
{ |
| 47 |
double fValue; |
| 48 |
ssize_t iValue; |
| 49 |
}; |
| 50 |
status_t nError; |
| 51 |
size_t nUnget; |
| 52 |
lsp_utf16_t *vPending; |
| 53 |
size_t nPending; |
| 54 |
size_t nCapacity; |
| 55 |
|
| 56 |
protected: |
| 57 |
lsp_swchar_t skip_whitespace(); |
| 58 |
inline lsp_swchar_t lookup(); |
| 59 |
token_t commit(token_t token); |
| 60 |
inline token_t skip(token_t token) { cCurrent = -1; return enToken = token; } |
| 61 |
lsp_swchar_t commit_lookup(token_t token); |
| 62 |
inline token_t set_error(status_t code); |
| 63 |
|
| 64 |
static bool is_reserved_word(const LSPString *text); |
| 65 |
static bool is_valid_identifier(const LSPString *text); |
| 66 |
static bool is_identifier_start(lsp_wchar_t ch); |
| 67 |
static bool is_identifier(lsp_wchar_t ch); |
| 68 |
static bool parse_digit(int *digit, lsp_wchar_t ch, int radix); |
| 69 |
|
| 70 |
status_t add_pending_character(lsp_utf16_t ch); |
| 71 |
status_t commit_pending_characters(); |
| 72 |
token_t parse_unicode_escape_sequence(token_t type); |
| 73 |
token_t parse_hexadecimal_escape_sequence(token_t type); |
| 74 |
|
| 75 |
token_t parse_string(token_t type); |
| 76 |
token_t parse_identifier(); |
| 77 |
token_t parse_single_line_comment(); |
| 78 |
token_t parse_multiline_comment(); |
| 79 |
token_t parse_number(); |
| 80 |
|
| 81 |
public: |
| 82 |
explicit Tokenizer(io::IInSequence *in); |
| 83 |
Tokenizer(const Tokenizer &) = delete; |
| 84 |
Tokenizer(Tokenizer &&) = delete; |
| 85 |
virtual ~Tokenizer(); |
| 86 |
|
| 87 |
Tokenizer & operator = (const Tokenizer &) = delete; |
| 88 |
Tokenizer & operator = (Tokenizer &&) = delete; |
| 89 |
|
| 90 |
public: |
| 91 |
/** |
| 92 |
* Get token |
| 93 |
* @param get force token lookup |
| 94 |
* @return current token value |
| 95 |
*/ |
| 96 |
token_t get_token(bool get); |
| 97 |
|
| 98 |
/** |
| 99 |
* Unget token |
| 100 |
*/ |
| 101 |
inline void unget() { ++nUnget; }; |
| 102 |
|
| 103 |
/** |
| 104 |
* Get current token |
| 105 |
* @return current token |
| 106 |
*/ |
| 107 |
inline token_t current() const { return enToken; } |
| 108 |
|
| 109 |
/** |
| 110 |
* Get current token value for tokens that consist of characters |
| 111 |
* @return current token value |
| 112 |
*/ |
| 113 |
inline const LSPString *text_value() const { return &sValue; } |
| 114 |
|
| 115 |
/** |
| 116 |
* Get floating-point value of the token |
| 117 |
* @return floating-point value of the token |
| 118 |
*/ |
| 119 |
inline const double float_value() const { return fValue; } |
| 120 |
|
| 121 |
/** |
| 122 |
* Get integer value of the token |
| 123 |
* @return integer value of the token |
| 124 |
*/ |
| 125 |
inline ssize_t int_value() const { return iValue; } |
| 126 |
|
| 127 |
/** |
| 128 |
* Get last error code |
| 129 |
* @return last error code |
| 130 |
*/ |
| 131 |
inline status_t error() const { return nError; } |
| 132 |
}; |
| 133 |
|
| 134 |
} /* namespace json */ |
| 135 |
} /* namespace lsp */ |
| 136 |
|
| 137 |
#endif /* LSP_PLUG_IN_FMT_JSON_TOKENIZER_H_ */ |