Le 22 juin 2012 à 14:37, Akim Demaille a écrit : > From 71da68b3662b7169c58f8c5d4c0e89bc3caa31c4 Mon Sep 17 00:00:00 2001 > From: Victor Santet <[email protected]> > Date: Fri, 22 Jun 2012 14:11:05 +0200 > Subject: [PATCH] maint: factor the handling of %printer and %destructor > > There is too much code duplication between %printer and %destructor. > We used to have two functions for each action: the first one for > destructors, the second one for printers. Factor using a > 'code_props_type', and an array of code_props instead of two > members. > > * src/symlist.h, src/symlist.c (symbol_list_destructor_set) > (symbol_list_printer_set): Fuse into... > (symbol_list_code_props_set): this. > * src/symtab.h, src/symtab.c (default_tagged_destructor) > (default_tagged_printer): Fuse into... > (default_tagged_code_props): this. > (default_tagless_destructor, default_tagless_printer) > (default_tagless_code_props): Likewise. > (code_props_type_string): new. > (symbol_destructor_set, symbol_destructor_get, semantic_type_destructor_set) > (default_tagged_destructor_set, default_tagless_destructor_set) > (symbol_printer_set, symbol_printer_get, semantic_type_printer_set) > (default_tagged_printer_set, default_tagless_printer_set): Replace by... > (symbol_code_props_set, symbol_code_props_get, semantic_type_code_props_set) > (default_tagged_code_props_set, default_tagless_code_props_set): these. > * src/parse-gram.y (grammar_declaration): Adjust. > * src/output.c (CODE_PROP, grammar_declaration): Ditto. > * src/reader.c (symbol_should_be_used): Ditto.
I have installed this, on top of your patch. From 6a0655d9cddec57a13a7fd813aa1dc81bdbcb38f Mon Sep 17 00:00:00 2001 From: Akim Demaille <[email protected]> Date: Fri, 22 Jun 2012 14:47:31 +0200 Subject: [PATCH] code_props: factor more. * src/symtab.h, src/symtab.c (code_props_type_string): No longer static. * src/output.c (CODE_PROPS): Remove, we can now iterate on both the destructor and the printer. (SET_KEY2): New. --- src/output.c | 58 +++++++++++++++++++++++++++++++++------------------------- src/symtab.c | 6 +----- src/symtab.h | 5 ++++- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/output.c b/src/output.c index c967fa7..e8cff20 100644 --- a/src/output.c +++ b/src/output.c @@ -402,9 +402,16 @@ prepare_symbol_definitions (void) const char *key; const char *value; -#define SET_KEY(Entry) \ - obstack_fgrow2 (&format_obstack, "symbol(%d, %s)", i, Entry); \ - obstack_1grow (&format_obstack, 0); \ +#define SET_KEY(Entry) \ + obstack_fgrow2 (&format_obstack, "symbol(%d, %s)", \ + i, Entry); \ + obstack_1grow (&format_obstack, 0); \ + key = obstack_finish (&format_obstack); + +#define SET_KEY2(Entry, Suffix) \ + obstack_fgrow3 (&format_obstack, "symbol(%d, %s_%s)", \ + i, Entry, Suffix); \ + obstack_1grow (&format_obstack, 0); \ key = obstack_finish (&format_obstack); // Whether the symbol has an identifier. @@ -436,28 +443,29 @@ prepare_symbol_definitions (void) SET_KEY("type"); MUSCLE_INSERT_STRING (key, sym->type_name ? sym->type_name : ""); -#define CODE_PROP(PropName) \ - do { \ - code_props const *p = symbol_code_props_get (sym, PropName); \ - SET_KEY("has_" #PropName); \ - MUSCLE_INSERT_INT (key, !!p->code); \ - \ - if (p->code) \ - { \ - SET_KEY(#PropName "_file"); \ - MUSCLE_INSERT_STRING (key, p->location.start.file); \ - \ - SET_KEY(#PropName "_line"); \ - MUSCLE_INSERT_INT (key, p->location.start.line); \ - \ - SET_KEY(#PropName); \ - MUSCLE_INSERT_STRING_RAW (key, p->code); \ - } \ - } while (0) - - CODE_PROP(destructor); - CODE_PROP(printer); -#undef CODE_PROP + { + int j; + for (j = 0; j < CODE_PROPS_SIZE; ++j) + { + char const *pname = code_props_type_string (j); + code_props const *p = symbol_code_props_get (sym, j); + SET_KEY2("has", pname); + MUSCLE_INSERT_INT (key, !!p->code); + + if (p->code) + { + SET_KEY2(pname, "file"); + MUSCLE_INSERT_STRING (key, p->location.start.file); + + SET_KEY2(pname, "line"); + MUSCLE_INSERT_INT (key, p->location.start.line); + + SET_KEY(pname); + MUSCLE_INSERT_STRING_RAW (key, p->code); + } + } + } +#undef SET_KEY2 #undef SET_KEY } } diff --git a/src/symtab.c b/src/symtab.c index b30ccaf..14d107c 100644 --- a/src/symtab.c +++ b/src/symtab.c @@ -100,11 +100,7 @@ symbol_new (uniqstr tag, location loc) return res; } -/*-------------------------------------------------------. -| Name of the code_props type: %destructor or %printer. | -`-------------------------------------------------------*/ - -static char const * +char const * code_props_type_string (code_props_type kind) { switch (kind) diff --git a/src/symtab.h b/src/symtab.h index 0ef4db2..70b4a81 100644 --- a/src/symtab.h +++ b/src/symtab.h @@ -154,7 +154,10 @@ void symbol_print (symbol *s, FILE *f); /** Is this a dummy nonterminal? */ bool symbol_is_dummy (const symbol *sym); -/** Return the name of the symbol that can be used as an identifier. +/** The name of the code_props type: "\%destructor" or "\%printer". */ +char const *code_props_type_string (code_props_type kind); + +/** The name of the symbol that can be used as an identifier. ** Consider the alias if needed. ** Return 0 if there is none (e.g., the symbol is only defined as ** a string). */ -- 1.7.10.4
