forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptSegmenter.cs
More file actions
83 lines (74 loc) · 2.81 KB
/
Copy pathScriptSegmenter.cs
File metadata and controls
83 lines (74 loc) · 2.81 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
73
74
75
76
77
78
79
80
81
82
83
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using ScriptCs.Engine.Mono.Segmenter.Analyser;
using ScriptCs.Engine.Mono.Segmenter.Parser;
namespace ScriptCs.Engine.Mono.Segmenter
{
public class ScriptSegmenter
{
public List<SegmentResult> Segment(string code)
{
const string ScriptPattern = @"#line 1.*?\n";
var isScriptFile = Regex.IsMatch(code, ScriptPattern);
if (isScriptFile)
{
// Remove debug line
code = Regex.Replace(code, ScriptPattern, "");
}
var analyser = new CodeAnalyzer();
var result = new List<SegmentResult>();
using (var parser = new RegionParser())
{
foreach (var region in parser.Parse(code))
{
// Calculate region linenumber
var lineNr = code.Substring(0, region.Offset).Count(x => x.Equals('\n'));
var segment = code.Substring(region.Offset, region.Length);
if (analyser.IsClass(segment))
{
result.Add(new SegmentResult
{
Type = SegmentType.Class,
BeginLine = lineNr,
Code = segment
});
}
else
{
var isMethod = analyser.IsMethod(segment);
if (isMethod)
{
// method ok
var method = analyser.ExtractPrototypeAndMethod(segment);
result.Add(new SegmentResult
{
Type = SegmentType.Prototype,
BeginLine = lineNr,
Code = method.ProtoType
});
result.Add(new SegmentResult
{
Type = SegmentType.Method,
BeginLine = lineNr,
Code = method.MethodExpression
});
}
else
{
result.Add(new SegmentResult
{
Type = SegmentType.Evaluation,
BeginLine = lineNr,
Code = segment
});
}
}
}
}
return result
.OrderBy(x => x.Type)
.ToList();
}
}
}