lestes::lang::cplus::lex::macro_head Class Reference

Macro parameter list. More...

#include <macro_head.hh>

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

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

Public Types

enum  state_type { BEGIN, PARSED, DEAD }
 Type of internal state. More...

Public Member Functions

bool parse (const ptr< token_input > &input)
 Parses macro parameter list.
ulint index_of (const ptr< token_value > &a_value) const
 Returns index of parameter.
ulint length (void) const
 Returns list length.
state_type state_get (void) const
 Returns internal state.
bool equals (const ptr< macro_head > &other) const
 Tests equality.

Static Public Member Functions

static ptr< macro_headcreate (void)
 Returns empty parameter list.

Protected Member Functions

 macro_head (void)
 Creates empty parameter list.
virtual void gc_mark (void)
 Marks the object.

Private Types

typedef ::lestes::std::map<
srp< token_value >, ulint,
token_value::compare_less
pars_type
 Type of map for storing the parameters.

Private Member Functions

bool add_param (const ptr< token_value > &a_value)
 Adds parameter to list.

Private Attributes

state_type state
 Internal state of the object.
srp< pars_typepars
 Map of parameters to indexes.

Detailed Description

Macro parameter list.

Representation of indexed macro parameter list. The parameter names are added at the end and can be searched for.

Definition at line 56 of file macro_head.hh.


Member Typedef Documentation

typedef ::lestes::std::map< srp<token_value>,ulint,token_value::compare_less > lestes::lang::cplus::lex::macro_head::pars_type [private]

Type of map for storing the parameters.

Definition at line 79 of file macro_head.hh.


Member Enumeration Documentation

enum lestes::lang::cplus::lex::macro_head::state_type

Type of internal state.

Enumerator:
BEGIN 
PARSED 
DEAD 

Definition at line 59 of file macro_head.hh.

00059 { BEGIN, PARSED, DEAD } state_type;


Constructor & Destructor Documentation

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

Creates empty parameter list.

Constructs empty parameter list.

Postcondition:
state == BEGIN

length() == 0

pars->size() == 0

Definition at line 56 of file macro_head.cc.

Referenced by create().

00056                           :
00057         state(BEGIN),
00058         pars(pars_type::create())
00059 {
00060 }


Member Function Documentation

bool lestes::lang::cplus::lex::macro_head::parse ( const ptr< token_input > &  input  ) 

Parses macro parameter list.

Parses macro parameter list.

Precondition:
state == BEGIN

input != NULL

Parameters:
input The source for tokens, starting after left parenthesis.
Returns:
false In case of duplicate parameters or unterminated parameter list.

Definition at line 69 of file macro_head.cc.

References add_param(), BEGIN, DEAD, lestes::lang::cplus::lex::duplicate_macro_parameter, lestes::lang::cplus::lex::expected_comma, lestes::lang::cplus::lex::expected_comma_right_par, lestes::lang::cplus::lex::expected_macro_parameter, lestes::lang::cplus::lex::expected_right_par, lassert, PARSED, lestes::report, state, state_get(), lestes::lang::cplus::lex::pp_token::TOK_COMMA, lestes::lang::cplus::lex::pp_token::TOK_LINE_END, and lestes::lang::cplus::lex::pp_token::TOK_RIGHT_PAR.

00070 {
00071         lassert(state_get() == BEGIN);
00072         lassert(input);
00073 
00074         ptr<pp_token> t;
00075         pp_token_type ptt;
00076   
00077         t = input->read_front_skip_ws();
00078 
00079         // function like macro with no parameters
00080         if (t->type_get() == pp_token::TOK_RIGHT_PAR) {
00081                 state = PARSED;
00082                 return true;
00083         }
00084 
00085         // first parameter is handled separately
00086         if (!t->is_name()) {
00087                 // expected macro parameter name identifier
00088                 report << expected_macro_parameter << t->location_get();
00089                 state = DEAD;
00090                 return false;
00091         }
00092 
00093         // first cannot be duplicate
00094         add_param(t->value_get());
00095 
00096         // read until terminating `)'
00097         while (t = input->read_front_skip_ws(), (ptt = t->type_get()) != pp_token::TOK_RIGHT_PAR) {
00098                 
00099                 if (ptt != pp_token::TOK_COMMA) {
00100                         if (ptt == pp_token::TOK_LINE_END) {
00101                                 // unterminated macro parameter list
00102                                 report << expected_right_par << t->location_get();
00103                         } else if (t->is_name()) {
00104                                 // macro parameters must be comma separated
00105                                 report << expected_comma << t->location_get();
00106                         } else {
00107                                 // invalid token in macro parameter list
00108                                 report << expected_comma_right_par << t->spelling_get() << t->location_get();
00109                         }
00110                         state = DEAD;
00111                         return false;
00112                 }
00113                 
00114                 t = input->read_front_skip_ws();
00115                 if (!t->is_name()) {
00116                         // expected parameter name identifier
00117                         report << expected_macro_parameter << t->location_get();
00118                         state = DEAD;
00119                         return false;
00120                 }
00121 
00122                 if (!add_param(t->value_get())) {
00123                         // duplicate macro parameter
00124                         report << duplicate_macro_parameter << t->spelling_get() << t->location_get();
00125                         state = DEAD;
00126                         return false;
00127                 }
00128         }
00129 
00130         state = PARSED;
00131         return true;
00132 }

