From 3ae81aa338fb08be451f7ed106adf94e35f52e15 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sat, 10 Nov 2018 15:48:39 +0100 Subject: [PATCH] dogfooding: use api.value.type union * src/parse-gram.y (api.value.type): Set to union. Replace occurrences of %union with explicit %types. * src/scan-gram.l: Adjust yylval's field names. (RETURN_VALUE): No longer needs the Field argument. Use it more. --- src/parse-gram.y | 52 +++++++++++++++++++----------------------------- src/scan-gram.l | 52 +++++++++++++++++++----------------------------- 2 files changed, 42 insertions(+), 62 deletions(-) diff --git a/src/parse-gram.y b/src/parse-gram.y index b40de037..dd3102f5 100644 --- a/src/parse-gram.y +++ b/src/parse-gram.y @@ -97,6 +97,7 @@ %define api.prefix {gram_} %define api.pure full +%define api.value.type union %define locations %define parse.error verbose %define parse.lac full @@ -180,16 +181,13 @@ %token TAG_ANY "<*>" %token TAG_NONE "<>" -%union {unsigned char character;} -%type CHAR -%printer { fputs (char_name ($$), yyo); } CHAR +%type CHAR +%printer { fputs (char_name ($$), yyo); } -%union {char *code;}; -%type "{...}" "%?{...}" "%{...%}" EPILOGUE STRING +%type "{...}" "%?{...}" "%{...%}" EPILOGUE STRING %printer { fputs (quotearg_style (c_quoting_style, $$), yyo); } STRING -%printer { fprintf (yyo, "{\n%s\n}", $$); } +%printer { fprintf (yyo, "{\n%s\n}", $$); } -%union {uniqstr uniqstr;} %type BRACKETED_ID ID ID_COLON PERCENT_FLAG TAG tag variable %printer { fputs ($$, yyo); } %printer { fprintf (yyo, "[%s]", $$); } BRACKETED_ID @@ -197,23 +195,18 @@ %printer { fprintf (yyo, "%%%s", $$); } PERCENT_FLAG %printer { fprintf (yyo, "<%s>", $$); } TAG tag -%union {int integer;}; -%token INT "integer" -%printer { fprintf (yyo, "%d", $$); } +%token INT "integer" +%printer { fprintf (yyo, "%d", $$); } -%union {symbol *symbol;} -%type id id_colon string_as_id symbol symbol.prec -%printer { fprintf (yyo, "%s", $$->tag); } +%type id id_colon string_as_id symbol symbol.prec +%printer { fprintf (yyo, "%s", $$->tag); } %printer { fprintf (yyo, "%s:", $$->tag); } id_colon -%union {assoc assoc;}; %type precedence_declarator -%union {symbol_list *list;} -%type symbols.1 symbols.prec generic_symlist generic_symlist_item +%type symbols.1 symbols.prec generic_symlist generic_symlist_item -%union {named_ref *named_ref;} -%type named_ref.opt +%type named_ref.opt /*---------. | %param. | @@ -239,8 +232,7 @@ static void add_param (param_type type, char *decl, location loc); static param_type current_param = param_none; }; -%union {param_type param;} -%token PERCENT_PARAM "%param"; +%token PERCENT_PARAM "%param"; %printer { switch ($$) @@ -253,7 +245,7 @@ #undef CASE case param_none: aver (false); break; } -} ; +} ; /*==========\ @@ -405,9 +397,8 @@ grammar_declaration: } ; -%type code_props_type; -%union {code_props_type code_type;}; -%printer { fprintf (yyo, "%s", code_props_type_string ($$)); } ; +%type code_props_type; +%printer { fprintf (yyo, "%s", code_props_type_string ($$)); } ; code_props_type: "%destructor" { $$ = destructor; } | "%printer" { $$ = printer; } @@ -642,16 +633,15 @@ variable: ; /* Some content or empty by default. */ -%code requires {#include "muscle-tab.h"}; -%union -{ - struct +%code requires { + #include "muscle-tab.h" + typedef struct { char const *chars; muscle_kind kind; - } value; + } value_type; }; -%type value; +%type value; %printer { switch ($$.kind) @@ -660,7 +650,7 @@ variable: case muscle_keyword: fprintf (yyo, "%s", $$.chars); break; case muscle_string: fprintf (yyo, "\"%s\"", $$.chars); break; } -} ; +} ; value: %empty { $$.kind = muscle_keyword; $$.chars = ""; } diff --git a/src/scan-gram.l b/src/scan-gram.l index f508edca..da6398be 100644 --- a/src/scan-gram.l +++ b/src/scan-gram.l @@ -53,15 +53,15 @@ static boundary scanner_cursor; static size_t no_cr_read (FILE *, char *, size_t); #define YY_INPUT(buf, result, size) ((result) = no_cr_read (yyin, buf, size)) -#define RETURN_PERCENT_PARAM(Value) \ - RETURN_VALUE(PERCENT_PARAM, param, param_ ## Value) +#define RETURN_PERCENT_PARAM(Value) \ + RETURN_VALUE(PERCENT_PARAM, param_ ## Value) -#define RETURN_PERCENT_FLAG(Value) \ - RETURN_VALUE(PERCENT_FLAG, uniqstr, uniqstr_new (Value)) +#define RETURN_PERCENT_FLAG(Value) \ + RETURN_VALUE(PERCENT_FLAG, uniqstr_new (Value)) -#define RETURN_VALUE(Token, Field, Value) \ +#define RETURN_VALUE(Token, Value) \ do { \ - val->Field = Value; \ + val->Token = Value; \ return Token; \ } while (0) @@ -134,6 +134,7 @@ letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_] notletter [^.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]{-}[%\{] id {letter}({letter}|[-0-9])* int [0-9]+ +xint 0[xX][0-9abcdefABCDEF]+ /* Zero or more instances of backslash-newline. Following GCC, allow white space between the backslash and the newline. */ @@ -281,20 +282,14 @@ eqopt ([[:space:]]*=)? ";" return SEMICOLON; {id} { - val->uniqstr = uniqstr_new (yytext); + val->ID = uniqstr_new (yytext); id_loc = *loc; bracketed_id_str = NULL; BEGIN SC_AFTER_IDENTIFIER; } - {int} { - val->integer = scan_integer (yytext, 10, *loc); - return INT; - } - 0[xX][0-9abcdefABCDEF]+ { - val->integer = scan_integer (yytext, 16, *loc); - return INT; - } + {int} RETURN_VALUE (INT, scan_integer (yytext, 10, *loc)); + {xint} RETURN_VALUE (INT, scan_integer (yytext, 16, *loc)); /* Identifiers may not start with a digit. Yet, don't silently accept "1FOO" as "1 FOO". */ @@ -437,7 +432,7 @@ eqopt ([[:space:]]*=)? { if (INITIAL == bracketed_id_context_state) { - val->uniqstr = bracketed_id_str; + val->BRACKETED_ID = bracketed_id_str; bracketed_id_str = 0; *loc = bracketed_id_loc; return BRACKETED_ID; @@ -464,7 +459,7 @@ eqopt ([[:space:]]*=)? { . { ROLLBACK_CURRENT_TOKEN; - val->uniqstr = bracketed_id_str; + val->BRACKETED_ID = bracketed_id_str; bracketed_id_str = 0; *loc = bracketed_id_loc; BEGIN INITIAL; @@ -517,10 +512,9 @@ eqopt ([[:space:]]*=)? { "\"" { STRING_FINISH; - loc->start = token_start; - val->code = last_string; BEGIN INITIAL; - return STRING; + loc->start = token_start; + RETURN_VALUE (STRING, last_string); } <> unexpected_eof (token_start, "\""); "\n" unexpected_newline (token_start, "\""); @@ -536,14 +530,14 @@ eqopt ([[:space:]]*=)? "'" { STRING_FINISH; loc->start = token_start; - val->character = last_string[0]; + val->CHAR = last_string[0]; /* FIXME: Eventually, make these errors. */ if (last_string[0] == '\0') { complain (loc, Wother, _("empty character literal")); /* '\0' seems dangerous even if we are about to complain. */ - val->character = '\''; + val->CHAR = '\''; } else if (last_string[1] != '\0') complain (loc, Wother, @@ -570,7 +564,7 @@ eqopt ([[:space:]]*=)? { STRING_FINISH; loc->start = token_start; - val->uniqstr = uniqstr_new (last_string); + val->TAG = uniqstr_new (last_string); STRING_FREE; BEGIN INITIAL; return TAG; @@ -725,9 +719,8 @@ eqopt ([[:space:]]*=)? { STRING_FINISH; loc->start = code_start; - val->code = last_string; BEGIN INITIAL; - return BRACED_CODE; + RETURN_VALUE (BRACED_CODE, last_string); } } } @@ -740,9 +733,8 @@ eqopt ([[:space:]]*=)? { STRING_FINISH; loc->start = code_start; - val->code = last_string; BEGIN INITIAL; - return BRACED_PREDICATE; + RETURN_VALUE (BRACED_PREDICATE, last_string); } else obstack_1grow (&obstack_for_string, '}'); @@ -758,9 +750,8 @@ eqopt ([[:space:]]*=)? "%}" { STRING_FINISH; loc->start = code_start; - val->code = last_string; BEGIN INITIAL; - return PROLOGUE; + RETURN_VALUE (PROLOGUE, last_string); } <> unexpected_eof (code_start, "%}"); @@ -777,9 +768,8 @@ eqopt ([[:space:]]*=)? <> { STRING_FINISH; loc->start = code_start; - val->code = last_string; BEGIN INITIAL; - return EPILOGUE; + RETURN_VALUE (EPILOGUE, last_string); } }