cpp_token.cc

Go to the documentation of this file.
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 C++ token.
00030   
00031   Decfinition of cpp_token class representing C++ token.
00032   \author pt
00033 */
00034 #include <lestes/common.hh>
00035 #include <lestes/std/source_location.hh>
00036 #include <lestes/std/reflect.hh>
00037 #include <lestes/lang/cplus/lex/cpp_token.hh>
00038 #include <lestes/lang/cplus/lex/token_value.hh>
00039 #include <lestes/lang/cplus/lex/lex_literal.g.hh>
00040 
00041 package(lestes);
00042 package(lang);
00043 package(cplus);
00044 package(lex);
00045 
00046 /*!
00047   Creates the object, initializes with location, type and value.
00048   \param a_location  The location of the token.
00049   \param a_type  The type of the token.
00050   \param a_value  The value of the token.
00051   \param a_literal  The literal properties.
00052 */
00053 cpp_token::cpp_token(const location_type &a_location, const type_type &a_type, const value_type &a_value,
00054                 const ptr<lex_literal> &a_literal):
00055         basic_cpp_token(a_location,a_type,a_value),
00056         literal(a_literal)
00057 {
00058 }
00059 
00060 /*!
00061   Returns literal properties.
00062   \return  The information about literal token.
00063 */
00064 ptr<lex_literal> cpp_token::literal_get(void) const
00065 {
00066         lassert(type_get() == TOK_LITERAL);
00067         return literal;
00068 }
00069 
00070 /*!
00071   Tests equality to other token.
00072   Does not test literal, only token value.
00073   \pre other != NULL
00074   \param other  The token to compare to.
00075   \return true  If both token represent the same entity.
00076 */
00077 bool cpp_token::equals(const ptr<cpp_token> &other) const
00078 {
00079         lassert(other);
00080         return is_equal(type_get(),other->type_get()) &&
00081                 is_equal(value_get(),other->value_get()) &&
00082                 is_equal(location_get(),other->location_get());
00083 }
00084 
00085 /*!
00086   Returns new token, initializes with token type.
00087   \pre a_type != TOK_LITERAL
00088   \param a_location  The location of the token.
00089   \param a_type  The type of token (syntactic class).
00090   \return  New C++ token.
00091 */
00092 ptr<cpp_token> cpp_token::create(const location_type &a_location, const type_type &a_type)
00093 {
00094         lassert(a_type != TOK_LITERAL);
00095         return new cpp_token(a_location,a_type,NULL,NULL);
00096 }
00097 
00098 /*!
00099   Returns new token, initializes with token type and value.
00100   \pre a_type != TOK_LITERAL
00101   \param a_type  The type of token (syntactic class).
00102   \param a_location  The location of the token.
00103   \return  New C++ token.
00104 */
00105 ptr<cpp_token> cpp_token::create(const location_type &a_location,
00106                 const type_type &a_type, const value_type &a_value)
00107 {
00108         lassert(a_type != TOK_LITERAL);
00109         return new cpp_token(a_location,a_type,a_value,NULL);
00110 }
00111 
00112 /*!
00113   Returns new literal token, initializes with token type, value and literal properties.
00114   \param a_location  The location of the token.
00115   \param a_literal  The properties of the literal.
00116   \param a_value  The value of the token.
00117   \return  New C++ token.
00118 */
00119 ptr<cpp_token> cpp_token::create_literal(const location_type &a_location,
00120                 const ptr<lex_literal> &a_literal, const value_type &a_value)
00121 {
00122         return new cpp_token(a_location,TOK_LITERAL,a_value,a_literal);
00123 }
00124 
00125 /*!
00126   Returns the internal description of the token.
00127   \return  The string representation of the token type.
00128 */
00129 lstring cpp_token::description_get(void) const
00130 {
00131         if (!descriptions) {
00132                 descriptions = descriptions_type::create();
00133 
00134                 descriptions->insert(::std::make_pair(cpp_token::TOK_EOF,"TOK_EOF"));
00135                 descriptions->insert(::std::make_pair(cpp_token::TOK_LITERAL,"TOK_LITERAL"));
00136                 descriptions->insert(::std::make_pair(cpp_token::TOK_ASM,"TOK_ASM"));
00137                 descriptions->insert(::std::make_pair(cpp_token::TOK_AUTO,"TOK_AUTO"));
00138                 descriptions->insert(::std::make_pair(cpp_token::TOK_BOOL,"TOK_BOOL"));
00139                 descriptions->insert(::std::make_pair(cpp_token::TOK_BREAK,"TOK_BREAK"));
00140                 descriptions->insert(::std::make_pair(cpp_token::TOK_CASE,"TOK_CASE"));
00141                 descriptions->insert(::std::make_pair(cpp_token::TOK_CATCH,"TOK_CATCH"));
00142                 descriptions->insert(::std::make_pair(cpp_token::TOK_CHAR,"TOK_CHAR"));
00143                 descriptions->insert(::std::make_pair(cpp_token::TOK_CLASS,"TOK_CLASS"));
00144                 descriptions->insert(::std::make_pair(cpp_token::TOK_CONST,"TOK_CONST"));
00145                 descriptions->insert(::std::make_pair(cpp_token::TOK_CONST_CAST,"TOK_CONST_CAST"));
00146                 descriptions->insert(::std::make_pair(cpp_token::TOK_CONTINUE,"TOK_CONTINUE"));
00147                 descriptions->insert(::std::make_pair(cpp_token::TOK_DEFAULT,"TOK_DEFAULT"));
00148                 descriptions->insert(::std::make_pair(cpp_token::TOK_DELETE,"TOK_DELETE"));
00149                 descriptions->insert(::std::make_pair(cpp_token::TOK_DO,"TOK_DO"));
00150                 descriptions->insert(::std::make_pair(cpp_token::TOK_DOUBLE,"TOK_DOUBLE"));
00151                 descriptions->insert(::std::make_pair(cpp_token::TOK_DYNAMIC_CAST,"TOK_DYNAMIC_CAST"));
00152                 descriptions->insert(::std::make_pair(cpp_token::TOK_ELSE,"TOK_ELSE"));
00153                 descriptions->insert(::std::make_pair(cpp_token::TOK_ENUM,"TOK_ENUM"));
00154                 descriptions->insert(::std::make_pair(cpp_token::TOK_EXPLICIT,"TOK_EXPLICIT"));
00155                 descriptions->insert(::std::make_pair(cpp_token::TOK_EXPORT,"TOK_EXPORT"));
00156                 descriptions->insert(::std::make_pair(cpp_token::TOK_EXTERN,"TOK_EXTERN"));
00157                 descriptions->insert(::std::make_pair(cpp_token::TOK_FLOAT,"TOK_FLOAT"));
00158                 descriptions->insert(::std::make_pair(cpp_token::TOK_FOR,"TOK_FOR"));
00159                 descriptions->insert(::std::make_pair(cpp_token::TOK_FRIEND,"TOK_FRIEND"));
00160                 descriptions->insert(::std::make_pair(cpp_token::TOK_GOTO,"TOK_GOTO"));
00161                 descriptions->insert(::std::make_pair(cpp_token::TOK_IF,"TOK_IF"));
00162                 descriptions->insert(::std::make_pair(cpp_token::TOK_INLINE,"TOK_INLINE"));
00163                 descriptions->insert(::std::make_pair(cpp_token::TOK_INT,"TOK_INT"));
00164                 descriptions->insert(::std::make_pair(cpp_token::TOK_LONG,"TOK_LONG"));
00165                 descriptions->insert(::std::make_pair(cpp_token::TOK_MUTABLE,"TOK_MUTABLE"));
00166                 descriptions->insert(::std::make_pair(cpp_token::TOK_NAMESPACE,"TOK_NAMESPACE"));
00167                 descriptions->insert(::std::make_pair(cpp_token::TOK_NEW,"TOK_NEW"));
00168                 descriptions->insert(::std::make_pair(cpp_token::TOK_OPERATOR,"TOK_OPERATOR"));
00169                 descriptions->insert(::std::make_pair(cpp_token::TOK_PRIVATE,"TOK_PRIVATE"));
00170                 descriptions->insert(::std::make_pair(cpp_token::TOK_PROTECTED,"TOK_PROTECTED"));
00171                 descriptions->insert(::std::make_pair(cpp_token::TOK_PUBLIC,"TOK_PUBLIC"));
00172                 descriptions->insert(::std::make_pair(cpp_token::TOK_REGISTER,"TOK_REGISTER"));
00173                 descriptions->insert(::std::make_pair(cpp_token::TOK_REINTERPRET_CAST,"TOK_REINTERPRET_CAST"));
00174                 descriptions->insert(::std::make_pair(cpp_token::TOK_RETURN,"TOK_RETURN"));
00175                 descriptions->insert(::std::make_pair(cpp_token::TOK_SHORT,"TOK_SHORT"));
00176                 descriptions->insert(::std::make_pair(cpp_token::TOK_SIGNED,"TOK_SIGNED"));
00177                 descriptions->insert(::std::make_pair(cpp_token::TOK_SIZEOF,"TOK_SIZEOF"));
00178                 descriptions->insert(::std::make_pair(cpp_token::TOK_STATIC,"TOK_STATIC"));
00179                 descriptions->insert(::std::make_pair(cpp_token::TOK_STATIC_CAST,"TOK_STATIC_CAST"));
00180                 descriptions->insert(::std::make_pair(cpp_token::TOK_STRUCT,"TOK_STRUCT"));
00181                 descriptions->insert(::std::make_pair(cpp_token::TOK_SWITCH,"TOK_SWITCH"));
00182                 descriptions->insert(::std::make_pair(cpp_token::TOK_TEMPLATE,"TOK_TEMPLATE"));
00183                 descriptions->insert(::std::make_pair(cpp_token::TOK_THIS,"TOK_THIS"));
00184                 descriptions->insert(::std::make_pair(cpp_token::TOK_THROW,"TOK_THROW"));
00185                 descriptions->insert(::std::make_pair(cpp_token::TOK_TRY,"TOK_TRY"));
00186                 descriptions->insert(::std::make_pair(cpp_token::TOK_TYPEDEF,"TOK_TYPEDEF"));
00187                 descriptions->insert(::std::make_pair(cpp_token::TOK_TYPEID,"TOK_TYPEID"));
00188                 descriptions->insert(::std::make_pair(cpp_token::TOK_TYPENAME,"TOK_TYPENAME"));
00189                 descriptions->insert(::std::make_pair(cpp_token::TOK_UNION,"TOK_UNION"));
00190                 descriptions->insert(::std::make_pair(cpp_token::TOK_UNSIGNED,"TOK_UNSIGNED"));
00191                 descriptions->insert(::std::make_pair(cpp_token::TOK_USING,"TOK_USING"));
00192                 descriptions->insert(::std::make_pair(cpp_token::TOK_VIRTUAL,"TOK_VIRTUAL"));
00193                 descriptions->insert(::std::make_pair(cpp_token::TOK_VOID,"TOK_VOID"));
00194                 descriptions->insert(::std::make_pair(cpp_token::TOK_VOLATILE,"TOK_VOLATILE"));
00195                 descriptions->insert(::std::make_pair(cpp_token::TOK_WCHAR_T,"TOK_WCHAR_T"));
00196                 descriptions->insert(::std::make_pair(cpp_token::TOK_WHILE,"TOK_WHILE"));
00197                 descriptions->insert(::std::make_pair(cpp_token::TOK_IDENT,"TOK_IDENT"));
00198                 descriptions->insert(::std::make_pair(cpp_token::TOK_RIGHT_BRACKET,"TOK_RIGHT_BRACKET"));
00199                 descriptions->insert(::std::make_pair(cpp_token::TOK_LEFT_BRACKET,"TOK_LEFT_BRACKET"));
00200                 descriptions->insert(::std::make_pair(cpp_token::TOK_EXCLAMATION,"TOK_EXCLAMATION"));
00201                 descriptions->insert(::std::make_pair(cpp_token::TOK_TILDE,"TOK_TILDE"));
00202                 descriptions->insert(::std::make_pair(cpp_token::TOK_PERCENT,"TOK_PERCENT"));
00203                 descriptions->insert(::std::make_pair(cpp_token::TOK_SLASH,"TOK_SLASH"));
00204                 descriptions->insert(::std::make_pair(cpp_token::TOK_STAR,"TOK_STAR"));
00205                 descriptions->insert(::std::make_pair(cpp_token::TOK_MINUS,"TOK_MINUS"));
00206                 descriptions->insert(::std::make_pair(cpp_token::TOK_PLUS,"TOK_PLUS"));
00207                 descriptions->insert(::std::make_pair(cpp_token::TOK_GT,"TOK_GT"));
00208                 descriptions->insert(::std::make_pair(cpp_token::TOK_LT,"TOK_LT"));
00209                 descriptions->insert(::std::make_pair(cpp_token::TOK_AMP,"TOK_AMP"));
00210                 descriptions->insert(::std::make_pair(cpp_token::TOK_HAT,"TOK_HAT"));
00211                 descriptions->insert(::std::make_pair(cpp_token::TOK_VBAR,"TOK_VBAR"));
00212                 descriptions->insert(::std::make_pair(cpp_token::TOK_QMARK,"TOK_QMARK"));
00213                 descriptions->insert(::std::make_pair(cpp_token::TOK_COMMA,"TOK_COMMA"));
00214                 descriptions->insert(::std::make_pair(cpp_token::TOK_RIGHT_BRACE,"TOK_RIGHT_BRACE"));
00215                 descriptions->insert(::std::make_pair(cpp_token::TOK_LEFT_BRACE,"TOK_LEFT_BRACE"));
00216                 descriptions->insert(::std::make_pair(cpp_token::TOK_SEMICOLON,"TOK_SEMICOLON"));
00217                 descriptions->insert(::std::make_pair(cpp_token::TOK_RIGHT_PAR,"TOK_RIGHT_PAR"));
00218                 descriptions->insert(::std::make_pair(cpp_token::TOK_LEFT_PAR,"TOK_LEFT_PAR"));
00219                 descriptions->insert(::std::make_pair(cpp_token::TOK_EQ,"TOK_EQ"));
00220                 descriptions->insert(::std::make_pair(cpp_token::TOK_COLON,"TOK_COLON"));
00221                 descriptions->insert(::std::make_pair(cpp_token::TOK_DOT_DOT_DOT,"TOK_DOT_DOT_DOT"));
00222                 descriptions->insert(::std::make_pair(cpp_token::TOK_MINUS_GT,"TOK_MINUS_GT"));
00223                 descriptions->insert(::std::make_pair(cpp_token::TOK_MINUS_GT_STAR,"TOK_MINUS_GT_STAR"));
00224                 descriptions->insert(::std::make_pair(cpp_token::TOK_DOT_STAR,"TOK_DOT_STAR"));
00225                 descriptions->insert(::std::make_pair(cpp_token::TOK_GT_EQ,"TOK_GT_EQ"));
00226                 descriptions->insert(::std::make_pair(cpp_token::TOK_LT_EQ,"TOK_LT_EQ"));
00227                 descriptions->insert(::std::make_pair(cpp_token::TOK_EXCLAMATION_EQ,"TOK_EXCLAMATION_EQ"));
00228                 descriptions->insert(::std::make_pair(cpp_token::TOK_EQ_EQ,"TOK_EQ_EQ"));
00229                 descriptions->insert(::std::make_pair(cpp_token::TOK_AMP_AMP,"TOK_AMP_AMP"));
00230                 descriptions->insert(::std::make_pair(cpp_token::TOK_VBAR_VBAR,"TOK_VBAR_VBAR"));
00231                 descriptions->insert(::std::make_pair(cpp_token::TOK_GT_GT,"TOK_GT_GT"));
00232                 descriptions->insert(::std::make_pair(cpp_token::TOK_LT_LT,"TOK_LT_LT"));
00233                 descriptions->insert(::std::make_pair(cpp_token::TOK_MINUS_MINUS,"TOK_MINUS_MINUS"));
00234                 descriptions->insert(::std::make_pair(cpp_token::TOK_PLUS_PLUS,"TOK_PLUS_PLUS"));
00235                 descriptions->insert(::std::make_pair(cpp_token::TOK_LT_LT_EQ,"TOK_LT_LT_EQ"));
00236                 descriptions->insert(::std::make_pair(cpp_token::TOK_GT_GT_EQ,"TOK_GT_GT_EQ"));
00237                 descriptions->insert(::std::make_pair(cpp_token::TOK_VBAR_EQ,"TOK_VBAR_EQ"));
00238                 descriptions->insert(::std::make_pair(cpp_token::TOK_AMP_EQ,"TOK_AMP_EQ"));
00239                 descriptions->insert(::std::make_pair(cpp_token::TOK_HAT_EQ,"TOK_HAT_EQ"));
00240                 descriptions->insert(::std::make_pair(cpp_token::TOK_PERCENT_EQ,"TOK_PERCENT_EQ"));
00241                 descriptions->insert(::std::make_pair(cpp_token::TOK_SLASH_EQ,"TOK_SLASH_EQ"));
00242                 descriptions->insert(::std::make_pair(cpp_token::TOK_STAR_EQ,"TOK_STAR_EQ"));
00243                 descriptions->insert(::std::make_pair(cpp_token::TOK_MINUS_EQ,"TOK_MINUS_EQ"));
00244                 descriptions->insert(::std::make_pair(cpp_token::TOK_PLUS_EQ,"TOK_PLUS_EQ"));
00245                 descriptions->insert(::std::make_pair(cpp_token::TOK_COLON_COLON,"TOK_COLON_COLON"));
00246         }
00247 
00248         descriptions_type::iterator it = descriptions->find(type_get());
00249         lassert(it != descriptions->end());
00250         return it->second;
00251 }
00252 
00253 /*!
00254   Marks the object.
00255 */
00256 void cpp_token::gc_mark(void)
00257 {
00258         literal.gc_mark();
00259         basic_cpp_token::gc_mark();
00260 }
00261 
00262 /*!
00263   Returns reflection list for cpp_token.
00264   \return The reflection list.
00265 */
00266 ptr< object::reflection_list > cpp_token::reflection_get(void) const
00267 {
00268         if (!reflection) {
00269                 typedef class_reflection::field_metadata md;
00270                 typedef class_reflection::field_metadata_list mdlist;
00271                 ptr<mdlist> mdl = mdlist::create();
00272                 mdl->push_back(md::create("type","lstring"));
00273                 mdl->push_back(md::create("value","ucn_string"));
00274                 reflection = reflection_list::create(object::reflection_get());
00275                 reflection->push_back(class_reflection::create("cpp_token",mdl));
00276         }
00277         return reflection;
00278 }
00279 
00280 /*!
00281   Returns values of the fields.
00282   \return List of field values.
00283 */
00284 ptr< object::field_list_list > cpp_token::field_values_get(void) const
00285 {
00286         ptr<field_list_list> result = object::field_values_get();
00287         result->push_back(value_list::create());
00288         result->back()->push_back(objectize<lstring>::create(description_get()));
00289         result->push_back(value_list::create());
00290         ptr<token_value> tv = value_get();
00291         if (tv) {
00292                 result->back()->push_back(objectize<ucn_string>::create(tv->content_get()));
00293         } else {
00294                 result->back()->push_back(NULL);
00295         }
00296         return result;
00297 }
00298 
00299 /*!
00300   Descriptions of tokens.
00301 */
00302 ptr<cpp_token::descriptions_type> cpp_token::descriptions = descriptions;
00303 
00304 /*!
00305   Reflection list for token.
00306 */
00307 ptr<object::reflection_list> cpp_token::reflection = reflection;
00308 
00309 end_package(lex);
00310 end_package(cplus);
00311 end_package(lang);
00312 end_package(lestes);
00313 /* vim: set ft=lestes : */

Generated on Mon Feb 12 18:22:33 2007 for lestes by doxygen 1.5.1-20070107