[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Displaying Ignored Tokens Inline With AST



How might one display an AST with all the ignored tokens (comments/whitespace) included without having to implement analysis code for each node type?  It's straightforward to extend Lexer and Parser to collect all the ignored tokens and associate them with the following or preceding nodes as you parse.  But I'm not sure how to "inject" the display of the ignored tokens into the display code provided by the various toString methods on all the node types.
 
I want to trying to parse, analyze, tweak, and resave Java source .  I'd like to retain all whitespace and comments where possible.  But I don't want to have to implement display code in Analysis for each production alternative.  I think what could work is modify Node.toString(Node) from:
 
    protected String toString(Node node)
    {
        if(node != null)
        {
            return node.toString();
        }
        return "";
    }
to:
 
    protected String toString(Node node)
    {
        ... // display preceding ignored tokens

        if(node != null)
        {
            return node.toString();
        }
        return "";
    }
But since Node is generated, this isn't very feasible.  Any better ideas?
 
Thanks,
Evan