Top Description Fields Constructors Methods
org.python.indexer

public Class AstCache

extends Object
Class Inheritance
Imports
java.io.File, .FileInputStream, .FileOutputStream, .IOException, .ObjectInputStream, .ObjectOutputStream, java.util.HashMap, .List, .Map, java.util.logging.Level, .Logger, org.antlr.runtime.ANTLRFileStream, .ANTLRStringStream, .CharStream, .RecognitionException, org.python.antlr.AnalyzingParser, org.python.antlr.base.mod, org.python.indexer.ast.NModule

Provides a factory for python source ASTs. Maintains configurable on-disk and in-memory caches to avoid re-parsing files during analysis.

Field Summary

Modifier and TypeField and Description
private Map<String, NModule>
public static final String
private static AstCache
private static final Logger

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Clears the memory cache.

public boolean

Returns:

true if all cached AST files were removed
clearDiskCache
()

Removes all serialized ASTs from the on-disk cache.

pack-priv NModule
deserialize(File sourcePath)

private NModule
fetch(String
absolute source path
path
)

Get or create an AST for path, checking and if necessary updating the disk and memory caches.

private void
fine(String msg)

private void
private NModule
generateAST(mod ast, String path)

public static AstCache
get()

public NModule

Returns:

the AST, or null if the parse failed for any reason
getAST
(String
absolute path to a source file
path
)

Returns the syntax tree for path.

public NModule
getAST(String
a name for the file. Can be relative.
path
,
String
the source to parse
contents
)

Returns the syntax tree for path with contents.

public String
getCachePath(File sourcePath)

Each source file's AST is saved in an object file named for the MD5 checksum of the source file.

public String
pack-priv NModule
private void
info(String msg)

private mod
invokeANTLR(String filename)

private mod
invokeANTLR(String filename, String contents)

private mod
invokeANTLR(CharStream text, String filename)

private void
log(Level level, String msg)

private NModule
parse(String path)

Parse a file.

private NModule
parse(String path, String contents)

Parse a string.

private void
pack-priv void
private void
private void
warn(String msg)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cacheback to summary
private Map<String, NModule> cache
CACHE_DIRback to summary
public static final String CACHE_DIR
INSTANCEback to summary
private static AstCache INSTANCE
LOGback to summary
private static final Logger LOG

Constructor Detail

AstCacheback to summary
private AstCache() throws Exception

Method Detail

clearback to summary
public void clear()

Clears the memory cache.

clearDiskCacheback to summary
public boolean clearDiskCache()

Removes all serialized ASTs from the on-disk cache.

Returns:boolean

true if all cached AST files were removed

deserializeback to summary
pack-priv NModule deserialize(File sourcePath) throws Exception
fetchback to summary
private NModule fetch(String path) throws Exception

Get or create an AST for path, checking and if necessary updating the disk and memory caches.

Parameters
path:String

absolute source path

fineback to summary
private void fine(String msg)
finerback to summary
private void finer(String msg)
generateASTback to summary
private NModule generateAST(mod ast, String path) throws Exception
getback to summary
public static AstCache get() throws Exception
getASTback to summary
public NModule getAST(String path) throws Exception

Returns the syntax tree for path. May find and/or create a cached copy in the mem cache or the disk cache.

Parameters
path:String

absolute path to a source file

Returns:NModule

the AST, or null if the parse failed for any reason

Exceptions
Exception:
if anything unexpected occurs

getASTback to summary
public NModule getAST(String path, String contents) throws Exception

Returns the syntax tree for path with contents. Uses the memory cache but not the disk cache. This method exists primarily for unit testing.

Parameters
path:String

a name for the file. Can be relative.

contents:String

the source to parse

getCachePathback to summary
public String getCachePath(File sourcePath) throws Exception

Each source file's AST is saved in an object file named for the MD5 checksum of the source file. All that is needed is the MD5, but the file's base name is included for ease of debugging.

getCachePathback to summary
public String getCachePath(String md5, String name)
getSerializedModuleback to summary
pack-priv NModule getSerializedModule(String sourcePath)
infoback to summary
private void info(String msg)
invokeANTLRback to summary
private mod invokeANTLR(String filename)
invokeANTLRback to summary
private mod invokeANTLR(String filename, String contents)
invokeANTLRback to summary
private mod invokeANTLR(CharStream text, String filename)
logback to summary
private void log(Level level, String msg)
parseback to summary
private NModule parse(String path) throws Exception

Parse a file. Does not look in the cache or cache the result.

parseback to summary
private NModule parse(String path, String contents) throws Exception

Parse a string. Does not look in the cache or cache the result.

recordParseErrorsback to summary
private void recordParseErrors(String path, List<RecognitionException> errs)
serializeback to summary
pack-priv void serialize(NModule ast) throws Exception
severeback to summary
private void severe(String msg)
warnback to summary
private void warn(String msg)