Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

/ray/src/lib/lex/scannerbase.cc

Go to the documentation of this file.
00001 /*
00002  * flex/scannerbase.cc
00003  * 
00004  * Base class for flex-based scanners. 
00005  * 
00006  * Copyright (c) 2003--2004 by Wolfgang Wieser ] wwieser (a) gmx <*> de [ 
00007  * 
00008  * This file may be distributed and/or modified under the terms of the 
00009  * GNU General Public License version 2 as published by the Free Software 
00010  * Foundation. (See COPYING.GPL for details.)
00011  * 
00012  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00013  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00014  * 
00015  */
00016 
00018 // THIS FILE IS SPECIAL!
00019 // It does NOT include any files; it gets included itself into a flex(1) 
00020 // .ll scanner. 
00022 
00051 #ifndef FLEX_BOTTOM_PART
00052 
00053 #define YY_NO_UNPUT
00054 
00067 
00068 
00069 #define PI_TOK()           do { yyextra->_pi_tok(yyleng);  } while(0)
00070 
00071 #define PI_TOKL(l)         do { yyextra->_pi_tok(l);  } while(0)
00072 
00073 #define PI_TAB()           do { yyextra->_pi_tab();  } while(0)
00074 
00075 #define PI_LINE()          do { yyextra->_pi_line(); } while(0)
00076 
00077 #define PI_CHAR(c)         do { yyextra->_pi_char(c); } while(0)
00078 
00080 #define PI_STR(str,len)    do { yyextra->_pi_str(str,len); } while(0)
00081 
00083 #define PI_START()         do { yyextra->_pi_start(); } while(0)
00084 
00085 #define PI_EOF()           do { yyextra->_pi_eof(); } while(0)
00086 
00088 #define YY_INPUT(buf,result,max_size) \
00089     { \
00090         _InternalFlexScannerBase::LexerInput *inp= \
00091             (_InternalFlexScannerBase::LexerInput*)yyin;  \
00092         ssize_t res=inp->read(buf,max_size); \
00093         if(res<0)  /* fatal error */  \
00094         {  abort();  }  \
00095         result=res;  \
00096     }
00097 
00098 /* This macro is needed to set the yyscanner pointer used by the 
00099  * access macros like yyextra, yyin, etc. */
00100 #define YY_SCANNER_MAGIC \
00101     yyscan_t yyscanner=scanner; \
00102     /* Special hack because the macros yyextra, etc. need yyg... (flex-2.5.31) */ \
00103     struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
00104 
00105 
00106 #else  /* defined(FLEX_BOTTOM_PART) */
00107 
00108 
00109 int PREFIX_FlexScannerBase::_lex(void *scanner)
00110 {
00111     return(PREFIX_lex(scanner));
00112 }
00113 
00114 int PREFIX_FlexScannerBase::_lex_init(void **scanner_p)
00115 {
00116     int rv=PREFIX_lex_init(scanner_p);
00117     if(!rv)
00118     {  PREFIX_set_extra(this,scanner);  }
00119     return(rv);
00120 }
00121 
00122 void PREFIX_FlexScannerBase::_lex_destroy(void *scanner)
00123 {
00124     PREFIX_lex_destroy(scanner);
00125 }
00126 
00127 
00128 int PREFIX_FlexScannerBase::_wrap()
00129 {
00130     Assert(magic==MyMagic);
00131     Assert(scanner);  /* Otherwise we may not be here. */
00132     
00133     YY_SCANNER_MAGIC
00134     
00135     // See if we are currently reading a buffer 
00136     // (i.e. there is one on the stack): 
00137     if(YY_CURRENT_BUFFER==NULL)
00138     {  Assert(!top_inp);  return(1);  }
00139     
00140     //fprintf(stderr,"YYWRAP %s --> ",
00141     //  yyin ? ((LexerInput*)yyin)->fp_path.str() : "NULL");
00142     
00143     // Close input file if needed: 
00144     bool must_pop_state=0;
00145     if(yyin)
00146     {
00147         LexerInput *inp=(LexerInput*)yyin;
00148         Assert(top_inp==inp);
00149         Assert(read_inp!=inp);
00150         
00151         must_pop_state=inp->must_pop_state;
00152         if(inp->must_pop_pos_arch)
00153         {
00154             Assert(pos_arch);
00155             pos_arch->EndFile();
00156         }
00157         
00158         // Must save the previous tokens which may not (yet) be deleted: 
00159         for(TokenEntry *_i=inp->prev_toks.first(); _i; )
00160         {
00161             TokenEntry *i=_i;
00162             _i=_i->next;
00163             
00164             if(!i->MayBeCleared())
00165             {
00166                 zombie_list.append(inp->prev_toks.dequeue(i));
00167                 ++zombie_list_nents;
00168             }
00169         }
00170         _TidyUpZombieList();  /* inline check */
00171         
00172         // We want <=old_toks_wanted in prev_toks, allow 1 more here. 
00173         if(inp->prev_toks.count()>cfg.old_toks_wanted+1)
00174         {  Error("OOPS: ~TokenInput: %d tokens left in prev_toks.",
00175             inp->prev_toks.count());  }
00176         
00177         delete inp;
00178         yyin=NULL;
00179         top_inp=NULL;
00180     }
00181     else Assert(top_inp==NULL);
00182     
00183     // Delete the current buffer. yypop_buffer_state() 
00184     // internally calls yy_delete_buffer(). 
00185     PREFIX_pop_buffer_state(scanner);
00186     --file_depth;
00187     //_lex_delete_buffer(buf,scanner);
00188     
00189     int retval;
00190     if(YY_CURRENT_BUFFER==NULL)
00191     {
00192         // Cannot read on; we're at EOF. 
00193         Assert(yyin==NULL);
00194         Assert(!file_depth);
00195         retval=1;
00196         //fprintf(stderr,"EOF\n");
00197     }
00198     else
00199     {
00200         // We'll read on... 
00201         Assert(yyin!=NULL);
00202         Assert(file_depth>0);
00203         top_inp=(LexerInput*)yyin;
00204         
00205         // Restore position: 
00206         p1=top_inp->saved;
00207         
00208         /* Pop state if needed: */
00209         if(must_pop_state)
00210         {  yy_pop_state(scanner);  }
00211         
00212         retval=0;
00213         //fprintf(stderr,"%s  [pos=%d:%d]\n",top_inp->fp_path.str(),
00214         //  p1.line,p1.lpos);*/
00215     }
00216     
00217     return(retval);
00218 }
00219 
00220 
00221 int PREFIX_FlexScannerBase::_SetInputBH(LexerInput *inp,bool included,
00222     XYPos alt_pos,SourcePositionArchive *alt_pos_arch)
00223 {
00224     bool need_push=included;
00225     
00226     // See if we really need to push: 
00227     YY_SCANNER_MAGIC
00228     if(YY_CURRENT_BUFFER==NULL)
00229     {  need_push=0;  }
00230     
00231     // Set the input: 
00232     size_t needlen=YY_BUF_SIZE;
00233     // Notice that I pass the LexerInput instead of the FILE. 
00234     YY_BUFFER_STATE buf=PREFIX_create_buffer((FILE*)inp,needlen,scanner);
00235     if(!buf)  _AllocFailure(needlen);
00236     
00237     // Save the current position (in old top_inp): 
00238     if(top_inp)
00239     {  top_inp->saved=p1;  }
00240     
00241     // Read on in passed LexerInput: 
00242     p0=alt_pos;
00243     p1=alt_pos;
00244     if(need_push)
00245     {
00246         Assert(pos_arch);
00247         Assert(!alt_pos_arch);  // Checks above should return error in 
00248                                    // that case.
00249         PREFIX_push_buffer_state(buf,scanner);
00250         ++file_depth;
00251     }
00252     else
00253     {
00254         Assert(!pos_arch);
00255         
00256         const TLString &file_name=inp->fp_path;
00257         
00258         if(alt_pos_arch)
00259         {
00260             pos_arch=alt_pos_arch;
00261             pos_arch_allocated=0;
00262             // Check that the passed source pos archive contains 
00263             // the correct file. 
00264             if(pos_arch->CurrentFile()->path()!=file_name)
00265             {  Assert(0);  }
00266         }
00267         else
00268         {
00269             //fprintf(stderr,"SPA: %s\n",file_name.str());
00270             pos_arch=new SourcePositionArchive(file_name);
00271             pos_arch_allocated=1;
00272         }
00273         
00274         PREFIX_switch_to_buffer(buf,scanner);
00275         //PREFIX_push_buffer_state(buf,scanner);
00276         Assert(file_depth==0);
00277         file_depth=1;
00278     }
00279     
00280     //fprintf(stderr,"SetInput(%s,%s)\n",inp->fp_path.str(),
00281     //  need_push ? "push" : "switch");
00282     
00283     return(0);
00284 }
00285 
00286 
00287 PREFIX_FlexScannerBase::PREFIX_FlexScannerBase() : 
00288     _InternalFlexScannerBase()
00289 {
00290     // Need to be called here due to virtual functions back here. 
00291     _InternalFlexScannerBase::_reset(/*no_virtuals=*/1);  // <-- CORRECT!
00292 }
00293 
00294 PREFIX_FlexScannerBase::~PREFIX_FlexScannerBase()
00295 {
00296     // Need to be called here due to virtual functions back here. 
00297     _InternalFlexScannerBase::_reset(/*no_virtuals=*/1);  // <-- CORRECT!
00298 }
00299 
00300 #endif  /* FLEX_BOTTOM_PART */

Generated on Sat Feb 19 22:33:45 2005 for Ray by doxygen 1.3.5