Package org.antlr.runtime

Examples of org.antlr.runtime.CommonToken


    // ^(10 100 101 ^(20 ^(30 40 (50 (60 70)))) (80 90)))
    // stream has 8 real + 10 nav nodes
    int n = 9;
    CommonTree[] nodes = new CommonTree[n];
    for (int i=0; i< n; i++) {
      nodes[i] = new CommonTree(new CommonToken((i+1)*10));
    }
    Tree g = nodes[0];
    Tree rules = nodes[1];
    Tree rule1 = nodes[2];
    Tree id = nodes[3];
    Tree block = nodes[4];
    Tree alt = nodes[5];
    Tree s = nodes[6];
    Tree rule2 = nodes[7];
    Tree id2 = nodes[8];
    g.addChild(new CommonTree(new CommonToken(100)));
    g.addChild(new CommonTree(new CommonToken(101)));
    g.addChild(rules);
    rules.addChild(rule1);
    rule1.addChild(id);
    rule1.addChild(block);
    block.addChild(alt);
View Full Code Here


  protected boolean debug = false;

  static class V extends CommonTree {
    public int x;
    public V(Token t) { this.token = t;}
    public V(int ttype, int x) { this.x=x; token=new CommonToken(ttype); }
View Full Code Here

      labelName = existingLabelName;
    }
    else {
      // else create new label
      labelName = generator.createUniqueLabel(refdSymbol);
      CommonToken label = new CommonToken(ANTLRParser.ID, labelName);
      if ( grammar.type != Grammar.LEXER &&
         Character.isUpperCase(refdSymbol.charAt(0)) )
      {
        grammar.defineTokenRefLabel(name, label, uniqueRefAST);
      }
View Full Code Here

    for (GrammarAST x : outerAltRuleRefs) {
      RuleRefAST rref = (RuleRefAST)x;
      boolean recursive = rref.getText().equals(ruleName);
      boolean rightmost = rref == outerAltRuleRefs.get(outerAltRuleRefs.size()-1);
      if ( recursive && rightmost ) {
        GrammarAST dummyValueNode = new GrammarAST(new CommonToken(ANTLRParser.INT, ""+prec));
        rref.setOption(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME, dummyValueNode);
      }
    }
    return t;
  }
View Full Code Here

    GrammarAST[] elements = combinedAST.getChildren().toArray(new GrammarAST[0]);

    // MAKE A GRAMMAR ROOT and ID
    String lexerName = combinedAST.getChild(0).getText()+"Lexer";
    GrammarRootAST lexerAST =
        new GrammarRootAST(new CommonToken(ANTLRParser.GRAMMAR, "LEXER_GRAMMAR"), combinedGrammar.ast.tokenStream);
    lexerAST.grammarType = ANTLRParser.LEXER;
    lexerAST.token.setInputStream(combinedAST.token.getInputStream());
    lexerAST.addChild((GrammarAST)adaptor.create(ANTLRParser.ID, lexerName));

    // COPY OPTIONS
    GrammarAST optionsRoot =
      (GrammarAST)combinedAST.getFirstChildWithType(ANTLRParser.OPTIONS);
    if ( optionsRoot!=null && optionsRoot.getChildCount()!=0 ) {
      GrammarAST lexerOptionsRoot = (GrammarAST)adaptor.dupNode(optionsRoot);
      lexerAST.addChild(lexerOptionsRoot);
      GrammarAST[] options = optionsRoot.getChildren().toArray(new GrammarAST[0]);
      for (GrammarAST o : options) {
        String optionName = o.getChild(0).getText();
        if ( Grammar.lexerOptions.contains(optionName) &&
           !Grammar.doNotCopyOptionsToLexer.contains(optionName) )
        {
          GrammarAST optionTree = (GrammarAST)adaptor.dupTree(o);
          lexerOptionsRoot.addChild(optionTree);
          lexerAST.setOption(optionName, (GrammarAST)optionTree.getChild(1));
        }
      }
    }

    // COPY all named actions, but only move those with lexer:: scope
    List<GrammarAST> actionsWeMoved = new ArrayList<GrammarAST>();
    for (GrammarAST e : elements) {
      if ( e.getType()==ANTLRParser.AT ) {
        lexerAST.addChild((Tree)adaptor.dupTree(e));
        if ( e.getChild(0).getText().equals("lexer") ) {
          actionsWeMoved.add(e);
        }
      }
    }

    for (GrammarAST r : actionsWeMoved) {
      combinedAST.deleteChild( r );
    }

    GrammarAST combinedRulesRoot =
      (GrammarAST)combinedAST.getFirstChildWithType(ANTLRParser.RULES);
    if ( combinedRulesRoot==null ) return lexerAST;

    // MOVE lexer rules

    GrammarAST lexerRulesRoot =
      (GrammarAST)adaptor.create(ANTLRParser.RULES, "RULES");
    lexerAST.addChild(lexerRulesRoot);
    List<GrammarAST> rulesWeMoved = new ArrayList<GrammarAST>();
    GrammarASTWithOptions[] rules;
    if (combinedRulesRoot.getChildCount() > 0) {
      rules = combinedRulesRoot.getChildren().toArray(new GrammarASTWithOptions[0]);
    }
    else {
      rules = new GrammarASTWithOptions[0];
    }

    for (GrammarASTWithOptions r : rules) {
      String ruleName = r.getChild(0).getText();
      if (Grammar.isTokenName(ruleName)) {
        lexerRulesRoot.addChild((Tree)adaptor.dupTree(r));
        rulesWeMoved.add(r);
      }
    }
    for (GrammarAST r : rulesWeMoved) {
      combinedRulesRoot.deleteChild( r );
    }

    // Will track 'if' from IF : 'if' ; rules to avoid defining new token for 'if'
    List<Pair<GrammarAST,GrammarAST>> litAliases =
      Grammar.getStringLiteralAliasesFromLexerRules(lexerAST);

    Set<String> stringLiterals = combinedGrammar.getStringLiterals();
    // add strings from combined grammar (and imported grammars) into lexer
    // put them first as they are keywords; must resolve ambigs to these rules
