Package org.antlr.v4.tool.ast

Examples of org.antlr.v4.tool.ast.GrammarAST


    List<Pair<GrammarAST,GrammarAST>> litAliases =
      Grammar.getStringLiteralAliasesFromLexerRules(g.ast);
    Set<String> conflictingLiterals = new HashSet<String>();
    if ( litAliases!=null ) {
      for (Pair<GrammarAST,GrammarAST> pair : litAliases) {
        GrammarAST nameAST = pair.a;
        GrammarAST litAST = pair.b;
        if ( !G.stringLiteralToTypeMap.containsKey(litAST.getText()) ) {
          G.defineTokenAlias(nameAST.getText(), litAST.getText());
        }
        else {
          // oops two literal defs in two rules (within or across modes).
          conflictingLiterals.add(litAST.getText());
        }
      }
      for (String lit : conflictingLiterals) {
        // Remove literal if repeated across rules so it's not
        // found by parser grammar.
View Full Code Here


    }

  }

  boolean hasTypeOrMoreCommand(@NotNull Rule r) {
    GrammarAST ast = r.ast;
    if (ast == null) {
      return false;
    }

    GrammarAST altActionAst = (GrammarAST)ast.getFirstDescendantWithType(ANTLRParser.LEXER_ALT_ACTION);
    if (altActionAst == null) {
      // the rule isn't followed by any commands
      return false;
    }

    // first child is the alt itself, subsequent are the actions
    for (int i = 1; i < altActionAst.getChildCount(); i++) {
      GrammarAST node = (GrammarAST)altActionAst.getChild(i);
      if (node.getType() == ANTLRParser.LEXER_ACTION_CALL) {
        if ("type".equals(node.getChild(0).getText())) {
          return true;
        }
      }
      else if ("more".equals(node.getText())) {
        return true;
      }
    }

    return false;
View Full Code Here

    public GrammarASTAdaptor() { }
    public GrammarASTAdaptor(org.antlr.runtime.CharStream input) { this.input = input; }

    @Override
    public Object create(Token token) {
        return new GrammarAST(token);
    }
View Full Code Here

    }

    @Override
    /** Make sure even imaginary nodes know the input stream */
    public Object create(int tokenType, String text) {
    GrammarAST t;
    if ( tokenType==ANTLRParser.RULE ) {
      // needed by TreeWizard to make RULE tree
          t = new RuleAST(new CommonToken(tokenType, text));
    }
    else if ( tokenType==ANTLRParser.STRING_LITERAL ) {
View Full Code Here

    pred.resolver = currentRule.alt[currentOuterAltNumber];
  }

  @Override
  public void ruleCatch(GrammarAST arg, ActionAST action) {
    GrammarAST catchme = (GrammarAST)action.getParent();
    currentRule.exceptions.add(catchme);
    action.resolver = currentRule;
  }
View Full Code Here

    typeToTokenList.add(null);
  }

    public void loadImportedGrammars() {
    if ( ast==null ) return;
        GrammarAST i = (GrammarAST)ast.getFirstChildWithType(ANTLRParser.IMPORT);
        if ( i==null ) return;
        importedGrammars = new ArrayList<Grammar>();
        for (Object c : i.getChildren()) {
            GrammarAST t = (GrammarAST)c;
            String importedGrammarName = null;
            if ( t.getType()==ANTLRParser.ASSIGN ) {
        t = (GrammarAST)t.getChild(1);
        importedGrammarName = t.getText();
            }
            else if ( t.getType()==ANTLRParser.ID ) {
                importedGrammarName = t.getText();
      }
      Grammar g;
      try {
        g = tool.loadImportedGrammar(this, t);
      }
      catch (IOException ioe) {
        tool.errMgr.grammarError(ErrorType.ERROR_READING_IMPORTED_GRAMMAR,
                     importedGrammarName,
                     t.getToken(),
                     importedGrammarName,
                     name);
        continue;
      }
      // did it come back as error node or missing?
View Full Code Here

  public static void setNodeOptions(GrammarAST node, GrammarAST options) {
    if ( options==null ) return;
    GrammarASTWithOptions t = (GrammarASTWithOptions)node;
    if ( t.getChildCount()==0 || options.getChildCount()==0 ) return;
    for (Object o : options.getChildren()) {
      GrammarAST c = (GrammarAST)o;
      if ( c.getType()==ANTLRParser.ASSIGN ) {
        t.setOption(c.getChild(0).getText(), (GrammarAST)c.getChild(1));
      }
      else {
        t.setOption(c.getText(), null); // no arg such as ID<VarNodeType>
      }
    }
  }
View Full Code Here

  public void buildNormalRuleFunction(Rule r, RuleFunction function) {
    CodeGenerator gen = delegate.getGenerator();
    // TRIGGER factory functions for rule alts, elements
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(r.ast.token.getInputStream());
    GrammarAST blk = (GrammarAST)r.ast.getFirstChildWithType(ANTLRParser.BLOCK);
    CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor,blk);
    walker = new SourceGenTriggers(nodes, this);
    try {
      // walk AST of rule alts/elements
      function.code = DefaultOutputModelFactory.list(walker.block(null, null));
View Full Code Here

        {
          elementOptions.append("tokenIndex=").append(tok.getTokenIndex());
        }

        if ( node instanceof GrammarASTWithOptions ) {
          GrammarASTWithOptions o = (GrammarASTWithOptions)node;
          for (Map.Entry<String, GrammarAST> entry : o.getOptions().entrySet()) {
            if (elementOptions.length() > 0) {
              elementOptions.append(',');
            }

            elementOptions.append(entry.getKey());
View Full Code Here

  public AltAST addPrecedenceArgToRules(AltAST t, int prec) {
    if ( t==null ) return null;
    // get all top-level rule refs from ALT
    List<GrammarAST> outerAltRuleRefs = t.getNodesWithTypePreorderDFS(IntervalSet.of(RULE_REF));
    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

TOP

Related Classes of org.antlr.v4.tool.ast.GrammarAST

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.