4 * Created on: Aug 26, 2012
8 #include "ParsedSource.h"
14 using std::runtime_error;
20 void ParsedSource::AddDeclaration(Declaration *d) {
21 map<string, Declaration *>::iterator i(declarations.find(d->Identifier()));
22 if (i != declarations.end()) {
23 if (d->TypeName() != i->second->TypeName()) {
24 throw runtime_error("invalid redeclaration of " + i->second->TypeName() + " " + d->Identifier());
27 declarations.insert(std::make_pair(d->Identifier(), d));
31 void ParsedSource::AddDefinition(Definition *d) {
32 map<string, Definition *>::iterator i(definitions.find(d->Identifier()));
33 if (i != definitions.end()) {
34 throw runtime_error("redefinition of " + i->second->TypeName() + " " + d->Identifier());
36 definitions.insert(std::make_pair(d->Identifier(), d));
40 void ParsedSource::ExportDeclaration(Declaration *d) {
42 exports.insert(d->Identifier());
45 void ParsedSource::ExportIdentifier(const std::string &i) {
46 if (declarations.count(i)) {
49 throw runtime_error("cannot export undeclared identifier " + i);
54 void Definition::SetValue(Literal *v) {
59 void Definition::SetValue(PropertyList *v) {
64 Literal *Definition::GetLiteral() {
66 return (Literal *)value;
68 throw runtime_error("tried to access properties as literal");
72 const Literal *Definition::GetLiteral() const {
74 return (Literal *)value;
76 throw runtime_error("tried to access properties as literal");
80 PropertyList *Definition::GetProperties() {
82 return (PropertyList *)value;
84 throw runtime_error("tried to access literal value as property list");
88 const PropertyList *Definition::GetProperties() const {
90 return (PropertyList *)value;
92 throw runtime_error("tried to access literal value as property list");
97 PropertyList::~PropertyList() {
98 for (map<string, Value *>::iterator i(props.begin()), end(props.end()); i != end; ++i) {
104 Literal::Literal(const vector<Value *> &v)
110 , type(ARRAY_VALUES) {
114 Literal::Literal(const std::vector<PropertyList *> &pls)
120 , type(ARRAY_PROPS) {
124 Literal::Literal(bool b)
133 Literal::Literal(int r, int g, int b, int a)
142 Literal::Literal(int number)
151 Literal::Literal(const string &str)
161 Literal::Literal(int x, int y)
170 Literal::Literal(const string &typeName, PropertyList *properties)
181 const vector<Value *> &Literal::GetValues() const {
182 if (type == ARRAY_VALUES) {
185 throw runtime_error("tried to access values of non-array literal");
189 const vector<PropertyList *> &Literal::GetPropertyLists() const {
190 if (type == ARRAY_PROPS) {
191 return propertyLists;
193 throw runtime_error("tried to access property lists of non-array literal");
197 bool Literal::GetBoolean() const {
198 if (type == BOOLEAN) {
201 throw runtime_error("tried to access boolean value of non-boolean literal");
205 int Literal::GetRed() const {
209 throw runtime_error("tried to access red component of non-color literal");
213 int Literal::GetGreen() const {
217 throw runtime_error("tried to access green component of non-color literal");
221 int Literal::GetBlue() const {
225 throw runtime_error("tried to access blue component of non-color literal");
229 int Literal::GetAlpha() const {
233 throw runtime_error("tried to access alpha component of non-color literal");
237 int Literal::GetNumber() const {
238 if (type == NUMBER) {
241 throw runtime_error("tried to access numerical value of non-number literal");
245 const string &Literal::GetString() const {
246 if (type == STRING) {
249 throw runtime_error("tried to access string value of non-color literal");
253 int Literal::GetX() const {
254 if (type == VECTOR) {
257 throw runtime_error("tried to access x component of non-vector literal");
261 int Literal::GetY() const {
262 if (type == VECTOR) {
265 throw runtime_error("tried to access y component of non-vector literal");
269 const string &Literal::GetTypeName() const {
270 if (type == OBJECT) {
273 throw runtime_error("tried to access type name of non-object literal");
277 const PropertyList *Literal::GetProperties() const {
278 if (type == OBJECT) {
281 throw runtime_error("tried to access properties of non-object literal");
290 ostream &operator <<(ostream &out, const loader::ParsedSource &source) {
291 out << "parsed source file" << endl;
292 out << "declared objects: " << endl;
293 for (map<string, loader::Declaration *>::const_iterator i(source.Declarations().begin()), end(source.Declarations().end()); i != end; ++i) {
294 out << " - " << i->first << " of type " << i->second->TypeName() << endl;
296 out << "defined objects: " << endl;
297 for (map<string, loader::Definition *>::const_iterator i(source.Definitions().begin()), end(source.Definitions().end()); i != end; ++i) {
298 out << " - " << i->first << " of type " << i->second->TypeName() << endl;
299 if (i->second->HasLiteralValue()) {
300 out << " literal value: " << *i->second->GetLiteral() << endl;
303 out << "exported objects: " << endl;
304 for (set<string>::const_iterator i(source.Exports().begin()), end(source.Exports().end()); i != end; ++i) {
305 out << " - " << *i << endl;
310 ostream &operator <<(ostream &out, const loader::Literal &l) {
311 switch (l.GetType()) {
312 case loader::Literal::ARRAY_VALUES:
313 out << "array of values";
315 case loader::Literal::ARRAY_PROPS:
316 out << "array of property lists";
318 case loader::Literal::BOOLEAN:
319 out << "boolean, " << (l.GetBoolean() ? "true" : "false");
321 case loader::Literal::COLOR:
322 out << "color, (" << l.GetRed() << ',' << l.GetGreen() << ',' << l.GetBlue() << ',' << l.GetAlpha() << ')';
324 case loader::Literal::NUMBER:
325 out << "number, " << l.GetNumber();
327 case loader::Literal::STRING:
328 out << "string, \"" << l.GetString() << '"';
330 case loader::Literal::VECTOR:
331 out << "vector, <" << l.GetX() << ',' << l.GetY() << '>';
333 case loader::Literal::OBJECT:
334 out << "object of type " << l.GetTypeName();