Package org.antlr.v4.runtime.atn

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


      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));
        if ( t instanceof EpsilonTransition ) {
          buf.append("->").append(getStateString(t.target)).append('\n');
        }
        else if ( t instanceof RuleTransition ) {
          buf.append("-").append(g.getRule(((RuleTransition)t).ruleIndex).name).append("->").append(getStateString(t.target)).append('\n');
        }
        else if ( t instanceof ActionTransition ) {
          ActionTransition a = (ActionTransition)t;
          buf.append("-").append(a.toString()).append("->").append(getStateString(t.target)).append('\n');
        }
        else if ( t instanceof SetTransition ) {
          SetTransition st = (SetTransition)t;
          boolean not = st instanceof NotSetTransition;
          if ( g.isLexer() ) {
            buf.append("-").append(not?"~":"").append(st.toString()).append("->").append(getStateString(t.target)).append('\n');
          }
          else {
            buf.append("-").append(not?"~":"").append(st.label().toString(g.getVocabulary())).append("->").append(getStateString(t.target)).append('\n');
          }
        }
        else if ( t instanceof AtomTransition ) {
          AtomTransition a = (AtomTransition)t;
          String label = g.getTokenDisplayName(a.label);
          buf.append("-").append(label).append("->").append(getStateString(t.target)).append('\n');
        }
        else {
          buf.append("-").append(t.toString()).append("->").append(getStateString(t.target)).append('\n');
        }
      }
    }
    return buf.toString();
  }
View Full Code Here


//      }

      // 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) {
View Full Code Here

        }
      }

      IntervalSet setTransitions = new IntervalSet();
      for (int i = 0; i < decision.getNumberOfTransitions(); i++) {
        Transition epsTransition = decision.transition(i);
        if (!(epsTransition instanceof EpsilonTransition)) {
          continue;
        }

        if (epsTransition.target.getNumberOfTransitions() != 1) {
          continue;
        }

        Transition transition = epsTransition.target.transition(0);
        if (!(transition.target instanceof BlockEndState)) {
          continue;
        }

        if (transition instanceof NotSetTransition) {
          // TODO: not yet implemented
          continue;
        }

        if (transition instanceof AtomTransition
          || transition instanceof RangeTransition
          || transition instanceof SetTransition)
        {
          setTransitions.add(i);
        }
      }

      // due to min alt resolution policies, can only collapse sequential alts
      for (int i = setTransitions.getIntervals().size() - 1; i >= 0; i--) {
        Interval interval = setTransitions.getIntervals().get(i);
        if (interval.length() <= 1) {
          continue;
        }

        ATNState blockEndState = decision.transition(interval.a).target.transition(0).target;
        IntervalSet matchSet = new IntervalSet();
        for (int j = interval.a; j <= interval.b; j++) {
          Transition matchTransition = decision.transition(j).target.transition(0);
          if (matchTransition instanceof NotSetTransition) {
            throw new UnsupportedOperationException("Not yet implemented.");
          } else {
            matchSet.addAll(matchTransition.label());
          }
        }

        Transition newTransition;
        if (matchSet.getIntervals().size() == 1) {
          if (matchSet.size() == 1) {
            newTransition = new AtomTransition(blockEndState, matchSet.getMinElement());
          } else {
            Interval matchInterval = matchSet.getIntervals().get(0);
            newTransition = new RangeTransition(blockEndState, matchInterval.a, matchInterval.b);
          }
        } else {
          newTransition = new SetTransition(blockEndState, matchSet);
        }

        decision.transition(interval.a).target.setTransition(0, newTransition);
        for (int j = interval.a + 1; j <= interval.b; j++) {
          Transition removed = decision.removeTransition(interval.a + 1);
          atn.removeState(removed.target);
          removedStates++;
        }
      }
    }
View Full Code Here

  @NotNull
  @Override
  public Handle wildcard(@NotNull GrammarAST node) {
    ATNState left = newState(node);
    ATNState right = newState(node);
    left.addTransition(new WildcardTransition(right));
    node.atnState = left;
    return new Handle(left, right);
  }
