Commit 6f767980 authored by baueko's avatar baueko
Browse files

added blocktype to Codeblock for different parsing methods

parent 30a51771
......@@ -443,7 +443,7 @@ action_code_part ::=
// TODO: attach inner range! based on user_codexleft, user_codexright
parserResult.appendActionCode (new SpecialCodeBlock(
debug_id,
SpecialCodeBlock.BlockType.Action,
CodeBlock.BlockType.Action,
user_code,
Range.fromLocations(actxleft, smixright)));
}
......@@ -464,7 +464,7 @@ parser_code_part ::=
int debug_id = get_new_debug_id();
parserResult.appendParserCode (new SpecialCodeBlock(
debug_id,
SpecialCodeBlock.BlockType.Parser,
CodeBlock.BlockType.Parser,
user_code,
new Range(Position.fromLocation(prsxleft),
Position.fromLocation(smixright))));
......@@ -485,7 +485,7 @@ init_code ::=
int debug_id = get_new_debug_id();
parserResult.appendInitCode(new SpecialCodeBlock(
debug_id,
SpecialCodeBlock.BlockType.Init,
CodeBlock.BlockType.Init,
user_code,
new Range(Position.fromLocation(initxleft),
Position.fromLocation(smixright))));
......@@ -507,7 +507,7 @@ scan_code ::=
int debug_id = get_new_debug_id();
parserResult.appendScanCode (new SpecialCodeBlock(
debug_id,
SpecialCodeBlock.BlockType.Scan,
CodeBlock.BlockType.Scan,
user_code,
new Range(Position.fromLocation(scanxleft),
Position.fromLocation(smixright))));
......@@ -975,7 +975,10 @@ prod_part ::=
int debug_id = get_new_debug_id();
add_rhs_part(new action_part(attach_debug_symbol(debug_id, code_str)));
ast_add_rhs_part(new ActionCodeBlock(debug_id, code_str,
ast_add_rhs_part(new ActionCodeBlock(
debug_id,
CodeBlock.BlockType.ActionCode,
code_str,
Range.fromLocations(code_strxleft, code_strxright)
));
:}
......
......@@ -3,12 +3,10 @@ package de.in.tum.www2.cup.ast;
import de.in.tum.www2.cup.Range;
import de.in.tum.www2.cup.analysis.AbstractVisitor;
public class ActionCodeBlock extends CodeBlock
implements IProductionRightPart
{
public class ActionCodeBlock extends CodeBlock implements IProductionRightPart {
public ActionCodeBlock(int debugId, String blob, Range range) {
super(debugId, blob, range);
public ActionCodeBlock(int debugId, BlockType bt, String blob, Range range) {
super(debugId, bt, blob, range);
}
@Override
......@@ -17,6 +15,9 @@ public class ActionCodeBlock extends CodeBlock
visitor.postVisit(this, data);
}
@Override protected String getNodeName() { return "ActionCodeBlock"; }
@Override
protected String getNodeName() {
return "ActionCodeBlock";
}
}
......@@ -5,10 +5,16 @@ import java_cup.runtime.XMLElement;
import de.in.tum.www2.cup.Position;
public abstract class CodeBlock extends AbstractNode {
public enum BlockType {
Action, Parser, Init, Scan, ActionCode
}
private int debugId;
private String blob;
private XMLElement javaTree;
private BlockType bt;
public int getDebugId() {
return debugId;
}
......@@ -16,21 +22,27 @@ public abstract class CodeBlock extends AbstractNode {
public String getBlob() {
return blob;
}
public CodeBlock(int debugId, String blob, Range range) {
public BlockType getBt() {
return bt;
}
public CodeBlock(int debugId, BlockType bt, String blob, Range range) {
super(range);
this.debugId = debugId;
this.bt = bt;
this.blob = blob;
// this.blob = " public class Parser{" + blob + "}";
try{
this.javaTree = JavaCompiler.parseJava(this.blob);
} catch(Exception e){}
// this.blob = " public class Parser{" + blob + "}";
try {
this.javaTree = JavaCompiler.parseJava(this.blob, this);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@Override
protected void putDescription(StringBuilder builder) {
builder.append(javaTree);
}
}
package de.in.tum.www2.cup.ast;
import java.io.*;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
......@@ -13,25 +14,34 @@ 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);
// System.out.println(elem);
public static XMLElement parseJava(String blob, CodeBlock codeblock) throws Exception {
SourceString src = new SourceString(blob);
JavaParser parser = new JavaParser();
boolean ok = false;
switch (codeblock.getBt()) {
case Action: // ?
case Parser:
// TODO change parsing method
ok = parser.parse(src);
break;
default:// Init, Scan, ActionCodeBlock
// TODO change parsing method
ok = parser.parse(src);
break;
}
return elem;
}
//TODO add exception
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);
System.out.println(elem);
return elem;
}
// TODO add exception
throw new RuntimeException("Java parsing failed.");
}
}
}
......@@ -3,25 +3,15 @@ package de.in.tum.www2.cup.ast;
import de.in.tum.www2.cup.analysis.AbstractVisitor;
import de.in.tum.www2.cup.Range;
public class SpecialCodeBlock extends CodeBlock
implements IProductionRightPart
{
public class SpecialCodeBlock extends CodeBlock implements IProductionRightPart {
private BlockType bt;
public enum BlockType {
Action,
Parser,
Init,
Scan
}
public BlockType getBlockType() {
return bt;
}
public SpecialCodeBlock(int debugId, BlockType bt, String blob, Range range) {
super(debugId, blob, range);
this.bt = bt;
super(debugId, bt, blob, range);
}
@Override
......@@ -30,7 +20,10 @@ public class SpecialCodeBlock extends CodeBlock
visitor.postVisit(this, data);
}
@Override protected String getNodeName() { return "SpecialCodeBlock"; }
@Override
protected String getNodeName() {
return "SpecialCodeBlock";
}
@Override
protected void putDescription(StringBuilder builder) {
......
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