Package org.antlr.v4.runtime.dfa

Examples of org.antlr.v4.runtime.dfa.DFAState


      // allow zero-length tokens
      captureSimState(prevAccept, input, ds0);
    }

    int t = input.LA(1);
    @NotNull
    DFAState s = ds0; // s is current/from DFA state

    while ( true ) { // while more work
      if ( debug ) {
        System.out.format(Locale.getDefault(), "execATN loop starting closure: %s\n", s.configs);
      }

      // As we move src->trg, src->trg, we keep track of the previous trg to
      // avoid looking up the DFA state again, which is expensive.
      // If the previous target was already part of the DFA, we might
      // be able to avoid doing a reach operation upon t. If s!=null,
      // it means that semantic predicates didn't prevent us from
      // creating a DFA state. Once we know s!=null, we check to see if
      // the DFA state has an edge already for t. If so, we can just reuse
      // it's configuration set; there's no point in re-computing it.
      // This is kind of like doing DFA simulation within the ATN
      // simulation because DFA simulation is really just a way to avoid
      // computing reach/closure sets. Technically, once we know that
      // we have a previously added DFA state, we could jump over to
      // the DFA simulator. But, that would mean popping back and forth
      // a lot and making things more complicated algorithmically.
      // This optimization makes a lot of sense for loops within DFA.
      // A character will take us back to an existing DFA state
      // that already has lots of edges out of it. e.g., .* in comments.
      DFAState target = getExistingTargetState(s, t);
      if (target == null) {
        target = computeTargetState(input, s, t);
      }

      if (target == ERROR) {
View Full Code Here


  protected DFAState getExistingTargetState(@NotNull DFAState s, int t) {
    if (s.edges == null || t < MIN_DFA_EDGE || t > MAX_DFA_EDGE) {
      return null;
    }

    DFAState target = s.edges[t - MIN_DFA_EDGE];
    if (debug && target != null) {
      System.out.println("reuse state "+s.stateNumber+
                 " edge to "+target.stateNumber);
    }
View Full Code Here

     * state, we can continue in pure DFA mode from there.
     */
    boolean suppressEdge = q.hasSemanticContext;
    q.hasSemanticContext = false;

    @NotNull
    DFAState to = addDFAState(q);

    if (suppressEdge) {
      return to;
    }
View Full Code Here

    /* the lexer evaluates predicates on-the-fly; by this point configs
     * should not contain any configurations with unevaluated predicates.
     */
    assert !configs.hasSemanticContext;

    DFAState proposed = new DFAState(configs);
    ATNConfig firstConfigWithRuleStopState = null;
    for (ATNConfig c : configs) {
      if ( c.state instanceof RuleStopState )  {
        firstConfigWithRuleStopState = c;
        break;
      }
    }

    if ( firstConfigWithRuleStopState!=null ) {
      proposed.isAcceptState = true;
      proposed.lexerActionExecutor = ((LexerATNConfig)firstConfigWithRuleStopState).getLexerActionExecutor();
      proposed.prediction = atn.ruleToTokenType[firstConfigWithRuleStopState.state.ruleIndex];
    }

    DFA dfa = decisionToDFA[mode];
    synchronized (dfa.states) {
      DFAState existing = dfa.states.get(proposed);
      if ( existing!=null ) return existing;

      DFAState newState = proposed;

      newState.stateNumber = dfa.states.size();
      configs.setReadonly(true);
      newState.configs = configs;
      dfa.states.put(newState, newState);
View Full Code Here

    }

    for (DFAState d : dfa.states.keySet()) {
      if ( d.edges!=null ) {
        for (int i = 0; i < d.edges.length; i++) {
          DFAState target = d.edges[i];
          if ( target==null) continue;
          if ( target.stateNumber == Integer.MAX_VALUE ) continue;
          int ttype = i-1; // we shift up for EOF as -1 for parser
          String label = String.valueOf(ttype);
          if ( isLexer ) label = "'"+getEdgeLabel(String.valueOf((char) i))+"'";
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

TOP

Related Classes of org.antlr.v4.runtime.dfa.DFAState

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.