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

Graham Mer gd.antlr at gmail.com
Thu Apr 14 08:10:11 PDT 2011


> 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?


More information about the antlr-interest mailing list