Fix generated alt numbers for non-greedy quantifiers

This commit is contained in:
Sam Harwell 2012-10-28 16:45:45 -05:00
parent 4d2df41cdb
commit b879d508ee
4 changed files with 9 additions and 7 deletions

View File

@ -402,7 +402,7 @@ setState(<choice.stateNumber>);
//_errHandler.sync(this); //_errHandler.sync(this);
switch ( getInterpreter().adaptivePredict(_input,<choice.decision>,_ctx) ) { switch ( getInterpreter().adaptivePredict(_input,<choice.decision>,_ctx) ) {
<alts:{alt | <alts:{alt |
case <i>: case <i><if(!choice.ast.greedy)>+1<endif>:
<alt> <alt>
break;}; separator="\n"> break;}; separator="\n">
} }
@ -413,7 +413,7 @@ setState(<choice.stateNumber>);
_errHandler.sync(this); _errHandler.sync(this);
_alt = getInterpreter().adaptivePredict(_input,<choice.decision>,_ctx); _alt = getInterpreter().adaptivePredict(_input,<choice.decision>,_ctx);
while ( _alt!=<choice.exitAlt> && _alt!=-1 ) { while ( _alt!=<choice.exitAlt> && _alt!=-1 ) {
if ( _alt==1 ) { if ( _alt==1<if(!choice.ast.greedy)>+1<endif> ) {
<iteration> <iteration>
<alts> <! should only be one !> <alts> <! should only be one !>
} }
@ -430,7 +430,7 @@ _alt = getInterpreter().adaptivePredict(_input,<choice.decision>,_ctx);
do { do {
switch ( _alt ) { switch ( _alt ) {
<alts:{alt| <alts:{alt|
case <i>: case <i><if(!choice.ast.greedy)>+1<endif>:
<alt> <alt>
break;}; separator="\n"> break;}; separator="\n">
default : default :

View File

@ -31,6 +31,7 @@ package org.antlr.v4.codegen.model;
import org.antlr.v4.codegen.OutputModelFactory; import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.tool.ast.GrammarAST; import org.antlr.v4.tool.ast.GrammarAST;
import org.antlr.v4.tool.ast.QuantifierAST;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -38,7 +39,7 @@ import java.util.List;
public class Loop extends Choice { public class Loop extends Choice {
public int blockStartStateNumber; public int blockStartStateNumber;
public int loopBackStateNumber; public int loopBackStateNumber;
public int exitAlt; public final int exitAlt;
@ModelElement public List<SrcOp> iteration; @ModelElement public List<SrcOp> iteration;
@ -47,6 +48,8 @@ public class Loop extends Choice {
List<CodeBlockForAlt> alts) List<CodeBlockForAlt> alts)
{ {
super(factory, blkOrEbnfRootAST, alts); super(factory, blkOrEbnfRootAST, alts);
boolean nongreedy = (blkOrEbnfRootAST instanceof QuantifierAST) && !((QuantifierAST)blkOrEbnfRootAST).isGreedy();
exitAlt = nongreedy ? 1 : alts.size() + 1;
} }
public void addIterationOp(SrcOp op) { public void addIterationOp(SrcOp op) {

View File

@ -30,7 +30,8 @@
package org.antlr.v4.codegen.model; package org.antlr.v4.codegen.model;
import org.antlr.v4.codegen.OutputModelFactory; 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 org.antlr.v4.tool.ast.GrammarAST;
import java.util.List; import java.util.List;
@ -51,6 +52,5 @@ public class PlusBlock extends Loop {
loopBackStateNumber = loop.stateNumber; loopBackStateNumber = loop.stateNumber;
this.error = getThrowNoViableAlt(factory, plusRoot, null); this.error = getThrowNoViableAlt(factory, plusRoot, null);
decision = loop.decision; decision = loop.decision;
exitAlt = alts.size()+1;
} }
} }

View File

@ -47,6 +47,5 @@ public class StarBlock extends Loop {
StarLoopEntryState star = (StarLoopEntryState)blkOrEbnfRootAST.atnState; StarLoopEntryState star = (StarLoopEntryState)blkOrEbnfRootAST.atnState;
loopBackStateNumber = star.loopBackState.stateNumber; loopBackStateNumber = star.loopBackState.stateNumber;
decision = star.decision; decision = star.decision;
exitAlt = alts.size()+1;
} }
} }