variant_parser.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*************************************************************************/
  2. /* variant_parser.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. virtual uint32_t _read_buffer(char32_t *p_buffer, uint32_t p_num_chars) = 0;
  46. public:
  47. char32_t saved = 0;
  48. char32_t get_char();
  49. virtual bool is_utf8() const = 0;
  50. bool is_eof() const { return eof; }
  51. Stream() {}
  52. virtual ~Stream() {}
  53. };
  54. struct StreamFile : public Stream {
  55. protected:
  56. virtual uint32_t _read_buffer(char32_t *p_buffer, uint32_t p_num_chars) override;
  57. public:
  58. Ref<FileAccess> f;
  59. virtual bool is_utf8() const override;
  60. StreamFile() {}
  61. };
  62. struct StreamString : public Stream {
  63. String s;
  64. private:
  65. int pos = 0;
  66. protected:
  67. virtual uint32_t _read_buffer(char32_t *p_buffer, uint32_t p_num_chars) override;
  68. public:
  69. virtual bool is_utf8() const override;
  70. StreamString() {}
  71. };
  72. typedef Error (*ParseResourceFunc)(void *p_self, Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
  73. struct ResourceParser {
  74. void *userdata = nullptr;
  75. ParseResourceFunc func = nullptr;
  76. ParseResourceFunc ext_func = nullptr;
  77. ParseResourceFunc sub_func = nullptr;
  78. };
  79. enum TokenType {
  80. TK_CURLY_BRACKET_OPEN,
  81. TK_CURLY_BRACKET_CLOSE,
  82. TK_BRACKET_OPEN,
  83. TK_BRACKET_CLOSE,
  84. TK_PARENTHESIS_OPEN,
  85. TK_PARENTHESIS_CLOSE,
  86. TK_IDENTIFIER,
  87. TK_STRING,
  88. TK_STRING_NAME,
  89. TK_NUMBER,
  90. TK_COLOR,
  91. TK_COLON,
  92. TK_COMMA,
  93. TK_PERIOD,
  94. TK_EQUAL,
  95. TK_EOF,
  96. TK_ERROR,
  97. TK_MAX
  98. };
  99. enum Expecting {
  100. EXPECT_OBJECT,
  101. EXPECT_OBJECT_KEY,
  102. EXPECT_COLON,
  103. EXPECT_OBJECT_VALUE,
  104. };
  105. struct Token {
  106. TokenType type;
  107. Variant value;
  108. };
  109. struct Tag {
  110. String name;
  111. HashMap<String, Variant> fields;
  112. };
  113. private:
  114. static const char *tk_name[TK_MAX];
  115. template <class T>
  116. static Error _parse_construct(Stream *p_stream, Vector<T> &r_construct, int &line, String &r_err_str);
  117. static Error _parse_enginecfg(Stream *p_stream, Vector<String> &strings, int &line, String &r_err_str);
  118. static Error _parse_dictionary(Dictionary &object, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser = nullptr);
  119. static Error _parse_array(Array &array, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser = nullptr);
  120. 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);
  121. public:
  122. 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);
  123. 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);
  124. static Error parse_value(Token &token, Variant &value, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser = nullptr);
  125. static Error get_token(Stream *p_stream, Token &r_token, int &line, String &r_err_str);
  126. static Error parse(Stream *p_stream, Variant &r_ret, String &r_err_str, int &r_err_line, ResourceParser *p_res_parser = nullptr);
  127. };
  128. class VariantWriter {
  129. public:
  130. typedef Error (*StoreStringFunc)(void *ud, const String &p_string);
  131. typedef String (*EncodeResourceFunc)(void *ud, const Ref<Resource> &p_resource);
  132. 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 recursion_count = 0);
  133. static Error write_to_string(const Variant &p_variant, String &r_string, EncodeResourceFunc p_encode_res_func = nullptr, void *p_encode_res_ud = nullptr);
  134. };
  135. #endif // VARIANT_PARSER_H