Rolf Nelson wrote:
> I have a question about
> your use of the "bless" mechanism to add 
> additional metadata onto a simple array.
> 
> I was wondering what set of constraints lead 
> you to use this mechanism, rather than some 
> other method of associating metadata with each array?

when you register a rule, I eventually want Parse::Nibbler
to create a package with the same name as the name of the rule
and to define that package to 'use base Parse::Nibbler'.
(and perhaps use base on some user defined package as well)

when you're done parsing, you can take the array that 
contains the lexical data in it, and simply call a method 
such as "PrettyPrint" or some such thing. 

blessing the array means that it will first look in the
package named after the rule name. If it doesn't find the
method there, it will use the base class method.

This means the user can define default behaviour for
all the rules, and then individually overload the 
behaviour for each specific rule.

example: every rule pretty prints using the default 
method except for the rule that handles Tables.
Tables have its own pretty print method which
makes sure that Tables get printed in a nice,
column aligned format.

example: Every rule has its own method to "Compile".
once you've parsed the text into arrays, you could
call the Compile method on each rule, and have it
spit out the appropriate byte code.  

The point was to design the parser from the beginning
so that you can write your entire grammar in pure perl.
Bless gives you the hooks so that you can define
the behaviour of how to handle the text once you've
finished parsing.

The only thing really outside the box was to have
the Register subroutine create a package on the fly 
with the same name as the Rule and to have that package 
'use base' on some base package.

The user can then define whatever other behaviour
they want for those packages/rules. Or, if the user
does nothing, the rule automatically gets whatever
defined default behaviour in the base package.

The rest of it is just Object Oriented Perl,
method calls on objects. The objects just happen
to be arrays of lexicals versus hashes with
standard looking Object data.

Greg

Reply via email to