[prev in list] [next in list] [prev in thread] [next in thread] 

List:       antlr-dev
Subject:    [antlr-dev] grammar/template change: finally now part of rule not
From:       parrt () cs ! usfca ! edu (Terence Parr)
Date:       2007-02-10 22:59:07
Message-ID: 74090C0E-367B-44AC-BC0A-957989822302 () cs ! usfca ! edu
[Download RAW message or body]

Folks, changed @finally to be @after and added a finally clause to  
the exception stuff.  I also removed the superfluous "exception"  
keyword.  Here's what the new syntax looks like:

a
@after { System.out.println("ick"); }
   : 'a'
   ;
   catch[RecognitionException e] { System.out.println("foo"); }
   catch[IOException e] { System.out.println("io"); }
   finally { System.out.println("foobar"); }

@after executes after $rule.stop, $rule.tree are set and before  
scopes pop and memoization happens.  Dynamic scopes and memoization  
are still in finally block because they must exec even if error in  
rule.  The @after action and tree setting stuff can technically be  
skipped upon syntax error in rule.  Later we might add something to  
finally to stick an ERROR token in the tree and set the return value.

Oh, the finally block you specify in the grammar executes after  
everything else: error reporting/recovery, tree setting, @after, and  
memoization.

IMPLEMENTATION

Basically, we change

         <(ruleDescriptor.actions.finally):execAction()>

to
	<finally>

and add

         <(ruleDescriptor.actions.after):execAction()>

to the try block.

We don't protect (!guessing) the finally action now as it's part of  
clean up / error handling.  Use @after {...} action to exec an action  
after rule finishes.

I had to move the memoization stuff out of the ruleCleanUp template:

memoize() ::= <<
<if(memoize)>
<if(backtracking)>
if ( backtracking>0 ) { memoize(input, <ruleDescriptor.index>,  
<ruleDescriptor.\
name>_StartIndex); }
<endif>
<endif>
 >>

so I could keep it in finally block and mvoe ruleCleanUp into try.

Added finally as an attribute to rule() template.  Now my finally  
block (for rule not lexerRule) looks like:

     finally {
         <if(trace)>traceOut("<ruleName>",  
<ruleDescriptor.index>);<endif>
         <ruleCleanUp()>
         <memoize()>
         <finally>
         <ruleScopeCleanUp()>
     }

Refs to finally action become "after" in the templates.  AND it moves  
out of the finally block into the try:

     try {
         <ruleMemoization(name=ruleName)>
         <block>
         <(ruleDescriptor.actions.after):execAction()>  <! new  
location !>
     }

I moved the action from finally into try for lexerRule also.

Changes should be clear looking at the Java.stg file.

I verified that this works with Mantra grammar, that uses @after like  
crazy.  It properly sets trees and such before the @after action in  
the try block not the finally.

edit //depot/code/antlr/main/README.txt#149
edit //depot/code/antlr/main/src/org/antlr/Tool.java#102
edit //depot/code/antlr/main/src/org/antlr/codegen/codegen.g#98
edit //depot/code/antlr/main/src/org/antlr/codegen/templates/ 
ANTLRCore.sti#23
edit //depot/code/antlr/main/src/org/antlr/codegen/templates/Java/ 
Java.stg#94
edit //depot/code/antlr/main/src/org/antlr/test/TestAttributes.java#56
edit //depot/code/antlr/main/src/org/antlr/tool/antlr.g#74
edit //depot/code/antlr/main/src/org/antlr/tool/antlr.print.g#39
edit //depot/code/antlr/main/src/org/antlr/tool/assign.types.g#42
edit //depot/code/antlr/main/src/org/antlr/tool/buildnfa.g#54
edit //depot/code/antlr/main/src/org/antlr/tool/define.g#50
edit //depot/code/usf/mantra/main/src/mantra/compiler/CodeGenPhase.g#4
edit //depot/code/usf/mantra/main/src/mantra/compiler/Mantra.g#3
edit //depot/code/usf/mantra/main/src/mantra/compiler/ResolvePhase.g#3
edit //depot/code/usf/mantra/main/src/mantra/compiler/SemanticPhase.g#3
Change 3425 submitted.

Don't forget to rebuild all parsers.

Shite.  Now I have to go change everything in the book. ;)

Thanks are due to suggestion by Kevin Ruland.

Ter
PS	see the readme for a shorter version of this email.  Feb 10 entry.

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic