Commit 5aa511c1 authored by Konrad Pröll's avatar Konrad Pröll
Browse files
parents 4160a57f 2bfe2e92
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="lib"/>
<classpathentry kind="lib" path="lib/java-cup-11b-runtime.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="lib"/>
<classpathentry kind="lib" path="lib/Mouse-1.7.runtime.jar"/>
<classpathentry kind="lib" path="lib/Mouse-1.7.jar"/>
<classpathentry kind="lib" path="lib/java-cup-11b-runtime.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
......@@ -20,7 +20,13 @@
debugsymbols="true" />
</target>
<path id="libraries"> <files includes="${lib}/java-cup-11b-runtime.jar" /> </path>
<!--<path id="libraries"> <files includes="${lib}/java-cup-11b-runtime.jar" /> </path>-->
<path id="libraries">
<pathelement location="${src}" />
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</path>
<target name="compile" depends="generate">
<mkdir dir="${classes}"/>
......
package de.in.tum.www2.cup.ast;
import de.in.tum.www2.cup.Range;
import java_cup.runtime.XMLElement;
import de.in.tum.www2.cup.Position;
public abstract class CodeBlock extends AbstractNode {
private int debugId;
private String blob;
private XMLElement javaTree;
public int getDebugId() {
return debugId;
......@@ -15,15 +17,19 @@ public abstract class CodeBlock extends AbstractNode {
return blob;
}
public CodeBlock(int debugId, String blob, Range range) {
super(range);
this.debugId = debugId;
this.blob = blob;
try{
this.javaTree = JavaCompiler.parseJava(blob);
} catch(Exception e){}
}
@Override
protected void putDescription(StringBuilder builder) {
builder.append(blob);
builder.append(javaTree);
}
}
package de.in.tum.www2.cup.ast;
import java.io.*;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java_cup.runtime.XMLElement;
import mouse.runtime.SourceString;
import java_cup.runtime.SyntaxTreeTransform;
public class JavaCompiler {
public static XMLElement parseJava(String blob) throws Exception {
SourceString src = new SourceString(blob);
JavaParser parser=new JavaParser();
boolean ok = parser.parse(src);
if (ok) {
JavaSemantics sem = parser.semantics();
System.out.println(" -> Compilation succeeded");
XMLElement elem = (XMLElement)sem.rule.rhs(0).get();
// Remove unwanted unary branch chains from the syntax tree
elem = SyntaxTreeTransform.removeUnaryChains(elem);
// Dump DoM into XML-String via output stream
XMLOutputFactory outFactory = XMLOutputFactory.newInstance();
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLStreamWriter sw = outFactory.createXMLStreamWriter(out);
XMLElement.dump(null, sw, elem);
//PrintWriter pw = new PrintWriter("debug.xml");
//String s = new String(out.toByteArray());
//pw.print(s);
//pw.flush();
//pw.close();
// Transform XML-Tree via XSL to carry symbolname as tagname
Transformer transformer;
transformer = TransformerFactory.newInstance().newTransformer(new StreamSource("tree.xsl"));
Source text = new StreamSource(new ByteArrayInputStream(out.toByteArray()));
out = new ByteArrayOutputStream();
transformer.transform(text, new StreamResult(out));
// Transform XML-Tree via XSL into an HTML-Based Syntaxtree
transformer= TransformerFactory.newInstance().newTransformer(new StreamSource("tree-view.xsl"));
text=new StreamSource(new ByteArrayInputStream(out.toByteArray()));
//transformer.transform(text,new StreamResult(new File(args[1]+".ast.html")));
System.out.println(" -> Transformation succeeded");
return elem;
}
//TODO add exception
throw new RuntimeException("Java parsing failed.");
}
}
package de.in.tum.www2.cup.ast;
import java_cup.runtime.ComplexSymbolFactory.Location;
import java_cup.runtime.XMLElement;
import mouse.runtime.Phrase;
public interface JavaSemanticsUsercode {
Phrase lhs();
Phrase rhs(int i);
//-------------------------------------------------------------------
// Identifier = !Keyword Letter LetterOrDigit* Spacing
//-------------------------------------------------------------------
default void Identifier()
{
XMLElement x = (XMLElement)lhs().get();
lhs().put(new XMLElement.Terminal(new Location(lhs().getStartLine(),lhs().getStartColumn(),lhs().getStartPos()),"Identifier",lhs().text().trim(),new Location(lhs().getEndLine(),lhs().getEndColumn(),lhs().getEndPos())));
}
//-------------------------------------------------------------------
// CLASS = "class" !LetterOrDigit Spacing
// INTERFACE = "interface" !LetterOrDigit Spacing
// LPAR = "(" Spacing
// RPAR = ")" Spacing
// LWING = "{" Spacing
// RWING = "}" Spacing
// SEMI = ";" Spacing
// Spacing = ([ \t\r\n\f]+ / "/*" _*+ "*/" / "//" _*+ [\r\n])*
//-------------------------------------------------------------------
default void Suppress()
{
lhs().put(null);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment