Commit ec9ad24b authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

TypeDefParser building a (sketch of a) parse tree and can pretty-print

.ls definition
parent 2e6a594f
......@@ -104,9 +104,20 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
Iterator<TypeDefListener> it = listeners.iterator();
while(it.hasNext()){
//it.next().onTypeDef(td);
it.next().onTypeDef(result);
//it.next().onTypeDef(typeDefs.get(d.getTypeIndex()));
//it.next().onTypeDef(result);
notifyListener(it.next(), result);
}
}
private void notifyListener(TypeDefListener l, ParsedTypeDef d) {
l.onTypeDef(d);
if(d instanceof ParsedSampleDef) {
for(ParsedTypeDef dep : ((ParsedSampleDef)d).getDependencies()) {
//do we want to change ParseTypeDef to have dependencies,
//and do recursion here?
//notifyListener(l, dep);
l.onTypeDef(dep);
}
}
}
......@@ -137,16 +148,122 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
// Sketch of result types "unparsed labcomm-file"
//
//
public abstract class ParsedType{
}
public class PrimitiveType extends ParsedType {
//TODO: instead, store tag and lookup in Constant.
private String name;
public PrimitiveType(String name) {
this.name = name;
}
public String toString() { return name;}
}
public class StructType extends ParsedType {
private Field fields[];
public StructType(int nFields) {
this.fields = new Field[nFields];
}
public void setField(int idx, Field f) {
fields[idx] = f;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("struct {\n");
for(Field f : fields) {
sb.append(f.toString());
sb.append(";\n");
}
sb.append("}");
return sb.toString();
}
}
public class Field {
private ParsedType type;
private String name;
public Field(String name, ParsedType type) {
this.name = name;
this.type = type;
}
public String toString() {
return type.toString() + " " + name;
}
}
public class ArrayType extends ParsedType {
private int idx[];
private ParsedType type;
public ArrayType(int idx[], ParsedType elementType) {
this.idx = idx;
this.type = elementType;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(type.toString());
for(int i : idx) {
sb.append("["+(i>0 ? i : "_")+"]");
}
return sb.toString();
}
}
public class UserType extends ParsedType {
private String name;
public String toString() {
return name;
}
public UserType(String name) {
this.name = name;
}
}
public class ParsedTypeDef{
private int idx;
private String name;
private String lcDecl;
private StringBuilder sb;
private ParsedType type;
public ParsedTypeDef(int idx, String name){
this.idx = idx;
this.name = name;
this.sb = new StringBuilder();
}
public ParsedTypeDef(int idx, String name, ParsedType type) {
this(idx, name);
this.type = type;
}
public int hashCode() {
return name.hashCode();
}
public boolean equals(Object o) {
if(! (o instanceof ParsedTypeDef)){
return false;
} else {
ParsedTypeDef other = (ParsedTypeDef) o;
return other.idx == idx && other.name.equals(name);
}
}
public void setType(ParsedType type) {
this.type = type;
}
public ParsedType getType() {
return type;
}
public int getIndex() {
......@@ -158,24 +275,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
}
public String toString() {
//return "TODO: pretty-print typedef";
return "FOOO: toString"+sb.toString();
}
public void addType(String type) {
System.out.println("addType: "+type);
sb.append(type);
sb.append("\n");
}
public void addFieldName(String name) {
System.out.println("addFieldName: "+name);
sb.append(name);
sb.append("\n");
}
public void addDim(String dim) {
System.out.println("addDim: "+dim);
sb.append(dim);
sb.append("\n");
return type.toString();
}
}
......@@ -183,7 +283,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
private HashSet<ParsedTypeDef> deps;
public ParsedSampleDef(ParsedTypeDef td) {
super(td.getIndex(), td.getName());
super(td.getIndex(), td.getName(), td.getType());
this.deps = new HashSet<ParsedTypeDef>();
}
......@@ -208,18 +308,6 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
return currentParsed;
}
public void addType(String type) {
currentParsed.addType(type);
}
public void addFieldName(String name) {
currentParsed.addFieldName(name);
}
public void addDim(String dim) {
currentParsed.addDim(dim);
}
private ParserState() {
typeStack = new LinkedList<TypeDef>();
}
......@@ -309,7 +397,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
// public void parseSignature(int typeIndex) throws IOException{
int typeIndex = td.getIndex();
System.out.println("parseSignature :"+td);
//System.out.println("parseSignature :"+td);
//ParserState s = new ParserState(typeIndex);
ParserState s = new ParserState(td);
......@@ -327,8 +415,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
return result;
}
private void parseArray(ParserState in) throws IOException {
in.addType("array");
private ArrayType parseArray(ParserState in) throws IOException {
int numIdx = in.decodePacked32();
int idx[] = new int[numIdx];
for(int i=0; i<numIdx; i++){
......@@ -336,62 +423,65 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
//System.out.println(idx[i]);
}
int type = in.decodePacked32();
lookupType(type, in);
ParsedType elementType = lookupType(type, in);
ArrayType result = new ArrayType(idx, elementType);
for(int i=0; i<numIdx; i++){
idx[i] = in.decodePacked32();
in.addDim( (idx[i] == 0 ? "_" : "0x"+Integer.toHexString(idx[i])));
}
return result;
}
private void parseStruct(ParserState in) throws IOException {
private StructType parseStruct(ParserState in) throws IOException {
//System.out.println("struct");
in.addType("struct");
int numFields = in.decodePacked32();
StructType result = new StructType(numFields);
for(int i=0; i<numFields; i++) {
parseField(in);
result.setField(i, parseField(in));
}
return result;
}
private void parseField(ParserState in) throws IOException {
private Field parseField(ParserState in) throws IOException {
String name = in.decodeString();
in.addFieldName(" "+name+" : ");
parseType(in);
return new Field(name, parseType(in));
}
private void lookupType(int tag, ParserState in) {
private ParsedType lookupType(int tag, ParserState in) {
ParsedType result = null;
switch(tag) {
case Constant.BOOLEAN:
in.addType("boolean");
result = new PrimitiveType("boolean");
break;
case Constant.BYTE:
in.addType("byte");
result = new PrimitiveType("byte");
break;
case Constant.SHORT:
in.addType("short");
result = new PrimitiveType("short");
break;
case Constant.INT:
in.addType("int");
result = new PrimitiveType("int");
break;
case Constant.LONG:
in.addType("long");
result = new PrimitiveType("long");
break;
case Constant.FLOAT:
in.addType("float");
result = new PrimitiveType("float");
break;
case Constant.DOUBLE:
in.addType("double");
result = new PrimitiveType("double");
break;
case Constant.STRING:
in.addType("string");
result = new PrimitiveType("string");
break;
default:
{
TypeDef td = typeDefs.get(tag);
//in.addType(td.getName());
result = new UserType(td.getName());
}
in.pushType(tag);
break;
}
return result;
}
private ParsedSampleDef parseSampleTypeDef(ParserState in) throws IOException {
......@@ -400,25 +490,27 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
}
private ParsedTypeDef parseTypeDef(ParserState in) throws IOException {
ParsedTypeDef result = in.newTypeDef();
parseType(in);
result.setType(parseType(in));
return result;
}
private void parseType(ParserState in) throws IOException {
private ParsedType parseType(ParserState in) throws IOException {
int tag = in.decodePacked32();
ParsedType result = null;
switch(tag) {
case 0:
System.out.println("SELF");
break;
case Constant.ARRAY:
parseArray(in);
result = parseArray(in);
break;
case Constant.STRUCT:
parseStruct(in);
result = parseStruct(in);
break;
default:
lookupType(tag, in);
result = lookupType(tag, in);
break;
}
return result;
}
}
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