Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Dynamic Languages Toolkit (DLTK) » Query regarding DLTK JRubySourceParser
Query regarding DLTK JRubySourceParser [message #21963] Wed, 02 April 2008 03:50 Go to next message
Eclipse UserFriend
Originally posted by: pankaj.tolani.gmail.com

This is a multi-part message in MIME format.
--------------020302090608020006010100
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hi,

I am interested in using DLTK JRubySourceParser in my application for
Ruby. I have managed to use that in isolation. But I wish to know if I
can achieve more.

Here is what I wish to

A.rb

require "java"
load "B"

class A
def a

end
end

B.rb

class B
def b

end
end

Now I start with A.rb, I want the declarations in Ruby files I depend on
B.rb to be included as part of the ModuleDeclaration I get at the end of
the parse step. Is that possible? Essentially just like C preprocessor,
all included files should be compiled in.

thanks,
pankaj

P.S. I have attached the file which shows how I do the parsing.

--------------020302090608020006010100
Content-Type: text/plain;
name="NewAction.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="NewAction.java"

package dltk.ruby.source.parser.popup.actions;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.dltk.ast.declarations.ModuleDeclaration;
import org.eclipse.dltk.compiler.SourceElementRequestVisitor;
import org.eclipse.dltk.compiler.problem.DLTKProblemReporter;
import org.eclipse.dltk.compiler.problem.DefaultProblemFactory;
import org.eclipse.dltk.core.SourceParserUtil;
import org.eclipse.dltk.core.ISourceModuleInfoCache.ISourceModuleIn fo;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

public class NewAction implements IObjectActionDelegate {

ISelection selection;

/**
* Constructor for Action1.
*/
public NewAction() {
super();
}

/**
* @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
*/
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
}

/**
* @see IActionDelegate#run(IAction)
*/
public void run(IAction action) {
/*Shell shell = new Shell();
MessageDialog.openInformation(
shell,
"Parser Plug-in",
"New Action was executed.");*/
final IFile selectedFile = (IFile) ((IStructuredSelection) this.selection).getFirstElement();
parseSourceModule(selectedFile);
}

private void parseSourceModule(IFile selectedFile) {

char[] filename = selectedFile.getFullPath().toString().toCharArray();
StringBuilder fileContents = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new FileReader(selectedFile.getRawLocation().toFile()));
String str;
while ((str = in.readLine()) != null) {
fileContents.append(str);
fileContents.append('\n');
}
} catch (IOException e) {
e.printStackTrace();
}
char[] contents = fileContents.toString().toCharArray();

DLTKProblemReporter problemReporter = new DLTKProblemReporter(selectedFile, new DefaultProblemFactory());

/*ISourceParser sourceParserById = SourceParserManager.getInstance().getSourceParserById(getPar serId());
ModuleDeclaration moduleDeclaration = sourceParserById.parse(filename, contents, problemReporter);*/

ModuleDeclaration moduleDeclaration = SourceParserUtil.getModuleDeclaration(filename, contents, getNatureId(), problemReporter, astCache);

SourceElementRequestVisitor requestor = createVisitor();

try {
moduleDeclaration.traverse(requestor);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* @see IActionDelegate#selectionChanged(IAction, ISelection)
*/
public void selectionChanged(IAction action, ISelection selection) {
this.selection = selection;
}

private String getParserId() {
return "org.eclipse.dltk.ruby.parser.jrubySourceParser";
}

private String getNatureId() {
return "org.eclipse.dltk.ruby.core.nature";
}

protected SourceElementRequestVisitor createVisitor() {
return new MyRubySourceElementRequestVisitor(new MySourceElementRequestor());
}

private static class SourceModuleInfo implements ISourceModuleInfo {
private Map map;

public Object get(Object key) {
if (map == null) {
return null;
}
return map.get(key);
}

public void put(Object key, Object value) {
if (map == null) {
map = new HashMap();
}
map.put(key, value);
}

public void remove(Object key) {
if (map != null) {
map.remove(key);
}
}

public boolean isEmpty() {
if( this.map == null ) {
return true;
}
return this.map.isEmpty();
}
}

ISourceModuleInfo astCache = new SourceModuleInfo();

}

--------------020302090608020006010100--
Re: Query regarding DLTK JRubySourceParser [message #22185 is a reply to message #21963] Tue, 08 April 2008 06:57 Go to previous message
Andrei Sobolev is currently offline Andrei SobolevFriend
Messages: 72
Registered: July 2009
Member
Hi,

If you need specific load only support you could do following:

Write AST visitor to visit source module and process all load statements.
For each load statement you need to parse appropriate source module and include it contents.

Please see following classes for references:
ASTVisitor, CallExpression.

Best regards,
Andrei Sobolev.

> Hi,
>
> I am interested in using DLTK JRubySourceParser in my application for
> Ruby. I have managed to use that in isolation. But I wish to know if I
> can achieve more.
>
> Here is what I wish to
>
> A.rb
>
> require "java"
> load "B"
>
> class A
> def a
>
> end
> end
>
> B.rb
>
> class B
> def b
>
> end
> end
>
> Now I start with A.rb, I want the declarations in Ruby files I depend on
> B.rb to be included as part of the ModuleDeclaration I get at the end of
> the parse step. Is that possible? Essentially just like C preprocessor,
> all included files should be compiled in.
>
> thanks,
> pankaj
>
> P.S. I have attached the file which shows how I do the parsing.
>
Previous Topic:Signature change for IBuildpathContainer.getBuildpathEntries()
Next Topic:DLTK IDE Guide examples - missing getTextTools?
Goto Forum:
  


Current Time: Thu Jun 27 19:13:40 GMT 2024

Powered by FUDForum. Page generated in 0.02959 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top