//    tool.log("grammar", "strings from parser: "+stringLiterals);
    int insertIndex = 0;
    nextLit:
    for (String lit : stringLiterals) {
      // if lexer already has a rule for literal, continue
      if ( litAliases!=null ) {
        for (Pair<GrammarAST,GrammarAST> pair : litAliases) {
          GrammarAST litAST = pair.b;
          if ( lit.equals(litAST.getText()) ) continue nextLit;
        }
      }
      // create for each literal: (RULE <uniquename> (BLOCK (ALT <lit>))
      String rname = combinedGrammar.getStringLiteralLexerRuleName(lit);
      // can't use wizard; need special node types
      GrammarAST litRule = new RuleAST(ANTLRParser.RULE);
      BlockAST blk = new BlockAST(ANTLRParser.BLOCK);
      AltAST alt = new AltAST(ANTLRParser.ALT);
      TerminalAST slit = new TerminalAST(new CommonToken(ANTLRParser.STRING_LITERAL, lit));
      alt.addChild(slit);
      blk.addChild(alt);
      CommonToken idToken = new CommonToken(ANTLRParser.TOKEN_REF, rname);
      litRule.addChild(new TerminalAST(idToken));
      litRule.addChild(blk);
      lexerRulesRoot.insertChild(insertIndex, litRule);
//      lexerRulesRoot.getChildren().add(0, litRule);
      lexerRulesRoot.freshenParentAndChildIndexes(); // reset indexes and set litRule parent
View Full Code Here

      epsilon(left, right);
      return new Handle(left, right);
    }

    // define action AST for this rule as if we had found in grammar
        ActionAST ast =  new ActionAST(new CommonToken(ANTLRParser.ACTION, action));
    currentRule.defineActionInAlt(currentOuterAlt, ast);
    return action(ast);
  }
View Full Code Here

  public GrammarAST(Token token) {
    initialize(token);
  }

  public void initialize(int i, String s) {
        token = new CommonToken(i,s);
    token.setTokenIndex(-1);
    }
View Full Code Here

    // In LR transform, we alter original token stream to make e -> e[n]
    // Since we will be altering the dup, we need dup to have the
    // original token.  We can set this tree (the original) to have
    // a new token.
    r.token = this.token;
    this.token = new CommonToken(r.token);
    return r;
  }
View Full Code Here

    super(node);
    this.g = node.g;
    this.atnState = node.atnState;
    this.textOverride = node.textOverride;
  }
    public GrammarAST(int type) { super(new CommonToken(type, ANTLRParser.tokenNames[type])); }
View Full Code Here

    this.atnState = node.atnState;
    this.textOverride = node.textOverride;
  }
    public GrammarAST(int type) { super(new CommonToken(type, ANTLRParser.tokenNames[type])); }
    public GrammarAST(int type, Token t) {
    this(new CommonToken(t));
    token.setType(type);
  }
View Full Code Here

TOP

Related Classes of org.antlr.runtime.CommonToken

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.