views:

389

answers:

2

I need to create a code editor for my own simple language:

className.MethodName(parameterName = 2, ... )

I've created the appropriate grammar and autogenerate parser using ANTLR tool. Now I would like to have an autocomplete for class, method, variables and parameter names. This list should be context dependent, f.e. for "class." it should display methods and for "class.Method(" - parameters. I was going to parse the text and display the list depending on in which node the cursor is. The problem is that for incomplete code like "aaa.bbb(" the parser produces an error instead of a syntax tree. Any idea how to solve this problem? Maybe I'm on the wrong way and I shouldn't parse code to display autocomplete?

A: 

how about TextMate on the Mac or its clones on other platforms? I've just started with it, but it seems to have a very open plugin toolset.

kenny
+1  A: 

You need to parse at least some parts of the code to know what to display.

You either need a parser that allows parsing until it finds an error and continues from there later (or even just ignore it and tries to fix it itself to be able to continue parsing — but it gets very difficult here, so you can access the last node and use your grammar and the existing syntax tree to autocomplete it, or you cache the latest syntax tree and use something like regex to see when to autocomplete and use the cache to see what to autocomplete.

The later suggestion might be the easier one but has the disadvantage that the cache might be out-to-date at the time you need the completion (i.e.: you might create a new class in a file, all completions based on that class won’t be available until the file has no errors).

Sidenode: Seeing that you do parsing, you might be interested in Parsing Expression Grammars, they don’t exactly help you with that problem, though.

Marenz