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