[IronPython] Is it possible to get the DLR's AST when it fails to parse?

Lee Culver leculver at microsoft.com
Wed Dec 2 00:06:47 CET 2009

I have been playing with an implementation of a simple GUI console for IronPython (similar to IDLE, without it's text-editor IDE).  I've mostly brought it up to rough feature parity with IDLE, but I'd like to add a rudimentary itellisense such that when you type something on the order of "foo.", the interpreter can pop up a menu containing the contents of foo (assuming it's something already defined in the local scope).  However, I'd really rather not get in the business of parsing Python code.  (That's what the DLR is for!)

Is there any way I can use DLR to give me the AST up to the point at which it failed to parse so that I can walk that and see if we are in a valid place to look up the fields of an object?  I looked at using the ErrorListener class with ScriptSource.Compile, but it only gives me "unexpected <eof> found" at the point where the "." is...which I already knew.

Basically, whenever the user presses the "." key in the console what I would like to do is roughly this code:
    ScriptEngine engine = Python.CreateEngine();
    ScriptScope scope = engine.CreateScope();
    ScriptSource source = engine.CreateScriptSourceFromString("if foo.", Microsoft.Scripting.SourceCodeKind.InteractiveCode);

    source.Compile();  // <-- This *should* fail.  I want the AST when it does.

Primarily, I'm trying to find chains of identifiers to walk so that I can display the Nth identifier's fields:
                identifier1 . identifier2 . identifier3 .   ...

I need to distinguish this from things like this, because there's nothing you can reasonably do to have intellisense in this case:
                Function(args) . identifier1 . identifier2

(Yes, I realize that walking this chain can execute code and have side effects, I'm willing to live with that for my purposes.)  Is there any mechanism through which I can have the DLR parse this for me and give me the resulting AST that I can walk so I can see if we can pop up an intellisense-like view?

Can anyone recommend a better way to do this outside of simply writing my own mini-parser?

-Lee Culver

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20091201/db144cfb/attachment.html>

More information about the Ironpython-users mailing list