4 * Created on: Aug 26, 2012
8 #include "ParsedSource.h"
16 using std::runtime_error;
22 ParsedSource::~ParsedSource() {
23 for (map<string, Declaration *>::const_iterator i(declarations.begin()), end(declarations.end()); i != end; ++i) {
28 void ParsedSource::AddDeclaration(Declaration *d) {
29 map<string, Declaration *>::iterator i(declarations.find(d->Identifier()));
30 if (i != declarations.end()) {
31 if (d->TypeName() != i->second->TypeName()) {
32 throw runtime_error("invalid redeclaration of " + i->second->TypeName() + " " + d->Identifier());
35 declarations.insert(std::make_pair(d->Identifier(), d));
39 void ParsedSource::AddDefinition(Definition *d) {
40 map<string, Definition *>::iterator i(definitions.find(d->Identifier()));
41 if (i != definitions.end()) {
42 throw runtime_error("redefinition of " + i->second->TypeName() + " " + d->Identifier());
44 definitions.insert(std::make_pair(d->Identifier(), d));
48 void ParsedSource::ExportDeclaration(Declaration *d) {
50 exports.insert(d->Identifier());
53 void ParsedSource::ExportIdentifier(const std::string &i) {
54 if (declarations.count(i)) {
57 throw runtime_error("cannot export undeclared identifier " + i);
62 bool ParsedSource::IsDeclared(const std::string &name) const {
63 return declarations.count(name);
66 Declaration &ParsedSource::GetDeclaration(const std::string &name) {
67 map<string, Declaration *>::const_iterator i(declarations.find(name));
68 if (i != declarations.end()) {
71 throw runtime_error("undeclared identifier " + name);
75 const Declaration &ParsedSource::GetDeclaration(const std::string &name) const {
76 map<string, Declaration *>::const_iterator i(declarations.find(name));
77 if (i != declarations.end()) {
80 throw runtime_error("undeclared identifier " + name);
84 bool ParsedSource::IsDefined(const std::string &name) const {
85 return definitions.count(name);
88 Definition &ParsedSource::GetDefinition(const std::string &name) {
89 map<string, Definition *>::const_iterator i(definitions.find(name));
90 if (i != definitions.end()) {
93 string msg("undefined identifier " + name);
94 map<string, Declaration *>::const_iterator i(declarations.find(name));
95 if (i != declarations.end()) {
96 msg += ", declared as " + i->second->TypeName();
98 msg += ", not declared";
100 throw runtime_error(msg);
104 const Definition &ParsedSource::GetDefinition(const std::string &name) const {
105 map<string, Definition *>::const_iterator i(definitions.find(name));
106 if (i != definitions.end()) {
109 string msg("undefined identifier " + name);
110 map<string, Declaration *>::const_iterator i(declarations.find(name));
111 if (i != declarations.end()) {
112 msg += ", declared as " + i->second->TypeName();
114 msg += ", not declared";
116 throw runtime_error(msg);
120 void ParsedSource::WriteHeader(std::ostream &out) const {
121 for (std::set<string>::const_iterator i(exports.begin()), end(exports.end()); i != end; ++i) {
122 out << GetDeclaration(*i).TypeName() << ' ' << *i << std::endl;
127 Definition::~Definition() {
129 delete reinterpret_cast<Literal *>(value);
131 delete reinterpret_cast<PropertyList *>(value);
135 void Definition::SetValue(Literal *v) {
140 void Definition::SetValue(PropertyList *v) {
145 Literal *Definition::GetLiteral() {
147 return reinterpret_cast<Literal *>(value);
149 throw runtime_error("tried to access properties as literal");
153 const Literal *Definition::GetLiteral() const {
155 return reinterpret_cast<Literal *>(value);
157 throw runtime_error("tried to access properties as literal");
161 PropertyList *Definition::GetProperties() {
163 return reinterpret_cast<PropertyList *>(value);
165 throw runtime_error("tried to access literal value as property list");
169 const PropertyList *Definition::GetProperties() const {
171 return reinterpret_cast<PropertyList *>(value);
172 } else if (GetLiteral()->GetType() == Literal::OBJECT) {
173 return GetLiteral()->GetProperties();
175 throw runtime_error("tried to access literal value as property list");
180 PropertyList::~PropertyList() {
181 for (map<string, Value *>::iterator i(props.begin()), end(props.end()); i != end; ++i) {
187 Literal::Literal(const vector<Value *> &v)
193 , type(ARRAY_VALUES) {
197 Literal::Literal(const std::vector<PropertyList *> &pls)
203 , type(ARRAY_PROPS) {
207 Literal::Literal(bool b)
216 Literal::Literal(int r, int g, int b, int a)
225 Literal::Literal(int number)
234 Literal::Literal(const string &dir, const string &path)
236 , str(CatPath(dir, path))
244 Literal::Literal(const string &str)
254 Literal::Literal(int x, int y)
263 Literal::Literal(const string &typeName, PropertyList *properties)
273 Literal::~Literal() {
276 for (vector<Value *>::const_iterator i(values.begin()), end(values.end()); i != end; ++i) {
281 for (vector<PropertyList *>::const_iterator i(propertyLists.begin()), end(propertyLists.end()); i != end; ++i) {
294 const vector<Value *> &Literal::GetValues() const {
295 if (type == ARRAY_VALUES) {
298 throw runtime_error("tried to access values of non-array literal");
302 const vector<PropertyList *> &Literal::GetPropertyLists() const {
303 if (type == ARRAY_PROPS) {
304 return propertyLists;
306 throw runtime_error("tried to access property lists of non-array literal");
310 bool Literal::GetBoolean() const {
311 if (type == BOOLEAN) {
314 throw runtime_error("tried to access boolean value of non-boolean literal");
318 int Literal::GetRed() const {
322 throw runtime_error("tried to access red component of non-color literal");
326 int Literal::GetGreen() const {
330 throw runtime_error("tried to access green component of non-color literal");
334 int Literal::GetBlue() const {
338 throw runtime_error("tried to access blue component of non-color literal");
342 int Literal::GetAlpha() const {
346 throw runtime_error("tried to access alpha component of non-color literal");
350 int Literal::GetNumber() const {
351 if (type == NUMBER) {
354 throw runtime_error("tried to access numerical value of non-number literal");
358 const string &Literal::GetString() const {
359 if (type == STRING) {
362 throw runtime_error("tried to access string value of non-color literal");
366 int Literal::GetX() const {
367 if (type == VECTOR) {
370 throw runtime_error("tried to access x component of non-vector literal");
374 int Literal::GetY() const {
375 if (type == VECTOR) {
378 throw runtime_error("tried to access y component of non-vector literal");
382 const string &Literal::GetTypeName() const {
383 if (type == OBJECT) {
386 throw runtime_error("tried to access type name of non-object literal");
390 const PropertyList *Literal::GetProperties() const {
391 if (type == OBJECT) {
394 throw runtime_error("tried to access properties of non-object literal");
405 const Literal &Value::GetLiteral() const {
409 throw runtime_error("tried to access literal of identifier value");
413 const std::string &Value::GetIdentifier() const {
417 throw runtime_error("tried to access identifier of literal value");
426 ostream &operator <<(ostream &out, const loader::ParsedSource &source) {
427 out << "parsed source file" << endl;
428 out << "declared objects: " << endl;
429 for (map<string, loader::Declaration *>::const_iterator i(source.Declarations().begin()), end(source.Declarations().end()); i != end; ++i) {
430 out << " - " << i->first << " of type " << i->second->TypeName() << endl;
432 out << "defined objects: " << endl;
433 for (map<string, loader::Definition *>::const_iterator i(source.Definitions().begin()), end(source.Definitions().end()); i != end; ++i) {
434 out << " - " << i->first << " of type " << i->second->TypeName() << endl;
435 if (i->second->HasLiteralValue()) {
436 out << " literal value: " << *i->second->GetLiteral() << endl;
439 out << "exported objects: " << endl;
440 for (set<string>::const_iterator i(source.Exports().begin()), end(source.Exports().end()); i != end; ++i) {
441 out << " - " << *i << endl;
446 ostream &operator <<(ostream &out, const loader::Literal &l) {
447 switch (l.GetType()) {
448 case loader::Literal::ARRAY_VALUES:
449 out << "array of values";
451 case loader::Literal::ARRAY_PROPS:
452 out << "array of property lists";
454 case loader::Literal::BOOLEAN:
455 out << "boolean, " << (l.GetBoolean() ? "true" : "false");
457 case loader::Literal::COLOR:
458 out << "color, (" << l.GetRed() << ',' << l.GetGreen() << ',' << l.GetBlue() << ',' << l.GetAlpha() << ')';
460 case loader::Literal::NUMBER:
461 out << "number, " << l.GetNumber();
463 case loader::Literal::PATH:
464 out << "path, \"" << l.GetString() << '"';
466 case loader::Literal::STRING:
467 out << "string, \"" << l.GetString() << '"';
469 case loader::Literal::VECTOR:
470 out << "vector, <" << l.GetX() << ',' << l.GetY() << '>';
472 case loader::Literal::OBJECT:
473 out << "object of type " << l.GetTypeName();