Parse d source file by using compiler

Brian Schott via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Nov 9 17:15:39 PST 2015


On Monday, 9 November 2015 at 05:49:25 UTC, tcak wrote:
> I checked for a flag in this page 
> http://dlang.org/dmd-linux.html , but couldn't have found any 
> for this purpose.
>
> Is there a way to parse a d source file so it generates a tree 
> in JSON, XML, or something-that-can-be-processed-easily file 
> format?
>
> ---
>
> My real purpose:
>
> I need to generate hash code (e.g. MD5) for a part of source 
> code (let's say a class, struct, or a function). So whether the 
> codes are changed or not can be detected. As you will guess, 
> comments, text formatting etc. shouldn't affect the hash result.
>
>
> Use-Case:
>
> I am writing a code generator/back up system. It will check the 
> last available code file. If important changes are done in a 
> specific part of code, it will increase version number by 1.

dscanner --ast path/to/file.d \
     | xmllint --xpath 
"//classDeclaration[name='ClassYouCareAbout']" - \
     | md5sum

https://github.com/Hackerpilot/Dscanner

The only problem here is that D-Scanner's XML output includes 
<ddoc> tags. You should be able to strip those out with sed or 
something.


More information about the Digitalmars-d-learn mailing list