문제

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