Search in sources :

Example 1 with GrammarASTErrorNode

use of org.antlr.v4.tool.ast.GrammarASTErrorNode in project antlr4 by antlr.

the class Tool method sortGrammarByTokenVocab.

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);
        // came back as error node
        if (t == null || t instanceof GrammarASTErrorNode)
            continue;
        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();
            // Strip quote characters if any
            int len = vocabName.length();
            int firstChar = vocabName.charAt(0);
            int lastChar = vocabName.charAt(len - 1);
            if (len >= 2 && firstChar == '\'' && lastChar == '\'') {
                vocabName = vocabName.substring(1, len - 1);
            }
            // If the name contains a path delimited by forward slashes,
            // use only the part after the last slash as the name
            int lastSlash = vocabName.lastIndexOf('/');
            if (lastSlash >= 0) {
                vocabName = vocabName.substring(lastSlash + 1);
            }
            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);
    }
    List<String> sortedGrammarNames = g.sort();
    //		System.out.println("sortedGrammarNames="+sortedGrammarNames);
    List<GrammarRootAST> sortedRoots = new ArrayList<GrammarRootAST>();
    for (String grammarName : sortedGrammarNames) {
        for (GrammarRootAST root : roots) {
            if (root.getGrammarName().equals(grammarName)) {
                sortedRoots.add(root);
                break;
            }
        }
    }
    return sortedRoots;
}
Also used : Graph(org.antlr.v4.misc.Graph) GrammarASTErrorNode(org.antlr.v4.tool.ast.GrammarASTErrorNode) GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Graph (org.antlr.v4.misc.Graph)1 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)1 GrammarASTErrorNode (org.antlr.v4.tool.ast.GrammarASTErrorNode)1 GrammarRootAST (org.antlr.v4.tool.ast.GrammarRootAST)1