Package org.antlr.v4.runtime.misc

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


    public boolean isExpectedToken(int symbol) {
//       return getInterpreter().atn.nextTokens(_ctx);
        ATN atn = getInterpreter().atn;
    ParserRuleContext ctx = _ctx;
        ATNState s = atn.states.get(getState());
        IntervalSet following = atn.nextTokens(s);
        if (following.contains(symbol)) {
            return true;
        }
//        System.out.println("following "+s+"="+following);
        if ( !following.contains(Token.EPSILON) ) return false;

        while ( ctx!=null && ctx.invokingState>=0 && following.contains(Token.EPSILON) ) {
            ATNState invokingState = atn.states.get(ctx.invokingState);
            RuleTransition rt = (RuleTransition)invokingState.transition(0);
            following = atn.nextTokens(rt.followState);
            if (following.contains(symbol)) {
                return true;
            }

            ctx = (ParserRuleContext)ctx.parent;
        }

        if ( following.contains(Token.EPSILON) && symbol == Token.EOF ) {
            return true;
        }

        return false;
    }
View Full Code Here


  @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());
        set.add(a, b);
      }
      else if ( t.getType()==ANTLRParser.LEXER_CHAR_SET ) {
        set.addAll(getSetFromCharSetLiteral(t));
      }
      else if ( t.getType()==ANTLRParser.STRING_LITERAL ) {
        int c = CharSupport.getCharValueFromGrammarCharLiteral(t.getText());
        if ( c != -1 ) {
          set.add(c);
        }
        else {
          g.tool.errMgr.grammarError(ErrorType.INVALID_LITERAL_IN_LEXER_SET,
                         g.fileName, t.getToken(), t.getText());

        }
      }
      else if ( t.getType()==ANTLRParser.TOKEN_REF ) {
        g.tool.errMgr.grammarError(ErrorType.UNSUPPORTED_REFERENCE_IN_LEXER_SET,
                       g.fileName, t.getToken(), t.getText());
      }
    }
    if ( invert ) {
      left.addTransition(new NotSetTransition(right, set));
    }
    else {
      Transition transition;
      if (set.getIntervals().size() == 1) {
        Interval interval = set.getIntervals().get(0);
        transition = new RangeTransition(right, interval.a, interval.b);
      } else {
        transition = new SetTransition(right, set);
      }
View Full Code Here

  /** [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

  public IntervalSet getSetFromCharSetLiteral(GrammarAST charSetAST) {
    String chars = charSetAST.getText();
    chars = chars.substring(1, chars.length()-1);
    String cset = '"'+ chars +'"';
    IntervalSet set = new IntervalSet();

    // unescape all valid escape char like \n, leaving escaped dashes as '\-'
    // so we can avoid seeing them as '-' range ops.
    chars = CharSupport.getStringFromGrammarStringLiteral(cset);
    // now make x-y become set of char
    int n = chars.length();
    for (int i=0; i< n; i++) {
      int c = chars.charAt(i);
      if ( c=='\\' && (i+1)<n && chars.charAt(i+1)=='-' ) { // \-
        set.add('-');
        i++;
      }
      else if ( (i+2)<n && chars.charAt(i+1)=='-' ) { // range x-y
        int x = c;
        int y = chars.charAt(i+2);
        if ( x<=y ) set.add(x,y);
        i+=2;
      }
      else {
        set.add(c);
      }
    }
    return set;
  }
View Full Code Here

  public void process() {
    if ( !CodeGenerator.targetExists(g.getOptionString("language")) ) return;

    CodeGenerator gen = new CodeGenerator(g);
    IntervalSet idTypes = new IntervalSet();
    idTypes.add(ANTLRParser.ID);
    idTypes.add(ANTLRParser.RULE_REF);
    idTypes.add(ANTLRParser.TOKEN_REF);
    List<GrammarAST> idNodes = g.ast.getNodesWithType(idTypes);
    for (GrammarAST idNode : idNodes) {
      if ( gen.getTarget().grammarSymbolCausesIssueInGeneratedCode(idNode) ) {
        g.tool.errMgr.grammarError(ErrorType.USE_OF_BAD_WORD,
                       g.fileName, idNode.getToken(),
View Full Code Here

    /** Lookahead for each alt 1..n */
    IntervalSet[] altLookSets = factory.getGrammar().decisionLOOK.get(decision);
    altLook = getAltLookaheadAsStringLists(altLookSets);

    IntervalSet expecting = IntervalSet.or(altLookSets); // combine alt sets
    this.error = getThrowNoViableAlt(factory, blkAST, expecting);
  }
View Full Code Here

    /** Lookahead for each alt 1..n */
//    IntervalSet[] altLookSets = LinearApproximator.getLL1LookaheadSets(dfa);
    IntervalSet[] altLookSets = factory.getGrammar().decisionLOOK.get(decision);
    altLook = getAltLookaheadAsStringLists(altLookSets);
    IntervalSet look = altLookSets[0];
    IntervalSet followLook = altLookSets[1];

    IntervalSet expecting = look.or(followLook);
    this.error = getThrowNoViableAlt(factory, blkAST, expecting);

    expr = addCodeForLookaheadTempVar(look);
    followExpr = factory.getLL1Test(followLook, blkAST);
  }
View Full Code Here

   */
  public Set<Decl> getDeclsForAllElements(List<AltAST> altASTs) {
    Set<String> needsList = new HashSet<String>();
    List<GrammarAST> allRefs = new ArrayList<GrammarAST>();
    for (AltAST ast : altASTs) {
      IntervalSet reftypes = new IntervalSet(RULE_REF, TOKEN_REF);
      List<GrammarAST> refs = ast.getNodesWithType(reftypes);
      allRefs.addAll(refs);
      FrequencySet<String> altFreq = getElementFrequenciesForAlt(ast);
      for (GrammarAST t : refs) {
        String refLabelName = t.getText();
View Full Code Here

    StarLoopEntryState star = (StarLoopEntryState)starRoot.atnState;
    loopBackStateNumber = star.loopBackState.stateNumber;
    this.decision = star.decision;
    IntervalSet[] altLookSets = factory.getGrammar().decisionLOOK.get(decision);
    assert altLookSets.length == 2;
    IntervalSet enterLook = altLookSets[0];
    IntervalSet exitLook = altLookSets[1];
    loopExpr = addCodeForLoopLookaheadTempVar(enterLook);
  }
View Full Code Here

    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);
  }
View Full Code Here

TOP

Related Classes of org.antlr.v4.runtime.misc.IntervalSet

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.