| |
libFAUDES
Sections
Index
|
symboltable.cppGo to the documentation of this file.00001 /** @file symboltable.cpp @brief Class SymbolTable */ 00002 00003 /* FAU Discrete Event Systems Library (libfaudes) 00004 00005 Copyright (C) 2006 Bernd Opitz 00006 Copyright (C) 2007 Thomas Moor 00007 Exclusive copyright is granted to Klaus Schmidt 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Lesser General Public 00011 License as published by the Free Software Foundation; either 00012 version 2.1 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ 00022 00023 00024 #include "symboltable.h" 00025 #include <iostream> 00026 00027 namespace faudes { 00028 00029 // static symbol table (used for events, should be in vGenerator) 00030 SymbolTable SymbolTable::msEventSymbolTable; 00031 00032 // constructor 00033 SymbolTable:: SymbolTable(void) : 00034 mMyName("SymbolTable"), 00035 mMaxIndex(std::numeric_limits<Idx>::max()), 00036 mNextIndex(1) { 00037 } 00038 00039 // Name() 00040 const std::string& SymbolTable::Name(void) const { 00041 return mMyName; 00042 } 00043 00044 //Name(name) 00045 void SymbolTable::Name(const std::string& rName) { 00046 mMyName = rName; 00047 } 00048 00049 // Clear() 00050 void SymbolTable::Clear(void) { 00051 mMaxIndex=std::numeric_limits<Idx>::max(); 00052 mNextIndex=1; 00053 mIndexMap.clear(); 00054 mNameMap.clear(); 00055 } 00056 00057 // Size() 00058 Idx SymbolTable::Size(void) { 00059 return mIndexMap.size(); 00060 } 00061 00062 // MaxIndex() 00063 Idx SymbolTable::MaxIndex(void) const { 00064 return mMaxIndex; 00065 } 00066 00067 // MaxIndex(index) 00068 void SymbolTable::MaxIndex(Idx index) { 00069 if(index <= std::numeric_limits<Idx>::max()) { 00070 mMaxIndex = index; 00071 return; 00072 } 00073 std::stringstream errstr; 00074 errstr << "symboltable overflow"; 00075 throw Exception("SymbolTable::MaxIndex(inde))", errstr.str(), 40); 00076 } 00077 00078 // LastIndex() 00079 Idx SymbolTable::LastIndex(void) const { 00080 return mNextIndex - 1; 00081 } 00082 00083 // ValidSymbol(rName) 00084 bool SymbolTable::ValidSymbol(const std::string& rName) { 00085 if(rName=="") return false; 00086 for(unsigned int cp=0;cp<rName.size();cp++) { 00087 char ch = rName[cp]; 00088 if(iscntrl(ch)) return false; 00089 if(isspace(ch)) return false; 00090 if(ch=='"') return false; 00091 if(ch=='#') return false; 00092 if(isdigit(ch)) continue; 00093 if(isalpha(ch)) continue; 00094 if(isprint(ch)) continue; 00095 return false; 00096 } 00097 return true; 00098 } 00099 00100 // UniqueSymbol(rName) 00101 std::string SymbolTable::UniqueSymbol(const std::string& rName) const { 00102 if(!Exists(rName)) return (rName); 00103 long int count=0; 00104 std::string name=rName; 00105 std::string bname=rName; 00106 // if the name ends with extension "_123", remove the extension 00107 unsigned int up = bname.find_last_of("_"); 00108 if(up != std::string::npos) { 00109 bool ad=true; 00110 for(unsigned int dp=up+1;dp<bname.size();dp++) 00111 if(!isdigit(bname[dp])) ad=false; 00112 if(ad) 00113 bname.erase(up); 00114 } 00115 // try append extension "_123" until name is unique 00116 do { 00117 count++; 00118 name=bname + "_" + ToStringInteger(count); 00119 } while(Exists(name)); 00120 return name; 00121 } 00122 00123 00124 // InsEntry(index, rName) 00125 Idx SymbolTable::InsEntry(Idx index, const std::string& rName) { 00126 if( ! (index <= mMaxIndex)) { 00127 std::stringstream errstr; 00128 errstr << "symboltable overflow"; 00129 throw Exception("SymbolTable::InsEntry(index,name))", errstr.str(), 40); 00130 } 00131 Idx nidx=Index(rName); 00132 if((nidx!=0) && (nidx!=index)) { 00133 std::stringstream errstr; 00134 errstr << "Name " << rName << " allready exists in EventSymbolTable"; 00135 throw Exception("SymbolTable::InsEntry(index,name)", errstr.str(), 41); 00136 } 00137 std::string idxname=Symbol(index); 00138 if((idxname != "") && (idxname != rName)) { 00139 std::stringstream errstr; 00140 errstr << "Index " << index << " allready exists in EventSymbolTable"; 00141 throw Exception("SymbolTable::InsEntry(index,name)", errstr.str(), 42); 00142 } 00143 if(!ValidSymbol(rName)) { 00144 std::stringstream errstr; 00145 errstr << "Name " << rName << " is not a valid symbol"; 00146 throw Exception("SymbolTable::InsEntry(index,name)", errstr.str(), 43); 00147 } 00148 00149 mIndexMap[rName] = index; 00150 mNameMap[index] = rName; 00151 00152 if(mNextIndex<=index) mNextIndex=index+1; 00153 return index; 00154 } 00155 00156 // InsEntry(rName) 00157 Idx SymbolTable::InsEntry(const std::string& rName) { 00158 Idx index=Index(rName); 00159 if( index != 0) return index; 00160 return InsEntry(mNextIndex,rName); 00161 } 00162 00163 00164 // SetEntry(index, rName) 00165 void SymbolTable::SetEntry(Idx index, const std::string& rName) { 00166 if(rName=="") { 00167 ClrEntry(index); 00168 return; 00169 } 00170 Idx nidx=Index(rName); 00171 if((nidx!=0) && (nidx!=index)) { 00172 std::stringstream errstr; 00173 errstr << "Name " << rName << " allready exists"; 00174 throw Exception("SymbolTable::SetEntry(index,name)", errstr.str(), 41); 00175 } 00176 if(!ValidSymbol(rName)) { 00177 std::stringstream errstr; 00178 errstr << "Name " << rName << " is not a valid symbol"; 00179 throw Exception("SymbolTable::SetEntry(index,name)", errstr.str(), 43); 00180 } 00181 mIndexMap[rName] = index; 00182 mNameMap[index] = rName; 00183 } 00184 00185 // SetDefaultSymbol(index) 00186 void SymbolTable::SetDefaultSymbol(Idx index) { 00187 ClrEntry(index); 00188 std::ostringstream o; 00189 std::string dname; 00190 o << index; 00191 dname = UniqueSymbol(ToStringInteger(index)); 00192 InsEntry(index,dname); 00193 } 00194 00195 00196 // ClrEntry(index) 00197 void SymbolTable::ClrEntry(Idx index) { 00198 std::map<Idx,std::string>::iterator it = mNameMap.find(index); 00199 if (it == mNameMap.end()) return; 00200 mIndexMap.erase(it->second); 00201 mNameMap.erase(it); 00202 } 00203 00204 // ClrEntry(rName) 00205 void SymbolTable::ClrEntry(const std::string& rName) { 00206 std::map<std::string,Idx>::iterator it = mIndexMap.find(rName); 00207 if (it == mIndexMap.end()) return; 00208 mNameMap.erase(it->second); 00209 mIndexMap.erase(it); 00210 } 00211 00212 // RestrictDomain(set) 00213 void SymbolTable::RestrictDomain(const IndexSet& rDomain) { 00214 // trivial cases 00215 if(rDomain.Empty()) { Clear(); return;} 00216 if(Size()==0) return; 00217 // lazy loop (todo: sorted iterate) 00218 std::map<Idx,std::string>::iterator it, oit; 00219 it = mNameMap.begin(); 00220 while(it!=mNameMap.end()) { 00221 oit=it; 00222 it++; 00223 if(!rDomain.Exists(oit->first)) mNameMap.erase(oit); 00224 } 00225 } 00226 00227 // Index(rName) 00228 Idx SymbolTable::Index(const std::string& rName) const { 00229 std::map<std::string,Idx>::const_iterator it = mIndexMap.find(rName); 00230 if (it == mIndexMap.end()) 00231 return 0; 00232 else 00233 return it->second; 00234 } 00235 00236 // Symbol(index) 00237 std::string SymbolTable::Symbol(Idx index) const { 00238 std::map<Idx,std::string>::const_iterator it = mNameMap.find(index); 00239 if (it == mNameMap.end()) 00240 return ""; 00241 else 00242 return it->second; 00243 } 00244 00245 00246 00247 // Exists(index) 00248 bool SymbolTable::Exists(Idx index) const { 00249 std::map<Idx,std::string>::const_iterator it = mNameMap.find(index); 00250 return (it != mNameMap.end()); 00251 } 00252 00253 // Exists(rName) 00254 bool SymbolTable::Exists(const std::string& rName) const { 00255 std::map<std::string,Idx>::const_iterator it = mIndexMap.find(rName); 00256 return ( it != mIndexMap.end()); 00257 } 00258 00259 00260 // GlobalEventSymbolTablep 00261 SymbolTable* SymbolTable::GlobalEventSymbolTablep(void) { 00262 return &msEventSymbolTable; 00263 } 00264 00265 00266 //DoWrite(rTr,rLabel,pContext) 00267 void SymbolTable::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const { 00268 (void) rTw; (void) rLabel; (void) pContext; 00269 FD_DC("SymbolTable::DoWrite(): dummy"); 00270 } 00271 00272 //DoRead(rTr,rLabel,pContext) 00273 void SymbolTable::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) { 00274 (void) rLabel; (void) pContext; (void) rTr; 00275 FD_DC("SymbolTable::DoRead(): dummy"); 00276 } 00277 00278 00279 } // namespace |
libFAUDES 2.14g --- 2009-12-3 --- c++ source docu by doxygen 1.5.6