Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #771 from ztone/code-segments
Mono engine using code segments to better parse scripts
- Loading branch information
Showing
23 changed files
with
1,174 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/ScriptCs.Engine.Mono/Segmenter/Analyser/CodeAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
using ScriptCs.Engine.Mono.Segmenter.Analyser.Visitors; | ||
|
||
using ICSharpCode.NRefactory.CSharp; | ||
|
||
namespace ScriptCs.Engine.Mono.Segmenter.Analyser | ||
{ | ||
public class CodeAnalyzer | ||
{ | ||
public bool IsClass(string code) | ||
{ | ||
var visitor = new ClassTypeVisitor(); | ||
var parser = new CSharpParser(); | ||
var syntaxTree = parser.Parse(code); | ||
syntaxTree.AcceptVisitor(visitor); | ||
syntaxTree.Freeze(); | ||
|
||
return visitor.GetClassDeclarations().Any(); | ||
} | ||
|
||
public bool IsMethod(string code) | ||
{ | ||
var @class = "class A { " + code + " } "; | ||
var visitor = new MethodVisitor(); | ||
var parser = new CSharpParser(); | ||
var syntaxTree = parser.Parse(@class); | ||
syntaxTree.AcceptVisitor(visitor); | ||
syntaxTree.Freeze(); | ||
|
||
return visitor.GetMethodDeclarations().Any() && code.TrimEnd().EndsWith("}"); | ||
} | ||
|
||
public MethodResult ExtractPrototypeAndMethod(string code) | ||
{ | ||
var @class = "class A { " + code + " } "; | ||
var visitor = new MethodVisitor(); | ||
var parser = new CSharpParser(); | ||
var syntaxTree = parser.Parse(@class); | ||
syntaxTree.AcceptVisitor(visitor); | ||
syntaxTree.Freeze(); | ||
|
||
var result = visitor.GetMethodDeclarations().FirstOrDefault(); | ||
|
||
// find newlines in method signature to maintain linenumbers | ||
var newLines = code.Substring(0, code.IndexOf("{", StringComparison.Ordinal) - 1) | ||
.Where(x => x.Equals('\n')) | ||
.Aggregate(string.Empty, (a, c) => a + c); | ||
|
||
// use code methodblock to maintain linenumbers | ||
var codeBlock = code.Substring(code.IndexOf("{", StringComparison.Ordinal), code.LastIndexOf("}", StringComparison.Ordinal) - code.IndexOf("{", StringComparison.Ordinal) + 1); | ||
var method = result.MethodExpression.GetText(); | ||
var blockStart = method.IndexOf("{", StringComparison.Ordinal); | ||
var blockEnd = method.LastIndexOf("}", StringComparison.Ordinal); | ||
method = method.Remove(blockStart, blockEnd - blockStart + 1); | ||
method = method.Insert(blockStart, codeBlock); | ||
|
||
return new MethodResult | ||
{ | ||
ProtoType = result.MethodPrototype.GetText().Trim() + newLines, | ||
MethodExpression = newLines + method.Trim() | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace ScriptCs.Engine.Mono.Segmenter.Analyser | ||
{ | ||
public class MethodResult | ||
{ | ||
public string ProtoType { get; set; } | ||
|
||
public string MethodExpression { get; set; } | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
....Mono/Parser/Visitors/ClassTypeVisitor.cs → ...ter/Analyser/Visitors/ClassTypeVisitor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
...no/Parser/Visitors/MethodVisitorResult.cs → .../Analyser/Visitors/MethodVisitorResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
namespace ScriptCs.Engine.Mono.Parser.Visitors | ||
{ | ||
using ICSharpCode.NRefactory.CSharp; | ||
using ICSharpCode.NRefactory.CSharp; | ||
|
||
namespace ScriptCs.Engine.Mono.Segmenter.Analyser.Visitors | ||
{ | ||
public class MethodVisitorResult | ||
{ | ||
public MethodDeclaration MethodDefinition { get; set; } | ||
|
||
public VariableDeclarationStatement MethodPrototype { get; set; } | ||
|
||
public ExpressionStatement MethodExpression { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace ScriptCs.Engine.Mono.Segmenter.Lexer | ||
{ | ||
public class LexerResult | ||
{ | ||
public int Token { get; set; } | ||
public string TokenValue { get; set; } | ||
public int Start { get; set; } | ||
public int End { get; set; } | ||
} | ||
} |
Oops, something went wrong.