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);
107 Definition::~Definition() {
109 delete reinterpret_cast<Literal *>(value);
111 delete reinterpret_cast<PropertyList *>(value);
115 void Definition::SetValue(Literal *v) {
120 void Definition::SetValue(PropertyList *v) {
125 Literal *Definition::GetLiteral() {
127 return reinterpret_cast<Literal *>(value);
129 throw runtime_error("tried to access properties as literal");
133 const Literal *Definition::GetLiteral() const {
135 return reinterpret_cast<Literal *>(value);
137 throw runtime_error("tried to access properties as literal");
141 PropertyList *Definition::GetProperties() {
143 return reinterpret_cast<PropertyList *>(value);
145 throw runtime_error("tried to access literal value as property list");
149 const PropertyList *Definition::GetProperties() const {
151 return reinterpret_cast<PropertyList *>(value);
152 } else if (GetLiteral()->GetType() == Literal::OBJECT) {
153 return GetLiteral()->GetProperties();
155 throw runtime_error("tried to access literal value as property list");
160 PropertyList::~PropertyList() {
161 for (map<string, Value *>::iterator i(props.begin()), end(props.end()); i != end; ++i) {
167 Literal::Literal(const vector<Value *> &v)
173 , type(ARRAY_VALUES) {
177 Literal::Literal(const std::vector<PropertyList *> &pls)
183 , type(ARRAY_PROPS) {
187 Literal::Literal(bool b)
196 Literal::Literal(int r, int g, int b, int a)
205 Literal::Literal(int number)
214 Literal::Literal(const string &dir, const string &path)
216 , str(CatPath(dir, path))
224 Literal::Literal(const string &str)
234 Literal::Literal(int x, int y)
243 Literal::Literal(const string &typeName, PropertyList *properties)
253 Literal::~Literal() {
256 for (vector<Value *>::const_iterator i(values.begin()), end(values.end()); i != end; ++i) {
261 for (vector<PropertyList *>::const_iterator i(propertyLists.begin()), end(propertyLists.end()); i != end; ++i) {
274 const vector<Value *> &Literal::GetValues() const {
275 if (type == ARRAY_VALUES) {
278 throw runtime_error("tried to access values of non-array literal");
282 const vector<PropertyList *> &Literal::GetPropertyLists() const {
283 if (type == ARRAY_PROPS) {
284 return propertyLists;
286 throw runtime_error("tried to access property lists of non-array literal");
290 bool Literal::GetBoolean() const {
291 if (type == BOOLEAN) {
294 throw runtime_error("tried to access boolean value of non-boolean literal");
298 int Literal::GetRed() const {
302 throw runtime_error("tried to access red component of non-color literal");
306 int Literal::GetGreen() const {
310 throw runtime_error("tried to access green component of non-color literal");
314 int Literal::GetBlue() const {
318 throw runtime_error("tried to access blue component of non-color literal");
322 int Literal::GetAlpha() const {
326 throw runtime_error("tried to access alpha component of non-color literal");
330 int Literal::GetNumber() const {
331 if (type == NUMBER) {
334 throw runtime_error("tried to access numerical value of non-number literal");
338 const string &Literal::GetString() const {
339 if (type == STRING) {
342 throw runtime_error("tried to access string value of non-color literal");
346 int Literal::GetX() const {
347 if (type == VECTOR) {
350 throw runtime_error("tried to access x component of non-vector literal");
354 int Literal::GetY() const {
355 if (type == VECTOR) {
358 throw runtime_error("tried to access y component of non-vector literal");
362 const string &Literal::GetTypeName() const {
363 if (type == OBJECT) {
366 throw runtime_error("tried to access type name of non-object literal");
370 const PropertyList *Literal::GetProperties() const {
371 if (type == OBJECT) {
374 throw runtime_error("tried to access properties of non-object literal");
385 const Literal &Value::GetLiteral() const {
389 throw runtime_error("tried to access literal of identifier value");
393 const std::string &Value::GetIdentifier() const {
397 throw runtime_error("tried to access identifier of literal value");
406 ostream &operator <<(ostream &out, const loader::ParsedSource &source) {
407 out << "parsed source file" << endl;
408 out << "declared objects: " << endl;
409 for (map<string, loader::Declaration *>::const_iterator i(source.Declarations().begin()), end(source.Declarations().end()); i != end; ++i) {
410 out << " - " << i->first << " of type " << i->second->TypeName() << endl;
412 out << "defined objects: " << endl;
413 for (map<string, loader::Definition *>::const_iterator i(source.Definitions().begin()), end(source.Definitions().end()); i != end; ++i) {
414 out << " - " << i->first << " of type " << i->second->TypeName() << endl;
415 if (i->second->HasLiteralValue()) {
416 out << " literal value: " << *i->second->GetLiteral() << endl;
419 out << "exported objects: " << endl;
420 for (set<string>::const_iterator i(source.Exports().begin()), end(source.Exports().end()); i != end; ++i) {
421 out << " - " << *i << endl;
426 ostream &operator <<(ostream &out, const loader::Literal &l) {
427 switch (l.GetType()) {
428 case loader::Literal::ARRAY_VALUES:
429 out << "array of values";
431 case loader::Literal::ARRAY_PROPS:
432 out << "array of property lists";
434 case loader::Literal::BOOLEAN:
435 out << "boolean, " << (l.GetBoolean() ? "true" : "false");
437 case loader::Literal::COLOR:
438 out << "color, (" << l.GetRed() << ',' << l.GetGreen() << ',' << l.GetBlue() << ',' << l.GetAlpha() << ')';
440 case loader::Literal::NUMBER:
441 out << "number, " << l.GetNumber();
443 case loader::Literal::PATH:
444 out << "path, \"" << l.GetString() << '"';
446 case loader::Literal::STRING:
447 out << "string, \"" << l.GetString() << '"';
449 case loader::Literal::VECTOR:
450 out << "vector, <" << l.GetX() << ',' << l.GetY() << '>';
452 case loader::Literal::OBJECT:
453 out << "object of type " << l.GetTypeName();