GME  13
AParser.cpp
Go to the documentation of this file.
00001 /* ANTLRParser.C
00002  *
00003  * SOFTWARE RIGHTS
00004  *
00005  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
00006  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
00007  * company may do whatever they wish with source code distributed with
00008  * PCCTS or the code generated by PCCTS, including the incorporation of
00009  * PCCTS, or its output, into commerical software.
00010  *
00011  * We encourage users to develop software with PCCTS.  However, we do ask
00012  * that credit is given to us for developing PCCTS.  By "credit",
00013  * we mean that if you incorporate our source code into one of your
00014  * programs (commercial product, research project, or otherwise) that you
00015  * acknowledge this fact somewhere in the documentation, research report,
00016  * etc...  If you like PCCTS and have developed a nice tool with the
00017  * output, please mention that you developed it using PCCTS.  In
00018  * addition, we ask that this header remain intact in our source code.
00019  * As long as these guidelines are kept, we expect to continue enhancing
00020  * this system and expect to make other tools available as they are
00021  * completed.
00022  *
00023  * ANTLR 1.33
00024  * Terence Parr
00025  * Parr Research Corporation
00026  * with Purdue University and AHPCRC, University of Minnesota
00027  * 1989-1998
00028  */
00029 // See http://support.microsoft.com/kb/148652
00030 //#include "forcelib.h"
00031 #include "stdafx.h"
00032 #include <stdlib.h>
00033 #include <stdarg.h>
00034 #include <string.h>//z!
00035 #include <stdio.h>
00036 
00037 /* I have to put this here due to C++ limitation
00038  * that you can't have a 'forward' decl for enums.
00039  * I hate C++!!!!!!!!!!!!!!!
00040  * Of course, if I could use real templates, this would go away.
00041  */
00042 // MR1
00043 // MR1  10-Apr-97  133MR1  Prevent use of varying sizes for the
00044 // MR1                          ANTLRTokenType enum
00045 // MR1
00046 
00047 enum ANTLRTokenType { TER_HATES_CPP=0, ITS_TOO_COMPLICATED=9999};           // MR1
00048 
00049 #define ANTLR_SUPPORT_CODE
00050 
00051 #include "config.h"
00052 #include ATOKEN_H
00053 
00054 #include ATOKENBUFFER_H
00055 #include APARSER_H
00056 
00057 static const int zzINF_DEF_TOKEN_BUFFER_SIZE = 2000;
00058 static const int zzINF_BUFFER_TOKEN_CHUNK_SIZE = 1000;
00059 
00060                  /* L o o k a h e a d  M a c r o s */
00061 
00062 /* maximum of 32 bits/unsigned int and must be 8 bits/byte;
00063  * we only use 8 bits of it.
00064  */
00065 SetWordType ANTLRParser::bitmask[sizeof(SetWordType)*8] = {
00066         0x00000001, 0x00000002, 0x00000004, 0x00000008,
00067         0x00000010, 0x00000020, 0x00000040, 0x00000080
00068 };
00069 
00070 char ANTLRParser::eMsgBuffer[500] = "";
00071 
00072 ANTLRParser::
00073 ~ANTLRParser()
00074 {
00075         delete [] token_type;
00076 }
00077 
00078 ANTLRParser::
00079 ANTLRParser(ANTLRTokenBuffer *_inputTokens,
00080                         int k,
00081                         int use_inf_look,
00082                         int dlook,
00083                         int ssize)
00084 {
00085         LLk = k;
00086         can_use_inf_look = use_inf_look;
00087         demand_look = dlook;
00088 
00089     bsetsize = ssize;
00090         guessing = 0;
00091         token_tbl = NULL;
00092         eofToken = (ANTLRTokenType)1;
00093 
00094         // allocate lookahead buffer
00095         token_type = new ANTLRTokenType[LLk];
00096         lap = 0;
00097         labase = 0;
00098         dirty = 0;
00099     inf_labase = 0;                                                     // MR7
00100     inf_last = 0;                                                       // MR7
00101         /* prime lookahead buffer, point to inputTokens */
00102         this->inputTokens = _inputTokens;
00103         this->inputTokens->setMinTokens(k);
00104         _inputTokens->setParser(this);                                                      // MR1
00105     resynchConsumed=1;                                                  // MR8
00106     zzFAILtext=NULL;                                                    // MR9
00107     traceOptionValueDefault=0;                                          // MR10
00108     traceReset();                                                       // MR10
00109     zzGuessSeq=0;                                                       // MR10
00110     syntaxErrCount=0;                                                   // MR11
00111 }
00112 
00113 void ANTLRParser::init()
00114 {
00115    prime_lookahead();
00116    resynchConsumed=1;                                                   // MR8
00117    traceReset();                                                        // MR10
00118 }
00119 
00120 void ANTLRParser::traceReset()
00121 {
00122    traceOptionValue=traceOptionValueDefault;
00123    traceGuessOptionValue=1;
00124    traceCurrentRuleName=NULL;
00125    traceDepth=0;
00126 }
00127 
00128 int ANTLRParser::
00129 guess(ANTLRParserState *st)
00130 {
00131         saveState(st);
00132         guessing = 1;
00133         return setjmp(guess_start.state);
00134 }
00135 
00136 void ANTLRParser::
00137 saveState(ANTLRParserState *buf)
00138 {
00139         buf->guess_start = guess_start;
00140         buf->guessing = guessing;
00141         buf->inf_labase = inf_labase;
00142         buf->inf_last = inf_last;
00143         buf->dirty = dirty;
00144     buf->traceOptionValue=traceOptionValue;            /* MR10 */
00145     buf->traceGuessOptionValue=traceGuessOptionValue;  /* MR10 */
00146     buf->traceCurrentRuleName=traceCurrentRuleName;    /* MR10 */
00147     buf->traceDepth=traceDepth;                        /* MR10 */
00148 }
00149 
00150 void ANTLRParser::
00151 restoreState(ANTLRParserState *buf)
00152 {
00153         int     i;
00154     int     prevTraceOptionValue;
00155 
00156         guess_start = buf->guess_start;
00157         guessing = buf->guessing;
00158         inf_labase = buf->inf_labase;
00159         inf_last = buf->inf_last;
00160         dirty = buf->dirty;
00161 
00162         // restore lookahead buffer from k tokens before restored TokenBuffer position
00163         // if demand_look, then I guess we don't look backwards for these tokens.
00164         for (i=1; i<=LLk; i++) token_type[i-1] =
00165                 inputTokens->bufferedToken(i-LLk)->getType();
00166         lap = 0;
00167         labase = 0;
00168 
00169     /* MR10 */
00170 
00171     prevTraceOptionValue=traceOptionValue;
00172     traceOptionValue=buf->traceOptionValue;
00173     if ( (prevTraceOptionValue > 0) !=
00174              (traceOptionValue > 0)) {
00175       if (traceOptionValue > 0) {
00176         fprintf(stderr,"trace enable restored in rule %s depth %d\n",traceCurrentRuleName,traceDepth);
00177       };
00178       if (traceOptionValue <= 0) {
00179         fprintf(stderr,"trace disable restored in rule %s depth %d\n",traceCurrentRuleName,traceDepth);
00180       };
00181     };
00182     traceGuessOptionValue=buf->traceGuessOptionValue;
00183     traceCurrentRuleName=buf->traceCurrentRuleName;
00184     traceDepth=buf->traceDepth;
00185     traceGuessDone(buf);
00186 }
00187 
00188 /* Get the next symbol from the input stream; put it into lookahead buffer;
00189  * fill token_type[] fast reference cache also.  NLA is the next place where
00190  * a lookahead ANTLRAbstractToken should go.
00191  */
00192 void ANTLRParser::
00193 consume()
00194 {
00195 
00196 #ifdef ZZDEBUG_CONSUME_ACTION
00197     zzdebug_consume_action();
00198 #endif
00199 
00200     NLA = inputTokens->getToken()->getType();
00201         dirty--;
00202         lap = (lap+1)&(LLk-1);
00203 }
00204 
00205 _ANTLRTokenPtr ANTLRParser::
00206 LT(int i)
00207 {
00208 #ifdef DEBUG_TOKENBUFFER
00209         if ( i >= inputTokens->bufferSize() || inputTokens->minTokens() <= LLk )
00210         {
00211                 static char buf[2000];
00212         sprintf(buf, "The minimum number of tokens you requested that the\nANTLRTokenBuffer buffer is not enough to satisfy your\nLT(%d) request; increase 'k' argument to constructor for ANTLRTokenBuffer\n", i);
00213                 panic(buf);
00214         }
00215 #endif
00216         return inputTokens->bufferedToken(i-LLk);
00217 }
00218 
00219 void
00220 ANTLRParser::
00221 look(int k)
00222 {
00223         int i, c = k - (LLk-dirty);
00224         for (i=1; i<=c; i++) consume();
00225 }
00226 
00227 /* fill the lookahead buffer up with k symbols (even if DEMAND_LOOK);
00228  */
00229 void
00230 ANTLRParser::
00231 prime_lookahead()
00232 {
00233         int i;
00234         for(i=1;i<=LLk; i++) consume();
00235         dirty=0;
00236         lap = 0;
00237         labase = 0;
00238 }
00239 
00240 /* check to see if the current input symbol matches '_t'.
00241  * During NON demand lookahead mode, dirty will always be 0 and
00242  * hence the extra code for consuming tokens in _match is never
00243  * executed; the same routine can be used for both modes.
00244  */
00245 int ANTLRParser::
00246 _match(ANTLRTokenType _t, ANTLRChar **MissText,
00247            ANTLRTokenType *MissTok, _ANTLRTokenPtr *BadTok,
00248            SetWordType **MissSet)
00249 {
00250         if ( dirty==LLk ) {
00251                 consume();
00252         }
00253         if ( LA(1)!=_t ) {
00254                 *MissText=NULL;
00255                 *MissTok= _t; *BadTok = LT(1);
00256                 *MissSet=NULL;
00257                 return 0;
00258         }
00259         dirty++;
00260         labase = (labase+1)&(LLk-1);    // labase maintained even if !demand look
00261         return 1;
00262 }
00263 
00264 /* check to see if the current input symbol matches '_t'.
00265  * Used during exception handling.
00266  */
00267 int ANTLRParser::
00268 _match_wsig(ANTLRTokenType _t)
00269 {
00270         if ( dirty==LLk ) {
00271                 consume();
00272         }
00273         if ( LA(1)!=_t ) return 0;
00274         dirty++;
00275         labase = (labase+1)&(LLk-1);    // labase maintained even if !demand look
00276         return 1;
00277 }
00278 
00279 /* check to see if the current input symbol matches any token in a set.
00280  * During NON demand lookahead mode, dirty will always be 0 and
00281  * hence the extra code for consuming tokens in _match is never
00282  * executed; the same routine can be used for both modes.
00283  */
00284 int ANTLRParser::
00285 _setmatch(SetWordType *tset, ANTLRChar **MissText,
00286            ANTLRTokenType *MissTok, _ANTLRTokenPtr *BadTok,
00287            SetWordType **MissSet)
00288 {
00289         if ( dirty==LLk ) {
00290                 consume();
00291         }
00292         if ( !set_el(LA(1), tset) ) {
00293                 *MissText=NULL;
00294                 *MissTok= (ANTLRTokenType)0; *BadTok=LT(1);
00295                 *MissSet=tset;
00296                 return 0;
00297         }
00298         dirty++;
00299         labase = (labase+1)&(LLk-1);    // labase maintained even if !demand look
00300         return 1;
00301 }
00302 
00303 int ANTLRParser::
00304 _setmatch_wsig(SetWordType *tset)
00305 {
00306         if ( dirty==LLk ) {
00307                 consume();
00308         }
00309         if ( !set_el(LA(1), tset) ) return 0;
00310         dirty++;
00311         labase = (labase+1)&(LLk-1);    // labase maintained even if !demand look
00312         return 1;
00313 }
00314 
00315                    /* Exception handling routines */
00316 //
00317 //  7-Apr-97 133MR1
00318 //           Change suggested by Eli Sternheim (eli@interhdl.com)
00319 //
00320 void ANTLRParser::
00321 consumeUntil(SetWordType *st)
00322 {
00323         ANTLRTokenType          tmp;                                                            // MR1
00324         const                   int Eof=1;                                          // MR1
00325         while ( !set_el( (tmp=LA(1)), st) && tmp!=Eof) { consume(); }       // MR1
00326 }
00327 
00328 //
00329 //  7-Apr-97 133MR1
00330 //           Change suggested by Eli Sternheim (eli@interhdl.com)
00331 //
00332 void ANTLRParser::
00333 consumeUntilToken(int t)
00334 {
00335         int     tmp;                                                            // MR1
00336         const   int Eof=1;                                                  // MR1
00337         while ( (tmp=LA(1)) !=t && tmp!=Eof) { consume(); }                 // MR1
00338 }
00339 
00340 
00341                         /* Old error stuff */
00342 
00343 void ANTLRParser::
00344 resynch(SetWordType *wd,SetWordType mask)
00345 {
00346 
00347 /* MR8              S.Bochnak@microtool.com.pl                          */
00348 /* MR8              Change file scope static "consumed" to instance var */
00349 
00350         /* if you enter here without having consumed a token from last resynch
00351          * force a token consumption.
00352          */
00353 /* MR8 */       if ( !resynchConsumed ) {consume(); resynchConsumed=1; return;}
00354 
00355         /* if current token is in resynch set, we've got what we wanted */
00356 
00357 /* MR8 */       if ( wd[LA(1)]&mask || LA(1) == eofToken ) {resynchConsumed=0; return;}
00358         
00359         /* scan until we find something in the resynch set */
00360 
00361                 while ( !(wd[LA(1)]&mask) && LA(1) != eofToken ) {consume();}
00362 
00363 /* MR8 */       resynchConsumed=1;
00364 }
00365 
00366 /* standard error reporting function that assumes DLG-based scanners;
00367  * you should redefine in subclass to change it or if you use your
00368  * own scanner.
00369  */
00370 void ANTLRParser::
00371 syn(_ANTLRTokenPtr tok, ANTLRChar *egroup, SetWordType *eset,
00372         ANTLRTokenType etok, int k)
00373 {
00374         int line;
00375 
00376         line = LT(1)->getLine();
00377 
00378     syntaxErrCount++;                                   /* MR11 */
00379         fprintf(stderr, "line %d: syntax error at \"%s\"",
00380                                         line, LT(1)->getText());
00381         if ( !etok && !eset ) {fprintf(stderr, "\n"); return;}
00382         if ( k==1 ) fprintf(stderr, " missing");
00383         else
00384         {
00385                 fprintf(stderr, "; \"%s\" not", LT(1)->getText());
00386                 if ( set_deg(eset)>1 ) fprintf(stderr, " in");
00387         }
00388         if ( set_deg(eset)>0 ) edecode(eset);
00389         else fprintf(stderr, " %s", token_tbl[etok]);
00390         if ( strlen(egroup) > 0 ) fprintf(stderr, " in %s", egroup);
00391         fprintf(stderr, "\n");
00392 }
00393 
00394 /* is b an element of set p? */
00395 int ANTLRParser::
00396 set_el(ANTLRTokenType b, SetWordType *p)
00397 {
00398         return( p[DIVWORD(b)] & bitmask[MODWORD(b)] );
00399 }
00400 
00401 int ANTLRParser::
00402 set_deg(SetWordType *a)
00403 {
00404         /* Fast compute degree of a set... the number
00405            of elements present in the set.  Assumes
00406            that all word bits are used in the set
00407         */
00408         register SetWordType *p = a;
00409         register SetWordType *endp = &(a[bsetsize]);
00410         register int degree = 0;
00411 
00412         if ( a == NULL ) return 0;
00413         while ( p < endp )
00414         {
00415                 register SetWordType t = *p;
00416                 register SetWordType *b = &(bitmask[0]);
00417                 do {
00418                         if (t & *b) ++degree;
00419                 } while (++b < &(bitmask[sizeof(SetWordType)*8]));
00420                 p++;
00421         }
00422 
00423         return(degree);
00424 }
00425 
00426 void ANTLRParser::
00427 edecode(SetWordType *a)
00428 {
00429         register SetWordType *p = a;
00430         register SetWordType *endp = &(p[bsetsize]);
00431         register unsigned e = 0;
00432 
00433         if ( set_deg(a)>1 ) fprintf(stderr, " {");
00434         do {
00435                 register SetWordType t = *p;
00436                 register SetWordType *b = &(bitmask[0]);
00437                 do {
00438                         if ( t & *b ) fprintf(stderr, " %s", token_tbl[e]);
00439                         e++;
00440                 } while (++b < &(bitmask[sizeof(SetWordType)*8]));
00441         } while (++p < endp);
00442         if ( set_deg(a)>1 ) fprintf(stderr, " }");
00443 }
00444 
00445 /* input looks like:
00446  *      zzFAIL(k, e1, e2, ...,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk)
00447  * where the zzMiss stuff is set here to the token that did not match
00448  * (and which set wasn't it a member of).
00449  */
00450 
00451 // MR9 29-Sep-97    Stan Bochnak (S.Bochnak@microTool.com.pl)
00452 // MR9              Original fix to static allocated text didn't
00453 // MR9                work because a pointer to it was passed back
00454 // MR9                to caller.  Replace with instance variable.
00455 
00456 const int   SETWORDCOUNT=20;
00457 
00458 void
00459 ANTLRParser::FAIL(int k, ...)
00460 {
00461 //
00462 //  MR1 10-Apr-97       
00463 //
00464 
00465     if (zzFAILtext == NULL) zzFAILtext=new char [1000];          // MR9
00466     SetWordType **f=new SetWordType *[SETWORDCOUNT];             // MR1 // MR9
00467     SetWordType **miss_set;
00468     ANTLRChar **miss_text;
00469     _ANTLRTokenPtr *bad_tok;
00470     ANTLRChar **bad_text;
00471 //
00472 //  7-Apr-97 133MR1
00473 //              err_k is passed as a "int *", not "unsigned *"
00474 //
00475     int *err_k;                                                         // MR1
00476     int i;
00477     va_list ap;
00478 
00479     va_start(ap, k);
00480 
00481     zzFAILtext[0] = '\0';
00482         if ( k > SETWORDCOUNT ) panic("FAIL: overflowed buffer");
00483     for (i=1; i<=k; i++)    /* collect all lookahead sets */
00484     {
00485         f[i-1] = va_arg(ap, SetWordType *);
00486     }
00487     for (i=1; i<=k; i++)    /* look for offending token */
00488     {
00489         if ( i>1 ) strcat(zzFAILtext, " ");
00490         strcat(zzFAILtext, LT(i)->getText());
00491         if ( !set_el(LA(i), f[i-1]) ) break;
00492     }
00493     miss_set = va_arg(ap, SetWordType **);
00494     miss_text = va_arg(ap, ANTLRChar **);
00495     bad_tok = va_arg(ap, _ANTLRTokenPtr *);
00496     bad_text = va_arg(ap, ANTLRChar **);
00497     err_k = va_arg(ap, int *);                                                          // MR1
00498     if ( i>k )
00499     {
00500         /* bad; lookahead is permutation that cannot be matched,
00501          * but, the ith token of lookahead is valid at the ith position
00502          * (The old LL sub 1 (k) versus LL(k) parsing technique)
00503          */
00504         *miss_set = NULL;
00505         *miss_text = LT(1)->getText();
00506         *bad_tok = LT(1);
00507         *bad_text = (*bad_tok)->getText();
00508         *err_k = k;
00509 //
00510 //  MR4 20-May-97       erroneously deleted contents of f[]
00511 //  MR4                         reported by Bruce Guenter (bruceg@qcc.sk.ca)
00512 //  MR1 10-Apr-97       release temporary storage
00513 //
00514       delete [] f;                                                      // MR1
00515       return;                                                           // MR1
00516     }
00517 /*  fprintf(stderr, "%s not in %dth set\n", zztokens[LA(i)], i);*/
00518     *miss_set = f[i-1];
00519     *miss_text = zzFAILtext;
00520     *bad_tok = LT(i);
00521     *bad_text = (*bad_tok)->getText();
00522     if ( i==1 ) *err_k = 1;
00523     else *err_k = k;
00524 //
00525 //  MR4 20-May-97       erroneously deleted contents of f[]
00526 //  MR4                       reported by Bruce Guenter (bruceg@qcc.sk.ca)
00527 //  MR1 10-Apr-97       release temporary storage
00528 //
00529     delete [] f;                                                        // MR1
00530     return;                                                             // MR1
00531 }
00532 
00533 int ANTLRParser::
00534 _match_wdfltsig(ANTLRTokenType tokenWanted, SetWordType *whatFollows)
00535 {
00536         if ( dirty==LLk ) consume();
00537 
00538         if ( LA(1)!=tokenWanted )
00539         {
00540         syntaxErrCount++;                                   /* MR11 */
00541                 fprintf(stderr,
00542                                 "line %d: syntax error at \"%s\" missing %s\n",
00543                                 LT(1)->getLine(),
00544                                 (LA(1)==eofToken)?"<eof>":LT(1)->getText(),
00545                                 token_tbl[tokenWanted]);
00546                 consumeUntil( whatFollows );
00547                 return 0;
00548         }
00549         else {
00550                 dirty++;
00551                 labase = (labase+1)&(LLk-1); // labase maintained even if !demand look
00552 /*              if ( !demand_look ) consume(); */
00553                 return 1;
00554         }
00555 }
00556 
00557 
00558 int ANTLRParser::
00559 _setmatch_wdfltsig(SetWordType *tokensWanted,
00560                                         ANTLRTokenType tokenTypeOfSet,
00561                                         SetWordType *whatFollows)
00562 {
00563         if ( dirty==LLk ) consume();
00564         if ( !set_el(LA(1), tokensWanted) )
00565         {
00566         syntaxErrCount++;                                   /* MR11 */
00567                 fprintf(stderr,
00568                                 "line %d: syntax error at \"%s\" missing %s\n",
00569                                 LT(1)->getLine(),
00570                                 (LA(1)==eofToken)?"<eof>":LT(1)->getText(),
00571                                 token_tbl[tokenTypeOfSet]);
00572                 consumeUntil( whatFollows );
00573                 return 0;
00574         }
00575         else {
00576                 dirty++;
00577                 labase = (labase+1)&(LLk-1); // labase maintained even if !demand look
00578 /*              if ( !demand_look ) consume(); */
00579                 return 1;
00580         }
00581 }
00582 
00583 char *ANTLRParser::
00584 eMsgd(char *err,int d)
00585 {
00586         sprintf(eMsgBuffer, err, d);    // dangerous, but I don't care
00587         return eMsgBuffer;
00588 }
00589 
00590 char *ANTLRParser::
00591 eMsg(char *err, char *s)
00592 {
00593         sprintf(eMsgBuffer, err, s);
00594         return eMsgBuffer;
00595 }
00596 
00597 char *ANTLRParser::
00598 eMsg2(char *err,char *s, char *t)
00599 {
00600         sprintf(eMsgBuffer, err, s, t);
00601         return eMsgBuffer;
00602 }
00603 
00604 void ANTLRParser::
00605 panic(char *msg)
00606 {
00607         fprintf(stderr, "ANTLR panic: %s\n", msg);
00608         exit(PCCTS_EXIT_FAILURE);           // MR1
00609 }
00610 
00611 const ANTLRChar *ANTLRParser::          // MR1
00612 parserTokenName(int tok) {              // MR1
00613         return token_tbl[tok];              // MR1
00614 }                                       // MR1
00615 
00616 void ANTLRParser::traceGuessDone(const ANTLRParserState *state) {
00617 
00618   int   doIt=0;
00619 
00620   if (traceCurrentRuleName == NULL) return;
00621 
00622   if (traceOptionValue <= 0) {
00623     doIt=0;
00624   } else if (traceGuessOptionValue <= 0) {
00625     doIt=0;
00626   } else {
00627     doIt=1;
00628   };
00629 
00630   if (doIt) {
00631     fprintf(stderr,"guess done - returning to rule %s {\"%s\"} at depth %d",
00632         state->traceCurrentRuleName,
00633         LT(1)->getType() == eofToken ? "@" : LT(1)->getText(),
00634         state->traceDepth);
00635     if (state->guessing != 0) {
00636       fprintf(stderr," (guess mode continues - an enclosing guess is still active)");
00637     } else {
00638       fprintf(stderr," (guess mode ends)");
00639     };
00640     fprintf(stderr,"\n");
00641   };
00642 }
00643 
00644 void ANTLRParser::traceGuessFail() {
00645 
00646   int   doIt=0;
00647 
00648   if (traceOptionValue <= 0) {
00649     doIt=0;
00650   } else if (guessing && traceGuessOptionValue <= 0) {
00651     doIt=0;
00652   } else {
00653     doIt=1;
00654   };
00655 
00656   if (doIt) {
00657     fprintf(stderr,"guess failed\n");
00658   };
00659 }
00660 
00661 /* traceOption:
00662      zero value turns off trace
00663 */
00664 
00665 void ANTLRParser::tracein(const ANTLRChar * rule) {
00666 
00667   int       doIt=0;
00668 
00669   traceDepth++;
00670   traceCurrentRuleName=rule;
00671 
00672   if (traceOptionValue <= 0) {
00673     doIt=0;
00674   } else if (guessing && traceGuessOptionValue <= 0) {
00675     doIt=0;
00676   } else {
00677     doIt=1;
00678   };
00679 
00680   if (doIt) {
00681     fprintf(stderr,"enter rule %s {\"%s\"} depth %d",
00682             rule,
00683             LT(1)->getType() == eofToken ? "@" : LT(1)->getText(),
00684             traceDepth);
00685     if (guessing) fprintf(stderr," guessing");
00686     fprintf(stderr,"\n");
00687   };
00688   return;
00689 }
00690 
00691 void ANTLRParser::traceout(const ANTLRChar * rule) {
00692 
00693   int       doIt=0;
00694 
00695   traceDepth--;
00696 
00697   if (traceOptionValue <= 0) {
00698     doIt=0;
00699   } else if (guessing && traceGuessOptionValue <= 0) {
00700     doIt=0;
00701   } else {
00702     doIt=1;
00703   };
00704 
00705   if (doIt) {
00706     fprintf(stderr,"exit rule %s {\"%s\"} depth %d",
00707             rule,
00708             LT(1)->getType() == eofToken ? "@" : LT(1)->getText(),
00709             traceDepth+1);
00710     if (guessing) fprintf(stderr," guessing");
00711     fprintf(stderr,"\n");
00712   };
00713 }
00714 
00715 int ANTLRParser::traceOption(int delta) {
00716 
00717     int     prevValue=traceOptionValue;
00718 
00719     traceOptionValue=traceOptionValue+delta;
00720 
00721     if (traceCurrentRuleName != NULL) {
00722       if (prevValue <= 0 && traceOptionValue > 0) {
00723         fprintf(stderr,"trace enabled in rule %s depth %d\n",traceCurrentRuleName,traceDepth);
00724       };
00725       if (prevValue > 0 && traceOptionValue <= 0) {
00726         fprintf(stderr,"trace disabled in rule %s depth %d\n",traceCurrentRuleName,traceDepth);
00727       };
00728     };
00729 
00730     return  prevValue;
00731 }
00732 
00733 int ANTLRParser::traceGuessOption(int delta) {
00734 
00735     int     prevValue=traceGuessOptionValue;
00736 
00737     traceGuessOptionValue=traceGuessOptionValue+delta;
00738 
00739     if (traceCurrentRuleName != NULL) {
00740       if (prevValue <= 0 && traceGuessOptionValue > 0) {
00741         fprintf(stderr,"guess trace enabled in rule %s depth %d\n",traceCurrentRuleName,traceDepth);
00742       };
00743       if (prevValue > 0 && traceGuessOptionValue <= 0) {
00744         fprintf(stderr,"guess trace disabled in rule %s depth %d\n",traceCurrentRuleName,traceDepth);
00745       };
00746     };
00747     return prevValue;
00748 }