Want to help DMD bugfixing? Write a simple utility.

Don nospam at nospam.com
Fri Mar 25 05:40:25 PDT 2011


spir wrote:
> On 03/25/2011 12:08 PM, Regan Heath wrote:
>> On Wed, 23 Mar 2011 21:16:02 -0000, Jonathan M Davis 
>> <jmdavisProg at gmx.com> wrote:
>>> There are tasks for which you need to be able to lex and parse D 
>>> code. To
>>> 100% correctly remove unit tests would be one such task.
>>
>> Is that last bit true? You definitely need to be able to lex it, but 
>> instead of
>> actually parsing it you just count { and } and remove 'unittest' plus 
>> { plus }
>> plus everything in between right?
> 
> At first sight, you're both wrong: you'd need to count { } levels. Also, 
> I think true lexing is not really needed: you'd only need to put apart 
> strings and comments that could hold non-code { & }.
> (But these are only very superficial notes.)
> 
> Denis

Yes, exactly: you just need to lex strings (including q{}), comments 
(which you remove),
unittest, and count levels of {.
You need to worry about backslashes in comments, but that's about it.

I even did this in a CTFE function once, I know it isn't complicated.
Should be possible in < 50 lines of code.
I just didn't want to have to do it myself.

In fact, it would be adequate to replace:
unittest
{
    blah...
}
with:
unittest{}

Then you don't need to worry about special cases like:

version(XXX)
unittest
{
...
}


More information about the Digitalmars-d-learn mailing list