mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-19 09:13:04 +00:00
reader: reduce the "scope" of global variables
We have too many global variables, adding structure would help. For a start, let's hide some of the variables closer to their usage. * src/getargs.c, src/files.h (current_file): Move to... * src/scan-gram.c: here. * src/scan-gram.h (gram_in, gram__flex_debug): Remove, make them private to the scanner. * src/reader.h, src/reader.c (reader): Take a grammar file as argument. Move the handling of scanner variables to... * src/scan-gram.l (gram_scanner_open, gram_scanner_close): here. (gram_scanner_initialize): Remove, replaced by gram_scanner_open. * src/main.c: Adjust.
This commit is contained in:
@@ -68,7 +68,6 @@ static generated_file *generated_files = NULL;
|
|||||||
static int generated_files_size = 0;
|
static int generated_files_size = 0;
|
||||||
|
|
||||||
uniqstr grammar_file = NULL;
|
uniqstr grammar_file = NULL;
|
||||||
uniqstr current_file = NULL;
|
|
||||||
|
|
||||||
/* If --output=dir/foo.c was specified,
|
/* If --output=dir/foo.c was specified,
|
||||||
DIR_PREFIX is 'dir/' and ALL_BUT_EXT and ALL_BUT_TAB_EXT are 'dir/foo'.
|
DIR_PREFIX is 'dir/' and ALL_BUT_EXT and ALL_BUT_TAB_EXT are 'dir/foo'.
|
||||||
|
|||||||
@@ -58,9 +58,6 @@ extern char *dir_prefix;
|
|||||||
and therefore GCC warns about a name clash. */
|
and therefore GCC warns about a name clash. */
|
||||||
extern uniqstr grammar_file;
|
extern uniqstr grammar_file;
|
||||||
|
|
||||||
/* The current file name. Might change with #line. */
|
|
||||||
extern uniqstr current_file;
|
|
||||||
|
|
||||||
/* The computed base for output file names. */
|
/* The computed base for output file names. */
|
||||||
extern char *all_but_ext;
|
extern char *all_but_ext;
|
||||||
|
|
||||||
|
|||||||
@@ -853,7 +853,7 @@ getargs (int argc, char *argv[])
|
|||||||
usage (EXIT_FAILURE);
|
usage (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
current_file = grammar_file = uniqstr_new (argv[optind]);
|
grammar_file = uniqstr_new (argv[optind]);
|
||||||
MUSCLE_INSERT_C_STRING ("file_name", grammar_file);
|
MUSCLE_INSERT_C_STRING ("file_name", grammar_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ main (int argc, char *argv[])
|
|||||||
the grammar; see gram.h. */
|
the grammar; see gram.h. */
|
||||||
|
|
||||||
timevar_push (tv_reader);
|
timevar_push (tv_reader);
|
||||||
reader ();
|
reader (grammar_file);
|
||||||
timevar_pop (tv_reader);
|
timevar_pop (tv_reader);
|
||||||
|
|
||||||
if (complaint_status == status_complaint)
|
if (complaint_status == status_complaint)
|
||||||
|
|||||||
12
src/reader.c
12
src/reader.c
@@ -710,24 +710,20 @@ packgram (void)
|
|||||||
`------------------------------------------------------------------*/
|
`------------------------------------------------------------------*/
|
||||||
|
|
||||||
void
|
void
|
||||||
reader (void)
|
reader (const char *gram)
|
||||||
{
|
{
|
||||||
/* Set up symbol_table, semantic_type_table, and the built-in
|
/* Set up symbol_table, semantic_type_table, and the built-in
|
||||||
symbols. */
|
symbols. */
|
||||||
symbols_new ();
|
symbols_new ();
|
||||||
|
|
||||||
gram_in = xfopen (grammar_file, "r");
|
gram_scanner_open (gram);
|
||||||
|
|
||||||
gram__flex_debug = trace_flag & trace_scan;
|
|
||||||
gram_debug = trace_flag & trace_parse;
|
|
||||||
gram_scanner_initialize ();
|
|
||||||
gram_parse ();
|
gram_parse ();
|
||||||
|
gram_scanner_close ();
|
||||||
|
|
||||||
prepare_percent_define_front_end_variables ();
|
prepare_percent_define_front_end_variables ();
|
||||||
|
|
||||||
if (complaint_status < status_complaint)
|
if (complaint_status < status_complaint)
|
||||||
check_and_convert_grammar ();
|
check_and_convert_grammar ();
|
||||||
|
|
||||||
xfclose (gram_in);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ void grammar_current_rule_action_append (const char *action, location loc,
|
|||||||
named_ref *nref, uniqstr tag);
|
named_ref *nref, uniqstr tag);
|
||||||
/* Attach a PREDICATE to the current rule. */
|
/* Attach a PREDICATE to the current rule. */
|
||||||
void grammar_current_rule_predicate_append (const char *predicate, location loc);
|
void grammar_current_rule_predicate_append (const char *predicate, location loc);
|
||||||
void reader (void);
|
void reader (const char *gram);
|
||||||
void free_merger_functions (void);
|
void free_merger_functions (void);
|
||||||
|
|
||||||
extern merger_list *merge_functions;
|
extern merger_list *merge_functions;
|
||||||
|
|||||||
@@ -21,18 +21,15 @@
|
|||||||
#ifndef SCAN_GRAM_H_
|
#ifndef SCAN_GRAM_H_
|
||||||
# define SCAN_GRAM_H_
|
# define SCAN_GRAM_H_
|
||||||
|
|
||||||
/* From the scanner. */
|
/* Initialize the scanner to read file GRAM. */
|
||||||
extern FILE *gram_in;
|
void gram_scanner_open (const char *gram);
|
||||||
extern int gram__flex_debug;
|
/* Close the open files. */
|
||||||
void gram_scanner_initialize (void);
|
void gram_scanner_close (void);
|
||||||
|
|
||||||
|
/* Free all the memory allocated to the scanner. */
|
||||||
void gram_scanner_free (void);
|
void gram_scanner_free (void);
|
||||||
void gram_scanner_last_string_free (void);
|
void gram_scanner_last_string_free (void);
|
||||||
|
|
||||||
/* These are declared by the scanner, but not used. We put them here
|
|
||||||
to pacify "make syntax-check". */
|
|
||||||
extern FILE *gram_out;
|
|
||||||
extern int gram_lineno;
|
|
||||||
|
|
||||||
# define GRAM_LEX_DECL int gram_lex (GRAM_STYPE *val, location *loc)
|
# define GRAM_LEX_DECL int gram_lex (GRAM_STYPE *val, location *loc)
|
||||||
GRAM_LEX_DECL;
|
GRAM_LEX_DECL;
|
||||||
|
|
||||||
|
|||||||
@@ -83,6 +83,9 @@ static boundary scanner_cursor;
|
|||||||
unput (Msg[i - 1]); \
|
unput (Msg[i - 1]); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
/* The current file name. Might change with #line. */
|
||||||
|
static uniqstr current_file = NULL;
|
||||||
|
|
||||||
/* A string representing the most recently saved token. */
|
/* A string representing the most recently saved token. */
|
||||||
static char *last_string = NULL;
|
static char *last_string = NULL;
|
||||||
|
|
||||||
@@ -971,25 +974,29 @@ unexpected_newline (boundary start, char const *token_end)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*-------------------------.
|
|
||||||
| Initialize the scanner. |
|
|
||||||
`-------------------------*/
|
|
||||||
|
|
||||||
void
|
void
|
||||||
gram_scanner_initialize (void)
|
gram_scanner_open (const char *gram)
|
||||||
{
|
{
|
||||||
|
gram__flex_debug = trace_flag & trace_scan;
|
||||||
|
gram_debug = trace_flag & trace_parse;
|
||||||
obstack_init (&obstack_for_string);
|
obstack_init (&obstack_for_string);
|
||||||
|
current_file = gram;
|
||||||
|
gram_in = xfopen (gram, "r");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
gram_scanner_close ()
|
||||||
|
{
|
||||||
|
xfclose (gram_in);
|
||||||
|
/* Reclaim Flex's buffers. */
|
||||||
|
yylex_destroy ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*-----------------------------------------------.
|
|
||||||
| Free all the memory allocated to the scanner. |
|
|
||||||
`-----------------------------------------------*/
|
|
||||||
|
|
||||||
void
|
void
|
||||||
gram_scanner_free (void)
|
gram_scanner_free (void)
|
||||||
{
|
{
|
||||||
obstack_free (&obstack_for_string, 0);
|
obstack_free (&obstack_for_string, 0);
|
||||||
/* Reclaim Flex's buffers. */
|
|
||||||
yylex_destroy ();
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user