@darabonba/python-generator 2.0.0 → 2.0.2

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.
Files changed (38) hide show
  1. package/lib/generator.js +43 -29
  2. package/package.json +1 -1
  3. package/tests/expected/complex/tea_python_tests/client.py +5 -4
  4. package/tests/expected/complex/tea_python_tests/models.py +16 -16
  5. package/tests/expected/model/tea_python_tests/models.py +41 -58
  6. package/tests/expected/multi/tea_python_tests/model/user.py +3 -0
  7. package/tests/fixtures/complex/main.dara +1 -0
  8. package/tests/fixtures/multi/libraries/darabonba_Util_0.2.11/Teafile +7 -1
  9. package/tests/fixtures/multi/libraries/darabonba_Util_0.2.11/util.tea +2 -0
  10. package/tests/fixtures/multi/model/user.dara +2 -0
  11. package/tests/output/complex/tea_python_tests/client.py +5 -4
  12. package/tests/output/complex/tea_python_tests/models.py +16 -16
  13. package/tests/output/model/tea_python_tests/models.py +41 -58
  14. package/tests/output/multi/tea_python_tests/model/user.py +3 -0
  15. package/src/generator.js +0 -231
  16. package/src/langs/common/combinator.js +0 -193
  17. package/src/langs/common/config.js +0 -60
  18. package/src/langs/common/enum.js +0 -179
  19. package/src/langs/common/items.js +0 -551
  20. package/src/langs/common/package_info.js +0 -53
  21. package/src/langs/python/combinator.js +0 -1898
  22. package/src/langs/python/config.js +0 -169
  23. package/src/langs/python/files/.gitignore.tmpl +0 -5
  24. package/src/langs/python/files/__init__.py.tmpl +0 -1
  25. package/src/langs/python/files/setup.py.tmpl +0 -74
  26. package/src/langs/python/package_info.js +0 -78
  27. package/src/langs/python2/combinator.js +0 -1770
  28. package/src/langs/python2/config.js +0 -162
  29. package/src/langs/python2/files/.gitignore.tmpl +0 -5
  30. package/src/langs/python2/files/__init__.py.tmpl +0 -1
  31. package/src/langs/python2/files/setup.py.tmpl +0 -82
  32. package/src/langs/python2/package_info.js +0 -78
  33. package/src/lib/debug.js +0 -55
  34. package/src/lib/emitter.js +0 -95
  35. package/src/lib/helper.js +0 -207
  36. package/src/resolver/base.js +0 -282
  37. package/src/resolver/client.js +0 -1013
  38. package/src/resolver/model.js +0 -159
