Search in sources :

Example 1 with Node

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

the class Analyzer method parseAndResolve.

@Nullable
private Type parseAndResolve(String file) {
    loadingProgress.tick();
    Node ast = getAstForFile(file);
    if (ast == null) {
        failedToParse.add(file);
        return null;
    } else {
        Type type = inferencer.visit(ast, moduleTable);
        loadedFiles.add(file);
        return type;
    }
}
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) Node(org.yinwang.pysonar.ast.Node) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with Node

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

the class AstCache method getAST.

/**
     * Returns the syntax tree for {@code path}.  May find and/or create a
     * cached copy in the mem cache or the disk cache.
     *
     * @param path absolute path to a source file
     * @return the AST, or {@code null} if the parse failed for any reason
     */
@Nullable
public Node getAST(@NotNull String path) {
    // Cache stores null value if the parse failed.
    if (cache.containsKey(path)) {
        return cache.get(path);
    }
    // Might be cached on disk but not in memory.
    Node node = getSerializedModule(path);
    if (node != null) {
        LOG.log(Level.FINE, "reusing " + path);
        cache.put(path, node);
        return node;
    }
    node = null;
    try {
        LOG.log(Level.FINE, "parsing " + path);
        node = parser.parseFile(path);
    } finally {
        // may be null
        cache.put(path, node);
    }
    if (node != null) {
        serialize(node);
    }
    return node;
}
Also used : Node(org.yinwang.pysonar.ast.Node) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with Node

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

the class JSONDump method graph.

/*
     * Precondition: srcpath and inclpaths are absolute paths
     */
private static void graph(String srcpath, String[] inclpaths, OutputStream symOut, OutputStream refOut, OutputStream docOut) throws Exception {
    // Compute parent dirs, sort by length so potential prefixes show up first
    List<String> parentDirs = Lists.newArrayList(inclpaths);
    parentDirs.add(dirname(srcpath));
    Collections.sort(parentDirs, new Comparator<String>() {

        @Override
        public int compare(String s1, String s2) {
            int diff = s1.length() - s2.length();
            if (0 == diff) {
                return s1.compareTo(s2);
            }
            return diff;
        }
    });
    Analyzer idx = newAnalyzer(srcpath, inclpaths);
    idx.multilineFunType = true;
    JsonFactory jsonFactory = new JsonFactory();
    JsonGenerator symJson = jsonFactory.createGenerator(symOut);
    JsonGenerator refJson = jsonFactory.createGenerator(refOut);
    JsonGenerator docJson = jsonFactory.createGenerator(docOut);
    JsonGenerator[] allJson = { symJson, refJson, docJson };
    for (JsonGenerator json : allJson) {
        json.writeStartArray();
    }
    for (Binding b : idx.getAllBindings()) {
        String path = b.qname.replace('.', '/').replace("%20", ".");
        if (b.getFile() != null) {
            if (b.getFile().startsWith(srcpath)) {
                writeSymJson(b, symJson);
                writeDocJson(b, idx, docJson);
            }
        }
        for (Node ref : b.refs) {
            if (ref.file != null) {
                if (ref.file.startsWith(srcpath)) {
                    writeRefJson(ref, b, refJson);
                }
            }
        }
    }
    for (JsonGenerator json : allJson) {
        json.writeEndArray();
        json.close();
    }
}
Also used : Node(org.yinwang.pysonar.ast.Node) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 4 with Node

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

the class Linker method processDefDebug.

private void processDefDebug(@NotNull Binding binding) {
    int hash = binding.hashCode();
    if (binding.isURL() || binding.start < 0 || seenDef.contains(hash)) {
        return;
    }
    seenDef.add(hash);
    Style style = new Style(Style.Type.ANCHOR, binding.start, binding.end);
    style.message = binding.type.toString();
    style.url = binding.qname;
    style.id = "" + Math.abs(binding.hashCode());
    Set<Node> refs = binding.refs;
    style.highlight = new ArrayList<>();
    for (Node r : refs) {
        style.highlight.add(Integer.toString(Math.abs(r.hashCode())));
    }
    addFileStyle(binding.getFile(), style);
}
Also used : Node(org.yinwang.pysonar.ast.Node)

Aggregations

Node (org.yinwang.pysonar.ast.Node)4 Nullable (org.jetbrains.annotations.Nullable)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)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