variant_parser.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*************************************************************************/
  2. /* variant_parser.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/os/file_access.h"
  33. #include "core/resource.h"
  34. #include "core/variant.h"
  35. class VariantParser {
  36. public:
  37. struct Stream {
  38. virtual CharType get_char() = 0;
  39. virtual bool is_utf8() const = 0;
  40. virtual bool is_eof() const = 0;
  41. CharType saved;
  42. Stream() :
  43. saved(0) {}
  44. virtual ~Stream() {}
  45. };
  46. struct StreamFile : public Stream {
  47. FileAccess *f;
  48. virtual CharType get_char();
  49. virtual bool is_utf8() const;
  50. virtual bool is_eof() const;
  51. StreamFile() { f = NULL; }
  52. };
  53. struct StreamString : public Stream {
  54. String s;
  55. int pos;
  56. virtual CharType get_char();
  57. virtual bool is_utf8() const;
  58. virtual bool is_eof() const;
  59. StreamString() { pos = 0; }
  60. };
  61. typedef Error (*ParseResourceFunc)(void *p_self, Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
  62. struct ResourceParser {
  63. void *userdata;
  64. ParseResourceFunc func;
  65. ParseResourceFunc ext_func;
  66. ParseResourceFunc sub_func;
  67. };
  68. enum TokenType {
  69. TK_CURLY_BRACKET_OPEN,
  70. TK_CURLY_BRACKET_CLOSE,
  71. TK_BRACKET_OPEN,
  72. TK_BRACKET_CLOSE,
  73. TK_PARENTHESIS_OPEN,
  74. TK_PARENTHESIS_CLOSE,
  75. TK_IDENTIFIER,
  76. TK_STRING,
  77. TK_NUMBER,
  78. TK_COLOR,
  79. TK_COLON,
  80. TK_COMMA,
  81. TK_PERIOD,
  82. TK_EQUAL,
  83. TK_EOF,
  84. TK_ERROR,
  85. TK_MAX
  86. };
  87. enum Expecting {
  88. EXPECT_OBJECT,
  89. EXPECT_OBJECT_KEY,
  90. EXPECT_COLON,
  91. EXPECT_OBJECT_VALUE,
  92. };
  93. struct Token {
  94. TokenType type;
  95. Variant value;
  96. };
  97. struct Tag {
  98. String name;
  99. Map<String, Variant> fields;
  100. };
  101. private:
  102. static const char *tk_name[TK_MAX];
  103. template <class T>
  104. static Error _parse_construct(Stream *p_stream, Vector<T> &r_construct, int &line, String &r_err_str);
  105. static Error _parse_enginecfg(Stream *p_stream, Vector<String> &strings, int &line, String &r_err_str);
  106. static Error _parse_dictionary(Dictionary &object, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser = NULL);
  107. static Error _parse_array(Array &array, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser = NULL);
  108. static Error _parse_tag(Token &token, Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, ResourceParser *p_res_parser = NULL, bool p_simple_tag = false);
  109. public:
  110. static Error parse_tag(Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, ResourceParser *p_res_parser = NULL, bool p_simple_tag = false);
  111. 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 = NULL, bool p_simple_tag = false);
  112. static Error parse_value(Token &token, Variant &value, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser = NULL);
  113. static Error get_token(Stream *p_stream, Token &r_token, int &line, String &r_err_str);
  114. static Error parse(Stream *p_stream, Variant &r_ret, String &r_err_str, int &r_err_line, ResourceParser *p_res_parser = NULL);
  115. };
  116. class VariantWriter {
  117. public:
  118. typedef Error (*StoreStringFunc)(void *ud, const String &p_string);
  119. typedef String (*EncodeResourceFunc)(void *ud, const RES &p_resource);
  120. 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);
  121. static Error write_to_string(const Variant &p_variant, String &r_string, EncodeResourceFunc p_encode_res_func = NULL, void *p_encode_res_ud = NULL);
  122. };
  123. #endif // VARIANT_PARSER_H