@@ -1,282 +0,0 @@
1
- 'use strict';
2
-
3
- const debug = require('../lib/debug');
4
-
5
- const {
6
- _string,
7
- _escape,
8
- _isBasicType
9
- } = require('../lib/helper');
10
-
11
- const {
12
- AnnotationItem,
13
- PropItem,
14
- } = require('../langs/common/items');
15
-
16
- const {
17
- // Symbol,
18
- Modify,
19
- // Exceptions,
20
- // Types
21
- } = require('../langs/common/enum');
22
-
23
- const DSL = require('@darabonba/parser');
24
-
25
- class BaseResolver {
26
- constructor(astNode, combinator, globalAst) {
27
- this.ast = astNode;
28
- this.combinator = combinator;
29
- this.config = combinator.config;
30
-
31
- this.comments = globalAst.comments ? globalAst.comments : {};
32
- this.commentsSet = [];
33
- }
34
-
35
- resolveAnnotations(annotations, belong) {
36
- if (annotations.length > 0) {
37
- let comments = [];
38
- annotations.forEach(annotation => {
39
- comments.push(this.resolveAnnotation(annotation, belong));
40
- });
41
- return comments;
42
- }
43
- return [];
44
- }
45
-
46
- resolveAnnotation(annotation, belong) {
47
- let type = annotation.value.indexOf('/**') === 0 ? 'multi' : 'single';
48
- let content = '';
49
- if (type === 'multi') {
50
- content = annotation.value.substring(3, annotation.value.length - 2)
51
- .trim().split('\n').filter(c => c.length > 0).map(c => {
52
- if (c.indexOf(' * ') === 0) {
53
- return c.substring(3);
54
- } else if (c.indexOf('* ') === 0) {
55
- return c.substring(2);
56
- }
57
- return c;
58
- });
59
- } else {
60
- content = annotation.value.substring(2).trim();
61
- }
62
- return new AnnotationItem(belong, type, content);
63
- }
64
-
65
- getBackComments(index) {
66
- return DSL.comment.getBackComments(this.comments, index);
67
- }
68
-
69
- getBetweenComments(begin, end) {
70
- return DSL.comment.getBetweenComments(this.comments, begin, end);
71
- }
72
-
73
- getFrontComments(index) {
74
- return DSL.comment.getFrontComments(this.comments, index);
75
- }
76
-
77
- getComments(node, position = 'front') {
78
- if (typeof node.tokenRange === 'undefined') {
79
- return [];
80
- }
81
- switch (position) {
82
- case 'back':
83
- return this.getBackComments(node.tokenRange[1]);
84
- case 'between':
85
- return this.getBetweenComments(node.tokenRange[0], node.tokenRange[1]);
86
- default:
87
- return this.getFrontComments(node.tokenRange[0]);
88
- }
89
- }
90
-
91
- addAnnotations(obj, node, position = 'front') {
92
- let comments = this.getComments(node, position);
93
- if (comments.length > 0) {
94
- comments.forEach(c => {
95
- if (this.commentsSet.indexOf(c.index) < 0) {
96
- if (typeof obj.annotations !== 'undefined') {
97
- obj.annotations.push(this.resolveAnnotation(c, obj.index));
98
- } else {
99
- debug.stack(obj, node);
100
- }
101
- this.commentsSet.push(c.index);
102
- }
103
- });
104
- }
105
- }
106
-
107
- findComments(obj, node, position = 'front') {
108
- let comments = this.getComments(node, position);
109
-
110
- if (comments.length > 0) {
111
- comments.forEach(c => {
112
- if (this.commentsSet.indexOf(c.index) < 0) {
113
- if (typeof obj.body !== 'undefined') {
114
- obj.body.push(this.resolveAnnotation(c, obj.index));
115
- } else if (typeof obj.value !== 'undefined') {
116
- obj.value.push(this.resolveAnnotation(c, obj.index));
117
- } else {
118
- debug.stack('Invalid data node', obj, node);
119
- }
120
- this.commentsSet.push(c.index);
121
- }
122
- });
123
- }
124
- }
125
-
126
- initAnnotation(annotation) {
127
- let topComments = this.getFrontComments(annotation.index);
128
- topComments.map(c => {
129
- this.object.topAnnotation.push(this.resolveAnnotation(c, this.object.index));
130
- });
131
- if (annotation && annotation.value) {
132
- this.object.annotations.push(this.resolveAnnotation(annotation, this.object.index));
133
- }
134
- }
135
-
136
- resolveProps(ast) {
137
- this.comments = ast.comments;
138
- ast.moduleBody.nodes.filter((item) => {
139
- return item.type === 'type';
140
- }).forEach(item => {
141
- const prop = new PropItem();
142
- prop.name = item.vid.lexeme.replace('@', '_');
143
- prop.type = this.resolveType(item.value, item);
144
- prop.addModify(Modify.protected());
145
- if (item.tokenRange) {
146
- let comments = this.getFrontComments(item.tokenRange[0]);
147
- if (comments.length > 0) {
148
- comments.forEach(c => {
149
- this.object.addBodyNode(this.resolveAnnotation(c, this.object.index));
150
- });
151
- }
152
- }
153
- this.object.addBodyNode(prop);
154
- });
155
- }
156
-
157
- resolveType(typeNode, sourceNode, prop) {
158
- let typeInfo = { objectType: 'module' };
159
- if (typeNode.idType) {
160
- if (typeNode.idType === 'model') {
161
- typeInfo['objectType'] = 'model';
162
- typeInfo['lexeme'] = this.combinator.addModelInclude(typeNode.lexeme);
163
- return typeInfo;
164
- } else if (typeNode.idType === 'module') {
165
- typeInfo['lexeme'] = this.combinator.addInclude(typeNode.lexeme);
166
- return typeInfo;
167
- } else if (typeNode.idType === 'typedef') {
168
- typeInfo['lexeme'] = this.combinator.addTypedefInclude(typeNode.lexeme);
169
- return typeInfo;
170
- } else if (typeNode.idType === 'builtin_model') {
171
- typeInfo['objectType'] = 'builtin_model';
172
- return this.combinator.addInclude(typeNode.lexeme);
173
- }
174
- debug.stack(typeNode, sourceNode);
175
- } else if (typeNode.type) {
176
- if (typeNode.type === 'fieldType') {
177
- return this.resolveType(typeNode.fieldType, typeNode, prop);
178
- } else if (typeNode.type === 'modelBody') {
179
- // is sub model
180
- typeInfo['objectType'] = 'model';
181
- typeInfo['lexeme'] = this.combinator.addModelInclude([this.object.name, _escape(sourceNode.fieldName.lexeme) || _string(sourceNode.fieldName)].join('.'));
182
- return typeInfo;
183
- } else if (_isBasicType(typeNode.type)) {
184
- return this.resolveType(typeNode.type, typeNode);
185
- } else if (typeNode.type === 'basic') {
186
- return this.resolveType(typeNode.name, sourceNode);
187
- } else if (typeNode.type === 'model') {
188
- let name = typeNode.name;
189
- if (typeNode.moduleName) {
190
- name = typeNode.moduleName + '.' + name;
191
- }
192
- typeInfo['objectType'] = 'model';
193
- typeInfo['lexeme'] = this.combinator.addModelInclude(name);
194
- return typeInfo;
195
- } else if (typeNode.type === 'module_instance') {
196
- typeInfo['lexeme'] = this.combinator.addInclude(typeNode.name);
197
- return typeInfo;
198
- } else if (typeNode.type === 'param') {
199
- if (typeNode.paramType.idType) {
200
- typeInfo['objectType'] = 'model';
201
- typeInfo['lexeme'] = this.combinator.addModelInclude(typeNode.paramType.lexeme);
202
- return typeInfo;
203
- }
204
- return this.resolveType(typeNode.paramType, typeNode);
205
- } else if (typeNode.type === 'array') {
206
- const subType = typeNode.subType ? typeNode.subType : typeNode.itemType;
207
- const arrayType = {
208
- lexeme: 'array',
209
- objectType: 'array',
210
- itemType: this.resolveType(subType)
211
- };
212
- return arrayType;
213
- } else if (typeNode.type === 'moduleModel') {
214
- let tmp = [];
215
- typeNode.path.forEach(item => {
216
- tmp.push(item.lexeme);
217
- });
218
- typeInfo['objectType'] = 'model';
219
- typeInfo['lexeme'] = this.combinator.addModelInclude(tmp.join('.'));
220
- return typeInfo;
221
- } else if (typeNode.type === 'moduleTypedef') {
222
- let tmp = [];
223
- typeNode.path.forEach(item => {
224
- tmp.push(item.lexeme);
225
- });
226
- typeInfo['lexeme'] = this.combinator.addTypedefInclude(tmp.join('.'));
227
- return typeInfo;
228
- } else if (typeNode.type === 'subModel') {
229
- let tmp = [];
230
- typeNode.path.forEach(item => {
231
- tmp.push(item.lexeme);
232
- });
233
- typeInfo['objectType'] = 'model';
234
- typeInfo['lexeme'] = this.combinator.addModelInclude(tmp.join('.'));
235
- return typeInfo;
236
- }
237
- debug.stack(typeNode, sourceNode);
238
- } else if (typeNode.lexeme) {
239
- return this.resolveType(typeNode.lexeme, sourceNode);
240
- } else if (typeNode === 'array') {
241
- let itemType;
242
- if (sourceNode.fieldItemType.type === 'modelBody') {
243
- typeInfo['objectType'] = 'model';
244
- typeInfo['lexeme'] = this.combinator.addModelInclude(sourceNode.itemType ? sourceNode.itemType : prop);
245
- itemType = typeInfo;
246
- } else if (sourceNode.fieldItemType.idType === 'model') {
247
- typeInfo['objectType'] = 'model';
248
- typeInfo['lexeme'] = this.combinator.addModelInclude(sourceNode.fieldItemType.lexeme);
249
- itemType = typeInfo;
250
- } else {
251
- itemType = this.resolveType(sourceNode.fieldItemType, sourceNode, sourceNode.itemType ? sourceNode.itemType : prop);
252
- }
253
- const arrayType = {
254
- lexeme: 'array',
255
- objectType: 'array',
256
- itemType: itemType
257
- };
258
- return arrayType;
259
- } else if (typeNode === 'map') {
260
- const keyType = this.resolveType(sourceNode.keyType);
261
- const valType = this.resolveType(sourceNode.valueType);
262
- const mapType = {
263
- lexeme: 'map',
264
- objectType: 'map',
265
- keyType: keyType,
266
- valType: valType
267
- };
268
- return mapType;
269
- } else if (typeNode) {
270
- return typeNode;
271
- }
272
-
273
- if (typeof typeNode === 'string' && typeNode.length > 0) {
274
- typeInfo['objectType'] = 'model';
275
- typeInfo['lexeme'] = this.combinator.addModelInclude(typeNode);
276
- return typeInfo;
277
- }
278
- debug.stack('Unsupported type node', { typeNode, sourceNode });
279
- }
280
- }
281
-
282
- module.exports = BaseResolver;