lestes::lang::cplus::lex::stringifier Class Reference

Token stringifier. More...

#include <stringifier.hh>

Inheritance diagram for lestes::lang::cplus::lex::stringifier:

lestes::std::object lestes::std::mem::keystone List of all members.

Public Member Functions

ptr< pp_tokenprocess (const ptr< token_input > &input)
 Stringifies tokens.

Static Public Member Functions

static ptr< stringifierinstance (void)
 Returns the only instance.

Protected Member Functions

 stringifier (void)
 Creates the object.

Private Member Functions

ucn_string escape_spelling (const ptr< pp_token > &tok)
 Adds guard characters to disable escape sequences.
 stringifier (const stringifier &)
 Hides copy constructor.
stringifieroperator= (const stringifier &)
 Hides assignment operator.

Static Private Attributes

static ptr< stringifiersingleton
 The only instance of the class.

Detailed Description

Token stringifier.

Performs token stringification.

Definition at line 53 of file stringifier.hh.


Constructor & Destructor Documentation

lestes::lang::cplus::lex::stringifier::stringifier ( void   )  [protected]

Creates the object.

Creates the only object of the class.

Definition at line 50 of file stringifier.cc.

Referenced by instance().

00051 {
00052 }

lestes::lang::cplus::lex::stringifier::stringifier ( const stringifier  )  [private]

Hides copy constructor.


Member Function Documentation

ptr< pp_token > lestes::lang::cplus::lex::stringifier::process ( const ptr< token_input > &  input  ) 

Stringifies tokens.

Attempts to stringify tokens. Creates new token of type pp_token::TOK_STRING_LIT, with location taken from the first nonblank input token and value representing joined stringification of all input tokens, with character and string literals escaped. Runs of blank tokens inside the sequence are represented by single space, surrounding blanks are discarded.

Precondition:
input != NULL
Todo:
pt necessary? pre input does not contain pp_token::TOK_LINE_END tokens.
Parameters:
input The tokens to stringify.
Returns:
New token containing the stringification, no correctness checks are done.

Definition at line 65 of file stringifier.cc.

References lestes::lang::cplus::lex::token_value::create(), lestes::lang::cplus::lex::pp_token::create(), escape_spelling(), lestes::lang::cplus::lex::pp_token::TOK_BLANK, lestes::lang::cplus::lex::pp_token::TOK_STRING_LIT, and lestes::lang::cplus::lex::pp_token::TOK_TERMINATOR.

00066 {
00067         ucn_string str;
00068         ptr<pp_token> tok = input->read_front();
00069         // skip the leading blank
00070         if (tok->type_get() == pp_token::TOK_BLANK) tok = input->read_front();
00071         ptr<source_location> loc = tok->location_get();
00072         
00073         if (tok->type_get() != pp_token::TOK_TERMINATOR) {
00074                 ptr<pp_token> last = tok;
00075                 tok = input->read_front();
00076                 
00077                 while (tok->type_get() != pp_token::TOK_TERMINATOR) {
00078                         str += escape_spelling(last);
00079                         last = tok;
00080                         tok = input->read_front();
00081                 }
00082 
00083                 // add the last token iff nonblank
00084                 if (last->type_get() != pp_token::TOK_BLANK) str += escape_spelling(last);
00085         }
00086         
00087         // TODO ??? set error flag, because the literal can be broken
00088         return pp_token::create(loc,pp_token::TOK_STRING_LIT,token_value::create(str));
00089 }

ptr< stringifier > lestes::lang::cplus::lex::stringifier::instance ( void   )  [static]

Returns the only instance.

Returns the only instance. Lazy initialized.

Returns:
The singleton.

Definition at line 158 of file stringifier.cc.

References singleton, and stringifier().

Referenced by lestes::lang::cplus::lex::macro_argument::stringified_get().

00159 {
00160         if (!singleton) {
00161                 singleton = new stringifier();
00162         }
00163         return singleton;
00164 }

ucn_string lestes::lang::cplus::lex::stringifier::escape_spelling ( const ptr< pp_token > &  tok  )  [private]

Adds guard characters to disable escape sequences.

Adds guard backslash characters before double quotes and backslash characters into the spelling of string, character and other literals to avoid interpreting them as escape sequences. Spelling of tokens with other types are returned intact.

Precondition:
tok != NULL
Parameters:
tok The token to process.
Returns:
The spelling string with special characters escaped.

Definition at line 99 of file stringifier.cc.

References lassert, lestes::lang::cplus::lex::pp_token::TOK_CHAR_LIT, lestes::lang::cplus::lex::pp_token::TOK_IDENT, lestes::lang::cplus::lex::pp_token::TOK_OTHER, lestes::lang::cplus::lex::pp_token::TOK_STRING_LIT, lestes::lang::cplus::lex::pp_token::TOK_WCHAR_LIT, lestes::lang::cplus::lex::pp_token::TOK_WSTRING_LIT, and u.

Referenced by process().

00100 {
00101         lassert(tok);
00102 
00103         switch (tok->type_get()) {
00104                 case pp_token::TOK_STRING_LIT:
00105                 case pp_token::TOK_WSTRING_LIT:
00106                 case pp_token::TOK_CHAR_LIT:
00107                 case pp_token::TOK_WCHAR_LIT:
00108                 case pp_token::TOK_OTHER:
00109                 case pp_token::TOK_IDENT:
00110                         break;
00111                 default:
00112                         return tok->spelling_get();
00113         }
00114 
00115         ucn_string str(tok->spelling_get());
00116         ucn_string::size_type len = str.length();
00117         ucn u;
00118         ucn_string work;
00119 
00120         for (ucn_string::size_type i = 0; i < len; i++) {
00121                 u = str[i];
00122                 
00123                 if (character::is_translated(u)) {
00124                         ulint x = character::extract_value(u);
00125                         work += character::ascii_backslash;
00126                         work += character::ascii_backslash;
00127                         if (x <= 0xffff) {
00128                                 work += character::ascii_lower_u;
00129                                 work += character::create_xdigit((x >> 12) & 0xf);
00130                                 work += character::create_xdigit((x >> 4) & 0xf);
00131                                 work += character::create_xdigit((x >> 8) & 0xf);
00132                                 work += character::create_xdigit(x & 0xf);
00133                         } else {
00134                                 work += character::ascii_upper_u;
00135                                 work += character::create_xdigit((x >> 28) & 0xf);
00136                                 work += character::create_xdigit((x >> 24) & 0xf);
00137                                 work += character::create_xdigit((x >> 20) & 0xf);
00138                                 work += character::create_xdigit((x >> 16) & 0xf);
00139                                 work += character::create_xdigit((x >> 12) & 0xf);
00140                                 work += character::create_xdigit((x >> 8) & 0xf);
00141                                 work += character::create_xdigit((x >> 4) & 0xf);
00142                                 work += character::create_xdigit(x & 0xf);
00143                         }
00144                 } else {
00145                         if (u == character::ascii_dquote || u == character::ascii_backslash)
00146                                 work += character::ascii_backslash;
00147                         work += u;
00148                 }
00149                 
00150         }
00151         return work;
00152 }

stringifier& lestes::lang::cplus::lex::stringifier::operator= ( const stringifier  )  [private]

Hides assignment operator.


Member Data Documentation

ptr< stringifier > lestes::lang::cplus::lex::stringifier::singleton [static, private]

The only instance of the class.

The only instance of the class. Lazy initialized in the instance() method.

Definition at line 70 of file stringifier.hh.

Referenced by instance().


The documentation for this class was generated from the following files:
Generated on Mon Feb 12 18:24:19 2007 for lestes by doxygen 1.5.1-20070107