protected String getStateLabel(ATNState s) { if ( s==null ) return "null"; String stateLabel = ""; if (s instanceof BlockStartState) { stateLabel += "→\\n"; } else if (s instanceof BlockEndState) { stateLabel += "←\\n"; } stateLabel += String.valueOf(s.stateNumber); if (s instanceof PlusBlockStartState || s instanceof PlusLoopbackState) { stateLabel += "+"; } else if (s instanceof StarBlockStartState || s instanceof StarLoopEntryState || s instanceof StarLoopbackState) { stateLabel += "*"; } if ( s instanceof DecisionState && ((DecisionState)s).decision>=0 ) { stateLabel = stateLabel+"\\nd="+((DecisionState)s).decision; } return stateLabel; }
String getStateString(ATNState s) { int n = s.stateNumber; String stateStr = "s"+n; if ( s instanceof StarBlockStartState ) stateStr = "StarBlockStart_"+n; else if ( s instanceof PlusBlockStartState ) stateStr = "PlusBlockStart_"+n; else if ( s instanceof BlockStartState) stateStr = "BlockStart_"+n; else if ( s instanceof BlockEndState ) stateStr = "BlockEnd_"+n; else if ( s instanceof RuleStartState) stateStr = "RuleStart_"+g.getRule(s.ruleIndex).name+"_"+n; else if ( s instanceof RuleStopState ) stateStr = "RuleStop_"+g.getRule(s.ruleIndex).name+"_"+n; else if ( s instanceof PlusLoopbackState) stateStr = "PlusLoopBack_"+n; else if ( s instanceof StarLoopbackState) stateStr = "StarLoopBack_"+n; else if ( s instanceof StarLoopEntryState) stateStr = "StarLoopEntry_"+n; return stateStr; }
/** * From {@code (blk)+} build * * <pre> * |---------| * v | * [o-blk-o]->o->o * </pre> * * We add a decision for loop back node to the existing one at {@code blk} * start. */ @Override public Handle plus(GrammarAST plusAST, Handle blk) { PlusBlockStartState blkStart = (PlusBlockStartState)blk.left; BlockEndState blkEnd = (BlockEndState)blk.right; preventEpsilonClosureBlocks.add(new Triple<Rule, ATNState, ATNState>(currentRule, blkStart, blkEnd)); PlusLoopbackState loop = newState(PlusLoopbackState.class, plusAST); loop.nonGreedy = !((QuantifierAST)plusAST).isGreedy(); atn.defineDecisionState(loop); LoopEndState end = newState(LoopEndState.class, plusAST); blkStart.loopBackState = loop; end.loopBackState = loop; plusAST.atnState = loop; epsilon(blkEnd, loop); // blk can see loop back BlockAST blkAST = (BlockAST)plusAST.getChild(0); if ( ((QuantifierAST)plusAST).isGreedy() ) { if (expectNonGreedy(blkAST)) { g.tool.errMgr.grammarError(ErrorType.EXPECTED_NON_GREEDY_WILDCARD_BLOCK, g.fileName, plusAST.getToken(), plusAST.getToken().getText()); } epsilon(loop, blkStart); // loop back to start epsilon(loop, end); // or exit } else { // if not greedy, priority to exit branch; make it first epsilon(loop, end); // exit epsilon(loop, blkStart); // loop back to start } return new Handle(blkStart, end); }
public LL1PlusBlockSingleAlt(OutputModelFactory factory, GrammarAST plusRoot, List<CodeBlockForAlt> alts) { super(factory, plusRoot, alts); BlockAST blkAST = (BlockAST)plusRoot.getChild(0); PlusBlockStartState blkStart = (PlusBlockStartState)blkAST.atnState; stateNumber = blkStart.loopBackState.stateNumber; blockStartStateNumber = blkStart.stateNumber; PlusBlockStartState plus = (PlusBlockStartState)blkAST.atnState; this.decision = plus.loopBackState.decision; IntervalSet[] altLookSets = factory.getGrammar().decisionLOOK.get(decision); IntervalSet loopBackLook = altLookSets[0]; loopExpr = addCodeForLoopLookaheadTempVar(loopBackLook); }
public PlusBlock(OutputModelFactory factory, GrammarAST plusRoot, List<CodeBlockForAlt> alts) { super(factory, plusRoot, alts); BlockAST blkAST = (BlockAST)plusRoot.getChild(0); PlusBlockStartState blkStart = (PlusBlockStartState)blkAST.atnState; PlusLoopbackState loop = blkStart.loopBackState; stateNumber = blkStart.loopBackState.stateNumber; blockStartStateNumber = blkStart.stateNumber; loopBackStateNumber = loop.stateNumber; this.error = getThrowNoViableAlt(factory, plusRoot, null); decision = loop.decision; }