CT regex in AA at compile time

Patrick Schluter Patrick.Schluter at bbox.fr
Tue Jan 7 16:19:11 UTC 2020


On Tuesday, 7 January 2020 at 15:40:58 UTC, Taylor Hillegeist 
wrote:
> I'm trying to trick the following code snippet into compilation.
>
> enum TokenType{
> 	//Terminal
> 	Plus,
> 	Minus,
> 	LPer,
> 	RPer,
> 	Number,
> }
>
> static auto Regexes =[
>   TokenType.Plus:   ctRegex!(`^ *\+`),
>   TokenType.Minus:  ctRegex!(`^ *\-`),
>   TokenType.LPer:   ctRegex!(`^ *\(`),
>   TokenType.RPer:   ctRegex!(`^ *\)`),
>   TokenType.Number: ctRegex!(`^ *[0-9]+(.[0-9]+)?`)
> ];
>
> but I can't get it to work. it says its an Error: non-constant 
> expression.
>
> I imagine this has to do with the ctRegex template or 
> something. maybe there is a better way? Does anyone know?

In that specific case: why don't you use an array indexed on 
TokenType? TokenType are consecutive integrals so indexing is the 
fastest possible access method.


More information about the Digitalmars-d-learn mailing list