-
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathTypeNameParser.cs
More file actions
138 lines (125 loc) · 4.04 KB
/
TypeNameParser.cs
File metadata and controls
138 lines (125 loc) · 4.04 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
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
using System.Text;
using System.Text.RegularExpressions;
namespace JavaToCSharp;
public static partial class TypeNameParser
{
private enum TokenType
{
EndOfString = 0,
Identifier,
Extends,
Super,
LeftSquareBracket,
RightSquareBracket,
LeftAngleBracket,
RightAngleBracket,
Comma,
QuestionMark
}
[GeneratedRegex(@"\w+|\[|\]|<|>|,|\?", RegexOptions.Compiled)]
private static partial Regex TokenizePattern { get; }
private static (string, TokenType)[]? _tokens;
private static (string text, TokenType type) _token;
private static int _currentIndex;
private static readonly StringBuilder _sb = new();
private static Func<string, string>? _translate;
internal static string ParseTypeName(string typename, Func<string, string> translateIdentifier)
{
// Example: List<Pair<Integer, ? extends Person>>
//
// EBNF:
// TypeName = identifier [ "<" TypeArgument { "," TypeArgument } ">" ].
// TypeArgument = [ "?" [ "extends" | "super" ] ] TypeName.
_translate = translateIdentifier;
_tokens = Tokenize(typename);
_currentIndex = -1;
NextToken();
_sb.Clear();
if (TypeName() && _token.type is TokenType.EndOfString)
{
// Otherwise we have extra tokens.
return _sb.ToString();
}
return typename;
}
private static (string, TokenType)[] Tokenize(string typeName)
{
var matches = TokenizePattern.Matches(typeName);
var tokens = new (string, TokenType)[matches.Count];
for (int i = 0; i < matches.Count; i++)
{
var match = matches[i];
string s = match.Value;
tokens[i] = s switch
{
"[" => (s, TokenType.LeftSquareBracket),
"]" => (s, TokenType.RightSquareBracket),
"<" => (s, TokenType.LeftAngleBracket),
">" => (s, TokenType.RightAngleBracket),
"," => (s, TokenType.Comma),
"?" => (s, TokenType.QuestionMark),
"extends" => (s, TokenType.Extends),
"super" => (s, TokenType.Super),
_ => (s, TokenType.Identifier)
};
}
return tokens;
}
private static void NextToken()
{
_currentIndex++;
_token = _tokens is not null && _currentIndex < _tokens.Length ? _tokens[_currentIndex] : default;
}
private static bool TypeName()
{
// TypeName = identifier [ "<" TypeArgument { "," TypeArgument } ">" ].
if (_token.type is TokenType.Identifier)
{
_sb.Append(_translate?.Invoke(_token.text));
NextToken();
if (_token.type is TokenType.LeftAngleBracket)
{
_sb.Append('<');
NextToken();
if (TypeArgument())
{
while (_token.type is TokenType.Comma)
{
_sb.Append(", ");
NextToken();
if (!TypeArgument()) return false;
}
if (_token.type is TokenType.RightAngleBracket)
{
_sb.Append('>');
NextToken();
return true;
}
}
}
else
{
return true;
}
}
return false;
}
private static bool TypeArgument()
{
// TypeArgument = [ "?" [ "extends" | "super" ] ] TypeName.
if (_token.type is TokenType.QuestionMark)
{
_sb.Append("TWildcardTodo");
NextToken();
if (_token.type is TokenType.Extends or TokenType.Super)
{
NextToken();
}
else if (_token.type is TokenType.RightAngleBracket)
{
return true;
}
}
return TypeName();
}
}