[antlr-interest] Previous rule element reference in a scope

Jim Idle jimi at temporal-wave.com
Thu Apr 14 08:48:14 PDT 2011


Perhaps my emails are not getting through one way or another.

The scope goes in your tree walker.
The scope contains a tree node
Push a scope in the WITH rule
Add the expression node to the scope
Use the scope when you see a . reference, it will automatically be the
correct scope
Done.

You say:
> For that input, I want an AST like:
>
> ^(CALL foo.method1)
> ^(ASSIGN x foo.bar.property)
>

No. You want:

^(CALL ^(DOT method1))
^(ASSIGN x ^(DOT ^(DOT bar property))

Then

with
scope
{
	CommonTreeNode withExpr;
}
: ^(WITH e=expression { $with::withExpr = $e; } block)
;

Then when you find a DOT node with just one child, you can reference the
expression to use via $with::withExpr

So, don't try to solve this with tree rewrites, just process it naturally
in the walker.

Jim


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Graham Mer
> Sent: Thursday, April 14, 2011 8:10 AM
> To: Peter Kooiman
> Cc: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] Previous rule element reference in a
> scope
>
> > There's your problem..the enclosed block needs to be part of the
> withStatement rule I would say (how else would you decide when the
> object in the with statement goes out of scope?). Once you bring the
> enclosed block into the rule, you will be able to reference the
> enclosing withStatement rule scope.
>
> Not a problem, there is an End With statement to terminate the block
> (but even if there wasn't, the end of the block would suffice). I
> really didn't think this was relevant to the original question - there
> are independent statements, including With and method calls (among
> others), and I want to distribute the object of the With into the other
> statements in its block. I provided the two rules as an example of the
> producer and one consumer of the token, but that is by no means the
> complete grammar.
>
> It doesn't seem practical to get the enclosed block into the
> withStatement, since With and every other statement (there are many)
> can be in the block, and you can nest With statements. This really
> seems like a scope to me, so it is possible to get the reference into
> the scope, and if so, what's the syntax, and what are the best
> practices when doing so?
>
> Another example:
>
> With foo
>   .method1()
>   With .bar
>     x = .property
>   End With
> End with
>
> For that input, I want an AST like:
>
> ^(CALL foo.method1)
> ^(ASSIGN x foo.bar.property)
>
> So, given that there are lots and lots of other statements that could
> be in the With block, I think I really do want a scope rooted in the
> With statement. I just need to know how to make it work. Specifically,
> I cannot see how to get the With object token reference into the scope,
> and how to retrieve it in the statements that can use it so that I can
> construct the desired AST. Passing it as an argument would require
> passing the argument to every single statement and expression rule,
> which seems like a bad design, and seems to be what the scope was
> designed to improve.
>
> Do I completely misunderstand the purpose of scopes? Are they not
> intended to be used during AST construction?
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address


More information about the antlr-interest mailing list