Search in sources :

Example 6 with GrammarRootAST

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

the class Tool method loadGrammar.

/** Convenience method to load and process an ANTLR grammar. Useful
	 *  when creating interpreters.  If you need to access to the lexer
	 *  grammar created while processing a combined grammar, use
	 *  getImplicitLexer() on returned grammar.
	 */
public Grammar loadGrammar(String fileName) {
    GrammarRootAST grammarRootAST = parseGrammar(fileName);
    final Grammar g = createGrammar(grammarRootAST);
    g.fileName = fileName;
    process(g, false);
    return g;
}
Also used : GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar)

Example 7 with GrammarRootAST

use of org.antlr.v4.tool.ast.GrammarRootAST 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)

Example 8 with GrammarRootAST

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

the class Tool method processGrammarsOnCommandLine.

public void processGrammarsOnCommandLine() {
    List<GrammarRootAST> sortedGrammars = sortGrammarByTokenVocab(grammarFiles);
    for (GrammarRootAST t : sortedGrammars) {
        final Grammar g = createGrammar(t);
        g.fileName = t.fileName;
        if (gen_dependencies) {
            BuildDependencyGenerator dep = new BuildDependencyGenerator(this, g);
            /*
					List outputFiles = dep.getGeneratedFileList();
					List dependents = dep.getDependenciesFileList();
					System.out.println("output: "+outputFiles);
					System.out.println("dependents: "+dependents);
					 */
            System.out.println(dep.getDependencies().render());
        } else if (errMgr.getNumErrors() == 0) {
            process(g, true);
        }
    }
}
Also used : GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) BuildDependencyGenerator(org.antlr.v4.tool.BuildDependencyGenerator) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar)

Example 9 with GrammarRootAST

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

the class Tool method parse.

public GrammarRootAST parse(String fileName, CharStream in) {
    try {
        GrammarASTAdaptor adaptor = new GrammarASTAdaptor(in);
        ToolANTLRLexer lexer = new ToolANTLRLexer(in, this);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        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;
                }
                return ((GrammarRootAST) root);
            }
        } catch (v3TreeGrammarException e) {
            errMgr.grammarError(ErrorType.V3_TREE_GRAMMAR, fileName, e.location);
        }
        return null;
    } catch (RecognitionException re) {
        // TODO: do we gen errors now?
        ErrorManager.internalError("can't generate this message at moment; antlr recovers");
    }
    return null;
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) ToolANTLRLexer(org.antlr.v4.parse.ToolANTLRLexer) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) org.antlr.v4.parse.v3TreeGrammarException(org.antlr.v4.parse.v3TreeGrammarException) ParserRuleReturnScope(org.antlr.runtime.ParserRuleReturnScope) ToolANTLRParser(org.antlr.v4.parse.ToolANTLRParser) RecognitionException(org.antlr.runtime.RecognitionException)

Example 10 with GrammarRootAST

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

the class Tool method process.

/** To process a grammar, we load all of its imported grammars into
		subordinate grammar objects. Then we merge the imported rules
		into the root grammar. If a root grammar is a combined grammar,
		we have to extract the implicit lexer. Once all this is done, we
		process the lexer first, if present, and then the parser grammar
	 */
public void process(Grammar g, boolean gencode) {
    g.loadImportedGrammars();
    GrammarTransformPipeline transform = new GrammarTransformPipeline(g, this);
    transform.process();
    LexerGrammar lexerg;
    GrammarRootAST lexerAST;
    if (g.ast != null && g.ast.grammarType == ANTLRParser.COMBINED && !g.ast.hasErrors) {
        // alters g.ast
        lexerAST = transform.extractImplicitLexer(g);
        if (lexerAST != null) {
            if (grammarOptions != null) {
                lexerAST.cmdLineOptions = grammarOptions;
            }
            lexerg = new LexerGrammar(this, lexerAST);
            lexerg.fileName = g.fileName;
            lexerg.originalGrammar = g;
            g.implicitLexer = lexerg;
            lexerg.implicitLexerOwner = g;
            processNonCombinedGrammar(lexerg, gencode);
        //				System.out.println("lexer tokens="+lexerg.tokenNameToTypeMap);
        //				System.out.println("lexer strings="+lexerg.stringLiteralToTypeMap);
        }
    }
    if (g.implicitLexer != null)
        g.importVocab(g.implicitLexer);
    //		System.out.println("tokens="+g.tokenNameToTypeMap);
    //		System.out.println("strings="+g.stringLiteralToTypeMap);
    processNonCombinedGrammar(g, gencode);
}
Also used : GrammarTransformPipeline(org.antlr.v4.tool.GrammarTransformPipeline) GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) LexerGrammar(org.antlr.v4.tool.LexerGrammar)

Aggregations

GrammarRootAST (org.antlr.v4.tool.ast.GrammarRootAST)11 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)7 LexerGrammar (org.antlr.v4.tool.LexerGrammar)6 Grammar (org.antlr.v4.tool.Grammar)5 File (java.io.File)3 ArrayList (java.util.ArrayList)3 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)3 RuleAST (org.antlr.v4.tool.ast.RuleAST)3 ANTLRFileStream (org.antlr.runtime.ANTLRFileStream)2 RecognitionException (org.antlr.runtime.RecognitionException)2 Pair (org.antlr.v4.runtime.misc.Pair)2 GrammarTransformPipeline (org.antlr.v4.tool.GrammarTransformPipeline)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CommonToken (org.antlr.runtime.CommonToken)1 CommonTokenStream (org.antlr.runtime.CommonTokenStream)1 ParserRuleReturnScope (org.antlr.runtime.ParserRuleReturnScope)1 Tree (org.antlr.runtime.tree.Tree)1