Mixin demo: associative array initializers (with support for comments)
janderson
askme at me.com
Sun Feb 11 16:46:49 PST 2007
Neat!
Reiner Pope wrote:
> After considerable work, I've made an associative array initializer that
> works via mixin(), so you can write the following code:
>
>> auto myAA = AAInit!(int, char[], `
>> "11" => 11; // Comment
>> /* Another comment */
>> "fruit /+ this isn't a comment+/ " => 42;
>> "apple" => -1;
>> `);
>>
>> foreach (key, value; myAA)
>> {
>> writefln("[%s => %s]", key, value);
>> }
>
> and get:
>
>> [11 => 11]
>> [fruit /+ this isn't a comment+/ => 42]
>> [apple => -1]
>
> I've attached the code. Most of the work was in the parser
> (specifically, BracketedIndexOf!(needle, haystack), which can find the
> index of a symbol, taking into account the D rules on comments and quotes).
>
> However, although it looks pretty horrible, it's a *much* easier job
> than having to parse the whole thing -- all it needs to do is read
> through and ignore comments and things inside quotes, and DMD will
> actually format it all correctly.
>
> Enjoy!
>
>
> Reiner
Neat!
More information about the Digitalmars-d
mailing list