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

[wishlist] State machine analyses



I would like a new kind of adapter that keeps a reference to a 'state'
DepthFirstAdapter, and, for each in/case/out method, invokes the
in/case/out methods of the current 'state'.  This analysis would also have
a method

  void setState(DepthFirstAdapter state);

to transition to a different state.  This would make it easier to write
analyses that change 'mode' upon encountering a given node.  Instead of
writing a large number of methods that look like:

  void inANode1(ANode1 node) {
    switch (state) {
      case A: ...; break;
      case B: ...; break;
      ...
    }
  }

you would write inner classes:

  class StateA extends DepthFirstAdapter {
    void inANode1(ANode1 node) { ... }
    ...
  }

  class StateB extends DepthFirstAdapter {
    void inANode1(ANode1 node) { ... }
    ...
  }

  class StateC extends DepthFirstAdapter {
    void inANode1(ANode1 node) { ... }
    ...
  }

This would be better because

  + Switch statements are error-prone
  + Separating the logic for the different states into distinct classes is
better (at least, sometimes) than mingling them in all the various node
visitor methods.
  + Cases where states do the same thing could be captured using common
superclasses for the state classes, which is more flexible than
fallthroughs in switch statements.

So, is there any chance you could add this to SableCC?  It seems almost
simple enough to do myself, but I'm too busy these days to dive into
hacking on a parser generator.

~k.lee

p.s. Since I'm using MultiJava (http://www.multijava.org) it would be
almost as nice to pass the State as an extra parameter to the visitor
methods (one could then use multi-dispatch to distinguish the various
states), but since MultiJava isn't widely used I wouldn't expect you to do
it that way.