-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkirito.js
302 lines (263 loc) · 7.13 KB
/
kirito.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
'use strict';
const fs = require('fs');
const parser = Symbol.for('kirito#parser');
const tokenizer = Symbol.for('kirito#tokenizer');
const transformer = Symbol.for('kirito#transformer');
const TYPE = {
KEYWORD: 'keyword',
VARIABLE: 'variable',
SYMBOL: 'symbol',
INDEX: 'index'
};
const EXP = {
VARIABLE: 'Identifier',
STRUCT_DECLARATIONL: 'StructDeclaration',
VAR_DECLARATION: 'VariableDeclaration',
TYPE: 'DataType',
};
class Kirito {
constructor() {
}
load (path) {
const ast = this.parse(path);
return this[transformer](ast);
}
parse(path) {
const proto = fs.readFileSync(path, 'utf8');
const tokens = this[tokenizer](proto);
const ast = this[parser](tokens);
return ast;
}
// 词法分析
[tokenizer] (input) {
const KEYWORD = ['service', 'struct', 'method'];
const SYMBOL = ['{', '}', '(', ')', '=', '@', ';'];
const WHITESPACE = /\s/;
const LETTERS = /^[a-z]$/i;
const NUMBER = /\d/;
const source = input.split('\n');
const tokens = [];
source.some(line => {
let current = 0;
let isContinue = false;
while (current < line.length) {
let char = line[current];
// 匹配任何空字符
if (WHITESPACE.test(char)) {
current++;
continue;
}
// 忽略注释
if (char === '#') {
isContinue = true;
break;
}
// 匹配字符(变量/保留字)、字符加数字(参数类型)
if (LETTERS.test(char)) {
let value = '';
while (LETTERS.test(char) || NUMBER.test(char)) {
value += char;
char = line[++current];
}
if (KEYWORD.indexOf(value) !== -1) {
// 匹配保留关键字
tokens.push({
type: TYPE.KEYWORD,
value: value
});
} else {
// 匹配变量名
tokens.push({
type: TYPE.VARIABLE,
value: value
});
}
continue;
}
// 匹配符号 { } ( ) = @
if (SYMBOL.indexOf(char) !== -1) {
tokens.push({
type: TYPE.SYMBOL,
value: char
});
if (char === '@') {
char = line[++current];
// 匹配参数位置
if (NUMBER.test(char)) {
let index = '';
while (NUMBER.test(char)) {
index += char;
char = line[++current];
}
tokens.push({
type: TYPE.INDEX,
value: index
});
}
continue;
}
current++;
continue;
}
current++;
}
if (isContinue) return false;
});
return tokens;
}
// 语法分析
[parser] (tokens) {
const ast = {
type: 'Program',
body: []
};
let current = 0;
function walk() {
let token = tokens[current];
// 检查变量
if (token.type === TYPE.VARIABLE) {
current++;
return {
type: EXP.VARIABLE,
struct: tokens[current].value === '=' ? false : true,
value: token.value
};
}
// 检查符号
if (token.type === TYPE.SYMBOL) {
// 检查@
if (token.value === '@') {
token = tokens[++current];
let node = {
type: EXP.VAR_DECLARATION,
name: '@',
value: token.value,
params: []
};
token = tokens[++current];
while (token.value !== ';') {
node.params.push(walk());
token = tokens[current];
}
current++;
return node;
}
// 检查=
if (token.value === '=') {
token = tokens[++current];
current++;
return {
type: EXP.TYPE,
value: token.value
};
}
current++;
}
// 检查保留字
if (token.type === TYPE.KEYWORD) {
// 检查service
if (['struct', 'service'].indexOf(token.value) !== -1) {
let keywordName = token.value;
token = tokens[++current];
let node = {
type: EXP.STRUCT_DECLARATIONL,
name: keywordName,
value: token.value,
params: []
};
token = tokens[++current];
if (token.type === TYPE.SYMBOL && token.value === '{') {
token = tokens[++current];
while (token.value !== '}') {
node.params.push(walk());
token = tokens[current];
}
current++;
}
return node;
}
if (token.value === 'method') {
// 检查method
token = tokens[++current];
let node = {
type: EXP.STRUCT_DECLARATIONL,
name: 'method',
value: token.value,
params: []
};
token = tokens[++current];
if (token.type === TYPE.SYMBOL && token.value === '(') {
token = tokens[++current];
while (token.value !== ')') {
node.params.push(walk());
token = tokens[current];
}
current++;
}
return node;
}
}
throw new TypeError(token.type);
}
while (current < tokens.length) {
ast.body.push(walk());
}
return ast;
}
// 转换器
[transformer] (ast) {
const services = {};
const structs = {};
function traverseArray(array, parent) {
array.some((child) => {
traverseNode(child, parent);
});
}
function traverseNode (node, parent) {
switch (node.type) {
case 'Program':
traverseArray(node.body, parent);
break;
case 'StructDeclaration':
if (node.name === 'service') {
parent[node.value] = {};
traverseArray(node.params, parent[node.value]);
} else if (node.name === 'method') {
parent[node.value] = function () {};
parent[node.value].param = {};
traverseArray(node.params, parent[node.value].param);
} else if (node.name === 'struct') {
structs[node.value] = {};
traverseArray(node.params, structs[node.value]);
}
break;
case 'Identifier':
parent[node.value] = {};
break;
case 'VariableDeclaration':
traverseArray(node.params, parent);
break;
case 'DataType':
parent[Object.keys(parent).pop()] = node.value;
break;
default:
throw new TypeError(node.type);
}
}
traverseNode(ast, services);
const serviceKeys = Object.getOwnPropertyNames(services);
serviceKeys.some(service => {
const methodKeys = Object.getOwnPropertyNames(services[service]);
methodKeys.some(method => {
Object.keys(services[service][method].param).some(p => {
if (structs[p] !== null) {
services[service][method].param[p] = structs[p];
delete structs[p];
}
});
});
});
return services;
}
}
module.exports = Kirito;