Jump to: navigation, search

Difference between revisions of "DLTK Core Architecture"

(Improve wording (no addition to content))
(fix table)
Line 16: Line 16:
 
The following table summarizes the different kinds of model elements. All elements classes support the <code lang="java">IModelElement</code> interface.
 
The following table summarizes the different kinds of model elements. All elements classes support the <code lang="java">IModelElement</code> interface.
  
<table border=1>
+
{| {{Greytable}}
<tr>
+
|-
<td>Element</td>
+
!Element
<td>JDT-analog</td>
+
!JDT-analog
<td>Description</td>
+
!Description
</tr>
+
|- valign="top"
<tr>
+
|<code lang="java">IScriptModel</code>
<td><code lang="java">IScriptModel</code></td>
+
|<code lang="java">IJavaModel</code>
<td><code lang="java">IJavaModel</code></td>
+
|Represents the root model element, corresponding to the workspace. The parent of all projects with the script natures. It also gives you access to the projects without the script nature.
<td>Represents the root model element, corresponding to the
+
|- valign="top"
workspace. The parent of all projects with the script natures. It also
+
|<code lang="java">IScriptProject</code>
gives you access to the projects without the script nature.</td>
+
|<code lang="java">IJavaProject</code>
</tr>
+
|Represents a script project in the workspace. (Child of <code lang="java">IScriptModel</code>)
<tr>
+
|- valign="top"
<td><code lang="java">IScriptProject</code></td>
+
|<code lang="java">IProjectFragment</code>
<td><code lang="java">IJavaProject</code></td>
+
|<code lang="java">IPackageFragmentRoot</code>
<td>Represents a script project in the workspace. (Child of
+
|Represents a project fragment, and maps the contents to an underlying resource which is either a folder, JAR, or ZIP file. (Child of <code lang="java">IScriptProject</code>)
<code lang="java">IScriptModel</code>)</td>
+
|- valign="top"
</tr>
+
|<code lang="java">IScriptFolder</code>
<tr>
+
|<code lang="java">IPackageFragment</code>
<td><code lang="java">IProjectFragment</code></td>
+
|Represents a folder containing script files inside. (Child of <code lang="java">IProjectFragment</code>)
<td><code lang="java">IPackageFragmentRoot</code></td>
+
|- valign="top"
<td>Represents a project fragment, and maps the contents to an
+
|<code lang="java">ISourceModule</code>
underlying resource which is either a folder, JAR, or ZIP file. (Child
+
|<code lang="java">ICompilationUnit</code>
of <code lang="java">IScriptProject</code>)</td>
+
|Represents a source file. (Child of <code lang="java">IScriptFolder</code>)
</tr>
+
|- valign="top"
<tr>
+
|<code lang="java">IPackageDeclaration</code>
<td><code lang="java">IScriptFolder</code></td>
+
|<code lang="java">IPackageDeclaration</code>
<td><code lang="java">IPackageFragment</code></td>
+
|Represents a package declaration in a source module. (Child of <code lang="java">ISourceModule</code>)
<td>Represents a folder containing script files inside. (Child of
+
|- valign="top"
<code lang="java">IProjectFragment</code>)</td>
+
|<code lang="java">IType</code>
</tr>
+
|<code lang="java">IType</code>
<tr>
+
|Represents either a class/module/namespace inside a source file.
<td><code lang="java">ISourceModule</code></td>
+
|- valign="top"
<td><code lang="java">ICompilationUnit</code></td>
+
|<code lang="java">IField</code>
<td>Represents a source file. (Child of <code lang="java">IScriptFolder</code>)</td>
+
|<code lang="java">IField</code>
</tr>
+
|Represents a field inside a type. (Child of <code lang="java">IType</code>)
<tr>
+
|- valign="top"
<td><code lang="java">IPackageDeclaration</code></td>
+
|<code lang="java">IMethod</code>
<td><code lang="java">IPackageDeclaration</code></td>
+
|<code lang="java">IMethod</code>
<td>Represents a package declaration in a source module. (Child
+
|Represents a method or constructor inside a type. (Child of <code lang="java">IType</code>)
of <code lang="java">ISourceModule</code>)</td>
+
|}
</tr>
+
<tr>
+
<td><code lang="java">IType</code></td>
+
<td><code lang="java">IType</code></td>
+
<td>Represents either a class/module/namespace inside a source
+
file.</td>
+
</tr>
+
<tr>
+
<td><code lang="java">IField</code></td>
+
<td><code lang="java">IField</code></td>
+
<td>Represents a field inside a type. (Child of IType</code>)</td>
+
</tr>
+
<tr>
+
<td><code lang="java">IMethod</code></td>
+
<td><code lang="java">IMethod</code></td>
+
<td>Represents a method or constructor inside a type. (Child of
+
IType</code>)</td>
+
</tr>
+
</table>
+
  
 
You should use the <code lang="java">DLTKCore.create(...)</code> methods to build out a model. These methods make it easy for the DLTK user to create the appropriate model element from a file, resource or project.
 
