Miscellaneous code readability improvements.

* src/reader.c (reader): Move %define front-end variable
defaults and checking into...
(prepare_percent_define_front_end_variables): ... this new
function.

* src/scan-gram.l (INITIAL): For consistency with string
literals, don't store open quote on character literal.  It's
discarded before returning anyway.
(SC_ESCAPED_CHARACTER): Similarly, don't store close quote.
Make length test more readable, and make the character stored
for an empty literal more obvious while consistent with the
previous behavior.

* src/symtab.c, src/symtab.h: Rename USER_NUMBER_ALIAS to
USER_NUMBER_HAS_STRING_ALIAS throughout.
* src/symtab.c (symbol_make_alias): Remove comment from symtab.c
that is repeated in symtab.h.  Improve argument names to make it
clear which side of the symbol-string alias pair is which.
(symbol_check_alias_consistency): Improve local variable names
for the same purpose.
* src/symtab.h (struct symbol): Make comments about aliases
clearer.
(symbol_make_alias): Improve comments and argument name.
* src/output.c (token_definitions_output): Update for rename to
USER_NUMBER_HAS_STRING_ALIAS and improve comments about aliases.
(cherry picked from commit dfaa48602d)

Conflicts:

	src/symtab.c
	src/symtab.h
This commit is contained in:
Joel E. Denny
2009-08-10 21:43:07 -04:00
parent 1a323c2f71
commit 07c0db18b4
6 changed files with 113 additions and 76 deletions

View File

@@ -1,3 +1,33 @@
2009-08-10 Joel E. Denny <jdenny@clemson.edu>
Miscellaneous code readability improvements.
* src/reader.c (reader): Move %define front-end variable
defaults and checking into...
(prepare_percent_define_front_end_variables): ... this new
function.
* src/scan-gram.l (INITIAL): For consistency with string
literals, don't store open quote on character literal. It's
discarded before returning anyway.
(SC_ESCAPED_CHARACTER): Similarly, don't store close quote.
Make length test more readable, and make the character stored
for an empty literal more obvious while consistent with the
previous behavior.
* src/symtab.c, src/symtab.h: Rename USER_NUMBER_ALIAS to
USER_NUMBER_HAS_STRING_ALIAS throughout.
* src/symtab.c (symbol_make_alias): Remove comment from symtab.c
that is repeated in symtab.h. Improve argument names to make it
clear which side of the symbol-string alias pair is which.
(symbol_check_alias_consistency): Improve local variable names
for the same purpose.
* src/symtab.h (struct symbol): Make comments about aliases
clearer.
(symbol_make_alias): Improve comments and argument name.
* src/output.c (token_definitions_output): Update for rename to
USER_NUMBER_HAS_STRING_ALIAS and improve comments about aliases.
2009-08-08 Alex Rozenman <rozenman@gmail.com> 2009-08-08 Alex Rozenman <rozenman@gmail.com>
Convert "misleading reference" messages to warnings. Convert "misleading reference" messages to warnings.

View File

@@ -343,10 +343,10 @@ token_definitions_output (FILE *out)
symbol *sym = symbols[i]; symbol *sym = symbols[i];
int number = sym->user_token_number; int number = sym->user_token_number;
/* At this stage, if there are literal aliases, they are part of /* At this stage, if there are literal string aliases, they are
SYMBOLS, so we should not find symbols which are the aliases part of SYMBOLS, so we should not find their aliased symbols
here. */ here. */
aver (number != USER_NUMBER_ALIAS); aver (number != USER_NUMBER_HAS_STRING_ALIAS);
/* Skip error token. */ /* Skip error token. */
if (sym == errtoken) if (sym == errtoken)

View File

@@ -36,6 +36,7 @@
#include "scan-gram.h" #include "scan-gram.h"
#include "scan-code.h" #include "scan-code.h"
static void prepare_percent_define_front_end_variables (void);
static void check_and_convert_grammar (void); static void check_and_convert_grammar (void);
static symbol_list *grammar = NULL; static symbol_list *grammar = NULL;
@@ -590,8 +591,18 @@ reader (void)
gram_debug = trace_flag & trace_parse; gram_debug = trace_flag & trace_parse;
gram_scanner_initialize (); gram_scanner_initialize ();
gram_parse (); gram_parse ();
prepare_percent_define_front_end_variables ();
/* Set front-end %define variable defaults. */ if (! complaint_issued)
check_and_convert_grammar ();
xfclose (gram_in);
}
static void
prepare_percent_define_front_end_variables (void)
{
/* Set %define front-end variable defaults. */
muscle_percent_define_default ("lr.keep-unreachable-states", "false"); muscle_percent_define_default ("lr.keep-unreachable-states", "false");
{ {
char *lr_type; char *lr_type;
@@ -606,7 +617,7 @@ reader (void)
free (lr_type); free (lr_type);
} }
/* Check front-end %define variables. */ /* Check %define front-end variables. */
{ {
static char const * const values[] = { static char const * const values[] = {
"lr.type", "LALR", "IELR", "canonical LR", NULL, "lr.type", "LALR", "IELR", "canonical LR", NULL,
@@ -615,11 +626,6 @@ reader (void)
}; };
muscle_percent_define_check_values (values); muscle_percent_define_check_values (values);
} }
if (! complaint_issued)
check_and_convert_grammar ();
xfclose (gram_in);
} }

