On Friday, 16 January 2015 at 15:55:53 UTC, Brian Schott wrote:
On Friday, 16 January 2015 at 15:06:42 UTC, Ary Borenszweig wrote:
The way I did it in Descent (I copied the logic from JDT) is to parse the code into an AST, and then walk the AST in sync with a lexer.

My dfmt tool does something similar. The parser runs over the code first and makes notes on things like the location of unary operators and which braces end function/aggregate declarations. Then the formatter iterates over the tokens (including comments) and is able to correctly print "a* b;" instead of "a * b;".

This is a short talk about clang-format's design and implementation:
https://www.youtube.com/watch?v=s7JmdCfI__c

They have this concept of "unwrapped lines" into which the source code is split by a "structural parser". I am not sure, if dfmt really needs it, because we don't have cpp macros littered around in D code.

While dfmt already works well for some code (e.g. the snippet on dlang.org frontpage), the biggest hurdle (imho) is an expression formatter, which for example understands operator precedences. Currently, dfmt uses a greedy line fill algorithm.

Reply via email to