ulint lestes::lang::cplus::lex::macro_head::index_of ( const ptr< token_value > &  a_value  )  const

Returns index of parameter.

Returns index of parameter in list.

Precondition:
state == PARSED

a_value != NULL

Returns:
The index of the parameter, length() if parameter is not present.

Definition at line 153 of file macro_head.cc.

References lassert, pars, PARSED, and state_get().

00154 {
00155         lassert(state_get() == PARSED);
00156         lassert(a_value);
00157         pars_type::const_iterator it = pars->find(a_value);
00158         if (it == pars->end()) return pars->size();
00159         return (*it).second;
00160 }

ulint lestes::lang::cplus::lex::macro_head::length ( void   )  const

Returns list length.

Returns length of the parameter list, which is also a special return value in index_of method.

Precondition:
state == PARSED
Returns:
Length of the parameter list.

Definition at line 167 of file macro_head.cc.

References lassert, pars, PARSED, and state_get().

Referenced by equals().

00168 {
00169         lassert(state_get() == PARSED);
00170         return pars->size();
00171 }

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

Returns internal state.

Returns internal state of the object.

Returns:
The internal state of the object.

Definition at line 177 of file macro_head.cc.

References state.

Referenced by index_of(), length(), and parse().

00178 {
00179         return state;
00180 }

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

Tests equality.

Tests equality to other macro head.

Parameters:
other The macro head to compare with.
Returns:
true If both parameter lists have the same length and spelling.

Definition at line 187 of file macro_head.cc.

References length(), pars, PARSED, and state.

00188 {
00189         ::std::cerr << "macro_head::equals\n";
00190         if (!other || state != other->state_get()) return false;
00191         ::std::cerr << "state ok\n";
00192         if (state != PARSED) return true;
00193         ulint len = length();
00194         if (len != other->length()) return false;
00195         ::std::cerr << "length ok\n";
00196         
00197         for (pars_type::iterator it = pars->begin(); it != pars->end(); ++it) {
00198                 ptr<token_value> tv = (*it).first;
00199                 ulint idx = (*it).second;
00200 
00201                 if (other->index_of(tv) != idx) return false;
00202                 ::std::cerr << "par " << idx << " ok\n";
00203         }
00204         
00205         return true;
00206 }

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

Returns empty parameter list.

Returns empty macro head.

Postcondition:
state == BEGIN
Returns:
A new empty instance of macro head.

Definition at line 222 of file macro_head.cc.

References macro_head().

00223 {
00224         return new macro_head();
00225 }

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

Marks the object.

Marks the object.

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

Definition at line 211 of file macro_head.cc.

References lestes::std::mem::keystone::gc_mark(), and pars.

00212 {
00213         pars.gc_mark();
00214 	::lestes::std::object::gc_mark();
00215 }

bool lestes::lang::cplus::lex::macro_head::add_param ( const ptr< token_value > &  a_value  )  [private]

Adds parameter to list.

Adds parameter to list.

Precondition:
a_value != NULL
Parameters:
a_value Value to be added as parameter.
Returns:
false If the value was already present in the list.

Definition at line 140 of file macro_head.cc.

References lassert, and pars.

Referenced by parse().

00141 {
00142         lassert(a_value);
00143         if (pars->insert(make_pair(a_value,pars->size())).second == false) return false;
00144         return true;
00145 }


Member Data Documentation

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

Internal state of the object.

Definition at line 83 of file macro_head.hh.

Referenced by equals(), parse(), and state_get().

srp<pars_type> lestes::lang::cplus::lex::macro_head::pars [private]

Map of parameters to indexes.

Definition at line 85 of file macro_head.hh.

Referenced by add_param(), equals(), gc_mark(), index_of(), and length().


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