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