www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Ways to parse D code.

reply Jan =?UTF-8?B?SMO2bmln?= <hrominium gmail.com> writes:
What is the "easiest" way to parse D code?

Given an Expression/Statement/Function/Template.... I want to put 
it into a program, and it returns me an AST.

D-Scanner seems to do that with `--ast` argument. I would need to 
dig into it, to get it programmatically, instead of as XML on the 
stdout.
https://github.com/dlang-community/D-Scanner

libdparse seems to do it as well with `parseModule` function.
https://github.com/dlang-community/libdparse/blob/master/src/dparse/parser.d

dmd has to do it somewhere as well. Although I don't know exactly 
where. I do know ldc uses dmd's frontend for parsing.
https://dlang.org/phobos/dmd_parse.html


I am also a little confused about who uses what. Does D-Scanner 
use libdparse?
Is there a D grammar for pegged?
https://github.com/PhilippeSigaud/Pegged

Thank you for any hints!
Nov 25 2020
next sibling parent Dennis <dkorpel gmail.com> writes:
On Wednesday, 25 November 2020 at 16:27:41 UTC, Jan Hönig wrote:
 What is the "easiest" way to parse D code?
 (...)
 libdparse seems to do it as well with `parseModule` function.
 https://github.com/dlang-community/libdparse/blob/master/src/dparse/parser.d
I recommend libdparse.
 dmd has to do it somewhere as well. Although I don't know 
 exactly where. I do know ldc uses dmd's frontend for parsing.
 https://dlang.org/phobos/dmd_parse.html
DMD as a library is still experimental. You can try to use it, but libdparse is more stable.
 I am also a little confused about who uses what. Does D-Scanner 
 use libdparse?
Yes, most tools that parse D code do, including Adam's documentation generator: https://github.com/adamdruppe/adrdox Only LDC, GDC and VisualD use the dmd front-end as far as I know.
 Is there a D grammar for pegged?
 https://github.com/PhilippeSigaud/Pegged
Not complete and outdated, but it's a start: https://github.com/PhilippeSigaud/Pegged/tree/master/examples/dgrammar
Nov 25 2020
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2020-11-25 17:27, Jan Hönig wrote:

 dmd has to do it somewhere as well. Although I don't know exactly where. 
 I do know ldc uses dmd's frontend for parsing.
 https://dlang.org/phobos/dmd_parse.html
Using DMD as a library will be most accurate and up to date. Because it's the same code as the compiler is using. Here's an example on how to use DMD to parse some code [1]. Here's some more advance usages [2]. [1] https://github.com/dlang/dmd/blob/b35572b07a6994385b6459a430674d32e9a97279/test/dub_package/frontend.d#L10-L24 [2] https://github.com/jacob-carlborg/dlp/blob/master/source/dlp/commands/infer_attributes.d#L61 -- /Jacob Carlborg
Nov 26 2020