00001 /* 00002 The lestes compiler suite 00003 Copyright (C) 2002, 2003, 2004, 2005 Miroslav Tichy 00004 Copyright (C) 2002, 2003, 2004, 2005 Petr Zika 00005 Copyright (C) 2002, 2003, 2004, 2005 Vojtech Hala 00006 Copyright (C) 2002, 2003, 2004, 2005 Jiri Kosina 00007 Copyright (C) 2002, 2003, 2004, 2005 Pavel Sanda 00008 Copyright (C) 2002, 2003, 2004, 2005 Jan Zouhar 00009 Copyright (C) 2002, 2003, 2004, 2005 Rudolf Thomas 00010 00011 This program is free software; you can redistribute it and/or modify 00012 it under the terms of the GNU General Public License as published by 00013 the Free Software Foundation; version 2 of the License. 00014 00015 This program is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 GNU General Public License for more details. 00019 00020 See the full text of the GNU General Public License version 2, and 00021 the limitations in the file doc/LICENSE. 00022 00023 By accepting the license the licensee waives any and all claims 00024 against the copyright holder(s) related in whole or in part to the 00025 work, its use, and/or the inability to use it. 00026 00027 */ 00028 /*! \file 00029 \brief Taboo macros. 00030 00031 Definition of taboo_macros class representing set of disallowed macros. 00032 \author pt 00033 */ 00034 #include <lestes/common.hh> 00035 #include <lestes/lang/cplus/lex/taboo_macros.hh> 00036 #include <lestes/lang/cplus/lex/macro.hh> 00037 #include <lestes/lang/cplus/lex/token_value.hh> 00038 00039 #include <iostream> 00040 00041 package(lestes); 00042 package(lang); 00043 package(cplus); 00044 package(lex); 00045 00046 using namespace ::std; 00047 00048 /*! 00049 Creates new empty set. 00050 \post taboo->length() == 0 00051 \post shared->length() == 0 00052 */ 00053 taboo_macros::taboo_macros(void): 00054 taboo(taboo_type::create()), 00055 shared(shared_type::create()) 00056 { 00057 } 00058 00059 /*! 00060 Creates copy of set, adding a new macro. 00061 \pre source != NULL 00062 \post taboo->length() == source->taboo->length() + (source->contains(a_macro) ? 0 : 1) 00063 \post shared->length() == 0 00064 \param source The source object to copy. 00065 \param a_macro The macro to insert into new object's set. 00066 */ 00067 taboo_macros::taboo_macros(const ptr<taboo_macros> &source, const ptr<macro> &a_macro): 00068 taboo(taboo_type::create()), 00069 shared(shared_type::create()) 00070 { 00071 lassert(source); 00072 lassert(a_macro); 00073 00074 // copy the content 00075 // TODO pt make more elegant 00076 // ??? bad bad coder copy(source->taboo->begin(),source->taboo->end(),inserter(*taboo)); 00077 00078 for (taboo_type::iterator it = source->taboo->begin(), 00079 end = source->taboo->end(); it != end; ++it) { 00080 taboo->insert(*it); 00081 } 00082 00083 // add the new macro 00084 taboo->insert(a_macro); 00085 /* TODO pt ::std::cerr << "inserted taboo " << a_macro->name_get()->content_get() << " :"; 00086 00087 for (taboo_type::iterator it = taboo->begin(), 00088 end = taboo->end(); it != end; ++it) { 00089 ::std::cerr << ' ' << (*it)->name_get()->content_get(); 00090 } 00091 00092 ::std::cerr << (taboo->find(a_macro) != taboo->end() ? "found" : "BOOM") << '\n'; 00093 */ 00094 } 00095 00096 /*! 00097 Returns empty taboo set. 00098 \post taboo->length() == 0 00099 \return Instance containing no macros. 00100 */ 00101 ptr<taboo_macros> taboo_macros::create(void) 00102 { 00103 if (!empty_instance) { 00104 empty_instance = new taboo_macros(); 00105 } 00106 return empty_instance; 00107 } 00108 00109 /*! 00110 Tests whether the taboo set contains a macro. 00111 \param a_macro The macro to test. 00112 \return true If the macro is contained in the set. 00113 */ 00114 00115 bool taboo_macros::contains(const ptr<macro> &a_macro) const 00116 { 00117 return taboo->find(a_macro) != taboo->end(); 00118 } 00119 00120 /*! 00121 Returns new taboo set with added macro. 00122 \pre a_macro != NULL 00123 \param a_macro The macro to be added to the new taboo. 00124 \return New object with set extended by a_macro. 00125 */ 00126 ptr<taboo_macros> taboo_macros::extend(const ptr<macro> &a_macro) const 00127 { 00128 lassert(a_macro); 00129 00130 // macro is already in this instance 00131 if (contains(a_macro)) return ptr<taboo_macros>(const_cast<taboo_macros *>(this)); 00132 00133 // lookup the cache of extensions 00134 shared_type::iterator it = shared->find(a_macro); 00135 // return shared instance 00136 if (it != shared->end()) return (*it).second; 00137 00138 // call special "copy and extend" constructor 00139 ptr<taboo_macros> nju = ptr<taboo_macros>( 00140 new taboo_macros(ptr<taboo_macros>(const_cast<taboo_macros *>(this)),a_macro)); 00141 00142 // record into cache 00143 shared->insert(make_pair(a_macro,nju)); 00144 00145 return nju; 00146 } 00147 00148 /*! 00149 Marks the object. 00150 */ 00151 void taboo_macros::gc_mark(void) 00152 { 00153 taboo.gc_mark(); 00154 shared.gc_mark(); 00155 ::lestes::std::object::gc_mark(); 00156 } 00157 00158 ucn_string taboo_macros::names_get(void) const 00159 { 00160 ucn_string u; 00161 00162 u += character::create_from_host('{'); 00163 00164 for (taboo_type::iterator it = taboo->begin(), 00165 end = taboo->end(); it != end; ++it) { 00166 u += character::create_from_host(' '); 00167 u += (*it)->name_get()->content_get(); 00168 } 00169 00170 u += character::create_from_host('}'); 00171 return u; 00172 } 00173 00174 /*! 00175 The shared empty instance of taboo macros. 00176 Used for fast initialization of tokens. 00177 */ 00178 ptr<taboo_macros> taboo_macros::empty_instance; 00179 00180 end_package(lex); 00181 end_package(cplus); 00182 end_package(lang); 00183 end_package(lestes); 00184 /* vim: set ft=lestes : */
1.5.1-20070107