May 10
On Tue, May 10, 2022 at 04:10:26PM +0000, Ben Jones via Digitalmars-d-learn wrote:
> On Tuesday, 10 May 2022 at 16:05:15 UTC, H. S. Teoh wrote:
> > Using wrapper structs, etc., for this is IMO total overkill. Just use an enum for your token types.  Something like this would suffice:
> 
> That's basically what sumtype is going to do for me, but (hopefully) more safely. Also, the token types are "user defined,"  my lexer just grabs everything annotated with @Token and passes those types/wrapped enums to sumtype.

Ah, I see what you're trying to do.


T

-- 
Why are you blatanly misspelling "blatant"? -- Branden Robinson
May 11

On Tuesday, 10 May 2022 at 16:10:26 UTC, Ben Jones wrote:

>

On Tuesday, 10 May 2022 at 16:05:15 UTC, H. S. Teoh wrote:

>

Using wrapper structs, etc., for this is IMO total overkill. Just use an enum for your token types. Something like this would suffice:

That's basically what sumtype is going to do for me, but (hopefully) more safely. Also, the token types are "user defined," my lexer just grabs everything annotated with @Token and passes those types/wrapped enums to sumtype.

How about being more explicit in the UDA ?
The idea would be to associate the enum value to a type or not:

import std.traits;
import std.stdio;

struct Token(T);

struct Token(T...)
if (T.length == 0) { }

@Token!(string) enum str_tok;
@Token!(float)  enum float_tok;
@Token!()       enum lparen_tok;

void main()
{
    alias toks = getSymbolsByUDA!(mixin(__MODULE__), Token);

    static foreach (t; toks)
    {{
        alias U = getUDAs!(t, Token);
        alias A = TemplateArgsOf!(U);
        static if (A.length)
            pragma(msg, "add a `" ~ A[0].stringof ~ "`for `" ~ t.stringof ~ "`");
        else
            pragma(msg, "no SumType data needed for `" ~ t.stringof ~ "`");
    }}
}
May 11

On Wednesday, 11 May 2022 at 12:29:05 UTC, Basile B. wrote:

>

How about being more explicit in the UDA ?
The idea would be to associate the enum value to a type or not:

I think that could work but would require some major changes to my existing code. Also, I think I'd prefer:

@Token{

 enum lparen = '(';
 enum rparen = ')';
 enum if_token;
}

to what you suggested as a user of the library.

Next ›   Last »
1 2