2023-01-10 15:26:54 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* variant_parser.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2015-11-24 14:42:05 +01:00
|
|
|
#ifndef VARIANT_PARSER_H
|
|
|
|
#define VARIANT_PARSER_H
|
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/file_access.h"
|
|
|
|
#include "core/resource.h"
|
|
|
|
#include "core/variant.h"
|
2015-11-24 14:42:05 +01:00
|
|
|
|
|
|
|
class VariantParser {
|
|
|
|
public:
|
|
|
|
struct Stream {
|
2022-08-30 12:42:36 +02:00
|
|
|
private:
|
|
|
|
enum { READAHEAD_SIZE = 2048 };
|
|
|
|
CharType readahead_buffer[READAHEAD_SIZE];
|
|
|
|
uint32_t readahead_pointer = 0;
|
|
|
|
uint32_t readahead_filled = 0;
|
|
|
|
bool eof = false;
|
|
|
|
|
|
|
|
protected:
|
2022-12-12 16:44:39 +01:00
|
|
|
bool readahead_enabled = true;
|
2022-08-30 12:42:36 +02:00
|
|
|
virtual uint32_t _read_buffer(CharType *p_buffer, uint32_t p_num_chars) = 0;
|
2022-12-12 16:44:39 +01:00
|
|
|
virtual bool _is_eof() const = 0;
|
2015-11-24 14:42:05 +01:00
|
|
|
|
2022-08-30 12:42:36 +02:00
|
|
|
public:
|
2015-11-24 14:42:05 +01:00
|
|
|
CharType saved;
|
|
|
|
|
2022-08-30 12:42:36 +02:00
|
|
|
CharType get_char();
|
|
|
|
virtual bool is_utf8() const = 0;
|
2022-12-12 16:44:39 +01:00
|
|
|
bool is_eof() const;
|
2022-08-30 12:42:36 +02:00
|
|
|
|
2018-12-08 21:07:33 +01:00
|
|
|
Stream() :
|
|
|
|
saved(0) {}
|
2015-11-24 14:42:05 +01:00
|
|
|
virtual ~Stream() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct StreamFile : public Stream {
|
2022-08-30 12:42:36 +02:00
|
|
|
protected:
|
|
|
|
virtual uint32_t _read_buffer(CharType *p_buffer, uint32_t p_num_chars);
|
2022-12-12 16:44:39 +01:00
|
|
|
virtual bool _is_eof() const;
|
2022-08-30 12:42:36 +02:00
|
|
|
|
|
|
|
public:
|
2015-11-24 14:42:05 +01:00
|
|
|
FileAccess *f;
|
|
|
|
|
|
|
|
virtual bool is_utf8() const;
|
2022-12-12 16:44:39 +01:00
|
|
|
StreamFile(bool p_readahead_enabled = true) {
|
|
|
|
f = nullptr;
|
|
|
|
readahead_enabled = p_readahead_enabled;
|
|
|
|
}
|
2015-11-24 14:42:05 +01:00
|
|
|
};
|
|
|
|
|
2015-12-31 04:54:00 +01:00
|
|
|
struct StreamString : public Stream {
|
2022-08-30 12:42:36 +02:00
|
|
|
private:
|
2015-12-31 04:54:00 +01:00
|
|
|
int pos;
|
|
|
|
|
2022-08-30 12:42:36 +02:00
|
|
|
protected:
|
|
|
|
virtual uint32_t _read_buffer(CharType *p_buffer, uint32_t p_num_chars);
|
2022-12-12 16:44:39 +01:00
|
|
|
virtual bool _is_eof() const;
|
2015-12-31 04:54:00 +01:00
|
|
|
|
2022-08-30 12:42:36 +02:00
|
|
|
public:
|
|
|
|
String s;
|
|
|
|
|
|
|
|
virtual bool is_utf8() const;
|
2022-12-12 16:44:39 +01:00
|
|
|
StreamString(bool p_readahead_enabled = true) {
|
|
|
|
pos = 0;
|
|
|
|
readahead_enabled = p_readahead_enabled;
|
|
|
|
}
|
2015-12-31 04:54:00 +01:00
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
typedef Error (*ParseResourceFunc)(void *p_self, Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
|
2015-11-24 14:42:05 +01:00
|
|
|
|
|
|
|
struct ResourceParser {
|
2021-07-30 23:22:00 +02:00
|
|
|
void *userdata = nullptr;
|
|
|
|
ParseResourceFunc func = nullptr;
|
|
|
|
ParseResourceFunc ext_func = nullptr;
|
|
|
|
ParseResourceFunc sub_func = nullptr;
|
2015-11-24 14:42:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
enum TokenType {
|
|
|
|
TK_CURLY_BRACKET_OPEN,
|
|
|
|
TK_CURLY_BRACKET_CLOSE,
|
|
|
|
TK_BRACKET_OPEN,
|
|
|
|
TK_BRACKET_CLOSE,
|
|
|
|
TK_PARENTHESIS_OPEN,
|
|
|
|
TK_PARENTHESIS_CLOSE,
|
|
|
|
TK_IDENTIFIER,
|
|
|
|
TK_STRING,
|
|
|
|
TK_NUMBER,
|
2015-12-31 18:30:50 +01:00
|
|
|
TK_COLOR,
|
2015-11-24 14:42:05 +01:00
|
|
|
TK_COLON,
|
|
|
|
TK_COMMA,
|
2016-01-07 13:04:44 +01:00
|
|
|
TK_PERIOD,
|
2015-11-24 14:42:05 +01:00
|
|
|
TK_EQUAL,
|
|
|
|
TK_EOF,
|
|
|
|
TK_ERROR,
|
|
|
|
TK_MAX
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Expecting {
|
|
|
|
|
|
|
|
EXPECT_OBJECT,
|
|
|
|
EXPECT_OBJECT_KEY,
|
|
|
|
EXPECT_COLON,
|
|
|
|
EXPECT_OBJECT_VALUE,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Token {
|
|
|
|
TokenType type;
|
|
|
|
Variant value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Tag {
|
|
|
|
String name;
|
2017-03-05 16:44:50 +01:00
|
|
|
Map<String, Variant> fields;
|
2015-11-24 14:42:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2017-03-05 16:44:50 +01:00
|
|
|
static const char *tk_name[TK_MAX];
|
2015-11-24 14:42:05 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
template <class T>
|
|
|
|
static Error _parse_construct(Stream *p_stream, Vector<T> &r_construct, int &line, String &r_err_str);
|
|
|
|
static Error _parse_enginecfg(Stream *p_stream, Vector<String> &strings, int &line, String &r_err_str);
|
2021-05-04 16:00:45 +02:00
|
|
|
static Error _parse_dictionary(Dictionary &object, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser = nullptr);
|
|
|
|
static Error _parse_array(Array &array, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser = nullptr);
|
|
|
|
static Error _parse_tag(Token &token, Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, ResourceParser *p_res_parser = nullptr, bool p_simple_tag = false);
|
2015-11-24 14:42:05 +01:00
|
|
|
|
|
|
|
public:
|
2021-05-04 16:00:45 +02:00
|
|
|
static Error parse_tag(Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, ResourceParser *p_res_parser = nullptr, bool p_simple_tag = false);
|
|
|
|
static Error parse_tag_assign_eof(Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, String &r_assign, Variant &r_value, ResourceParser *p_res_parser = nullptr, bool p_simple_tag = false);
|
2015-11-24 14:42:05 +01:00
|
|
|
|
2021-05-04 16:00:45 +02:00
|
|
|
static Error parse_value(Token &token, Variant &value, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser = nullptr);
|
2017-03-05 16:44:50 +01:00
|
|
|
static Error get_token(Stream *p_stream, Token &r_token, int &line, String &r_err_str);
|
2021-05-04 16:00:45 +02:00
|
|
|
static Error parse(Stream *p_stream, Variant &r_ret, String &r_err_str, int &r_err_line, ResourceParser *p_res_parser = nullptr);
|
2015-11-24 14:42:05 +01:00
|
|
|
};
|
|
|
|
|
2015-12-31 04:31:00 +01:00
|
|
|
class VariantWriter {
|
|
|
|
public:
|
2017-03-05 16:44:50 +01:00
|
|
|
typedef Error (*StoreStringFunc)(void *ud, const String &p_string);
|
|
|
|
typedef String (*EncodeResourceFunc)(void *ud, const RES &p_resource);
|
2015-12-31 04:31:00 +01:00
|
|
|
|
2023-08-29 12:46:53 +02:00
|
|
|
static Error write(const Variant &p_variant, StoreStringFunc p_store_string_func, void *p_store_string_ud, EncodeResourceFunc p_encode_res_func, void *p_encode_res_ud, int p_recursion_count = 0);
|
2021-05-04 16:00:45 +02:00
|
|
|
static Error write_to_string(const Variant &p_variant, String &r_string, EncodeResourceFunc p_encode_res_func = nullptr, void *p_encode_res_ud = nullptr);
|
2015-12-31 04:31:00 +01:00
|
|
|
};
|
|
|
|
|
2015-11-24 14:42:05 +01:00
|
|
|
#endif // VARIANT_PARSER_H
|