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:
Akim Demaille
2019-10-22 09:03:00 +02:00
parent a5fc4e3b44
commit 8228d96d33
8 changed files with 30 additions and 34 deletions

View File

@@ -68,7 +68,6 @@ static generated_file *generated_files = NULL;
static int generated_files_size = 0;
uniqstr grammar_file = NULL;
uniqstr current_file = NULL;
/* If --output=dir/foo.c was specified,
DIR_PREFIX is 'dir/' and ALL_BUT_EXT and ALL_BUT_TAB_EXT are 'dir/foo'.

View File

@@ -58,9 +58,6 @@ extern char *dir_prefix;
and therefore GCC warns about a name clash. */
extern uniqstr grammar_file;
/* The current file name. Might change with #line. */
extern uniqstr current_file;
/* The computed base for output file names. */
extern char *all_but_ext;

View File

@@ -853,7 +853,7 @@ getargs (int argc, char *argv[])
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);
}

View File

@@ -101,7 +101,7 @@ main (int argc, char *argv[])
the grammar; see gram.h. */
timevar_push (tv_reader);
reader ();
reader (grammar_file);
timevar_pop (tv_reader);
if (complaint_status == status_complaint)

View File

@@ -710,24 +710,20 @@ packgram (void)
`------------------------------------------------------------------*/
void
reader (void)
reader (const char *gram)
{
/* Set up symbol_table, semantic_type_table, and the built-in
symbols. */
symbols_new ();
gram_in = xfopen (grammar_file, "r");
gram__flex_debug = trace_flag & trace_scan;
gram_debug = trace_flag & trace_parse;
gram_scanner_initialize ();
gram_scanner_open (gram);
gram_parse ();
gram_scanner_close ();
prepare_percent_define_front_end_variables ();
if (complaint_status < status_complaint)
check_and_convert_grammar ();
xfclose (gram_in);
}
static void

View File

@@ -60,7 +60,7 @@ void grammar_current_rule_action_append (const char *action, location loc,
named_ref *nref, uniqstr tag);
/* Attach a PREDICATE to the current rule. */
void grammar_current_rule_predicate_append (const char *predicate, location loc);
void reader (void);
void reader (const char *gram);
void free_merger_functions (void);
extern merger_list *merge_functions;

View File

@@ -21,18 +21,15 @@
#ifndef SCAN_GRAM_H_
# define SCAN_GRAM_H_
/* From the scanner. */
extern FILE *gram_in;
extern int gram__flex_debug;
void gram_scanner_initialize (void);
/* Initialize the scanner to read file GRAM. */
void gram_scanner_open (const char *gram);
/* Close the open files. */
void gram_scanner_close (void);
/* Free all the memory allocated to the scanner. */
void gram_scanner_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)
GRAM_LEX_DECL;

View File

@@ -83,6 +83,9 @@ static boundary scanner_cursor;
unput (Msg[i - 1]); \
} while (0)
/* The current file name. Might change with #line. */
static uniqstr current_file = NULL;
/* A string representing the most recently saved token. */
static char *last_string = NULL;
@@ -971,25 +974,29 @@ unexpected_newline (boundary start, char const *token_end)
}
/*-------------------------.
| Initialize the scanner. |
`-------------------------*/
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);
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
gram_scanner_free (void)
{
obstack_free (&obstack_for_string, 0);
/* Reclaim Flex's buffers. */
yylex_destroy ();
}