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.
Vote for this RCR
The Ruby grammar is implemented with lex/yacc IIRC. However, I'm not sure that this makes any sense as stated. As of right now, it's not necessary to enclose parameter lists inside of parentheses, so no "code" can be between def foo and (a, ...). I also don't think that meta-code is necessary or useful for the parameter lists themselves.
Far better would be the oft-suggested (and it might even be on the current list of RCRs) change that all keywords return something useful, e.g.:
- def foo ... end => :foo (or def foo ... end => self.method(:foo))
- class Foo ... end => Foo
- module Bar ... end => Bar
And so on, as needed. -- Austin Ziegler
This RCR is breaking a few RCR rules:
- NEVER mention POLS in the proposal.
- proposal should be concrete, so that we can implement it.
- ratinale/reason required. mere generalization is not enough.
Please resubmit it, if you really want to propose something.
--matz.
Back to RCRchive.
RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog