Source parsing
Working on some code completion feature, I need to parse the source that the user has typed. I've studied ANTLR and JavaCC, but it seems not-so-obvious how to pass the source to some parser, that returns me the type of the object the user has his caret on.
private com.foo.bar.MyObject blah;
....
blah.| < caret is here
I want to know that blah is an object of type com.foo.bar.MyObject. What am I supposed to use: an AST, a Parser or a Lexer? And how to generate one?
Thanks all for thinking with me.
> I want to know that blah is an object of type com.foo.bar.MyObject.
> What am I supposed to use: an AST, a Parser or a Lexer? And how to
> generate one?
What you want is beyond lexical analysis and context free parsing. That's
where the semantic analyzer comes in: your system should (at the least)
has to have a notion of (nested) symbol tables and type information, i.e.
given a symbol ("blah"), what is its type in the current context. Only then
can your system reliable show the alternative continuations given its
current state.
If you are able to complete that you have almost written a complete compiler:
all that it's lacking then is a code generator and possibly a code optimizer.
kind regards,
Jos
This is a scripting environment for our applicaton. I incorporated BeanShell to automate tasks using user-scripts. The editor is just a JTextPane, and I want to soup it up a bit with Syntax Highlighting, Code Completion, you know, the nice stuff :)
The scripts are running fine. It runs very smooth, except the editor sucks. I am used to the very slick interface of IntelliJ, so I wanted it to look like that... But I have a feeling this fancy bit is just a little too much.
> @Jos: Sounds like this is not going to happen :)
Then forget all about it; all you can do then is show the possible
continuations on a syntactical level but if you do so, most of the alternatives
will be garbage considering the semantical context.
Continuations are just the 'follow' set of the current token (which by itself
is an element of the 'first' set of the current grammar rule). Read the fine
'Dragon Book' for an in depth explanation of those two darn sets ;-)
kind regards,
Jos