variant_parser.h
1 #ifndef VARIANT_PARSER_H
2 #define VARIANT_PARSER_H
3 
4 #include "variant.h"
5 #include "os/file_access.h"
6 #include "resource.h"
7 
8 class VariantParser {
9 public:
10 
11  struct Stream {
12 
13  virtual CharType get_char()=0;
14  virtual bool is_utf8() const=0;
15  virtual bool is_eof() const=0;
16 
17  CharType saved;
18 
19  Stream() { saved=0; }
20  virtual ~Stream() {}
21  };
22 
23  struct StreamFile : public Stream {
24 
25  FileAccess *f;
26 
27  virtual CharType get_char();
28  virtual bool is_utf8() const;
29  virtual bool is_eof() const;
30 
31  StreamFile() { f=NULL; }
32 
33  };
34 
35  struct StreamString : public Stream {
36 
37  String s;
38  int pos;
39 
40  virtual CharType get_char();
41  virtual bool is_utf8() const;
42  virtual bool is_eof() const;
43 
44  StreamString() { pos=0; }
45 
46  };
47 
48  typedef Error (*ParseResourceFunc)(void* p_self, Stream* p_stream,Ref<Resource>& r_res,int &line,String &r_err_str);
49 
50  struct ResourceParser {
51 
52  void *userdata;
53  ParseResourceFunc func;
54  ParseResourceFunc ext_func;
55  ParseResourceFunc sub_func;
56 
57  };
58 
59  enum TokenType {
60  TK_CURLY_BRACKET_OPEN,
61  TK_CURLY_BRACKET_CLOSE,
62  TK_BRACKET_OPEN,
63  TK_BRACKET_CLOSE,
64  TK_PARENTHESIS_OPEN,
65  TK_PARENTHESIS_CLOSE,
66  TK_IDENTIFIER,
67  TK_STRING,
68  TK_NUMBER,
69  TK_COLOR,
70  TK_COLON,
71  TK_COMMA,
72  TK_PERIOD,
73  TK_EQUAL,
74  TK_EOF,
75  TK_ERROR,
76  TK_MAX
77  };
78 
79  enum Expecting {
80 
81  EXPECT_OBJECT,
82  EXPECT_OBJECT_KEY,
83  EXPECT_COLON,
84  EXPECT_OBJECT_VALUE,
85  };
86 
87  struct Token {
88 
89  TokenType type;
90  Variant value;
91  };
92 
93  struct Tag {
94 
95  String name;
96  Map<String,Variant> fields;
97  };
98 
99 private:
100  static const char * tk_name[TK_MAX];
101 
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 
109 public:
110 
111  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);
112  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);
113 
114  static Error parse_value(Token& token,Variant &value, Stream *p_stream, int &line, String &r_err_str,ResourceParser *p_res_parser=NULL);
115  static Error get_token(Stream *p_stream,Token& r_token,int &line,String &r_err_str);
116  static Error parse(Stream *p_stream, Variant &r_ret, String &r_err_str, int &r_err_line,ResourceParser *p_res_parser=NULL);
117 };
118 
119 
120 
121 
123 public:
124 
125  typedef Error (*StoreStringFunc)(void *ud,const String& p_string);
126  typedef String (*EncodeResourceFunc)(void *ud,const RES& p_resource);
127 
128  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);
129  static Error write_to_string(const Variant& p_variant, String& r_string, EncodeResourceFunc p_encode_res_func=NULL,void* p_encode_res_ud=NULL);
130 
131 
132 };
133 
134 
135 
136 
137 
138 
139 #endif // VARIANT_PARSER_H
Definition: array.h:38
Definition: variant.h:74
Definition: variant_parser.h:50
Definition: variant_parser.h:35
Definition: variant_parser.h:11
Definition: variant_parser.h:23
Definition: variant_parser.h:93
Definition: dictionary.h:42
Definition: variant_parser.h:122
Definition: variant_parser.h:8
Definition: ustring.h:64
Definition: vector.h:43
Definition: file_access.h:40
Definition: variant_parser.h:87