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