Search in sources :

Example 36 with Pos

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

the class Macro method instantiate.

/**
 * Instantiate it.
 *
 * @param warnings - the list that will receive any warning we generate; can be
 *            null if we wish to ignore warnings
 */
Expr instantiate(Context cx, List<ErrorWarning> warnings) throws Err {
    if (cx.unrolls <= 0) {
        Pos p = span();
        return new ExprBad(p, toString(), new ErrorType(p, "Macro substitution too deep; possibly indicating an infinite recursion."));
    }
    if (params.size() != args.size())
        return this;
    Context cx2 = new Context(realModule, warnings, cx.unrolls - 1);
    for (int n = params.size(), i = 0; i < n; i++) {
        Expr tmp = args.get(i);
        if (!(tmp instanceof Macro))
            tmp = tmp.resolve(tmp.type(), warnings);
        cx2.put(params.get(i).label, tmp);
    }
    return cx2.check(body);
}
Also used : Context(edu.mit.csail.sdg.parser.CompModule.Context) ExprBad(edu.mit.csail.sdg.ast.ExprBad) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) Expr(edu.mit.csail.sdg.ast.Expr) Pos(edu.mit.csail.sdg.alloy4.Pos)

Example 37 with Pos

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

the class A4Solution method highLevelCore.

/**
 * If this solution is unsatisfiable and its unsat core is available, then
 * return the core; else return an empty set.
 */
public Pair<Set<Pos>, Set<Pos>> highLevelCore() {
    if (hCoreCache != null)
        return hCoreCache;
    Set<Pos> ans1 = new LinkedHashSet<Pos>(), ans2 = new LinkedHashSet<Pos>();
    if (hCore != null)
        for (Node f : hCore) {
            Object x = k2pos(f);
            if (x instanceof Pos) {
                // System.out.println("F: "+f+" at "+x+"\n");
                // System.out.flush();
                ans1.add((Pos) x);
            } else if (x instanceof Expr) {
                Expr expr = (Expr) x;
                Pos p = ((Expr) x).span();
                ans1.add(p);
                // System.out.flush();
                for (Func func : expr.findAllFunctions()) ans2.add(func.getBody().span());
            }
        }
    return hCoreCache = new Pair<Set<Pos>, Set<Pos>>(Collections.unmodifiableSet(ans1), Collections.unmodifiableSet(ans2));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) TupleSet(kodkod.instance.TupleSet) Expr(edu.mit.csail.sdg.ast.Expr) Pos(edu.mit.csail.sdg.alloy4.Pos) Func(edu.mit.csail.sdg.ast.Func) Node(kodkod.ast.Node)

Example 38 with Pos

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

the class CompLexer method alloy_string.

private final Symbol alloy_string(String txt) throws Err {
    Pos p = alloy_here(txt);
    if (!Version.experimental)
        throw new ErrorSyntax(p, "String literal is not currently supported.");
    StringBuilder sb = new StringBuilder(txt.length());
    for (int i = 0; i < txt.length(); i++) {
        char c = txt.charAt(i);
        if (c == '\r' || c == '\n')
            throw new ErrorSyntax(p, "String literal cannot span multiple lines; use \\n instead.");
        if (c == '\\') {
            i++;
            if (i >= txt.length())
                throw new ErrorSyntax(p, "String literal cannot end with a single \\");
            c = txt.charAt(i);
            if (c == 'n')
                c = '\n';
            else if (c != '\'' && c != '\"' && c != '\\')
                throw new ErrorSyntax(p, "String literal currenty only supports\nfour escape sequences: \\\\, \\n, \\\', and \\\"");
        }
        sb.append(c);
    }
    txt = sb.toString();
    if (txt.length() == 2)
        throw new ErrorSyntax(p, "Empty string is not allowed; try rewriting your model to use an empty set instead.");
    return new Symbol(CompSym.STR, p, ExprConstant.Op.STRING.make(p, txt));
}
Also used : ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Pos(edu.mit.csail.sdg.alloy4.Pos) Symbol(java_cup.runtime.Symbol)

Example 39 with Pos

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

the class CompLexer method alloy_binarynum.

private final Symbol alloy_binarynum(String txt) throws Err {
    Pos p = alloy_here(txt);
    int n = 0;
    try {
        txt = txt.substring(2).replaceAll("_", "");
        n = Integer.parseInt(txt, 2);
    } catch (NumberFormatException ex) {
        throw new ErrorSyntax(p, "The binary number " + txt + " " + ex);
    }
    return new Symbol(CompSym.NUMBER, p, ExprConstant.Op.NUMBER.make(p, n));
}
Also used : ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Pos(edu.mit.csail.sdg.alloy4.Pos) Symbol(java_cup.runtime.Symbol)

Example 40 with Pos

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

the class CompModule method addSig.

Sig addSig(String name, ExprVar par, List<ExprVar> parents, List<Decl> fields, Expr fact, Attr... attributes) throws Err {
    Sig obj;
    Pos pos = Pos.UNKNOWN.merge(WHERE.find(attributes));
    status = 3;
    dup(pos, name, true);
    String full = (path.length() == 0) ? "this/" + name : path + "/" + name;
    Pos subset = null, subsig = null;
    boolean exact = false;
    if (par != null) {
        if (par.label.equals("extends")) {
            subsig = par.span().merge(parents.get(0).span());
        } else {
            exact = !par.label.equals("in");
            subset = par.span();
            for (ExprVar p : parents) subset = p.span().merge(subset);
        }
    }
    attributes = Util.append(attributes, exact ? Attr.EXACT : null);
    if (subset != null) {
        attributes = Util.append(attributes, SUBSET.makenull(subset));
        List<Sig> newParents = new ArrayList<Sig>(parents == null ? 0 : parents.size());
        if (parents != null)
            for (ExprVar p : parents) newParents.add(new PrimSig(p.label, WHERE.make(p.pos)));
        obj = new SubsetSig(full, newParents, attributes);
    } else {
        attributes = Util.append(attributes, SUBSIG.makenull(subsig));
        PrimSig newParent = (parents != null && parents.size() > 0) ? (new PrimSig(parents.get(0).label, WHERE.make(parents.get(0).pos))) : UNIV;
        obj = new PrimSig(full, newParent, attributes);
    }
    sigs.put(name, obj);
    old2fields.put(obj, fields);
    old2appendedfacts.put(obj, fact);
    return obj;
}
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) ExprVar(edu.mit.csail.sdg.ast.ExprVar) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Pos(edu.mit.csail.sdg.alloy4.Pos) ArrayList(java.util.ArrayList) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

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