Search in sources :

Example 1 with Tool

use of org.antlr.v4.Tool in project antlr4 by antlr.

the class BaseGoTest method locateTool.

private static String locateTool(String tool) {
    // default cap is about right
    ArrayList<String> paths = new ArrayList<String>();
    // GOROOT should have priority if set
    String goroot = System.getenv("GOROOT");
    if (goroot != null) {
        paths.add(goroot + File.separatorChar + "bin");
    }
    String pathEnv = System.getenv("PATH");
    if (pathEnv != null) {
        paths.addAll(Arrays.asList(pathEnv.split(File.pathSeparator)));
    }
    // OS specific default locations of binary dist as last resort
    paths.add("/usr/local/go/bin");
    paths.add("c:\\Go\\bin");
    for (String path : paths) {
        File candidate = new File(new File(path), tool);
        if (candidate.exists()) {
            return candidate.getPath();
        }
        candidate = new File(new File(path), tool + ".exe");
        if (candidate.exists()) {
            return candidate.getPath();
        }
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)

Example 2 with Tool

use of org.antlr.v4.Tool in project antlr4 by antlr.

the class BaseJavaTest method semanticProcess.

protected void semanticProcess(Grammar g) {
    if (g.ast != null && !g.ast.hasErrors) {
        //			System.out.println(g.ast.toStringTree());
        Tool antlr = new Tool();
        SemanticPipeline sem = new SemanticPipeline(g);
        sem.process();
        if (g.getImportedGrammars() != null) {
            // process imported grammars (if any)
            for (Grammar imp : g.getImportedGrammars()) {
                antlr.processNonCombinedGrammar(imp, false);
            }
        }
    }
}
Also used : SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Tool(org.antlr.v4.Tool)

Example 3 with Tool

use of org.antlr.v4.Tool in project antlr4 by antlr.

the class GrammarDependencies method analyse.

private void analyse(File grammarFile, Collection<File> grammarFiles, Tool tool) {
    GrammarRootAST grammar = tool.parseGrammar(grammarFile.getAbsolutePath());
    if (grammar == null)
        return;
    for (GrammarAST importDecl : grammar.getAllChildrenWithType(ANTLRParser.IMPORT)) {
        Tree id = importDecl.getFirstChildWithType(ANTLRParser.ID);
        // being reported by the ANTLR tool
        if (id != null) {
            String grammarPath = getRelativePath(grammarFile);
            graph.addEdge(id.getText() + ".g4", grammarPath);
        }
    }
    for (GrammarAST options : grammar.getAllChildrenWithType(ANTLRParser.OPTIONS)) {
        for (int i = 0, count = options.getChildCount(); i < count; i++) {
            Tree option = options.getChild(i);
            if (option.getType() == ANTLRParser.ASSIGN) {
                String key = option.getChild(0).getText();
                String value = option.getChild(1).getText();
                if ("tokenVocab".equals(key)) {
                    String name = stripQuotes(value);
                    // the grammar name may be qualified, but we resolve the path anyway
                    String grammarName = stripPath(name);
                    String grammarPath = MojoUtils.findSourceSubdir(sourceDirectory, grammarFile);
                    File depGrammarFile = resolve(grammarName, grammarPath);
                    // (files probably reside in the root directory anyway with such a configuration )
                    if (packageName != null)
                        grammarPath = packageName;
                    graph.addEdge(getRelativePath(depGrammarFile), grammarPath + grammarFile.getName());
                }
            }
        }
    }
}
Also used : GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Tree(org.antlr.runtime.tree.Tree) File(java.io.File)

Example 4 with Tool

use of org.antlr.v4.Tool in project antlr4 by antlr.

the class TestATNConstruction method testParserRuleRefInLexerRule.

@Test
public void testParserRuleRefInLexerRule() throws Exception {
    boolean threwException = false;
    ErrorQueue errorQueue = new ErrorQueue();
    try {
        String gstr = "grammar U;\n" + "a : A;\n" + "A : a;\n";
        Tool tool = new Tool();
        tool.removeListeners();
        tool.addListener(errorQueue);
        assertEquals(0, errorQueue.size());
        GrammarRootAST grammarRootAST = tool.parseGrammarFromString(gstr);
        assertEquals(0, errorQueue.size());
        Grammar g = tool.createGrammar(grammarRootAST);
        assertEquals(0, errorQueue.size());
        g.fileName = "<string>";
        tool.process(g, false);
    } catch (Exception e) {
        threwException = true;
        e.printStackTrace();
    }
    System.out.println(errorQueue);
    assertEquals(1, errorQueue.errors.size());
    assertEquals(ErrorType.PARSER_RULE_REF_IN_LEXER_RULE, errorQueue.errors.get(0).getErrorType());
    assertEquals("[a, A]", Arrays.toString(errorQueue.errors.get(0).getArgs()));
    assertTrue(!threwException);
}
Also used : GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Tool(org.antlr.v4.Tool) Test(org.junit.Test)

Example 5 with Tool

use of org.antlr.v4.Tool in project antlr4 by antlr.

the class BaseNodeTest method canExecute.

private boolean canExecute(String tool) {
    try {
        ProcessBuilder builder = new ProcessBuilder(tool, "--version");
        builder.redirectErrorStream(true);
        Process process = builder.start();
        StreamVacuum vacuum = new StreamVacuum(process.getInputStream());
        vacuum.start();
        process.waitFor();
        vacuum.join();
        return process.exitValue() == 0;
    } catch (Exception e) {
        ;
    }
    return false;
}
Also used : StreamVacuum(org.antlr.v4.test.runtime.StreamVacuum)

Aggregations

Tool (org.antlr.v4.Tool)9 Grammar (org.antlr.v4.tool.Grammar)7 LexerGrammar (org.antlr.v4.tool.LexerGrammar)7 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)6 File (java.io.File)5 RecognitionException (org.antlr.runtime.RecognitionException)3 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ANTLRMessage (org.antlr.v4.tool.ANTLRMessage)2 GrammarRootAST (org.antlr.v4.tool.ast.GrammarRootAST)2 RuleAST (org.antlr.v4.tool.ast.RuleAST)2 ST (org.stringtemplate.v4.ST)2 STGroupFile (org.stringtemplate.v4.STGroupFile)2 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStreamReader (java.io.InputStreamReader)1 Field (java.lang.reflect.Field)1 LinkedHashMap (java.util.LinkedHashMap)1