CCCC - C and C++ Code Counter  9999-git
CCCC Development version (post-3.1.4)
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
ParseUtility Class Reference

#include <cccc_utl.h>

Collaboration diagram for ParseUtility:
[legend]

Public Member Functions

 ParseUtility (ANTLRParser *parser)
 
 ~ParseUtility ()
 
void tracein (const char *rulename, int guessing, ANTLRAbstractToken *tok)
 
void traceout (const char *rulename, int guessing, ANTLRAbstractToken *tok)
 
void syn (_ANTLRTokenPtr tok, ANTLRChar *egroup, SetWordType *eset, ANTLRTokenType etok, int k)
 
string lookahead_text (int n)
 
void resynchronize (int initial_nesting, SetWordType *resync_token_class, ANTLRTokenPtr &resync_token)
 
string scopeCombine (const string &baseScope, const string &name)
 

Static Public Member Functions

static ParseUtilitycurrentInstance ()
 

Private Member Functions

 ParseUtility (const ParseUtility &)
 
const ParseUtilityoperator= (const ParseUtility &)
 

Private Attributes

ANTLR_Assisted_Parserparser
 
int trace_depth
 

Static Private Attributes

static ParseUtilitytheCurrentInstance =NULL
 
static int stack_depth
 
static string stack_tokentext [MAX_STACK_DEPTH]
 
static int stack_tokenline [MAX_STACK_DEPTH]
 
static string stack_rules [MAX_STACK_DEPTH]
 

Detailed Description

Definition at line 139 of file cccc_utl.h.

Constructor & Destructor Documentation

ParseUtility::ParseUtility ( ANTLRParser parser)

Definition at line 183 of file cccc_utl.cc.

184 {
185  // This is designed as a serial-singleton class (e.g. many
186  // instances may exist over time but no more than one at a
187  // time).
188  // For the lifetime of an instance, the static member theCurrentInstance
189  // points to it. When no instance exists, this pointer is null.
190  assert(theCurrentInstance==NULL);
191  theCurrentInstance=this;
192 
193  trace_depth=0;
194  stack_depth=0;
195  this->parser=(ANTLR_Assisted_Parser*)parser;
196 
197 }
static int stack_depth
Definition: cccc_utl.h:176
int trace_depth
Definition: cccc_utl.h:175
static ParseUtility * theCurrentInstance
Definition: cccc_utl.h:172
ParseUtility::~ParseUtility ( )

Definition at line 199 of file cccc_utl.cc.

200 {
201  theCurrentInstance=NULL;
202 }
static ParseUtility * theCurrentInstance
Definition: cccc_utl.h:172
ParseUtility::ParseUtility ( const ParseUtility )
private

Member Function Documentation

static ParseUtility* ParseUtility::currentInstance ( )
inlinestatic

Definition at line 169 of file cccc_utl.h.

169 { return theCurrentInstance; }
static ParseUtility * theCurrentInstance
Definition: cccc_utl.h:172
string ParseUtility::lookahead_text ( int  n)

Definition at line 99 of file cccc_utl.cc.

100 {
101  static string retval;
102  retval="";
103  int i;
104  for(i=1; i<=n; i++)
105  {
106  if(parser->LT(i) != NULL)
107  {
108  retval=retval+parser->LT(i)->getText();
109  retval=retval+" ";
110  }
111  }
112  return retval;
113 }
ANTLR_Assisted_Parser * parser
Definition: cccc_utl.h:174
const ParseUtility& ParseUtility::operator= ( const ParseUtility )
private
void ParseUtility::resynchronize ( int  initial_nesting,
SetWordType *  resync_token_class,
ANTLRTokenPtr &  resync_token 
)

Definition at line 115 of file cccc_utl.cc.