View File

@@ -251,7 +251,7 @@ splice (\\[ \f\t\v]*\n)*
} }
/* Characters. */ /* Characters. */
"'" STRING_GROW; token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER; "'" token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
/* Strings. */ /* Strings. */
"\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING; "\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING;
@@ -465,16 +465,18 @@ splice (\\[ \f\t\v]*\n)*
<SC_ESCAPED_CHARACTER> <SC_ESCAPED_CHARACTER>
{ {
"'"|"\n" { "'"|"\n" {
STRING_GROW;
STRING_FINISH; STRING_FINISH;
loc->start = token_start; loc->start = token_start;
val->character = last_string[1]; val->character = last_string[0];
{ {
/* FIXME: Eventually, make these errors. */ /* FIXME: Eventually, make these errors. */
size_t length = strlen (last_string); if (last_string[0] == '\0')
if (length < 3) {
warn_at (*loc, _("empty character literal")); warn_at (*loc, _("empty character literal"));
else if (length > 3) /* '\0' seems dangerous even if we are about to complain. */
val->character = '\'';
}
else if (last_string[1] != '\0')
warn_at (*loc, _("extra characters in character literal")); warn_at (*loc, _("extra characters in character literal"));
} }
if (yytext[0] == '\n') if (yytext[0] == '\n')
@@ -486,17 +488,17 @@ splice (\\[ \f\t\v]*\n)*
<<EOF>> { <<EOF>> {
STRING_FINISH; STRING_FINISH;
loc->start = token_start; loc->start = token_start;
val->character = last_string[0];
{ {
size_t length = strlen (last_string);
/* FIXME: Eventually, make these errors. */ /* FIXME: Eventually, make these errors. */
if (length < 2) if (last_string[0] == '\0')
warn_at (*loc, _("empty character literal")); {
else if (length > 2) warn_at (*loc, _("empty character literal"));
/* '\0' seems dangerous even if we are about to complain. */
val->character = '\'';
}
else if (last_string[1] != '\0')
warn_at (*loc, _("extra characters in character literal")); warn_at (*loc, _("extra characters in character literal"));
if (length > 1)
val->character = last_string[1];
else
val->character = last_string[0];
} }
unexpected_eof (token_start, "'"); unexpected_eof (token_start, "'");
STRING_FREE; STRING_FREE;

View File

@@ -367,7 +367,7 @@ symbol_user_token_number_set (symbol *sym, int user_token_number, location loc)
{ {
int *user_token_numberp; int *user_token_numberp;
if (sym->user_token_number != USER_NUMBER_ALIAS) if (sym->user_token_number != USER_NUMBER_HAS_STRING_ALIAS)
user_token_numberp = &sym->user_token_number; user_token_numberp = &sym->user_token_number;
else else
user_token_numberp = &sym->alias->user_token_number; user_token_numberp = &sym->alias->user_token_number;
@@ -416,29 +416,24 @@ symbol_check_defined_processor (void *sym, void *null ATTRIBUTE_UNUSED)
} }
/*------------------------------------------------------------------.
| Declare the new symbol SYM. Make it an alias of SYMVAL, and type |
| SYMVAL with SYM's type. |
`------------------------------------------------------------------*/
void void
symbol_make_alias (symbol *sym, symbol *symval, location loc) symbol_make_alias (symbol *sym, symbol *str, location loc)
{ {
if (symval->alias) if (str->alias)
warn_at (loc, _("symbol `%s' used more than once as a literal string"), warn_at (loc, _("symbol `%s' used more than once as a literal string"),
symval->tag); str->tag);
else if (sym->alias) else if (sym->alias)
warn_at (loc, _("symbol `%s' given more than one literal string"), warn_at (loc, _("symbol `%s' given more than one literal string"),
sym->tag); sym->tag);
else else
{ {
symval->class = token_sym; str->class = token_sym;
symval->user_token_number = sym->user_token_number; str->user_token_number = sym->user_token_number;
sym->user_token_number = USER_NUMBER_ALIAS; sym->user_token_number = USER_NUMBER_HAS_STRING_ALIAS;
symval->alias = sym; str->alias = sym;
sym->alias = symval; sym->alias = str;
symval->number = sym->number; str->number = sym->number;
symbol_type_set (symval, sym->type_name, loc); symbol_type_set (str, sym->type_name, loc);
} }
} }
@@ -451,46 +446,47 @@ symbol_make_alias (symbol *sym, symbol *symval, location loc)
static inline void static inline void
symbol_check_alias_consistency (symbol *this) symbol_check_alias_consistency (symbol *this)
{ {
symbol *alias = this; symbol *sym = this;
symbol *orig = this->alias; symbol *str = this->alias;
/* Check only those that _are_ the aliases. */ /* Check only the symbol in the symbol-string pair. */
if (!(this->alias && this->user_token_number == USER_NUMBER_ALIAS)) if (!(this->alias
&& this->user_token_number == USER_NUMBER_HAS_STRING_ALIAS))
return; return;
if (orig->type_name != alias->type_name) if (str->type_name != sym->type_name)
{ {
if (orig->type_name) if (str->type_name)
symbol_type_set (alias, orig->type_name, orig->type_location); symbol_type_set (sym, str->type_name, str->type_location);
else else
symbol_type_set (orig, alias->type_name, alias->type_location); symbol_type_set (str, sym->type_name, sym->type_location);
} }
if (orig->destructor.code || alias->destructor.code) if (str->destructor.code || sym->destructor.code)
{ {
if (orig->destructor.code) if (str->destructor.code)
symbol_destructor_set (alias, &orig->destructor); symbol_destructor_set (sym, &str->destructor);
else else
symbol_destructor_set (orig, &alias->destructor); symbol_destructor_set (str, &sym->destructor);
} }
if (orig->printer.code || alias->printer.code) if (str->printer.code || sym->printer.code)
{ {
if (orig->printer.code) if (str->printer.code)
symbol_printer_set (alias, &orig->printer); symbol_printer_set (sym, &str->printer);
else else
symbol_printer_set (orig, &alias->printer); symbol_printer_set (str, &sym->printer);
} }
if (alias->prec || orig->prec) if (sym->prec || str->prec)
{ {
if (orig->prec) if (str->prec)
symbol_precedence_set (alias, orig->prec, orig->assoc, symbol_precedence_set (sym, str->prec, str->assoc,
orig->prec_location); str->prec_location);
else else
symbol_precedence_set (orig, alias->prec, alias->assoc, symbol_precedence_set (str, sym->prec, sym->assoc,
alias->prec_location); sym->prec_location);
} }
} }
@@ -530,8 +526,8 @@ symbol_pack (symbol *this)
this->number = this->alias->number; this->number = this->alias->number;
} }
} }
/* Do not do processing below for USER_NUMBER_ALIASes. */ /* Do not do processing below for USER_NUMBER_HAS_STRING_ALIASes. */
if (this->user_token_number == USER_NUMBER_ALIAS) if (this->user_token_number == USER_NUMBER_HAS_STRING_ALIAS)
return true; return true;
} }
else /* this->class == token_sym */ else /* this->class == token_sym */
@@ -578,7 +574,7 @@ symbol_translation (symbol *this)
{ {
/* Non-terminal? */ /* Non-terminal? */
if (this->class == token_sym if (this->class == token_sym
&& this->user_token_number != USER_NUMBER_ALIAS) && this->user_token_number != USER_NUMBER_HAS_STRING_ALIAS)
{ {
/* A token which translation has already been set? */ /* A token which translation has already been set? */
if (token_translations[this->user_token_number] != undeftoken->number) if (token_translations[this->user_token_number] != undeftoken->number)

View File

@@ -85,9 +85,9 @@ struct symbol
assoc assoc; assoc assoc;
int user_token_number; int user_token_number;
/* Points to the other in the identifier-symbol pair for an alias. /* Points to the other in the symbol-string pair for an alias.
Special value USER_NUMBER_ALIAS in the identifier half of the Special value USER_NUMBER_HAS_STRING_ALIAS in the symbol half of the
identifier-symbol pair for an alias. */ symbol-string pair for an alias. */
symbol *alias; symbol *alias;
symbol_class class; symbol_class class;
bool declared; bool declared;
@@ -96,11 +96,11 @@ struct symbol
/** Undefined user number. */ /** Undefined user number. */
#define USER_NUMBER_UNDEFINED -1 #define USER_NUMBER_UNDEFINED -1
/* `symbol->user_token_number == USER_NUMBER_ALIAS' means this symbol /* `symbol->user_token_number == USER_NUMBER_HAS_STRING_ALIAS' means
*has* (not is) a string literal alias. For instance, `%token foo this symbol has a literal string alias. For instance, `%token foo
"foo"' has `"foo"' numbered regularly, and `foo' numbered as "foo"' has `"foo"' numbered regularly, and `foo' numbered as
USER_NUMBER_ALIAS. */ USER_NUMBER_HAS_STRING_ALIAS. */
#define USER_NUMBER_ALIAS -9991 #define USER_NUMBER_HAS_STRING_ALIAS -9991
/* Undefined internal token number. */ /* Undefined internal token number. */
#define NUMBER_UNDEFINED (-1) #define NUMBER_UNDEFINED (-1)
@@ -122,8 +122,11 @@ symbol *dummy_symbol_get (location loc);
/** Is this a dummy nonterminal? */ /** Is this a dummy nonterminal? */
bool symbol_is_dummy (const symbol *sym); bool symbol_is_dummy (const symbol *sym);
/** Declare the new symbol \c sym. Make it an alias of \c symval. */ /**
void symbol_make_alias (symbol *sym, symbol *symval, location loc); * Make \c str the literal string alias of \c sym. Copy token number,
* symbol number, and type from \c sym to \c str.
*/
void symbol_make_alias (symbol *sym, symbol *str, location loc);
/** Set the \c type_name associated with \c sym. /** Set the \c type_name associated with \c sym.