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