Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/variant/variant_parser.h
9903 views
1
/**************************************************************************/
2
/* variant_parser.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/io/file_access.h"
34
#include "core/io/resource.h"
35
#include "core/variant/variant.h"
36
37
class VariantParser {
38
public:
39
struct Stream {
40
private:
41
enum { READAHEAD_SIZE = 2048 };
42
char32_t readahead_buffer[READAHEAD_SIZE];
43
uint32_t readahead_pointer = 0;
44
uint32_t readahead_filled = 0;
45
bool eof = false;
46
47
protected:
48
bool readahead_enabled = true;
49
virtual uint32_t _read_buffer(char32_t *p_buffer, uint32_t p_num_chars) = 0;
50
virtual bool _is_eof() const = 0;
51
52
public:
53
char32_t saved = 0;
54
55
char32_t get_char();
56
virtual bool is_utf8() const = 0;
57
bool is_eof() const;
58
59
Stream() {}
60
virtual ~Stream() {}
61
};
62
63
struct StreamFile : public Stream {
64
protected:
65
virtual uint32_t _read_buffer(char32_t *p_buffer, uint32_t p_num_chars) override;
66
virtual bool _is_eof() const override;
67
68
public:
69
Ref<FileAccess> f;
70
71
virtual bool is_utf8() const override;
72
73
StreamFile(bool p_readahead_enabled = true) { readahead_enabled = p_readahead_enabled; }
74
};
75
76
struct StreamString : public Stream {
77
String s;
78
79
private:
80
int pos = 0;
81
82
protected:
83
virtual uint32_t _read_buffer(char32_t *p_buffer, uint32_t p_num_chars) override;
84
virtual bool _is_eof() const override;
85
86
public:
87
virtual bool is_utf8() const override;
88
StreamString(bool p_readahead_enabled = true) { readahead_enabled = p_readahead_enabled; }
89
};
90
91
typedef Error (*ParseResourceFunc)(void *p_self, Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
92
93
struct ResourceParser {
94
void *userdata = nullptr;
95
ParseResourceFunc func = nullptr;
96
ParseResourceFunc ext_func = nullptr;
97
ParseResourceFunc sub_func = nullptr;
98
};
99
100
enum TokenType {
101
TK_CURLY_BRACKET_OPEN,
102
TK_CURLY_BRACKET_CLOSE,
103
TK_BRACKET_OPEN,
104
TK_BRACKET_CLOSE,
105
TK_PARENTHESIS_OPEN,
106
TK_PARENTHESIS_CLOSE,
107
TK_IDENTIFIER,
108
TK_STRING,
109
TK_STRING_NAME,
110
TK_NUMBER,
111
TK_COLOR,
112
TK_COLON,
113
TK_COMMA,
114
TK_PERIOD,
115
TK_EQUAL,
116
TK_EOF,
117
TK_ERROR,
118
TK_MAX
119
};
120
121
enum Expecting {
122
EXPECT_OBJECT,
123
EXPECT_OBJECT_KEY,
124
EXPECT_COLON,
125
EXPECT_OBJECT_VALUE,
126
};
127
128
struct Token {
129
TokenType type;
130
Variant value;
131
};
132
133
struct Tag {
134
String name;
135
HashMap<String, Variant> fields;
136
};
137
138
private:
139
static const char *tk_name[TK_MAX];
140
141
template <typename T>
142
static Error _parse_construct(Stream *p_stream, Vector<T> &r_construct, int &line, String &r_err_str);
143
static Error _parse_byte_array(Stream *p_stream, Vector<uint8_t> &r_construct, int &line, String &r_err_str);
144
static Error _parse_enginecfg(Stream *p_stream, Vector<String> &strings, int &line, String &r_err_str);
145
static Error _parse_dictionary(Dictionary &object, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser = nullptr);
146
static Error _parse_array(Array &array, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser = nullptr);
147
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);
148
149
public:
150
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);
151
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);
152
153
static Error parse_value(Token &token, Variant &value, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser = nullptr);
154
static Error get_token(Stream *p_stream, Token &r_token, int &line, String &r_err_str);
155
static Error parse(Stream *p_stream, Variant &r_ret, String &r_err_str, int &r_err_line, ResourceParser *p_res_parser = nullptr);
156
};
157
158
class VariantWriter {
159
public:
160
typedef Error (*StoreStringFunc)(void *ud, const String &p_string);
161
typedef String (*EncodeResourceFunc)(void *ud, const Ref<Resource> &p_resource);
162
163
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, bool p_compat = true);
164
static Error write_to_string(const Variant &p_variant, String &r_string, EncodeResourceFunc p_encode_res_func = nullptr, void *p_encode_res_ud = nullptr, bool p_compat = true);
165
};
166
167