|
||||
| << | >> |
|
Comments. There are two basic ways of using a parser. You can create a tree (an AST) which can
then be walked arbitrarily; or you can set up methods that will be called when the source is scanned
and certain constructs are encountered. Think of these in terms of XML parsing: You have DOM-style
and you have SAX-style respectively.
But I don't want to have to use any such API in my little refactoring scripts. I don't want to keep track of character offsets manually and walk the tree in search of certain tokens. I am suggesting a third higher-level approach in addition to the other two, a "reflection" style approach. This will be on top of the AST, not replacing it; it will simply be a higher level of abstraction.
What do I mean by "reflection"? Well, think about Ruby's reflection API. To ask the names of the
instance methods of Array, I can simply say But the concept is similar to what I'm suggesting. I want easy, pre-parsed, "canned" access to every set of elements in the Ruby source being edited.
For this proof of concept, I only implemented two API methods:
I'm not a parser guy. The parser code I'm using currently is based on
This is truly ugly code. The file is named |