From 51c38c110349c5698c0dfce9ea8679fa90305780 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Wed, 29 Feb 2012 07:56:29 -0600 Subject: [PATCH] Use single int _alt in generated code instead of a new _alt for each PlusBlock and StarBlock --- .../v4/tool/templates/codegen/Java/Java.stg | 24 +++++++++++-------- .../v4/codegen/OutputModelController.java | 1 + .../org/antlr/v4/codegen/SourceGenTriggers.g | 6 ++++- .../antlr/v4/codegen/model/RuleFunction.java | 1 + 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg index 7992fbb87..422e4ac3d 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg @@ -225,6 +225,9 @@ RuleFunction(currentRule,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAc try { + + int _alt; + _localctx.stop = _input.LT(-1); @@ -258,6 +261,9 @@ LeftRecursiveRuleFunction(currentRule,code,locals,ruleCtx,altLabelCtxs, try { + + int _alt; + _localctx.stop = _input.LT(-1); @@ -409,29 +415,27 @@ case : } >> -// TODO: we we need uniqueID? a single _alt might work - StarBlock(choice, alts, sync, iteration) ::= << setState(); _errHandler.sync(this); -int _alt = getInterpreter().adaptivePredict(_input,,_ctx); -while ( _alt!= && _alt!=-1 ) { - if ( _alt==1 ) { +_alt = getInterpreter().adaptivePredict(_input,,_ctx); +while ( _alt!= && _alt!=-1 ) { + if ( _alt==1 ) { } setState(); _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,,_ctx); + _alt = getInterpreter().adaptivePredict(_input,,_ctx); } >> PlusBlock(choice, alts, error) ::= << setState(); _errHandler.sync(this); -int _alt = getInterpreter().adaptivePredict(_input,,_ctx); +_alt = getInterpreter().adaptivePredict(_input,,_ctx); do { - switch ( _alt ) { + switch ( _alt ) { : @@ -441,8 +445,8 @@ case : } setState(); _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,,_ctx); -} while ( _alt!= && _alt!=-1 ); + _alt = getInterpreter().adaptivePredict(_input,,_ctx); +} while ( _alt!= && _alt!=-1 ); >> Sync(s) ::= "sync();" diff --git a/tool/src/org/antlr/v4/codegen/OutputModelController.java b/tool/src/org/antlr/v4/codegen/OutputModelController.java index 3e7bfd7cc..d3aadfdf1 100644 --- a/tool/src/org/antlr/v4/codegen/OutputModelController.java +++ b/tool/src/org/antlr/v4/codegen/OutputModelController.java @@ -274,6 +274,7 @@ public class OutputModelController { try { // walk AST of rule alts/elements function.code = DefaultOutputModelFactory.list(walker.block(null, null)); + function.hasLookaheadBlock = walker.hasLookaheadBlock; } catch (org.antlr.runtime.RecognitionException e){ e.printStackTrace(System.err); diff --git a/tool/src/org/antlr/v4/codegen/SourceGenTriggers.g b/tool/src/org/antlr/v4/codegen/SourceGenTriggers.g index 11c9720da..b7adef656 100644 --- a/tool/src/org/antlr/v4/codegen/SourceGenTriggers.g +++ b/tool/src/org/antlr/v4/codegen/SourceGenTriggers.g @@ -47,6 +47,7 @@ import java.util.HashMap; @members { public OutputModelController controller; + public boolean hasLookaheadBlock; public SourceGenTriggers(TreeNodeStream input, OutputModelController controller) { this(input); this.controller = controller; @@ -66,7 +67,9 @@ block[GrammarAST label, GrammarAST ebnfRoot] returns [List omos $omos = DefaultOutputModelFactory.list(controller.getChoiceBlock((BlockAST)$blk, alts, $label)); } else { - $omos = DefaultOutputModelFactory.list(controller.getEBNFBlock($ebnfRoot, alts)); + Choice choice = controller.getEBNFBlock($ebnfRoot, alts); + hasLookaheadBlock |= choice instanceof PlusBlock || choice instanceof StarBlock; + $omos = DefaultOutputModelFactory.list(choice); } } ; @@ -132,6 +135,7 @@ subrule returns [List omos] alt.addOp(blk); alts.add(alt); SrcOp loop = controller.getEBNFBlock($op, alts); // "star it" + hasLookaheadBlock |= loop instanceof PlusBlock || loop instanceof StarBlock; $omos = DefaultOutputModelFactory.list(loop); } | block[null, null] {$omos = $block.omos;} diff --git a/tool/src/org/antlr/v4/codegen/model/RuleFunction.java b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java index fad7479be..ff31dbedd 100644 --- a/tool/src/org/antlr/v4/codegen/model/RuleFunction.java +++ b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java @@ -76,6 +76,7 @@ public class RuleFunction extends OutputModelObject { public Collection args = null; public Rule rule; public AltLabelStructDecl[] altToContext; + public boolean hasLookaheadBlock; @ModelElement public List code; @ModelElement public OrderedHashSet locals; // TODO: move into ctx?