Search in sources :

Example 1 with Err

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

the class CompModule method addModelName.

/**
 * Add the "MODULE" declaration.
 */
void addModelName(Pos pos, String moduleName, List<ExprVar> list) throws Err {
    if (status > 0)
        throw new ErrorSyntax(pos, "The \"module\" declaration must occur at the top,\n" + "and can occur at most once.");
    this.moduleName = moduleName;
    this.modulePos = pos;
    boolean nextIsExact = false;
    if (list != null)
        for (ExprVar expr : list) {
            if (expr == null) {
                nextIsExact = true;
                continue;
            }
            String name = expr.label;
            dup(expr.span(), name, true);
            if (path.length() == 0) {
                Sig newSig = addSig(name, null, null, null, null, WHERE.make(expr.span()));
                if (nextIsExact)
                    exactSigs.add(newSig);
            } else {
                params.put(name, null);
                if (nextIsExact)
                    exactParams.add(name);
            }
            nextIsExact = false;
        }
    // This line must be at the end, since "addSig" will
    this.status = 1;
// otherwise bump the status value to 3
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax)

Example 2 with Err

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

the class CompModule method addCommand.

// ============================================================================================================================//
/**
 * Add a COMMAND declaration.
 */
void addCommand(boolean followUp, Pos pos, ExprVar name, boolean check, int overall, int bitwidth, int seq, int exp, List<CommandScope> scopes, ExprVar label) throws Err {
    if (followUp && !Version.experimental)
        throw new ErrorSyntax(pos, "Syntax error encountering => symbol.");
    if (label != null)
        pos = Pos.UNKNOWN.merge(pos).merge(label.pos);
    status = 3;
    if (name.label.length() == 0)
        throw new ErrorSyntax(pos, "Predicate/assertion name cannot be empty.");
    if (name.label.indexOf('@') >= 0)
        throw new ErrorSyntax(pos, "Predicate/assertion name cannot contain \'@\'");
    String labelName = (label == null || label.label.length() == 0) ? name.label : label.label;
    Command parent = followUp ? commands.get(commands.size() - 1) : null;
    Command newcommand = new Command(pos, name, labelName, check, overall, bitwidth, seq, exp, scopes, null, name, parent);
    if (parent != null)
        commands.set(commands.size() - 1, newcommand);
    else
        commands.add(newcommand);
}
Also used : ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Command(edu.mit.csail.sdg.ast.Command)

Example 3 with Err

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

the class CompModule method parseOneExpressionFromString.

/**
 * Parse one expression by starting fromt this module as the root module.
 */
@Override
public Expr parseOneExpressionFromString(String input) throws Err, FileNotFoundException, IOException {
    Map<String, String> fc = new LinkedHashMap<String, String>();
    // We prepend the line "run{"
    fc.put("", "run {\n" + input + "}");
    CompModule m = CompUtil.parse(new ArrayList<Object>(), null, fc, null, -1, "", "", 1);
    if (m.funcs.size() == 0)
        throw new ErrorSyntax("The input does not correspond to an Alloy expression.");
    Expr body = m.funcs.values().iterator().next().get(0).getBody();
    Context cx = new Context(this, null);
    body = cx.check(body);
    body = body.resolve(body.type(), null);
    if (body.errors.size() > 0)
        throw body.errors.pick();
    else
        return body;
}
Also used : ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Expr(edu.mit.csail.sdg.ast.Expr) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with Err

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

the class CompModule method resolveAll.

// ============================================================================================================================//
/**
 * This method resolves the entire world; NOTE: if it throws an exception, it
 * may leave the world in an inconsistent state!
 */