You should use the <code lang="java">DLTKCore.create(...)</code> methods to build out a model. These methods make it easy for the DLTK user to create the appropriate model element from a file, resource or project.

Revision as of 17:29, 13 September 2010

Build paths

Similar to Java's class paths, DLTK has the concept of build paths.

A build path is the set of source folders, library containers and references to other projects. The build path is used for model building and launching.

The build path is stored in the file .buildpath relative to the project's root folder. DLTK automatically reads it when required. You can get the current project's build path as an array of IBuildpathEntrys via the IScriptProject.getRawBuildpath() method. To change the build path for a project use the setRawBuildpath() method. New elements of IBuildpathEntry may be created with the DLTKCore.new*Entry(...) methods.

Model

The DLTK model is central to understanding DLTK. The DLTK model is based on the JDT model, so if you are familiar with that then you will understand everything here quickly.

Like JDT, DLTK uses an in-memory, hierarchical object model to represent the workspace structure from the project level down to source file internals. This structure is derived from the project's build path.

The following table summarizes the different kinds of model elements. All elements classes support the IModelElement interface.

Element JDT-analog Description
IScriptModel IJavaModel Represents the root model element, corresponding to the workspace. The parent of all projects with the script natures. It also gives you access to the projects without the script nature.
IScriptProject IJavaProject Represents a script project in the workspace. (Child of IScriptModel)
IProjectFragment IPackageFragmentRoot Represents a project fragment, and maps the contents to an underlying resource which is either a folder, JAR, or ZIP file. (Child of IScriptProject)
IScriptFolder IPackageFragment Represents a folder containing script files inside. (Child of IProjectFragment)
ISourceModule ICompilationUnit Represents a source file. (Child of IScriptFolder)
IPackageDeclaration IPackageDeclaration Represents a package declaration in a source module. (Child of ISourceModule)
IType IType Represents either a class/module/namespace inside a source file.
IField IField Represents a field inside a type. (Child of IType)
IMethod IMethod Represents a method or constructor inside a type. (Child of IType)

You should use the DLTKCore.create(...) methods to build out a model. These methods make it easy for the DLTK user to create the appropriate model element from a file, resource or project.

Model building

DLTK automatically provides model elements from the workspace level down to the source modules level. To extend the model, the DLTK user should:

  • contribute a IDLTKLanguageToolkit interface implementation via the org.eclipse.dltk.core.language extension point,
  • contribute the language-specific nature as an extension point attribute, and return that from the getNatureId() method,
  • implement methods validateSourceModule() and validateSourcePackage() to return OK only for source modules or packages that are real source modules.

User projects that have the right nature will then be considered as a script project and the DLTK model for them will be built accordingly to internal structure, results from validate...() methods and from build paths.

For building a source module's internal model elements, there is another mechanism called source element parsers. These are contributed via the org.eclipse.dltk.core.sourceElementParsers extension point and should implement ISourceElementParser.

The main task of the source element parser is to parse source files and report internal model element information to the given ISourceElementRequestor.

Search engine

Indexes

The platform's search engine uses indexes. An index is a set of documents and the keys associated with them. There are possible several different indexes (for type names, methods, ...).

DLTK automatically builds an index for all script source files in a separate thread. The DLTK provides a standard source element parser with a requester set to it's own SourceIndexerRequestor object. The source element parser doesn't know anything about search and just reports model elements info. The task of the SourceIndexerRequestor is to find and report appropriate index keys to the platform's SourceIndexer. The DLTK user may extend the DLTK's SourceIndexerRequestor as required.

Search

Before using the search engine, the user should prepare DLTK SearchPattern objects. These may be a TypeDeclarationPattern or a MethodPattern</a>. These can be created using the static method <code lang="java">SearchPattern.createPattern().

After that, the user should specify a search scope(project, workspace,...). The scope may be created with the SearchEngine.createSearchScope() method.

The final item required is a SearchRequestor object that will receive all successful search matches. When that is ready, DLTK's SearchEngine.search() may be called. Here is an example:

SearchRequestor requestor = new SearchRequestor() {
    public void acceptSearchMatch(SearchMatch match)
        throws CoreException {
            // process match
        }
    };
 
SearchPattern pattern = SearchPattern.createPattern(namePattern,
    IDLTKSearchConstants.METHOD, IDLTKSearchConstants.DECLARATIONS,
    SearchPattern.R_PATTERN_MATCH | SearchPattern.R_EXACT_MATCH);
IDLTKSearchScope scope = SearchEngine.createWorkspaceScope(RubyLanguageToolkit
    .getDefault());
 
