d: add api.value.type union support

The union of the values is handled by the backend.
In D, unions can hold classes, structs, etc., so this is more similar
to the C++ api.value.type variant.

* data/skeletons/d.m4, data/skeletons/lalr1.d: Here.
* tests/calc.at, tests/local.at: Test it.
This commit is contained in:
Akim Demaille
2021-01-21 21:45:45 +01:00
parent a9c2549c27
commit ae0889d805
4 changed files with 124 additions and 11 deletions

View File

@@ -275,7 +275,7 @@ m4_define([b4_declare_symbol_enum],
]])
# b4-case(ID, CODE, [COMMENTS])
# b4_case(ID, CODE, [COMMENTS])
# -----------------------------
m4_define([b4_case], [ case $1:m4_ifval([$3], [ b4_comment([$3])])
$2
@@ -302,6 +302,109 @@ m4_define([b4_location_type], b4_percent_define_ifdef([[location_type]],[b4_perc
m4_define([b4_position_type], b4_percent_define_ifdef([[position_type]],[b4_percent_define_get([[position_type]])],[YYPosition]))
## ---------------- ##
## api.value.type. ##
## ---------------- ##
# ---------------------- #
# api.value.type=union. #
# ---------------------- #
# b4_symbol_type_register(SYMBOL-NUM)
# -----------------------------------
# Symbol SYMBOL-NUM has a type (for union) instead of a type-tag.
# Extend the definition of %union's body (b4_union_members) with a
# field of that type, and extend the symbol's "type" field to point to
# the field name, instead of the type name.
m4_define([b4_symbol_type_register],
[m4_define([b4_symbol($1, type_tag)],
[b4_symbol_if([$1], [has_id],
[b4_symbol([$1], [id])],
[yykind_[]b4_symbol([$1], [number])])])dnl
m4_append([b4_union_members],
m4_expand([m4_format([ %-40s %s],
m4_expand([b4_symbol([$1], [type]) b4_symbol([$1], [type_tag]);]),
[b4_symbol_tag_comment([$1])])]))
])
# b4_type_define_tag(SYMBOL1-NUM, ...)
# ------------------------------------
# For the batch of symbols SYMBOL1-NUM... (which all have the same
# type), enhance the %union definition for each of them, and set
# there "type" field to the field tag name, instead of the type name.
m4_define([b4_type_define_tag],
[b4_symbol_if([$1], [has_type],
[m4_map([b4_symbol_type_register], [$@])])
])
# b4_symbol_value_union(VAL, SYMBOL-NUM, [TYPE])
# ----------------------------------------------
# Same of b4_symbol_value, but when api.value.type=union.
m4_define([b4_symbol_value_union],
[m4_ifval([$3],
[(*($3*)(&$1))],
[m4_ifval([$2],
[b4_symbol_if([$2], [has_type],
[($1.b4_symbol([$2], [type_tag]))],
[$1])],
[$1])])])
# b4_value_type_setup_union
# -------------------------
# Setup support for api.value.type=union. Symbols are defined with a
# type instead of a union member name: build the corresponding union,
# and give the symbols their tag.
m4_define([b4_value_type_setup_union],
[m4_define([b4_union_members])
b4_type_foreach([b4_type_define_tag])
m4_copy_force([b4_symbol_value_union], [b4_symbol_value])
])
# _b4_value_type_setup_keyword
# ----------------------------
# api.value.type is defined with a keyword/string syntax. Check if
# that is properly defined, and prepare its use.
m4_define([_b4_value_type_setup_keyword],
[b4_percent_define_check_values([[[[api.value.type]],
[[none]],
[[union]],
[[union-directive]],
[[yystype]]]])dnl
m4_case(b4_percent_define_get([[api.value.type]]),
[union], [b4_value_type_setup_union])])
# b4_value_type_setup
# -------------------
# Check if api.value.type is properly defined, and possibly prepare
# its use.
b4_define_silent([b4_value_type_setup],
[
# Define default value.
b4_percent_define_ifdef([[api.value.type]], [],
[# %union => api.value.type=union-directive
m4_ifdef([b4_union_members],
[m4_define([b4_percent_define_kind(api.value.type)], [keyword])
m4_define([b4_percent_define(api.value.type)], [union-directive])],
[# no tag seen => api.value.type={int}
m4_if(b4_tag_seen_flag, 0,
[m4_define([b4_percent_define_kind(api.value.type)], [code])
m4_define([b4_percent_define(api.value.type)], [int])],
[# otherwise api.value.type=yystype
m4_define([b4_percent_define_kind(api.value.type)], [keyword])
m4_define([b4_percent_define(api.value.type)], [yystype])])])])
# Set up.
m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
[keyword], [_b4_value_type_setup_keyword])
])
## ----------------- ##
## Semantic Values. ##
## ----------------- ##
@@ -454,11 +557,18 @@ alias Position = ]b4_position_type[;]])[
m4_define([b4_basic_symbol_constructor_define],
[b4_token_visible_if([$1],
[ this(TokenKind token]b4_symbol_if([$1], [has_type],
[[, typeof(YYSemanticType.]b4_symbol([$1], [type])dnl
[) val]])[]b4_locations_if([[, Location loc]])[)
[[, ]b4_union_if([], [[typeof(YYSemanticType.]])b4_symbol([$1], [type])dnl
[]b4_union_if([], [[) ]])[ val]])[]b4_locations_if([[, Location loc]])[)
{
kind = yytranslate_(token);]b4_symbol_if([$1], [has_type], [[
value_.]b4_symbol([$1], [type])[ = val;]])[]b4_locations_if([
kind = yytranslate_(token);]b4_union_if([b4_symbol_if([$1], [has_type], [[
static foreach (member; __traits(allMembers, YYSemanticType))
{
static if (is(typeof(mixin("value_." ~ member)) == ]b4_symbol([$1], [type])[))
{
mixin("value_." ~ member ~ " = val;");
}
}]])], [b4_symbol_if([$1], [has_type], [[
value_.]b4_symbol([$1], [type])[ = val;]])])[]b4_locations_if([
location_ = loc;])[
}
])])

View File

@@ -206,7 +206,7 @@ public struct ]b4_location_type[
private immutable bool yy_location_is_class = false;
]])])m4_ifdef([b4_user_union_members], [private union YYSemanticType
]])])[]b4_value_type_setup[]m4_ifdef([b4_user_union_members], [private union YYSemanticType
{
b4_user_union_members
};],