Search in sources :

Example 16 with Pos

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

the class ScopeComputer method setBitwidth.

/**
 * Modifies the integer bitwidth of this solution's model (and sets the max
 * sequence length to 0)
 */
private void setBitwidth(Pos pos, int newBitwidth) throws ErrorAPI, ErrorSyntax {
    if (newBitwidth < 0)
        throw new ErrorSyntax(pos, "Cannot specify a bitwidth less than 0");
    if (newBitwidth > 30)
        throw new ErrorSyntax(pos, "Cannot specify a bitwidth greater than 30");
    bitwidth = newBitwidth;
    maxseq = 0;
    sig2scope.put(SIGINT, bitwidth < 1 ? 0 : 1 << bitwidth);
    sig2scope.put(SEQIDX, 0);
}
Also used : ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax)

Example 17 with Pos

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

the class TranslateAlloyToKodkod method execute_commandFromBook.

/**
 * Based on the specified "options", execute one command and return the
 * resulting A4Solution object.
 * <p>
 * Note: it will first test whether the model fits one of the model from the
 * "Software Abstractions" book; if so, it will use the exact instance that was
 * in the book.
 *
 * @param rep - if nonnull, we'll send compilation diagnostic messages to it
 * @param sigs - the list of sigs; this list must be complete
 * @param cmd - the Command to execute
 * @param opt - the set of options guiding the execution of the command
 * @return null if the user chose "save to FILE" as the SAT solver, and nonnull
 *         if the solver finishes the entire solving and is either satisfiable
 *         or unsatisfiable.
 *         <p>
 *         If the return value X is satisfiable, you can call X.next() to get
 *         the next satisfying solution X2; and you can call X2.next() to get
 *         the next satisfying solution X3... until you get an unsatisfying
 *         solution.
 */
public static A4Solution execute_commandFromBook(A4Reporter rep, Iterable<Sig> sigs, Command cmd, A4Options opt) throws Err {
    if (rep == null)
        rep = A4Reporter.NOP;
    TranslateAlloyToKodkod tr = null;
    try {
        if (cmd.parent != null || !cmd.getGrowableSigs().isEmpty())
            return execute_greedyCommand(rep, sigs, cmd, opt);
        tr = new TranslateAlloyToKodkod(rep, opt, sigs, cmd);
        tr.makeFacts(cmd.formula);
        return tr.frame.solve(rep, cmd, new Simplifier(), true);
    } catch (UnsatisfiedLinkError ex) {
        throw new ErrorFatal("The required JNI library cannot be found: " + ex.toString().trim(), ex);
    } catch (CapacityExceededException ex) {
        throw rethrow(ex);
    } catch (HigherOrderDeclException ex) {
        Pos p = tr != null ? tr.frame.kv2typepos(ex.decl().variable()).b : Pos.UNKNOWN;
        throw new ErrorType(p, "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;
        else
            throw new ErrorFatal("Unknown exception occurred: " + ex, ex);
    }
}
Also used : CapacityExceededException(kodkod.engine.CapacityExceededException) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) Err(edu.mit.csail.sdg.alloy4.Err) Pos(edu.mit.csail.sdg.alloy4.Pos) HigherOrderDeclException(kodkod.engine.fol2sat.HigherOrderDeclException)

Example 18 with Pos

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

the class CUP$CompParser$actions method c.

private void c(boolean follow, ExprVar o, ExprVar x, ExprVar n, Expr e, List<CommandScope> s, ExprConstant c) throws Err {
    if (n != null)
        nod(n);
    int bitwidth = (-1), maxseq = (-1), overall = (-1), expects = (c == null ? -1 : c.num);
    Pos p = o.pos.merge(n != null ? n.span() : e.span());
    for (int i = s.size() - 1; i >= 0; i--) {
        Sig j = s.get(i).sig;
        int k = s.get(i).startingScope;
        p = p.merge(j.pos);
        if (j.label.equals("univ")) {
            overall = k;
            s.remove(i);
            continue;
        }
        if (j.label.equals("int")) {
            if (bitwidth >= 0)
                throw new ErrorSyntax(j.pos, "The bitwidth cannot be specified more than once.");
            bitwidth = k;
            s.remove(i);
            continue;
        }
        if (j.label.equals("seq")) {
            if (maxseq >= 0)
                throw new ErrorSyntax(j.pos, "The maximum sequence length cannot be specified more than once.");
            maxseq = k;
            s.remove(i);
            continue;
        }
    }
    if (n != null)
        parser.alloymodule.addCommand(follow, p, n, o.label.equals("c"), overall, bitwidth, maxseq, expects, s, x);
    else
        parser.alloymodule.addCommand(follow, p, e, o.label.equals("c"), overall, bitwidth, maxseq, expects, s, x);
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Pos(edu.mit.csail.sdg.alloy4.Pos)

Example 19 with Pos

use of edu.mit.csail.sdg.alloy4.Pos 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 20 with Pos

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

the class CompUtil method parseRecursively.

// =============================================================================================================//
/**
 * Helper method that recursively parse a file and all its included subfiles
 *
 * @param loaded - this stores the text files we've loaded while parsing; cannot
 *            be null
 * @param fc - if a file cannot be found, we consult this cache first before
 *            attempting to load it from disk/jar; cannot be null
 * @param pos - the position of the "open" statement
 * @param filename - the filename to open
 * @param root - the root module (this field is ignored if prefix=="")
 * @param prefix - the prefix for the file we are about to parse
 * @param thispath - the set of filenames involved in the current
 *            chain_of_file_opening
 */
private static CompModule parseRecursively(List<Object> seenDollar, Map<String, String> loaded, Map<String, String> fc, Pos pos, String filename, CompModule root, String prefix, Set<String> thispath, int initialResolution) throws Err, FileNotFoundException, IOException {
    // repeated.
    if (thispath.contains(filename))
        throw new ErrorSyntax(pos, "Circular dependency in module import. The file \"" + (new File(filename)).getName() + "\" is imported infinitely often.");
    thispath.add(filename);
    // No cycle detected so far. So now we parse the file.
    CompModule u = CompUtil.parse(seenDollar, loaded, fc, root, 0, filename, prefix, initialResolution);
    if (prefix.length() == 0)
        root = u;
    // Here, we recursively open the included files
    for (Open x : u.getOpens()) {
        String cp = Util.canon(computeModulePath(u.getModelName(), filename, x.filename)), content = fc.get(cp);
        try {
            if (content == null) {
                content = loaded.get(cp);
            }
            if (content == null) {
                content = fc.get(x.filename);
                if (content != null)
                    cp = x.filename;
            }
            if (content == null) {
                content = loaded.get(x.filename);
                if (content != null)
                    cp = x.filename;
            }
            if (content == null) {
                content = Util.readAll(cp);
            }
        } catch (IOException ex1) {
            try {
                String newCp = (Util.jarPrefix() + "models/" + x.filename + ".als").replace('/', File.separatorChar);
                content = Util.readAll(newCp);
                cp = newCp;
            } catch (IOException ex) {
                throw new ErrorSyntax(x.pos, "This module cannot be found.\nIt is not a built-in library module, and it cannot be found at \"" + cp + "\".\n");
            }
        }
        loaded.put(cp, content);
        x.setResolvedFilePath(cp);
        CompModule y = parseRecursively(seenDollar, loaded, fc, x.pos, cp, root, (prefix.length() == 0 ? x.alias : prefix + "/" + x.alias), thispath, initialResolution);
        x.connect(y);
    }
    // Remove this file from the CYCLE DETECTION
    thispath.remove(filename);
    // LIST.
    return u;
}
Also used : ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) IOException(java.io.IOException) File(java.io.File) Open(edu.mit.csail.sdg.parser.CompModule.Open)

Aggregations

Pos (edu.mit.csail.sdg.alloy4.Pos)28 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)24 ErrorType (edu.mit.csail.sdg.alloy4.ErrorType)16 ArrayList (java.util.ArrayList)12 Expr (edu.mit.csail.sdg.ast.Expr)10 Sig (edu.mit.csail.sdg.ast.Sig)10 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)10 Err (edu.mit.csail.sdg.alloy4.Err)9 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)8 TempList (edu.mit.csail.sdg.alloy4.ConstList.TempList)7 LinkedHashMap (java.util.LinkedHashMap)7 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)6 Command (edu.mit.csail.sdg.ast.Command)5 ExprVar (edu.mit.csail.sdg.ast.ExprVar)5 Func (edu.mit.csail.sdg.ast.Func)5 IOException (java.io.IOException)5 Symbol (java_cup.runtime.Symbol)5 Field (edu.mit.csail.sdg.ast.Sig.Field)4 ErrorWarning (edu.mit.csail.sdg.alloy4.ErrorWarning)3 HashMap (java.util.HashMap)3