-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
CSharpTermInfoMutator.cs
141 lines (125 loc) · 4.88 KB
/
CSharpTermInfoMutator.cs
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using Newtonsoft.Json.Linq;
using Templates.CodeGen.Util;
namespace Templates
{
public class CSharpTermInfoMutator
{
public static string[] Keywords =
{
"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char",
"checked", "class", "const", "continue", "decimal", "default", "delegate",
"do", "double", "else", "enum", "event", "explicit", "extern", "false",
"finally", "fixed", "float", "for", "forech", "goto", "if", "implicit",
"in", "int", "interface", "internal", "is", "lock", "long", "namespace",
"new", "null", "object", "operator", "out", "override", "params", "private",
"protected", "public", "readonly", "ref", "return", "sbyte", "sealed", "short",
"sizeof", "stackalloc", "static", "string", "struct", "switch", "this", "throw",
"true", "try", "uint", "ulong", "unchecked", "unsafe", "ushort",
"using", "virtual", "volatile", "void", "while",
};
public static string[] ObjectMethods =
{
"Equals", "ToString", "GetHashCode", "GetType"
};
public static string[] Blacklist =
{
"row"
};
public static NameValueCollection TokenMutate = new NameValueCollection()
{
{"TIME.signatures[0].args[0].type", "int"},
{"TIME.signatures[0].args[1].type", "int"},
{"TIME.signatures[0].args[2].type", "int"},
{"TIME.signatures[0].args[3].type", "int"},
{"TIME.signatures[1].args[0].type", "int"},
{"TIME.signatures[1].args[1].type", "int"},
{"TIME.signatures[1].args[2].type", "int"},
{"TIME.signatures[1].args[3].type", "int"},
{"TIME.signatures[1].args[4].type", "int"},
{"TIME.signatures[1].args[5].type", "int"},
{"TIME.signatures[1].args[6].type", "int"},
};
private readonly Dictionary<string, JObject> allTerms;
public CSharpTermInfoMutator(Dictionary<string, JObject> allTerms)
{
this.allTerms = allTerms;
}
public void EnsureLanguageSafeTerms()
{
DeleteIfBlackListed();
MutateMethodName();
MutateParameterArguments();
MutateImpelments();
MutateSignatures();
}
private void MutateSignatures()
{
foreach( var termInfo in allTerms )
{
var term = termInfo.Key;
var info = termInfo.Value;
}
}
private void MutateImpelments()
{
foreach( var termInfo in allTerms )
{
var term = termInfo.Key;
var info = termInfo.Value;
}
}
private void MutateParameterArguments()
{
foreach( var termInfo in allTerms )
{
var term = termInfo.Key;
var info = termInfo.Value;
}
}
private void MutateClassName()
{
//nothing to do.... yet.
}
private void MutateMethodName()
{
foreach( var termInfo in allTerms )
{
var term = termInfo.Key;
var info = termInfo.Value;
var proposedNames = info["methodnames"].ToObject<string[]>();
var lst = new List<string>();
foreach( var proposedName in proposedNames )
{
var finalName = proposedName;
if( Keywords.Any(k => string.Equals(k, proposedName, StringComparison.OrdinalIgnoreCase)) )
{
finalName += "_";
}
else if( ObjectMethods.Any(m => string.Equals(m, proposedName, StringComparison.OrdinalIgnoreCase)) )
{
finalName += "_";
}
lst.Add(finalName);
}
info["methodnames"] = JArray.FromObject(lst);
}
}
private void DeleteIfBlackListed()
{
foreach( var termInfo in allTerms )
{
var term = termInfo.Key;
var info = termInfo.Value;
var termName = term.MethodName();
if( Blacklist.Any(b => string.Equals(termName, b, StringComparison.OrdinalIgnoreCase)) )
{
allTerms.Remove(term);
}
}
}
}
}