public class Indexer
extends java.lang.Object
This class is not thread-safe.
Modifier and Type | Field and Description |
---|---|
Builtins |
builtins
Manages the built-in modules -- that is, modules from the standard Python
library that are implemented in C and consequently have no Python source.
|
java.lang.String |
currentFile |
java.util.Set<java.lang.String> |
failedModules
When resolving imports we look in various possible locations.
|
Scope |
globaltable
The top-level (builtin) scope.
|
static Indexer |
idx
The global indexer instance.
|
Scope |
moduleTable
A scope containing bindings for all modules currently loaded by the indexer.
|
java.util.Map<java.lang.String,java.util.List<Diagnostic>> |
parseErrs |
java.util.List<java.lang.String> |
path |
java.util.Map<java.lang.String,java.util.List<Diagnostic>> |
problems
Diagnostics.
|
java.lang.String |
projDir |
Constructor and Description |
---|
Indexer() |
Modifier and Type | Method and Description |
---|---|
void |
addPath(java.lang.String p)
Adds the specified absolute path to the module search path.
|
void |
addPaths(java.util.List<java.lang.String> p)
Adds the specified absolute paths to the module search path.
|
boolean |
aggressiveAssertionsEnabled() |
void |
clearAstCache()
Clears the AST cache (to free up memory).
|
void |
clearModuleTable()
Clears the module table, discarding all resolved ASTs (modules)
and their scope information.
|
void |
enableAggressiveAssertions(boolean enable)
Configures whether the indexer should abort with an exception when it
encounters an internal error or unexpected program state.
|
void |
fine(java.lang.String msg) |
void |
finer(java.lang.String msg) |
java.util.List<Outliner.Entry> |
generateOutline(java.lang.String path)
Create an outline for a file in the index.
|
NModule |
getAstForFile(java.lang.String file)
Returns the syntax tree for
file . |
NModule |
getAstForFile(java.lang.String file,
java.lang.String contents)
Returns the syntax tree for
file . |
java.util.Map<java.lang.String,NBinding> |
getBindings()
Returns the mutable set of all bindings collected, keyed on their qnames.
|
NModuleType |
getBuiltinModule(java.lang.String qname) |
java.util.List<Diagnostic> |
getDiagnosticsForFile(java.lang.String file)
Returns the list, possibly empty but never
null , of
errors and warnings generated in the file. |
java.util.List<java.lang.String> |
getLoadedFiles() |
java.util.List<java.lang.String> |
getLoadPath()
Returns the module search path -- the project directory followed by any
paths that were added by
addPath(String) . |
java.util.logging.Logger |
getLogger() |
NModuleType |
getModuleForFile(java.lang.String file)
Returns (loading/resolving if necessary) the module for a given source path.
|
java.lang.String |
getStatusReport()
Report resolution rate and other statistics data.
|
void |
handleException(java.lang.String msg,
java.lang.Throwable cause)
If aggressive assertions are enabled, propagates the passed
Throwable , wrapped in an IndexingException . |
void |
info(java.lang.String msg) |
boolean |
isLibFile(java.lang.String file) |
NModuleType |
loadFile(java.lang.String path)
Loads a file and all its ancestor packages.
|
NModuleType |
loadFile(java.lang.String path,
boolean skipChain)
Load, parse and analyze a source file given its absolute path.
|
void |
loadFileRecursive(java.lang.String fullname)
Load all Python source files recursively if the given fullname is a
directory; otherwise just load a file.
|
NModuleType |
loadModule(java.lang.String modname)
This method searches the module path for the module
modname . |
NModuleType |
loadString(java.lang.String path,
java.lang.String contents)
Loads a module from a string containing the module contents.
|
void |
log(java.util.logging.Level level,
java.lang.String msg) |
NBinding |
lookupQname(java.lang.String qname)
Return the binding for
qname , or null if not known. |
NType |
lookupQnameType(java.lang.String qname)
Return the type for
qname , or null if not known. |
int |
numFilesLoaded() |
NBinding |
putBinding(NBinding b) |
void |
putLocation(NNode node,
NBinding b)
Add a reference to binding
b at AST node node . |
void |
putLocation(Ref ref,
NBinding b) |
void |
putProblem(NNode loc,
java.lang.String msg) |
void |
putProblem(java.lang.String file,
int beg,
int end,
java.lang.String msg) |
void |
ready()
Performs final indexing-building passes, including marking references to
undeclared variables.
|
void |
recordUnresolvedModule(java.lang.String qname,
java.lang.String file)
Reports a failed module or submodule resolution.
|
void |
release()
Releases all resources for the current indexer.
|
void |
removeBinding(NBinding b) |
void |
reportFailedAssertion(java.lang.String msg)
Signals a failed assertion about the state of the indexer or index.
|
void |
setLogger(java.util.logging.Logger logger) |
void |
setPath(java.util.List<java.lang.String> path)
Sets the module search path to the specified list of absolute paths.
|
void |
setProjectDir(java.lang.String cd) |
void |
severe(java.lang.String msg) |
java.lang.String |
toString() |
void |
updateLocation(Ref node,
NBinding b)
Add
node as a reference to binding b , removing
node from any other binding ref-lists that it may have occupied. |
void |
warn(java.lang.String msg) |
public static Indexer idx
public Scope moduleTable
public Scope globaltable
public java.util.Map<java.lang.String,java.util.List<Diagnostic>> problems
public java.util.Map<java.lang.String,java.util.List<Diagnostic>> parseErrs
public java.lang.String currentFile
public java.lang.String projDir
public java.util.List<java.lang.String> path
public java.util.Set<java.lang.String> failedModules
public Builtins builtins
public void setLogger(java.util.logging.Logger logger)
public java.util.logging.Logger getLogger()
public void setProjectDir(java.lang.String cd) throws java.io.IOException
java.io.IOException
public void enableAggressiveAssertions(boolean enable)
public boolean aggressiveAssertionsEnabled()
public void handleException(java.lang.String msg, java.lang.Throwable cause)
Throwable
, wrapped in an IndexingException
.msg
- descriptive message; ok to be null
IndexingException
public void reportFailedAssertion(java.lang.String msg)
IndexingException
.
Otherwise the message is logged as a warning, and indexing continues.msg
- a descriptive message about the problemIndexingException
enableAggressiveAssertions(boolean)
public void addPaths(java.util.List<java.lang.String> p) throws java.io.IOException
java.io.IOException
public void addPath(java.lang.String p) throws java.io.IOException
java.io.IOException
public void setPath(java.util.List<java.lang.String> path) throws java.io.IOException
java.io.IOException
public java.util.List<java.lang.String> getLoadPath()
addPath(String)
.public boolean isLibFile(java.lang.String file)
public java.util.Map<java.lang.String,NBinding> getBindings()
public NBinding lookupQname(java.lang.String qname)
qname
, or null
if not known.public NType lookupQnameType(java.lang.String qname)
qname
, or null
if not known.java.lang.IllegalStateException
- if ready()
has not been called.public NModuleType getModuleForFile(java.lang.String file) throws java.lang.Exception
file
- absolute file pathjava.lang.Exception
public java.util.List<Diagnostic> getDiagnosticsForFile(java.lang.String file)
null
, of
errors and warnings generated in the file.public java.util.List<Outliner.Entry> generateOutline(java.lang.String path) throws java.lang.Exception
path
- the file for which to build the outlinejava.lang.Exception
public void putLocation(NNode node, NBinding b)
b
at AST node node
.public void updateLocation(Ref node, NBinding b)
node
as a reference to binding b
, removing
node
from any other binding ref-lists that it may have occupied.
Currently only used in retargeting attribute references from provisional
bindings once the actual binding is determined.public void removeBinding(NBinding b)
public void putProblem(NNode loc, java.lang.String msg)
public void putProblem(java.lang.String file, int beg, int end, java.lang.String msg)
public NModuleType loadFile(java.lang.String path) throws java.lang.Exception
java.lang.Exception
loadFile(String,boolean)
public NModuleType loadString(java.lang.String path, java.lang.String contents) throws java.lang.Exception
path
- a path for reporting/caching purposes. The filename
component is used to construct the module qualified name.java.lang.Exception
public NModuleType loadFile(java.lang.String path, boolean skipChain) throws java.lang.Exception
path
- the absolute path to the file or directory.
If it is a directory, it is suffixed with "__init__.py", and
only that file is loaded from the directory.skipChain
- true
to skip loading ancestor packagesnull
if path
could not be loadedjava.lang.Exception
public NModule getAstForFile(java.lang.String file) throws java.lang.Exception
file
. java.lang.Exception
public NModule getAstForFile(java.lang.String file, java.lang.String contents) throws java.lang.Exception
file
. java.lang.Exception
public NModuleType getBuiltinModule(java.lang.String qname) throws java.lang.Exception
java.lang.Exception
public NModuleType loadModule(java.lang.String modname) throws java.lang.Exception
modname
. If found, it is passed
to loadFile(java.lang.String)
.
The mechanisms for importing modules are in general statically undecidable. We make a reasonable effort to follow the most common lookup rules.
modname
- a module name. Can be a relative path to a directory or a file (without the
extension) or a possibly-qualified module name. If it is a module name, cannot
contain leading dots.java.lang.Exception
public void loadFileRecursive(java.lang.String fullname) throws java.lang.Exception
java.lang.Exception
public void ready()
public void clearAstCache()
getAstForFile(java.lang.String)
will either fetch the serialized AST from a
disk cache or re-parse the file from scratch.public void clearModuleTable()
public void recordUnresolvedModule(java.lang.String qname, java.lang.String file)
qname
- module qname, e.g. "org.foo.bar"file
- the file where the unresolved import occurredpublic java.lang.String getStatusReport()
public int numFilesLoaded()
public java.util.List<java.lang.String> getLoadedFiles()
public void log(java.util.logging.Level level, java.lang.String msg)
public void severe(java.lang.String msg)
public void warn(java.lang.String msg)
public void info(java.lang.String msg)
public void fine(java.lang.String msg)
public void finer(java.lang.String msg)
public void release()
public java.lang.String toString()
toString
in class java.lang.Object