Skip to content

Commit

Permalink
fixed loading php parser from jar file
Browse files Browse the repository at this point in the history
  • Loading branch information
basten committed Aug 27, 2014
1 parent 0b278cb commit 939afb2
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 64 deletions.
70 changes: 70 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
Any modifications will be overwritten.
To include a user specific buildfile here, simply create one in the same
directory with the processing instruction <?eclipse.ant.import?>
as the first entry and export the buildfile again. --><project basedir="." default="build" name="php-analysis">
<property environment="env"/>
<property name="ECLIPSE_HOME" value="../../../programs/eclipse"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.7"/>
<property name="source" value="1.7"/>
<path id="Plug-in Dependencies.libraryclasspath">
<pathelement location="../../../../../pdb.values"/>
<pathelement location="../../../../../rascal"/>
</path>
<path id="php-analysis.classpath">
<path refid="Plug-in Dependencies.libraryclasspath"/>
<pathelement location="bin"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.java"/>
<exclude name="**/tests/**"/>
</fileset>
</copy>
<copy includeemptydirs="false" todir="bin">
<fileset dir="lib">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="bin"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="build-subprojects,build-project" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
<src path="lib"/>
<classpath refid="php-analysis.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</copy>
<unzip dest="${ant.library.dir}">
<patternset includes="jdtCompilerAdapter.jar"/>
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</unzip>
</target>
<target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
<target name="jar">
<mkdir dir="jar"/>
<jar manifest="META-INF/MANIFEST.MF" destfile="jar/php-analysis.jar">
<fileset dir="." includes="src/**,META-INF/**" excludes="**/tests/**"/>
<fileset dir="bin" includes="**/*.class"/>
<fileset dir="lib"/>
</jar>
</target>
</project>
11 changes: 6 additions & 5 deletions src/lang/php/util/Utils.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ private str executePHP(list[str] opts, loc cwd) {
}

private Script parsePHPfile(loc f, list[str] opts, Script error) {
str parserLoc = usePhpParserJar ? getPhpParserLocFromJar() + astToRascal : rgenLoc.path;
loc parserLoc = usePhpParserJar ? getPhpParserLocFromJar() : lang::php::util::Config::parserLoc;

str phpOut;
try {
phpOut = executePHP(["-d memory_limit="+parserMemLimit, rgenLoc.path, "-f<f.path>"] + opts, rgenCwd);
phpOut = executePHP(["-d memory_limit="+parserMemLimit, (parserLoc + astToRascal).path, "-f<f.path>"] + opts, parserLoc + libRascal);
}
catch RuntimeException:
return error;
Expand Down Expand Up @@ -78,7 +79,7 @@ public bool testPHPInstallation() {
public Stmt parsePHPStatement(str s) {
tempFile = |file:///tmp/parseStmt.php|;
writeFile(tempFile, "\<?php\n<s>?\>");
Script res = executePHPfile(tempFile, [], errscript("Could not parse <s>"));
Script res = parsePHPfile(tempFile, [], errscript("Could not parse <s>"));
if (errscript(_) := res) throw "Found error in PHP code to parse";
if (script(sl) := res && size(sl) == 1) return head(sl);
if (script(sl) := res) return block(sl);
Expand Down Expand Up @@ -111,8 +112,8 @@ public Script loadPHPFile(loc l, bool addLocationAnnotations, bool addUniqueIds)
if (l.scheme == "home") opts += "-r";
if (includePhpDocs) opts += "--phpdocs";

Script res = executePHPfile(l, opts, errscript("Could not parse file <l.path>: <phcErr>"));
if (errscript(err) := res) println("Found error in file <l.path>. Error: <err>");
Script res = parsePHPfile(l, opts, errscript("Could not parse file <l.path>"));
if (errscript(err) := res) logMessage("Found error in file <l.path>. Error: <err>", 2);
return res;
}

Expand Down
86 changes: 27 additions & 59 deletions src/org/rascal/phpanalysis/PhpJarExtractor.java
Original file line number Diff line number Diff line change
@@ -1,82 +1,50 @@
package org.rascal.phpanalysis;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Enumeration;
import java.nio.file.StandardCopyOption;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;

import org.eclipse.imp.pdb.facts.ISourceLocation;
import org.eclipse.imp.pdb.facts.IValue;
import org.eclipse.imp.pdb.facts.IValueFactory;
import org.rascalmpl.interpreter.utils.RuntimeExceptionFactory;

public class PhpJarExtractor {
protected final IValueFactory values;
private static ISourceLocation phpParserLoc = null;

public PhpJarExtractor(IValueFactory values){
this.values = values;
}

public IValue getPhpParserLocFromJar() throws IOException {
InputStream is = null;
FileOutputStream fo = null;
JarFile jarFile = null;

try {
if (phpParserLoc == null) {
File tempDir = Files.createTempDirectory("rascal-php-parser-jar").toAbsolutePath().toFile();
File tempJar = new File(tempDir, "php-parser.jar");

//System.out.println(getClass().getClassLoader().);

if (!tempJar.exists()) {
Files.copy(getClass().getClassLoader().getResourceAsStream("php-parser.jar"), tempJar.toPath());
}

jarFile = new JarFile(tempJar);
Enumeration<JarEntry> entries = jarFile.entries();

while (entries.hasMoreElements())
{
JarEntry je = entries.nextElement();
File fl = new File(tempDir, je.getName());

if (!fl.exists())
{
fl.getParentFile().mkdirs();
//fl = new File(tempDir, je.getName());
}
if (je.isDirectory())
{
continue;
}

is = jarFile.getInputStream(je);
fo = new FileOutputStream(fl);

while (is.available() > 0)
{
fo.write(is.read());
}

fo.close();
is.close();

fo = null;
is = null;
}

return values.sourceLocation(tempDir.getAbsolutePath());
}
catch(IOException ioex) {
throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null);
}
finally {
if (fo != null) fo.close();
if (is != null) is.close();
if (jarFile != null) jarFile.close();
try (JarInputStream jarStream = new JarInputStream(getClass().getClassLoader().getResourceAsStream("php-parser.jar"))) {
JarEntry je;
while ((je = jarStream.getNextJarEntry()) != null)
{
File fl = new File(tempDir, je.getName());

if (!fl.exists())
{
fl.getParentFile().mkdirs();
//fl = new File(tempDir, je.getName());
}
if (je.isDirectory())
{
continue;
}

Files.copy(jarStream, fl.toPath(), StandardCopyOption.REPLACE_EXISTING);
}

phpParserLoc = values.sourceLocation(tempDir.getAbsolutePath() + "/PHP-Parser/");
}
}
return phpParserLoc;
}
}

0 comments on commit 939afb2

Please sign in to comment.