]> git.localhorst.tv Git - l2e.git/blob - src/loader/ParsedSource.h
e10b287aa661fbd9b5b9cdfac71c593d0714dc47
[l2e.git] / src / loader / ParsedSource.h
1 /*
2  * ParsedSource.h
3  *
4  *  Created on: Aug 26, 2012
5  *      Author: holy
6  */
7
8 #ifndef LOADER_PARSEDSOURCE_H_
9 #define LOADER_PARSEDSOURCE_H_
10
11 #include <iosfwd>
12 #include <map>
13 #include <set>
14 #include <string>
15 #include <vector>
16
17 namespace loader {
18
19 class PropertyList;
20 class Value;
21
22 class Literal {
23
24 public:
25         enum Type {
26                 ARRAY_VALUES,
27                 ARRAY_PROPS,
28                 BOOLEAN,
29                 COLOR,
30                 NUMBER,
31                 PATH,
32                 STRING,
33                 VECTOR,
34                 OBJECT
35         };
36
37 public:
38         explicit Literal(const std::vector<Value *> &);
39         explicit Literal(const std::vector<PropertyList *> &);
40         explicit Literal(bool);
41         Literal(int r, int g, int b, int a = 255);
42         explicit Literal(int number);
43         Literal(const std::string &dir, const std::string &path);
44         Literal(const std::string &);
45         Literal(int x, int y);
46         Literal(const std::string &typeName, PropertyList *properties);
47         ~Literal();
48 private:
49         Literal(const Literal &);
50         Literal &operator =(const Literal &);
51
52 public:
53         Type GetType() const { return type; }
54         bool IsArray() const { return GetType() == ARRAY_VALUES || GetType() == ARRAY_PROPS; }
55         bool IsObject() const { return GetType() == OBJECT; }
56         int ArraySize() const { return GetType() == ARRAY_VALUES ? GetValues().size() : GetPropertyLists().size(); }
57
58         const std::vector<Value *> &GetValues() const;
59         const std::vector<PropertyList *> &GetPropertyLists() const;
60         bool GetBoolean() const;
61         int GetRed() const;
62         int GetGreen() const;
63         int GetBlue() const;
64         int GetAlpha() const;
65         int GetNumber() const;
66         const std::string &GetString() const;
67         int GetX() const;
68         int GetY() const;
69         const std::string &GetTypeName() const;
70         const PropertyList *GetProperties() const;
71
72 private:
73         PropertyList *props;
74         std::string str;
75         std::vector<Value *> values;
76         std::vector<PropertyList *> propertyLists;
77         int i1, i2, i3, i4;
78         bool b;
79         Type type;
80
81 };
82
83
84 class Value {
85
86 public:
87         explicit Value(const std::string &identifier)
88         : literal(0), identifier(identifier), isLiteral(false) { }
89         explicit Value(Literal *literal)
90         : literal(literal), isLiteral(true) { }
91         ~Value();
92 private:
93         Value(const Value &);
94         Value &operator =(const Value &);
95
96 public:
97         bool IsLiteral() const { return isLiteral; }
98         const Literal &GetLiteral() const;
99         const std::string &GetIdentifier() const;
100
101 private:
102         Literal *literal;
103         std::string identifier;
104         bool isLiteral;
105
106 };
107
108
109 class PropertyList {
110
111 public:
112         PropertyList() { }
113         ~PropertyList();
114 private:
115         PropertyList(const PropertyList &);
116         PropertyList &operator =(const PropertyList &);
117
118 public:
119         void SetProperty(const std::string &name, Value *value) {
120                 props[name] = value;
121         }
122
123         typedef std::map<std::string, Value *>::iterator Iterator;
124         typedef std::map<std::string, Value *>::const_iterator ConstIterator;
125         Iterator Begin() { return props.begin(); }
126         ConstIterator Begin() const { return props.begin(); }
127         Iterator End() { return props.end(); }
128         ConstIterator End() const { return props.end(); }
129
130 private:
131         std::map<std::string, Value *> props;
132
133 };
134
135
136 class Declaration {
137
138 public:
139         Declaration(const std::string &typeName, const std::string &identifier)
140         : typeName(typeName), identifier(identifier) { }
141         virtual ~Declaration() { }
142 private:
143         Declaration(const Declaration &);
144         Declaration &operator =(const Declaration &);
145
146 public:
147         const std::string &TypeName() const { return typeName; }
148         const std::string &Identifier() const { return identifier; }
149
150 private:
151         std::string typeName;
152         std::string identifier;
153
154 };
155
156
157 class Definition
158 : public Declaration {
159
160 public:
161         Definition(const std::string &typeName, const std::string &identifier)
162         : Declaration(typeName, identifier), value(0), isLiteral(false) { }
163         virtual ~Definition();
164 private:
165         Definition(const Definition &);
166         Definition &operator =(const Definition &);
167
168 public:
169         void SetValue(Literal *);
170         void SetValue(PropertyList *);
171
172         bool HasLiteralValue() const { return isLiteral && value; }
173         bool HasProperties() const { return !isLiteral && value; }
174         Literal *GetLiteral();
175         const Literal *GetLiteral() const;
176         PropertyList *GetProperties();
177         const PropertyList *GetProperties() const;
178
179 private:
180         void *value;
181         bool isLiteral;
182
183 };
184
185
186 class ParsedSource {
187
188 public:
189         ParsedSource() { }
190         ~ParsedSource();
191 private:
192         ParsedSource(const ParsedSource &);
193         ParsedSource &operator =(const ParsedSource &);
194
195 public:
196         void AddDeclaration(Declaration *);
197         void AddDefinition(Definition *);
198         void ExportDeclaration(Declaration *);
199         void ExportIdentifier(const std::string &);
200
201         bool IsDeclared(const std::string &) const;
202         Declaration &GetDeclaration(const std::string &);
203         const Declaration &GetDeclaration(const std::string &) const;
204         bool IsDefined(const std::string &) const;
205         Definition &GetDefinition(const std::string &);
206         const Definition &GetDefinition(const std::string &) const;
207
208         const std::map<std::string, Declaration *> &Declarations() const { return declarations; }
209         const std::map<std::string, Definition *> &Definitions() const { return definitions; }
210         const std::set<std::string> &Exports() const { return exports; }
211
212 public:
213         void WriteHeader(std::ostream &) const;
214
215 private:
216         std::map<std::string, Declaration *> declarations;
217         std::map<std::string, Definition *> definitions;
218         std::set<std::string> exports;
219
220 };
221
222 }
223
224
225 namespace std {
226
227 ostream &operator <<(ostream &, const loader::ParsedSource &);
228 ostream &operator <<(ostream &, const loader::Literal &);
229
230 }
231
232 #endif /* LOADER_PARSEDSOURCE_H_ */