Search in sources :

Example 1 with Url

use of org.yinwang.pysonar.ast.Url in project pysonar2 by yinwang0.

the class Analyzer method loadModule.

@Nullable
public Type loadModule(@NotNull List<Name> name, @NotNull State state) {
    if (name.isEmpty()) {
        return null;
    }
    String qname = makeQname(name);
    Type mt = getBuiltinModule(qname);
    if (mt != null) {
        state.insert(name.get(0).id, new Url(Builtins.LIBRARY_URL + mt.table.path + ".html"), mt, Binding.Kind.SCOPE);
        return mt;
    }
    // If there are more than one segment
    // load the packages first
    Type prev = null;
    String startPath = locateModule(name.get(0).id);
    if (startPath == null) {
        return null;
    }
    File path = new File(startPath);
    for (int i = 0; i < name.size(); i++) {
        path = new File(path, name.get(i).id);
        File initFile = new File($.joinPath(path, "__init__.py").getPath());
        if (initFile.exists()) {
            Type mod = loadFile(initFile.getPath());
            if (mod == null) {
                return null;
            }
            Binding binding = Binding.createFileBinding(name.get(i).id, initFile.getPath(), mod);
            if (prev != null) {
                prev.table.update(name.get(i).id, binding);
            } else {
                state.update(name.get(i).id, binding);
            }
            Analyzer.self.putRef(name.get(i), binding);
            prev = mod;
        } else if (i == name.size() - 1) {
            File startFile = new File(path + Globals.FILE_SUFFIX);
            if (startFile.exists()) {
                Type mod = loadFile(startFile.getPath());
                if (mod == null) {
                    return null;
                }
                Binding binding = Binding.createFileBinding(name.get(i).id, startFile.getPath(), mod);
                if (prev != null) {
                    prev.table.update(name.get(i).id, binding);
                } else {
                    state.update(name.get(i).id, binding);
                }
                Analyzer.self.putRef(name.get(i), binding);
                prev = mod;
            } else {
                return null;
            }
        }
    }
    return prev;
}
Also used : ClassType(org.yinwang.pysonar.types.ClassType) Type(org.yinwang.pysonar.types.Type) UnionType(org.yinwang.pysonar.types.UnionType) FunType(org.yinwang.pysonar.types.FunType) ModuleType(org.yinwang.pysonar.types.ModuleType) File(java.io.File) Url(org.yinwang.pysonar.ast.Url) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with Url

use of org.yinwang.pysonar.ast.Url in project pysonar2 by yinwang0.

the class Builtins method buildClassType.

// XXX:  finish wiring this up.  ClassType needs to inherit from it somehow,
// so we can remove the per-instance attributes from NClassDef.
void buildClassType() {
    State t = BaseClass.table;
    for (String s : list("__name__", "__doc__", "__module__")) {
        t.insert(s, new Url(DATAMODEL_URL), Types.StrInstance, ATTRIBUTE);
    }
    t.insert("__dict__", new Url(DATAMODEL_URL), new DictType(Types.StrInstance, Types.UNKNOWN), ATTRIBUTE);
}
Also used : Url(org.yinwang.pysonar.ast.Url)

Example 3 with Url

use of org.yinwang.pysonar.ast.Url in project pysonar2 by yinwang0.

the class Builtins method buildFunctionType.

void buildFunctionType() {
    State t = BaseFunction.table;
    for (String s : list("func_doc", "__doc__", "func_name", "__name__", "__module__")) {
        t.insert(s, new Url(DATAMODEL_URL), Types.StrInstance, ATTRIBUTE);
    }
    t.insert("func_closure", new Url(DATAMODEL_URL), newTuple(), ATTRIBUTE);
    t.insert("func_code", new Url(DATAMODEL_URL), Types.UNKNOWN, ATTRIBUTE);
    t.insert("func_defaults", new Url(DATAMODEL_URL), newTuple(), ATTRIBUTE);
    t.insert("func_globals", new Url(DATAMODEL_URL), new DictType(Types.StrInstance, Types.UNKNOWN), ATTRIBUTE);
    t.insert("func_dict", new Url(DATAMODEL_URL), new DictType(Types.StrInstance, Types.UNKNOWN), ATTRIBUTE);
    // Assume any function can become a method, for simplicity.
    for (String s : list("__func__", "im_func")) {
        t.insert(s, new Url(DATAMODEL_URL), new FunType(), METHOD);
    }
}
Also used : Url(org.yinwang.pysonar.ast.Url)

Aggregations

Url (org.yinwang.pysonar.ast.Url)3 File (java.io.File)1 Nullable (org.jetbrains.annotations.Nullable)1 ClassType (org.yinwang.pysonar.types.ClassType)1 FunType (org.yinwang.pysonar.types.FunType)1 ModuleType (org.yinwang.pysonar.types.ModuleType)1 Type (org.yinwang.pysonar.types.Type)1 UnionType (org.yinwang.pysonar.types.UnionType)1