<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page Section1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
        {page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=EN-US link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal>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!)<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>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.<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>Basically, whenever the user presses the “.” key
in the console what I would like to do is roughly this code:<o:p></o:p></p>
<p class=MsoNormal style='text-autospace:none'><span style='font-size:8.0pt;
font-family:Consolas'> <span style='color:#2B91AF'>ScriptEngine</span>
engine = <span style='color:#2B91AF'>Python</span>.CreateEngine();<o:p></o:p></span></p>
<p class=MsoNormal style='text-autospace:none'><span style='font-size:8.0pt;
font-family:Consolas'> <span style='color:#2B91AF'>ScriptScope</span>
scope = engine.CreateScope();<o:p></o:p></span></p>
<p class=MsoNormal style='text-autospace:none'><span style='font-size:8.0pt;
font-family:Consolas'> <span style='color:#2B91AF'>ScriptSource</span>
source = engine.CreateScriptSourceFromString(<span style='color:#A31515'>"if
foo."</span>, Microsoft.Scripting.<span style='color:#2B91AF'>SourceCodeKind</span>.InteractiveCode);<o:p></o:p></span></p>
<p class=MsoNormal style='text-autospace:none'><span style='font-size:8.0pt;
font-family:Consolas'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:8.0pt;font-family:Consolas'>
source.Compile(); <span style='color:green'>// <-- This *should*
fail. I want the AST when it does.</span></span><span style='font-size:
8.0pt;font-family:Consolas'><o:p></o:p></span></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>Primarily, I’m trying to find chains of identifiers to
walk so that I can display the Nth identifier’s fields:<o:p></o:p></p>
<p class=MsoNormal> identifier1
. identifier2 . identifier3 . …<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>I need to distinguish this from things like this, because
there’s nothing you can reasonably do to have intellisense in this case:<o:p></o:p></p>
<p class=MsoNormal> Function(args)
. identifier1 . identifier2<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>(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?<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>Can anyone recommend a better way to do this outside of
simply writing my own mini-parser?<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>Thanks,<o:p></o:p></p>
<p class=MsoNormal>-Lee Culver<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
</div>
</body>
</html>