Search in sources :

Example 11 with ErrorFatal

use of edu.mit.csail.sdg.alloy4.ErrorFatal in project org.alloytools.alloy by AlloyTools.

the class CUP$CompParser$actions method alloy_parseStream.

static CompModule alloy_parseStream(List<Object> seenDollar, Map<String, String> loaded, Map<String, String> fc, CompModule root, int lineOffset, String filename, String prefix, int initialResolutionMode) throws Err, FileNotFoundException, IOException {
    Reader isr = null;
    try {
        if (root == null && prefix.length() != 0)
            throw new ErrorFatal("Internal error (parse subfile with root==null)");
        if (root != null && prefix.length() == 0)
            throw new ErrorFatal("Internal error (parse topfile with root!=null)");
        CompModule u = new CompModule(root, filename, prefix);
        if (!filename.endsWith(INT_LIB_NAME + ".als"))
            u.addOpen(null, null, ExprVar.make(null, INT_LIB_NAME), null, ExprVar.make(null, "integer"));
        u.resolution = initialResolutionMode;
        String content = fc != null ? fc.get(filename) : null;
        if (content == null && loaded != null)
            content = loaded.get(filename);
        if (content == null)
            content = Util.readAll(filename);
        if (loaded != null)
            loaded.put(filename, content);
        content = MarkdownHandler.strip(content);
        content = Util.convertLineBreak(content);
        isr = new StringReader(content);
        CompFilter s = new CompFilter(u, seenDollar, filename, lineOffset, new BufferedReader(isr));
        CompParser p = new CompParser(s);
        p.alloymodule = u;
        try {
            p.parse();
        } catch (Throwable ex) {
            if (ex instanceof Err)
                throw (Err) ex;
            throw new ErrorFatal("Parser Exception", ex);
        }
        return u;
    } finally {
        Util.close(isr);
    }
}
Also used : ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Err(edu.mit.csail.sdg.alloy4.Err) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader)

Example 12 with ErrorFatal

use of edu.mit.csail.sdg.alloy4.ErrorFatal in project org.alloytools.alloy by AlloyTools.

the class CompUtil method parseOneModule.

public static CompModule parseOneModule(String content) throws Err {
    try {
        Map<String, String> fc = new LinkedHashMap<String, String>();
        fc.put("", content);
        return CompUtil.parse(new ArrayList<Object>(), null, fc, null, 0, "", "", 1);
    } 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);
    }
}
Also used : ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Err(edu.mit.csail.sdg.alloy4.Err) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap)

Example 13 with ErrorFatal

use of edu.mit.csail.sdg.alloy4.ErrorFatal 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
 * @param initialResolutionMode - use 1 for the historical behavior, and 2 for
 *            Alloy 4.2's new "universal implicit this" name resolution behavior
 * @return the root CompModule 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, int initialResolutionMode) 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, initialResolutionMode);
        // if no sigs are defined by the user, add one
        if (root.getAllReachableUserDefinedSigs().isEmpty()) {
            root.addGhostSig();
        }
        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);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Err(edu.mit.csail.sdg.alloy4.Err) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Pos(edu.mit.csail.sdg.alloy4.Pos)

Example 14 with ErrorFatal

use of edu.mit.csail.sdg.alloy4.ErrorFatal in project org.alloytools.alloy by AlloyTools.

the class A4SolutionReader method parseTuple.

/**
 * Parse tuple.
 */
private Tuple parseTuple(XMLNode tuple, int arity) throws Err {
    Tuple ans = null;
    try {
        for (XMLNode sub : tuple) if (sub.is("atom")) {
            Tuple x = factory.tuple(sub.getAttribute("label"));
            if (ans == null)
                ans = x;
            else
                ans = ans.product(x);
        }
        if (ans == null)
            throw new ErrorFatal("Expecting: <tuple> <atom label=\"..\"/> .. </tuple>");
        if (ans.arity() != arity)
            throw new ErrorFatal("Expecting: tuple of arity " + arity + " but got tuple of arity " + ans.arity());
        return ans;
    } catch (Throwable ex) {
        throw new ErrorFatal("Expecting: <tuple> <atom label=\"..\"/> .. </tuple>", ex);
    }
}
Also used : ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) XMLNode(edu.mit.csail.sdg.alloy4.XMLNode) Tuple(kodkod.instance.Tuple)

Example 15 with ErrorFatal

use of edu.mit.csail.sdg.alloy4.ErrorFatal in project org.alloytools.alloy by AlloyTools.

the class StaticInstanceReader method setOrRel.

/**
 * Construct an AlloySet or AlloyRelation corresponding to the given expression.
 */
private void setOrRel(A4Solution sol, String label, Expr expr, boolean isPrivate, boolean isMeta) throws Err {
    for (List<PrimSig> ps : expr.type().fold()) {
        if (ps.size() == 1) {
            PrimSig t = ps.get(0);
            AlloySet set = makeSet(label, isPrivate, isMeta, sig(t));
            sets.add(set);
            for (A4Tuple tp : (A4TupleSet) (sol.eval(expr.intersect(t)))) {
                atom2sets.get(string2atom.get(tp.atom(0))).add(set);
            }
        } else {
            Expr mask = null;
            List<AlloyType> types = new ArrayList<AlloyType>(ps.size());
            for (int i = 0; i < ps.size(); i++) {
                types.add(sig(ps.get(i)));
                if (mask == null)
                    mask = ps.get(i);
                else
                    mask = mask.product(ps.get(i));
            }
            AlloyRelation rel = makeRel(label, isPrivate, isMeta, types);
            Set<AlloyTuple> ts = new LinkedHashSet<AlloyTuple>();
            for (A4Tuple tp : (A4TupleSet) (sol.eval(expr.intersect(mask)))) {
                AlloyAtom[] atoms = new AlloyAtom[tp.arity()];
                for (int i = 0; i < tp.arity(); i++) {
                    atoms[i] = string2atom.get(tp.atom(i));
                    if (atoms[i] == null)
                        throw new ErrorFatal("Unexpected XML inconsistency: cannot resolve atom " + tp.atom(i));
                }
                ts.add(new AlloyTuple(atoms));
            }
            rels.put(rel, ts);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) A4TupleSet(edu.mit.csail.sdg.translator.A4TupleSet) A4Tuple(edu.mit.csail.sdg.translator.A4Tuple) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Expr(edu.mit.csail.sdg.ast.Expr) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Aggregations

ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)29 Err (edu.mit.csail.sdg.alloy4.Err)8 Expr (edu.mit.csail.sdg.ast.Expr)6 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)6 Pos (edu.mit.csail.sdg.alloy4.Pos)4 ArrayList (java.util.ArrayList)4 LinkedHashMap (java.util.LinkedHashMap)4 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)3 ErrorType (edu.mit.csail.sdg.alloy4.ErrorType)3 ExprVar (edu.mit.csail.sdg.ast.ExprVar)3 Sig (edu.mit.csail.sdg.ast.Sig)3 IOException (java.io.IOException)3 LinkedHashSet (java.util.LinkedHashSet)3 BinaryExpression (kodkod.ast.BinaryExpression)3 CapacityExceededException (kodkod.engine.CapacityExceededException)3 HigherOrderDeclException (kodkod.engine.fol2sat.HigherOrderDeclException)3 ErrorAPI (edu.mit.csail.sdg.alloy4.ErrorAPI)2 Field (edu.mit.csail.sdg.ast.Sig.Field)2 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)2 A4Tuple (edu.mit.csail.sdg.translator.A4Tuple)2