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#ifndef VARIANT_PARSER_H
32#define VARIANT_PARSER_H
33
34#include "core/io/file_access.h"
35#include "core/io/resource.h"
36#include "core/variant/variant.h"
37
38class VariantParser {
39public:
40 struct Stream {
41 private:
42 enum { READAHEAD_SIZE = 2048 };
43 char32_t readahead_buffer[READAHEAD_SIZE];
44 uint32_t readahead_pointer = 0;
45 uint32_t readahead_filled = 0;
46 bool eof = false;
47
48 protected:
49 bool readahead_enabled = true;
50 virtual uint32_t _read_buffer(char32_t *p_buffer, uint32_t p_num_chars) = 0;
51 virtual bool _is_eof() const = 0;
52
53 public:
54 char32_t saved = 0;
55
56 char32_t get_char();
57 virtual bool is_utf8() const = 0;
58 bool is_eof() const;
59
60 Stream() {}
61 virtual ~Stream() {}
62 };
63
64 struct StreamFile : public Stream {
65 protected:
66 virtual uint32_t _read_buffer(char32_t *p_buffer, uint32_t p_num_chars) override;
67 virtual bool _is_eof() const override;
68
69 public:
70 Ref<FileAccess> f;
71
72 virtual bool is_utf8() const override;
73
74 StreamFile(bool p_readahead_enabled = true) { readahead_enabled = p_readahead_enabled; }
75 };
76
77 struct StreamString : public Stream {
78 String s;
79
80 private:
81 int pos = 0;
82
83 protected:
84 virtual uint32_t _read_buffer(char32_t *p_buffer, uint32_t p_num_chars) override;
85 virtual bool _is_eof() const override;
86
87 public:
88 virtual bool is_utf8() const override;
89 StreamString(bool p_readahead_enabled = true) { readahead_enabled = p_readahead_enabled; }
90 };
91
92 typedef Error (*ParseResourceFunc)(void *p_self, Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
93
94 struct ResourceParser {
95 void *userdata = nullptr;
96 ParseResourceFunc func = nullptr;
97 ParseResourceFunc ext_func = nullptr;
98 ParseResourceFunc sub_func = nullptr;
99 };
100
101 enum TokenType {
102 TK_CURLY_BRACKET_OPEN,
103 TK_CURLY_BRACKET_CLOSE,
104 TK_BRACKET_OPEN,
105 TK_BRACKET_CLOSE,
106 TK_PARENTHESIS_OPEN,
107 TK_PARENTHESIS_CLOSE,
108 TK_IDENTIFIER,
109 TK_STRING,
110 TK_STRING_NAME,
111 TK_NUMBER,
112 TK_COLOR,
113 TK_COLON,
114 TK_COMMA,
115 TK_PERIOD,
116 TK_EQUAL,
117 TK_EOF,
118 TK_ERROR,
119 TK_MAX
120 };
121
122 enum Expecting {
123 EXPECT_OBJECT,
124 EXPECT_OBJECT_KEY,
125 EXPECT_COLON,
126 EXPECT_OBJECT_VALUE,
127 };
128
129 struct Token {
130 TokenType type;
131 Variant value;
132 };
133
134 struct Tag {
135 String name;
136 HashMap<String, Variant> fields;
137 };
138
139private:
140 static const char *tk_name[TK_MAX];
141
142 template <class T>
143 static Error _parse_construct(Stream *p_stream, Vector<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
149public:
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
158class VariantWriter {
159public:
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);
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);
165};
166
167#endif // VARIANT_PARSER_H
168