variant_parser.h 5.5 KB

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