variant_parser.h 6.4 KB

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