Package org.antlr.v4.runtime.misc

Examples of org.antlr.v4.runtime.misc.IntegerList


    if ( grammar!=null ) {
      Set<Integer> alts = s.getAltSet();
      if ( alts!=null ) {
        buf.append("\\n");
        // separate alts
        IntegerList altList = new IntegerList();
        altList.addAll(alts);
        altList.sort();
        Set<ATNConfig> configurations = s.configs;
        for (int altIndex = 0; altIndex < altList.size(); altIndex++) {
          int alt = altList.get(altIndex);
          if ( altIndex>0 ) {
            buf.append("\\n");
          }
          buf.append("alt");
          buf.append(alt);
View Full Code Here


                String inputString,
                int expected)
  {
    ATN lexatn = createATN(lg, true);
    LexerATNSimulator lexInterp = new LexerATNSimulator(lexatn,new DFA[] { new DFA(lexatn.modeToStartState.get(Lexer.DEFAULT_MODE)) },null);
    IntegerList types = getTokenTypesViaATN(inputString, lexInterp);
    System.out.println(types);

    g.importVocab(lg);

    ParserATNFactory f = new ParserATNFactory(g);
View Full Code Here

   *      decision-0-start-state, decision-1-start-state, ...
   *
   *  Convenient to pack into unsigned shorts to make as Java string.
   */
  public IntegerList serialize() {
    IntegerList data = new IntegerList();
    data.add(ATNDeserializer.SERIALIZED_VERSION);
    serializeUUID(data, ATNDeserializer.SERIALIZED_UUID);

    // convert grammar type to ATN const to avoid dependence on ANTLRParser
    data.add(atn.grammarType.ordinal());
    data.add(atn.maxTokenType);
    int nedges = 0;

    Map<IntervalSet, Integer> setIndices = new HashMap<IntervalSet, Integer>();
    List<IntervalSet> sets = new ArrayList<IntervalSet>();

    // dump states, count edges and collect sets while doing so
    IntegerList nonGreedyStates = new IntegerList();
    IntegerList precedenceStates = new IntegerList();
    data.add(atn.states.size());
    for (ATNState s : atn.states) {
      if ( s==null ) { // might be optimized away
        data.add(ATNState.INVALID_TYPE);
        continue;
      }

      int stateType = s.getStateType();
      if (s instanceof DecisionState && ((DecisionState)s).nonGreedy) {
        nonGreedyStates.add(s.stateNumber);
      }

      if (s instanceof RuleStartState && ((RuleStartState)s).isPrecedenceRule) {
        precedenceStates.add(s.stateNumber);
      }

      data.add(stateType);

      if (s.ruleIndex == -1) {
        data.add(Character.MAX_VALUE);
      }
      else {
        data.add(s.ruleIndex);
      }

      if ( s.getStateType() == ATNState.LOOP_END ) {
        data.add(((LoopEndState)s).loopBackState.stateNumber);
      }
      else if ( s instanceof BlockStartState ) {
        data.add(((BlockStartState)s).endState.stateNumber);
      }

      if (s.getStateType() != ATNState.RULE_STOP) {
        // the deserializer can trivially derive these edges, so there's no need to serialize them
        nedges += s.getNumberOfTransitions();
      }

      for (int i=0; i<s.getNumberOfTransitions(); i++) {
        Transition t = s.transition(i);
        int edgeType = Transition.serializationTypes.get(t.getClass());
        if ( edgeType == Transition.SET || edgeType == Transition.NOT_SET ) {
          SetTransition st = (SetTransition)t;
          if (!setIndices.containsKey(st.set)) {
            sets.add(st.set);
            setIndices.put(st.set, sets.size() - 1);
          }
        }
      }
    }

    // non-greedy states
    data.add(nonGreedyStates.size());
    for (int i = 0; i < nonGreedyStates.size(); i++) {
      data.add(nonGreedyStates.get(i));
    }

    // precedence states
    data.add(precedenceStates.size());
    for (int i = 0; i < precedenceStates.size(); i++) {
      data.add(precedenceStates.get(i));
    }

    int nrules = atn.ruleToStartState.length;
    data.add(nrules);
    for (int r=0; r<nrules; r++) {
View Full Code Here

  public static char[] getSerializedAsChars(ATN atn) {
    return Utils.toCharArray(getSerialized(atn));
  }

  public static String getDecoded(ATN atn, List<String> tokenNames) {
    IntegerList serialized = getSerialized(atn);
    char[] data = Utils.toCharArray(serialized);
    return new ATNSerializer(atn, tokenNames).decode(data);
  }
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.misc.IntegerList

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.