Debian Codegraph @

core/variant/variant_parser.h

raw symbols SCIP
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 virtual ~Stream() {}
60 };
61
62 struct StreamFile : public Stream {
63 protected:
64 virtual uint32_t _read_buffer(char32_t *p_buffer, uint32_t p_num_chars) override;
65 virtual bool _is_eof() const override;
66
67 public:
68 Ref<FileAccess> f;
69
70 virtual bool is_utf8() const override;
71
72 StreamFile(bool p_readahead_enabled = true) { readahead_enabled = p_readahead_enabled; }
73 };
74
75 struct StreamString : public Stream {
76 String s;
77
78 private:
79 int pos = 0;
80
81 protected:
82 virtual uint32_t _read_buffer(char32_t *p_buffer, uint32_t p_num_chars) override;
83 virtual bool _is_eof() const override;
84
85 public:
86 virtual bool is_utf8() const override;
87 StreamString(bool p_readahead_enabled = true) { readahead_enabled = p_readahead_enabled; }
88 };
89
90 typedef Error (*ParseResourceFunc)(void *p_self, Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
91
92 struct ResourceParser {
93 void *userdata = nullptr;
94 ParseResourceFunc func = nullptr;
95 ParseResourceFunc ext_func = nullptr;
96 ParseResourceFunc sub_func = nullptr;
97 };
98
99 enum TokenType {
100 TK_CURLY_BRACKET_OPEN,
101 TK_CURLY_BRACKET_CLOSE,
102 TK_BRACKET_OPEN,
103 TK_BRACKET_CLOSE,
104 TK_PARENTHESIS_OPEN,
105 TK_PARENTHESIS_CLOSE,
106 TK_IDENTIFIER,
107 TK_STRING,
108 TK_STRING_NAME,
109 TK_NUMBER,
110 TK_COLOR,
111 TK_COLON,
112 TK_COMMA,
113 TK_PERIOD,
114 TK_EQUAL,
115 TK_EOF,
116 TK_ERROR,
117 TK_MAX
118 };
119
120 enum Expecting {
121 EXPECT_OBJECT,
122 EXPECT_OBJECT_KEY,
123 EXPECT_COLON,
124 EXPECT_OBJECT_VALUE,
125 };
126
127 struct Token {
128 TokenType type;
129 Variant value;
130 };
131
132 struct Tag {
133 String name;
134 HashMap<String, Variant> fields;
135 };
136
137 private:
138 static const char *tk_name[TK_MAX];
139
140 template <typename T>
141 static Error _parse_construct(Stream *p_stream, Vector<T> &r_construct, int &line, String &r_err_str);
142 static Error _parse_byte_array(Stream *p_stream, Vector<uint8_t> &r_construct, int &line, String &r_err_str);
143 static Error _parse_enginecfg(Stream *p_stream, Vector<String> &strings, int &line, String &r_err_str);
144 static Error _parse_dictionary(Dictionary &object, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser = nullptr);
145 static Error _parse_array(Array &array, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser = nullptr);
146 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);
147
148 public:
149 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);
150 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);
151
152 static Error parse_value(Token &token, Variant &value, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser = nullptr);
153 static Error get_token(Stream *p_stream, Token &r_token, int &line, String &r_err_str);
154 static Error parse(Stream *p_stream, Variant &r_ret, String &r_err_str, int &r_err_line, ResourceParser *p_res_parser = nullptr);
155 };
156
157 class VariantWriter {
158 public:
159 typedef Error (*StoreStringFunc)(void *ud, const String &p_string);
160 typedef String (*EncodeResourceFunc)(void *ud, const Ref<Resource> &p_resource);
161
162 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);
163 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);
164 };