Reputation: 1547
I am wondering is there a way to get the AST of an incomplete code fragment that the user inputs. For example, lets imagine that the code looks something like this:
String str = "This is some string";
int length = str.length +
Can I get the AST of such code? This is not syntactically correct, but I would still need the AST.
Additionally, would I be able to inject such code into the text editor? Ideally, I would add it as a string, but AST format could do as well.
Update 1:
To be precise, would I be able to use the ASTParser in some way so I get something like the following AST for the last line:
=
/ \
decl length +
/
str.length
Partial Answer:
JDT's ASTParser can be used with setStatementsRecovery which will yield the following AST:
=
/ \
decl length str.length
It is not exactly the desired solution, but someone may find it useful, like I did.
Upvotes: 1
Views: 143
Reputation: 776
I don't understand what you are trying to achieve exactly, but depending on your syntax and the tools used, you could get an incomplete AST. From your code, such an AST could be as follows:
__ = __
/ \
decl length fail
Upvotes: 1