Examples of ATNState


Examples of org.antlr.v4.runtime.atn.ATNState

    return new Handle(left, right);
  }

  @Override
  public Handle set(GrammarAST associatedAST, List<GrammarAST> alts, boolean invert) {
    ATNState left = newState(associatedAST);
    ATNState right = newState(associatedAST);
    IntervalSet set = new IntervalSet();
    for (GrammarAST t : alts) {
      if ( t.getType()==ANTLRParser.RANGE ) {
        int a = CharSupport.getCharValueFromGrammarCharLiteral(t.getChild(0).getText());
        int b = CharSupport.getCharValueFromGrammarCharLiteral(t.getChild(1).getText());
View Full Code Here

Examples of org.antlr.v4.runtime.atn.ATNState

  @Override
  public Handle stringLiteral(TerminalAST stringLiteralAST) {
    String chars = stringLiteralAST.getText();
    chars = CharSupport.getStringFromGrammarStringLiteral(chars);
    int n = chars.length();
    ATNState left = newState(stringLiteralAST);
    ATNState prev = left;
    ATNState right = null;
    for (int i=0; i<n; i++) {
      right = newState(stringLiteralAST);
      prev.addTransition(new AtomTransition(right, chars.charAt(i)));
      prev = right;
    }
View Full Code Here

Examples of org.antlr.v4.runtime.atn.ATNState

  }

  /** [Aa\t \u1234a-z\]\-] char sets */
  @Override
  public Handle charSetLiteral(GrammarAST charSetAST) {
    ATNState left = newState(charSetAST);
    ATNState right = newState(charSetAST);
    IntervalSet set = getSetFromCharSetLiteral(charSetAST);
    left.addTransition(new SetTransition(right, set));
    charSetAST.atnState = left;
    return new Handle(left, right);
  }
View Full Code Here

Examples of org.antlr.v4.runtime.atn.ATNState

  @Override
  public Handle tokenRef(TerminalAST node) {
    // Ref to EOF in lexer yields char transition on -1
    if ( node.getText().equals("EOF") ) {
      ATNState left = newState(node);
      ATNState right = newState(node);
      left.addTransition(new AtomTransition(right, IntStream.EOF));
      return new Handle(left, right);
    }
    return _ruleRef(node);
  }
View Full Code Here

Examples of org.antlr.v4.runtime.atn.ATNState

  }

  @Override
  public void visitState(@NotNull ATNState p) {
    if (p.getStateType() == ATNState.BASIC && p.getNumberOfTransitions() == 1) {
      ATNState q = p.transition(0).target;
      if (p.transition(0) instanceof RuleTransition) {
        q = ((RuleTransition) p.transition(0)).followState;
      }
      if (q.getStateType() == ATNState.BASIC) {
        // we have p-x->q for x in {rule, action, pred, token, ...}
        // if edge out of q is single epsilon to block end
        // we can strip epsilon p-x->q-eps->r
        Transition trans = q.transition(0);
        if (q.getNumberOfTransitions() == 1 && trans instanceof EpsilonTransition) {
          ATNState r = trans.target;
          if (r instanceof BlockEndState || r instanceof PlusLoopbackState || r instanceof StarLoopbackState) {
            // skip over q
            if (p.transition(0) instanceof RuleTransition) {
              ((RuleTransition) p.transition(0)).followState = r;
            } else {
View Full Code Here

Examples of org.antlr.v4.runtime.atn.ATNState

  }

  protected void checkLexerMatches(LexerGrammar lg, String inputString, String expecting) {
    ATN atn = createATN(lg, true);
    CharStream input = new ANTLRInputStream(inputString);
    ATNState startState = atn.modeNameToStartState.get("DEFAULT_MODE");
    DOTGenerator dot = new DOTGenerator(lg);
    System.out.println(dot.getDOT(startState, true));

    List<String> tokenTypes = getTokenTypes(lg, atn, input);
View Full Code Here

Examples of org.antlr.v4.runtime.atn.ATNState

    throws Exception
  {
    ErrorQueue equeue = new ErrorQueue();
    Grammar g = new Grammar(gtext, equeue);
    ATN atn = createATN(g, false);
    ATNState s = atn.ruleToStartState[g.getRule(ruleName).index];
    if ( s==null ) {
      System.err.println("no such rule: "+ruleName);
      return null;
    }
    ATNState t = s.transition(0).target;
    if ( !(t instanceof DecisionState) ) {
      System.out.println(ruleName+" has no decision");
      return null;
    }
    DecisionState blk = (DecisionState)t;
View Full Code Here

Examples of org.antlr.v4.runtime.atn.ATNState

  void checkRuleATN(Grammar g, String ruleName, String expecting) {
    DOTGenerator dot = new DOTGenerator(g);
    System.out.println(dot.getDOT(g.atn.ruleToStartState[g.getRule(ruleName).index]));

    Rule r = g.getRule(ruleName);
    ATNState startState = g.atn.ruleToStartState[r.index];
    ATNPrinter serializer = new ATNPrinter(g, startState);
    String result = serializer.asString();

    //System.out.print(result);
    assertEquals(expecting, result);
View Full Code Here

Examples of org.antlr.v4.runtime.atn.ATNState

    work = new ArrayList<ATNState>();
    work.add(start);

    StringBuilder buf = new StringBuilder();
    ATNState s;

    while ( !work.isEmpty() ) {
      s = work.remove(0);
      if ( marked.contains(s) ) continue;
      int n = s.getNumberOfTransitions();
//      System.out.println("visit "+s+"; edges="+n);
      marked.add(s);
      for (int i=0; i<n; i++) {
        Transition t = s.transition(i);
        if ( !(s instanceof RuleStopState) ) { // don't add follow states to work
          if ( t instanceof RuleTransition ) work.add(((RuleTransition)t).followState);
          else work.add( t.target );
        }
        buf.append(getStateString(s));
View Full Code Here

Examples of org.antlr.v4.runtime.atn.ATNState

    List<ATNState> work = new LinkedList<ATNState>();

    work.add(startState);
    while ( !work.isEmpty() ) {
      ATNState s = work.get(0);
      if ( markedStates.contains(s) ) { work.remove(0); continue; }
      markedStates.add(s);

      // don't go past end of rule node to the follow states
      if ( s instanceof RuleStopState) continue;

      // special case: if decision point, then line up the alt start states
      // unless it's an end of block
//      if ( s instanceof BlockStartState ) {
//        ST rankST = stlib.getInstanceOf("decision-rank");
//        DecisionState alt = (DecisionState)s;
//        for (int i=0; i<alt.getNumberOfTransitions(); i++) {
//          ATNState target = alt.transition(i).target;
//          if ( target!=null ) {
//            rankST.add("states", target.stateNumber);
//          }
//        }
//        dot.add("decisionRanks", rankST);
//      }

      // make a DOT edge for each transition
      ST edgeST;
      for (int i = 0; i < s.getNumberOfTransitions(); i++) {
        Transition edge = s.transition(i);
        if ( edge instanceof RuleTransition ) {
          RuleTransition rr = ((RuleTransition)edge);
          // don't jump to other rules, but display edge to follow node
          edgeST = stlib.getInstanceOf("edge");

          String label = "<" + ruleNames[rr.ruleIndex];
          if (((RuleStartState)rr.target).isPrecedenceRule) {
            label += "[" + rr.precedence + "]";
          }
          label += ">";

          edgeST.add("label", label);
          edgeST.add("src", "s"+s.stateNumber);
          edgeST.add("target", "s"+rr.followState.stateNumber);
          edgeST.add("arrowhead", arrowhead);
          dot.add("edges", edgeST);
          work.add(rr.followState);
          continue;
        }
        if ( edge instanceof ActionTransition) {
          edgeST = stlib.getInstanceOf("action-edge");
          edgeST.add("label", getEdgeLabel(edge.toString()));
        }
        else if ( edge instanceof AbstractPredicateTransition ) {
          edgeST = stlib.getInstanceOf("edge");
          edgeST.add("label", getEdgeLabel(edge.toString()));
        }
        else if ( edge.isEpsilon() ) {
          edgeST = stlib.getInstanceOf("epsilon-edge");
          edgeST.add("label", getEdgeLabel(edge.toString()));
          boolean loopback = false;
          if (edge.target instanceof PlusBlockStartState) {
            loopback = s.equals(((PlusBlockStartState)edge.target).loopBackState);
          }
          else if (edge.target instanceof StarLoopEntryState) {
            loopback = s.equals(((StarLoopEntryState)edge.target).loopBackState);
          }
          edgeST.add("loopback", loopback);
        }
        else if ( edge instanceof AtomTransition ) {
          edgeST = stlib.getInstanceOf("edge");
          AtomTransition atom = (AtomTransition)edge;
          String label = String.valueOf(atom.label);
          if ( isLexer ) label = "'"+getEdgeLabel(String.valueOf((char)atom.label))+"'";
          else if ( grammar!=null ) label = grammar.getTokenDisplayName(atom.label);
          edgeST.add("label", getEdgeLabel(label));
        }
        else if ( edge instanceof SetTransition ) {
          edgeST = stlib.getInstanceOf("edge");
          SetTransition set = (SetTransition)edge;
          String label = set.label().toString();
          if ( isLexer ) label = set.label().toString(true);
          else if ( grammar!=null ) label = set.label().toString(grammar.getVocabulary());
          if ( edge instanceof NotSetTransition ) label = "~"+label;
          edgeST.add("label", getEdgeLabel(label));
        }
        else if ( edge instanceof RangeTransition ) {
          edgeST = stlib.getInstanceOf("edge");
          RangeTransition range = (RangeTransition)edge;
          String label = range.label().toString();
          if ( isLexer ) label = range.toString();
          else if ( grammar!=null ) label = range.label().toString(grammar.getVocabulary());
          edgeST.add("label", getEdgeLabel(label));
        }
        else {
          edgeST = stlib.getInstanceOf("edge");
          edgeST.add("label", getEdgeLabel(edge.toString()));
        }
        edgeST.add("src", "s"+s.stateNumber);
        edgeST.add("target", "s"+edge.target.stateNumber);
        edgeST.add("arrowhead", arrowhead);
        if (s.getNumberOfTransitions() > 1) {
          edgeST.add("transitionIndex", i);
        } else {
          edgeST.add("transitionIndex", false);
        }
        dot.add("edges", edgeST);
        work.add(edge.target);
      }
    }

    // define nodes we visited (they will appear first in DOT output)
    // this is an example of ST's lazy eval :)
    // define stop state first; seems to be a bug in DOT where doublecircle
    // shape only works if we define them first. weird.
//    ATNState stopState = startState.atn.ruleToStopState.get(startState.rule);
//    if ( stopState!=null ) {
//      ST st = stlib.getInstanceOf("stopstate");
//      st.add("name", "s"+stopState.stateNumber);
//      st.add("label", getStateLabel(stopState));
//      dot.add("states", st);
//    }
    for (ATNState s : markedStates) {
      if ( !(s instanceof RuleStopState) ) continue;
      ST st = stlib.getInstanceOf("stopstate");
      st.add("name", "s"+s.stateNumber);
      st.add("label", getStateLabel(s));
      dot.add("states", st);
    }

    for (ATNState s : markedStates) {
      if ( s instanceof RuleStopState ) continue;
      ST st = stlib.getInstanceOf("state");
      st.add("name", "s"+s.stateNumber);
      st.add("label", getStateLabel(s));
      st.add("transitions", s.getTransitions());
      dot.add("states", st);
    }

    return dot.render();
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.