118 {
119  // the interface for resynchronisation is as follows:
120  // the caller supplies a nesting level at which the resynchronisation must
121  // occur, and a token class containing all of the tokens which can
122  // be accepted to delimit the resynchronisation
123  // this function will scan until it finds that it is at the correct level and
124  // the next token of lookahead is in the resynchronisation set
125  // it will then accept as many tokens from the resynchronisation set as
126  // are available, consolidating the text of the tokens accepted
127  // as the text associated with the last token
128  string resync_text="...";
129 
130  string string1=parser->LT(1)->getText();
131  int line1=parser->LT(1)->getLine();
132  string string2;
133  int line2=0;
134 
135  int resynchronising=1;
136  while(resynchronising)
137  {
138  parser->consumeUntil(resync_token_class);
139  if(
140  (MY_TOK(parser->LT(1))->getNestingLevel() > initial_nesting) &&
141  (parser->LT(2) != NULL)
142  )
143  {
144  parser->consume();
145  }
146  else
147  {
148  // we are ready to resynchronise
149  resynchronising=0;
150  string2=parser->LT(1)->getText();
151  line2=parser->LT(1)->getLine();
152  }
153  }
154 
155  // we now consume a succession of tokens from the resynchronisation token
156  // class until we come across a token which is not in the set, or the
157  // nesting level changes
158  resync_token=parser->LT(1);
159  while(
160  parser->set_el(parser->LT(1)->getType(),resync_token_class) &&
161  ( MY_TOK(parser->LT(1))->getNestingLevel() == initial_nesting)
162  )
163  {
164  string2=parser->LT(1)->getText();
165  line2=parser->LT(1)->getLine();
166 
167  resync_text+=parser->LT(1)->getText();
168  resync_text+=" ";
169  resync_token=parser->LT(1);
170  resync_token->setText(resync_text.c_str());
171  parser->consume();
172  }
173 
174  cerr << "Unrecognized section from "
175  << string1.c_str() << " on line " << line1 << " to "
176  << string2.c_str() << " on line " << line2 << endl
177  << "=====ignored section begins=====" << endl
178  << resync_text.c_str() << endl
179  << "===== ignored section ends =====" << endl;
180 }
#define MY_TOK(t)
Definition: cccc_tok.h:86
ANTLR_Assisted_Parser * parser
Definition: cccc_utl.h:174
string ParseUtility::scopeCombine ( const string &  baseScope,
const string &  name 
)

Definition at line 207 of file cccc_utl.cc.

208 {
209  // I am presently (as at 3.pre44) experimenting with
210  // how I handle scopes. The present code has a policy
211  // of discarding scope information altogether and defining
212  // modules based solely on the final component of the
213  // fully qualified name.
214  // This variable may become a parameter to control policy in this
215  // area.
216  bool bIgnoreScope=true;
217  string retval;
218  if(bIgnoreScope)
219  {
220  retval=name;
221  }
222  else if(baseScope.size()>0 && name.size()>0)
223  {
224  retval=baseScope+"::"+name;
225  }
226  else
227  {
228  retval=baseScope+name;
229  }
230 
231  return retval;
232 }

Here is the call graph for this function:

void ParseUtility::syn ( _ANTLRTokenPtr  tok,
ANTLRChar *  egroup,
SetWordType *  eset,
ANTLRTokenType  etok,
int  k 
)

Definition at line 549 of file cccc_utl.cc.

552 {
553  string filename=ParseStore::currentInstance()->filename();
554  if(tok != NULL)
555  {
556  cerr << filename << '(' << tok->getLine() << "):"
557  << " syntax error at token " << tok->getText() << endl;
558  }
559  else
560  {
561  cerr << filename << "(0): syntax error at null token" << endl;
562  }
563 
564 #if 1
565  // The logic in the other half of this #if section
566  // generated too much noise for some people's taste.
567  // It's only really useful to myself (TJL) or anyone
568  // else with a taste for debugging cccc.g/java.g etc.
569  int i=stack_depth-1;
570  cerr << filename << '(' << stack_tokenline[i]
571  << "): trying to match " << stack_rules[i]
572  << " at '" << stack_tokentext[i] << "'"
573  << endl;
574 #else
575  cerr << "Parser context:" << endl;
576  for(int i=stack_depth-1; i>=0; i--)
577  {
578  cerr << filename << '(' << stack_tokenline[i]
579  << "): trying to match " << stack_rules[i]
580  << " at '" << stack_tokentext[i] << "'"
581  << endl;
582  }
583  cerr << endl;
584 #endif
585 }
static int stack_depth
Definition: cccc_utl.h:176
static string stack_rules[MAX_STACK_DEPTH]
Definition: cccc_utl.h:179
static ParseStore * currentInstance()
Definition: cccc_utl.h:269
string filename()
Definition: cccc_utl.cc:281
static int stack_tokenline[MAX_STACK_DEPTH]
Definition: cccc_utl.h:178
static string stack_tokentext[MAX_STACK_DEPTH]
Definition: cccc_utl.h:177

