Search in sources :

Example 11 with org.antlr.v4.runtime

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

the class BaseBrowserTest method locateRuntime.

private String locateRuntime() {
    String propName = "antlr-javascript-runtime";
    String prop = System.getProperty(propName);
    if (prop == null || prop.length() == 0)
        prop = "../runtime/JavaScript/src";
    File file = new File(prop);
    System.out.println(file.getAbsolutePath());
    if (!file.exists())
        throw new RuntimeException("Missing system property:" + propName);
    return file.getAbsolutePath();
}
Also used : 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 12 with org.antlr.v4.runtime

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

the class BaseRuntimeTest method testParser.

public void testParser(RuntimeTestDescriptor descriptor) throws Exception {
    mkdir(delegate.getTmpDir());
    Pair<String, String> pair = descriptor.getGrammar();
    ClassLoader cloader = getClass().getClassLoader();
    URL templates = cloader.getResource("org/antlr/v4/test/runtime/templates/" + descriptor.getTarget() + ".test.stg");
    STGroupFile targetTemplates = new STGroupFile(templates, "UTF-8", '<', '>');
    targetTemplates.registerRenderer(String.class, new StringRenderer());
    // write out any slave grammars
    List<Pair<String, String>> slaveGrammars = descriptor.getSlaveGrammars();
    if (slaveGrammars != null) {
        for (Pair<String, String> spair : slaveGrammars) {
            STGroup g = new STGroup('<', '>');
            g.registerRenderer(String.class, new StringRenderer());
            g.importTemplates(targetTemplates);
            ST grammarST = new ST(g, spair.b);
            writeFile(delegate.getTmpDir(), spair.a + ".g4", grammarST.render());
        }
    }
    String grammarName = pair.a;
    String grammar = pair.b;
    STGroup g = new STGroup('<', '>');
    g.importTemplates(targetTemplates);
    g.registerRenderer(String.class, new StringRenderer());
    ST grammarST = new ST(g, grammar);
    grammar = grammarST.render();
    String found = delegate.execParser(grammarName + ".g4", grammar, grammarName + "Parser", grammarName + "Lexer", grammarName + "Listener", grammarName + "Visitor", descriptor.getStartRule(), descriptor.getInput(), descriptor.showDiagnosticErrors());
    if (delegate instanceof SpecialRuntimeTestAssert) {
        ((SpecialRuntimeTestAssert) delegate).assertEqualStrings(descriptor.getErrors(), delegate.getParseErrors());
        ((SpecialRuntimeTestAssert) delegate).assertEqualStrings(descriptor.getOutput(), found);
    } else {
        assertEquals(descriptor.getErrors(), delegate.getParseErrors());
        assertEquals(descriptor.getOutput(), found);
    }
}
Also used : ST(org.stringtemplate.v4.ST) STGroup(org.stringtemplate.v4.STGroup) StringRenderer(org.stringtemplate.v4.StringRenderer) STGroupFile(org.stringtemplate.v4.STGroupFile) URL(java.net.URL) Pair(org.antlr.v4.runtime.misc.Pair)

Example 13 with org.antlr.v4.runtime

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

the class BaseRuntimeTest method testLexer.

public void testLexer(RuntimeTestDescriptor descriptor) throws Exception {
    mkdir(delegate.getTmpDir());
    Pair<String, String> pair = descriptor.getGrammar();
    ClassLoader cloader = getClass().getClassLoader();
    URL templates = cloader.getResource("org/antlr/v4/test/runtime/templates/" + descriptor.getTarget() + ".test.stg");
    STGroupFile targetTemplates = new STGroupFile(templates, "UTF-8", '<', '>');
    targetTemplates.registerRenderer(String.class, new StringRenderer());
    // write out any slave grammars
    List<Pair<String, String>> slaveGrammars = descriptor.getSlaveGrammars();
    if (slaveGrammars != null) {
        for (Pair<String, String> spair : slaveGrammars) {
            STGroup g = new STGroup('<', '>');
            g.registerRenderer(String.class, new StringRenderer());
            g.importTemplates(targetTemplates);
            ST grammarST = new ST(g, spair.b);
            writeFile(delegate.getTmpDir(), spair.a + ".g4", grammarST.render());
        }
    }
    String grammarName = pair.a;
    String grammar = pair.b;
    STGroup g = new STGroup('<', '>');
    g.registerRenderer(String.class, new StringRenderer());
    g.importTemplates(targetTemplates);
    ST grammarST = new ST(g, grammar);
    grammar = grammarST.render();
    String found = delegate.execLexer(grammarName + ".g4", grammar, grammarName, descriptor.getInput(), descriptor.showDFA());
    if (delegate instanceof SpecialRuntimeTestAssert) {
        ((SpecialRuntimeTestAssert) delegate).assertEqualStrings(descriptor.getOutput(), found);
        ((SpecialRuntimeTestAssert) delegate).assertEqualStrings(descriptor.getANTLRToolErrors(), delegate.getANTLRToolErrors());
        ((SpecialRuntimeTestAssert) delegate).assertEqualStrings(descriptor.getErrors(), delegate.getParseErrors());
    } else {
        assertEquals(descriptor.getOutput(), found);
        assertEquals(descriptor.getANTLRToolErrors(), delegate.getANTLRToolErrors());
        assertEquals(descriptor.getErrors(), delegate.getParseErrors());
    }
}
Also used : ST(org.stringtemplate.v4.ST) STGroup(org.stringtemplate.v4.STGroup) StringRenderer(org.stringtemplate.v4.StringRenderer) STGroupFile(org.stringtemplate.v4.STGroupFile) URL(java.net.URL) Pair(org.antlr.v4.runtime.misc.Pair)

