@darabonba/python-generator 1.2.19 → 2.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/ChangeLog.md +0 -158
- package/lib/builtin.js +818 -0
- package/lib/generator.js +2708 -0
- package/lib/helper.js +422 -0
- package/package.json +6 -5
- package/tests/expected/alias/setup.py +79 -0
- package/tests/expected/alias/tea_python_tests/__init__.py +1 -0
- package/tests/expected/alias/tea_python_tests/client.py +35 -0
- package/tests/expected/annotation/tea_python_tests/__init__.py +1 -0
- package/tests/expected/annotation/tea_python_tests/client.py +93 -0
- package/tests/expected/annotation/tea_python_tests/models.py +37 -0
- package/tests/expected/api/tea_python_tests/__init__.py +1 -0
- package/tests/expected/api/tea_python_tests/client.py +106 -0
- package/tests/expected/builtin/tea_python_tests/client.py +685 -0
- package/tests/expected/comment/tea_python_tests/__init__.py +1 -0
- package/tests/expected/comment/tea_python_tests/client.py +288 -0
- package/tests/expected/comment/tea_python_tests/models.py +195 -0
- package/tests/expected/complex/tea_python_tests/__init__.py +1 -0
- package/tests/expected/complex/tea_python_tests/client.py +1169 -0
- package/tests/expected/complex/tea_python_tests/exceptions.py +39 -0
- package/tests/expected/complex/tea_python_tests/models.py +570 -0
- package/tests/expected/const/tea_python_tests/__init__.py +1 -0
- package/tests/expected/const/tea_python_tests/client.py +14 -0
- package/{src/langs/python/files/setup.py.tmpl → tests/expected/empty/setup.py} +11 -11
- package/tests/expected/empty/tea_python_tests/__init__.py +1 -0
- package/tests/expected/empty/tea_python_tests/client.py +10 -0
- package/tests/expected/exec/tea_python_tests/__init__.py +1 -0
- package/tests/expected/exec/tea_python_tests/exec_client.py +21 -0
- package/tests/expected/function/tea_python_tests/__init__.py +1 -0
- package/tests/expected/function/tea_python_tests/client.py +64 -0
- package/tests/expected/import/setup.py +78 -0
- package/tests/expected/import/tea_python_tests/__init__.py +1 -0
- package/tests/expected/import/tea_python_tests/client.py +29 -0
- package/tests/expected/map/tea_python_tests/__init__.py +1 -0
- package/tests/expected/map/tea_python_tests/client.py +39 -0
- package/tests/expected/model/tea_python_tests/__init__.py +1 -0
- package/tests/expected/model/tea_python_tests/client.py +10 -0
- package/tests/expected/model/tea_python_tests/exceptions.py +28 -0
- package/tests/expected/model/tea_python_tests/models.py +780 -0
- package/tests/expected/multi/tea_python_tests/api.py +95 -0
- package/tests/expected/multi/tea_python_tests/client.py +40 -0
- package/tests/expected/multi/tea_python_tests/lib/util.py +19 -0
- package/tests/expected/multi/tea_python_tests/model/user.py +26 -0
- package/tests/expected/multi/tea_python_tests/model/user_exceptions.py +83 -0
- package/tests/expected/multi/tea_python_tests/model/user_models.py +145 -0
- package/tests/expected/statements/tea_python_tests/__init__.py +1 -0
- package/tests/expected/statements/tea_python_tests/client.py +86 -0
- package/tests/expected/super/tea_python_tests/__init__.py +1 -0
- package/tests/expected/super/tea_python_tests/client.py +16 -0
- package/tests/expected/typedef/setup.py +77 -0
- package/tests/expected/typedef/tea_python_tests/__init__.py +1 -0
- package/tests/expected/typedef/tea_python_tests/client.py +74 -0
- package/tests/expected/typedef/tea_python_tests/models.py +42 -0
- package/tests/fixtures/alias/Darafile +11 -0
- package/tests/fixtures/alias/libraries/Alias/Darafile +11 -0
- package/tests/fixtures/alias/libraries/Alias-Symbol/Darafile +11 -0
- package/tests/fixtures/builtin/Darafile +6 -0
- package/tests/fixtures/builtin/main.dara +333 -0
- package/tests/fixtures/complex/libraries/import.dara +18 -11
- package/tests/fixtures/complex/main.dara +452 -103
- package/tests/fixtures/import/libraries/Darafile +1 -1
- package/tests/fixtures/import/pack/Darafile +3 -0
- package/tests/fixtures/map/libraries/Darafile +3 -0
- package/tests/fixtures/map/libraries/import.dara +4 -0
- package/tests/fixtures/map/main.dara +17 -2
- package/tests/fixtures/model/.libraries.json +3 -0
- package/tests/fixtures/model/Darafile +5 -2
- package/tests/fixtures/model/libraries/Darafile +20 -0
- package/tests/fixtures/model/libraries/import.dara +27 -0
- package/tests/fixtures/model/main.dara +60 -42
- package/tests/fixtures/multi/.libraries.json +3 -0
- package/tests/fixtures/multi/Darafile +12 -0
- package/tests/fixtures/multi/api.dara +20 -0
- package/tests/fixtures/multi/lib/util.dara +5 -0
- package/tests/fixtures/multi/libraries/darabonba_Util_0.2.11/Teafile +68 -0
- package/tests/fixtures/multi/libraries/darabonba_Util_0.2.11/main.tea +244 -0
- package/tests/fixtures/multi/libraries/darabonba_Util_0.2.11/util.tea +2 -0
- package/tests/fixtures/multi/main.dara +32 -0
- package/tests/fixtures/multi/model/user.dara +35 -0
- package/tests/{python3.tests.js → main.tests.js} +107 -65
- package/tests/output/alias/setup.py +79 -0
- package/tests/output/alias/tea_python_tests/client.py +35 -0
- package/{src/langs/python2/files/setup.py.tmpl → tests/output/annotation/setup.py} +19 -24
- package/tests/output/annotation/tea_python_tests/client.py +93 -0
- package/tests/output/annotation/tea_python_tests/models.py +37 -0
- package/tests/output/api/setup.py +77 -0
- package/tests/output/api/tea_python_tests/client.py +106 -0
- package/tests/output/builtin/setup.py +77 -0
- package/tests/output/builtin/tea_python_tests/client.py +685 -0
- package/tests/output/comment/setup.py +77 -0
- package/tests/output/comment/tea_python_tests/client.py +288 -0
- package/tests/output/comment/tea_python_tests/models.py +195 -0
- package/tests/output/complex/setup.py +77 -0
- package/tests/output/complex/tea_python_tests/client.py +1169 -0
- package/tests/output/complex/tea_python_tests/exceptions.py +39 -0
- package/tests/output/complex/tea_python_tests/models.py +570 -0
- package/tests/output/const/setup.py +77 -0
- package/tests/output/const/tea_python_tests/client.py +14 -0
- package/tests/output/empty/setup.py +77 -0
- package/tests/output/empty/tea_python_tests/client.py +10 -0
- package/tests/output/exec/setup.py +77 -0
- package/tests/output/exec/tea_python_tests/exec_client.py +21 -0
- package/tests/output/function/setup.py +77 -0
- package/tests/output/function/tea_python_tests/client.py +64 -0
- package/tests/output/import/setup.py +78 -0
- package/tests/output/import/tea_python_tests/client.py +29 -0
- package/tests/output/map/setup.py +77 -0
- package/tests/output/map/tea_python_tests/client.py +39 -0
- package/tests/output/model/setup.py +77 -0
- package/tests/output/model/tea_python_tests/client.py +10 -0
- package/tests/output/model/tea_python_tests/exceptions.py +28 -0
- package/tests/output/model/tea_python_tests/models.py +780 -0
- package/tests/output/multi/setup.py +77 -0
- package/tests/output/multi/tea_python_tests/api.py +95 -0
- package/tests/output/multi/tea_python_tests/client.py +40 -0
- package/tests/output/multi/tea_python_tests/lib/util.py +19 -0
- package/tests/output/multi/tea_python_tests/model/user.py +26 -0
- package/tests/output/multi/tea_python_tests/model/user_exceptions.py +83 -0
- package/tests/output/multi/tea_python_tests/model/user_models.py +145 -0
- package/tests/output/statements/setup.py +77 -0
- package/tests/output/statements/tea_python_tests/client.py +86 -0
- package/tests/output/super/setup.py +77 -0
- package/tests/output/super/tea_python_tests/client.py +16 -0
- package/tests/output/typedef/setup.py +78 -0
- package/tests/output/typedef/tea_python_tests/client.py +74 -0
- package/tests/output/typedef/tea_python_tests/models.py +42 -0
- package/src/generator.js +0 -231
- package/src/langs/common/combinator.js +0 -193
- package/src/langs/common/config.js +0 -60
- package/src/langs/common/enum.js +0 -179
- package/src/langs/common/items.js +0 -551
- package/src/langs/common/package_info.js +0 -53
- package/src/langs/python/combinator.js +0 -1898
- package/src/langs/python/config.js +0 -169
- package/src/langs/python/files/.gitignore.tmpl +0 -5
- package/src/langs/python/files/__init__.py.tmpl +0 -1
- package/src/langs/python/package_info.js +0 -78
- package/src/langs/python2/combinator.js +0 -1770
- package/src/langs/python2/config.js +0 -162
- package/src/langs/python2/files/.gitignore.tmpl +0 -5
- package/src/langs/python2/files/__init__.py.tmpl +0 -1
- package/src/langs/python2/package_info.js +0 -78
- package/src/lib/debug.js +0 -55
- package/src/lib/emitter.js +0 -95
- package/src/lib/helper.js +0 -207
- package/src/resolver/base.js +0 -282
- package/src/resolver/client.js +0 -1013
- package/src/resolver/model.js +0 -159
- package/tests/common.tests.js +0 -244
- package/tests/lib.tests.js +0 -269
- package/tests/python2.tests.js +0 -283
- package/tests/resolver.tests.js +0 -434
package/src/resolver/client.js
DELETED
|
@@ -1,1013 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const debug = require('../lib/debug');
|
|
4
|
-
const BaseResolver = require('./base');
|
|
5
|
-
|
|
6
|
-
const {
|
|
7
|
-
ObjectItem,
|
|
8
|
-
AnnotationItem,
|
|
9
|
-
ConstructItem,
|
|
10
|
-
FuncItem,
|
|
11
|
-
|
|
12
|
-
GrammerVar,
|
|
13
|
-
GrammerCall,
|
|
14
|
-
GrammerExpr,
|
|
15
|
-
GrammerLoop,
|
|
16
|
-
GrammerBreak,
|
|
17
|
-
GrammerCatch,
|
|
18
|
-
GrammerValue,
|
|
19
|
-
GrammerReturn,
|
|
20
|
-
GrammerThrows,
|
|
21
|
-
GrammerFinally,
|
|
22
|
-
GrammerContinue,
|
|
23
|
-
GrammerTryCatch,
|
|
24
|
-
GrammerNewObject,
|
|
25
|
-
GrammerCondition,
|
|
26
|
-
GrammerException,
|
|
27
|
-
GrammerReturnType,
|
|
28
|
-
|
|
29
|
-
BehaviorRetry,
|
|
30
|
-
BehaviorToMap,
|
|
31
|
-
BehaviorToModel,
|
|
32
|
-
BehaviorTimeNow,
|
|
33
|
-
BehaviorDoAction,
|
|
34
|
-
BehaviorSetMapItem,
|
|
35
|
-
BehaviorStrFormat
|
|
36
|
-
} = require('../langs/common/items');
|
|
37
|
-
|
|
38
|
-
const {
|
|
39
|
-
Symbol,
|
|
40
|
-
Modify,
|
|
41
|
-
Exceptions,
|
|
42
|
-
Types
|
|
43
|
-
} = require('../langs/common/enum');
|
|
44
|
-
|
|
45
|
-
const {
|
|
46
|
-
_isBasicType,
|
|
47
|
-
_string,
|
|
48
|
-
_escape
|
|
49
|
-
|
|
50
|
-
} = require('../lib/helper');
|
|
51
|
-
|
|
52
|
-
const systemPackage = ['Util'];
|
|
53
|
-
|
|
54
|
-
class ClientResolver extends BaseResolver {
|
|
55
|
-
constructor(astNode, combinator, globalAst) {
|
|
56
|
-
super(astNode, combinator, globalAst);
|
|
57
|
-
this.object = new ObjectItem('client');
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
resolve() {
|
|
61
|
-
const object = this.object;
|
|
62
|
-
const combinator = this.combinator;
|
|
63
|
-
const config = this.config;
|
|
64
|
-
const ast = this.ast;
|
|
65
|
-
|
|
66
|
-
combinator.config.emitType = 'client';
|
|
67
|
-
object.name = config.clientName || 'Client';
|
|
68
|
-
|
|
69
|
-
// resolve props
|
|
70
|
-
this.resolveProps(ast);
|
|
71
|
-
|
|
72
|
-
// resolve global annotation
|
|
73
|
-
if (ast.annotation) {
|
|
74
|
-
this.initAnnotation(ast.annotation);
|
|
75
|
-
}
|
|
76
|
-
object.topAnnotation.push(new AnnotationItem(
|
|
77
|
-
object.index,
|
|
78
|
-
'single',
|
|
79
|
-
config.generateFileInfo
|
|
80
|
-
));
|
|
81
|
-
|
|
82
|
-
// resolve extends
|
|
83
|
-
if (ast.extends) {
|
|
84
|
-
object.extends.push(combinator.addInclude(ast.extends.lexeme));
|
|
85
|
-
} else if (config.baseClient) {
|
|
86
|
-
let extendsClass = [];
|
|
87
|
-
if (Array.isArray(config.baseClient)) {
|
|
88
|
-
config.baseClient.forEach(item => extendsClass.push(combinator.addInclude(item)));
|
|
89
|
-
} else {
|
|
90
|
-
extendsClass = [combinator.addInclude(config.baseClient)];
|
|
91
|
-
}
|
|
92
|
-
object.extends = extendsClass;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// resolve construct body
|
|
96
|
-
const [init] = ast.moduleBody.nodes.filter((item) => {
|
|
97
|
-
return item.type === 'init';
|
|
98
|
-
});
|
|
99
|
-
if (init) {
|
|
100
|
-
this.resolveInitBody(init);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// resolve api
|
|
104
|
-
ast.moduleBody.nodes.filter((item) => {
|
|
105
|
-
return item.type === 'api';
|
|
106
|
-
}).forEach(item => {
|
|
107
|
-
const func = new FuncItem();
|
|
108
|
-
func.name = item.apiName.lexeme;
|
|
109
|
-
this.resolveFunc(func, item, item.apiBody);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
// resolve function
|
|
113
|
-
ast.moduleBody.nodes.filter((item) => {
|
|
114
|
-
return item.type === 'function';
|
|
115
|
-
}).forEach(item => {
|
|
116
|
-
const func = new FuncItem();
|
|
117
|
-
func.name = item.functionName.lexeme;
|
|
118
|
-
this.resolveFunc(func, item, item.functionBody);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
return object;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
resolveInitBody(init) {
|
|
125
|
-
const object = this.object;
|
|
126
|
-
|
|
127
|
-
let constructNode = new ConstructItem();
|
|
128
|
-
if (init.params && init.params.params) {
|
|
129
|
-
init.params.params.forEach(param => {
|
|
130
|
-
constructNode.addParamNode(new GrammerValue(this.resolveType(param.paramType, param), param.defaultValue, param.paramName.lexeme));
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
if (init.annotation) {
|
|
134
|
-
constructNode.addAnnotation(this.resolveAnnotation(init.annotation, constructNode.index));
|
|
135
|
-
}
|
|
136
|
-
if (init.initBody && init.initBody.stmts) {
|
|
137
|
-
init.initBody.stmts.forEach(stmt => {
|
|
138
|
-
this.visitStmt(constructNode, stmt, constructNode.index);
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
object.addBodyNode(constructNode);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
resolveFunc(func, ast, body) {
|
|
145
|
-
if (ast.annotation) {
|
|
146
|
-
func.addAnnotation(this.resolveAnnotation(ast.annotation, func.index));
|
|
147
|
-
}
|
|
148
|
-
this.addAnnotations(func, ast);
|
|
149
|
-
if (body === null) {
|
|
150
|
-
func.addBodyNode(new GrammerThrows(Exceptions.base(), [], 'Un-implemented'));
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
if (ast.isAsync) {
|
|
154
|
-
func.modify.push(Modify.async());
|
|
155
|
-
}
|
|
156
|
-
if (ast.isStatic) {
|
|
157
|
-
func.modify.push(Modify.static());
|
|
158
|
-
}
|
|
159
|
-
func.modify.push(Modify.public());
|
|
160
|
-
|
|
161
|
-
ast.params.params.forEach(p => {
|
|
162
|
-
var param = new GrammerValue();
|
|
163
|
-
param.type = this.resolveType(p.paramType, p);
|
|
164
|
-
param.key = _escape(p.paramName.lexeme || _string(p.paramName));
|
|
165
|
-
if (p.needValidate) {
|
|
166
|
-
func.addBodyNode(new GrammerCall('method', [
|
|
167
|
-
{ type: 'object', name: param.key },
|
|
168
|
-
{ type: 'call', name: 'validate' }
|
|
169
|
-
], [], null, true)); // validator
|
|
170
|
-
}
|
|
171
|
-
func.params.push(param);
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
func.return.push(this.resolveType(ast.returnType));
|
|
175
|
-
|
|
176
|
-
if (ast.runtimeBody) {
|
|
177
|
-
this.runtimeMode(func, ast, body);
|
|
178
|
-
} else {
|
|
179
|
-
if (ast.functionBody || ast.wrapBody) {
|
|
180
|
-
body.stmts.stmts.forEach(stmtItem => {
|
|
181
|
-
this.visitStmt(func, stmtItem, func.index);
|
|
182
|
-
});
|
|
183
|
-
} else {
|
|
184
|
-
this.requestBody(ast, body, func);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
if (func.body.length === 0) {
|
|
189
|
-
this.findComments(func, body, 'between');
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
this.object.addBodyNode(func);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
runtimeMode(func, ast, body) {
|
|
196
|
-
var val = new GrammerValue('array', []);
|
|
197
|
-
this.renderGrammerValue(val, ast.runtimeBody);
|
|
198
|
-
if (ast.runtimeBody.tokenRange) {
|
|
199
|
-
this.resolveAnnotations(this.getFrontComments(ast.runtimeBody.tokenRange[1])).forEach(c => {
|
|
200
|
-
val.value.push(c);
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// _runtime = {}
|
|
205
|
-
func.addBodyNode(new GrammerExpr(
|
|
206
|
-
new GrammerVar(this.config.runtime, Types.any.key), Symbol.assign(), val
|
|
207
|
-
));
|
|
208
|
-
|
|
209
|
-
// _lastRequest = null;
|
|
210
|
-
func.addBodyNode(new GrammerExpr(
|
|
211
|
-
new GrammerVar('_lastRequest', this.combinator.addInclude('$Request'), 'var'),
|
|
212
|
-
Symbol.assign(),
|
|
213
|
-
new GrammerValue('null')
|
|
214
|
-
));
|
|
215
|
-
|
|
216
|
-
// _lastException = null;
|
|
217
|
-
func.addBodyNode(new GrammerExpr(
|
|
218
|
-
new GrammerVar('_lastException', this.combinator.addInclude('$Exception'), 'var'),
|
|
219
|
-
Symbol.assign(),
|
|
220
|
-
new GrammerValue('null')
|
|
221
|
-
));
|
|
222
|
-
|
|
223
|
-
// _now = Date.now();
|
|
224
|
-
func.addBodyNode(new GrammerExpr(
|
|
225
|
-
new GrammerVar('_now', Types.int32.key),
|
|
226
|
-
Symbol.assign(),
|
|
227
|
-
new GrammerValue('behavior', new BehaviorTimeNow())
|
|
228
|
-
));
|
|
229
|
-
|
|
230
|
-
// let _retryTimes = 0;
|
|
231
|
-
func.addBodyNode(new GrammerExpr(
|
|
232
|
-
new GrammerVar('_retryTimes', Types.int16.key, 'var'),
|
|
233
|
-
Symbol.assign(),
|
|
234
|
-
new GrammerValue('number', 0)
|
|
235
|
-
));
|
|
236
|
-
|
|
237
|
-
let whileOperation = new GrammerCondition('while');
|
|
238
|
-
whileOperation.addCondition(
|
|
239
|
-
new GrammerCall('method', [
|
|
240
|
-
{ type: 'object_static', name: this.combinator.addInclude('$Core') },
|
|
241
|
-
{ type: 'call_static', name: this.config.tea.core.allowRetry }
|
|
242
|
-
], [
|
|
243
|
-
new GrammerValue('call', new GrammerCall('key', [
|
|
244
|
-
{ type: 'object', name: this.config.runtime },
|
|
245
|
-
{ type: 'map', name: 'retry' }
|
|
246
|
-
], [], new GrammerReturnType(Types.any.key))),
|
|
247
|
-
new GrammerValue('param', '_retryTimes'),
|
|
248
|
-
new GrammerValue('param', '_now'),
|
|
249
|
-
])
|
|
250
|
-
);
|
|
251
|
-
|
|
252
|
-
let retryTimesIf = new GrammerCondition('if');
|
|
253
|
-
retryTimesIf.addCondition(
|
|
254
|
-
new GrammerExpr(
|
|
255
|
-
new GrammerVar('_retryTimes'),
|
|
256
|
-
Symbol.greater(),
|
|
257
|
-
new GrammerValue('number', 0)
|
|
258
|
-
)
|
|
259
|
-
);
|
|
260
|
-
retryTimesIf.addBodyNode(
|
|
261
|
-
new GrammerExpr(
|
|
262
|
-
new GrammerVar('_backoffTime', Types.int16.key),
|
|
263
|
-
Symbol.assign(),
|
|
264
|
-
new GrammerCall('method', [
|
|
265
|
-
{ type: 'object_static', name: this.combinator.addInclude('$Core') },
|
|
266
|
-
{ type: 'call_static', name: this.config.tea.core.getBackoffTime }
|
|
267
|
-
], [
|
|
268
|
-
new GrammerValue('call', new GrammerCall('key', [
|
|
269
|
-
{ type: 'object', name: this.config.runtime },
|
|
270
|
-
{ type: 'map', name: 'backoff' }
|
|
271
|
-
])),
|
|
272
|
-
new GrammerValue('param', '_retryTimes'),
|
|
273
|
-
])
|
|
274
|
-
)
|
|
275
|
-
);
|
|
276
|
-
|
|
277
|
-
let backoffTimeIf = new GrammerCondition('if');
|
|
278
|
-
backoffTimeIf.addCondition(
|
|
279
|
-
new GrammerExpr(
|
|
280
|
-
new GrammerVar('_backoffTime'),
|
|
281
|
-
Symbol.greater(),
|
|
282
|
-
new GrammerValue('number', 0)
|
|
283
|
-
)
|
|
284
|
-
);
|
|
285
|
-
backoffTimeIf.addBodyNode(
|
|
286
|
-
new GrammerCall('method', [
|
|
287
|
-
{ type: 'object_static', name: this.combinator.addInclude('$Core') },
|
|
288
|
-
{ type: 'call_static', name: this.config.tea.core.sleep }
|
|
289
|
-
], [
|
|
290
|
-
new GrammerValue('param', '_backoffTime'),
|
|
291
|
-
])
|
|
292
|
-
);
|
|
293
|
-
|
|
294
|
-
retryTimesIf.addBodyNode(backoffTimeIf);
|
|
295
|
-
whileOperation.addBodyNode(retryTimesIf);
|
|
296
|
-
|
|
297
|
-
whileOperation.addBodyNode(new GrammerExpr(
|
|
298
|
-
new GrammerVar('_retryTimes'),
|
|
299
|
-
Symbol.assign(),
|
|
300
|
-
new GrammerExpr(
|
|
301
|
-
new GrammerVar('_retryTimes'),
|
|
302
|
-
Symbol.plus(),
|
|
303
|
-
new GrammerValue('number', 1)
|
|
304
|
-
)
|
|
305
|
-
));
|
|
306
|
-
|
|
307
|
-
let requestTryCatch = new GrammerTryCatch();
|
|
308
|
-
let exceptionVar = new GrammerVar('e');
|
|
309
|
-
let exceptionParam = new GrammerValue('var', exceptionVar);
|
|
310
|
-
let catchException = new GrammerException(Exceptions.base(), exceptionVar);
|
|
311
|
-
|
|
312
|
-
let tryCatch = new GrammerCatch([
|
|
313
|
-
new GrammerCondition('if', [
|
|
314
|
-
new GrammerCall('method', [
|
|
315
|
-
{ type: 'object_static', name: this.combinator.addInclude('$Core') },
|
|
316
|
-
{ type: 'call_static', name: this.config.tea.core.isRetryable }
|
|
317
|
-
], [exceptionVar])
|
|
318
|
-
], [
|
|
319
|
-
new GrammerExpr(
|
|
320
|
-
new GrammerVar('_lastException', this.combinator.addInclude('$Exception'), 'var'),
|
|
321
|
-
Symbol.assign(),
|
|
322
|
-
exceptionVar
|
|
323
|
-
),
|
|
324
|
-
new GrammerContinue(whileOperation.index)
|
|
325
|
-
]),
|
|
326
|
-
new GrammerThrows(null, [exceptionParam])
|
|
327
|
-
], catchException);
|
|
328
|
-
this.requestBody(ast, body, requestTryCatch);
|
|
329
|
-
requestTryCatch.addCatch(tryCatch);
|
|
330
|
-
|
|
331
|
-
whileOperation.addBodyNode(requestTryCatch);
|
|
332
|
-
|
|
333
|
-
func.addBodyNode(whileOperation);
|
|
334
|
-
|
|
335
|
-
func.addBodyNode(
|
|
336
|
-
new GrammerThrows(
|
|
337
|
-
this.combinator.addInclude('$ExceptionUnretryable'), [
|
|
338
|
-
new GrammerValue('var', new GrammerVar('_lastRequest', this.combinator.addInclude('$Request'))),
|
|
339
|
-
new GrammerValue('var', new GrammerVar('_lastException', this.combinator.addInclude('$Exception')))
|
|
340
|
-
]
|
|
341
|
-
)
|
|
342
|
-
);
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
requestBody(ast, body, func) {
|
|
346
|
-
if (body) {
|
|
347
|
-
if (body.type === 'apiBody') {
|
|
348
|
-
// TeaRequest _request = new TeaRequest()
|
|
349
|
-
func.addBodyNode(
|
|
350
|
-
new GrammerExpr(
|
|
351
|
-
new GrammerVar(this.config.request, this.combinator.addInclude('$Request')),
|
|
352
|
-
Symbol.assign(),
|
|
353
|
-
new GrammerNewObject(this.combinator.addInclude('$Request'))
|
|
354
|
-
)
|
|
355
|
-
);
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
const stmt = ['declares', 'protocol', 'port', 'method', 'pathname', 'quert', 'headers', 'body', 'appendStmts'];
|
|
359
|
-
stmt.forEach(key => {
|
|
360
|
-
if (body[key] && body[key] !== 'undefined') {
|
|
361
|
-
if (Array.isArray(body[key])) {
|
|
362
|
-
body[key].forEach(stmtItem => {
|
|
363
|
-
this.visitStmt(func, stmtItem, func.index);
|
|
364
|
-
});
|
|
365
|
-
} else {
|
|
366
|
-
this.visitStmt(func, body[key], func.index);
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
if (body.type === 'apiBody') {
|
|
372
|
-
var doActionParams = [];
|
|
373
|
-
doActionParams.push(new GrammerValue('param', this.config.request));
|
|
374
|
-
|
|
375
|
-
if (ast.runtimeBody) {
|
|
376
|
-
doActionParams.push(new GrammerValue('param', this.config.runtime));
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
// response = Tea.doAction
|
|
380
|
-
const doActionBehavior = new BehaviorDoAction(new GrammerVar(this.config.response, '$Response'), doActionParams);
|
|
381
|
-
|
|
382
|
-
if (body.stmts) {
|
|
383
|
-
body.stmts.stmts.forEach(stmt => {
|
|
384
|
-
this.visitStmt(func, stmt, func.index);
|
|
385
|
-
});
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
// _lastRequest = request_;
|
|
389
|
-
func.addBodyNode(
|
|
390
|
-
new GrammerExpr(
|
|
391
|
-
new GrammerVar('_lastRequest', this.combinator.addInclude('$Request'), 'var'),
|
|
392
|
-
Symbol.assign(),
|
|
393
|
-
new GrammerVar(this.config.request, this.combinator.addInclude('$Request'))
|
|
394
|
-
)
|
|
395
|
-
);
|
|
396
|
-
|
|
397
|
-
// returns
|
|
398
|
-
if (ast.returns) {
|
|
399
|
-
if (ast.returns.stmts.stmts.length > 0) {
|
|
400
|
-
ast.returns.stmts.stmts.forEach(stmt => {
|
|
401
|
-
this.visitStmt(doActionBehavior, stmt, doActionBehavior.index);
|
|
402
|
-
});
|
|
403
|
-
} else {
|
|
404
|
-
this.findComments(doActionBehavior, ast.returns, 'between');
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
func.addBodyNode(doActionBehavior);
|
|
408
|
-
}
|
|
409
|
-
if (body.tokenRange) {
|
|
410
|
-
this.resolveAnnotations(this.getFrontComments(body.tokenRange[1], func.index)).forEach(c => {
|
|
411
|
-
func.addBodyNode(c);
|
|
412
|
-
});
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
renderGrammerValue(valGrammer, object, expectedType = null) {
|
|
418
|
-
if (!valGrammer) {
|
|
419
|
-
valGrammer = new GrammerValue();
|
|
420
|
-
}
|
|
421
|
-
if (!valGrammer.value && object.type === 'object') {
|
|
422
|
-
valGrammer.value = [];
|
|
423
|
-
}
|
|
424
|
-
if (object.type === 'variable') {
|
|
425
|
-
if (object.needCast) {
|
|
426
|
-
let grammerVar = new GrammerVar(object.id.lexeme);
|
|
427
|
-
valGrammer.type = 'behavior';
|
|
428
|
-
grammerVar.belong = valGrammer.index;
|
|
429
|
-
const behaviorToMap = new BehaviorToMap(grammerVar, object.inferred);
|
|
430
|
-
behaviorToMap.belong = valGrammer.index;
|
|
431
|
-
valGrammer.value = behaviorToMap;
|
|
432
|
-
} else {
|
|
433
|
-
valGrammer.type = 'var';
|
|
434
|
-
let grammerVar = new GrammerVar(object.id.lexeme);
|
|
435
|
-
if (object.id.type === 'model') {
|
|
436
|
-
grammerVar.varType = 'static_class';
|
|
437
|
-
grammerVar.name = this.combinator.addModelInclude(object.id.lexeme);
|
|
438
|
-
} else if (object.type === 'variable') {
|
|
439
|
-
grammerVar.varType = 'var';
|
|
440
|
-
}
|
|
441
|
-
valGrammer.value = grammerVar;
|
|
442
|
-
}
|
|
443
|
-
} else if (object.type === 'object') {
|
|
444
|
-
object.fields.forEach(field => {
|
|
445
|
-
var val = null;
|
|
446
|
-
var type = field.expr.type;
|
|
447
|
-
if (field.expr.type === 'object') {
|
|
448
|
-
val = [];
|
|
449
|
-
type = 'array';
|
|
450
|
-
}
|
|
451
|
-
var exprChild = new GrammerValue(type, val);
|
|
452
|
-
if (field.type === 'expandField') {
|
|
453
|
-
valGrammer.needCast = true;
|
|
454
|
-
exprChild.isExpand = true;
|
|
455
|
-
}
|
|
456
|
-
if (field.fieldName && (field.fieldName.lexeme || field.fieldName.string)) {
|
|
457
|
-
exprChild.key = (field.fieldName.lexeme) || _string(field.fieldName);
|
|
458
|
-
}
|
|
459
|
-
this.renderGrammerValue(exprChild, field.expr, expectedType);
|
|
460
|
-
this.findComments(valGrammer, field);
|
|
461
|
-
valGrammer.value.push(exprChild);
|
|
462
|
-
this.findComments(valGrammer, field.expr, 'back');
|
|
463
|
-
});
|
|
464
|
-
valGrammer.type = 'array';
|
|
465
|
-
valGrammer.expected = expectedType ? this.resolveType(expectedType) : null;
|
|
466
|
-
} else if (object.type === 'string') {
|
|
467
|
-
valGrammer.type = 'string';
|
|
468
|
-
valGrammer.value = object.value.string;
|
|
469
|
-
} else if (object.type === 'property_access') {
|
|
470
|
-
var current = object.id.inferred;
|
|
471
|
-
|
|
472
|
-
let call = new GrammerCall('key');
|
|
473
|
-
var paramName = object.id.lexeme;
|
|
474
|
-
call.addPath({ type: 'object', name: paramName });
|
|
475
|
-
for (var i = 0; i < object.propertyPath.length; i++) {
|
|
476
|
-
var name = object.propertyPath[i].lexeme;
|
|
477
|
-
|
|
478
|
-
if (current.type === 'model') {
|
|
479
|
-
call.addPath({ type: 'prop', name: name });
|
|
480
|
-
} else {
|
|
481
|
-
call.addPath({ type: 'map', name: name });
|
|
482
|
-
}
|
|
483
|
-
current = object.propertyPathTypes[i];
|
|
484
|
-
}
|
|
485
|
-
valGrammer.type = 'call';
|
|
486
|
-
valGrammer.value = call;
|
|
487
|
-
if (object.needCast) {
|
|
488
|
-
valGrammer.type = 'behavior';
|
|
489
|
-
valGrammer.value = new BehaviorToMap(valGrammer.value, object.inferred);
|
|
490
|
-
}
|
|
491
|
-
} else if (object.type === 'number') {
|
|
492
|
-
valGrammer.type = 'number';
|
|
493
|
-
valGrammer.value = object.value.value;
|
|
494
|
-
} else if (object.type === 'virtualVariable') {
|
|
495
|
-
valGrammer.type = 'call';
|
|
496
|
-
let call = new GrammerCall('prop', [
|
|
497
|
-
{ type: 'parent', name: '' },
|
|
498
|
-
{ type: 'prop', name: object.vid.lexeme },
|
|
499
|
-
]);
|
|
500
|
-
valGrammer.value = call;
|
|
501
|
-
} else if (object.type === 'template_string') {
|
|
502
|
-
valGrammer.type = 'behavior';
|
|
503
|
-
let tmpStr = '';
|
|
504
|
-
let exprItem = [];
|
|
505
|
-
object.elements.forEach(ele => {
|
|
506
|
-
if (ele.type === 'element') {
|
|
507
|
-
ele.value.string = ele.value.string.replace(/{/g, '{{').replace(/}/g, '}}');
|
|
508
|
-
tmpStr += ele.value.string;
|
|
509
|
-
} else if (ele.type === 'expr') {
|
|
510
|
-
tmpStr += '${}';
|
|
511
|
-
const exprValue = this.renderGrammerValue(null, ele.expr);
|
|
512
|
-
exprItem.push(exprValue);
|
|
513
|
-
}
|
|
514
|
-
});
|
|
515
|
-
valGrammer.value = new BehaviorStrFormat(tmpStr, exprItem);
|
|
516
|
-
} else if (object.type === 'null') {
|
|
517
|
-
valGrammer.type = 'null';
|
|
518
|
-
valGrammer.value = 'null';
|
|
519
|
-
} else if (object.type === 'construct_model') {
|
|
520
|
-
let objectName = object.aliasId.lexeme ? object.aliasId.lexeme : '';
|
|
521
|
-
if (object.propertyPath && object.propertyPath.length > 0) {
|
|
522
|
-
if (object.propertyPath.length > 0 && objectName !== '') {
|
|
523
|
-
objectName = objectName + '.';
|
|
524
|
-
}
|
|
525
|
-
object.propertyPath.forEach((p, i) => {
|
|
526
|
-
objectName += p.lexeme;
|
|
527
|
-
if (i !== object.propertyPath.length - 1) {
|
|
528
|
-
objectName += '.';
|
|
529
|
-
}
|
|
530
|
-
});
|
|
531
|
-
}
|
|
532
|
-
objectName = this.combinator.addModelInclude(objectName);
|
|
533
|
-
|
|
534
|
-
valGrammer.type = 'instance';
|
|
535
|
-
let params = [];
|
|
536
|
-
if (object.object) {
|
|
537
|
-
params = this.renderGrammerValue(null, object.object);
|
|
538
|
-
if (params.value.length === 0) {
|
|
539
|
-
this.findComments(params, object.object, 'between');
|
|
540
|
-
}
|
|
541
|
-
if (object.object.inferred.type === 'map') {
|
|
542
|
-
params.type = 'model_construct_params';
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
valGrammer.value = new GrammerNewObject(objectName, params);
|
|
546
|
-
} else if (object.type === 'call') {
|
|
547
|
-
let call_type = 'method';
|
|
548
|
-
valGrammer.type = 'call';
|
|
549
|
-
if (object.left && object.left.id && object.left.id.type === 'module') {
|
|
550
|
-
if (systemPackage.indexOf(object.left.id.lexeme) > -1) {
|
|
551
|
-
call_type = 'sys_func';
|
|
552
|
-
}
|
|
553
|
-
} else {
|
|
554
|
-
valGrammer.type = 'call';
|
|
555
|
-
call_type = 'method';
|
|
556
|
-
}
|
|
557
|
-
let call = new GrammerCall(call_type);
|
|
558
|
-
|
|
559
|
-
if (object.left) {
|
|
560
|
-
let staticTag = object.isStatic ? '_static' : '';
|
|
561
|
-
let asyncTag = object.isAsync ? '_async' : '';
|
|
562
|
-
const callType = staticTag + asyncTag;
|
|
563
|
-
|
|
564
|
-
if (object.left.type === 'method_call') {
|
|
565
|
-
call.addPath({ type: 'parent' + asyncTag, name: '' });
|
|
566
|
-
call.addPath({ type: 'call' + callType, name: object.left.id.lexeme });
|
|
567
|
-
} else if (object.left.type === 'instance_call') {
|
|
568
|
-
if (object.left && object.left.id && object.left.id.lexeme) {
|
|
569
|
-
if (object.left.id.type === 'variable') {
|
|
570
|
-
call.addPath({ type: 'object' + callType, name: object.left.id.lexeme });
|
|
571
|
-
} else if (typeof object.left.id.type === 'undefined' && object.left.id.lexeme.indexOf('@') > -1) {
|
|
572
|
-
call.addPath({ type: 'parent' + asyncTag, name: object.left.id.lexeme.replace('@', '_') });
|
|
573
|
-
} else {
|
|
574
|
-
debug.stack('Unsupported object.left.id.type : ' + object.left.id.type, object);
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
} else if (object.left.type === 'static_call') {
|
|
578
|
-
if (object.left.id.type === 'module') {
|
|
579
|
-
call.addPath({ type: 'object' + callType, name: this.combinator.addInclude(object.left.id.lexeme) });
|
|
580
|
-
} else {
|
|
581
|
-
// call.addPath({ type: 'call_static', name: object.left.id.lexeme });
|
|
582
|
-
debug.stack(object);
|
|
583
|
-
}
|
|
584
|
-
} else {
|
|
585
|
-
debug.stack(object.left);
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
if (object.left.propertyPath) {
|
|
589
|
-
object.left.propertyPath.forEach(p => {
|
|
590
|
-
call.addPath({
|
|
591
|
-
type: 'call' + callType,
|
|
592
|
-
name: p.lexeme
|
|
593
|
-
});
|
|
594
|
-
});
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
if (object.args) {
|
|
598
|
-
object.args.forEach(arg => {
|
|
599
|
-
const grammerValue = this.renderGrammerValue(null, arg);
|
|
600
|
-
grammerValue.belong = call.index;
|
|
601
|
-
call.addParams(grammerValue);
|
|
602
|
-
});
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
valGrammer.value = call;
|
|
606
|
-
} else if (object.type === 'construct') {
|
|
607
|
-
valGrammer.type = 'module_instance';
|
|
608
|
-
const objectName = this.combinator.addInclude(object.aliasId.lexeme);
|
|
609
|
-
valGrammer.value = new GrammerNewObject(objectName);
|
|
610
|
-
object.args.forEach(item => {
|
|
611
|
-
valGrammer.value.addParam(this.renderGrammerValue(null, item));
|
|
612
|
-
});
|
|
613
|
-
} else if (object.type === 'boolean') {
|
|
614
|
-
valGrammer.type = 'bool';
|
|
615
|
-
valGrammer.value = object.value;
|
|
616
|
-
} else if (object.type === 'not') {
|
|
617
|
-
valGrammer.type = 'not';
|
|
618
|
-
valGrammer.value = this.renderGrammerValue(null, object.expr);
|
|
619
|
-
} else if (object.type === 'array') {
|
|
620
|
-
valGrammer.type = 'array';
|
|
621
|
-
valGrammer.value = [];
|
|
622
|
-
if (object.items.length > 0) {
|
|
623
|
-
object.items.forEach(field => {
|
|
624
|
-
this.findComments(valGrammer, field);
|
|
625
|
-
var exprChild = this.renderGrammerValue(null, field, expectedType);
|
|
626
|
-
valGrammer.value.push(exprChild);
|
|
627
|
-
this.findComments(valGrammer, field, 'back');
|
|
628
|
-
});
|
|
629
|
-
} else {
|
|
630
|
-
this.findComments(valGrammer, object, 'between');
|
|
631
|
-
}
|
|
632
|
-
valGrammer.type = 'array';
|
|
633
|
-
valGrammer.expected = expectedType ? this.resolveType(expectedType) : null;
|
|
634
|
-
} else if (object.type === 'property') {
|
|
635
|
-
object.type = 'property_access';
|
|
636
|
-
this.renderGrammerValue(valGrammer, object);
|
|
637
|
-
} else if (object.type === 'super') {
|
|
638
|
-
valGrammer.type = 'call';
|
|
639
|
-
let call = new GrammerCall('super');
|
|
640
|
-
object.args.forEach(arg => {
|
|
641
|
-
call.addParams(this.renderGrammerValue(null, arg));
|
|
642
|
-
});
|
|
643
|
-
valGrammer.value = call;
|
|
644
|
-
} else if (object.type === 'array_access') {
|
|
645
|
-
valGrammer.type = 'call';
|
|
646
|
-
let accessIndex;
|
|
647
|
-
if (object.accessKey.type === 'number') {
|
|
648
|
-
accessIndex = object.accessKey.value.value;
|
|
649
|
-
} else if (object.accessKey.type === 'variable') {
|
|
650
|
-
accessIndex = object.accessKey.id.lexeme;
|
|
651
|
-
} else {
|
|
652
|
-
debug.stack(object);
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
let call = new GrammerCall('key', [
|
|
656
|
-
{ type: 'object', name: object.id.lexeme },
|
|
657
|
-
]);
|
|
658
|
-
if (object.propertyPath) {
|
|
659
|
-
const current = object.id.inferred;
|
|
660
|
-
object.propertyPath.forEach(path => {
|
|
661
|
-
if (current.type === 'model') {
|
|
662
|
-
call.addPath({ type: 'prop', name: path.lexeme });
|
|
663
|
-
} else if (current.type === 'map') {
|
|
664
|
-
call.addPath({ type: 'map_set', name: path.lexeme });
|
|
665
|
-
} else {
|
|
666
|
-
call.addPath({ type: 'list', name: path.lexeme });
|
|
667
|
-
}
|
|
668
|
-
});
|
|
669
|
-
}
|
|
670
|
-
call.addPath({ type: 'list', name: accessIndex, isVar: object.accessKey.type === 'variable' });
|
|
671
|
-
valGrammer.value = call;
|
|
672
|
-
} else if (object.type === 'map_access') {
|
|
673
|
-
valGrammer.type = 'call';
|
|
674
|
-
let isVar = false;
|
|
675
|
-
if (object.accessKey.type === 'variable') {
|
|
676
|
-
isVar = true;
|
|
677
|
-
}
|
|
678
|
-
|
|
679
|
-
let accessKey = object.accessKey.id ? object.accessKey.id.lexeme : object.accessKey.value.lexeme;
|
|
680
|
-
if (object.accessKey.id) {
|
|
681
|
-
accessKey = object.accessKey.id.lexeme;
|
|
682
|
-
} else if (object.accessKey.type === 'string') {
|
|
683
|
-
accessKey = object.accessKey.value.string;
|
|
684
|
-
} else {
|
|
685
|
-
debug.stack(object);
|
|
686
|
-
}
|
|
687
|
-
let call = new GrammerCall('key', [
|
|
688
|
-
{ type: 'object', name: object.id.lexeme },
|
|
689
|
-
]);
|
|
690
|
-
if (object.propertyPath) {
|
|
691
|
-
const current = object.id.inferred;
|
|
692
|
-
object.propertyPath.forEach(path => {
|
|
693
|
-
if (current.type === 'model') {
|
|
694
|
-
call.addPath({ type: 'prop', name: path.lexeme });
|
|
695
|
-
} else if (current.type === 'map') {
|
|
696
|
-
call.addPath({ type: 'map_set', name: path.lexeme });
|
|
697
|
-
} else {
|
|
698
|
-
call.addPath({ type: 'list', name: path.lexeme });
|
|
699
|
-
}
|
|
700
|
-
});
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
if (object.accessKey.propertyPath) {
|
|
704
|
-
isVar = true;
|
|
705
|
-
object.accessKey.propertyPath.forEach(prop => {
|
|
706
|
-
accessKey += `.${prop.lexeme}`;
|
|
707
|
-
});
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
call.addPath({ type: 'map', name: accessKey, isVar: isVar});
|
|
711
|
-
valGrammer.value = call;
|
|
712
|
-
} else if (['and', 'or', 'not'].indexOf(object.type) > -1) {
|
|
713
|
-
valGrammer.type = 'expr';
|
|
714
|
-
valGrammer.value = this.visitIfConfition(object);
|
|
715
|
-
} else {
|
|
716
|
-
debug.stack('unimpelemented : ' + object.type, object);
|
|
717
|
-
}
|
|
718
|
-
|
|
719
|
-
return valGrammer;
|
|
720
|
-
}
|
|
721
|
-
|
|
722
|
-
visitStmt(obj, stmt, belong) {
|
|
723
|
-
let node;
|
|
724
|
-
let renderByGrammerValueTypes = [
|
|
725
|
-
'construct_model',
|
|
726
|
-
'super',
|
|
727
|
-
'map_access',
|
|
728
|
-
'boolean',
|
|
729
|
-
'not',
|
|
730
|
-
'property_access'
|
|
731
|
-
];
|
|
732
|
-
if (renderByGrammerValueTypes.indexOf(stmt.type) > -1) {
|
|
733
|
-
node = this.renderGrammerValue(null, stmt);
|
|
734
|
-
} else if (stmt.type === 'declare') {
|
|
735
|
-
let type = null;
|
|
736
|
-
if (stmt.expectedType) {
|
|
737
|
-
type = stmt.expectedType.lexeme;
|
|
738
|
-
} else if (stmt.expr.inferred) {
|
|
739
|
-
type = stmt.expr.inferred.name;
|
|
740
|
-
} else {
|
|
741
|
-
debug.stack(stmt);
|
|
742
|
-
}
|
|
743
|
-
let expectedType = stmt.expectedType ? stmt.expectedType : null;
|
|
744
|
-
let variate = new GrammerVar(stmt.id.lexeme, type);
|
|
745
|
-
let value = this.renderGrammerValue(null, stmt.expr, expectedType);
|
|
746
|
-
node = new GrammerExpr(variate, Symbol.assign(), value);
|
|
747
|
-
} else if (stmt.type === 'requestAssign') {
|
|
748
|
-
let variate = new GrammerCall('prop', [
|
|
749
|
-
{ type: 'object', name: this.config.request },
|
|
750
|
-
{ type: 'prop', name: stmt.left.id.lexeme }
|
|
751
|
-
]);
|
|
752
|
-
let value = this.renderGrammerValue(null, stmt.expr);
|
|
753
|
-
if (stmt.left.type === 'request_property_assign') {
|
|
754
|
-
let key = '';
|
|
755
|
-
stmt.left.propertyPath.forEach((p, i) => {
|
|
756
|
-
if (i === stmt.left.propertyPath.length - 1) {
|
|
757
|
-
key = p.lexeme;
|
|
758
|
-
} else {
|
|
759
|
-
variate.addPath({ type: 'map', name: p.lexeme });
|
|
760
|
-
}
|
|
761
|
-
});
|
|
762
|
-
node = new BehaviorSetMapItem(variate, key, value);
|
|
763
|
-
} else {
|
|
764
|
-
node = new GrammerExpr(variate, Symbol.assign(), value);
|
|
765
|
-
}
|
|
766
|
-
} else if (stmt.type === 'ifRequestAssign' || stmt.type === 'if') {
|
|
767
|
-
node = this.visitIfElse(stmt, 'if');
|
|
768
|
-
} else if (stmt.type === 'elseIfRequestAssign') {
|
|
769
|
-
node = this.visitIfElse(stmt, 'elseif');
|
|
770
|
-
} else if (stmt.type === 'return') {
|
|
771
|
-
node = new GrammerReturn();
|
|
772
|
-
if (typeof stmt.expr === 'undefined') {
|
|
773
|
-
node.type = 'null';
|
|
774
|
-
} else if (stmt.expr.type === 'null') {
|
|
775
|
-
node.type = 'null';
|
|
776
|
-
} else if (stmt.expr.type === 'call') {
|
|
777
|
-
let val = this.renderGrammerValue(null, stmt.expr);
|
|
778
|
-
node.expr = val;
|
|
779
|
-
node.type = 'grammer';
|
|
780
|
-
} else if (_isBasicType(stmt.expr.type)) {
|
|
781
|
-
node.type = stmt.expr.type;
|
|
782
|
-
node.expr = this.renderGrammerValue(null, stmt.expr);
|
|
783
|
-
} else {
|
|
784
|
-
node.expr = this.renderGrammerValue(null, stmt.expr);
|
|
785
|
-
node.type = 'grammer';
|
|
786
|
-
}
|
|
787
|
-
if (stmt.expectedType && stmt.expectedType.type === 'model') {
|
|
788
|
-
let expected = '';
|
|
789
|
-
if (stmt.expectedType.moduleName) {
|
|
790
|
-
expected += `${stmt.expectedType.moduleName}.`;
|
|
791
|
-
}
|
|
792
|
-
expected += stmt.expectedType.name;
|
|
793
|
-
node.expr = new BehaviorToModel(node.expr, expected);
|
|
794
|
-
}
|
|
795
|
-
} else if (stmt.type === 'throw') {
|
|
796
|
-
node = new GrammerThrows(this.combinator.addInclude('$Exception'));
|
|
797
|
-
if (Array.isArray(stmt.expr)) {
|
|
798
|
-
stmt.expr.forEach(e => {
|
|
799
|
-
node.addParam(this.renderGrammerValue(null, e));
|
|
800
|
-
});
|
|
801
|
-
} else {
|
|
802
|
-
node.addParam(this.renderGrammerValue(null, stmt.expr));
|
|
803
|
-
}
|
|
804
|
-
} else if (stmt.type === 'virtualCall') {
|
|
805
|
-
let val = this.renderGrammerValue(null, stmt);
|
|
806
|
-
node = val.value;
|
|
807
|
-
} else if (stmt.type === 'while') {
|
|
808
|
-
node = new GrammerCondition('while');
|
|
809
|
-
if (Array.isArray(stmt.condition)) {
|
|
810
|
-
stmt.condition.forEach(c => {
|
|
811
|
-
node.addCondition(this.renderGrammerValue(null, c));
|
|
812
|
-
});
|
|
813
|
-
} else {
|
|
814
|
-
node.addCondition(this.renderGrammerValue(null, stmt.condition));
|
|
815
|
-
}
|
|
816
|
-
if (stmt.stmts) {
|
|
817
|
-
stmt.stmts.stmts.forEach(s => {
|
|
818
|
-
this.visitStmt(node, s, node.index);
|
|
819
|
-
});
|
|
820
|
-
}
|
|
821
|
-
} else if (stmt.type === 'for') {
|
|
822
|
-
node = new GrammerLoop('foreach');
|
|
823
|
-
node.item = new GrammerVar(stmt.id.lexeme);
|
|
824
|
-
node.source = this.renderGrammerValue(null, stmt.list);
|
|
825
|
-
if (stmt.stmts) {
|
|
826
|
-
stmt.stmts.stmts.forEach(s => {
|
|
827
|
-
this.visitStmt(node, s, node.index);
|
|
828
|
-
});
|
|
829
|
-
}
|
|
830
|
-
} else if (stmt.type === 'assign') {
|
|
831
|
-
let hasMapAccess = false;
|
|
832
|
-
const right = this.renderGrammerValue(null, stmt.expr);
|
|
833
|
-
if (stmt.left.type === 'property') {
|
|
834
|
-
hasMapAccess = stmt.left.propertyPathTypes.some(item => item.type === 'map');
|
|
835
|
-
if (hasMapAccess) {
|
|
836
|
-
const grammerValue = this.renderGrammerValue(null, stmt.left);
|
|
837
|
-
const call = grammerValue.value;
|
|
838
|
-
let lastIndex = 0;
|
|
839
|
-
for (let i = 0; i < stmt.left.propertyPathTypes.length; i++) {
|
|
840
|
-
const propertyPathType = stmt.left.propertyPathTypes[i];
|
|
841
|
-
if (propertyPathType.type === 'map') {
|
|
842
|
-
lastIndex = i;
|
|
843
|
-
break;
|
|
844
|
-
}
|
|
845
|
-
}
|
|
846
|
-
if (lastIndex + 1 < stmt.left.propertyPathTypes.length) {
|
|
847
|
-
call.type = 'prop';
|
|
848
|
-
call.path = call.path.splice(0, lastIndex + 2);
|
|
849
|
-
const key = stmt.left.propertyPath[lastIndex + 1].lexeme;
|
|
850
|
-
node = new BehaviorSetMapItem(call, key, right);
|
|
851
|
-
} else {
|
|
852
|
-
hasMapAccess = false;
|
|
853
|
-
}
|
|
854
|
-
}
|
|
855
|
-
} else if (stmt.left.type === 'map_access') {
|
|
856
|
-
hasMapAccess = true;
|
|
857
|
-
const grammerValue = this.renderGrammerValue(null, stmt.left);
|
|
858
|
-
const call = grammerValue.value;
|
|
859
|
-
call.type = 'prop';
|
|
860
|
-
call.path = call.path.splice(0, call.path.length - 1);
|
|
861
|
-
let isVar = false;
|
|
862
|
-
var item = stmt.left.accessKey.value ? stmt.left.accessKey.value.string : stmt.left.accessKey.id.lexeme;
|
|
863
|
-
if (stmt.left.accessKey.type === 'variable') {
|
|
864
|
-
isVar = true;
|
|
865
|
-
} else if (stmt.left.accessKey.type === 'property_access') {
|
|
866
|
-
isVar = true;
|
|
867
|
-
stmt.left.accessKey.propertyPath.forEach(p => {
|
|
868
|
-
item += `.${p.lexeme}`;
|
|
869
|
-
});
|
|
870
|
-
}
|
|
871
|
-
node = new BehaviorSetMapItem(call, item, right, isVar);
|
|
872
|
-
}
|
|
873
|
-
|
|
874
|
-
if (!hasMapAccess) {
|
|
875
|
-
const left = this.renderGrammerValue(null, stmt.left);
|
|
876
|
-
let isBehavior = false;
|
|
877
|
-
if (left.type === 'call' && left.value instanceof GrammerCall && left.value.type === 'key') {
|
|
878
|
-
const keyPath = left.value.path[left.value.path.length - 1];
|
|
879
|
-
if (keyPath.type === 'map') {
|
|
880
|
-
left.value.path = left.value.path.slice(0, left.value.path.length - 1);
|
|
881
|
-
node = new BehaviorSetMapItem(left.value, keyPath.name, right);
|
|
882
|
-
isBehavior = true;
|
|
883
|
-
}
|
|
884
|
-
}
|
|
885
|
-
if (!isBehavior) {
|
|
886
|
-
node = new GrammerExpr(
|
|
887
|
-
left,
|
|
888
|
-
Symbol.assign(),
|
|
889
|
-
right
|
|
890
|
-
);
|
|
891
|
-
}
|
|
892
|
-
}
|
|
893
|
-
} else if (stmt.type === 'call') {
|
|
894
|
-
node = this.renderGrammerValue(null, stmt).value;
|
|
895
|
-
} else if (stmt.type === 'break') {
|
|
896
|
-
node = new GrammerBreak();
|
|
897
|
-
} else if (stmt.type === 'retry') {
|
|
898
|
-
node = new BehaviorRetry();
|
|
899
|
-
} else if (stmt.type === 'try') {
|
|
900
|
-
const tryGram = new GrammerTryCatch();
|
|
901
|
-
stmt.tryBlock.stmts.forEach(item => {
|
|
902
|
-
this.visitStmt(tryGram, item, tryGram.index);
|
|
903
|
-
});
|
|
904
|
-
if (stmt.catchId) {
|
|
905
|
-
const exceptionGram = new GrammerException(
|
|
906
|
-
Exceptions.base(),
|
|
907
|
-
new GrammerVar(stmt.catchId.lexeme)
|
|
908
|
-
);
|
|
909
|
-
const catchGram = new GrammerCatch([], exceptionGram);
|
|
910
|
-
|
|
911
|
-
if (stmt.catchBlock) {
|
|
912
|
-
stmt.catchBlock.stmts.forEach(item => {
|
|
913
|
-
this.visitStmt(catchGram, item, catchGram.index);
|
|
914
|
-
});
|
|
915
|
-
}
|
|
916
|
-
tryGram.addCatch(catchGram);
|
|
917
|
-
}
|
|
918
|
-
if (stmt.finallyBlock) {
|
|
919
|
-
const finallyGram = new GrammerFinally();
|
|
920
|
-
stmt.finallyBlock.stmts.forEach(item => {
|
|
921
|
-
this.visitStmt(finallyGram, item, finallyGram.index);
|
|
922
|
-
});
|
|
923
|
-
tryGram.setFinally(finallyGram);
|
|
924
|
-
}
|
|
925
|
-
node = tryGram;
|
|
926
|
-
} else {
|
|
927
|
-
debug.stack(stmt);
|
|
928
|
-
}
|
|
929
|
-
if (belong) {
|
|
930
|
-
node.belong = belong;
|
|
931
|
-
}
|
|
932
|
-
this.findComments(obj, stmt, belong);
|
|
933
|
-
obj.addBodyNode(node);
|
|
934
|
-
this.findComments(obj, stmt, belong, 'back');
|
|
935
|
-
}
|
|
936
|
-
|
|
937
|
-
visitIfConfition(stmtCondition) {
|
|
938
|
-
let condition;
|
|
939
|
-
if (stmtCondition.left) {
|
|
940
|
-
let opt;
|
|
941
|
-
let optList = ['and', 'or', 'not'];
|
|
942
|
-
if (optList.indexOf(stmtCondition.type) > -1) {
|
|
943
|
-
switch (stmtCondition.type) {
|
|
944
|
-
case 'and': opt = Symbol.and(); break;
|
|
945
|
-
case 'or': opt = Symbol.or(); break;
|
|
946
|
-
case 'not': opt = Symbol.not(); break;
|
|
947
|
-
}
|
|
948
|
-
condition = new GrammerExpr(
|
|
949
|
-
this.renderGrammerValue(null, stmtCondition.left),
|
|
950
|
-
opt,
|
|
951
|
-
this.renderGrammerValue(null, stmtCondition.right)
|
|
952
|
-
);
|
|
953
|
-
} else if (stmtCondition.type === 'call') {
|
|
954
|
-
condition = this.renderGrammerValue(null, stmtCondition);
|
|
955
|
-
} else {
|
|
956
|
-
debug.stack(stmtCondition);
|
|
957
|
-
}
|
|
958
|
-
} else {
|
|
959
|
-
condition = this.renderGrammerValue(null, stmtCondition);
|
|
960
|
-
}
|
|
961
|
-
return condition;
|
|
962
|
-
}
|
|
963
|
-
|
|
964
|
-
visitIfElse(stmt, type = 'if') {
|
|
965
|
-
let node = new GrammerCondition(type);
|
|
966
|
-
if (stmt.condition) {
|
|
967
|
-
node.addCondition(this.visitIfConfition(stmt.condition));
|
|
968
|
-
}
|
|
969
|
-
if (stmt.stmts) {
|
|
970
|
-
if (stmt.stmts.type && stmt.stmts.type === 'stmts') {
|
|
971
|
-
if (stmt.stmts.stmts.length > 0) {
|
|
972
|
-
stmt.stmts.stmts.forEach(item => {
|
|
973
|
-
this.visitStmt(node, item, node.index);
|
|
974
|
-
});
|
|
975
|
-
} else {
|
|
976
|
-
this.findComments(node, stmt.stmts, 'between');
|
|
977
|
-
}
|
|
978
|
-
} else if (stmt.type && stmt.type === 'stmts') {
|
|
979
|
-
if (stmt.stmts.length > 0) {
|
|
980
|
-
stmt.stmts.forEach(item => {
|
|
981
|
-
this.visitStmt(node, item, node.index);
|
|
982
|
-
});
|
|
983
|
-
} else {
|
|
984
|
-
this.findComments(node, stmt, 'between');
|
|
985
|
-
}
|
|
986
|
-
} else {
|
|
987
|
-
debug.stack(stmt);
|
|
988
|
-
}
|
|
989
|
-
} else {
|
|
990
|
-
debug.stack(stmt);
|
|
991
|
-
}
|
|
992
|
-
|
|
993
|
-
if (stmt.elseIfs) {
|
|
994
|
-
stmt.elseIfs.forEach(elseIf => {
|
|
995
|
-
node.addElse(this.visitIfElse(elseIf, 'elseif'));
|
|
996
|
-
});
|
|
997
|
-
}
|
|
998
|
-
|
|
999
|
-
if (stmt.elseStmts) {
|
|
1000
|
-
node.addElse(this.visitIfElse(stmt.elseStmts, 'else'));
|
|
1001
|
-
}
|
|
1002
|
-
|
|
1003
|
-
if (stmt.elseAssigns) {
|
|
1004
|
-
stmt.elseAssigns.forEach(elseAssign => {
|
|
1005
|
-
node.elseItem.push(this.visitIfElse(elseAssign, 'else'));
|
|
1006
|
-
});
|
|
1007
|
-
}
|
|
1008
|
-
|
|
1009
|
-
return node;
|
|
1010
|
-
}
|
|
1011
|
-
}
|
|
1012
|
-
|
|
1013
|
-
module.exports = ClientResolver;
|