Mixin demo: associative array initializers (with support for comments)

Reiner Pope xxxx at xxx.xxx
Sun Feb 11 04:22:55 PST 2007


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
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: aainit.d
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20070211/3626d3c6/attachment.ksh>


More information about the Digitalmars-d mailing list