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);
150 } else if (GetLiteral()->GetType() == Literal::OBJECT) {
151 return GetLiteral()->GetProperties();
153 throw runtime_error("tried to access literal value as property list");
158 PropertyList::~PropertyList() {
159 for (map<string, Value *>::iterator i(props.begin()), end(props.end()); i != end; ++i) {
165 Literal::Literal(const vector<Value *> &v)
171 , type(ARRAY_VALUES) {
175 Literal::Literal(const std::vector<PropertyList *> &pls)
181 , type(ARRAY_PROPS) {
185 Literal::Literal(bool b)
194 Literal::Literal(int r, int g, int b, int a)
203 Literal::Literal(int number)
212 Literal::Literal(const string &str)
222 Literal::Literal(int x, int y)
231 Literal::Literal(const string &typeName, PropertyList *properties)
241 Literal::~Literal() {
244 for (vector<Value *>::const_iterator i(values.begin()), end(values.end()); i != end; ++i) {
249 for (vector<PropertyList *>::const_iterator i(propertyLists.begin()), end(propertyLists.end()); i != end; ++i) {
262 const vector<Value *> &Literal::GetValues() const {
263 if (type == ARRAY_VALUES) {
266 throw runtime_error("tried to access values of non-array literal");
270 const vector<PropertyList *> &Literal::GetPropertyLists() const {
271 if (type == ARRAY_PROPS) {
272 return propertyLists;
274 throw runtime_error("tried to access property lists of non-array literal");
278 bool Literal::GetBoolean() const {
279 if (type == BOOLEAN) {
282 throw runtime_error("tried to access boolean value of non-boolean literal");
286 int Literal::GetRed() const {
290 throw runtime_error("tried to access red component of non-color literal");
294 int Literal::GetGreen() const {
298 throw runtime_error("tried to access green component of non-color literal");
302 int Literal::GetBlue() const {
306 throw runtime_error("tried to access blue component of non-color literal");
310 int Literal::GetAlpha() const {
314 throw runtime_error("tried to access alpha component of non-color literal");
318 int Literal::GetNumber() const {
319 if (type == NUMBER) {
322 throw runtime_error("tried to access numerical value of non-number literal");
326 const string &Literal::GetString() const {
327 if (type == STRING) {
330 throw runtime_error("tried to access string value of non-color literal");
334 int Literal::GetX() const {
335 if (type == VECTOR) {
338 throw runtime_error("tried to access x component of non-vector literal");
342 int Literal::GetY() const {
343 if (type == VECTOR) {
346 throw runtime_error("tried to access y component of non-vector literal");
350 const string &Literal::GetTypeName() const {
351 if (type == OBJECT) {
354 throw runtime_error("tried to access type name of non-object literal");
358 const PropertyList *Literal::GetProperties() const {
359 if (type == OBJECT) {
362 throw runtime_error("tried to access properties of non-object literal");
373 const Literal &Value::GetLiteral() const {
377 throw runtime_error("tried to access literal of identifier value");
381 const std::string &Value::GetIdentifier() const {
385 throw runtime_error("tried to access identifier of literal value");
394 ostream &operator <<(ostream &out, const loader::ParsedSource &source) {
395 out << "parsed source file" << endl;
396 out << "declared objects: " << endl;
397 for (map<string, loader::Declaration *>::const_iterator i(source.Declarations().begin()), end(source.Declarations().end()); i != end; ++i) {
398 out << " - " << i->first << " of type " << i->second->TypeName() << endl;
400 out << "defined objects: " << endl;
401 for (map<string, loader::Definition *>::const_iterator i(source.Definitions().begin()), end(source.Definitions().end()); i != end; ++i) {
402 out << " - " << i->first << " of type " << i->second->TypeName() << endl;
403 if (i->second->HasLiteralValue()) {
404 out << " literal value: " << *i->second->GetLiteral() << endl;
407 out << "exported objects: " << endl;
408 for (set<string>::const_iterator i(source.Exports().begin()), end(source.Exports().end()); i != end; ++i) {
409 out << " - " << *i << endl;
414 ostream &operator <<(ostream &out, const loader::Literal &l) {
415 switch (l.GetType()) {
416 case loader::Literal::ARRAY_VALUES:
417 out << "array of values";
419 case loader::Literal::ARRAY_PROPS:
420 out << "array of property lists";
422 case loader::Literal::BOOLEAN:
423 out << "boolean, " << (l.GetBoolean() ? "true" : "false");
425 case loader::Literal::COLOR:
426 out << "color, (" << l.GetRed() << ',' << l.GetGreen() << ',' << l.GetBlue() << ',' << l.GetAlpha() << ')';
428 case loader::Literal::NUMBER:
429 out << "number, " << l.GetNumber();
431 case loader::Literal::STRING:
432 out << "string, \"" << l.GetString() << '"';
434 case loader::Literal::VECTOR:
435 out << "vector, <" << l.GetX() << ',' << l.GetY() << '>';
437 case loader::Literal::OBJECT:
438 out << "object of type " << l.GetTypeName();