@cicctencent/tars2node-cli 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +74 -0
- package/bin/tars.js +81 -0
- package/jce.pegjs +359 -0
- package/lib/generator/finder.js +122 -0
- package/lib/generator/printer.js +116 -0
- package/lib/generator/ts/def/base.js +55 -0
- package/lib/generator/ts/def/const.js +108 -0
- package/lib/generator/ts/def/enum.js +144 -0
- package/lib/generator/ts/def/interf.js +508 -0
- package/lib/generator/ts/def/proxy.js +483 -0
- package/lib/generator/ts/def/struct.js +383 -0
- package/lib/generator/ts/generator.js +645 -0
- package/lib/generator/ts/printer.js +66 -0
- package/lib/generator/ts/typing.js +193 -0
- package/lib/generator/ts/util.js +21 -0
- package/lib/generator/typing.js +351 -0
- package/lib/parser/parser.js +383 -0
- package/main.d.ts +29 -0
- package/main.js +35 -0
- package/package.json +44 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class Printer {
|
|
4
|
+
constructor() {
|
|
5
|
+
/** @type {string[]} */
|
|
6
|
+
this._buf = [];
|
|
7
|
+
/** @type {number} */
|
|
8
|
+
this._indent = 0;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
resetIndent() {
|
|
12
|
+
this._indent = 0;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
print() {
|
|
16
|
+
const buf = this._buf.join('');
|
|
17
|
+
this._buf = [];
|
|
18
|
+
|
|
19
|
+
return buf;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
indent() {
|
|
23
|
+
this._indent++;
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
dedent() {
|
|
28
|
+
this._indent--;
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
semicolon() {
|
|
33
|
+
this._buf.push(';');
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 添加n个空格
|
|
39
|
+
* @param {number} n
|
|
40
|
+
* @return {this}
|
|
41
|
+
*/
|
|
42
|
+
space(n) {
|
|
43
|
+
for (let i = 0; i < n; i++) {
|
|
44
|
+
this._buf.push(' ');
|
|
45
|
+
}
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 添加一个字符串,并根据第二个参数判断是否需要左右空格
|
|
51
|
+
* @param {string} str
|
|
52
|
+
* @param {'NO'|'R'|'L'|'LR'} [needSpace = 'NO']
|
|
53
|
+
* @return {this}
|
|
54
|
+
*/
|
|
55
|
+
word(str, needSpace = 'NO') {
|
|
56
|
+
switch (needSpace) {
|
|
57
|
+
case 'L':
|
|
58
|
+
this.space(1);
|
|
59
|
+
this._buf.push(str);
|
|
60
|
+
break;
|
|
61
|
+
case 'R':
|
|
62
|
+
this._buf.push(str);
|
|
63
|
+
this.space(1);
|
|
64
|
+
break;
|
|
65
|
+
case 'LR':
|
|
66
|
+
this.space(1);
|
|
67
|
+
this._buf.push(str);
|
|
68
|
+
this.space(1);
|
|
69
|
+
break;
|
|
70
|
+
default:
|
|
71
|
+
this._buf.push(str);
|
|
72
|
+
}
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 添加一个新行,并根据第二个参数判断是否需要行后的空行
|
|
78
|
+
* @param {string} str
|
|
79
|
+
* @param {boolean} [afterSpace = false]
|
|
80
|
+
* @return {this}
|
|
81
|
+
*/
|
|
82
|
+
line(str, afterSpace = false) {
|
|
83
|
+
this.newline().word(str);
|
|
84
|
+
if (afterSpace) this.newline();
|
|
85
|
+
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 添加一个空行
|
|
91
|
+
* @param {boolean} [needSpace = true] - 是否需要按照当前缩进缩进
|
|
92
|
+
* @return {this}
|
|
93
|
+
*/
|
|
94
|
+
newline(needSpace = true) {
|
|
95
|
+
this._buf.push('\n');
|
|
96
|
+
if (needSpace) this.space(this._indent * 2);
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* @param {() => void} callback
|
|
102
|
+
* @param {boolean} [inline = false]
|
|
103
|
+
* @return {this}
|
|
104
|
+
*/
|
|
105
|
+
scope(callback, inline = false) {
|
|
106
|
+
this.word('{');
|
|
107
|
+
if (!inline) this.indent();
|
|
108
|
+
callback();
|
|
109
|
+
if (!inline) this.dedent().newline();
|
|
110
|
+
this.word('}');
|
|
111
|
+
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
module.exports = Printer;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class BaseDef {
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* 工具类类型
|
|
7
|
+
* @typedef {import("../printer")} Printer
|
|
8
|
+
* @typedef {import("../typing")} Typing
|
|
9
|
+
*
|
|
10
|
+
* @typedef {object} CompilerOptions
|
|
11
|
+
* @property {'all'|'client'|'service'} mode
|
|
12
|
+
* @property {'string'|'number'|'bigint'} long
|
|
13
|
+
* @property {boolean} withReturn
|
|
14
|
+
* @property {boolean} gather
|
|
15
|
+
* @property {boolean} interface
|
|
16
|
+
* @property {string} [output]
|
|
17
|
+
*
|
|
18
|
+
* 创建Const解析实例
|
|
19
|
+
* @constructor
|
|
20
|
+
* @param {string} moduleName - 当前Const所属模块
|
|
21
|
+
* @param {Printer} printer - print类实例
|
|
22
|
+
* @param {Typing} typing - Typing类实例
|
|
23
|
+
* @param {CompilerOptions} options - 编译配置选项
|
|
24
|
+
*/
|
|
25
|
+
constructor(moduleName, printer, typing, options) {
|
|
26
|
+
this._moduleName = moduleName;
|
|
27
|
+
this._options = options;
|
|
28
|
+
this._printer = printer;
|
|
29
|
+
this._typing = typing;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @return {undefined | 2 | true}
|
|
34
|
+
*/
|
|
35
|
+
generateLastParamForLong() {
|
|
36
|
+
switch (this._options.long) {
|
|
37
|
+
case 'bigint':
|
|
38
|
+
return 2;
|
|
39
|
+
case 'string':
|
|
40
|
+
return true;
|
|
41
|
+
case 'number':
|
|
42
|
+
default:
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @param {string} name
|
|
49
|
+
*/
|
|
50
|
+
setModuleName(name) {
|
|
51
|
+
this._moduleName = name;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
module.exports = BaseDef;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const BaseDef = require('./base');
|
|
4
|
+
|
|
5
|
+
class ConstDef extends BaseDef {
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* 声明JCE-Struct节点类型
|
|
9
|
+
* @typedef { refType | vecType | string | mapType } tarsType
|
|
10
|
+
*
|
|
11
|
+
* @typedef {object} refType
|
|
12
|
+
* @property {string} target
|
|
13
|
+
* @property {string[]} nested
|
|
14
|
+
*
|
|
15
|
+
* @typedef {object} vecType
|
|
16
|
+
* @property {'vector'} type
|
|
17
|
+
* @property {tarsType} target
|
|
18
|
+
*
|
|
19
|
+
* @typedef {object} mapType
|
|
20
|
+
* @property {'map'} type
|
|
21
|
+
* @property {tarsType} key
|
|
22
|
+
* @property {tarsType} value
|
|
23
|
+
*
|
|
24
|
+
* @typedef {object} constValue
|
|
25
|
+
* @property {tarsType} type
|
|
26
|
+
* @property {*} init
|
|
27
|
+
*
|
|
28
|
+
* @typedef {object} TarsConstNode
|
|
29
|
+
* @property {string} id
|
|
30
|
+
* @property {'const'} type
|
|
31
|
+
* @property {constValue} value
|
|
32
|
+
*
|
|
33
|
+
* 工具类类型
|
|
34
|
+
* @typedef {import("../printer")} Printer
|
|
35
|
+
* @typedef {import("../typing")} Typing
|
|
36
|
+
*
|
|
37
|
+
* @typedef {object} CompilerOptions
|
|
38
|
+
* @property {'all'|'client'|'service'} mode
|
|
39
|
+
* @property {'string'|'number'|'bigint'} long
|
|
40
|
+
* @property {boolean} withReturn
|
|
41
|
+
* @property {boolean} gather
|
|
42
|
+
* @property {boolean} interface
|
|
43
|
+
* @property {string} [output]
|
|
44
|
+
*
|
|
45
|
+
* 创建Const解析实例
|
|
46
|
+
* @constructor
|
|
47
|
+
* @param {Printer} printer - print类实例
|
|
48
|
+
* @param {Typing} typing - Typing类实例
|
|
49
|
+
* @param {CompilerOptions} options - 编译配置选项
|
|
50
|
+
*/
|
|
51
|
+
constructor(printer, typing, options) {
|
|
52
|
+
super('', printer, typing, options);
|
|
53
|
+
/** @type {string} */
|
|
54
|
+
this._id = '';
|
|
55
|
+
/** @type {constValue} */
|
|
56
|
+
this._value = { type: 'string', init: null };
|
|
57
|
+
this._beforeComment = '';
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
build() {
|
|
61
|
+
if (typeof this._value.type === 'string') {
|
|
62
|
+
this._handleBase();
|
|
63
|
+
} else {
|
|
64
|
+
this._handleEnum();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @param {string} name
|
|
70
|
+
* @param {TarsConstNode} node
|
|
71
|
+
*/
|
|
72
|
+
reset(name, node) {
|
|
73
|
+
this.setModuleName(name);
|
|
74
|
+
this.setNode(node);
|
|
75
|
+
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** @param {TarsConstNode} node */
|
|
80
|
+
setNode({ id, value, beforeComment = '' }) {
|
|
81
|
+
this._id = id;
|
|
82
|
+
this._value = value;
|
|
83
|
+
this._beforeComment = beforeComment;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
_handleBase() {
|
|
87
|
+
this._printer
|
|
88
|
+
.word(this._beforeComment)
|
|
89
|
+
.newline()
|
|
90
|
+
.line(`export const ${this._id} = ${this._value.init};`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
_handleEnum() {
|
|
94
|
+
const father = this._value.type;
|
|
95
|
+
const chainName = this._typing.toTarsStreamType(
|
|
96
|
+
father,
|
|
97
|
+
this._moduleName
|
|
98
|
+
).tafType;
|
|
99
|
+
const target = `${chainName}.${this._value.init.target}`;
|
|
100
|
+
|
|
101
|
+
this._printer
|
|
102
|
+
.word(this._beforeComment)
|
|
103
|
+
.newline()
|
|
104
|
+
.line(`const ${this._id} = ${target};`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
module.exports = ConstDef;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const BaseDef = require('./base');
|
|
4
|
+
const util = require('../util');
|
|
5
|
+
|
|
6
|
+
const WRITE_SIGNATURE = 'export function _write(os: TarsStream.TarsOutputStream, tag: number, val: any){ return os.writeInt32(tag, val); };';
|
|
7
|
+
const READ_SIGNATURE = 'export function _read(is: TarsStream.TarsInputStream, tag: number, def?: any){ return is.readInt32(tag, true, def); };';
|
|
8
|
+
|
|
9
|
+
class EnumDef extends BaseDef {
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
* 声明JCE-Struct节点类型
|
|
13
|
+
* @typedef { refType | vecType | string | mapType } tarsType
|
|
14
|
+
*
|
|
15
|
+
* @typedef {object} refType
|
|
16
|
+
* @property {string} target
|
|
17
|
+
* @property {string[]} nested
|
|
18
|
+
*
|
|
19
|
+
* @typedef {object} vecType
|
|
20
|
+
* @property {'vector'} type
|
|
21
|
+
* @property {tarsType} target
|
|
22
|
+
*
|
|
23
|
+
* @typedef {object} mapType
|
|
24
|
+
* @property {'map'} type
|
|
25
|
+
* @property {tarsType} key
|
|
26
|
+
* @property {tarsType} value
|
|
27
|
+
*
|
|
28
|
+
* @typedef {object} enumValue
|
|
29
|
+
* @property {string} id
|
|
30
|
+
* @property {number} value
|
|
31
|
+
*
|
|
32
|
+
* @typedef {object} TarsEnumNode
|
|
33
|
+
* @property {string} id
|
|
34
|
+
* @property {'enum'} type
|
|
35
|
+
* @property {enumValue[]} value
|
|
36
|
+
*
|
|
37
|
+
* 工具类类型
|
|
38
|
+
* @typedef {import("../printer")} Printer
|
|
39
|
+
* @typedef {import("../typing")} Typing
|
|
40
|
+
*
|
|
41
|
+
* @typedef {object} CompilerOptions
|
|
42
|
+
* @property {'all'|'client'|'service'} mode
|
|
43
|
+
* @property {'string'|'number'|'bigint'} long
|
|
44
|
+
* @property {boolean} withReturn
|
|
45
|
+
* @property {boolean} gather
|
|
46
|
+
* @property {boolean} interface
|
|
47
|
+
* @property {string} [output]
|
|
48
|
+
*
|
|
49
|
+
* 创建Struct解析实例
|
|
50
|
+
* @constructor
|
|
51
|
+
* @param {Printer} printer - print类实例
|
|
52
|
+
* @param {Typing} typing - Typing类实例
|
|
53
|
+
* @param {CompilerOptions} options - 编译配置选项
|
|
54
|
+
*/
|
|
55
|
+
constructor(printer, typing, options) {
|
|
56
|
+
super('', printer, typing, options);
|
|
57
|
+
this._id = '';
|
|
58
|
+
this._chainName = '';
|
|
59
|
+
/** @type {enumValue[]} */
|
|
60
|
+
this._value = [];
|
|
61
|
+
/** @type {number} */
|
|
62
|
+
this._latestValue = 0;
|
|
63
|
+
this._beforeComment = '';
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @param {string} name
|
|
68
|
+
* @param {TarsEnumNode} node
|
|
69
|
+
*/
|
|
70
|
+
reset(name, node) {
|
|
71
|
+
this.setModuleName(name);
|
|
72
|
+
this.setNode(node);
|
|
73
|
+
this.resetLatestValue();
|
|
74
|
+
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @param {string} name
|
|
80
|
+
*/
|
|
81
|
+
setModuleName(name) {
|
|
82
|
+
super.setModuleName(name);
|
|
83
|
+
this._chainName = `${this._moduleName}.${this._id}`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param {TarsEnumNode} node
|
|
88
|
+
*/
|
|
89
|
+
setNode({ id, value, beforeComment = '' }) {
|
|
90
|
+
this._id = id;
|
|
91
|
+
this._value = value;
|
|
92
|
+
this._chainName = `${this._moduleName}.${id}`;
|
|
93
|
+
this._beforeComment = beforeComment;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
resetLatestValue() {
|
|
97
|
+
this._latestValue = 0;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
build() {
|
|
101
|
+
this._enum();
|
|
102
|
+
if (this._options.mode !== 'web') {
|
|
103
|
+
this._namespace();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
_enum() {
|
|
108
|
+
this._printer
|
|
109
|
+
.word(this._beforeComment)
|
|
110
|
+
.newline()
|
|
111
|
+
.word('export', 'R')
|
|
112
|
+
.word('enum', 'R')
|
|
113
|
+
.word(this._id, 'R')
|
|
114
|
+
.word('{')
|
|
115
|
+
.indent();
|
|
116
|
+
this._value.forEach(this._enumMember.bind(this));
|
|
117
|
+
this._printer.dedent().line('}');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
_namespace() {
|
|
121
|
+
this._printer
|
|
122
|
+
.line(`export namespace ${this._id} {`)
|
|
123
|
+
.indent()
|
|
124
|
+
.line(`export const _classname = "${this._chainName}";`)
|
|
125
|
+
.line(WRITE_SIGNATURE)
|
|
126
|
+
.line(READ_SIGNATURE)
|
|
127
|
+
.dedent()
|
|
128
|
+
.line('}');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* @param {enumValue} value
|
|
133
|
+
*/
|
|
134
|
+
_enumMember({ id, value, comment = '' }) {
|
|
135
|
+
let result = value;
|
|
136
|
+
if (typeof result === "undefined") {
|
|
137
|
+
result = this._latestValue;
|
|
138
|
+
this._latestValue = result + 1;
|
|
139
|
+
}
|
|
140
|
+
this._printer.line(`${util.toMultilineComment(comment)}${id} = ${result},`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
module.exports = EnumDef;
|