I'm building a REPL with Roslyn.

How can I figure out if the input to the session is only partially complete:

> 1+1;
> 2 //that works
> int MyMethod(string a){
  //error here, i'd like to continue writing the expression

This code shows the problem:

var engine = new ScriptEngine();
var session = engine.CreateSession();
var input = "int MyMethod(string a){";
session.Execute(input); //Exception: error CS1513: } expected

What do I have to do to find out if the input is complete or not? So I can show another line in my REPL until the expression is complete.

有帮助吗?

解决方案

The interactive window that is part of the CTP uses the Syntax.IsCompleteSubmission API for this purpose.

其他提示

You can use SyntaxTree.ParseText and GetDiagnostics() to check if syntax is correct. If not, you will probably want to append this line to some buffer and check this buffer until it will be valid.

var st = SyntaxTree.ParseText(input);
var errors = st.GetDiagnostics();

Note: there always be some error (even for simple 1+1 expression) so you will have to filter it out by default:

enter image description here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top