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 throw runtime_error("undefined identifier " + name);
97 const Definition &ParsedSource::GetDefinition(const std::string &name) const {
98 map<string, Definition *>::const_iterator i(definitions.find(name));
99 if (i != definitions.end()) {
102 throw runtime_error("undefined identifier " + name);
106 void ParsedSource::WriteHeader(std::ostream &out) const {
107 for (std::set<string>::const_iterator i(exports.begin()), end(exports.end()); i != end; ++i) {
108 out << GetDeclaration(*i).TypeName() << ' ' << *i << std::endl;
113 Definition::~Definition() {
115 delete reinterpret_cast<Literal *>(value);
117 delete reinterpret_cast<PropertyList *>(value);
121 void Definition::SetValue(Literal *v) {
126 void Definition::SetValue(PropertyList *v) {
131 Literal *Definition::GetLiteral() {
133 return reinterpret_cast<Literal *>(value);
135 throw runtime_error("tried to access properties as literal");
139 const Literal *Definition::GetLiteral() const {
141 return reinterpret_cast<Literal *>(value);
143 throw runtime_error("tried to access properties as literal");
147 PropertyList *Definition::GetProperties() {
149 return reinterpret_cast<PropertyList *>(value);
151 throw runtime_error("tried to access literal value as property list");
155 const PropertyList *Definition::GetProperties() const {
157 return reinterpret_cast<PropertyList *>(value);
158 } else if (GetLiteral()->GetType() == Literal::OBJECT) {
159 return GetLiteral()->GetProperties();
161 throw runtime_error("tried to access literal value as property list");
166 PropertyList::~PropertyList() {
167 for (map<string, Value *>::iterator i(props.begin()), end(props.end()); i != end; ++i) {
173 Literal::Literal(const vector<Value *> &v)
179 , type(ARRAY_VALUES) {
183 Literal::Literal(const std::vector<PropertyList *> &pls)
189 , type(ARRAY_PROPS) {
193 Literal::Literal(bool b)
202 Literal::Literal(int r, int g, int b, int a)
211 Literal::Literal(int number)
220 Literal::Literal(const string &dir, const string &path)
222 , str(CatPath(dir, path))
230 Literal::Literal(const string &str)
240 Literal::Literal(int x, int y)
249 Literal::Literal(const string &typeName, PropertyList *properties)
259 Literal::~Literal() {
262 for (vector<Value *>::const_iterator i(values.begin()), end(values.end()); i != end; ++i) {
267 for (vector<PropertyList *>::const_iterator i(propertyLists.begin()), end(propertyLists.end()); i != end; ++i) {
280 const vector<Value *> &Literal::GetValues() const {
281 if (type == ARRAY_VALUES) {
284 throw runtime_error("tried to access values of non-array literal");
288 const vector<PropertyList *> &Literal::GetPropertyLists() const {
289 if (type == ARRAY_PROPS) {
290 return propertyLists;
292 throw runtime_error("tried to access property lists of non-array literal");
296 bool Literal::GetBoolean() const {
297 if (type == BOOLEAN) {
300 throw runtime_error("tried to access boolean value of non-boolean literal");
304 int Literal::GetRed() const {
308 throw runtime_error("tried to access red component of non-color literal");
312 int Literal::GetGreen() const {
316 throw runtime_error("tried to access green component of non-color literal");
320 int Literal::GetBlue() const {
324 throw runtime_error("tried to access blue component of non-color literal");
328 int Literal::GetAlpha() const {
332 throw runtime_error("tried to access alpha component of non-color literal");
336 int Literal::GetNumber() const {
337 if (type == NUMBER) {
340 throw runtime_error("tried to access numerical value of non-number literal");
344 const string &Literal::GetString() const {
345 if (type == STRING) {
348 throw runtime_error("tried to access string value of non-color literal");
352 int Literal::GetX() const {
353 if (type == VECTOR) {
356 throw runtime_error("tried to access x component of non-vector literal");
360 int Literal::GetY() const {
361 if (type == VECTOR) {
364 throw runtime_error("tried to access y component of non-vector literal");
368 const string &Literal::GetTypeName() const {
369 if (type == OBJECT) {
372 throw runtime_error("tried to access type name of non-object literal");
376 const PropertyList *Literal::GetProperties() const {
377 if (type == OBJECT) {
380 throw runtime_error("tried to access properties of non-object literal");
391 const Literal &Value::GetLiteral() const {
395 throw runtime_error("tried to access literal of identifier value");
399 const std::string &Value::GetIdentifier() const {
403 throw runtime_error("tried to access identifier of literal value");
412 ostream &operator <<(ostream &out, const loader::ParsedSource &source) {
413 out << "parsed source file" << endl;
414 out << "declared objects: " << endl;
415 for (map<string, loader::Declaration *>::const_iterator i(source.Declarations().begin()), end(source.Declarations().end()); i != end; ++i) {
416 out << " - " << i->first << " of type " << i->second->TypeName() << endl;
418 out << "defined objects: " << endl;
419 for (map<string, loader::Definition *>::const_iterator i(source.Definitions().begin()), end(source.Definitions().end()); i != end; ++i) {
420 out << " - " << i->first << " of type " << i->second->TypeName() << endl;
421 if (i->second->HasLiteralValue()) {
422 out << " literal value: " << *i->second->GetLiteral() << endl;
425 out << "exported objects: " << endl;
426 for (set<string>::const_iterator i(source.Exports().begin()), end(source.Exports().end()); i != end; ++i) {
427 out << " - " << *i << endl;
432 ostream &operator <<(ostream &out, const loader::Literal &l) {
433 switch (l.GetType()) {
434 case loader::Literal::ARRAY_VALUES:
435 out << "array of values";
437 case loader::Literal::ARRAY_PROPS:
438 out << "array of property lists";
440 case loader::Literal::BOOLEAN:
441 out << "boolean, " << (l.GetBoolean() ? "true" : "false");
443 case loader::Literal::COLOR:
444 out << "color, (" << l.GetRed() << ',' << l.GetGreen() << ',' << l.GetBlue() << ',' << l.GetAlpha() << ')';
446 case loader::Literal::NUMBER:
447 out << "number, " << l.GetNumber();
449 case loader::Literal::PATH:
450 out << "path, \"" << l.GetString() << '"';
452 case loader::Literal::STRING:
453 out << "string, \"" << l.GetString() << '"';
455 case loader::Literal::VECTOR:
456 out << "vector, <" << l.GetX() << ',' << l.GetY() << '>';
458 case loader::Literal::OBJECT:
459 out << "object of type " << l.GetTypeName();