Fennel: /home/pub/open/dev/fennel/calculator/CalcAssemblerException.h Source File (original) (raw)

00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023 #ifndef Fennel_CalcAssemblerException_Included 00024 #define Fennel_CalcAssemblerException_Included 00025 00026 #include "fennel/calculator/CalcTypedefs.h" 00027 #include "fennel/common/FennelExcn.h" 00028 00029 #include 00030 00031 FENNEL_BEGIN_NAMESPACE 00032 00033 using namespace std; 00034 00038 class FENNEL_CALCULATOR_EXPORT InvalidTypeException 00039 : public FennelExcn 00040 { 00041 public: 00044 explicit 00045 InvalidTypeException( 00046 string str, 00047 StandardTypeDescriptorOrdinal type, 00048 StandardTypeDescriptorOrdinal expected) 00049 : FennelExcn(str), 00050 mInvalidType(type), 00051 mExpectedType(expected) 00052 { 00053 if (msg.length() > 0) { 00054 msg += ": "; 00055 } 00056 msg += "Invalid type "; 00057 msg += StandardTypeDescriptor::toString(type); 00058 msg += ", expecting "; 00059 msg += StandardTypeDescriptor::toString(expected); 00060 } 00061 InvalidTypeException() throw() {} 00062 00063 protected: 00065 StandardTypeDescriptorOrdinal mInvalidType; 00067 StandardTypeDescriptorOrdinal mExpectedType; 00068 }; 00069 00073 template 00074 class InvalidValueException : public FennelExcn 00075 { 00076 public: 00079 explicit 00080 InvalidValueException( 00081 string str, StandardTypeDescriptorOrdinal type, T value) 00082 : FennelExcn(str), mType(type), mValue(value) 00083 { 00084 ostringstream ostr(""); 00085 ostr << "Invalid value " << value << " for type " 00086 << StandardTypeDescriptor::toString(type); 00087 if (msg.length() > 0) { 00088 msg += ": "; 00089 } 00090 msg += ostr.str(); 00091 } 00092 [InvalidValueException](classInvalidValueException.html#cdb6aee47f562825cda7a4d377bb541a)() throw() {} 00093 00094 protected: 00095 StandardTypeDescriptorOrdinal mType; 00096 T mValue; 00097 }; 00098 00108 class FENNEL_CALCULATOR_EXPORT CalcAssemblerException 00109 : public FennelExcn 00110 { 00111 public: 00113 explicit 00114 CalcAssemblerException(string str, CalcYYLocType loc) 00115 : FennelExcn(str), mDescription(str), mLoc(loc), mLocValid(true) 00116 { 00117 msg += getLocationString(); 00118 } 00119 00121 explicit 00122 CalcAssemblerException(string str) 00123 : FennelExcn(str), mDescription(str), mLocValid(false) 00124 { 00125 } 00126 00127 ~CalcAssemblerException() throw() {} 00128 00130 void setLocation(CalcYYLocType loc) 00131 { 00132 mLocValid = true; 00133 mLoc = loc; 00134 msg = mDescription + getLocationString(); 00135 } 00136 00138 void setCode(string s) 00139 { 00140 mCode = s; 00141 if (mLocValid) { 00142
00143
00144
00145
00146 00147
00148
00149
00150 00151
00152 00153
00154
00155
00156
00157
00158
00159 00160 if (mLoc.first_pos <= mLoc.last_pos && 00161 mLoc.first_pos < s.length()) 00162 { 00163 mCodeSnippet = 00164 s.substr( 00165 mLoc.first_pos, 00166 mLoc.last_pos - mLoc.first_pos + 1); 00167 } else { 00168 mCodeSnippet = s; 00169 } 00170 } 00171 } 00172 00174 const string& getCode() const 00175 { 00176 return mCode; 00177 } 00178 00180 const string& getCodeSnippet() const 00181 { 00182 return mCodeSnippet; 00183 } 00184 00186 const string getLocationString() const 00187 { 00188 string locStr; 00189 if (mLocValid) { 00190 ostringstream ostr(locStr); 00191 ostr << " (at line:col " << mLoc.first_line 00192 << ":" << mLoc.first_column 00193 << " to " << mLoc.last_line << ":" << mLoc.last_column 00194 << ", characters " << mLoc.first_pos 00195 << " to " << mLoc.last_pos << ")"; 00196 locStr = ostr.str(); 00197 } else { 00198 locStr = "Unknown location"; 00199 } 00200 return locStr; 00201 } 00202 00204 string mDescription; 00206 CalcYYLocType mLoc; 00208 bool mLocValid; 00210 string mCode; 00212 string mCodeSnippet; 00213 }; 00214 00215 FENNEL_END_NAMESPACE 00216 00217 #endif 00218 00219