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