[Moving this thread out of the ticket queue because it's no longer
relevant to the original ticket.)

On Tue, Nov 18, 2008 at 07:26:47PM +0100, Bernhard Schmalhofer wrote:
> So my real problem is:
>    In the action for <thingy> how do I determine that the match is below 
> <TOP>?

How do you mean "determine that the match is below <TOP>?"  From the
example that was in the test file, it's clear that <thingy> is
below <TOP> because that's the only place it's called from.

I think you're wanting higher rules to be able to store state information
for subrules.  There are at least a couple of ways to do something like
this:

1.  With a special subrule:

    grammar Foo {
        token TOP    { <.INIT> <thingy> {*} }
        token INIT   { {*} }
        token thingy { 'thingy' {*} }
    }

    ...
    method TOP($/) { make $( $<thingy> ); }

    method INIT($/) {
       our $?MY_OUR_VAR := 'was passed down';
    }

    method thingy($/) {
        our $?MY_OUR_VAR;
        my $past := PAST::Op.new( 
                        :pirop('say'),
                        PAST::Val.new( 
                            :value( 'our var ' ~ $?MY_OUR_VAR )
                        )
                    );
        make $past;
    }

Here the <.INIT> subrule gets called before <thingy>, so
its actions take place before <thingy> is parsed.

2.  With action keys:

    grammar Foo {
        token TOP {
            {*}            #= init
            <thingy>
            {*}            #= close
        }
        token thingy { 'thingy' {*} }
    }

    ...
    method TOP($/, $key) {
        if $key eq 'init' {
            our $?MY_OUR_VAR := 'was passed down';
        }
        if $key eq 'close' {
            make $( $<thingy> );
        }
    }

Here the TOP action method gets called twice -- once
with a $key of 'init' when TOP is first entered, and again
with a key of 'close' after <thingy> has been parsed.

Note that in Rakudo we do this very rarely, though, as
the design of PCT is intended to be such that the compiler
doesn't have to always know exactly how something was
declared in an outer scope in order to generate the
correct thing in an inner scope.

That said, I am planning to have PCT provide a way to
quickly look up symbols in outer block nodes for when
it is needed.

Pm

Reply via email to