forked from scriptcs-contrib/scriptcs-engine-mono
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyntaxParser.cs
More file actions
72 lines (61 loc) · 2.69 KB
/
Copy pathSyntaxParser.cs
File metadata and controls
72 lines (61 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ICSharpCode.NRefactory.CSharp;
namespace ScriptCs.SyntaxTreeParser
{
public class SyntaxParser
{
public ParseResult Parse(string code)
{
var result = new ParseResult();
var parser = new CSharpParser();
var syntaxTree = parser.Parse(code);
var codeLines = code.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
var codeLinesDictionary = new Dictionary<int, Tuple<string, bool>>();
for (int i = 0; i < codeLines.Length; i++)
{
codeLinesDictionary.Add(i, new Tuple<string, bool>(codeLines[i], true));
}
var typeMembersTree = parser.ParseTypeMembers(code);
foreach (var typeMember in typeMembersTree.Where(x => x is TypeDeclaration || x is MethodDeclaration))
{
var element = typeMember.GetText();
if (typeMember is TypeDeclaration)
{
result.Declarations += element;
}
//else
//{
// result.Declarations += "public static partial ScriptCsMethod {";
// result.Declarations += element;
// result.Declarations += "}";
//}
for (var i = typeMember.StartLocation.Line - 1; i < typeMember.StartLocation.Line; i++)
{
var oldItem = codeLinesDictionary[i];
codeLinesDictionary[i] = new Tuple<string, bool>(oldItem.Item1, false);
}
}
var keysToRemove = codeLinesDictionary.Where(x => x.Value.Item2 == false).Select(i => i.Key);
keysToRemove.ToList().ForEach(x => codeLinesDictionary.Remove(x));
foreach (var correct in syntaxTree.Members)
{
var element = correct.GetText(); ;
result.Declarations += element;
}
if (syntaxTree.Errors.Any())
{
var evalLines = codeLines.Skip(syntaxTree.Errors.First().Region.BeginLine - 1).ToList();
result.Evaluations += string.Join(Environment.NewLine, evalLines);
//result.Evaluations = "public void ScriptCsInvoke() {" + Environment.NewLine;
//result.Evaluations = string.Join(Environment.NewLine, codeLinesDictionary.Select(i => i.Value.Item1));
//result.Evaluations += Environment.NewLine + "}";
}
var evaluationTree = parser.ParseStatements(result.Evaluations);
return result;
}
}
}