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();
}
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);
}
}
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());
}
}
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;
}
}
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;
}
Aggregations