View Full Code Here

    // ignore tokens from existing option subtrees like:
    //    (ELEMENT_OPTIONS (= assoc right))
    //
    // element options are added back according to the values in the map
    // returned by getOptions().
    IntervalSet ignore = new IntervalSet();
    List<GrammarAST> optionsSubTrees = t.getNodesWithType(ELEMENT_OPTIONS);
    for (GrammarAST sub : optionsSubTrees) {
      ignore.add(sub.getTokenStartIndex(), sub.getTokenStopIndex());
    }

    // Individual labels appear as RULE_REF or TOKEN_REF tokens in the tree,
    // but do not support the ELEMENT_OPTIONS syntax. Make sure to not try
    // and add the tokenIndex option when writing these tokens.
    IntervalSet noOptions = new IntervalSet();
    List<GrammarAST> labeledSubTrees = t.getNodesWithType(new IntervalSet(ASSIGN,PLUS_ASSIGN));
    for (GrammarAST sub : labeledSubTrees) {
      noOptions.add(sub.getChild(0).getTokenStartIndex());
    }

    StringBuilder buf = new StringBuilder();
    for (int i=tokenStartIndex; i<=tokenStopIndex; i++) {
      if ( ignore.contains(i) ) {
        continue;
      }

      Token tok = tokenStream.get(i);

      StringBuilder elementOptions = new StringBuilder();
      if (!noOptions.contains(i)) {
        GrammarAST node = t.getNodeWithTokenIndex(tok.getTokenIndex());
        if ( node!=null &&
           (tok.getType()==TOKEN_REF ||
            tok.getType()==STRING_LITERAL ||
            tok.getType()==RULE_REF) )
View Full Code Here

  @NotNull
  @Override
  public Handle set(@NotNull GrammarAST associatedAST, @NotNull List<GrammarAST> terminals, boolean invert) {
    ATNState left = newState(associatedAST);
    ATNState right = newState(associatedAST);
    IntervalSet set = new IntervalSet();
    for (GrammarAST t : terminals) {
      int ttype = g.getTokenType(t.getText());
      set.add(ttype);
    }
    if ( invert ) {
      left.addTransition(new NotSetTransition(right, set));
    }
    else {
View Full Code Here

          }
        }
      }
      if(arg0.getParent() instanceof ExpressionContext) {
        // we are the leftmost child of the expression
        ParseTree chld = arg0.getParent().getChild(arg0.getParent().getChildCount()-1);
        if(!chld.equals(arg0)) return;
        addQuery(classUtils.expandExpression(arg0.getParent().getText(), registry));
      }
    }
  }
View Full Code Here

//    parser.setTokenFactory(factory);
    parser.addErrorListener(new DiagnosticErrorListener());
    parser.getInterpreter().setPredictionMode(
        PredictionMode.LL_EXACT_AMBIG_DETECTION);
    parser.setBuildParseTree(true);
    ParseTree tree = parser.mson();
    // show tree in text form
    // System.out.println(tree.toStringTree(parser));

    ParseTreeWalker walker = new ParseTreeWalker();
    SymbolTable symtab = new SymbolTable();
View Full Code Here

    JavaParser parser = new JavaParser(tokens);
    parser.removeErrorListeners();
   
    // start parsing at the compilationUnit rule
    ParserRuleContext t = parser.compilationUnit();
    ParseTreeWalker walker = new ParseTreeWalker();
    List<AutocompleteCandidate> q = new ArrayList<AutocompleteCandidate>();
         
    ImportDeclarationCompletion extractor = new ImportDeclarationCompletion(txt,cur,registry,cps,cu);
    NameBuilder extractor2 = new NameBuilder(registry,cu );
    NodeCompletion extractor3 = new NodeCompletion(txt,cur, registry, cu);
    walker.walk(extractor, t);
    if(extractor.getQuery()!=null)
      q.addAll(extractor.getQuery());
    walker.walk(extractor2, t);
    walker.walk(extractor3, t);
    if(extractor3.getQuery()!=null)
      q.addAll(extractor3.getQuery());
    List<String> ret = registry.searchCandidates(q);

    // this shows the GUI
View Full Code Here

    parser.setBuildParseTree(true);
    ParseTree tree = parser.mson();
    // show tree in text form
    // System.out.println(tree.toStringTree(parser));

    ParseTreeWalker walker = new ParseTreeWalker();
    SymbolTable symtab = new SymbolTable();
    DefPhase def = new DefPhase(symtab);
    walker.walk(def, tree);
    // create next phase and feed symbol table info from def to ref phase
    RefPhase ref = new RefPhase(symtab, def.scopes);
    walker.walk(ref, tree);
  }
View Full Code Here

TOP

Related Classes of org.antlr.v4.runtime.atn.Transition

Copyright © 2018 www.massapicom. 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.