try {
    SearchEngine engine = new SearchEngine();
    engine.search(pattern, new SearchParticipant[] { SearchEngine
        .getDefaultSearchParticipant() }, scope, requestor, null);
} catch (CoreException e) {
    if (DLTKCore.DEBUG)
        e.printStackTrace();
}

How search works

Extending the search engine requires understanding of how the search engine works. When SearchEngine.search() is called, a special PatternSearchJob is created containing all the indexes being enumerated. As result the list of documents containing matching keys will be found. Next, each document is reparsed with a MatchLocator and appropriate SearchMatch objects are reported.

The DLTK user should provide an implementation of MatchLocatorParser to use for reparsing. This object will receive a MatchLocator and a PossibleMatch (indicating a candidate source file) and while parsing should invoke the match(...) method on the MatchLocator with the matches it determines.

Runtime model ("mixin" model)

DLTK has a very simple, but really powerful structure for managing runtime source files model. If model elements may be constructed from several source files(that's why model is called "mixin") or modified during execution, this approach can help.

In fact, mixin-model is built on top of the standard indexes facility. Mixin-parser(IMixinParser implementation, ext. point org.eclipse.dltk.core.mixin) reports keys(String objects) while parsing. One key may be reported many times from many places. To each key some Object may be attached. And... that's all. DLTK worries about everything else.

Lets consider the following example for Ruby.

file1.rb

class Foo # key "Foo" reported, IType object attached
end

class Foo # key "Foo" reported, IType object attached
	def doo # key "Foo{doo" reported, IMethod object attached
	end
end

file2.rb

class Foo # key "Foo" reported, IType object attached
	def doo2 # key "Foo{doo2" reported, IMethod object attached
	end
end

Now, if we'll ask model for Foo key(MixinModel.get() method), we'll fetch IMixinElement with information, that this key has been reported from file1.rb and file2.rb and we'll be able to get every IType object attached. More. "{" is a standard delimeter, so get can call getChilds() and fetch information about "Foo{doo" and "Foo{doo2" keys.

Type inference

DLTK has a language independent engine for building type inference systems. It uses demand-driven analysis with subgoal pruning algorithm. Key abstractions here:

  • Goal. It may be type of some ASTNode in source file, incoming/outcoming data flow, whatever. Goals are unique. So, two object of IGoal having a common ASTNode object is equal.
  • Goal evaluator. Strategy, that knows how to evaluate goal. While evaluating it may produce another helper goals (sub-goals), may wait for their results and only after that produce it's own result.
  • Goal evaluator factory. This factory constructs a GoalEvaluator objects for given IGoals.
  • Pruner. Pruner is a straregy that are able to cancel some evaluators as non-important. Also using pruner is possible to implement time limits for evaluations.

// TODO

Launching

Interpreter management

Each interpreter installation in system is stored inside a IInterpreterInstall object. Such object knows interpreter name, executable path, arguments, library paths and also installation type and IInterpreterRunner object. Installation type is represented via IInterpreterInstallType object. Installation type knows about all installations with such type, knows how to fetch default library locations and able to validate installations.

For each language, DLTK stores a separate set of IInterpreterInstalls. One of them should be marked as "default".

The key class for accessing installation is a ScriptRuntime.

However, ScriptRuntime allows only to fetch installs, not to modify them. In fact, there is no beautiful way for that. So here we'll describe relatively low-level way to do that.

All information about interpreter installs are stored in plugin preferences in XML format. XML data can be read/created using an InterpreterDefinitionsContainer. This class represent a set of interpreter installs and allows to read them from XML or store to XML.

For setting set of interpreters in preferences, there are exists an IntepreterUpdater class. It takes an InterpreterDefinitionsContainer object and stores in plugin preferences. After that ScriptRuntime will be able to read new values.

Launching a script

DLTK launching engine is fully based on standard Eclipse launching, so there are not a lot of new things are here.

Cause each InterpreterInstall has an IInterpreterRunner object, launch process looks like following:

  • get selected interpreter install info from launch configuration (it should be put there before)
  • get runner
  • launch it

In fact, all this stuff is already implemented. First, there is a AbstractScriptLaunchConfigurationDelegate class. It requires only two methods from user: getLanguageId() and createInterpreterConfig(). InterpreterConfig is a simple structure containing information for launch in a low-level form.

Also, for IInterpreterRunner exists a AbstractInterpreterRunner class, that requires from user only a launching plugin id and created process type.

For launching scripts programmatically there is a ScriptLaunchUtil class. It contains lots of methods for launching scripts.

DLTK AST

ASTNote Superclass of all the ast nodes.
ASTListNode Represents list of nodes.
ModuleDeclaration Top-level node for a source file.
TypeDeclaration Declaration of class/module/namespace.
MethodDeclaration Declaration of procedure or method.


Other classes you can find in org.eclipse.dltk.ast.* packages. Usage of DLTK AST is not mandatory, but some DLTK features like folding may rely on it and greatly simplify implementation.