Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Developing a AST parser plugin, but starting as a standalone first(FIRST QUESTION OF MANY.)
Developing a AST parser plugin, but starting as a standalone first [message #1730044] Thu, 21 April 2016 07:08 Go to next message
Kauan Klein is currently offline Kauan KleinFriend
Messages: 8
Registered: April 2016
Location: Brazil, RJ, Petrópolis
Junior Member
this is the first question of what I indent to be be a series of question consolidated as one thread.

Since the tutorials about plugins are either cryptic with very little information usable by a plugin dev beginer (e.g. Incremental builders and Project Natures) or too simple to be useful in anyway (Hello world)I decided to make my AST parser as a standalone.

the purpose of this program is to parse a java project and take into considerations certain annotations (mainly the @Const annotation, whose javadoc I'll provide bellow) and make sure the code enforces those annotations.

That all being said as an introduction to the context of the project, what I'd really like to know is the actual different between using the visitor pattern or the query methods of an AST. Is there any difference between


CompilationUnit#accept(new TypeDeclarationVisitor()
{
    void visit (TypeDeclaration decl)
    {

    }
});


and

TypeDeclaration typeDecl = CompilationUnit#types()
//continue down the AST tree, doing the same for methods, fields, parameters, statements, etc.


(and then progress this way until the leaf nodes of the AST, without ever implementing the visitor pattern)

My goal is, once I have the standalone working, I'll bother about turning it into a plugin (hopefully with the help of someone knowledgful and interested in teaching).
Re: Developing a AST parser plugin, but starting as a standalone first [message #1730091 is a reply to message #1730044] Thu, 21 April 2016 10:55 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Kauan Klein wrote on Thu, 21 April 2016 09:08
... I'd really like to know is the actual different between using the visitor pattern or the query methods of an AST.


The purpose of visitors (sub types of ASTVisitor) is to traverse the entire AST, so you can inspect every single AST node with little effort (unless a visit method returns false, at which point the sub-tree below the current node is skipped).

When directly querying the AST, you are responsible of traversing to all interesting nodes.

For the example of the TypeDeclarationVisitor (which btw is an internal class designed as part of the search engine) you automatically get invoked for each type in the current AST including nested and local types. When using query methods you have to manually do that traversal. But when implemented correctly both approaches should exhibit the same behaviour. Thus differences are in the amount of code needing to be written and clarity of code (a trained eye will immediately understand usage of a visitor, but needs to carefully read the manual traversal).

HTH,
Stephan
Re: Developing a AST parser plugin, but starting as a standalone first [message #1730364 is a reply to message #1730091] Mon, 25 April 2016 10:13 Go to previous messageGo to next message
Kauan Klein is currently offline Kauan KleinFriend
Messages: 8
Registered: April 2016
Location: Brazil, RJ, Petrópolis
Junior Member
Thank you. I learned a lot since the first question andt he project is no longer a standalone, but a plugin now. I have another question, though:

I want to check if a variable (local, field, parameter, anything) participates in any side of an assignment, but assignments can be complex expressions. rather than a simple a = b; we can have something like this:

SomeType.SomeStaticMethod(someParams).someInstanceMethod(someMoreParams).fieldGetter() = [Another Expression of arbitrary complexity]


How can I know the final Names that are the left and right side of that assignment?

currently, I have this:

		@Override
		public boolean visit(SimpleName node)
		{
			IBinding bind = node.resolveBinding();
			if(bind.getKind() == IBinding.VARIABLE)
			{
				IVariableBinding vBind = (IVariableBinding)bind;
				if(node.getParent().getNodeType() == ASTNode.ASSIGNMENT)
				{
					//I know the name participates in a direct assignment like a = b;
					//but I don't think this covers an arbitraryly complex expression
				}
			}
		}


(more similar questions may follow).
Re: Developing a AST parser plugin, but starting as a standalone first [message #1730563 is a reply to message #1730364] Tue, 26 April 2016 16:52 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
To learn the pure AST structure, install https://www.eclipse.org/jdt/ui/astview/index.php
Re: Developing a AST parser plugin, but starting as a standalone first [message #1730573 is a reply to message #1730563] Tue, 26 April 2016 18:36 Go to previous messageGo to next message
Kauan Klein is currently offline Kauan KleinFriend
Messages: 8
Registered: April 2016
Location: Brazil, RJ, Petrópolis
Junior Member
Stephan Herrmann wrote on Tue, 26 April 2016 13:52
To learn the pure AST structure, install https://www.eclipse.org/jdt/ui/astview/index.php


I have that installed. I can see the structure of the AST, what I don't know is how to navigate through it the way I need to, described above.
Re: Developing a AST parser plugin, but starting as a standalone first [message #1730877 is a reply to message #1730364] Fri, 29 April 2016 12:33 Go to previous message
Kauan Klein is currently offline Kauan KleinFriend
Messages: 8
Registered: April 2016
Location: Brazil, RJ, Petrópolis
Junior Member
please, this question has also been posted in http://stackoverflow.com/questions/36826136/how-do-i-get-to-the-bottom-of-an-ast-expression. Please someone answer it. I'm offerinf a 50 reputation points bounty for the answer, and even so, the only answer I have is unsatisfactory. I really need help with this one, I'm growing increasingly ansious and depressed over not being able to continue my project because I'm stuck at this.
Previous Topic:unmappable character for encoding UTF8 when compiling by Eclipse
Next Topic:Is there a plugin to help create Printable?
Goto Forum:
  


Current Time: Sun Oct 13 07:34:02 GMT 2024

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

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

Back to the top