Search in sources :

Example 11 with ErrorSyntax

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

the class CompModule method addEnum.

/**
 * Add an enumeration.
 */
void addEnum(Pos pos, Pos priv, ExprVar name, List<ExprVar> atoms, Pos closingBracket) throws Err {
    ExprVar EXTENDS = ExprVar.make(null, "extends");
    ExprVar THIS = ExprVar.make(null, "this/" + name);
    List<ExprVar> THESE = Arrays.asList(THIS);
    if (atoms == null || atoms.size() == 0)
        throw new ErrorSyntax(pos, "Enumeration must contain at least one name.");
    addSig(name.label, null, null, null, null, WHERE.make(name.pos), ABSTRACT.make(name.pos), PRIVATE.makenull(priv), Attr.ENUM);
    for (ExprVar a : atoms) addSig(a.label, EXTENDS, THESE, null, null, WHERE.make(a.pos), ONE.make(a.pos), PRIVATE.makenull(priv));
    int oldStatus = status;
    status = 0;
    try {
        addOpen(null, null, ExprVar.make(pos, "util/ordering"), Arrays.asList(THIS), null);
    } finally {
        status = oldStatus;
    }
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax)

Example 12 with ErrorSyntax

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

the class CompModule method addOpen.

/**
 * Add an OPEN declaration.
 */
void addOpen(Pos pos, Pos isPrivate, ExprVar name, List<ExprVar> args, ExprVar alias) throws Err {
    if (status > 2)
        throw new ErrorSyntax(pos, "The \"open\" declaration must occur before any\n" + "sig/pred/fun/fact/assert/check/run command.");
    String as = (alias == null ? "" : alias.label);
    if (name.label.length() == 0)
        throw new ErrorSyntax(name.span(), "The filename cannot be empty.");
    if (as.indexOf('$') >= 0)
        throw new ErrorSyntax(alias == null ? null : alias.span(), "Alias must not contain the \'$\' character");
    if (as.indexOf('@') >= 0)
        throw new ErrorSyntax(alias == null ? null : alias.span(), "Alias must not contain the \'@\' character");
    if (as.indexOf('/') >= 0)
        throw new ErrorSyntax(alias == null ? null : alias.span(), "Alias must not contain the \'/\' character");
    if (as.length() == 0) {
        as = "open$" + (1 + opens.size());
        if (args == null || args.size() == 0) {
            for (int i = 0; ; i++) {
                if (i >= name.label.length()) {
                    as = name.label;
                    break;
                }
                char c = name.label.charAt(i);
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
                    continue;
                if (i == 0)
                    break;
                if (!(c >= '0' && c <= '9') && c != '_' && c != '\'' && c != '\"')
                    break;
            }
        }
    }
    final TempList<String> newlist = new TempList<String>(args == null ? 0 : args.size());
    if (args != null)
        for (int i = 0; i < args.size(); i++) {
            ExprVar arg = args.get(i);
            if (arg.label.length() == 0)
                throw new ErrorSyntax(arg.span(), "Argument cannot be empty.");
            if (arg.label.indexOf('@') >= 0)
                throw new ErrorSyntax(arg.span(), "Argument cannot contain the \'@\' chracter.");
            newlist.add(arg.label);
        }
    Open x = opens.get(as);
    if (x != null) {
        // we allow this, especially because of util/sequniv
        if (x.args.equals(newlist.makeConst()) && x.filename.equals(name.label))
            return;
        throw new ErrorSyntax(pos, "You cannot import two different modules\n" + "using the same alias.");
    }
    List<Expr> expressions = new ArrayList<>(args == null ? Collections.emptySet() : args);
    expressions.add(0, name);
    expressions.add(alias);
    x = new Open(pos, isPrivate != null, as, newlist.makeConst(), name.label, expressions);
    opens.put(as, x);
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) TempList(edu.mit.csail.sdg.alloy4.ConstList.TempList) Expr(edu.mit.csail.sdg.ast.Expr) ArrayList(java.util.ArrayList)

Example 13 with ErrorSyntax

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

the class CompModule method resolveFieldDecl.

