forked from alik0211/tl-to-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
135 lines (101 loc) · 3.1 KB
/
index.js
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
class Parser {
constructor(raw) {
this.lines = raw.toString().split('\n');
this.mode = 'constructors';
this.constructors = [];
this.methods = [];
this.layer = 0;
this.parse();
}
parse() {
const { lines } = this;
lines.forEach((line) => {
line = line.replace(';', '').trim();
if (line.indexOf('// LAYER') === 0) {
let lineLayer = line.split(' ');
this.layer = parseInt(lineLayer[2]);
return;
}
if (line === '' || line.indexOf('//') === 0) {
return;
}
this.parseLine(line);
});
}
parseLine(line) {
if (line === '---types---') {
this.mode = 'constructors';
return;
}
if (line === '---functions---') {
this.mode = 'methods';
return;
}
if (this.mode === 'constructors') {
return this.parseConstructor(line);
}
if (this.mode === 'methods') {
return this.parseMethod(line);
}
throw Error(`Mode ${this.mode} is not support`);
}
parseConstructor(line) {
const splitedLine = line.split('=');
const body = splitedLine[0].trim();
const type = splitedLine[1].trim();
const [predicateWithId, ...paramsAsArray] = body.split(' ');
const [predicate, idAsString] = predicateWithId.split('#');
const id = parseInt(idAsString, 16);
const isVector = predicate === 'vector';
const params = isVector ? [] :
paramsAsArray.map((param) => {
const [paramName, paramType] = param.split(':');
return {
name: paramName,
type: paramType,
};
});
this.constructors.push({
id,
predicate,
params,
type,
});
}
parseMethod(line) {
const splitedLine = line.split('=');
const body = splitedLine[0].trim();
const type = splitedLine[1].trim();
const [predicateWithId, ...paramsAsArray] = body.split(' ');
const [method, idAsString] = predicateWithId.split('#');
const id = parseInt(idAsString, 16);
const params = paramsAsArray
.filter((param) => {
if (param[0] === '{' && param[param.length - 1] === '}') {
return false;
}
return true;
})
.map((param) => {
const [paramName, paramType] = param.split(':');
return {
name: paramName,
type: paramType,
};
});
this.methods.push({
id,
method,
params,
type,
});
}
getJS() {
const { constructors, methods, layer } = this;
return { constructors, methods, layer };
}
getJSON() {
return JSON.stringify(this.getJS());
}
}
module.exports = { Parser };