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) {
195 typeName = v.front()->GetLiteral().GetTypeName();
199 Literal::Literal(const string &typeName, const vector<PropertyList *> &pls)
206 , type(ARRAY_PROPS) {
210 Literal::Literal(bool b)
212 , typeName("Boolean")
220 Literal::Literal(int r, int g, int b, int a)
230 Literal::Literal(int number)
240 Literal::Literal(const string &dir, const string &path)
243 , str(CatPath(dir, path))
251 Literal::Literal(const string &str)
262 Literal::Literal(int x, int y)
272 Literal::Literal(const string &typeName, PropertyList *properties)
282 Literal::~Literal() {
285 for (vector<Value *>::const_iterator i(values.begin()), end(values.end()); i != end; ++i) {
290 for (vector<PropertyList *>::const_iterator i(propertyLists.begin()), end(propertyLists.end()); i != end; ++i) {
303 const vector<Value *> &Literal::GetValues() const {
304 if (type == ARRAY_VALUES) {
307 throw runtime_error("tried to access values of non-array literal");
311 const vector<PropertyList *> &Literal::GetPropertyLists() const {
312 if (type == ARRAY_PROPS) {
313 return propertyLists;
315 throw runtime_error("tried to access property lists of non-array literal");
319 bool Literal::GetBoolean() const {
320 if (type == BOOLEAN) {
323 throw runtime_error("tried to access boolean value of non-boolean literal");
327 int Literal::GetRed() const {
331 throw runtime_error("tried to access red component of non-color literal");
335 int Literal::GetGreen() const {
339 throw runtime_error("tried to access green component of non-color literal");
343 int Literal::GetBlue() const {
347 throw runtime_error("tried to access blue component of non-color literal");
351 int Literal::GetAlpha() const {
355 throw runtime_error("tried to access alpha component of non-color literal");
359 int Literal::GetNumber() const {
360 if (type == NUMBER) {
363 throw runtime_error("tried to access numerical value of non-number literal");
367 const string &Literal::GetString() const {
368 if (type == STRING) {
371 throw runtime_error("tried to access string value of non-color literal");
375 int Literal::GetX() const {
376 if (type == VECTOR) {
379 throw runtime_error("tried to access x component of non-vector literal");
383 int Literal::GetY() const {
384 if (type == VECTOR) {
387 throw runtime_error("tried to access y component of non-vector literal");
391 const string &Literal::GetTypeName() const {
395 const PropertyList *Literal::GetProperties() const {
396 if (type == OBJECT) {
399 throw runtime_error("tried to access properties of non-object literal");
410 const Literal &Value::GetLiteral() const {
414 throw runtime_error("tried to access literal of identifier value");
418 const std::string &Value::GetIdentifier() const {
422 throw runtime_error("tried to access identifier of literal value");
431 ostream &operator <<(ostream &out, const loader::ParsedSource &source) {
432 out << "parsed source file" << endl;
433 out << "declared objects: " << endl;
434 for (map<string, loader::Declaration *>::const_iterator i(source.Declarations().begin()), end(source.Declarations().end()); i != end; ++i) {
435 out << " - " << i->first << " of type " << i->second->TypeName() << endl;
437 out << "defined objects: " << endl;
438 for (map<string, loader::Definition *>::const_iterator i(source.Definitions().begin()), end(source.Definitions().end()); i != end; ++i) {
439 out << " - " << i->first << " of type " << i->second->TypeName() << endl;
440 if (i->second->HasLiteralValue()) {
441 out << " literal value: " << *i->second->GetLiteral() << endl;
444 out << "exported objects: " << endl;
445 for (set<string>::const_iterator i(source.Exports().begin()), end(source.Exports().end()); i != end; ++i) {
446 out << " - " << *i << endl;
451 ostream &operator <<(ostream &out, const loader::Literal &l) {
452 switch (l.GetType()) {
453 case loader::Literal::ARRAY_VALUES:
454 out << "array of values";
456 case loader::Literal::ARRAY_PROPS:
457 out << "array of property lists";
459 case loader::Literal::BOOLEAN:
460 out << "boolean, " << (l.GetBoolean() ? "true" : "false");
462 case loader::Literal::COLOR:
463 out << "color, (" << l.GetRed() << ',' << l.GetGreen() << ',' << l.GetBlue() << ',' << l.GetAlpha() << ')';
465 case loader::Literal::NUMBER:
466 out << "number, " << l.GetNumber();
468 case loader::Literal::PATH:
469 out << "path, \"" << l.GetString() << '"';
471 case loader::Literal::STRING:
472 out << "string, \"" << l.GetString() << '"';
474 case loader::Literal::VECTOR:
475 out << "vector, <" << l.GetX() << ',' << l.GetY() << '>';
477 case loader::Literal::OBJECT:
478 out << "object of type " << l.GetTypeName();