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;
}
}
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;
}
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();
}
}
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);
}
Aggregations