Search in sources :

Example 11 with Pos

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

the class CompModule method addMacro.

// ============================================================================================================================//
/**
 * Add a MACRO declaration.
 */
void addMacro(Pos p, Pos isPrivate, String n, List<ExprVar> decls, Expr v) throws Err {
    if (!Version.experimental)
        throw new ErrorSyntax(p, "LET declaration is allowed only inside a toplevel paragraph.");
    ConstList<ExprVar> ds = ConstList.make(decls);
    status = 3;
    dup(p, n, false);
    for (int i = 0; i < ds.size(); i++) for (int j = i + 1; j < ds.size(); j++) if (ds.get(i).label.equals(ds.get(j).label))
        throw new ErrorSyntax(ds.get(j).span(), "The parameter name \"" + ds.get(j).label + "\" cannot appear more than once.");
    Macro ans = new Macro(p, isPrivate, this, n, ds, v);
    Macro old = macros.put(n, ans);
    if (old != null) {
        macros.put(n, old);
        throw new ErrorSyntax(p, "You cannot declare more than one macro with the same name \"" + n + "\" in the same file.");
    }
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax)

Example 12 with Pos

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

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

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

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

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