Make an enum idiomatic D - enhancing GNU Bison's Dlang support
Adela Vais
adela.vais99 at gmail.com
Tue Sep 1 17:47:24 UTC 2020
Hello,
I am working on GNU Bison, in order to enhance the current Dlang
support.
The issue I have is regarding only D, there is no need to be
familiar with Bison.
I need some advice on how to modify the current form of an enum,
SymbolKind, to make it more idiomatic D (or leave it as it is -
similar to how it is handled for the C++ support).
The problem is explained here [0]. (Java allows methods in enums
[1].)
The way I modified it is by wrapping it in a struct and adding
functionalities to change the already existing interaction with
the enum as little as possible [2]. The code I changed in the
repository is written in M4, so I will add the output after Bison
is run:
/* Symbol kinds. */
struct SymbolKind
{
public enum SymbolKindEnum
{
S_YYEMPTY = -2, /* No symbol. */
S_YYEOF = 0, /* "end of file" */
S_YYerror = 1, /* error */
...
S_input = 14, /* input */
S_line = 15, /* line */
S_exp = 16, /* exp */
}
private SymbolKindEnum yycode_;
alias SymbolKindEnum this;
this(int code)
{
yycode_ = cast(SymbolKindEnum) code;
}
public void opAssign(in SymbolKindEnum code)
{
yycode_ = code;
}
public bool opEquals(const int s)
{
return yycode_ == s;
}
SymbolKindEnum value() const
{
return yycode_;
}
/* YYTNAME[SYMBOL-NUM] -- String name of the symbol
SYMBOL-NUM.
First, the terminals, then, starting at \a YYNTOKENS_,
nonterminals. */
private static string[] yytname_ = [
"\"end of file\"", "error", "\"invalid token\"", "\"=\"",
"\"+\"",
"\"-\"", "\"*\"", "\"/\"", "\"(\"", "\")\"", "\"end of line\"",
"\"number\"", "UNARY", "$accept", "input", "line", "exp", null
];
public string toString() const
{
return yytname_[yycode_];
}
}
Should I modify it like this, or is it overkill? Is there a
better way to deal with this?
[0]: https://github.com/akimd/bison/blob/master/TODO#L255
[1]: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
[2]: https://github.com/adelavais/bison/tree/symbol-kind
More information about the Digitalmars-d
mailing list