4 * Created on: Aug 26, 2012
8 #include "ParsedSource.h"
14 using std::runtime_error;
20 ParsedSource::~ParsedSource() {
21 for (map<string, Declaration *>::const_iterator i(declarations.begin()), end(declarations.end()); i != end; ++i) {
26 void ParsedSource::AddDeclaration(Declaration *d) {
27 map<string, Declaration *>::iterator i(declarations.find(d->Identifier()));
28 if (i != declarations.end()) {
29 if (d->TypeName() != i->second->TypeName()) {
30 throw runtime_error("invalid redeclaration of " + i->second->TypeName() + " " + d->Identifier());
33 declarations.insert(std::make_pair(d->Identifier(), d));
37 void ParsedSource::AddDefinition(Definition *d) {
38 map<string, Definition *>::iterator i(definitions.find(d->Identifier()));
39 if (i != definitions.end()) {
40 throw runtime_error("redefinition of " + i->second->TypeName() + " " + d->Identifier());
42 definitions.insert(std::make_pair(d->Identifier(), d));
46 void ParsedSource::ExportDeclaration(Declaration *d) {
48 exports.insert(d->Identifier());
51 void ParsedSource::ExportIdentifier(const std::string &i) {
52 if (declarations.count(i)) {
55 throw runtime_error("cannot export undeclared identifier " + i);
60 bool ParsedSource::IsDeclared(const std::string &name) const {
61 return declarations.count(name);
64 Declaration &ParsedSource::GetDeclaration(const std::string &name) {
65 map<string, Declaration *>::const_iterator i(declarations.find(name));
66 if (i != declarations.end()) {
69 throw runtime_error("undeclared identifier " + name);
73 const Declaration &ParsedSource::GetDeclaration(const std::string &name) const {
74 map<string, Declaration *>::const_iterator i(declarations.find(name));
75 if (i != declarations.end()) {
78 throw runtime_error("undeclared identifier " + name);
82 bool ParsedSource::IsDefined(const std::string &name) const {
83 return definitions.count(name);
86 Definition &ParsedSource::GetDefinition(const std::string &name) {
87 map<string, Definition *>::const_iterator i(definitions.find(name));
88 if (i != definitions.end()) {
91 throw runtime_error("undefined identifier " + name);
95 const Definition &ParsedSource::GetDefinition(const std::string &name) const {
96 map<string, Definition *>::const_iterator i(definitions.find(name));
97 if (i != definitions.end()) {
100 throw runtime_error("undefined identifier " + name);
105 Definition::~Definition() {
107 delete reinterpret_cast<Literal *>(value);
109 delete reinterpret_cast<PropertyList *>(value);
113 void Definition::SetValue(Literal *v) {
118 void Definition::SetValue(PropertyList *v) {
123 Literal *Definition::GetLiteral() {
125 return reinterpret_cast<Literal *>(value);
127 throw runtime_error("tried to access properties as literal");
131 const Literal *Definition::GetLiteral() const {
133 return reinterpret_cast<Literal *>(value);
135 throw runtime_error("tried to access properties as literal");
139 PropertyList *Definition::GetProperties() {
141 return reinterpret_cast<PropertyList *>(value);
143 throw runtime_error("tried to access literal value as property list");
147 const PropertyList *Definition::GetProperties() const {
149 return reinterpret_cast<PropertyList *>(value);
151 throw runtime_error("tried to access literal value as property list");
156 PropertyList::~PropertyList() {
157 for (map<string, Value *>::iterator i(props.begin()), end(props.end()); i != end; ++i) {
163 Literal::Literal(const vector<Value *> &v)
169 , type(ARRAY_VALUES) {
173 Literal::Literal(const std::vector<PropertyList *> &pls)
179 , type(ARRAY_PROPS) {
183 Literal::Literal(bool b)
192 Literal::Literal(int r, int g, int b, int a)
201 Literal::Literal(int number)
210 Literal::Literal(const string &str)
220 Literal::Literal(int x, int y)
229 Literal::Literal(const string &typeName, PropertyList *properties)
239 Literal::~Literal() {
242 for (vector<Value *>::const_iterator i(values.begin()), end(values.end()); i != end; ++i) {
247 for (vector<PropertyList *>::const_iterator i(propertyLists.begin()), end(propertyLists.end()); i != end; ++i) {
260 const vector<Value *> &Literal::GetValues() const {
261 if (type == ARRAY_VALUES) {
264 throw runtime_error("tried to access values of non-array literal");
268 const vector<PropertyList *> &Literal::GetPropertyLists() const {
269 if (type == ARRAY_PROPS) {
270 return propertyLists;
272 throw runtime_error("tried to access property lists of non-array literal");
276 bool Literal::GetBoolean() const {
277 if (type == BOOLEAN) {
280 throw runtime_error("tried to access boolean value of non-boolean literal");
284 int Literal::GetRed() const {
288 throw runtime_error("tried to access red component of non-color literal");
292 int Literal::GetGreen() const {
296 throw runtime_error("tried to access green component of non-color literal");
300 int Literal::GetBlue() const {
304 throw runtime_error("tried to access blue component of non-color literal");
308 int Literal::GetAlpha() const {
312 throw runtime_error("tried to access alpha component of non-color literal");
316 int Literal::GetNumber() const {
317 if (type == NUMBER) {
320 throw runtime_error("tried to access numerical value of non-number literal");
324 const string &Literal::GetString() const {
325 if (type == STRING) {
328 throw runtime_error("tried to access string value of non-color literal");
332 int Literal::GetX() const {
333 if (type == VECTOR) {
336 throw runtime_error("tried to access x component of non-vector literal");
340 int Literal::GetY() const {
341 if (type == VECTOR) {
344 throw runtime_error("tried to access y component of non-vector literal");
348 const string &Literal::GetTypeName() const {
349 if (type == OBJECT) {
352 throw runtime_error("tried to access type name of non-object literal");
356 const PropertyList *Literal::GetProperties() const {
357 if (type == OBJECT) {
360 throw runtime_error("tried to access properties of non-object literal");
371 const Literal &Value::GetLiteral() const {
375 throw runtime_error("tried to access literal of identifier value");
379 const std::string &Value::GetIdentifier() const {
383 throw runtime_error("tried to access identifier of literal value");
392 ostream &operator <<(ostream &out, const loader::ParsedSource &source) {
393 out << "parsed source file" << endl;
394 out << "declared objects: " << endl;
395 for (map<string, loader::Declaration *>::const_iterator i(source.Declarations().begin()), end(source.Declarations().end()); i != end; ++i) {
396 out << " - " << i->first << " of type " << i->second->TypeName() << endl;
398 out << "defined objects: " << endl;
399 for (map<string, loader::Definition *>::const_iterator i(source.Definitions().begin()), end(source.Definitions().end()); i != end; ++i) {
400 out << " - " << i->first << " of type " << i->second->TypeName() << endl;
401 if (i->second->HasLiteralValue()) {
402 out << " literal value: " << *i->second->GetLiteral() << endl;
405 out << "exported objects: " << endl;
406 for (set<string>::const_iterator i(source.Exports().begin()), end(source.Exports().end()); i != end; ++i) {
407 out << " - " << *i << endl;
412 ostream &operator <<(ostream &out, const loader::Literal &l) {
413 switch (l.GetType()) {
414 case loader::Literal::ARRAY_VALUES:
415 out << "array of values";
417 case loader::Literal::ARRAY_PROPS:
418 out << "array of property lists";
420 case loader::Literal::BOOLEAN:
421 out << "boolean, " << (l.GetBoolean() ? "true" : "false");
423 case loader::Literal::COLOR:
424 out << "color, (" << l.GetRed() << ',' << l.GetGreen() << ',' << l.GetBlue() << ',' << l.GetAlpha() << ')';
426 case loader::Literal::NUMBER:
427 out << "number, " << l.GetNumber();
429 case loader::Literal::STRING:
430 out << "string, \"" << l.GetString() << '"';
432 case loader::Literal::VECTOR:
433 out << "vector, <" << l.GetX() << ',' << l.GetY() << '>';
435 case loader::Literal::OBJECT:
436 out << "object of type " << l.GetTypeName();