return null;
}
@SuppressWarnings("unused")
private IValue buildTerm(IMap t, Type type) {
INode result;
IValue key = t.get(nameKey);
if (debug && key != null)
System.err.println("builterm key=" + key);
if (key == null) {
if (type.isMap() || t.getType().isMap())
return _buildTerm(t, type);
else
return t;
}
/* Abstract Data Type */
final String funname = ((IString) key).getValue();
if (debug && key != null)
System.err.println("builterm funname=" + funname);
IList rs = vf.list(tf.valueType());
final Iterator<IValue> args = ((IList) t.get(argKey)).iterator();
while (args.hasNext()) {
IValue arg = (IValue) args.next();
arg = buildTerm(arg, type);
rs = rs.append(arg);
}
IValue[] a = new IValue[rs.length()];
Type[] b = new Type[rs.length()];
{
int i = 0;
for (IValue c : rs) {
a[i] = c;
b[i] = c.getType();
i++;
}
}
HashMap<String, IValue> map = new HashMap<String, IValue>();
IMap annoMap = (IMap) t.get(annoKey);
if (annoMap != null) {
Iterator<IValue> iterator = annoMap.iterator();
while (iterator.hasNext()) {
IValue k = iterator.next();
String ky = ((IString) k).getValue();
IValue v = annoMap.get(k);
map.put(ky, v);
}
}
// keyword parameters
HashMap<String, IValue> kwmap = new HashMap<String, IValue>();
Iterator<IValue> iterator = t.iterator();
while (iterator.hasNext()) {
IValue k = iterator.next();
String ky = ((IString) k).getValue();
if(!(k.equals(nameKey) || k.equals(argKey) || k.equals(annoKey))){
IValue v = t.get(k);
kwmap.put(ky, v);
}
}
if (funname.equals("#rat")) {
int dn = ((IInteger) a[0]).intValue(), nm = ((IInteger) a[1])
.intValue();
return vf.rational(dn, nm);
}
if (funname.equals("#tuple")) {
return vf.tuple(a);
}
if (funname.equals("#loc")) {
try {
final URI uri = URIUtil.createFromEncoded(((IString) a[0]).getValue());
return vf.sourceLocation(uri);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
if (funname.equals("#set")) {
return a.length == 0 ? vf.set(tf.valueType()) : vf.set(a);
}
if (funname.equals("#map")) {
IMapWriter w = vf.mapWriter();
for (int i = 0; i < a.length; i++) {
w.put(((ITuple) a[i]).get(0), ((ITuple) a[i]).get(1));
}
return w.done();
}
if (funname.equals("#datetime"))
return dateTime(((IString) a[0]).getValue());
Type computed = tf.tupleType(b);
if (debug)
System.err.println("lookupFirstConstructor:" + funname + " "
+ computed + " " + type);
Type found = null;
if (type.isAbstractData())
found = ts.lookupConstructor(type, funname, computed);
if (found == null)
found = ts.lookupFirstConstructor(funname, computed);
ArrayList<IValue[]> r = new ArrayList<IValue[]>();
ArrayList<Type> f = new ArrayList<Type>();
if (found == null) {
Set<Type> nodes = ts.lookupConstructors(funname);
if (debug)
System.err.println("Number of nodes found:" + nodes.size());
Iterator<Type> it = nodes.iterator();
while (it.hasNext()) {
found = it.next();
IValue[] v = matched(found, a);
if (v == null)
break;
r.add(v);
f.add(found);
}
if (r.size() != 1)
found = null;
else {
found = f.get(0);
a = r.get(0);
}
}
if (debug)
System.err.println("JSonReader found:" + r.size());
if (debug)
System.err.println("node2=" + found);
if (found == null) {
result = vf.node(funname, a);
} else {
if (found.isAliased())
found = found.getAliased();
result = vf.constructor(found, a);
if (debug)
System.err.println("JSonReader result:" + result);
}
if (annoMap != null)
result = result.asAnnotatable().setAnnotations(map);
if(kwmap.size() > 0){
result = result.asWithKeywordParameters().setParameters(kwmap);
}
return result;
}