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