Examples of GrammarAST


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

  public List<GrammarRootAST> sortGrammarByTokenVocab(List<String> fileNames) {
//    System.out.println(fileNames);
    Graph<String> g = new Graph<String>();
    List<GrammarRootAST> roots = new ArrayList<GrammarRootAST>();
    for (String fileName : fileNames) {
      GrammarAST t = parseGrammar(fileName);
      if ( t==null || t instanceof GrammarASTErrorNode) continue; // came back as error node
      if ( ((GrammarRootAST)t).hasErrors ) continue;
      GrammarRootAST root = (GrammarRootAST)t;
      roots.add(root);
      root.fileName = fileName;
      String grammarName = root.getChild(0).getText();

      GrammarAST tokenVocabNode = findOptionValueAST(root, "tokenVocab");
      // Make grammars depend on any tokenVocab options
      if ( tokenVocabNode!=null ) {
        String vocabName = tokenVocabNode.getText();
        g.addEdge(grammarName, vocabName);
      }
      // add cycle to graph so we always process a grammar if no error
      // even if no dependency
      g.addEdge(grammarName, grammarName);
View Full Code Here

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

    return sortedRoots;
  }

  /** Manually get option node from tree; return null if no defined. */
  public static GrammarAST findOptionValueAST(GrammarRootAST root, String option) {
    GrammarAST options = (GrammarAST)root.getFirstChildWithType(ANTLRParser.OPTIONS);
    if ( options!=null && options.getChildCount() > 0 ) {
      for (Object o : options.getChildren()) {
        GrammarAST c = (GrammarAST)o;
        if ( c.getType() == ANTLRParser.ASSIGN &&
           c.getChild(0).getText().equals(option) )
        {
          return (GrammarAST)c.getChild(1);
        }
      }
    }
    return null;
  }
View Full Code Here

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

      lexer.tokens = tokens;
      ToolANTLRParser p = new ToolANTLRParser(tokens, this);
      p.setTreeAdaptor(adaptor);
      try {
        ParserRuleReturnScope r = p.grammarSpec();
        GrammarAST root = (GrammarAST)r.getTree();
        if ( root instanceof GrammarRootAST) {
          ((GrammarRootAST)root).hasErrors = lexer.getNumberOfSyntaxErrors()>0 || p.getNumberOfSyntaxErrors()>0;
          assert ((GrammarRootAST)root).tokenStream == tokens;
          if ( grammarOptions!=null ) {
            ((GrammarRootAST)root).cmdLineOptions = grammarOptions;
View Full Code Here

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

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

    }

  }

  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

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

    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

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

    }

    @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

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

    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

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

    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

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

  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
TOP
Copyright © 2018 www.massapi.com. 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.