pp_token.hh

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 #ifndef lestes__lang__cplus__lex___pp_token_hh___included
00029 #define lestes__lang__cplus__lex___pp_token_hh___included
00030 
00031 /*! \file
00032   \brief Preprocessor token.
00033 
00034   Declaration of pp_token class representing preprocessor token.
00035   \author pt
00036 */
00037 #include <lestes/common.hh>
00038 #include <lestes/std/ucn_string.hh>
00039 #include <lestes/lang/cplus/lex/basic_token.hh>
00040 
00041 package(lestes);
00042 
00043 package(std);
00044 // forward declaration to avoid cycle
00045 class source_location;
00046 end_package(std);
00047 
00048 package(lang);
00049 package(cplus);
00050 package(lex);
00051 
00052 // forward declaration to avoid cycle
00053 class taboo_macros;
00054 class token_value;
00055 
00056 /*!
00057   Preprocessing tokens list definition.
00058   Each entry is of the form
00059   DEF_PP_TOKEN(symbolic name, token spelling, alternative spelling, flags).
00060   The entries act as function-like macro calls, that are redefined to select one of the items.
00061 */
00062 #define PP_TOKENS_LIST \
00063 DEF_PP_TOKEN(FILE_END,"","",FLG_NONE)\
00064 DEF_PP_TOKEN(TERMINATOR,"","",FLG_NONE)\
00065 DEF_PP_TOKEN(LEFT_BRACKET,"[","<:",FLG_VARIANT)\
00066 DEF_PP_TOKEN(RIGHT_BRACKET,"]",":>",FLG_VARIANT)\
00067 DEF_PP_TOKEN(LEFT_BRACE,"{","<%",FLG_VARIANT)\
00068 DEF_PP_TOKEN(RIGHT_BRACE,"}","%>",FLG_VARIANT)\
00069 DEF_PP_TOKEN(LEFT_PAR,"(","",FLG_NONE)\
00070 DEF_PP_TOKEN(RIGHT_PAR,")","",FLG_NONE)\
00071 DEF_PP_TOKEN(SEMICOLON,";","",FLG_NONE)\
00072 DEF_PP_TOKEN(COLON,":","",FLG_NONE)\
00073 DEF_PP_TOKEN(QMARK,"?","",FLG_NONE)\
00074 DEF_PP_TOKEN(DOT,".","",FLG_NONE)\
00075 DEF_PP_TOKEN(PLUS,"+","",FLG_NONE)\
00076 DEF_PP_TOKEN(STAR,"*","",FLG_NONE)\
00077 DEF_PP_TOKEN(PERCENT,"%","",FLG_NONE)\
00078 DEF_PP_TOKEN(SLASH,"/","",FLG_NONE)\
00079 DEF_PP_TOKEN(HAT,"^","xor",FLG_VARIANT)\
00080 DEF_PP_TOKEN(AMP,"&","bitand",FLG_VARIANT)\
00081 DEF_PP_TOKEN(VBAR,"|","bitor",FLG_VARIANT)\
00082 DEF_PP_TOKEN(TILDE,"~","compl",FLG_VARIANT)\
00083 DEF_PP_TOKEN(EMARK,"!","not",FLG_VARIANT)\
00084 DEF_PP_TOKEN(EQ,"=","",FLG_NONE)\
00085 DEF_PP_TOKEN(LT,"<","",FLG_NONE)\
00086 DEF_PP_TOKEN(GT,">","",FLG_NONE)\
00087 DEF_PP_TOKEN(COMMA,",","",FLG_NONE)\
00088 DEF_PP_TOKEN(MINUS,"-","",FLG_NONE)\
00089 DEF_PP_TOKEN(DOT_DOT_DOT,"...","",FLG_NONE)\
00090 DEF_PP_TOKEN(COLON_COLON,"::","",FLG_NONE)\
00091 DEF_PP_TOKEN(DOT_STAR,".*","",FLG_NONE)\
00092 DEF_PP_TOKEN(PLUS_EQ,"+=","",FLG_NONE)\
00093 DEF_PP_TOKEN(MINUS_EQ,"-=","",FLG_NONE)\
00094 DEF_PP_TOKEN(STAR_EQ,"*=","",FLG_NONE)\
00095 DEF_PP_TOKEN(SLASH_EQ,"/=","",FLG_NONE)\
00096 DEF_PP_TOKEN(PERCENT_EQ,"%=","",FLG_NONE)\
00097 DEF_PP_TOKEN(HAT_EQ,"^=","xor_eq",FLG_VARIANT)\
00098 DEF_PP_TOKEN(AMP_EQ,"&=","and_eq",FLG_VARIANT)\
00099 DEF_PP_TOKEN(VBAR_EQ,"|=","or_eq",FLG_VARIANT)\
00100 DEF_PP_TOKEN(LT_LT,"<<","",FLG_NONE)\
00101 DEF_PP_TOKEN(GT_GT,">>","",FLG_NONE)\
00102 DEF_PP_TOKEN(LT_LT_EQ,"<<=","",FLG_NONE)\
00103 DEF_PP_TOKEN(GT_GT_EQ,">>=","",FLG_NONE)\
00104 DEF_PP_TOKEN(EQ_EQ,"==","",FLG_NONE)\
00105 DEF_PP_TOKEN(EMARK_EQ,"!=","not_eq",FLG_VARIANT)\
00106 DEF_PP_TOKEN(LT_EQ,"<=","",FLG_NONE)\
00107 DEF_PP_TOKEN(GT_EQ,">=","",FLG_NONE)\
00108 DEF_PP_TOKEN(AMP_AMP,"&&","and",FLG_VARIANT)\
00109 DEF_PP_TOKEN(VBAR_VBAR,"||","or",FLG_VARIANT)\
00110 DEF_PP_TOKEN(PLUS_PLUS,"++","",FLG_NONE)\
00111 DEF_PP_TOKEN(MINUS_MINUS,"--","",FLG_NONE)\
00112 DEF_PP_TOKEN(MINUS_GT_STAR,"->*","",FLG_NONE)\
00113 DEF_PP_TOKEN(MINUS_GT,"->","",FLG_NONE)\
00114 DEF_PP_TOKEN(ASM,"asm","",FLG_NAME | FLG_INTERNAL)\
00115 DEF_PP_TOKEN(AUTO,"auto","",FLG_NAME | FLG_INTERNAL)\
00116 DEF_PP_TOKEN(BOOL,"bool","",FLG_NAME | FLG_INTERNAL)\
00117 DEF_PP_TOKEN(BREAK,"break","",FLG_NAME | FLG_INTERNAL)\
00118 DEF_PP_TOKEN(CASE,"case","",FLG_NAME | FLG_INTERNAL)\
00119 DEF_PP_TOKEN(CATCH,"catch","",FLG_NAME | FLG_INTERNAL)\
00120 DEF_PP_TOKEN(CHAR,"char","",FLG_NAME | FLG_INTERNAL)\
00121 DEF_PP_TOKEN(CLASS,"class","",FLG_NAME | FLG_INTERNAL)\
00122 DEF_PP_TOKEN(CONST,"const","",FLG_NAME | FLG_INTERNAL)\
00123 DEF_PP_TOKEN(CONST_CAST,"const_cast","",FLG_NAME | FLG_INTERNAL)\
00124 DEF_PP_TOKEN(CONTINUE,"continue","",FLG_NAME | FLG_INTERNAL)\
00125 DEF_PP_TOKEN(DEFAULT,"default","",FLG_NAME | FLG_INTERNAL)\
00126 DEF_PP_TOKEN(DELETE,"delete","",FLG_NAME | FLG_INTERNAL)\
00127 DEF_PP_TOKEN(DO,"do","",FLG_NAME | FLG_INTERNAL)\
00128 DEF_PP_TOKEN(DOUBLE,"double","",FLG_NAME | FLG_INTERNAL)\
00129 DEF_PP_TOKEN(DYNAMIC_CAST,"dynamic_cast","",FLG_NAME | FLG_INTERNAL)\
00130 DEF_PP_TOKEN(ELSE,"else","",FLG_NAME | FLG_INTERNAL)\
00131 DEF_PP_TOKEN(ENUM,"enum","",FLG_NAME | FLG_INTERNAL)\
00132 DEF_PP_TOKEN(EXPLICIT,"explicit","",FLG_NAME | FLG_INTERNAL)\
00133 DEF_PP_TOKEN(EXPORT,"export","",FLG_NAME | FLG_INTERNAL)\
00134 DEF_PP_TOKEN(EXTERN,"extern","",FLG_NAME | FLG_INTERNAL)\
00135 DEF_PP_TOKEN(FLOAT,"float","",FLG_NAME | FLG_INTERNAL)\
00136 DEF_PP_TOKEN(FOR,"for","",FLG_NAME | FLG_INTERNAL)\
00137 DEF_PP_TOKEN(FRIEND,"friend","",FLG_NAME | FLG_INTERNAL)\
00138 DEF_PP_TOKEN(GOTO,"goto","",FLG_NAME | FLG_INTERNAL)\
00139 DEF_PP_TOKEN(IF,"if","",FLG_NAME | FLG_INTERNAL)\
00140 DEF_PP_TOKEN(INLINE,"inline","",FLG_NAME | FLG_INTERNAL)\
00141 DEF_PP_TOKEN(INT,"int","",FLG_NAME | FLG_INTERNAL)\
00142 DEF_PP_TOKEN(LONG,"long","",FLG_NAME | FLG_INTERNAL)\
00143 DEF_PP_TOKEN(MUTABLE,"mutable","",FLG_NAME | FLG_INTERNAL)\
00144 DEF_PP_TOKEN(NAMESPACE,"namespace","",FLG_NAME | FLG_INTERNAL)\
00145 DEF_PP_TOKEN(NEW,"new","",FLG_NAME | FLG_INTERNAL)\
00146 DEF_PP_TOKEN(OPERATOR,"operator","",FLG_NAME | FLG_INTERNAL)\
00147 DEF_PP_TOKEN(PRIVATE,"private","",FLG_NAME | FLG_INTERNAL)\
00148 DEF_PP_TOKEN(PROTECTED,"protected","",FLG_NAME | FLG_INTERNAL)\
00149 DEF_PP_TOKEN(PUBLIC,"public","",FLG_NAME | FLG_INTERNAL)\
00150 DEF_PP_TOKEN(REGISTER,"register","",FLG_NAME | FLG_INTERNAL)\
00151 DEF_PP_TOKEN(REINTERPRET_CAST,"reinterpret_cast","",FLG_NAME | FLG_INTERNAL)\
00152 DEF_PP_TOKEN(RETURN,"return","",FLG_NAME | FLG_INTERNAL)\
00153 DEF_PP_TOKEN(SHORT,"short","",FLG_NAME | FLG_INTERNAL)\
00154 DEF_PP_TOKEN(SIGNED,"signed","",FLG_NAME | FLG_INTERNAL)\
00155 DEF_PP_TOKEN(SIZEOF,"sizeof","",FLG_NAME | FLG_INTERNAL)\
00156 DEF_PP_TOKEN(STATIC,"static","",FLG_NAME | FLG_INTERNAL)\
00157 DEF_PP_TOKEN(STATIC_CAST,"static_cast","",FLG_NAME | FLG_INTERNAL)\
00158 DEF_PP_TOKEN(STRUCT,"struct","",FLG_NAME | FLG_INTERNAL)\
00159 DEF_PP_TOKEN(SWITCH,"switch","",FLG_NAME | FLG_INTERNAL)\
00160 DEF_PP_TOKEN(TEMPLATE,"template","",FLG_NAME | FLG_INTERNAL)\
00161 DEF_PP_TOKEN(THIS,"this","",FLG_NAME | FLG_INTERNAL)\
00162 DEF_PP_TOKEN(THROW,"throw","",FLG_NAME | FLG_INTERNAL)\
00163 DEF_PP_TOKEN(TRY,"try","",FLG_NAME | FLG_INTERNAL)\
00164 DEF_PP_TOKEN(TYPEDEF,"typedef","",FLG_NAME | FLG_INTERNAL)\
00165 DEF_PP_TOKEN(TYPEID,"typeid","",FLG_NAME | FLG_INTERNAL)\
00166 DEF_PP_TOKEN(TYPENAME,"typename","",FLG_NAME | FLG_INTERNAL)\
00167 DEF_PP_TOKEN(UNION,"union","",FLG_NAME | FLG_INTERNAL)\
00168 DEF_PP_TOKEN(UNSIGNED,"unsigned","",FLG_NAME | FLG_INTERNAL)\
00169 DEF_PP_TOKEN(USING,"using","",FLG_NAME | FLG_INTERNAL)\
00170 DEF_PP_TOKEN(VIRTUAL,"virtual","",FLG_NAME | FLG_INTERNAL)\
00171 DEF_PP_TOKEN(VOID,"void","",FLG_NAME | FLG_INTERNAL)\
00172 DEF_PP_TOKEN(VOLATILE,"volatile","",FLG_NAME | FLG_INTERNAL)\
00173 DEF_PP_TOKEN(WCHAR,"wchar_t","",FLG_NAME | FLG_INTERNAL)\
00174 DEF_PP_TOKEN(WHILE,"while","",FLG_NAME | FLG_INTERNAL)\
00175 DEF_PP_TOKEN(BOOL_LIT,"","",FLG_NAME | FLG_EXTERNAL)\
00176 DEF_PP_TOKEN(CHAR_LIT,"","",FLG_EXTERNAL)\
00177 DEF_PP_TOKEN(WCHAR_LIT,"","",FLG_EXTERNAL)\
00178 DEF_PP_TOKEN(STRING_LIT,"","",FLG_EXTERNAL)\
00179 DEF_PP_TOKEN(WSTRING_LIT,"","",FLG_EXTERNAL)\
00180 DEF_PP_TOKEN(NUMBER_LIT,"","",FLG_EXTERNAL)\
00181 DEF_PP_TOKEN(IDENT,"","",FLG_NAME | FLG_EXTERNAL)\
00182 DEF_PP_TOKEN(HASH,"#","%:",FLG_VARIANT)\
00183 DEF_PP_TOKEN(HASH_HASH,"##","%:%:",FLG_VARIANT)\
00184 DEF_PP_TOKEN(BLANK," ","",FLG_NONE)\
00185 DEF_PP_TOKEN(LINE_END,"\n","",FLG_NONE)\
00186 DEF_PP_TOKEN(OTHER,"","",FLG_EXTERNAL)\
00187 DEF_PP_TOKEN(INCLUDE,"include","",FLG_NAME)\
00188 DEF_PP_TOKEN(DEFINE,"define","",FLG_NAME)\
00189 DEF_PP_TOKEN(UNDEF,"undef","",FLG_NAME)\
00190 DEF_PP_TOKEN(LINE,"line","",FLG_NAME)\
00191 DEF_PP_TOKEN(PRAGMA,"pragma","",FLG_NAME)\
00192 DEF_PP_TOKEN(ERROR,"error","",FLG_NAME)\
00193 DEF_PP_TOKEN(DEFINED,"defined","",FLG_NAME)\
00194 DEF_PP_TOKEN(IFDEF,"ifdef","",FLG_NAME)\
00195 DEF_PP_TOKEN(IFNDEF,"ifndef","",FLG_NAME)\
00196 DEF_PP_TOKEN(ELIF,"elif","",FLG_NAME)\
00197 DEF_PP_TOKEN(ENDIF,"endif","",FLG_NAME)\
00198 DEF_PP_TOKEN(INCL_HCHAR,"","",FLG_EXTERNAL)\
00199 DEF_PP_TOKEN(INCL_QCHAR,"","",FLG_EXTERNAL)\
00200 DEF_PP_TOKEN(INCL_SIG,"","",FLG_NONE)\
00201 DEF_PP_TOKEN(LAST,"","",FLG_NONE)
00202 
00203 //! Defines basic preprocessor token.
00204 typedef basic_token<lc_host_uint_least16,ptr<source_location>, ptr<token_value> > basic_pp_token;
00205 
00206 /*!
00207   \brief Preprocessor token.
00208 
00209   Represents preprocessor token. Each token has type and location, literals and
00210   names also have value. Token type properties are represented by internal flags.
00211 */
00212 class pp_token:
00213         public basic_pp_token {
00214 public:
00215         //! Token can be macro name.
00216         bool is_name(void) const;
00217         //! Token has alternative spelling.
00218         bool is_alternative(void) const;
00219         //! Token has value.
00220         bool is_valued(void) const;
00221         //! Tests equality.
00222         bool equals(const ptr<pp_token> &other) const;
00223         //! Tests congruence.
00224         bool congruent(const ptr<pp_token> &other) const;
00225         //! Returns description.
00226         lstring description_get(void) const;
00227         //! Returns token spelling.
00228         ucn_string spelling_get(void) const;
00229         //! Returns taboo macros.
00230         ptr<taboo_macros> taboo_get(void) const;
00231         //! Returns copy of token with different location.
00232         ptr<pp_token> clone(const location_type &a_location) const;
00233         //! Returns copy of token with different taboo macros.
00234         ptr<pp_token> clone(const ptr<taboo_macros> &a_taboo) const;
00235         //! Returns new token, initializes with location and type.
00236         static ptr<pp_token> create(const location_type &a_location, const type_type &a_type);
00237         //! Returns new alternative token, initializes with location and type.
00238         static ptr<pp_token> create_alternative(const location_type &a_location, const type_type &a_type);
00239         //! Returns new token, initializes with location, type and value.
00240         static ptr<pp_token> create(const location_type &a_location, const type_type &a_type,
00241                 const value_type &a_value);
00242         //! Returns the terminator token.
00243         static ptr<pp_token> terminator(void);
00244 
00245 #define DEF_PP_TOKEN(x,y,z,w) TOK_##x, 
00246         //! Token type constants.
00247         enum token_types {
00248                 PP_TOKENS_LIST
00249                 TOK_AVOID_WARNING
00250         };
00251 #undef DEF_PP_TOKEN
00252         
00253         enum { 
00254                 //! Number of token types.
00255                 TOKEN_TYPE_COUNT = TOK_AVOID_WARNING
00256         };
00257 
00258 protected:
00259         //! Creates new token, initializes with appropriate values. 
00260         pp_token(const location_type &a_location, const type_type &a_type,
00261                                 const value_type &a_value, bool a_alternative,
00262                                 const ptr<taboo_macros> &a_taboo);
00263         //! Marks the object.
00264         virtual void gc_mark(void);
00265 private:
00266         //! Type of flags.
00267         typedef lc_host_uint_least16 flags_type;
00268         //! Token flags.
00269         enum pp_token_flags {
00270                 //! Empty flags.
00271                 FLG_NONE = 0x0,
00272                 //! Token has internally initialized value.
00273                 FLG_INTERNAL = 0x1,
00274                 //! Token has externally initialized value.
00275                 FLG_EXTERNAL = 0x2,
00276                 //! Token is a name.
00277                 FLG_NAME = 0x4,
00278                 //! Token has two variants of spelling.
00279                 FLG_VARIANT = 0x08
00280         };
00281         //! Token has alternative spelling.
00282         bool alternative;
00283         //! Taboo macros for token.
00284         srp<taboo_macros> taboo;
00285         //! Hides copy constructor.
00286         pp_token(const pp_token &);   
00287         //! Hides assignment operator.
00288         pp_token &operator=(const pp_token &);
00289         //! Initializes static structures. 
00290         static void initialize(void);
00291         //! Initialization flag.
00292         static bool initialized;
00293         //! Descriptions of tokens.
00294         static lstring the_description[TOKEN_TYPE_COUNT];
00295         //! Spelling of tokens.
00296         static ucn_string the_spelling[2][TOKEN_TYPE_COUNT];
00297         //! Values of tokens.
00298         static ptr<token_value> the_value[TOKEN_TYPE_COUNT];
00299         //! Flags of tokens.
00300         static flags_type the_flags[TOKEN_TYPE_COUNT];
00301         //! The terminator token instance.
00302         static ptr<pp_token> terminator_instance;
00303 };
00304 
00305 //! Type of token in pp_token.
00306 typedef pp_token::type_type pp_token_type;
00307 
00308 end_package(lex);
00309 end_package(cplus);
00310 end_package(lang);
00311 end_package(lestes);
00312 
00313 #endif
00314 /* vim: set ft=lestes : */

Generated on Mon Feb 12 18:23:10 2007 for lestes by doxygen 1.5.1-20070107