-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtiny.ts
executable file
·277 lines (232 loc) · 7.32 KB
/
tiny.ts
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Represents the tiny file parser.
*/
import { parse_function_descriptor, FunctionDescriptor, DETAILED_TYPE_REGEX } from "./java.mjs";
const HEADER_REGEX = /^tiny\t(\d)\t(\d)\t([a-z_]+)\t([a-z_]+)$/;
const CLASS_REGEX = /^c\t([A-z0-9$\/_]+)\t([A-z0-9$\/_]+)$/;
const ELEMENT_REGEX = /^\t([mf])\t(\S+)\t([A-z0-9$\/_]+)\t([A-z0-9$\/_]+)$/;
/**
* Represents a tiny file.
*/
export class TinyFile {
version: string;
from_mappings: string;
to_mappings: string;
classes: TinyClass[];
constructor(version: string, from_mappings: string, to_mappings: string) {
this.version = version;
this.from_mappings = from_mappings;
this.to_mappings = to_mappings;
this.classes = [];
}
methods() {
return this.classes.flatMap(clazz => clazz.methods);
}
resolve_type(type: string): string {
let result;
if ((result = DETAILED_TYPE_REGEX.exec(type)) && result[2][0] === "L") {
// const remapped_class = this.find_base_class(result[2].substr(1, result[2].length - 2));
const remapped_class = this.find_base_class(result[2].substring(1, result[2].length - 1));
if (remapped_class) {
type = `${result[1]}L${remapped_class.to};`;
}
}
return type;
}
/**
* Finds the specified class from the base name.
*
* @param {string} name the base name
* @returns {TinyClass|null} the class if found, otherwise `null`
*/
find_base_class(name: string): TinyClass | null {
for (const clazz of this.classes) {
if (clazz.from === name)
return clazz;
}
return null;
}
find_base_method(name: string): TinyMethod | null {
for (const clazz of this.classes) {
for (const method of clazz.methods) {
if (method.from === name) {
return method;
}
}
}
return null;
}
find_target_field(name: string): TinyField | null {
for (const clazz of this.classes) {
for (const field of clazz.fields) {
if (field.to === name) {
return field;
}
}
}
return null;
}
find_target_method(name: string): TinyMethod | null {
for (const clazz of this.classes) {
for (const method of clazz.methods) {
if (method.to === name) {
return method;
}
}
}
return null;
}
}
/**
* Represents an entry in a tiny file.
*/
class TinyEntry {
from: string;
to: string;
constructor(from: string, to: string) {
this.from = from;
this.to = to;
}
/**
* Returns the type of this entry.
*
* @return {string} the type of this entry
*/
get_type(): string {
throw new ReferenceError("get_type isn't implemented.");
}
}
/**
* Represents a class in the tiny format.
*/
export class TinyClass extends TinyEntry {
fields: TinyField[];
methods: TinyMethod[];
constructor(from: string, to: string) {
super(from, to);
this.fields = [];
this.methods = [];
}
get_type(): string {
return "c";
}
/**
* Returns whether this class is a subclass.
*
* @returns {boolean} `true` if this is a subclass, otherwise `false`
*/
is_subclass(): boolean {
return this.from.includes("$");
}
push(element: TinyClassEntry) {
if (element instanceof TinyField) {
this.fields.push(element);
} else if (element instanceof TinyMethod) {
this.methods.push(element);
} else {
throw new Error(`Cannot add the given element ${element} to the class (${this.from} => ${this.to}): unknown type.`);
}
}
}
/**
* Represents an entry inside a class.
*/
class TinyClassEntry extends TinyEntry {
owner: TinyClass;
/**
* @param {TinyClass} owner the owner class
* @param {string} from the base name
* @param {string} to the new name
*/
constructor(owner: TinyClass, from: string, to: string) {
super(from, to);
this.owner = owner;
}
}
/**
* Represents a field in the tiny format.
*/
export class TinyField extends TinyClassEntry {
type: string;
/**
* @param {TinyClass} owner the owner class
* @param {string} from the base name
* @param {string} to the new name
* @param {string} type the type/descriptor of this field
*/
constructor(owner: TinyClass, from: string, to: string, type: string) {
super(owner, from, to);
this.type = type;
}
resolve_type(tiny_file: TinyFile) {
return tiny_file.resolve_type(this.type);
}
get_type(): string {
return "f";
}
}
/**
* Represents a method in the tiny format.
*/
export class TinyMethod extends TinyClassEntry {
descriptor: FunctionDescriptor;
/**
* @param {TinyClass} owner the owner class
* @param {string} from the base name
* @param {string} to the new name
* @param {FunctionDescriptor} descriptor the descriptor of this field
*/
constructor(owner: TinyClass, from: string, to: string, descriptor: FunctionDescriptor) {
super(owner, from, to);
this.descriptor = descriptor;
}
/**
* Resolves the descriptor to the target mapping.
*
* @param {TinyFile} tiny_file the tiny file
* @return {FunctionDescriptor} the remapped descriptor
*/
resolve_descriptor(tiny_file: TinyFile): FunctionDescriptor {
const return_type = tiny_file.resolve_type(this.descriptor.return_type);
return new FunctionDescriptor(this.descriptor.params.map((param: string) => tiny_file.resolve_type(param)), return_type);
}
get_type() {
return "m";
}
}
export function parse(source: string): TinyFile {
const lines = source.split(/\r?\n/);
const header = HEADER_REGEX.exec(lines[0]);
if (!header) {
throw new SyntaxError("Header could not be parsed.");
}
const tiny = new TinyFile(header[1] + "." + header[2], header[3], header[4]);
let current_class;
for (let i = 1; i < lines.length; i++) {
const line = lines[i];
let result = CLASS_REGEX.exec(line);
if (result) {
tiny.classes.push(current_class = new TinyClass(result[1], result[2]));
} else if ((result = ELEMENT_REGEX.exec(line))) {
if (current_class === undefined) continue;
let element;
switch (result[1]) {
case "f": { // This is a field.
element = new TinyField(current_class, result[3], result[4], result[2]);
break;
}
case "m": { // This is a method.
element = new TinyMethod(current_class, result[3], result[4], parse_function_descriptor(result[2]));
break;
}
default: { // Unkown type.
throw new SyntaxError(`Cannot parse line ${i} ("${line}"): unknown element type ${result[1]}.`);
}
}
current_class.push(element);
} else if (line !== "") {
throw new SyntaxError(`Could not parse line ${i}: "${line}."`);
}
}
return tiny;
}