Example 14 with org.antlr.v4.runtime

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

the class BaseCSharpTest method createProject.

public boolean createProject() {
    try {
        String pack = BaseCSharpTest.class.getPackage().getName().replace(".", "/") + "/";
        // save auxiliary files
        saveResourceAsFile(pack + "AssemblyInfo.cs", new File(tmpdir, "AssemblyInfo.cs"));
        saveResourceAsFile(pack + "App.config", new File(tmpdir, "App.config"));
        // update project
        String projectName = isWindows() ? "Antlr4.Test.vs2013.csproj" : "Antlr4.Test.mono.csproj";
        final ClassLoader loader = Thread.currentThread().getContextClassLoader();
        InputStream input = loader.getResourceAsStream(pack + projectName);
        Document prjXml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input);
        // update runtime project reference
        // find project file as a resource not relative pathname (now that we've merged repos)
        String runtimeName = isWindows() ? "Antlr4.Runtime.vs2013.csproj" : "Antlr4.Runtime.mono.csproj";
        final URL runtimeProj = loader.getResource("CSharp/runtime/CSharp/Antlr4.Runtime/" + runtimeName);
        if (runtimeProj == null) {
            throw new RuntimeException("C# runtime project file not found!");
        }
        String runtimeProjPath = runtimeProj.getPath();
        if (isWindows()) {
            runtimeProjPath = runtimeProjPath.replaceFirst("/", "");
        }
        XPathExpression exp = XPathFactory.newInstance().newXPath().compile("/Project/ItemGroup/ProjectReference[@Include='" + runtimeName + "']");
        Element node = (Element) exp.evaluate(prjXml, XPathConstants.NODE);
        node.setAttribute("Include", runtimeProjPath.replace("/", "\\"));
        // update project file list
        exp = XPathFactory.newInstance().newXPath().compile("/Project/ItemGroup[Compile/@Include='AssemblyInfo.cs']");
        Element group = (Element) exp.evaluate(prjXml, XPathConstants.NODE);
        if (group == null)
            return false;
        // remove existing children
        while (group.hasChildNodes()) group.removeChild(group.getFirstChild());
        // add AssemblyInfo.cs, not a generated source
        sourceFiles.add("AssemblyInfo.cs");
        // add files to compile
        for (String file : sourceFiles) {
            Element elem = group.getOwnerDocument().createElement("Compile");
            elem.setAttribute("Include", file);
            group.appendChild(elem);
        }
        // save project
        File prjFile = getTestProjectFile();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(new DOMSource(prjXml), new StreamResult(prjFile));
        return true;
    } catch (Exception e) {
        e.printStackTrace(System.err);
        return false;
    }
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) Document(org.w3c.dom.Document) URL(java.net.URL) IOException(java.io.IOException) File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)

Example 15 with org.antlr.v4.runtime

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

the class BaseCSharpTest method buildDotnetProject.

public boolean buildDotnetProject() {
    // find runtime package
    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    final URL runtimeProj = loader.getResource("CSharp/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.dotnet.xproj");
    if (runtimeProj == null) {
        throw new RuntimeException("C# runtime project file not found!");
    }
    File runtimeProjFile = new File(runtimeProj.getFile());
    String runtimeProjPath = runtimeProjFile.getParentFile().getParentFile().getPath();
    String projectRefPath = runtimeProjPath.substring(0, runtimeProjPath.lastIndexOf("/"));
    // update global.json to reference runtime path
    try {
        String content = new java.util.Scanner(new File(tmpdir + "/global.json")).useDelimiter("\\Z").next();
        content = content.replaceAll("replace_this", projectRefPath);
        java.io.PrintWriter out = new java.io.PrintWriter(tmpdir + "/global.json");
        out.write(content);
        out.close();
    } catch (Exception e) {
        return false;
    }
    // build test
    String dotnetcli = locateTool("dotnet");
    String[] args = new String[] { dotnetcli, "restore", ".", projectRefPath };
    try {
        boolean success = runProcess(args, tmpdir);
        args = new String[] { dotnetcli, "build", "src" };
        success = runProcess(args, tmpdir);
    } catch (Exception e) {
        return false;
    }
    return true;
}
Also used : BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) URL(java.net.URL) IOException(java.io.IOException) File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)

Aggregations

BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)8 File (java.io.File)6 URL (java.net.URL)6 BaseRuntimeTest.writeFile (org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)6 STGroupString (org.stringtemplate.v4.STGroupString)6 IOException (java.io.IOException)5 URISyntaxException (java.net.URISyntaxException)3 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)3 InputStream (java.io.InputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 NoViableAltException (org.antlr.v4.runtime.NoViableAltException)2 RecognitionException (org.antlr.v4.runtime.RecognitionException)2 Pair (org.antlr.v4.runtime.misc.Pair)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 ST (org.stringtemplate.v4.ST)2 STGroup (org.stringtemplate.v4.STGroup)2 STGroupFile (org.stringtemplate.v4.STGroupFile)2 StringRenderer (org.stringtemplate.v4.StringRenderer)2