use of edu.mit.csail.sdg.alloy4.Err in project org.alloytools.alloy by AlloyTools.
the class TranslateAlloyToKodkod method alloy2kodkod.
/**
* Translate the Alloy expression into an equivalent Kodkod Expression or
* IntExpression or Formula object.
*
* @param sol - an existing satisfiable A4Solution object
* @param expr - this is the Alloy expression we want to translate
*/
public static Object alloy2kodkod(A4Solution sol, Expr expr) throws Err {
if (expr.ambiguous && !expr.errors.isEmpty())
expr = expr.resolve(expr.type(), null);
if (!expr.errors.isEmpty())
throw expr.errors.pick();
TranslateAlloyToKodkod tr = new TranslateAlloyToKodkod(sol.getBitwidth(), sol.unrolls(), sol.a2k(), sol.s2k());
Object ans;
try {
ans = tr.visitThis(expr);
} catch (UnsatisfiedLinkError ex) {
throw new ErrorFatal("The required JNI library cannot be found: " + ex.toString().trim());
} catch (CapacityExceededException ex) {
throw rethrow(ex);
} catch (HigherOrderDeclException ex) {
throw new ErrorType("Analysis cannot be performed since it requires higher-order quantification that could not be skolemized.");
} catch (Throwable ex) {
if (ex instanceof Err)
throw (Err) ex;
throw new ErrorFatal("Unknown exception occurred: " + ex, ex);
}
if ((ans instanceof IntExpression) || (ans instanceof Formula) || (ans instanceof Expression))
return ans;
throw new ErrorFatal("Unknown internal error encountered in the evaluator.");
}
use of edu.mit.csail.sdg.alloy4.Err in project org.alloytools.alloy by AlloyTools.
the class TranslateAlloyToKodkod method visit.
/**
* {@inheritDoc}
*/
@Override
public Object visit(ExprCall x) throws Err {
final Func f = x.fun;
final Object candidate = f.count() == 0 ? cacheForConstants.get(f) : null;
if (candidate != null)
return candidate;
final Expr body = f.getBody();
if (body.type().arity() < 0 || body.type().arity() != f.returnDecl.type().arity())
throw new ErrorType(body.span(), "Function return value not fully resolved.");
final int n = f.count();
int maxRecursion = unrolls;
for (Func ff : current_function) if (ff == f) {
if (maxRecursion < 0) {
throw new ErrorSyntax(x.span(), "" + f + " cannot call itself recursively!");
}
if (maxRecursion == 0) {
Type t = f.returnDecl.type();
if (t.is_bool)
return Formula.FALSE;
if (t.is_int())
return IntConstant.constant(0);
int i = t.arity();
Expression ans = Expression.NONE;
while (i > 1) {
ans = ans.product(Expression.NONE);
i--;
}
return ans;
}
maxRecursion--;
}
Env<ExprVar, Object> newenv = new Env<ExprVar, Object>();
for (int i = 0; i < n; i++) newenv.put(f.get(i), cset(x.args.get(i)));
Env<ExprVar, Object> oldenv = env;
env = newenv;
current_function.add(f);
Object ans = visitThis(body);
env = oldenv;
current_function.remove(current_function.size() - 1);
if (ans instanceof Formula)
k2pos((Formula) ans, x);
if (f.count() == 0)
cacheForConstants.put(f, ans);
return ans;
}
use of edu.mit.csail.sdg.alloy4.Err in project org.alloytools.alloy by AlloyTools.
the class CUP$CompParser$actions method syntax_error.
@Override
public void syntax_error(Symbol x) throws Err {
Map<Integer, String> ch = new LinkedHashMap<Integer, String>();
ch.put(CompSym.ARROW, "->");
ch.put(CompSym.ANY_ARROW_SOME, "->");
ch.put(CompSym.ANY_ARROW_ONE, "->");
ch.put(CompSym.ANY_ARROW_LONE, "->");
ch.put(CompSym.SOME_ARROW_ANY, "some");
ch.put(CompSym.SOME_ARROW_SOME, "some");
ch.put(CompSym.SOME_ARROW_ONE, "some");
ch.put(CompSym.SOME_ARROW_LONE, "some");
ch.put(CompSym.ONE_ARROW_ANY, "one");
ch.put(CompSym.ONE_ARROW_SOME, "one");
ch.put(CompSym.ONE_ARROW_ONE, "one");
ch.put(CompSym.ONE_ARROW_LONE, "one");
ch.put(CompSym.LONE_ARROW_ANY, "lone");
ch.put(CompSym.LONE_ARROW_SOME, "lone");
ch.put(CompSym.LONE_ARROW_ONE, "lone");
ch.put(CompSym.LONE_ARROW_LONE, "lone");
ch.put(CompSym.INTADD, "fun");
ch.put(CompSym.INTSUB, "fun");
ch.put(CompSym.INTMUL, "fun");
ch.put(CompSym.INTDIV, "fun");
ch.put(CompSym.INTREM, "fun");
ch.put(CompSym.INTMIN, "fun");
ch.put(CompSym.INTMAX, "fun");
ch.put(CompSym.INTNEXT, "fun");
ch.put(CompSym.TOTALORDER, "pred");
ch.put(CompSym.ABSTRACT, "abstract");
ch.put(CompSym.ALL, "all");
ch.put(CompSym.ALL2, "all");
ch.put(CompSym.AMPERSAND, "&");
ch.put(CompSym.AND, "&&");
ch.put(CompSym.AS, "as");
ch.put(CompSym.ASSERT, "assert");
ch.put(CompSym.AT, "@");
ch.put(CompSym.BAR, "|");
ch.put(CompSym.BUT, "but");
ch.put(CompSym.CARET, "^");
ch.put(CompSym.CHECK, "check");
ch.put(CompSym.COLON, ":");
ch.put(CompSym.COMMA, ", ");
ch.put(CompSym.DISJ, "disj");
ch.put(CompSym.DOMAIN, "<:");
ch.put(CompSym.DOT, ".");
ch.put(CompSym.ELSE, "else");
ch.put(CompSym.ENUM, "enum");
ch.put(CompSym.EQUALS, "=");
ch.put(CompSym.EXACTLY, "exactly");
ch.put(CompSym.EXH, "exh");
ch.put(CompSym.EXPECT, "expect");
ch.put(CompSym.EXTENDS, "extends");
ch.put(CompSym.FACT, "fact");
ch.put(CompSym.FOR, "for");
ch.put(CompSym.FUN, "fun");
ch.put(CompSym.GT, ">");
ch.put(CompSym.GTE, ">=");
ch.put(CompSym.HASH, "#");
ch.put(CompSym.IDEN, "iden");
ch.put(CompSym.IFF, "iff");
ch.put(CompSym.IMPLIES, "=>");
ch.put(CompSym.IN, "in");
ch.put(CompSym.INT, "int");
ch.put(CompSym.LBRACE, "{");
ch.put(CompSym.LBRACKET, "[");
ch.put(CompSym.LET, "let");
ch.put(CompSym.LONE2, "lone");
ch.put(CompSym.LONE, "lone");
ch.put(CompSym.LPAREN, "(");
ch.put(CompSym.LT, "<");
ch.put(CompSym.LTE, "<=");
ch.put(CompSym.MINUS, "-");
ch.put(CompSym.MODULE, "module");
ch.put(CompSym.NO2, "no");
ch.put(CompSym.NO, "no");
ch.put(CompSym.NONE, "none");
ch.put(CompSym.NOT, "!");
ch.put(CompSym.NOTEQUALS, "!");
ch.put(CompSym.NOTGT, "!");
ch.put(CompSym.NOTGTE, "!");
ch.put(CompSym.NOTIN, "!");
ch.put(CompSym.NOTLT, "!");
ch.put(CompSym.NOTLTE, "!");
ch.put(CompSym.ONE2, "one");
ch.put(CompSym.ONE, "one");
ch.put(CompSym.OPEN, "open");
ch.put(CompSym.OR, "||");
ch.put(CompSym.PART, "part");
ch.put(CompSym.PLUS, "+");
ch.put(CompSym.PLUSPLUS, "++");
ch.put(CompSym.PRED, "pred");
ch.put(CompSym.PRIVATE, "private");
ch.put(CompSym.RANGE, ":>");
ch.put(CompSym.RBRACE, "}");
ch.put(CompSym.RBRACKET, "]");
ch.put(CompSym.RPAREN, ")");
ch.put(CompSym.RUN, "run");
ch.put(CompSym.SEQ, "seq");
ch.put(CompSym.SET, "set");
ch.put(CompSym.SHL, "<<");
ch.put(CompSym.SHR, ">>>");
ch.put(CompSym.SHA, ">>");
ch.put(CompSym.SIG, "sig");
ch.put(CompSym.SIGINT, "Int");
ch.put(CompSym.SLASH, "/");
ch.put(CompSym.SOME2, "some");
ch.put(CompSym.SOME, "some");
ch.put(CompSym.STAR, "*");
ch.put(CompSym.STRING, "String");
ch.put(CompSym.SUM2, "sum");
ch.put(CompSym.SUM, "sum");
ch.put(CompSym.THIS, "this");
ch.put(CompSym.TILDE, "~");
ch.put(CompSym.UNIV, "univ");
ch.put(CompSym.ID, "NAME");
ch.put(CompSym.NUMBER, "NUMBER");
ch.put(CompSym.STR, "STRING");
TreeSet<String> list = new TreeSet<String>();
Pos p = Pos.UNKNOWN;
if (x != null && x.value instanceof Pos)
p = (Pos) (x.value);
else if (x != null && x.value instanceof Expr)
p = ((Expr) (x.value)).pos;
else if (x != null)
p = x.pos;
if (!stack.empty())
for (Map.Entry<Integer, String> e : ch.entrySet()) {
int key = e.getKey(), act = get_action(((Symbol) stack.peek()).parse_state, key);
if (act == 0)
continue;
try {
if (act > 0 || alloy_confirm(key))
list.add(e.getValue());
} catch (Throwable ex) {
// If the parser is really really confused, alloy_confirm() could fail with array out-of-bound exception, etc.
}
}
String result = "There are " + list.size() + " possible tokens that can appear here:\n";
for (String item : list) result = result + item + " ";
throw new ErrorSyntax(p, (list.size() != 0) ? result : "");
}
use of edu.mit.csail.sdg.alloy4.Err in project org.alloytools.alloy by AlloyTools.
the class CompUtil method parseEverything_fromFile.
// =============================================================================================================//
/**
* Read everything from "file" and parse it; if it mentions submodules, open
* them and parse them too.
*
* @param rep - if nonnull, we will report compilation progress messages to it
* @param loaded - a cache of files that have been pre-fetched (can be null if
* there were no prefetching)
* @param filename - the main module we are parsing
* @return the root Module which contains pointers to all submodules
* @throws Err if an error occurred
* <p>
* And if loaded!=null, it will contain all the files needed for
* this parse, and furthermore, other entries will be deleted.
*/
public static CompModule parseEverything_fromFile(A4Reporter rep, Map<String, String> loaded, String filename) throws Err {
try {
filename = Util.canon(filename);
Set<String> thispath = new LinkedHashSet<String>();
if (loaded == null)
loaded = new LinkedHashMap<String, String>();
Map<String, String> fc = new LinkedHashMap<String, String>(loaded);
loaded.clear();
List<Object> seenDollar = new ArrayList<Object>();
CompModule root = parseRecursively(seenDollar, loaded, fc, new Pos(filename, 1, 1), filename, null, "", thispath, 1);
root.seenDollar = seenDollar.size() > 0;
return CompModule.resolveAll(rep == null ? A4Reporter.NOP : rep, root);
} catch (FileNotFoundException ex) {
throw new ErrorSyntax("File cannot be found.\n" + ex.getMessage(), ex);
} catch (IOException ex) {
throw new ErrorFatal("IOException occurred: " + ex.getMessage(), ex);
} catch (Throwable ex) {
if (ex instanceof Err)
throw (Err) ex;
else
throw new ErrorFatal("Unknown exception occurred: " + ex, ex);
}
}
use of edu.mit.csail.sdg.alloy4.Err in project org.alloytools.alloy by AlloyTools.
the class CompUtil method areIntsUsed.
// =============================================================================================================//
/**
* Whether or not Int appears in the relation types found in these sigs
*/
public static boolean areIntsUsed(Iterable<Sig> sigs, Command cmd) {
/* check for Int-typed relations */
for (Sig s : sigs) {
for (Field f : s.getFields()) {
for (ProductType pt : f.type()) {
for (int k = 0; k < pt.arity(); k++) {
if (pt.get(k) == SIGINT || pt.get(k) == SEQIDX)
return true;
}
}
}
}
if (cmd == null)
return false;
/* check expressions; look for CAST2SIGING (Int[]) */
try {
Object intTriggerNode;
intTriggerNode = cmd.formula.accept(new VisitQueryOnce<Object>() {
@Override
public Object visit(ExprCall x) throws Err {
// Int[]
if (x.fun.label.startsWith("integer/"))
return null;
return super.visit(x);
}
@Override
public Object visit(ExprUnary x) throws Err {
if (x.op == Op.CAST2SIGINT)
return x;
return super.visit(x);
}
});
if (intTriggerNode != null)
return true;
} catch (Err e) {
}
return false;
}
Aggregations