Here is the call graph for this function:

void ParseUtility::tracein ( const char *  rulename,
int  guessing,
ANTLRAbstractToken *  tok 
)

Definition at line 501 of file cccc_utl.cc.

504 {
505  if(guessing == 0)
506  {
507  stack_tokentext[stack_depth]=tok->getText();
508  stack_tokenline[stack_depth]=tok->getLine();
509  stack_rules[stack_depth]=rulename;
510  stack_depth++;
511  }
512 
513  // first put out the token details
514  toktrace(tok);
515 
516  // then the indented recognition trace
517  rectrace(rulename,"-> ",guessing,tok);
518 }
static void rectrace(const char *rulename, const char *dir_indic, int guessing, ANTLRAbstractToken *tok)
Definition: cccc_utl.cc:481
static int stack_depth
Definition: cccc_utl.h:176
static void toktrace(ANTLRAbstractToken *tok)
Definition: cccc_utl.cc:460
static string stack_rules[MAX_STACK_DEPTH]
Definition: cccc_utl.h:179
static int stack_tokenline[MAX_STACK_DEPTH]
Definition: cccc_utl.h:178
static string stack_tokentext[MAX_STACK_DEPTH]
Definition: cccc_utl.h:177

Here is the call graph for this function:

void ParseUtility::traceout ( const char *  rulename,
int  guessing,
ANTLRAbstractToken *  tok 
)

Definition at line 520 of file cccc_utl.cc.

523 {
524  if(guessing == 0)
525  {
526  stack_depth--;
527  // some error checking...
528  if(stack_depth<0)
529  {
530  cerr << "ParseUtility::traceout negative stack depth - "
531  << "exiting from rule " << rulename
532  << " at " << tok->getText() << " on line " << tok->getLine()
533  << endl;
534  }
535  else if(rulename!=stack_rules[stack_depth])
536  {
537  cerr << "ParseStore::traceout rule name mismatch - "
538  << rulename << "!=" << stack_rules[stack_depth] << endl;
539  }
543  }
544  // first put out the token details
545  toktrace(tok);
546  rectrace(rulename,"<- ",guessing,tok);
547 }
static void rectrace(const char *rulename, const char *dir_indic, int guessing, ANTLRAbstractToken *tok)
Definition: cccc_utl.cc:481
static int stack_depth
Definition: cccc_utl.h:176
static void toktrace(ANTLRAbstractToken *tok)
Definition: cccc_utl.cc:460
static string stack_rules[MAX_STACK_DEPTH]
Definition: cccc_utl.h:179
static int stack_tokenline[MAX_STACK_DEPTH]
Definition: cccc_utl.h:178
static string stack_tokentext[MAX_STACK_DEPTH]
Definition: cccc_utl.h:177

Here is the call graph for this function:

Member Data Documentation

ANTLR_Assisted_Parser* ParseUtility::parser
private

Definition at line 174 of file cccc_utl.h.

int ParseUtility::stack_depth
staticprivate

Definition at line 176 of file cccc_utl.h.

string ParseUtility::stack_rules
staticprivate

Definition at line 179 of file cccc_utl.h.

int ParseUtility::stack_tokenline
staticprivate

Definition at line 178 of file cccc_utl.h.

string ParseUtility::stack_tokentext
staticprivate

Definition at line 177 of file cccc_utl.h.

ParseUtility * ParseUtility::theCurrentInstance =NULL
staticprivate

Definition at line 172 of file cccc_utl.h.

int ParseUtility::trace_depth
private

Definition at line 175 of file cccc_utl.h.


The documentation for this class was generated from the following files: