use of org.spoofax.terms.typesmart.types.SortType in project spoofax by metaborg.
the class Typesmart method processSignature.
private void processSignature(IStrategoTerm sigDecls) {
for (IStrategoTerm decl : sigDecls) {
String declName = ((IStrategoAppl) decl).getName();
if (!declName.equals("Constructors")) {
continue;
}
next_constr: for (IStrategoTerm constr : decl.getSubterm(0)) {
String kind = ((IStrategoAppl) constr).getName();
String cname;
IStrategoAppl typeTerm;
if (kind.equals("OpDeclInj") || kind.equals("ExtOpDeclInj")) {
cname = "";
typeTerm = (IStrategoAppl) constr.getSubterm(0);
} else {
cname = ((IStrategoString) constr.getSubterm(0)).stringValue();
typeTerm = (IStrategoAppl) constr.getSubterm(1);
}
List<SortType> sortTypes;
if (typeTerm.getName().equals("ConstType")) {
// no constructor arguments
sortTypes = new ArrayList<>(1);
SortType t = extractSortType(typeTerm.getSubterm(0));
if (t == null) {
continue next_constr;
}
sortTypes.add(t);
} else if (typeTerm.getName().equals("FunType")) {
IStrategoTerm[] argTypes = typeTerm.getSubterm(0).getAllSubterms();
sortTypes = new ArrayList<>(argTypes.length + 1);
for (IStrategoTerm argType : argTypes) {
SortType t = extractSortType(argType.getSubterm(0));
if (t == null) {
continue next_constr;
}
sortTypes.add(t);
}
SortType t = extractSortType(typeTerm.getSubterm(1).getSubterm(0));
if (t == null) {
continue next_constr;
}
sortTypes.add(t);
} else {
throw new IllegalArgumentException("Found constructor declaration in unexpected format " + constr);
}
addConstructorSignature(cname, sortTypes);
}
}
}
use of org.spoofax.terms.typesmart.types.SortType in project spoofax by metaborg.
the class Typesmart method extractSortType.
private SortType extractSortType(IStrategoTerm sort) {
String kind = ((IStrategoAppl) sort).getName();
if (kind.equals("SortList") || kind.equals("SortListTl") || kind.equals("SortVar")) {
logger.error("Unsupported Stratego signature: " + sort);
return TAny.instance;
} else if (kind.equals("SortTuple")) {
IStrategoTerm[] kids = sort.getSubterm(0).getAllSubterms();
SortType[] sorts = new SortType[kids.length];
for (int i = 0; i < kids.length; i++) {
sorts[i] = extractSortType(kids[i]);
}
return new TTuple(sorts);
}
if (sort.getSubterm(0).getTermType() != IStrategoTerm.STRING) {
throw new IllegalArgumentException("Found type in unexpected format " + sort);
}
String sortName = ((IStrategoString) sort.getSubterm(0)).stringValue();
if (kind.equals("SortNoArgs") && sortName.equals(SortType.LEXICAL_SORT)) {
return TLexical.instance;
} else if (kind.equals("SortNoArgs") && sortName.equals(SortType.ANY_SORT)) {
return TAny.instance;
} else if (kind.equals("SortNoArgs")) {
return new TSort(sortName);
} else if (kind.equals("Sort") && sortName.equals("List")) {
SortType t = extractSortType(sort.getSubterm(1).getSubterm(0));
return t == null ? null : new TList(t);
} else if (kind.equals("Sort") && sortName.equals("Option")) {
SortType t = extractSortType(sort.getSubterm(1).getSubterm(0));
return t == null ? null : new TOption(t);
} else if (kind.equals("SortVar")) {
return null;
} else {
throw new IllegalArgumentException("Found type in unexpected format " + sort);
}
}
Aggregations