// ============================================================================================================================//
private static void resolveFieldDecl(CompModule res, final A4Reporter rep, final Sig s, final List<ErrorWarning> warns, boolean defined) throws Err {
    // When typechecking each field:
    // * it is allowed to refer to earlier fields in the same SIG or in any
    // visible ancestor sig
    // * it is allowed to refer to visible sigs
    // * it is NOT allowed to refer to any predicate or function
    // For example, if A.als opens B.als, and B/SIGX extends A/SIGY,
    // then B/SIGX's fields cannot refer to A/SIGY, nor any fields in
    // A/SIGY)
    final List<Decl> oldDecls = res.old2fields.get(res.new2old.get(s));
    if (oldDecls == null)
        return;
    final CompModule m = res.sig2module.get(s);
    final Context cx = new Context(m, warns);
    final ExprHasName dup = Decl.findDuplicateName(oldDecls);
    if (dup != null)
        throw new ErrorSyntax(dup.span(), "sig \"" + s + "\" cannot have 2 fields named \"" + dup.label + "\"");
    for (final Decl d : oldDecls) {
        if (d.expr.mult() != ExprUnary.Op.EXACTLYOF) {
            if (defined)
                continue;
        } else {
            if (!defined)
                continue;
        }
        // The name "this" does matter, since the parser and the typechecker
        // both refer to it as "this"
        cx.rootfield = d;
        cx.rootsig = s;
        cx.put("this", s.decl.get());
        Expr bound = cx.check(d.expr).resolve_as_set(warns);
        cx.remove("this");
        String[] names = new String[d.names.size()];
        for (int i = 0; i < names.length; i++) names[i] = d.names.get(i).label;
        Field[] fields = s.addTrickyField(d.span(), d.isPrivate, d.disjoint, d.disjoint2, null, names, bound);
        for (Field f : fields) {
            rep.typecheck("Sig " + s + ", Field " + f.label + ": " + f.type() + "\n");
        }
    }
}
Also used : Field(edu.mit.csail.sdg.ast.Sig.Field) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Expr(edu.mit.csail.sdg.ast.Expr) ExprHasName(edu.mit.csail.sdg.ast.ExprHasName) Decl(edu.mit.csail.sdg.ast.Decl)

Example 14 with ErrorSyntax

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

the class CompModule method addAssertion.

// ============================================================================================================================//
/**
 * Add an ASSERT declaration.
 */
String addAssertion(Pos pos, String name, Expr value) throws Err {
    status = 3;
    if (name == null || name.length() == 0)
        name = "assert$" + (1 + asserts.size());
    dup(pos, name, false);
    Expr old = asserts.put(name, ExprUnary.Op.NOOP.make(value.span().merge(pos), value));
    if (old != null) {
        asserts.put(name, old);
        throw new ErrorSyntax(pos, "\"" + name + "\" is already the name of an assertion in this module.");
    }
    return name;
}
Also used : ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Expr(edu.mit.csail.sdg.ast.Expr)

Example 15 with ErrorSyntax

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

the class ScopeComputer method setMaxSeq.

/**
 * Modifies the maximum sequence length.
 */
private void setMaxSeq(Pos pos, int newMaxSeq) throws ErrorAPI, ErrorSyntax {
    if (newMaxSeq > max())
        throw new ErrorSyntax(pos, "With integer bitwidth of " + bitwidth + ", you cannot have sequence length longer than " + max());
    if (newMaxSeq < 0)
        // throw new ErrorSyntax(pos, "The maximum sequence
        newMaxSeq = 0;
    // length cannot be negative.");
    maxseq = newMaxSeq;
    sig2scope.put(SEQIDX, maxseq);
}
Also used : ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax)

Aggregations

ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)35 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)10 Pos (edu.mit.csail.sdg.alloy4.Pos)9 Expr (edu.mit.csail.sdg.ast.Expr)9 Sig (edu.mit.csail.sdg.ast.Sig)9 ExprVar (edu.mit.csail.sdg.ast.ExprVar)7 ArrayList (java.util.ArrayList)7 ErrorType (edu.mit.csail.sdg.alloy4.ErrorType)6 Symbol (java_cup.runtime.Symbol)6 Err (edu.mit.csail.sdg.alloy4.Err)5 Func (edu.mit.csail.sdg.ast.Func)5 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)5 LinkedHashMap (java.util.LinkedHashMap)5 TempList (edu.mit.csail.sdg.alloy4.ConstList.TempList)4 Command (edu.mit.csail.sdg.ast.Command)4 IOException (java.io.IOException)4 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)3 Env (edu.mit.csail.sdg.alloy4.Env)2 Decl (edu.mit.csail.sdg.ast.Decl)2 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)2