summaryrefslogtreecommitdiff
path: root/data
diff options
context:
space:
mode:
authorAdela Vais <adela.vais99@gmail.com>2021-01-25 22:53:30 +0200
committerAkim Demaille <akim.demaille@gmail.com>2021-03-26 07:01:55 +0100
commit4bd4cdf37749bbd589e684f39da9aeb1d2aabc51 (patch)
tree8df1eab22ac7f53113d3ce1ab777f879c1b7cba2 /data
parentae0889d805b0965568c9f95f5640d54058288246 (diff)
downloadbison-4bd4cdf37749bbd589e684f39da9aeb1d2aabc51.tar.gz
d: add token constructors support
The user can return from yylex() by calling the Symbol method of the same name as the TokenKind reported, and adding the parameters for value and location if necessary. These methods generate compile-time errors if the parameters are not correlated. Token constructors work with both %union and api.value.type union. * data/skeletons/d.m4: Here. * tests/calc.at: Test it.
Diffstat (limited to 'data')
-rw-r--r--data/skeletons/d.m427
1 files changed, 27 insertions, 0 deletions
diff --git a/data/skeletons/d.m4 b/data/skeletons/d.m4
index d2fa6e29..0aae111e 100644
--- a/data/skeletons/d.m4
+++ b/data/skeletons/d.m4
@@ -208,6 +208,32 @@ b4_symbol_foreach([b4_token_enum])dnl
m4_define([b4_symbol_translate],
[[_($1)]])
+
+# _b4_token_constructor_define
+# ----------------------------
+# Define make_symbol for a value type.
+m4_define([_b4_token_constructor_define],
+[b4_token_visible_if([$1],
+[[
+ static auto ]b4_symbol([$1], [id])[(]b4_symbol_if([$1], [has_type],
+[b4_union_if([b4_symbol([$1], [type]],
+[[typeof(YYSemanticType.]b4_symbol([$1], [type])[]])) [val]])dnl
+[]b4_locations_if([b4_symbol_if([$1], [has_type], [[, ]])[Location l]])[)
+ {
+ return Symbol(TokenKind.]b4_symbol([$1], [id])[]b4_symbol_if([$1], [has_type],
+ [[, val]])[]b4_locations_if([[, l]])[);
+ }]])])
+
+# b4_token_constructor_define
+# ---------------------------
+# Define the overloaded versions of make_symbol for all the value types.
+m4_define([b4_token_constructor_define],
+[[
+ /* Implementation of token constructors for each symbol type visible to
+ * the user. The code generates static methods that have the same names
+ * as the TokenKinds.
+ */]b4_symbol_foreach([_b4_token_constructor_define])])
+
## -------------- ##
## Symbol kinds. ##
## -------------- ##
@@ -593,5 +619,6 @@ m4_define([b4_symbol_type_define],
SymbolKind token() { return kind; }
Value value() { return value_; }]b4_locations_if([[
Location location() { return location_; }]])[
+]b4_token_ctor_if([b4_token_constructor_define])[
}
]])