]> git.localhorst.tv Git - l2e.git/blob - src/loader/ParsedSource.h
more information in parsed source and output
[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(false) { }
82
83 private:
84         Literal *literal;
85         std::string identifier;
86         bool isLiteral;
87
88 };
89
90
91 class PropertyList {
92
93 public:
94         ~PropertyList();
95
96 public:
97         void SetProperty(const std::string &name, Value *value) {
98                 props[name] = value;
99         }
100
101 private:
102         std::map<std::string, Value *> props;
103
104 };
105
106
107 class Declaration {
108
109 public:
110         Declaration(const std::string &typeName, const std::string &identifier)
111         : typeName(typeName), identifier(identifier) { }
112         virtual ~Declaration() { }
113
114 public:
115         const std::string &TypeName() const { return typeName; }
116         const std::string &Identifier() const { return identifier; }
117
118 private:
119         std::string typeName;
120         std::string identifier;
121
122 };
123
124
125 class Definition
126 : public Declaration {
127
128 public:
129         Definition(const std::string &typeName, const std::string &identifier)
130         : Declaration(typeName, identifier), value(0), isLiteral(false) { }
131
132 public:
133         void SetValue(Literal *);
134         void SetValue(PropertyList *);
135
136         bool HasLiteralValue() const { return isLiteral && value; }
137         bool HasProperties() const { return !isLiteral && value; }
138         Literal *GetLiteral();
139         const Literal *GetLiteral() const;
140         PropertyList *GetProperties();
141         const PropertyList *GetProperties() const;
142
143 private:
144         void *value;
145         bool isLiteral;
146
147 };
148
149
150 class ParsedSource {
151
152 public:
153         ParsedSource() { }
154         ~ParsedSource() { }
155
156 public:
157         void AddDeclaration(Declaration *);
158         void AddDefinition(Definition *);
159         void ExportDeclaration(Declaration *);
160         void ExportIdentifier(const std::string &);
161
162         const std::map<std::string, Declaration *> &Declarations() const { return declarations; }
163         const std::map<std::string, Definition *> &Definitions() const { return definitions; }
164         const std::set<std::string> &Exports() const { return exports; }
165
166 private:
167         std::map<std::string, Declaration *> declarations;
168         std::map<std::string, Definition *> definitions;
169         std::set<std::string> exports;
170
171 };
172
173 }
174
175
176 namespace std {
177
178 ostream &operator <<(ostream &, const loader::ParsedSource &);
179 ostream &operator <<(ostream &, const loader::Literal &);
180
181 }
182
183 #endif /* LOADER_PARSEDSOURCE_H_ */