Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jdt-core-dev] Suggestion for implementing a code formatter

A code formatter needs to have the stream of tokens to reformat. It also
needs to be able to recognize templates of source which describe the
formatting style. Pattern matching is doable but quite hard to achieve if
intending to cover the entire grammar specificities. Or in the end, the
complexity might be close to a full fledge parser.

So, we could use the standard parser to figure which templates are
involved, and then traverse the resulting AST using the token stream in
parallel and a set of templates (containing the formatting rules).

The entire AST would get traversed, and each parent node would be
responsible for finding the best matching template to use (e.g. could be
several IF layouts), then consume tokens from the token stream as long as
they are belonging to the current AST node (enclosed by the AST start/end
positions). If a token is detected as being part of a child node (based on
source positions), then the traversal would be delegated to the child node
until hitting a token which no longer belong to the child node, in which
case, control would return to the parent node... etc...

This approach would take care of comments (and/or whitespaces) since these
would have been tokenized into the token stream.

I also believe the token stream doesn't need to be eagerly built, since
only one token at a time is necessary according to this approach.



Back to the top