Compile time regex matching

Philippe Sigaud via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 15 08:24:37 PDT 2014


> I did, and I got it to work. Unfortunately, the code used to in the CTFE is
> left in the final executable even though it is not used at runtime. So now
> the question is, is there away to get rid of the excess baggage?

Not that I know of. Once code is injected, it's compiled into the executable.

>   auto result = MyRegex(import("config-file.txt")); // compile-time parsing
>   return "writeln(\""~result.matches[0]~"\");";


>   mixin(get_match());

I never tried that, I'm happy that works.

Another solution would be to push these actions at runtime, by using a
small script instead of your compilation command. This script can be
in D.

- The script takes a file name as input
- Open the file
- Use regex to parse it
- Extract the values you want and write them to a temporary file.
- Invoke the compiler (with std.process) on your main file with -Jpath
flag to the temporary file. Inside your real code, you can thus use
mixin(import("temp file")) happily.
- Delete the temporary file once the previous step is finished.

Compile the script once and for all, it should execute quite rapidly.
It's a unusual pre-processor, in a way.


More information about the Digitalmars-d-learn mailing list