public ITree filterAmbiguity(ITree ambCluster, Object environment) { ISet alts = (ISet) ambCluster.get("alternatives"); if (alts.size() == 0) { return null; } Environment env = (Environment) environment; Result<IValue> var = env.getFrameVariable("amb"); if (var != null && var instanceof ICallableValue) { Type type = RascalTypeFactory.getInstance().nonTerminalType(ambCluster); ICallableValue func = (ICallableValue) var; try { Result<IValue> result = func.call(new Type[] {TF.setType(type)}, new IValue[] {alts}, null); if (result.getType().isBottom()) { return ambCluster; } ITree r = (ITree) result.getValue(); if (TreeAdapter.isAmb(r)) { ISet returnedAlts = TreeAdapter.getAlternatives(r); if (returnedAlts.size() == 1) { return (ITree) returnedAlts.iterator().next(); } else if (returnedAlts.size() == 0) { return null; } else { return r; } } return (ITree) result.getValue(); } catch (ArgumentMismatch e) { return ambCluster; } } return ambCluster; }
public ISourceLocation resolveSourceLocation(ISourceLocation loc) { String scheme = loc.getScheme(); int pos; ICallableValue resolver = sourceResolvers.get(scheme); if (resolver == null) { for (char sep : new char[] {'+', ':'}) { pos = scheme.indexOf(sep); if (pos != -1) { scheme = scheme.substring(0, pos); } } resolver = sourceResolvers.get(scheme); if (resolver == null) { return loc; } } Type[] argTypes = new Type[] {TypeFactory.getInstance().sourceLocationType()}; IValue[] argValues = new IValue[] {loc}; return (ISourceLocation) resolver.call(argTypes, argValues, null).getValue(); }
private static Result<IValue> call(ICallableValue function, IList args) { try { int nrOfArgs = args.length(); Type[] types = new Type[nrOfArgs]; IValue[] actuals = new IValue[nrOfArgs]; for (int i = nrOfArgs - 1; i >= 0; --i) { IValue arg = args.get(i); types[i] = RascalTypeFactory.getInstance().nonTerminalType((IConstructor) arg); actuals[i] = arg; } return function.call(types, actuals, null); } catch (MatchFailed e) { return null; } catch (Failure f) { return null; } }