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.

[575 byte] By [SurfManNLa] at [2007-9-25]
# 1

> 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

JosAHa at 2007-7-14 > top of java,Java Essentials,Java Programming...
# 2
Netbeans does this stuff and is open source. Why not dig into their code and see how they do it?
malcolmmca at 2007-7-14 > top of java,Java Essentials,Java Programming...
# 3
@Jos: Sounds like this is not going to happen :)@malcolmmc: good point, downloading now...
SurfManNLa at 2007-7-14 > top of java,Java Essentials,Java Programming...
# 4
This could get fairly complicated. You need to worry about scope and whatnot. Extended types and the like. This seems like a fairly in depth project. What're you writing (out of curriosity)
Norweeda at 2007-7-14 > top of java,Java Essentials,Java Programming...
# 5

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.

SurfManNLa at 2007-7-14 > top of java,Java Essentials,Java Programming...
# 6

> @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

JosAHa at 2007-7-14 > top of java,Java Essentials,Java Programming...
# 7
Sounds like a GREAT idea, but it's a lot of work for an apparent (managerially at least) short slide.
Norweeda at 2007-7-14 > top of java,Java Essentials,Java Programming...