00001
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "symboltable.h"
00025 #include <iostream>
00026
00027 namespace faudes {
00028
00029
00030 SymbolTable SymbolTable::msEventSymbolTable;
00031
00032
00033 SymbolTable:: SymbolTable(void) :
00034 mMyName("SymbolTable"),
00035 mMaxIndex(std::numeric_limits<Idx>::max()),
00036 mNextIndex(1) {
00037 }
00038
00039
00040 std::string SymbolTable::Name(void) const {
00041 return mMyName;
00042 }
00043
00044
00045 void SymbolTable::Name(const std::string& rName) {
00046 mMyName = rName;
00047 }
00048
00049
00050 void SymbolTable::Clear(void) {
00051 mMaxIndex=std::numeric_limits<Idx>::max();
00052 mNextIndex=1;
00053 mIndexMap.clear();
00054 mNameMap.clear();
00055 }
00056
00057
00058 Idx SymbolTable::Size(void) {
00059 return mIndexMap.size();
00060 }
00061
00062
00063 Idx SymbolTable::MaxIndex(void) const {
00064 return mMaxIndex;
00065 }
00066
00067
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
00079 Idx SymbolTable::LastIndex(void) const {
00080 return mNextIndex - 1;
00081 }
00082
00083
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
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
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
00116 do {
00117 count++;
00118 name=bname + "_" + ToStringInteger(count);
00119 } while(Exists(name));
00120 return name;
00121 }
00122
00123
00124
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
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
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
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
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
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
00213 Idx SymbolTable::Index(const std::string& rName) const {
00214 std::map<std::string,Idx>::const_iterator it = mIndexMap.find(rName);
00215 if (it == mIndexMap.end())
00216 return 0;
00217 else
00218 return it->second;
00219 }
00220
00221
00222 std::string SymbolTable::Symbol(Idx index) const {
00223 std::map<Idx,std::string>::const_iterator it = mNameMap.find(index);
00224 if (it == mNameMap.end())
00225 return "";
00226 else
00227 return it->second;
00228 }
00229
00230
00231
00232
00233 bool SymbolTable::Exists(Idx index) const {
00234 std::map<Idx,std::string>::const_iterator it = mNameMap.find(index);
00235 return (it != mNameMap.end());
00236 }
00237
00238
00239 bool SymbolTable::Exists(const std::string& rName) const {
00240 std::map<std::string,Idx>::const_iterator it = mIndexMap.find(rName);
00241 return ( it != mIndexMap.end());
00242 }
00243
00244
00245
00246 SymbolTable* SymbolTable::GlobalEventSymbolTablep(void) {
00247 return &msEventSymbolTable;
00248 }
00249
00250
00251
00252 void SymbolTable::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const {
00253 (void) rTw; (void) rLabel; (void) pContext;
00254 FD_DC("SymbolTable::DoWrite(): dummy");
00255 }
00256
00257
00258 void SymbolTable::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
00259 (void) rLabel; (void) pContext; (void) rTr;
00260 FD_DC("SymbolTable::DoRead(): dummy");
00261 }
00262
00263
00264 }