CalcAssemblerException.h

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/calculator/CalcAssemblerException.h#3 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2004-2009 SQLstream, Inc.
00006 // Copyright (C) 2009-2009 LucidEra, Inc.
00007 //
00008 // This program is free software; you can redistribute it and/or modify it
00009 // under the terms of the GNU General Public License as published by the Free
00010 // Software Foundation; either version 2 of the License, or (at your option)
00011 // any later version approved by The Eigenbase Project.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 <strstream>
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 <typename T>
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() 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             // The following assert is often not met with weird
00143             // programs: Better to get some sort of error message with
00144             // the invariant violated, than to just get an assert
00145             // error masking the problem.
00146 
00147             // SWZ: REVIEW: 9/15/2005: I find this to occur for
00148             // certain *sequences* of very simple programs, any one of
00149             // which when run singly, causes no problems!
00150 
00151             //assert(mLoc.first_pos <= mLoc.last_pos);
00152 
00153             // SWZ: REVIEW: 9/15/2005: Additionally, when invalid,
00154             // mLoc.first_pos can be very large (e.g. past the end of
00155             // s) which causes an out_of_range exception.  Added code
00156             // to prevent s.substr from throwing.  A better solution
00157             // would be to fix the bug that causes a supposedly valid
00158             // mLoc to have wildly out-of-range values.
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 // End CalcAssemblerException.h

Generated on Mon Jun 22 04:00:17 2009 for Fennel by  doxygen 1.5.1