From f4a3d6f3a826fff46e0b2cd94f7ed4e03b19ab90 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Fri, 26 Oct 2012 15:46:22 -0500 Subject: [PATCH] Move initialization of finallyAction to fillNamedActions - factory cannot provide context information for the Action construction before the constructor completes --- tool/src/org/antlr/v4/codegen/model/RuleFunction.java | 5 ++++- tool/test/org/antlr/v4/test/TestActionTranslation.java | 8 +++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/tool/src/org/antlr/v4/codegen/model/RuleFunction.java b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java index 299d1f0a7..6cddce32a 100644 --- a/tool/src/org/antlr/v4/codegen/model/RuleFunction.java +++ b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java @@ -125,7 +125,6 @@ public class RuleFunction extends OutputModelObject { exceptions.add(new ExceptionClause(factory, catchArg, catchAction)); } } - if ( r.finallyAction!=null ) finallyAction = new Action(factory, r.finallyAction); startState = factory.getGrammar().atn.ruleToStartState[r.index]; } @@ -157,6 +156,10 @@ public class RuleFunction extends OutputModelObject { } public void fillNamedActions(OutputModelFactory factory, Rule r) { + if ( r.finallyAction!=null ) { + finallyAction = new Action(factory, r.finallyAction); + } + namedActions = new HashMap(); for (String name : r.namedActions.keySet()) { ActionAST ast = r.namedActions.get(name); diff --git a/tool/test/org/antlr/v4/test/TestActionTranslation.java b/tool/test/org/antlr/v4/test/TestActionTranslation.java index ce1c79669..f243efb6c 100644 --- a/tool/test/org/antlr/v4/test/TestActionTranslation.java +++ b/tool/test/org/antlr/v4/test/TestActionTranslation.java @@ -124,15 +124,13 @@ public class TestActionTranslation extends BaseTest { @Test public void testRefToTextAttributeForCurrentRule() throws Exception { String action = "$a.text; $text"; + + // this is the expected translation for all cases String expected = "_input.getText(_localctx.start, _input.LT(-1)); _input.getText(_localctx.start, _input.LT(-1))"; + testActions(attributeTemplate, "init", action, expected); - expected = - "_input.getText(_localctx.start, _input.LT(-1)); _input.getText(_localctx.start, _input.LT(-1))"; testActions(attributeTemplate, "inline", action, expected); - expected = - "(_localctx.a!=null?_input.getText(_localctx.a.start,_localctx.a.stop):null);" + - " _input.getText(_localctx.start, _input.LT(-1))"; testActions(attributeTemplate, "finally", action, expected); }