Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 10:06 Go to next message
Kevin KIN-FOO is currently offline Kevin KIN-FOOFriend
Messages: 58
Registered: January 2010
Member
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 10:24]

Report message to a moderator

Re: Handle field declaration in parser [message #532660 is a reply to message #531830] Mon, 10 May 2010 17:56 Go to previous messageGo to next message
Alex Panchenko is currently offline Alex PanchenkoFriend
Messages: 342
Registered: July 2009
Senior Member
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 16:02 Go to previous messageGo to next message
Kevin KIN-FOO is currently offline Kevin KIN-FOOFriend
Messages: 58
Registered: January 2010
Member
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 16:07]

Report message to a moderator

Re: Handle field declaration in parser [message #533586 is a reply to message #533232] Fri, 14 May 2010 16:08 Go to previous message
Kevin KIN-FOO is currently offline Kevin KIN-FOOFriend
Messages: 58
Registered: January 2010
Member
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 14:39]

Report message to a moderator

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


Current Time: Sat Apr 20 02:46:16 GMT 2024

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

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

Back to the top