lestes::lang::cplus::lex::macro_argument Class Reference

Macro argument. More...

#include <macro_argument.hh>

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

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

Public Types

enum  state_type { BEGIN, PARSED, DEAD }
 Type of internal state. More...
enum  result_type { CONTINUE, LAST, ERROR, EMPTY }
 Parse result type. More...

Public Member Functions

result_type parse (const ptr< token_input > &input, bool first)
 Parses argument.
state_type state_get (void) const
 Returns internal state.
ptr< token_sequencenonexpanded_get (void) const
 Returns nonexpanded argument.
ptr< token_sequenceexpanded_get (const ptr< macro_storage > &macros)
 Returns expanded argument.
ptr< token_sequencestringified_get (void)
 Returns stringified argument.
bool equals (const ptr< macro_argument > &other) const
 Test equality.

Static Public Member Functions

static ptr< macro_argumentcreate (void)
 Returns new instance.

Protected Member Functions

 macro_argument (void)
 Creates empty object.
virtual void gc_mark (void)
 Marks the object.

Private Attributes

state_type state
 Internal state of the object.
srp< token_sequencenonexpanded
 The nonexpanded sequence.
srp< token_sequenceexpanded
 The expanded sequence, lazy evaluated.
srp< token_sequencestringified
 The stringified sequence as a token in sequence, lazy evaluated.

Detailed Description

Macro argument.

Encapsulates macro argument with lazy evaluation. Provides nonexpanded, expanded and stringified representation of the argument.

Definition at line 56 of file macro_argument.hh.


Member Enumeration Documentation

enum lestes::lang::cplus::lex::macro_argument::state_type

Type of internal state.

Enumerator:
BEGIN 
PARSED 
DEAD 

Definition at line 59 of file macro_argument.hh.

00059 { BEGIN, PARSED, DEAD } state_type;

enum lestes::lang::cplus::lex::macro_argument::result_type

Parse result type.

Enumerator:
CONTINUE  The arguments will continue.
LAST  The last argument in the list.
ERROR  Error in argument.
EMPTY  The first argument is empty.

Definition at line 61 of file macro_argument.hh.

00061                      {
00062                 //! The arguments will continue.  
00063                 CONTINUE, 
00064                 //! The last argument in the list.
00065                 LAST,
00066                 //! Error in argument.
00067                 ERROR,
00068                 //! The first argument is empty.
00069                 EMPTY
00070         } result_type;


Constructor & Destructor Documentation

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

Creates empty object.

Creates empty macro argument.

Postcondition:
state == BEGIN

nonexpanded == NULL

expanded == NULL

stringified == NULL

Definition at line 54 of file macro_argument.cc.

Referenced by create().

00054                                   :
00055         state(BEGIN),
00056         nonexpanded(NULL),
00057         expanded(NULL),
00058         stringified(NULL)
00059 {
00060 }


Member Function Documentation

macro_argument::result_type lestes::lang::cplus::lex::macro_argument::parse ( const ptr< token_input > &  input,
bool  first 
)

Parses argument.

Parses single argument of macro. Front and back whitespace is discarded, newlines are converted to spaces. Value is stored into nonexpanded.

Precondition:
parse() was not called yet.
Postcondition:
state == DEAD || nonexpanded == contents of front part of input
Parameters:
input The source for arguments, parsed tokens (including terminating ones) are removed.
first true If the argument is first in the argument list.
Returns:
macro_argument::LAST When top level right parenthesis was encountered.

macro_argument::EMPTY When first argument was empty.

macro_argument::CONTINUE When top level comma was encountered.

macro_argument::ERROR For end of file inside input.

Definition at line 74 of file macro_argument.cc.

References BEGIN, CONTINUE, lestes::lang::cplus::lex::pp_token::create(), lestes::lang::cplus::lex::token_sequence::create(), DEAD, EMPTY, ERROR, lassert, LAST, nonexpanded, PARSED, state, lestes::lang::cplus::lex::pp_token::TOK_BLANK, lestes::lang::cplus::lex::pp_token::TOK_COMMA, lestes::lang::cplus::lex::pp_token::TOK_LEFT_PAR, lestes::lang::cplus::lex::pp_token::TOK_LINE_END, lestes::lang::cplus::lex::pp_token::TOK_RIGHT_PAR, and lestes::lang::cplus::lex::pp_token::TOK_TERMINATOR.

00075 {
00076         lassert(state == BEGIN);
00077         
00078         nonexpanded = token_sequence::create();
00079         result_type result = CONTINUE;
00080         ulint depth = 1;
00081 
00082         input->skip_front_ws();
00083         ptr<pp_token> t;
00084         pp_token_type ptt;
00085 
00086         ptr<pp_token> blank;
00087         
00088         while (input->peek_front()->type_get() == pp_token::TOK_LINE_END) {
00089                 input->read_front();
00090         }
00091         
00092         do {
00093                 t = input->read_front();
00094                 ptt = t->type_get();
00095 
00096                 switch (ptt) {
00097                         case pp_token::TOK_TERMINATOR:
00098                                 state = DEAD;
00099                                 return ERROR;
00100                         case pp_token::TOK_LEFT_PAR:
00101                                 depth++;
00102                                 break;
00103                         case pp_token::TOK_RIGHT_PAR:
00104                                 depth--;
00105                                 if (depth != 0) break;
00106                                 result = nonexpanded->length() == 0 && first ? EMPTY : LAST;
00107 
00108                                 state = PARSED;
00109                                 return result;
00110                         case pp_token::TOK_COMMA:
00111                                 if (depth != 1) break;
00112 
00113                                 state = PARSED;
00114                                 return result;
00115                         case pp_token::TOK_LINE_END:
00116                                 if (blank) continue;
00117                                 blank = pp_token::create(t->location_get(),pp_token::TOK_BLANK);
00118                                 break;
00119                         case pp_token::TOK_BLANK:
00120                                 if (blank) continue;
00121                                 blank = t;
00122                                 break;
00123                         default:
00124                                 break;
00125                 }
00126                 if (blank) {
00127                         nonexpanded->add_back(blank);
00128                         blank = NULL;
00129                 }
00130                 nonexpanded->add_back(t);
00131         } while (true);
00132 }

