Avoiding use of static variables

This commit is contained in:
Pierre Pronchery 2008-02-27 00:07:28 +00:00
parent e419f54298
commit e03c887ee1

View File

@ -41,6 +41,12 @@ typedef struct _CppParser
Parser * parser; Parser * parser;
const CppOperator * operators; const CppOperator * operators;
size_t operators_cnt; size_t operators_cnt;
/* for cpp_filter_newlines */
int newlines_last;
int newlines_last_cnt;
/* for cpp_filter_trigraphs */
int trigraphs_last;
int trigraphs_last_cnt;
} CppParser; } CppParser;
@ -139,16 +145,17 @@ static CppParser * _cppparser_new(char const * filename, int filters,
return NULL; return NULL;
cppparser->operators = operators; cppparser->operators = operators;
cppparser->operators_cnt = operators_cnt; cppparser->operators_cnt = operators_cnt;
cppparser->newlines_last_cnt = 0;
cppparser->trigraphs_last_cnt = 0;
if((cppparser->parser = parser_new(filename)) == NULL) if((cppparser->parser = parser_new(filename)) == NULL)
{ {
_cppparser_delete(cppparser); _cppparser_delete(cppparser);
return NULL; return NULL;
} }
parser_add_filter(cppparser->parser, _cpp_filter_newlines, parser_add_filter(cppparser->parser, _cpp_filter_newlines, cppparser);
cppparser->parser);
if(filters & CPP_FILTER_TRIGRAPH) if(filters & CPP_FILTER_TRIGRAPH)
parser_add_filter(cppparser->parser, _cpp_filter_trigraphs, parser_add_filter(cppparser->parser, _cpp_filter_trigraphs,
cppparser->parser); cppparser);
parser_add_callback(cppparser->parser, _cpp_callback_whitespace, NULL); parser_add_callback(cppparser->parser, _cpp_callback_whitespace, NULL);
parser_add_callback(cppparser->parser, _cpp_callback_comment, NULL); parser_add_callback(cppparser->parser, _cpp_callback_comment, NULL);
parser_add_callback(cppparser->parser, _cpp_callback_comma, NULL); parser_add_callback(cppparser->parser, _cpp_callback_comma, NULL);
@ -175,66 +182,61 @@ static void _cppparser_delete(CppParser * cppparser)
/* cpp_filter_newlines */ /* cpp_filter_newlines */
static int _cpp_filter_newlines(int * c, void * data) static int _cpp_filter_newlines(int * c, void * data)
{ {
Parser * parser = data; CppParser * cp = data;
/* FIXME obtain from data */
static int last;
static int last_cnt = 0;
if(last_cnt) if(cp->newlines_last_cnt != 0)
{ {
last_cnt--; cp->newlines_last_cnt--;
*c = last; *c = cp->newlines_last;
return 0; return 0;
} }
if(*c != '\\') if(*c != '\\')
return 0; return 0;
if((*c = parser_scan(parser)) == '\n') if((*c = parser_scan(cp->parser)) == '\n')
{ {
*c = parser_scan(parser); /* skip the newline */ *c = parser_scan(cp->parser); /* skip the newline */
return 0; return 0;
} }
last = *c; cp->newlines_last = *c;
last_cnt = 1; cp->newlines_last_cnt = 1;
*c = '\\'; *c = '\\';
return 1; return 1;
} }
/* cpp_filter_trigraphs */ /* cpp_filter_trigraphs */
static int _trigraph_get(int last, int * c); static int _trigraphs_get(int last, int * c);
static int _cpp_filter_trigraphs(int * c, void * data) static int _cpp_filter_trigraphs(int * c, void * data)
{ {
Parser * parser = data; CppParser * cp = data;
static int last;
static int last_cnt = 0;
if(last_cnt == 2) if(cp->trigraphs_last_cnt == 2)
{ {
last_cnt--; cp->trigraphs_last_cnt--;
*c = '?'; *c = '?';
return 0; return 0;
} }
else if(last_cnt == 1) else if(cp->trigraphs_last_cnt == 1)
{ {
last_cnt--; cp->trigraphs_last_cnt--;
*c = last; *c = cp->trigraphs_last;
return 0; return 0;
} }
if(*c != '?') if(*c != '?')
return 0; return 0;
if((last = parser_scan(parser)) != '?') if((cp->trigraphs_last = parser_scan(cp->parser)) != '?')
{ {
last_cnt = 1; cp->trigraphs_last_cnt = 1;
return 1; return 1;
} }
last = parser_scan(parser); cp->trigraphs_last = parser_scan(cp->parser);
if(_trigraph_get(last, c) != 0) if(_trigraphs_get(cp->trigraphs_last, c) != 0)
{ {
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "DEBUG: last=%c\n", last); fprintf(stderr, "DEBUG: last=%c\n", last);
#endif #endif
last_cnt = 2; cp->trigraphs_last_cnt = 2;
return 2; return 2;
} }
#ifdef DEBUG #ifdef DEBUG
@ -243,7 +245,7 @@ static int _cpp_filter_trigraphs(int * c, void * data)
return 0; return 0;
} }
static int _trigraph_get(int last, int * c) static int _trigraphs_get(int last, int * c)
{ {
switch(last) switch(last)
{ {