lestes::lang::cplus::syn::one_manager Class Reference

Inheritance diagram for lestes::lang::cplus::syn::one_manager:

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

Public Member Functions

ptr< bison_tokenyylex ()
ptr< bison_tokenprev_yylex ()
ptr< bison_tokenpeek ()
void back_up ()
void start (int)
void commit (int)
void rollback ()
ptr< bison_pack_tokenpack (int, const ptr< source_location > &)
void unpack ()
bool in_disambiguation () const
void install_undo_action (ptr< action > a)
bool failing_get () const
void failing_set_true ()

Static Public Member Functions

static ptr< one_managercreate (const ptr< streamer > &a_src)
static void init ()
 Initializes the maps.

Protected Member Functions

 one_manager (const ptr< streamer > &)
void gc_mark ()
 Marks the keystone.
void find_closing (int closing, int opening, const ptr< source_location > &loc)
 Finds closing token (e.g. parenthesis); opening token must be specified too.

Protected Attributes

bool run_hinter
bool failing
srp< token_list_typetoken_list
token_list_iterator token_list_pos
srp< list< srp< transaction > > > open_transactions
static::std::set< int > error_stmt_end_set
 Contains a set of skeletal tokens used in error recovery.

Private Attributes

srp< streamersrc
srp< bison_tokenlast_token

Detailed Description

Definition at line 238 of file manager.cc.


Constructor & Destructor Documentation

lestes::lang::cplus::syn::one_manager::one_manager ( const ptr< streamer > &  s  )  [protected]

Immediately adds first token from the tokeniser into token_list.

Definition at line 313 of file manager.cc.

References create(), failing, last_token, open_transactions, run_hinter, src, token_list, and token_list_pos.

00314 {
00315         src = s;
00316         last_token = NULL;
00317         run_hinter = true;
00318         failing = false;
00319         token_list = token_list_type::create();
00320         token_list->push_back( s->first() );
00321         token_list_pos = token_list->begin();
00322         open_transactions = list< srp<transaction> >::create();
00323 }


Member Function Documentation

void lestes::lang::cplus::syn::one_manager::gc_mark ( void   )  [inline, protected, virtual]

Marks the keystone.

Marks all directly reachable parts of the class. The method must be overriden for each inherited class. It should contain abc.gc_mark() for each field abc of the inherited class and call to gc_mark() of the direct ancestor of the class. Does nothing for keystone, only stops processing of ancestors.

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

Definition at line 251 of file manager.cc.

00252         {
00253                 src.gc_mark();
00254                 last_token.gc_mark();
00255                 token_list.gc_mark();
00256                 open_transactions.gc_mark();
00257                 object::gc_mark();
00258         }

void lestes::lang::cplus::syn::one_manager::find_closing ( int  closing,
int  opening,
const ptr< source_location > &  loc 
) [protected]

Finds closing token (e.g. parenthesis); opening token must be specified too.

Reads tokens using yylex() to find closing parenthesis/bracket/brace.

Tokens are read until one with closing type is found. Each occurence of token with opening type requires its counterpart with closing type to be read before a token with the closing type is considered to be terminating.

When EOF is read before all occurences of opening tokens are closed, errors are reported.

The position in the stream is left to point just after the terminating token. This is needed for pack() to work. This method is called exclusively from there.

When closing == opening nesting is not done and very first occurence of a token with such value is considered closing.

A location must be provided for cases when the unclosed opening token lies before tokens read by the method. It is used for error reporting only.

Parameters:
closing type of token considered to be closing [parenthesis]
opening type of token considered to be opening [parenthesis]
loc location used to error reports that are out of "scope"

Definition at line 560 of file manager.cc.

References failing_set_true(), lassert, lestes::report, TOK_EOF, lestes::lang::cplus::syn::unmatched_open, and yylex().

Referenced by pack().

00561 {
00562         lassert( loc );
00563         typedef list< srp<bison_token> > stack_type;
00564         ptr<stack_type> st = stack_type::create();
00565         while (true) {
00566                 ptr<bison_token> t = yylex();
00567                 int ttype = t->type_get();
00568                 if (ttype == bison_token::TOK_EOF) {
00569                         // BOOM
00570                         if (st->empty())
00571                                 report << unmatched_open << loc;
00572                         else
00573                                 report << unmatched_open << st->back()->location_get();
00574                         // do not report errors when we return back to the parser
00575                         failing_set_true();
00576                         break;
00577                 }
00578                 if (ttype == closing) {
00579                         if (st->empty())
00580                                 break;  // closing one found
00581                         else
00582                                 st->pop_back();
00583                 } else if (ttype == opening)
00584                         st->push_back( t );
00585         }
00586 }

static ptr<one_manager> lestes::lang::cplus::syn::one_manager::create ( const ptr< streamer > &  a_src  )  [inline, static]

Definition at line 265 of file manager.cc.

Referenced by lestes::lang::cplus::syn::manager::init(), one_manager(), and lestes::lang::cplus::syn::manager::spawn().

00266         {
00267                 return new one_manager(a_src);
00268         }

void lestes::lang::cplus::syn::one_manager::init (  )  [static]

Initializes the maps.

Definition at line 289 of file manager.cc.

References error_stmt_end_set, TOK_BREAK, TOK_CATCH, TOK_CONTINUE, TOK_DO, TOK_ELSE, TOK_EOF, TOK_FOR, TOK_GOTO, TOK_IF, TOK_LEFT_BRACE, TOK_RETURN, TOK_RIGHT_BRACE, TOK_SEMICOLON, TOK_SWITCH, TOK_TRY, and TOK_WHILE.

Referenced by lestes::lang::cplus::syn::manager::init().

ptr< bison_token > lestes::lang::cplus::syn::one_manager::yylex (  ) 

Definition at line 330 of file manager.cc.

References lestes::lang::cplus::syn::hinter::hint(), in_disambiguation(), last_token, peek(), run_hinter, TOK_IDENT, token_list, and token_list_pos.

Referenced by find_closing(), and pack().

00331 {
00332         ptr<bison_token> t = peek();
00333         // discard unneeded tokens; this only happens at top-level
00334         if (!in_disambiguation() && token_list_pos != token_list->begin())
00335                 token_list->pop_front();
00336         ++token_list_pos;
00337 
00338         // run the hinter, if needed
00339         // note that the old (not-hinted) token is stored in token_list
00340         if (run_hinter && t->type_get() == bison_token::TOK_IDENT)
00341                 t = hinter::hint( t, peek() );
00342         last_token = t;
00343         return t;
00344 }

ptr< bison_token > lestes::lang::cplus::syn::one_manager::prev_yylex (  ) 

Definition at line 346 of file manager.cc.

References lassert2, and last_token.

Referenced by pack().

00347 {
00348         lassert2( last_token, "yylex() was not called previously." );
00349         return last_token;
00350 }

ptr< bison_token > lestes::lang::cplus::syn::one_manager::peek (  ) 

Definition at line 352 of file manager.cc.

References src, token_list, and token_list_pos.

Referenced by start(), and yylex().

00353 {
00354         ptr<bison_token> t;
00355         if (token_list_pos == token_list->end()) {
00356                 t = src->next();
00357                 token_list_pos = token_list->insert( token_list_pos, t );
00358         } else
00359                 t = *token_list_pos;
00360         return t;
00361 }

void lestes::lang::cplus::syn::one_manager::back_up (  ) 

Definition at line 363 of file manager.cc.

References lassert, llog, open_transactions, token_list, and token_list_pos.

00364 {
00365         // make sure there is room to back up
00366         // this ensures that only one backing up is allowed between two successive calls to yylex()
00367         lassert( token_list_pos != token_list->begin() );
00368         // make sure we do not back up over current start token
00369         lassert( open_transactions->empty() ||
00370                         token_list_pos != open_transactions->back()->start_pos_get() );
00371 
00372         --token_list_pos;
00373 
00374         llog(ml) << "backing up " << ptr<bison_token>(*token_list_pos) << "\n";
00375 }

