1 2 /* Web Polygraph http://www.web-polygraph.org/ 3 * Copyright 2003-2011 The Measurement Factory 4 * Licensed under the Apache License, Version 2.0 */ 5 6 #include "xparser/xparser.h" 7 8 #include "xstd/h/string.h" 9 #include "xstd/h/iostream.h" 10 #include "xstd/h/sstream.h" 11 12 #include "xparser/SynSym.h" 13 14 15 /* SynSym */ 16 SynSym(const String & aType)17SynSym::SynSym(const String &aType): theType(0) { 18 type(aType); 19 } 20 ~SynSym()21SynSym::~SynSym() { 22 type(0); 23 } 24 isA(const String & type) const25bool SynSym::isA(const String &type) const { 26 return theType && type && type == theType; 27 } 28 29 // may be expensive canBe(const String & type) const30bool SynSym::canBe(const String &type) const { 31 if (isA(type)) 32 return true; 33 if (SynSym *s = clone(type)) { 34 delete s; 35 return true; 36 } 37 return false; 38 } 39 40 // expensive: comparison is done using print method! equal(const SynSym & s) const41bool SynSym::equal(const SynSym &s) const { 42 ostringstream os1, os2; 43 print(os1) << ends; 44 s.print(os2) << ends; 45 46 const bool res = os1.str() == os2.str(); 47 48 streamFreeze(os1, false); 49 streamFreeze(os2, false); 50 51 return res; 52 } 53 cast(const String & typ)54SynSym &SynSym::cast(const String &typ) { 55 ((const SynSym *)this)->cast(typ); // will abort if needed 56 return *this; 57 } 58 cast(const String & typ) const59const SynSym &SynSym::cast(const String &typ) const { 60 if (!isA(typ)) { 61 cerr << here << "cannot cast " << type() << " to " << typ << 62 endl << xabort; 63 } 64 return *this; 65 } 66 clone(const String & type) const67SynSym *SynSym::clone(const String &type) const { 68 SynSym *copy = dupe(type); 69 if (copy) 70 copy->loc(loc()); 71 return copy; 72 } 73 type(const String & aType)74void SynSym::type(const String &aType) { 75 theType = aType; 76 } 77 memberItem(const String &)78SynSymTblItem **SynSym::memberItem(const String &) { 79 return 0; 80 } 81 print(ostream & os,const String &) const82ostream &SynSym::print(ostream &os, const String &) const { 83 return os << "<something " << type() << "-ish>"; 84 } 85