<div>That's only because you've decided to arbitrarily[*] define "\n\n" as being a signal to mean "complete". That's not part of the actual language specification. In fact, if I append "\n\n print 2\n\n" to that string, it's still a valid Python program. The key here is that "the user has entered a complete thought" is a property of the interpreter and not of the language. I might well decide that the "commit" key sequence is Control+E (as it is in SQL Server Management Studio) instead of "enter enter".</div>
<div> </div>
<div>My point is that it's not correct for IronPython to dictate the semantics of your interpreter.</div>
<div> </div>
<div>[*] Okay, "arbitrary" is a bit strong in that it's what python.exe and ipy.exe defines. :) <br><br></div>
<div class="gmail_quote">On Thu, Apr 16, 2009 at 10:36 AM, Michael Foord <span dir="ltr"><<a href="mailto:fuzzyman@voidspace.org.uk">fuzzyman@voidspace.org.uk</a>></span> wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">
<div class="im">Curt Hagenlocher wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">"IncompleteStatement" means that the user is allowed to type more code. If you want to know whether or not it's a valid (complete) string, just check for it not being Invalid. A function definition is never "complete" in Python because there's never a terminating curly brace :).<br>
</blockquote><br></div>But that isn't sufficient to implement an interactive interpreter on top of. This code conceptually is complete as far as an interactive interpreter is concerned:
<div class="im"><br><br> 'def f():\n print 1\n\n'<br><br></div>It also means you can't distinguish between the previous kind of incomplete (which is incomplete because the user *could* type more code) and this kind of incomplete:<br>
<br> 'a = """'<br><br>or:<br><br> 'a = (1 + 2 +'<br><br>Which are both incomplete because the user *must* type more code. (Although the latter two give IncompleteToken - I wonder if that would be enough.)<br>
<br>Because of the other IronPython bugs we can't use the code module and ScriptSource / ScriptParseResult doesn't give sufficient information. Any other ideas?<br><br>Michael<br><br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">
<div>
<div></div>
<div class="h5"><br>On Thu, Apr 16, 2009 at 10:05 AM, Michael Foord <<a href="mailto:fuzzyman@voidspace.org.uk" target="_blank">fuzzyman@voidspace.org.uk</a> <mailto:<a href="mailto:fuzzyman@voidspace.org.uk" target="_blank">fuzzyman@voidspace.org.uk</a>>> wrote:<br>
<br> Hello guys,<br><br> We're trying to detect whether a section of code is complete (to<br> mimic the behaviour of the interactive interpreter).<br><br> First of all we tried using the Python standard library code<br>
module which provides interactive console classes. There are two<br> outstanding bugs on codeplex (one reported by me today) which<br> prevent this being an ideal solution:<br><br> <a href="http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=22064" target="_blank">http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=22064</a><br>
<a href="http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21881" target="_blank">http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21881</a><br><br> The second approach was to create a ScriptSource and looking at<br>
the code properties to tell if the statement is complete or not<br> (using IronPython 2.0.1). However we can never get it to return a<br> ScriptParseResult.Complete for function definitions. Code below<br> shows using \n for newlines but we have also tried with \r\n.<br>
<br> >>> import clr<br> >>> clr.AddReference('IronPython')<br> >>> clr.AddReference('Microsoft.Scripting')<br> >>> from IronPython.Hosting import Python<br> >>> from Microsoft.Scripting import SourceCodeKind,<br>
ScriptCodeParseResult<br> >>><br> >>> engine = Python.CreateEngine()<br> >>> s = engine.CreateScriptSourceFromString('def f():\n print<br> 1\n', 'foo', SourceCodeKind.InteractiveCode)<br>
>>> s.GetCodeProperties()<br> <Microsoft.Scripting.ScriptCodeParseResult object at<br> 0x000000000000003F [IncompleteStatement]><br> >>> s = engine.CreateScriptSourceFromString('def f():\n print<br>
1\n\n', 'foo', SourceCodeKind.InteractiveCode)<br> >>> s.GetCodeProperties()<br> <Microsoft.Scripting.ScriptCodeParseResult object at<br> 0x0000000000000040 [IncompleteStatement]><br> >>><br>
<br> The DLR hosting spec has little helpful to say on the matter as<br> far as I can tell.<br><br> Looking at an example from Tomas it doesn't seem very different<br> from what we're doing:<br><br> <a href="http://blog.tomasm.net/2009/04/15/python-says-hello-to-ruby/" target="_blank">http://blog.tomasm.net/2009/04/15/python-says-hello-to-ruby/</a><br>
<br> Any clues as to what we are doing wrong or how to procede?<br><br> Thanks<br><br> Michael<br><br> -- <a href="http://www.ironpythoninaction.com/" target="_blank">http://www.ironpythoninaction.com/</a><br> <a href="http://www.voidspace.org.uk/blog" target="_blank">http://www.voidspace.org.uk/blog</a><br>
<br><br> _______________________________________________<br> Users mailing list<br></div></div> <a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a> <mailto:<a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a>>
<div class="im"><br> <a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br><br><br></div>------------------------------------------------------------------------
<div class="im"><br><br>_______________________________________________<br>Users mailing list<br><a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a><br><a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
<br></div></blockquote>
<div>
<div></div>
<div class="h5"><br><br>-- <br><a href="http://www.ironpythoninaction.com/" target="_blank">http://www.ironpythoninaction.com/</a><br><a href="http://www.voidspace.org.uk/blog" target="_blank">http://www.voidspace.org.uk/blog</a><br>
<br><br>_______________________________________________<br>Users mailing list<br><a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a><br><a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
</div></div></blockquote></div><br>