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