Search in sources :

Example 1 with Open

use of edu.mit.csail.sdg.parser.CompModule.Open 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;
}
Also used : ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) IOException(java.io.IOException) File(java.io.File) Open(edu.mit.csail.sdg.parser.CompModule.Open)

Aggregations

ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)1 Open (edu.mit.csail.sdg.parser.CompModule.Open)1 File (java.io.File)1 IOException (java.io.IOException)1