tokens: define the "$undefined" token kind

* data/skeletons/bison.m4 (b4_symbol_token_kind): Give a definition to
$undefined.
(b4_token_visible_if): $undefined has an id.
* src/output.c (prepare_symbol_definitions): Stop lying: $undefined
_is_ a token.
* tests/input.at: Adjust.
This commit is contained in:
Akim Demaille
2020-04-10 09:26:31 +02:00
parent a4ed94bc13
commit 95421df67b
6 changed files with 44 additions and 16 deletions

View File

@@ -456,10 +456,6 @@ m4_define([b4_symbol_if],
[m4_fatal([$0: field $2 of $1 is not a Boolean:] b4_symbol([$1], [$2]))])]) [m4_fatal([$0: field $2 of $1 is not a Boolean:] b4_symbol([$1], [$2]))])])
# Give the error token a better name.
m4_define([b4_symbol(1, id)], [b4_api_PREFIX[][ERRCODE]])
# b4_symbol_tag_comment(SYMBOL-NUM) # b4_symbol_tag_comment(SYMBOL-NUM)
# --------------------------------- # ---------------------------------
# Issue a comment giving the tag of symbol NUM. # Issue a comment giving the tag of symbol NUM.
@@ -541,9 +537,11 @@ m4_define([b4_symbol_map],
# Whether NUM denotes a token that has an exported definition (i.e., # Whether NUM denotes a token that has an exported definition (i.e.,
# shows in enum yytokentype). # shows in enum yytokentype).
m4_define([b4_token_visible_if], m4_define([b4_token_visible_if],
[b4_symbol_if([$1], [is_token], [m4_case(b4_symbol([$1], [tag]),
[b4_symbol_if([$1], [has_id], [$2], [$3])], [$undefined], [$2],
[$3])]) [b4_symbol_if([$1], [is_token],
[b4_symbol_if([$1], [has_id], [$2], [$3])],
[$3])])])
# b4_token_has_definition(NUM) # b4_token_has_definition(NUM)

View File

@@ -430,6 +430,12 @@ static const b4_int_type_for([$2]) yy$1[[]] =
## Token kinds. ## ## Token kinds. ##
## ------------- ## ## ------------- ##
# Because C enums are not scoped, because tokens are exposed in the
# header, and because these tokens are common to all the parser, we
# need to make sure their names don't collide: use the api.prefix.
m4_define([b4_symbol(1, id)], [b4_api_PREFIX[][ERRCODE]])
m4_define([b4_symbol(2, id)], [b4_api_PREFIX[][UNDEF]])
# b4_token_define(TOKEN-NUM) # b4_token_define(TOKEN-NUM)
# -------------------------- # --------------------------
# Output the definition of this token as #define. # Output the definition of this token as #define.

View File

@@ -549,8 +549,7 @@ prepare_symbol_definitions (void)
MUSCLE_INSERT_INT (key, sym->content->user_token_number); MUSCLE_INSERT_INT (key, sym->content->user_token_number);
SET_KEY ("is_token"); SET_KEY ("is_token");
MUSCLE_INSERT_INT (key, MUSCLE_INSERT_INT (key, i < ntokens);
i < ntokens && sym != undeftoken);
SET_KEY ("number"); SET_KEY ("number");
MUSCLE_INSERT_INT (key, sym->content->number); MUSCLE_INSERT_INT (key, sym->content->number);

View File

@@ -79,6 +79,7 @@ extern int gram_debug;
{ {
GRAM_EOF = 0, /* "end of file" */ GRAM_EOF = 0, /* "end of file" */
GRAM_ERRCODE = 1, /* error */ GRAM_ERRCODE = 1, /* error */
GRAM_UNDEF = 2, /* $undefined */
STRING = 3, /* "string" */ STRING = 3, /* "string" */
TSTRING = 4, /* "translatable string" */ TSTRING = 4, /* "translatable string" */
PERCENT_TOKEN = 5, /* "%token" */ PERCENT_TOKEN = 5, /* "%token" */

View File

@@ -65,6 +65,17 @@ static bool *used_assoc = NULL;
bool tag_seen = false; bool tag_seen = false;
/* Whether SYM was defined by the user. */
static bool
symbol_is_user_defined (symbol *sym)
{
return sym->tag[0] != '$'
&& sym != errtoken && sym->alias != errtoken
&& sym != undeftoken && sym->alias != undeftoken;
}
/*--------------------------. /*--------------------------.
| Create a new sym_content. | | Create a new sym_content. |
`--------------------------*/ `--------------------------*/
@@ -279,6 +290,7 @@ is_identifier (uniqstr s)
/*-----------------------------------------------. /*-----------------------------------------------.
| Get the identifier associated to this symbol. | | Get the identifier associated to this symbol. |
`-----------------------------------------------*/ `-----------------------------------------------*/
uniqstr uniqstr
symbol_id_get (symbol const *sym) symbol_id_get (symbol const *sym)
{ {
@@ -419,6 +431,7 @@ symbol_code_props_set (symbol *sym, code_props_type kind,
sym->content->props[kind] = *code; sym->content->props[kind] = *code;
} }
/*-----------------------------------------------------. /*-----------------------------------------------------.
| Set the DESTRUCTOR or PRINTER associated with TYPE. | | Set the DESTRUCTOR or PRINTER associated with TYPE. |
`-----------------------------------------------------*/ `-----------------------------------------------------*/
@@ -457,7 +470,7 @@ symbol_code_props_get (symbol *sym, code_props_type kind)
} }
/* Apply default code props's only to user-defined symbols. */ /* Apply default code props's only to user-defined symbols. */
if (sym->tag[0] != '$' && sym != errtoken) if (symbol_is_user_defined (sym))
{ {
code_props *code = &semantic_type_get (sym->content->type_name ? "*" : "", code_props *code = &semantic_type_get (sym->content->type_name ? "*" : "",
NULL)->props[kind]; NULL)->props[kind];
@@ -831,16 +844,26 @@ symbols_new (void)
accept->content->class = nterm_sym; accept->content->class = nterm_sym;
accept->content->number = nvars++; accept->content->number = nvars++;
/* Construct the error token */ /* Construct the YYERRCODE/"error" token */
errtoken = symbol_get ("error", empty_loc); errtoken = symbol_get ("YYERRCODE", empty_loc);
errtoken->content->class = token_sym; errtoken->content->class = token_sym;
errtoken->content->number = ntokens++; errtoken->content->number = ntokens++;
{
symbol *alias = symbol_get ("error", empty_loc);
symbol_class_set (alias, token_sym, empty_loc, false);
symbol_make_alias (errtoken, alias, empty_loc);
}
/* Construct a token that represents all undefined literal tokens. /* Construct the YYUNDEF/"$undefined" token that represents all
It is always token number 2. */ undefined literal tokens. It is always symbol number 2. */
undeftoken = symbol_get ("$undefined", empty_loc); undeftoken = symbol_get ("YYUNDEF", empty_loc);
undeftoken->content->class = token_sym; undeftoken->content->class = token_sym;
undeftoken->content->number = ntokens++; undeftoken->content->number = ntokens++;
{
symbol *alias = symbol_get ("$undefined", empty_loc);
symbol_class_set (alias, token_sym, empty_loc, false);
symbol_make_alias (undeftoken, alias, empty_loc);
}
semantic_type_table = hash_xinitialize (HT_INITIAL_CAPACITY, semantic_type_table = hash_xinitialize (HT_INITIAL_CAPACITY,
NULL, NULL,

View File

@@ -345,6 +345,7 @@ int main (void)
{ {
assert (YYERRCODE == 123); assert (YYERRCODE == 123);
assert (YYTRANSLATE (YYERRCODE) == YYSYMBOL_YYERROR); assert (YYTRANSLATE (YYERRCODE) == YYSYMBOL_YYERROR);
assert (YYTRANSLATE (YYUNDEF) == YYSYMBOL_YYUNDEF);
return 0; return 0;
} }
]]) ]])
@@ -431,7 +432,7 @@ AT_CHECK([cat symbols.csv], [],
[[number, class, tag, id, user_number, type, [[number, class, tag, id, user_number, type,
0, Token, $end, , 0, , 0, Token, $end, , 0, ,
1, Token, error, YYERRCODE, 256, , 1, Token, error, YYERRCODE, 256, ,
2, Nonterminal, $undefined, , 257, , 2, Token, $undefined, YYUNDEF, 257, ,
3, Token, 'a', , 97, , 3, Token, 'a', , 97, ,
4, Token, "A1", A1, 1, , 4, Token, "A1", A1, 1, ,
5, Token, A2, A2, 258, , 5, Token, A2, A2, 258, ,