From b879d508eeac02d92f12068dff96de4ec84ccca4 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 28 Oct 2012 16:45:45 -0500 Subject: [PATCH] Fix generated alt numbers for non-greedy quantifiers --- .../org/antlr/v4/tool/templates/codegen/Java/Java.stg | 6 +++--- tool/src/org/antlr/v4/codegen/model/Loop.java | 5 ++++- tool/src/org/antlr/v4/codegen/model/PlusBlock.java | 4 ++-- tool/src/org/antlr/v4/codegen/model/StarBlock.java | 1 - 4 files changed, 9 insertions(+), 7 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 1d105d4de..167775201 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 @@ -402,7 +402,7 @@ setState(); //_errHandler.sync(this); switch ( getInterpreter().adaptivePredict(_input,,_ctx) ) { : +case +1: break;}; separator="\n"> } @@ -413,7 +413,7 @@ setState(); _errHandler.sync(this); _alt = getInterpreter().adaptivePredict(_input,,_ctx); while ( _alt!= && _alt!=-1 ) { - if ( _alt==1 ) { + if ( _alt==1+1 ) { } @@ -430,7 +430,7 @@ _alt = getInterpreter().adaptivePredict(_input,,_ctx); do { switch ( _alt ) { : +case +1: break;}; separator="\n"> default : diff --git a/tool/src/org/antlr/v4/codegen/model/Loop.java b/tool/src/org/antlr/v4/codegen/model/Loop.java index 95335999c..11a1f2430 100644 --- a/tool/src/org/antlr/v4/codegen/model/Loop.java +++ b/tool/src/org/antlr/v4/codegen/model/Loop.java @@ -31,6 +31,7 @@ package org.antlr.v4.codegen.model; import org.antlr.v4.codegen.OutputModelFactory; import org.antlr.v4.tool.ast.GrammarAST; +import org.antlr.v4.tool.ast.QuantifierAST; import java.util.ArrayList; import java.util.List; @@ -38,7 +39,7 @@ import java.util.List; public class Loop extends Choice { public int blockStartStateNumber; public int loopBackStateNumber; - public int exitAlt; + public final int exitAlt; @ModelElement public List iteration; @@ -47,6 +48,8 @@ public class Loop extends Choice { List alts) { super(factory, blkOrEbnfRootAST, alts); + boolean nongreedy = (blkOrEbnfRootAST instanceof QuantifierAST) && !((QuantifierAST)blkOrEbnfRootAST).isGreedy(); + exitAlt = nongreedy ? 1 : alts.size() + 1; } public void addIterationOp(SrcOp op) { diff --git a/tool/src/org/antlr/v4/codegen/model/PlusBlock.java b/tool/src/org/antlr/v4/codegen/model/PlusBlock.java index 10b2e8fd1..73af26517 100644 --- a/tool/src/org/antlr/v4/codegen/model/PlusBlock.java +++ b/tool/src/org/antlr/v4/codegen/model/PlusBlock.java @@ -30,7 +30,8 @@ package org.antlr.v4.codegen.model; import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.atn.PlusBlockStartState; +import org.antlr.v4.runtime.atn.PlusLoopbackState; import org.antlr.v4.tool.ast.GrammarAST; import java.util.List; @@ -51,6 +52,5 @@ public class PlusBlock extends Loop { loopBackStateNumber = loop.stateNumber; this.error = getThrowNoViableAlt(factory, plusRoot, null); decision = loop.decision; - exitAlt = alts.size()+1; } } diff --git a/tool/src/org/antlr/v4/codegen/model/StarBlock.java b/tool/src/org/antlr/v4/codegen/model/StarBlock.java index a7408afd7..8d8bd8405 100644 --- a/tool/src/org/antlr/v4/codegen/model/StarBlock.java +++ b/tool/src/org/antlr/v4/codegen/model/StarBlock.java @@ -47,6 +47,5 @@ public class StarBlock extends Loop { StarLoopEntryState star = (StarLoopEntryState)blkOrEbnfRootAST.atnState; loopBackStateNumber = star.loopBackState.stateNumber; decision = star.decision; - exitAlt = alts.size()+1; } }