void lestes::lang::cplus::syn::one_manager::start ( int   ) 

Definition at line 377 of file manager.cc.

References _START_FIRST, _START_LAST, lestes::lang::cplus::syn::transaction::create(), lestes::intercode::intercode::create(), lassert, llog, open_transactions, peek(), lestes::lang::cplus::syn::manager::start_event, token_list, and token_list_pos.

Referenced by pack().

00378 {
00379         lassert( type > bison_token::_START_FIRST && type < bison_token::_START_LAST );
00380 
00381         ptr<bison_token> t = bison_madeup_token::create( peek()->location_get(), type );
00382         token_list_pos = token_list->insert( token_list_pos, t );
00383         open_transactions->push_back( transaction::create(token_list_pos) );
00384         llog(ml) << "starting with " << t << "\n";
00385 
00386         manager::start_event->trigger();
00387 }

void lestes::lang::cplus::syn::one_manager::commit ( int   ) 

Definition at line 389 of file manager.cc.

References _PAD_FIRST, _PAD_LAST, lestes::lang::cplus::syn::manager::commit_event, lestes::lang::cplus::syn::undo_delete_token::create(), lestes::intercode::intercode::create(), in_disambiguation(), lassert, llog, open_transactions, token_list, and token_list_pos.

Referenced by pack().

00390 {
00391         lassert( in_disambiguation() );
00392         lassert( type > bison_token::_PAD_FIRST && type < bison_token::_PAD_LAST );
00393 
00394         ptr<transaction> this_tx = open_transactions->back();
00395         open_transactions->pop_back();
00396 
00397         token_list_pos = this_tx->start_pos_get();
00398 
00399         // create the pad, take location from the start-token
00400         ptr<bison_token> pad = bison_madeup_token::create( (*token_list_pos)->location_get(), type );
00401 
00402         llog(ml) << "committing " << pad << " over " <<
00403                 ptr<bison_token>(*this_tx->start_pos_get()) << "\n";
00404 
00405         // do overwrite (commit) the token
00406         *token_list_pos = pad;
00407 
00408         // hand over undo actions from nested diambiguation(s), if needed
00409         if (in_disambiguation()) {
00410                 ptr<transaction> prev_tx = open_transactions->back();
00411                 prev_tx->add_undo_action( undo_delete_token::create( token_list, token_list_pos ) );
00412                 this_tx->submit_undo( prev_tx );
00413         }
00414 
00415         manager::commit_event->trigger();
00416 }

void lestes::lang::cplus::syn::one_manager::rollback (  ) 

Definition at line 418 of file manager.cc.

References in_disambiguation(), lassert, llog, llog_plain, open_transactions, lestes::lang::cplus::syn::manager::rollback_event, token_list, and token_list_pos.

00419 {
00420         lassert( in_disambiguation() );
00421 
00422         ptr<transaction> this_tx = open_transactions->back();
00423         open_transactions->pop_back();
00424 
00425         llog(ml) << "rolling back " << ptr<bison_token>(*this_tx->start_pos_get());
00426         ulint count = this_tx->undo();
00427         token_list_pos = token_list->erase( this_tx->start_pos_get() );
00428 
00429         llog_plain(ml) << " (" << count << " undo actions)\n";
00430 
00431         manager::rollback_event->trigger();
00432 }

ptr< bison_pack_token > lestes::lang::cplus::syn::one_manager::pack ( int  pack_type,
const ptr< source_location > &  start_loc 
)

Packs tokens from the stream to form a pack token with given type. The actual packing method used depends on the type of the desired token.

Some packing methods need to report the error as being located before the first token of the pack. It is passed in start_loc in these cases. Note that we cannot rely on prev_yylex() here, because there may have been no previous calls to yylex(). However, the parser (which usually calls us) can be pretty sure.

Parameters:
pack_type Type of desired token pack. Determines the packing method used.
start_loc Location that is passed to some of the packing method for error reporting.

Definition at line 447 of file manager.cc.

References _PACK_FIRST, _PACK_LAST, commit(), lestes::lang::cplus::syn::undo_unpack::create(), lestes::intercode::intercode::create(), error_stmt_end_set, find_closing(), in_disambiguation(), lassert, lassert2, llog, open_transactions, prev_yylex(), run_hinter, start(), TOK_LEFT_BRACE, TOK_LEFT_PAR, TOK_PACK_BODY, TOK_PACK_ERROR_COND, TOK_PACK_ERROR_FOR_COND, TOK_PACK_ERROR_STMT, TOK_PAD_TO_BE_DISCARDED, TOK_RIGHT_BRACE, TOK_RIGHT_PAR, TOK_SEMICOLON, TOK_START_TO_BE_DISCARDED, token_list, token_list_pos, unpack(), and yylex().

00448 {
00449         lassert( pack_type > bison_token::_PACK_FIRST && pack_type < bison_token::_PACK_LAST );
00450         // save state and disable running the hinter
00451         bool run_hinter_old = run_hinter;
00452         run_hinter = false;
00453         // insert a dommy token, it will be overwritten by a pack token in commit()
00454         start( bison_token::TOK_START_TO_BE_DISCARDED );
00455         // skip it
00456         yylex();
00457         switch (pack_type) {
00458                 case bison_token::TOK_PACK_BODY:
00459                         find_closing( bison_token::TOK_RIGHT_BRACE,
00460                                         bison_token::TOK_LEFT_BRACE, start_loc );
00461                         break;
00462                 case bison_token::TOK_PACK_ERROR_COND:
00463                         find_closing( bison_token::TOK_RIGHT_PAR,
00464                                         bison_token::TOK_LEFT_PAR, start_loc );
00465                         break;
00466                 case bison_token::TOK_PACK_ERROR_FOR_COND:
00467                         find_closing( bison_token::TOK_SEMICOLON,
00468                                         bison_token::TOK_SEMICOLON, start_loc );
00469                         break;
00470                 case bison_token::TOK_PACK_ERROR_STMT:
00471                         // skip all tokens until one in the terminator set is read
00472                         while (error_stmt_end_set.find( yylex()->type_get() ) == error_stmt_end_set.end())
00473                                 ;
00474                         /* if the terminating token was ';', skip it too; this solves "if(1)int;else;" */
00475                         if (prev_yylex()->type_get() == bison_token::TOK_SEMICOLON)
00476                                 yylex();
00477                         break;
00478                 default:
00479                         lassert2( false, "This type of packing is not implemented yet." );
00480         }
00481         // 'to' will point just after the terminating token (which we do *not* want in the pack)
00482         token_list_iterator to = token_list_pos;
00483         commit( bison_token::TOK_PAD_TO_BE_DISCARDED );
00484         token_list_iterator from = token_list_pos;
00485 
00486         // remember not to pack the pack token nor the terminating token
00487         ++from;
00488         --to;
00489 
00490         // location of the pack token is the location of the first token in the pack
00491         // its end_location is the location of the token that terminated this pack
00492         //   (it is *not* included in the pack)
00493         ptr<bison_pack_token> pack_tok = bison_pack_token::create(
00494                         (*from)->location_get(), pack_type, (*to)->location_get() );
00495         // the actual packing:
00496         ptr<token_list_type> the_pack = pack_tok->pack_get();
00497         the_pack->splice( the_pack->end(), *token_list, from, to );
00498         // when rolling back, unpack the pack just before the terminating token
00499         ptr<action> unpack = undo_unpack::create( token_list, to, the_pack );
00500         if (in_disambiguation())
00501                 open_transactions->back()->add_undo_action( unpack );
00502 
00503         llog(ml) << "replacing " << ptr<bison_token>(*token_list_pos) <<
00504                 " with " << ptr<bison_token>(pack_tok) << "\n";
00505         *token_list_pos = pack_tok;
00506         // restore state
00507         run_hinter = run_hinter_old;
00508 
00509         return pack_tok;
00510 }

void lestes::lang::cplus::syn::one_manager::unpack (  ) 

Definition at line 512 of file manager.cc.

References _PACK_FIRST, _PACK_LAST, lassert, llog, token_list, and token_list_pos.

Referenced by pack().

00513 {
00514         const ptr<bison_token> & next_tok = *token_list_pos;
00515         lassert( next_tok->type_get() > bison_token::_PACK_FIRST &&
00516                         next_tok->type_get() < bison_token::_PACK_LAST );
00517         llog(ml) << "unpacking " << next_tok << "\n";
00518 
00519         const ptr<bison_pack_token> & pack_tok = next_tok.dncast<bison_pack_token>();
00520 
00521         // We cannot use token_list_pos, as splice "moves the iterator away"...
00522         token_list_iterator pos_after_pack = token_list_pos;
00523         ++pos_after_pack;
00524         token_list->splice( pos_after_pack, *(pack_tok->pack_get()) );
00525         // ...now pos_after_pack points to token following the last unpacked one, not to the first of the pack.
00526         token_list_pos = token_list->erase( token_list_pos );
00527 }

bool lestes::lang::cplus::syn::one_manager::in_disambiguation (  )  const

Definition at line 325 of file manager.cc.

References open_transactions.

Referenced by commit(), install_undo_action(), pack(), rollback(), and yylex().

00326 {
00327         return !open_transactions->empty();
00328 }

void lestes::lang::cplus::syn::one_manager::install_undo_action ( ptr< action a  ) 

Definition at line 529 of file manager.cc.

References in_disambiguation(), lassert, and open_transactions.

00530 {
00531         lassert( in_disambiguation() );
00532         open_transactions->back()->add_undo_action(a);
00533 }

bool lestes::lang::cplus::syn::one_manager::failing_get (  )  const

Definition at line 588 of file manager.cc.

References failing.

00589 {
00590         return failing;
00591 }

void lestes::lang::cplus::syn::one_manager::failing_set_true (  ) 

Definition at line 593 of file manager.cc.

References failing.

Referenced by find_closing().

00594 {
00595         failing = true;
00596 }


Member Data Documentation

srp<streamer> lestes::lang::cplus::syn::one_manager::src [private]

Definition at line 240 of file manager.cc.

Referenced by one_manager(), and peek().

srp<bison_token> lestes::lang::cplus::syn::one_manager::last_token [private]

Definition at line 241 of file manager.cc.

Referenced by one_manager(), prev_yylex(), and yylex().

bool lestes::lang::cplus::syn::one_manager::run_hinter [protected]

Definition at line 243 of file manager.cc.

Referenced by one_manager(), pack(), and yylex().

bool lestes::lang::cplus::syn::one_manager::failing [protected]

Definition at line 244 of file manager.cc.

Referenced by failing_get(), failing_set_true(), and one_manager().

srp<token_list_type> lestes::lang::cplus::syn::one_manager::token_list [protected]

Definition at line 245 of file manager.cc.

Referenced by back_up(), commit(), one_manager(), pack(), peek(), rollback(), start(), unpack(), and yylex().

token_list_iterator lestes::lang::cplus::syn::one_manager::token_list_pos [protected]

Definition at line 246 of file manager.cc.

Referenced by back_up(), commit(), one_manager(), pack(), peek(), rollback(), start(), unpack(), and yylex().

srp< list< srp<transaction> > > lestes::lang::cplus::syn::one_manager::open_transactions [protected]

Definition at line 248 of file manager.cc.

Referenced by back_up(), commit(), in_disambiguation(), install_undo_action(), one_manager(), pack(), rollback(), and start().

std::set< int > lestes::lang::cplus::syn::one_manager::error_stmt_end_set [protected]

Contains a set of skeletal tokens used in error recovery.

Definition at line 263 of file manager.cc.

Referenced by init(), and pack().


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