RCR 228: Uniform meta-access while parsing Ruby code
submitted by itsme213 on Thu Mar 11 2004 11:34:50 AM -0800
Status: withdrawn
Abstract
Ruby's exposing of self within a class body is one of its powerful extensibility features. This RCR proposes similar uniform access via self to methods, parameters, constants, etc. to allow extensibility of all of these elements.
Problem
Facilities such as attr rely on methods on Class being invoked within a class body with self = current_class. By POLS, similar access should be provided to methods defined on syntactic elements such as:
- Method
- Parameter
- Constant
Proposal
Generalize class facilities like attr (in the same spirit as SAX events) by doing the following:
- Expose classes for as many syntactic elements as possible: Method, Parameter, Constant, perhaps even more fine grained such as LocalVariable, InstanceVaeiable, Block, Expression.
- Allow these classes to be extended with new methods (of course).
- Ensure that
self is bound to the smallest, most current object in as many places as possible in the grammer e.g.:
def foo ** (a *** ...)
- at **, self is Method foo
- at ***, self is Param a
- Add a few more sentinel methods, guaranteed called while parsing, with empty default implementation e.g. Class#end_of_body (to be called at the "end" of the class. These can be overriden to do further extension processing e.g. register classes with selected properties at their "end".
Analysis
- New meta-level methods and meta-data can be defined and used easily
- The AST is available incrementally while parsing
- Opens up a whole new level of extending Ruby at the meta-level for typing, documentation, method combination, module combination and aspects, etc.
Implementation
If I knew more of how the Ruby grammar was being implemented I could probably suggest something.
Back to RCRchive.
RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog