[antlr-interest] Handling 'finally' in language target that doesn't have 'finally'
Gavin Lambert
antlr at mirality.co.nz
Tue Jan 20 01:50:59 PST 2009
At 09:27 20/01/2009, Sam Harwell wrote:
>For the sake of creating an ANTLR target (I think that's
>the real goal?), you have a few options. If you simply
>want to disallow finally code in the grammar when the
>C++ target is used, make the template for finally code
>spit out the following if the finally code is not blank.
>
>#error The C++ target does not support 'finally' blocks in
>grammars. Use RAII instead.
Sure, you could do that, but why not just make it work?
As you posted earlier, any psuedoblock:
try
{
[a]
}
finally
{
[b]
}
is exactly identical (in terms of behaviour) to this C++ code:
try
{
[a]
}
catch(...)
{
[b]
throw;
}
{
[b]
}
(Note the extra braces at the end; that's to keep the variable
scoping clean. I guess if you're ultra-paranoid, you could wrap
the whole thing in another set, since technically the first is a
single statement while the second is two statements. But that's
usually not an issue.)
Sure, it's a little messier than using RAII, but that's just an
incentive to use RAII wherever possible. But if you really want
finally-like behaviour without RAII (eg. to support the ANTLR
grammar keyword), then it's trivial to add. (It's even more
trivial when using a template or codegen like ANTLR, since the
duplication of [b] will only appear in the generated file, not the
source file. And the generated code is ugly anyway, so it doesn't
matter so much.)
More information about the antlr-interest
mailing list