]> git.localhorst.tv Git - l2e.git/blob - src/loader/ParsedSource.h
added support for arrays of property lists
[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         enum Type {
25                 ARRAY_VALUES,
26                 ARRAY_PROPS,
27                 BOOLEAN,
28                 COLOR,
29                 NUMBER,
30                 STRING,
31                 VECTOR,
32                 OBJECT
33         };
34
35 public:
36         explicit Literal(const std::vector<Value *> &);
37         explicit Literal(const std::vector<PropertyList *> &);
38         explicit Literal(bool);
39         Literal(int r, int g, int b, int a = 255);
40         Literal(const std::string &);
41         Literal(int x, int y);
42         Literal(const std::string &typeName, PropertyList *properties);
43
44 private:
45         PropertyList *props;
46         std::string str;
47         std::vector<Value *> values;
48         std::vector<PropertyList *> propertyLists;
49         int i1, i2, i3, i4;
50         bool b;
51         Type type;
52
53 };
54
55
56 class Value {
57
58 public:
59         explicit Value(const std::string &identifier)
60         : literal(0), identifier(identifier), isLiteral(false) { }
61         explicit Value(Literal *literal)
62         : literal(literal), isLiteral(false) { }
63
64 private:
65         Literal *literal;
66         std::string identifier;
67         bool isLiteral;
68
69 };
70
71
72 class PropertyList {
73
74 public:
75         ~PropertyList();
76
77 public:
78         void SetProperty(const std::string &name, Value *value) {
79                 props[name] = value;
80         }
81
82 private:
83         std::map<std::string, Value *> props;
84
85 };
86
87
88 class Declaration {
89
90 public:
91         Declaration(const std::string &typeName, const std::string &identifier)
92         : typeName(typeName), identifier(identifier) { }
93         virtual ~Declaration() { }
94
95 public:
96         const std::string &TypeName() const { return typeName; }
97         const std::string &Identifier() const { return identifier; }
98
99 private:
100         std::string typeName;
101         std::string identifier;
102
103 };
104
105
106 class Definition
107 : public Declaration {
108
109 public:
110         Definition(const std::string &typeName, const std::string &identifier)
111         : Declaration(typeName, identifier), value(0), isLiteral(false) { }
112
113 public:
114         void SetValue(Literal *);
115         void SetValue(PropertyList *);
116
117         bool HasLiteralValue() const { return isLiteral && value; }
118         bool HasProperties() const { return !isLiteral && value; }
119         Literal *GetLiteral();
120         const Literal *GetLiteral() const;
121         PropertyList *GetProperties();
122         const PropertyList *GetProperties() const;
123
124 private:
125         void *value;
126         bool isLiteral;
127
128 };
129
130
131 class ParsedSource {
132
133 public:
134         ParsedSource() { }
135         ~ParsedSource() { }
136
137 public:
138         void AddDeclaration(Declaration *);
139         void ExportDeclaration(Declaration *);
140         void ExportIdentifier(const std::string &);
141
142         const std::map<std::string, Declaration *> Declarations() const { return declarations; }
143         const std::set<std::string> Exports() const { return exports; }
144
145 private:
146         std::map<std::string, Declaration *> declarations;
147         std::set<std::string> exports;
148
149 };
150
151 }
152
153
154 namespace std {
155
156 ostream &operator <<(ostream &, const loader::ParsedSource &);
157
158 }
159
160 #endif /* LOADER_PARSEDSOURCE_H_ */