scriptcs/scriptcs — pull request #771 by ztone (2014-06-23 15:39:28+00:00)
Title: Code segments

This PR is a proposed solution to get the Mono engine close to be at par with Roslyn and solve most of the Mono issues.

* Fixes #713 (capture local vars)
* Blocks in script no longer interferes with parsing (had an commented out test for this in SyntaxParserTests + had this problem when investigating #740)
* Fixes #664 (Mono error reporting)
* Returns compile errors in ScriptResult from Mono.CSharp and NRefactory (when rewriting methods) and displays the correct line of the erroneous code (not supported before).
* Partly addresses #665 (does still not support loose functions in multi-line)

**Why this approach**
NRefactory failed to parse correctly if script contained blocks { }
Needed a way to fix #713

**What this PR does**
So the approach I've taken is to split the script into code segments.

* The lexer : find a few token needed for parsing and disregard stuff in strings, chars and comments so it doesn't interfere with the parsing.

* Region parser: Eats all tokens until it find either a semicolon or an opening/closed bracket.

For example Console.WriteLine(“hello”); is a region and if(true) { doStuff(); } is also a region. The lexer and parser don't care about the syntax or semantics, it's just used to do a logical split of the code.

* NRefactory: Each segment is then passed into the NRefactory parser to check if the segment is a class, method or evaluation. It the segment is a method it is rewritten by NRefactory to a method expression to support loose functions.

Segments are also ordered as we want to evaluate classes first, then method prototypes. After that we evaluate method expressions and lastly the evaluation.

If you look at ScriptCs code this approach does make some sense.
```c#
class A { you can write anything here }

void Bar() { also here }

Bar( even inside parentheses );
```
Here we identify three regions/segments. The Mono.CSharp evaluator will handle all the compiler errors that are inside each segment (and NRefactory for methods).

**Other**
* Mono engine will return the correct line number for a compilation errors. However, a few method errors are still a bit strange because of the method rewrite. Needs investigating.

* Before, code like error. didn't cause any failure (as the evaluator regards this as a partial, because of the dot). Handling partial loose functions is going to be a bit tricker and needs to be in another PR.

Please test drive.

Stone