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

Declaring Common Nodes and Accessors in P-classes



Etienne, anyone else,

Have you given much/any thought to the possibility of moving the
node+accessors for nodes that are common across all of a production's
alternatives into the production superclass?  What I mean is, consider a
production with a couple alternatives such as try_statement from the Java
1.1 grammar:

try_statement =
    {try}
        try block catch_clause+ |
    {finally}
        try block catch_clause* P.finally;

Because catch_clause is common in name and in type to both alternatives, the
list and its accessors could be put into the PTryStatement class.  This
could be done for all cases where the signatures were truly identical, even
if the semantic meaning of the node were different.

The benefits would be threefold.
1. there might be a little code space savings, of course as the cost of the
additional analysis.
2. one could write code that accepts PTryStatements and uses their
catch_clauses without forcing the caller to call getCatchClause() on the
subtype and pass a weakly typed LinkedList.

Instead of:
    void caseATryTryStatement(ATryTryStatement node)
    {
        processCatchClauses(node.getCatchClause());
    }
    void caseAFinallyTryStatement(AFinallyTryStatement node);
    {
        processCatchClauses(node.getCatchClause());
    }
    void processCatchClauses(LinkedList list) { ... }
You'd have:
    void caseATryTryStatement(ATryTryStatement node)
    {
        processCatchClauses(node);
    }
    void caseAFinallyTryStatement(AFinallyTryStatement node);
    {
        processCatchClauses(node);
    }
    void processCatchClauses(PTryStatement stmt)
    {
        LinkedList list = stmt.getCatchClause();
    }

3. For single-alternative productions you would not need to down cast to the
alternative to gain access to the nodes.  For example, instead of:

    void caseAFinallyTryStatement(AFinallyTryStatement node);
    {
        AFinally finally = (AFinally)node.getFinally(); // downcase to
single alternative
        ABlock finally_block=  finally.getBlock();
    }
you could avoid the case with

    void caseAFinallyTryStatement(AFinallyTryStatement node);
    {
        PFinally finally = node.getFinally();
        ABlock finally_block=  finally.getBlock();
    }


Benefits #1 and #3 seem to provide the strongest rationale.

So is this idea a reasonable one?  Is it something that would be difficult
to implement?  Or are the problems that this type of feature would address
just minor inconveniences in other people's minds?

Evan