Skip to main content



      Home
Home » Eclipse Projects » Dynamic Languages Toolkit (DLTK) » Handle field declaration in parser(Writing a DLTK parser, you handle to handle nodes such as classes and methods. What about fields)
Handle field declaration in parser [message #531830] Thu, 06 May 2010 06:06 Go to next message
Eclipse UserFriend
Hi,

In the parser I wrote I manage to handle classes and methods declaration, but I am facing some issues handling fields. As instance, for the following code:
	public ModuleDeclaration getRoot() {
		// Defining module
		ModuleDeclaration m = new ModuleDeclaration(25);

		// Defining class
		TypeDeclaration dec = new TypeDeclaration("class", 0, 5, 0, 25);

		// Defining method and attribute
		Declaration method = new MethodDeclaration("method", 10, 16, 10, 25);
		Declaration attibute = new FieldDeclaration("field", 20, 25, 20, 25);

		// Place method and attribute in class
		Block block = new Block();
		block.addStatement(method);
		block.addStatement(attibute);
		dec.setBody(block);

		// Place class in module
		m.addStatement(dec);
		return m;
	}

Checking outline I do have class and method, but no field at all. Am I using the wrong type for fields? Do I need to override FieldDeclartion's getKind() Method? Even reading the code of existing editors I do not see the difference. I'm pretty lost about It.

Regards

[Updated on: Thu, 06 May 2010 06:24] by Moderator

Re: Handle field declaration in parser [message #532660 is a reply to message #531830] Mon, 10 May 2010 13:56 Go to previous messageGo to next message
Eclipse UserFriend
Hi Kevin,

I suppose you use the base implementation of SourceElementRequestVisitor and it doesn't handle fields, only methods & types.
You can extend it - override createVisitor() of your AbstractSourceElementParser and create your own visitor .

Regards,
Alex
Re: Handle field declaration in parser [message #533232 is a reply to message #532660] Wed, 12 May 2010 12:02 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

You we're definitely right, I did extend SourceElementRequestVisitor, without override any method. So I proceeded as you suggested with the following code.
public class LuaSourceElementRequestor extends SourceElementRequestVisitor {

	public LuaSourceElementRequestor(ISourceElementRequestor requesor) {
		super(requesor);
	}

	public boolean visit(MethodDeclaration method) throws Exception {
		return super.visit(method);
	}

	public boolean visit(FieldDeclaration field) throws Exception {
		return true;
	}

	public boolean endvisit(FieldDeclaration f) throws Exception {
		return true;
	}
}
While running it with the debugger an pertinent AST, the visit method for MethodDeclaration objects is called but not for FieldDeclaration ones. Is there any further interface to implement? What did I missed?
Regards

[Updated on: Wed, 12 May 2010 12:07] by Moderator

Re: Handle field declaration in parser [message #533586 is a reply to message #533232] Fri, 14 May 2010 12:08 Go to previous message
Eclipse UserFriend
My bad, I thought that the visitor was using the object type for its visit. But you must cast it yourself. So I tried it this way. I hope it's good.

Regards
public class LuaSourceElementRequestor extends SourceElementRequestVisitor {

	public LuaSourceElementRequestor(ISourceElementRequestor requesor) {
		super(requesor);
	}

	@Override
	public boolean visit(Statement s) throws Exception {
		if (s instanceof FieldDeclaration) {
			return visit((FieldDeclaration) s);
		}
		return super.visit(s);
	}

	public boolean visit(FieldDeclaration f) throws Exception {
		return true;
	}
}

[Updated on: Wed, 19 May 2010 10:39] by Moderator

Previous Topic:[BUILD] I-I201005130427-201005130427
Next Topic:[BUILD] S-2.0RC1-201005190624
Goto Forum:
  


Current Time: Tue Jul 15 14:07:45 EDT 2025

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

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

Back to the top