variant_parser.h 6.3 KB

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