macro_argument::state_type lestes::lang::cplus::lex::macro_argument::state_get ( void   )  const

Returns internal state.

Returns internal state of the object.

Returns:
The internal state of the object.

Definition at line 138 of file macro_argument.cc.

References state.

Referenced by equals().

00139 {
00140         return state;
00141 }

ptr< token_sequence > lestes::lang::cplus::lex::macro_argument::nonexpanded_get ( void   )  const

Returns nonexpanded argument.

Returns the nonexpanded macro argument.

Precondition:
parse() != ERROR
Returns:
The nonexpanded token sequence.

Definition at line 148 of file macro_argument.cc.

References lassert, nonexpanded, PARSED, and state.

Referenced by equals().

00149 {
00150         lassert(state == PARSED);
00151         return nonexpanded->clone();
00152 }

ptr< token_sequence > lestes::lang::cplus::lex::macro_argument::expanded_get ( const ptr< macro_storage > &  macros  ) 

Returns expanded argument.

Returns the completely expanded macro argument. Expansion is performed only once.

Precondition:
parse() != ERROR
Returns:
The completely expanded token sequence.

Definition at line 159 of file macro_argument.cc.

References expanded, lassert, nonexpanded, PARSED, and state.

00160 {
00161         lassert(state == PARSED);
00162         if (!expanded) {
00163                 expanded = nonexpanded->clone();
00164                 expanded = expanded->expand_all(macros);
00165         }
00166         return expanded->clone();
00167 }

ptr< token_sequence > lestes::lang::cplus::lex::macro_argument::stringified_get ( void   ) 

Returns stringified argument.

Returns stringified macro argument. Stringification is performed only once.

Precondition:
parse() != ERROR
Returns:
Token sequence containing single token with value equal to the stringified token sequence.

Definition at line 174 of file macro_argument.cc.

References lestes::lang::cplus::lex::token_sequence::create(), lestes::lang::cplus::lex::stringifier::instance(), lassert, nonexpanded, PARSED, state, and stringified.

00175 {
00176         lassert(state == PARSED);
00177         if (!stringified) {
00178                 ptr<pp_token> t = stringifier::instance()->process(nonexpanded->clone());
00179                 stringified = token_sequence::create();
00180                 stringified->add_back(t);
00181         }
00182         return stringified->clone();
00183 }

bool lestes::lang::cplus::lex::macro_argument::equals ( const ptr< macro_argument > &  other  )  const

Test equality.

Tests equality to other macro argument. Only the nonexpanded sequences are compared.

Parameters:
other The macro argument to compare with.
Returns:
true If both the objects are equal.

Definition at line 190 of file macro_argument.cc.

References lestes::is_equal(), nonexpanded_get(), PARSED, state, and state_get().

00191 {
00192         if (!other || state_get() != other->state_get()) return false;
00193         if (state != PARSED) return true;
00194         
00195         // compare nonexpanded
00196         return is_equal(nonexpanded_get(),other->nonexpanded_get());
00197 }

ptr< macro_argument > lestes::lang::cplus::lex::macro_argument::create ( void   )  [static]

Returns new instance.

Returns new empty macro argument.

Postcondition:
state == BEGIN

nonexpanded == NULL

expanded == NULL

stringified == NULL

Returns:
New instance of the object.

Definition at line 218 of file macro_argument.cc.

References macro_argument().

Referenced by lestes::lang::cplus::lex::macro_arguments::parse().

00219 {
00220         return new macro_argument();
00221 }

void lestes::lang::cplus::lex::macro_argument::gc_mark ( void   )  [protected, virtual]

Marks the object.

Marks the object.

Reimplemented from lestes::std::mem::keystone.

Definition at line 202 of file macro_argument.cc.

References expanded, lestes::std::mem::keystone::gc_mark(), nonexpanded, and stringified.

00203 {
00204         nonexpanded.gc_mark();
00205         expanded.gc_mark();
00206         stringified.gc_mark();
00207 	::lestes::std::object::gc_mark();
00208 }


Member Data Documentation

state_type lestes::lang::cplus::lex::macro_argument::state [private]

Internal state of the object.

Definition at line 92 of file macro_argument.hh.

Referenced by equals(), expanded_get(), nonexpanded_get(), parse(), state_get(), and stringified_get().

srp<token_sequence> lestes::lang::cplus::lex::macro_argument::nonexpanded [private]

The nonexpanded sequence.

Definition at line 94 of file macro_argument.hh.

Referenced by expanded_get(), gc_mark(), nonexpanded_get(), parse(), and stringified_get().

srp<token_sequence> lestes::lang::cplus::lex::macro_argument::expanded [private]

The expanded sequence, lazy evaluated.

Definition at line 96 of file macro_argument.hh.

Referenced by expanded_get(), and gc_mark().

srp<token_sequence> lestes::lang::cplus::lex::macro_argument::stringified [private]

The stringified sequence as a token in sequence, lazy evaluated.

Definition at line 98 of file macro_argument.hh.

Referenced by gc_mark(), and stringified_get().


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