00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <lestes/common.hh>
00035 #include <lestes/equality.hh>
00036 #include <lestes/std/source_location.hh>
00037 #include <lestes/std/ucn_string.hh>
00038 #include <lestes/lang/cplus/lex/pp_token.hh>
00039 #include <lestes/lang/cplus/lex/basic_token.hh>
00040 #include <lestes/lang/cplus/lex/token_value.hh>
00041 #include <lestes/lang/cplus/lex/taboo_macros.hh>
00042
00043 #include <iostream>
00044
00045 package(lestes);
00046 package(lang);
00047 package(cplus);
00048 package(lex);
00049
00050 using namespace ::std;
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 pp_token::pp_token(const location_type &a_location, const type_type &a_type,
00065 const value_type &a_value, bool a_alternative, const ptr<taboo_macros> &a_taboo):
00066 basic_pp_token(a_location,a_type,a_value),
00067 alternative(a_alternative),
00068 taboo(checked(a_taboo))
00069 {
00070 }
00071
00072
00073
00074
00075
00076 bool pp_token::is_name(void) const
00077 {
00078 return the_flags[type_get()] & FLG_NAME;
00079 }
00080
00081
00082
00083
00084
00085 bool pp_token::is_alternative(void) const
00086 {
00087 return alternative;
00088 }
00089
00090
00091
00092
00093
00094 bool pp_token::is_valued(void) const
00095 {
00096 return the_flags[type_get()] & (FLG_EXTERNAL | FLG_INTERNAL);
00097 }
00098
00099
00100
00101
00102
00103 lstring pp_token::description_get(void) const
00104 {
00105 return the_description[type_get()];
00106 }
00107
00108
00109
00110
00111
00112 ucn_string pp_token::spelling_get(void) const
00113 {
00114 pp_token_type ptt = type_get();
00115
00116 switch (ptt) {
00117 case pp_token::TOK_STRING_LIT:
00118 {
00119 ucn_string us = "\"";
00120 us += value_get()->content_get();
00121 us += character::create_from_host('"');
00122 return us;
00123 }
00124 case pp_token::TOK_CHAR_LIT:
00125 {
00126 ucn_string us = "'";
00127 us += value_get()->content_get();
00128 us += character::create_from_host('\'');
00129 return us;
00130 }
00131 case pp_token::TOK_WSTRING_LIT:
00132 {
00133 ucn_string us = "L\"";
00134 us += value_get()->content_get();
00135 us += character::create_from_host('"');
00136 return us;
00137 }
00138 case pp_token::TOK_WCHAR_LIT:
00139 {
00140 ucn_string us = "L'";
00141 us += value_get()->content_get();
00142 us += character::create_from_host('\'');
00143 return us;
00144 }
00145 default:
00146 break;
00147 }
00148
00149 if (is_valued()) {
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 return value_get()->content_get();
00160 }
00161
00162 return the_spelling[is_alternative()][ptt];
00163 }
00164
00165
00166
00167
00168
00169 ptr<taboo_macros> pp_token::taboo_get(void) const
00170 {
00171 return taboo;
00172 }
00173
00174
00175
00176
00177
00178
00179
00180 bool pp_token::congruent(const ptr<pp_token> &other) const
00181 {
00182 return other &&
00183 is_equal(type_get(),other->type_get()) &&
00184 is_equal(is_alternative(),other->is_alternative()) &&
00185 is_equal(value_get(),other->value_get());
00186 }
00187
00188
00189
00190
00191
00192
00193 bool pp_token::equals(const ptr<pp_token> &other) const
00194 {
00195
00196 return other &&
00197 is_equal(type_get(),other->type_get()) &&
00198 is_equal(is_alternative(),other->is_alternative()) &&
00199 is_equal(location_get(),other->location_get()) &&
00200 is_equal(value_get(),other->value_get());
00201 }
00202
00203
00204
00205
00206
00207
00208
00209 ptr<pp_token> pp_token::clone(const location_type &a_location) const
00210 {
00211 lassert(a_location);
00212 return new pp_token(a_location,type_get(),value_get(),is_alternative(),taboo_get());
00213 }
00214
00215
00216
00217
00218
00219
00220
00221
00222 ptr<pp_token> pp_token::clone(const ptr<taboo_macros> &a_taboo) const
00223 {
00224 lassert(a_taboo);
00225 lassert(is_name());
00226 return new pp_token(location_get(),type_get(),value_get(),is_alternative(),a_taboo);
00227 }
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 ptr<pp_token> pp_token::create(const location_type &a_location, const type_type &a_type)
00239 {
00240 initialize();
00241 lassert(!(the_flags[a_type] & FLG_EXTERNAL));
00242 return new pp_token(a_location,a_type,the_value[a_type],false,taboo_macros::create());
00243 }
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 ptr<pp_token> pp_token::create_alternative(const location_type &a_location, const type_type &a_type)
00256 {
00257 initialize();
00258 lassert(!(the_flags[a_type] & FLG_EXTERNAL));
00259 lassert(the_flags[a_type] & FLG_VARIANT);
00260 return new pp_token(a_location,a_type,the_value[a_type],true,taboo_macros::create());
00261 }
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 ptr<pp_token> pp_token::create(const location_type &a_location, const type_type &a_type,
00275 const value_type &a_value)
00276 {
00277 initialize();
00278 lassert(the_flags[a_type] & FLG_EXTERNAL);
00279 return new pp_token(a_location,a_type,a_value,false,taboo_macros::create());
00280 }
00281
00282 ptr<pp_token> pp_token::terminator(void)
00283 {
00284 if (!terminator_instance) {
00285 terminator_instance = pp_token::create(source_location::zero(),pp_token::TOK_TERMINATOR);
00286 }
00287 return terminator_instance;
00288 }
00289
00290
00291
00292
00293 void pp_token::gc_mark(void)
00294 {
00295 taboo.gc_mark();
00296 basic_pp_token::gc_mark();
00297 }
00298
00299
00300
00301
00302
00303 void pp_token::initialize(void)
00304 {
00305 if (initialized) return;
00306
00307 ulint i;
00308
00309 i = 0;
00310 #undef DEF_PP_TOKEN
00311 #define DEF_PP_TOKEN(x,y,z,w) \
00312 the_description[i++] = #x;
00313 PP_TOKENS_LIST
00314
00315 i = 0;
00316 #undef DEF_PP_TOKEN
00317 #define DEF_PP_TOKEN(x,y,z,w) \
00318 the_spelling[0][i] = y; the_spelling[1][i++] = z;
00319 PP_TOKENS_LIST
00320
00321 i = 0;
00322 #undef DEF_PP_TOKEN
00323 #define DEF_PP_TOKEN(x,y,z,w) \
00324 the_value[i++] = ((w) & FLG_INTERNAL) ? token_value::create(ucn_string(y)) : ptr<token_value>(NULL);
00325 PP_TOKENS_LIST
00326
00327 i = 0;
00328 #undef DEF_PP_TOKEN
00329 #define DEF_PP_TOKEN(x,y,z,w) \
00330 the_flags[i++] = w;
00331 PP_TOKENS_LIST
00332
00333 initialized = true;
00334 }
00335
00336
00337
00338
00339 bool pp_token::initialized;
00340
00341
00342
00343
00344 ptr<token_value> pp_token::the_value[TOKEN_TYPE_COUNT];
00345
00346
00347
00348
00349 lstring pp_token::the_description[TOKEN_TYPE_COUNT];
00350
00351
00352
00353
00354 ucn_string pp_token::the_spelling[2][TOKEN_TYPE_COUNT];
00355
00356
00357
00358
00359 pp_token::flags_type pp_token::the_flags[TOKEN_TYPE_COUNT];
00360
00361
00362
00363
00364 ptr<pp_token> pp_token::terminator_instance = terminator_instance;
00365
00366 end_package(lex);
00367 end_package(cplus);
00368 end_package(lang);
00369 end_package(lestes);
00370
00371