| 1 |
/* |
| 2 |
* Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2026 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 16 окт. 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_PARSER_H_ |
| 23 |
#define LSP_PLUG_IN_FMT_JSON_PARSER_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/common/types.h> |
| 27 |
#include <lsp-plug.in/io/IInStream.h> |
| 28 |
#include <lsp-plug.in/io/Path.h> |
| 29 |
#include <lsp-plug.in/fmt/json/token.h> |
| 30 |
#include <lsp-plug.in/fmt/json/Tokenizer.h> |
| 31 |
#include <lsp-plug.in/lltl/darray.h> |
| 32 |
|
| 33 |
namespace lsp |
| 34 |
{ |
| 35 |
namespace json |
| 36 |
{ |
| 37 |
|
| 38 |
class Parser |
| 39 |
{ |
| 40 |
protected: |
| 41 |
enum pmode_t |
| 42 |
{ |
| 43 |
READ_ROOT, |
| 44 |
READ_ARRAY, |
| 45 |
READ_OBJECT |
| 46 |
}; |
| 47 |
|
| 48 |
enum parse_flags_t |
| 49 |
{ |
| 50 |
PF_COMMA = 1 << 0, |
| 51 |
PF_COLON = 1 << 1, |
| 52 |
PF_PROPERTY = 1 << 2, |
| 53 |
PF_VALUE = 1 << 3, |
| 54 |
|
| 55 |
PF_ARRAY_ALL = PF_COMMA | PF_VALUE, |
| 56 |
PF_OBJECT_ALL = PF_COMMA | PF_COLON | PF_PROPERTY | PF_VALUE |
| 57 |
}; |
| 58 |
|
| 59 |
typedef struct state_t |
| 60 |
{ |
| 61 |
pmode_t mode; |
| 62 |
size_t flags; |
| 63 |
} state_t; |
| 64 |
|
| 65 |
protected: |
| 66 |
Tokenizer *pTokenizer; |
| 67 |
io::IInSequence *pSequence; |
| 68 |
size_t nWFlags; |
| 69 |
json_version_t enVersion; |
| 70 |
state_t sState; |
| 71 |
event_t sCurrent; |
| 72 |
lltl::darray<state_t> sStack; |
| 73 |
|
| 74 |
protected: |
| 75 |
status_t read_root(); |
| 76 |
status_t read_array(); |
| 77 |
status_t read_object(); |
| 78 |
status_t read_primitive(token_t tok); |
| 79 |
|
| 80 |
inline status_t push_state(pmode_t state); |
| 81 |
inline status_t pop_state(); |
| 82 |
|
| 83 |
inline token_t lookup_token(); |
| 84 |
|
| 85 |
public: |
| 86 |
explicit Parser(); |
| 87 |
Parser(const Parser &) = delete; |
| 88 |
Parser(Parser &&) = delete; |
| 89 |
virtual ~Parser(); |
| 90 |
|
| 91 |
Parser & operator = (const Parser &) = delete; |
| 92 |
Parser & operator = (Parser &&) = delete; |
| 93 |
|
| 94 |
public: |
| 95 |
/** |
| 96 |
* Open parser |
| 97 |
* @param path UTF-8 path to the file |
| 98 |
* @param version JSON version |
| 99 |
* @param charset character set |
| 100 |
* @return status of operation |
| 101 |
*/ |
| 102 |
status_t open(const char *path, json_version_t version, const char *charset = NULL); |
| 103 |
|
| 104 |
/** |
| 105 |
* Open parser |
| 106 |
* @param path string representation of path to the file |
| 107 |
* @param version JSON version |
| 108 |
* @param charset character set |
| 109 |
* @return status of operation |
| 110 |
*/ |
| 111 |
status_t open(const LSPString *path, json_version_t version, const char *charset = NULL); |
| 112 |
|
| 113 |
/** |
| 114 |
* Open parser |
| 115 |
* @param path path to the file |
| 116 |
* @param version JSON version |
| 117 |
* @param charset character set |
| 118 |
* @return status of operation |
| 119 |
*/ |
| 120 |
status_t open(const io::Path *path, json_version_t version, const char *charset = NULL); |
| 121 |
|
| 122 |
/** |
| 123 |
* Wrap string with parser |
| 124 |
* @param str string to wrap |
| 125 |
* @param version JSON version |
| 126 |
* @param charset character set |
| 127 |
* @return status of operation |
| 128 |
*/ |
| 129 |
status_t wrap(const char *str, json_version_t version, const char *charset = NULL); |
| 130 |
|
| 131 |
/** |
| 132 |
* Wrap string with parser |
| 133 |
* @param str string to wrap |
| 134 |
* @param version JSON version |
| 135 |
* @return status of operation |
| 136 |
*/ |
| 137 |
status_t wrap(const LSPString *str, json_version_t version); |
| 138 |
|
| 139 |
/** |
| 140 |
* Wrap input sequence with parser |
| 141 |
* @param seq sequence to use for reads |
| 142 |
* @param version JSON version |
| 143 |
* @return status of operation |
| 144 |
*/ |
| 145 |
status_t wrap(io::IInSequence *seq, json_version_t version, size_t flags = WRAP_NONE); |
| 146 |
|
| 147 |
/** |
| 148 |
* Wrap input stream with parser |
| 149 |
* @param is input stream |
| 150 |
* @param version JSON version |
| 151 |
* @param flags wrap flags |
| 152 |
* @param charset character set |
| 153 |
* @return status of operation |
| 154 |
*/ |
| 155 |
status_t wrap(io::IInStream *is, json_version_t version, size_t flags = WRAP_NONE, const char *charset = NULL); |
| 156 |
|
| 157 |
/** |
| 158 |
* Close parser |
| 159 |
* @return status of operation |
| 160 |
*/ |
| 161 |
status_t close(); |
| 162 |
|
| 163 |
public: |
| 164 |
/** |
| 165 |
* Read next event |
| 166 |
* @param ev event to read |
| 167 |
* @return status of operation |
| 168 |
*/ |
| 169 |
status_t read_next(event_t *ev); |
| 170 |
|
| 171 |
/** |
| 172 |
* Read next event and return it's type. The event |
| 173 |
* will be accessible by get_current() call |
| 174 |
* |
| 175 |
* @param type event type |
| 176 |
* @return status of operation |
| 177 |
*/ |
| 178 |
status_t read_next_type(event_type_t *type); |
| 179 |
|
| 180 |
/** |
| 181 |
* Read string value |
| 182 |
* @param dst pointer to string to store the value |
| 183 |
* @return status of operation, STATUS_NULL if value is null |
| 184 |
*/ |
| 185 |
status_t read_string(LSPString *dst); |
| 186 |
|
| 187 |
/** |
| 188 |
* Read double value |
| 189 |
* @param dst pointer to store the value |
| 190 |
* @return status of operation, STATUS_NULL if value is null |
| 191 |
*/ |
| 192 |
status_t read_double(double *dst); |
| 193 |
|
| 194 |
/** |
| 195 |
* Read signed integer value |
| 196 |
* @param dst pointer to store the value |
| 197 |
* @return status of operation, STATUS_NULL if value is null |
| 198 |
*/ |
| 199 |
status_t read_int(ssize_t *dst); |
| 200 |
|
| 201 |
/** |
| 202 |
* Read unsigned integer value |
| 203 |
* @param dst pointer to store the value |
| 204 |
* @return status of operation, STATUS_NULL if value is null |
| 205 |
*/ |
| 206 |
status_t read_int(size_t *dst); |
| 207 |
|
| 208 |
/** |
| 209 |
* Read boolean value |
| 210 |
* @param dst pointer to store the value |
| 211 |
* @return status of operation, STATUS_NULL if value is null |
| 212 |
*/ |
| 213 |
status_t read_bool(bool *dst); |
| 214 |
|
| 215 |
/** |
| 216 |
* Get current event |
| 217 |
* @param ev pointer to save the event |
| 218 |
* @return status of operation |
| 219 |
*/ |
| 220 |
status_t get_current(event_t *ev); |
| 221 |
|
| 222 |
/** |
| 223 |
* Get current string value |
| 224 |
* @param dst pointer to string to store the value |
| 225 |
* @return status of operation, STATUS_NULL if value is null |
| 226 |
*/ |
| 227 |
status_t get_string(LSPString *dst); |
| 228 |
|
| 229 |
/** |
| 230 |
* Get current double value |
| 231 |
* @param dst pointer to store the value |
| 232 |
* @return status of operation, STATUS_NULL if value is null |
| 233 |
*/ |
| 234 |
status_t get_double(double *dst); |
| 235 |
|
| 236 |
/** |
| 237 |
* Get current integer value |
| 238 |
* @param dst pointer to store the value |
| 239 |
* @return status of operation, STATUS_NULL if value is null |
| 240 |
*/ |
| 241 |
status_t get_int(ssize_t *dst); |
| 242 |
|
| 243 |
/** |
| 244 |
* Get current boolean value |
| 245 |
* @param dst pointer to store the value |
| 246 |
* @return status of operation, STATUS_NULL if value is null |
| 247 |
*/ |
| 248 |
status_t get_bool(bool *dst); |
| 249 |
|
| 250 |
/** |
| 251 |
* Skip nested data structures after current event |
| 252 |
* @return status of operation |
| 253 |
*/ |
| 254 |
status_t skip_next(); |
| 255 |
|
| 256 |
/** |
| 257 |
* Skip nested data structures associated with current event. |
| 258 |
* If current event is JE_OBJECT_START |
| 259 |
* @return status of operation |
| 260 |
*/ |
| 261 |
status_t skip_current(); |
| 262 |
}; |
| 263 |
|
| 264 |
} /* namespace json */ |
| 265 |
} /* namespace lsp */ |
| 266 |
|
| 267 |
#endif /* LSP_PLUG_IN_FMT_JSON_PARSER_H_ */ |