use of edu.mit.csail.sdg.alloy4.Err in project org.alloytools.alloy by AlloyTools.
the class CUP$CompParser$actions method alloy_parseStream.
static CompModule alloy_parseStream(List<Object> seenDollar, Map<String, String> loaded, Map<String, String> fc, CompModule root, int lineOffset, String filename, String prefix, int initialResolutionMode) throws Err, FileNotFoundException, IOException {
Reader isr = null;
try {
if (root == null && prefix.length() != 0)
throw new ErrorFatal("Internal error (parse subfile with root==null)");
if (root != null && prefix.length() == 0)
throw new ErrorFatal("Internal error (parse topfile with root!=null)");
CompModule u = new CompModule(root, filename, prefix);
if (!filename.endsWith(INT_LIB_NAME + ".als"))
u.addOpen(null, null, ExprVar.make(null, INT_LIB_NAME), null, ExprVar.make(null, "integer"));
u.resolution = initialResolutionMode;
String content = fc != null ? fc.get(filename) : null;
if (content == null && loaded != null)
content = loaded.get(filename);
if (content == null)
content = Util.readAll(filename);
if (loaded != null)
loaded.put(filename, content);
content = MarkdownHandler.strip(content);
content = Util.convertLineBreak(content);
isr = new StringReader(content);
CompFilter s = new CompFilter(u, seenDollar, filename, lineOffset, new BufferedReader(isr));
CompParser p = new CompParser(s);
p.alloymodule = u;
try {
p.parse();
} catch (Throwable ex) {
if (ex instanceof Err)
throw (Err) ex;
throw new ErrorFatal("Parser Exception", ex);
}
return u;
} finally {
Util.close(isr);
}
}
use of edu.mit.csail.sdg.alloy4.Err in project org.alloytools.alloy by AlloyTools.
the class CompUtil method parseOneModule.
public static CompModule parseOneModule(String content) throws Err {
try {
Map<String, String> fc = new LinkedHashMap<String, String>();
fc.put("", content);
return CompUtil.parse(new ArrayList<Object>(), null, fc, null, 0, "", "", 1);
} 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);
}
}
use of edu.mit.csail.sdg.alloy4.Err 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);
}
}
use of edu.mit.csail.sdg.alloy4.Err 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;
}
use of edu.mit.csail.sdg.alloy4.Err in project org.alloytools.alloy by AlloyTools.
the class A4SolutionReader method parseType.
/**
* Parse type.
*/
private Expr parseType(XMLNode node) throws IOException, Err {
Expr expr = null;
if (!node.is("types"))
throw new IOException("<types>...</type> expected");
for (XMLNode n : node) if (n.is("type")) {
Sig sig = parseSig(n.getAttribute("ID"), 0);
if (expr == null)
expr = sig;
else
expr = expr.product(sig);
}
if (expr == null)
throw new IOException("<type ID=../> expected");
return expr;
}
Aggregations