Is anyone working on a D source code formatting tool?
Ary Borenszweig via Digitalmars-d
digitalmars-d at puremagic.com
Fri Jan 16 07:06:39 PST 2015
On 1/11/15 3:48 PM, Walter Bright wrote:
> On 1/11/2015 9:45 AM, Stefan Koch wrote:
>> I'm powerful writing a parser-generator, that will be able to
>> transform the
>> generated parse-tree back into source automatically.
>> writing a rule-based formatter should be pretty doable.
>
> Formatting the AST into text is straightforward, dmd already does that
> for .di file generation.
>
> The main problem is what to do about comments, which don't fit into the
> grammar.
>
> A secondary problem is what to do when the line length limit is
> exceeded, such as for long expressions.
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. So if
you have this:
void /* comment /* foo() {}
the AST would be a FunctionDecl (whatever the name is) so you'd expect a
type (consume that AST node, in sync with the lexer), then check for
comments/newlines/etc., skip/print them, then consume the name, check
for comments/newlines/etc.
That way the AST doesn't have to know anything about comments, but
comments need to be known by the lexer (via a flag, probably).
Considering how flexible is JDT's formatter, I think this solution is
pretty good.
More information about the Digitalmars-d
mailing list