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