static CompModule resolveAll(final A4Reporter rep, final CompModule root) throws Err {
    final List<ErrorWarning> warns = new ArrayList<ErrorWarning>();
    for (CompModule m : root.getAllReachableModules()) root.allModules.add(m);
    resolveParams(rep, root.allModules);
    resolveModules(rep, root.allModules);
    for (CompModule m : root.allModules) for (Sig s : m.sigs.values()) root.sig2module.put(s, m);
    // Resolves SigAST -> Sig, and topologically sort the sigs into the
    // "sorted" array
    root.new2old.put(UNIV, UNIV);
    root.new2old.put(SIGINT, SIGINT);
    root.new2old.put(SEQIDX, SEQIDX);
    root.new2old.put(STRING, STRING);
    root.new2old.put(NONE, NONE);
    HashSet<Object> topo = new HashSet<Object>();
    for (CompModule m : root.allModules) for (Sig s : m.sigs.values()) resolveSig(root, topo, s);
    // (since fields in subsigs are allowed to refer to parent's fields)
    for (Sig oldS : root.new2old.keySet()) resolveFieldDecl(root, rep, oldS, warns, false);
    // Typecheck the function declarations
    JoinableList<Err> errors = new JoinableList<Err>();
    for (CompModule x : root.allModules) errors = x.resolveFuncDecls(rep, errors, warns);
    if (!errors.isEmpty())
        throw errors.pick();
    // Typecheck the defined fields
    for (Sig oldS : root.new2old.keySet()) resolveFieldDecl(root, rep, oldS, warns, true);
    if (Version.experimental && root.seenDollar)
        resolveMeta(root);
    // Reject name clash
    rejectNameClash(root.allModules);
    // to function declarations)
    for (CompModule x : root.allModules) {
        errors = x.resolveFuncBody(rep, errors, warns);
        errors = x.resolveAssertions(rep, errors, warns);
        errors = x.resolveFacts(root, rep, errors, warns);
        // root module's list of exact sigs
        for (String n : x.exactParams) {
            Sig sig = x.params.get(n);
            if (sig != null)
                root.exactSigs.add(sig);
        }
    }
    if (!errors.isEmpty())
        throw errors.pick();
    // Typecheck the run/check commands (which can refer to function bodies
    // and assertions)
    root.resolveCommands(root.getAllReachableFacts());
    if (!errors.isEmpty())
        throw errors.pick();
    for (ErrorWarning w : warns) rep.warning(w);
    for (Sig s : root.exactSigs) rep.debug("Forced to be exact: " + s + "\n");
    return root;
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Err(edu.mit.csail.sdg.alloy4.Err) ArrayList(java.util.ArrayList) ErrorWarning(edu.mit.csail.sdg.alloy4.ErrorWarning) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) JoinableList(edu.mit.csail.sdg.alloy4.JoinableList)

Example 5 with Err

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

the class CompModule method resolveSig.

/**
 * The given Sig will now point to a nonnull Sig.
 */
private static Sig resolveSig(CompModule res, Set<Object> topo, Sig oldS) throws Err {
    if (res.new2old.containsKey(oldS))
        return oldS;
    Sig realSig;
    final Pos pos = oldS.pos;
    final CompModule u = res.sig2module.get(oldS);
    final String name = base(oldS);
    final String fullname = (u.path.length() == 0) ? ("this/" + name) : (u.path + "/" + name);
    if (!topo.add(oldS))
        throw new ErrorType(pos, "Sig " + oldS + " is involved in a cyclic inheritance.");
    if (oldS instanceof SubsetSig) {
        List<Sig> parents = new ArrayList<Sig>();
        for (Sig n : ((SubsetSig) oldS).parents) {
            Sig parentAST = u.getRawSIG(n.pos, n.label);
            if (parentAST == null)
                throw new ErrorSyntax(n.pos, "The sig \"" + n.label + "\" cannot be found.");
            parents.add(resolveSig(res, topo, parentAST));
        }
        realSig = new SubsetSig(fullname, parents, oldS.attributes.toArray(new Attr[0]));
    } else {
        Sig sup = ((PrimSig) oldS).parent;
        Sig parentAST = u.getRawSIG(sup.pos, sup.label);
        if (parentAST == null)
            throw new ErrorSyntax(sup.pos, "The sig \"" + sup.label + "\" cannot be found.");
        Sig parent = resolveSig(res, topo, parentAST);
        if (!(parent instanceof PrimSig))
            throw new ErrorSyntax(sup.pos, "Cannot extend the subset signature \"" + parent + "\".\n" + "A signature can only extend a toplevel signature or a subsignature.");
        PrimSig p = (PrimSig) parent;
        realSig = new PrimSig(fullname, p, oldS.attributes.toArray(new Attr[0]));
    }
    res.new2old.put(realSig, oldS);
    res.sig2module.put(realSig, u);
    for (CompModule m : res.allModules) {
        for (Map.Entry<String, Sig> e : m.sigs.entrySet()) if (e.getValue() == oldS)
            e.setValue(realSig);
        for (Map.Entry<String, Sig> e : m.params.entrySet()) if (e.getValue() == oldS)
            e.setValue(realSig);
    }
    if (res.exactSigs.remove(oldS))
        res.exactSigs.add(realSig);
    return realSig;
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) Pos(edu.mit.csail.sdg.alloy4.Pos) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Aggregations

ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)32 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)28 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)27 Err (edu.mit.csail.sdg.alloy4.Err)25 Expr (edu.mit.csail.sdg.ast.Expr)25 Sig (edu.mit.csail.sdg.ast.Sig)24 Pos (edu.mit.csail.sdg.alloy4.Pos)16 ErrorType (edu.mit.csail.sdg.alloy4.ErrorType)15 ExprVar (edu.mit.csail.sdg.ast.ExprVar)15 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)15 ArrayList (java.util.ArrayList)15 Field (edu.mit.csail.sdg.ast.Sig.Field)12 LinkedHashMap (java.util.LinkedHashMap)10 IOException (java.io.IOException)9 TempList (edu.mit.csail.sdg.alloy4.ConstList.TempList)8 Func (edu.mit.csail.sdg.ast.Func)8 TupleSet (kodkod.instance.TupleSet)8 Command (edu.mit.csail.sdg.ast.Command)7 ErrorAPI (edu.mit.csail.sdg.alloy4.ErrorAPI)6 XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)5