@idlizer/core 2.1.2 → 2.1.5

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 (79) hide show
  1. package/build/lib/src/LanguageWriters/ArgConvertors.d.ts +3 -3
  2. package/build/lib/src/LanguageWriters/ArgConvertors.js +36 -30
  3. package/build/lib/src/LanguageWriters/LanguageWriter.d.ts +26 -13
  4. package/build/lib/src/LanguageWriters/LanguageWriter.js +26 -52
  5. package/build/lib/src/LanguageWriters/convertors/CJConvertors.d.ts +2 -1
  6. package/build/lib/src/LanguageWriters/convertors/CJConvertors.js +16 -5
  7. package/build/lib/src/LanguageWriters/convertors/CppConvertors.d.ts +4 -2
  8. package/build/lib/src/LanguageWriters/convertors/CppConvertors.js +20 -12
  9. package/build/lib/src/LanguageWriters/convertors/ETSConvertors.js +7 -17
  10. package/build/lib/src/LanguageWriters/convertors/InteropConvertors.d.ts +4 -2
  11. package/build/lib/src/LanguageWriters/convertors/InteropConvertors.js +9 -3
  12. package/build/lib/src/LanguageWriters/convertors/JavaConvertors.d.ts +2 -1
  13. package/build/lib/src/LanguageWriters/convertors/JavaConvertors.js +24 -4
  14. package/build/lib/src/LanguageWriters/convertors/TSConvertors.d.ts +4 -2
  15. package/build/lib/src/LanguageWriters/convertors/TSConvertors.js +47 -28
  16. package/build/lib/src/LanguageWriters/nameConvertor.d.ts +3 -1
  17. package/build/lib/src/LanguageWriters/nameConvertor.js +5 -1
  18. package/build/lib/src/LanguageWriters/writers/CJLanguageWriter.d.ts +12 -9
  19. package/build/lib/src/LanguageWriters/writers/CJLanguageWriter.js +10 -37
  20. package/build/lib/src/LanguageWriters/writers/CLikeLanguageWriter.d.ts +4 -1
  21. package/build/lib/src/LanguageWriters/writers/CLikeLanguageWriter.js +1 -1
  22. package/build/lib/src/LanguageWriters/writers/CppLanguageWriter.d.ts +9 -9
  23. package/build/lib/src/LanguageWriters/writers/CppLanguageWriter.js +15 -24
  24. package/build/lib/src/LanguageWriters/writers/ETSLanguageWriter.d.ts +14 -8
  25. package/build/lib/src/LanguageWriters/writers/ETSLanguageWriter.js +18 -18
  26. package/build/lib/src/LanguageWriters/writers/JavaLanguageWriter.d.ts +4 -6
  27. package/build/lib/src/LanguageWriters/writers/JavaLanguageWriter.js +15 -14
  28. package/build/lib/src/LanguageWriters/writers/TsLanguageWriter.d.ts +7 -5
  29. package/build/lib/src/LanguageWriters/writers/TsLanguageWriter.js +22 -43
  30. package/build/lib/src/LibraryInterface.d.ts +1 -4
  31. package/build/lib/src/config.d.ts +206 -66
  32. package/build/lib/src/config.js +7 -3
  33. package/build/lib/src/configDescriber.d.ts +30 -3
  34. package/build/lib/src/configDescriber.js +99 -1
  35. package/build/lib/src/from-idl/DtsPrinter.js +30 -18
  36. package/build/lib/src/from-idl/IDLLinter.d.ts +40 -7
  37. package/build/lib/src/from-idl/IDLLinter.js +211 -25
  38. package/build/lib/src/from-idl/common.js +1 -1
  39. package/build/lib/src/from-idl/deserialize.d.ts +4 -1
  40. package/build/lib/src/from-idl/deserialize.js +434 -346
  41. package/build/lib/src/idl.d.ts +25 -17
  42. package/build/lib/src/idl.js +363 -109
  43. package/build/lib/src/idlize.d.ts +2 -1
  44. package/build/lib/src/idlize.js +82 -26
  45. package/build/lib/src/index.d.ts +4 -3
  46. package/build/lib/src/index.js +3 -2
  47. package/build/lib/src/options.d.ts +1 -1
  48. package/build/lib/src/peer-generation/BuilderClass.d.ts +0 -2
  49. package/build/lib/src/peer-generation/BuilderClass.js +0 -8
  50. package/build/lib/src/peer-generation/LayoutManager.d.ts +10 -3
  51. package/build/lib/src/peer-generation/LayoutManager.js +3 -2
  52. package/build/lib/src/peer-generation/Materialized.d.ts +1 -1
  53. package/build/lib/src/peer-generation/Materialized.js +2 -2
  54. package/build/lib/src/peer-generation/PeerClass.d.ts +3 -8
  55. package/build/lib/src/peer-generation/PeerClass.js +0 -1
  56. package/build/lib/src/peer-generation/PeerFile.d.ts +1 -2
  57. package/build/lib/src/peer-generation/PeerFile.js +1 -1
  58. package/build/lib/src/peer-generation/PeerLibrary.d.ts +12 -7
  59. package/build/lib/src/peer-generation/PeerLibrary.js +129 -65
  60. package/build/lib/src/peer-generation/ReferenceResolver.d.ts +1 -1
  61. package/build/lib/src/peer-generation/ReferenceResolver.js +2 -2
  62. package/build/lib/src/peer-generation/idl/IdlNameConvertor.d.ts +1 -0
  63. package/build/lib/src/peer-generation/idl/IdlNameConvertor.js +6 -2
  64. package/build/lib/src/peer-generation/idl/common.d.ts +2 -1
  65. package/build/lib/src/peer-generation/idl/common.js +13 -2
  66. package/build/lib/src/peer-generation/isMaterialized.js +25 -8
  67. package/build/lib/src/peer-generation/unions.d.ts +3 -2
  68. package/build/lib/src/peer-generation/unions.js +6 -2
  69. package/build/lib/src/resolveNamedNode.d.ts +3 -0
  70. package/build/lib/src/resolveNamedNode.js +105 -0
  71. package/build/lib/src/util.d.ts +6 -0
  72. package/build/lib/src/util.js +33 -0
  73. package/build/lib/src/visitor.d.ts +0 -1
  74. package/build/lib/src/visitor.js +1 -7
  75. package/package.json +2 -2
  76. package/webidl2.js/LICENSE +21 -0
  77. package/webidl2.js/README.md +827 -0
  78. package/webidl2.js/dist/package.json +3 -0
  79. package/webidl2.js/dist/webidl2.js +35 -7
@@ -18,385 +18,471 @@ import { isAttribute, isCallback, isClass, isConstant, isConstructor, isDictiona
18
18
  import { toString } from "./toString";
19
19
  import * as idl from "../idl";
20
20
  import { isDefined, warn } from "../util";
21
- import { generateSyntheticUnionName } from "../peer-generation/idl/common";
21
+ import { collapseTypes, generateSyntheticUnionName } from "../peer-generation/idl/common";
22
+ function getTokens(node) {
23
+ return node.tokens;
24
+ }
22
25
  const syntheticTypes = new Map();
23
26
  export function addSyntheticType(name, type) {
24
- if (syntheticTypes.has(name))
25
- warn(`duplicate synthetic type name "${name}"`); ///throw?
27
+ if (syntheticTypes.has(name)) {
28
+ warn(`duplicate synthetic type name "${name}"`);
29
+ }
26
30
  syntheticTypes.set(name, type);
27
31
  } // check
28
32
  export function resolveSyntheticType(type) {
29
33
  return syntheticTypes.get(type.name);
30
34
  }
31
- function toIDLNode(file, node) {
32
- return toIDLNodeForward(file, node);
33
- }
34
- function toIDLNodeForward(file, node) {
35
- if (isEnum(node)) {
36
- return toIDLEnum(file, node);
35
+ class IDLDeserializer {
36
+ constructor(info) {
37
+ this.info = info;
38
+ this.namespacePathNames = [];
39
+ this.currentPackage = [];
40
+ this.genericsScopes = [];
37
41
  }
38
- if (isImport(node)) {
39
- return toIDLImport(node);
42
+ ///
43
+ withInfo(from, result) {
44
+ this.info.set(result, getTokens(from));
45
+ return result;
40
46
  }
41
- if (isClass(node)) {
42
- return toIDLInterface(file, node);
47
+ setPackage(pkg) {
48
+ this.currentPackage = pkg;
43
49
  }
44
- if (isInterface(node)) {
45
- return toIDLInterface(file, node);
50
+ ///
51
+ sanitizeTypeParameter(param) {
52
+ const extendsIdx = param.indexOf('extends');
53
+ if (extendsIdx !== -1) {
54
+ return param.substring(0, extendsIdx).trim();
55
+ }
56
+ const eqIdx = param.indexOf('=');
57
+ if (eqIdx !== -1) {
58
+ return param.substring(0, eqIdx).trim();
59
+ }
60
+ return param;
46
61
  }
47
- if (isCallback(node)) {
48
- return toIDLCallback(file, node);
62
+ extractGenerics(extAttrs) {
63
+ var _a, _b;
64
+ return new Set((_b = (_a = this.findExtendedAttribute(extAttrs, idl.IDLExtendedAttributes.TypeParameters)) === null || _a === void 0 ? void 0 : _a.split(",")) === null || _b === void 0 ? void 0 : _b.map(it => this.sanitizeTypeParameter(it)));
49
65
  }
50
- if (isTypedef(node)) {
51
- return toIDLTypedef(file, node);
66
+ ///
67
+ toIDLNode(file, node) {
68
+ return this.toIDLNodeForward(file, node);
52
69
  }
53
- if (isDictionary(node)) {
54
- return toIDLDictionary(file, node);
70
+ toIDLNodeForward(file, node) {
71
+ if (isEnum(node)) {
72
+ return this.toIDLEnum(file, node);
73
+ }
74
+ if (this.isImport(node)) {
75
+ return this.toIDLImport(node);
76
+ }
77
+ if (isClass(node)) {
78
+ return this.toIDLInterface(file, node);
79
+ }
80
+ if (isInterface(node)) {
81
+ return this.toIDLInterface(file, node);
82
+ }
83
+ if (isCallback(node)) {
84
+ return this.toIDLCallback(file, node);
85
+ }
86
+ if (isTypedef(node)) {
87
+ return this.toIDLTypedef(file, node);
88
+ }
89
+ if (isDictionary(node)) {
90
+ return this.toIDLDictionary(file, node);
91
+ }
92
+ if (this.isNamespace(node)) {
93
+ return this.toIDLNamespace(file, node);
94
+ }
95
+ if (this.isVersion(node)) {
96
+ return this.toIDLVersion(file, node);
97
+ }
98
+ if (isAttribute(node)) {
99
+ return this.toIDLProperty(file, node);
100
+ }
101
+ if (isOperation(node)) {
102
+ return this.toIDLMethod(file, node, true);
103
+ }
104
+ if (isConstant(node)) {
105
+ return this.toIDLConstant(file, node);
106
+ }
107
+ throw new Error(`unexpected node type: ${toString(node)}`);
55
108
  }
56
- if (isNamespace(node)) {
57
- return toIDLNamespace(file, node);
109
+ toIDLImport(node) {
110
+ // console.log(node)
111
+ return this.withInfo(node, idl.createImport(node.clause.split("."), node.alias || undefined));
58
112
  }
59
- if (isVersion(node)) {
60
- return toIDLVersion(file, node);
113
+ interfaceSubkind(node) {
114
+ var _a, _b;
115
+ const nodeIDLEntity = (_b = (_a = node.extAttrs.find(it => it.name === "Entity")) === null || _a === void 0 ? void 0 : _a.rhs) === null || _b === void 0 ? void 0 : _b.value;
116
+ if (nodeIDLEntity == idl.IDLEntity.Class)
117
+ return idl.IDLInterfaceSubkind.Class;
118
+ if (nodeIDLEntity == idl.IDLEntity.Interface)
119
+ return idl.IDLInterfaceSubkind.Interface;
120
+ if (nodeIDLEntity == idl.IDLEntity.Tuple)
121
+ return idl.IDLInterfaceSubkind.Tuple;
122
+ return idl.IDLInterfaceSubkind.Interface;
61
123
  }
62
- if (isAttribute(node)) {
63
- return toIDLProperty(file, node);
124
+ toIDLInterface(file, node) {
125
+ var _a;
126
+ const generics = this.extractGenerics(node.extAttrs);
127
+ this.genericsScopes.push(generics);
128
+ const result = idl.createInterface(node.name, this.interfaceSubkind(node), (() => {
129
+ var _a, _b;
130
+ if (!node.inheritance)
131
+ return [];
132
+ const parentTypeArgs = this.extractTypeArguments(file, (_a = node.inheritanceExtAttrs) !== null && _a !== void 0 ? _a : [], idl.IDLExtendedAttributes.TypeArguments);
133
+ const parentType = idl.createReferenceType(node.inheritance, parentTypeArgs);
134
+ parentType.fileName = file;
135
+ if (node.inheritanceExtAttrs)
136
+ parentType.extendedAttributes = (_b = this.toExtendedAttributes(node.inheritanceExtAttrs)) === null || _b === void 0 ? void 0 : _b.filter(it => it.name !== idl.IDLExtendedAttributes.TypeArguments);
137
+ return [parentType];
138
+ })(), node.members
139
+ .filter(isConstructor)
140
+ .map(it => this.toIDLConstructor(file, it)), [], node.members
141
+ .filter(isAttribute)
142
+ .map(it => this.toIDLProperty(file, it)), node.members
143
+ .filter(isOperation)
144
+ .filter(it => !this.isCallable(it))
145
+ .map(it => this.toIDLMethod(file, it, false)), node.members
146
+ .filter(isOperation)
147
+ .filter(it => this.isCallable(it))
148
+ .map(it => this.toIDLCallable(file, it)), (_a = this.findExtendedAttribute(node.extAttrs, idl.IDLExtendedAttributes.TypeParameters)) === null || _a === void 0 ? void 0 : _a.split(","), {
149
+ fileName: file,
150
+ documentation: this.makeDocs(node),
151
+ extendedAttributes: this.toExtendedAttributes(node.extAttrs),
152
+ });
153
+ this.genericsScopes.pop();
154
+ this.info.set(result, getTokens(node));
155
+ if (node.extAttrs.find(it => it.name === "Synthetic")) {
156
+ const fqName = this.currentPackage.concat(this.namespacePathNames).concat([node.name]).join('.');
157
+ addSyntheticType(fqName, result);
158
+ }
159
+ return result;
64
160
  }
65
- if (isOperation(node)) {
66
- return toIDLMethod(file, node);
161
+ toIDLType(file, type, extAttrs) {
162
+ var _a;
163
+ if (typeof type === "string") {
164
+ // is it IDLStringType?
165
+ const refType = idl.createReferenceType(type);
166
+ refType.fileName = file;
167
+ refType.typeArguments = this.extractTypeArguments(file, extAttrs, idl.IDLExtendedAttributes.TypeArguments);
168
+ return refType;
169
+ }
170
+ if (type.nullable) {
171
+ return this.withInfo(type, idl.createOptionalType(this.toIDLType(file, Object.assign(Object.assign({}, type), { nullable: false }), extAttrs)));
172
+ }
173
+ if (isUnionTypeDescription(type)) {
174
+ let types = type.idlType
175
+ .map(it => this.toIDLType(file, it, undefined))
176
+ .filter(isDefined);
177
+ if (types.includes(idl.IDLUndefinedType)) {
178
+ types = types.filter(it => it !== idl.IDLUndefinedType);
179
+ return this.withInfo(type, idl.createOptionalType(collapseTypes(types)));
180
+ }
181
+ const name = generateSyntheticUnionName(types);
182
+ return this.withInfo(type, idl.createUnionType(types, name));
183
+ }
184
+ if (isSingleTypeDescription(type)) {
185
+ // must match with primitive types in idl.ts
186
+ switch (type.idlType) {
187
+ case idl.IDLPointerType.name: return idl.IDLPointerType;
188
+ case idl.IDLVoidType.name: return idl.IDLVoidType;
189
+ case idl.IDLBooleanType.name: return idl.IDLBooleanType;
190
+ case idl.IDLObjectType.name: return idl.IDLObjectType;
191
+ case idl.IDLI8Type.name: return idl.IDLI8Type;
192
+ case idl.IDLU8Type.name: return idl.IDLU8Type;
193
+ case idl.IDLI16Type.name: return idl.IDLI16Type;
194
+ case idl.IDLU16Type.name: return idl.IDLU16Type;
195
+ case idl.IDLI32Type.name: return idl.IDLI32Type;
196
+ case idl.IDLU32Type.name: return idl.IDLU32Type;
197
+ case idl.IDLI64Type.name: return idl.IDLI64Type;
198
+ case idl.IDLU64Type.name: return idl.IDLU64Type;
199
+ case idl.IDLF32Type.name: return idl.IDLF32Type;
200
+ case idl.IDLF64Type.name: return idl.IDLF64Type;
201
+ case idl.IDLBigintType.name: return idl.IDLBigintType;
202
+ case idl.IDLNumberType.name: return idl.IDLNumberType;
203
+ case idl.IDLStringType.name: return idl.IDLStringType;
204
+ case idl.IDLAnyType.name: return idl.IDLAnyType;
205
+ case idl.IDLUndefinedType.name: return idl.IDLUndefinedType;
206
+ case idl.IDLUnknownType.name: return idl.IDLUnknownType;
207
+ case idl.IDLObjectType.name: return idl.IDLObjectType;
208
+ case idl.IDLThisType.name: return idl.IDLThisType;
209
+ case idl.IDLDate.name: return idl.IDLDate;
210
+ case idl.IDLBufferType.name: return idl.IDLBufferType;
211
+ case idl.IDLSerializerBuffer.name: return idl.IDLSerializerBuffer;
212
+ }
213
+ const combinedExtAttrs = ((_a = type.extAttrs) !== null && _a !== void 0 ? _a : []).concat(extAttrs !== null && extAttrs !== void 0 ? extAttrs : []);
214
+ let idlRefType;
215
+ if (this.genericsScopes.some(it => it.has(type.idlType))) {
216
+ idlRefType = idl.createTypeParameterReference(type.idlType);
217
+ }
218
+ else {
219
+ const ref = idl.createReferenceType(type.idlType);
220
+ ref.typeArguments = this.extractTypeArguments(file, combinedExtAttrs, idl.IDLExtendedAttributes.TypeArguments);
221
+ idlRefType = ref;
222
+ }
223
+ idlRefType.fileName = file;
224
+ idlRefType.extendedAttributes = this.toExtendedAttributes(combinedExtAttrs);
225
+ return this.withInfo(type, idlRefType);
226
+ }
227
+ if (isSequenceTypeDescription(type) || isPromiseTypeDescription(type) || isRecordTypeDescription(type)) {
228
+ return this.withInfo(type, idl.createContainerType(type.generic, type.idlType.map(it => this.toIDLType(file, it, undefined))));
229
+ }
230
+ if (isUnspecifiedGenericTypeDescription(type)) {
231
+ return this.withInfo(type, idl.createUnspecifiedGenericType(type.generic, type.idlType.map(it => this.toIDLType(file, it, undefined))));
232
+ }
233
+ throw new Error(`unexpected type: ${toString(type)}`);
67
234
  }
68
- if (isConstant(node)) {
69
- return toIDLConstant(file, node);
235
+ toIDLCallable(file, node) {
236
+ var _a, _b;
237
+ if (!node.idlType) {
238
+ throw new Error(`method with no type ${toString(node)}`);
239
+ }
240
+ const generics = this.extractGenerics(node.extAttrs);
241
+ this.genericsScopes.push(generics);
242
+ const returnType = this.toIDLType(file, node.idlType, node.extAttrs);
243
+ if (idl.isReferenceType(returnType)) {
244
+ const returnTypeArgs = this.extractTypeArguments(file, node.extAttrs, idl.IDLExtendedAttributes.TypeArguments);
245
+ returnType.typeArguments = returnTypeArgs;
246
+ }
247
+ const result = this.withInfo(node, idl.createCallable((_a = node.name) !== null && _a !== void 0 ? _a : "", node.arguments.map(it => this.toIDLParameter(file, it)), returnType, {
248
+ isStatic: node.special === "static",
249
+ isAsync: node.async,
250
+ }, {
251
+ documentation: this.makeDocs(node),
252
+ extendedAttributes: this.toExtendedAttributes(node.extAttrs),
253
+ }, (_b = this.findExtendedAttribute(node.extAttrs, idl.IDLExtendedAttributes.TypeParameters)) === null || _b === void 0 ? void 0 : _b.split(",")));
254
+ this.genericsScopes.pop();
255
+ return result;
70
256
  }
71
- throw new Error(`unexpected node type: ${toString(node)}`);
72
- }
73
- function isNamespace(node) {
74
- return node.type === 'namespace';
75
- }
76
- function isVersion(node) {
77
- return node.type === 'version';
78
- }
79
- function isPackage(node) {
80
- return node.type === 'package';
81
- }
82
- function isImport(node) {
83
- return node.type === 'import';
84
- }
85
- function isCallable(node) {
86
- return node.extAttrs.some(it => it.name == "Invoke");
87
- }
88
- function toIDLImport(node) {
89
- // console.log(node)
90
- return idl.createImport(node.clause.split("."), node.alias || undefined);
91
- }
92
- function interfaceSubkind(node) {
93
- var _a, _b;
94
- const nodeIDLEntity = (_b = (_a = node.extAttrs.find(it => it.name === "Entity")) === null || _a === void 0 ? void 0 : _a.rhs) === null || _b === void 0 ? void 0 : _b.value;
95
- if (nodeIDLEntity == idl.IDLEntity.Class)
96
- return idl.IDLInterfaceSubkind.Class;
97
- if (nodeIDLEntity == idl.IDLEntity.Interface)
98
- return idl.IDLInterfaceSubkind.Interface;
99
- if (nodeIDLEntity == idl.IDLEntity.Tuple)
100
- return idl.IDLInterfaceSubkind.Tuple;
101
- return idl.IDLInterfaceSubkind.Interface;
102
- }
103
- function toIDLInterface(file, node) {
104
- var _a;
105
- const result = idl.createInterface(node.name, interfaceSubkind(node), (() => {
257
+ toIDLMethod(file, node, isFree = false) {
106
258
  var _a, _b;
107
- if (!node.inheritance)
108
- return [];
109
- const parentTypeArgs = extractTypeArguments(file, (_a = node.inheritanceExtAttrs) !== null && _a !== void 0 ? _a : [], idl.IDLExtendedAttributes.TypeArguments);
110
- const parentType = idl.createReferenceType(node.inheritance, parentTypeArgs);
111
- parentType.fileName = file;
112
- if (node.inheritanceExtAttrs)
113
- parentType.extendedAttributes = (_b = toExtendedAttributes(node.inheritanceExtAttrs)) === null || _b === void 0 ? void 0 : _b.filter(it => it.name !== idl.IDLExtendedAttributes.TypeArguments);
114
- return [parentType];
115
- })(), node.members
116
- .filter(isConstructor)
117
- .map(it => toIDLConstructor(file, it)), [], node.members
118
- .filter(isAttribute)
119
- .map(it => toIDLProperty(file, it)), node.members
120
- .filter(isOperation)
121
- .filter(it => !isCallable(it))
122
- .map(it => toIDLMethod(file, it)), node.members
123
- .filter(isOperation)
124
- .filter(it => isCallable(it))
125
- .map(it => toIDLCallable(file, it)), (_a = findExtendedAttribute(node.extAttrs, idl.IDLExtendedAttributes.TypeParameters)) === null || _a === void 0 ? void 0 : _a.split(","), {
126
- fileName: file,
127
- documentation: makeDocs(node),
128
- extendedAttributes: toExtendedAttributes(node.extAttrs),
129
- });
130
- if (node.extAttrs.find(it => it.name === "Synthetic"))
131
- addSyntheticType(node.name, result);
132
- return result;
133
- }
134
- function extractTypeArguments(file, extAttrs, attribute) {
135
- var _a;
136
- const attr = extAttrs === null || extAttrs === void 0 ? void 0 : extAttrs.find(it => it.name === attribute);
137
- if (!attr)
138
- return undefined;
139
- let value = toExtendedAttributeValue(attr);
140
- return (_a = value === null || value === void 0 ? void 0 : value.split(",") // TODO need real parsing here. What about "<T, Map<K, Callback<K,R>>, U>"
141
- ) === null || _a === void 0 ? void 0 : _a.map(it => toIDLType(file, it));
142
- }
143
- function toIDLType(file, type, extAttrs) {
144
- var _a;
145
- if (typeof type === "string") {
146
- // is it IDLStringType?
147
- const refType = idl.createReferenceType(type);
148
- refType.fileName = file;
149
- refType.typeArguments = extractTypeArguments(file, extAttrs, idl.IDLExtendedAttributes.TypeArguments);
150
- return refType;
151
- }
152
- if (type.nullable) {
153
- return idl.createOptionalType(toIDLType(file, Object.assign(Object.assign({}, type), { nullable: false }), extAttrs));
154
- }
155
- if (isUnionTypeDescription(type)) {
156
- const types = type.idlType
157
- .map(it => toIDLType(file, it))
158
- .filter(isDefined);
159
- const name = generateSyntheticUnionName(types);
160
- return idl.createUnionType(types, name);
161
- }
162
- if (isSingleTypeDescription(type)) {
163
- switch (type.idlType) {
164
- case idl.IDLUnknownType.name: return idl.IDLUnknownType;
165
- case idl.IDLObjectType.name: return idl.IDLObjectType;
166
- case idl.IDLAnyType.name: return idl.IDLAnyType;
167
- case idl.IDLBooleanType.name: return idl.IDLBooleanType;
168
- case idl.IDLNumberType.name: return idl.IDLNumberType;
169
- case idl.IDLStringType.name: return idl.IDLStringType;
170
- case idl.IDLUndefinedType.name: return idl.IDLUndefinedType;
171
- case idl.IDLVoidType.name: return idl.IDLVoidType;
172
- case idl.IDLI8Type.name: return idl.IDLI8Type;
173
- case idl.IDLU8Type.name: return idl.IDLU8Type;
174
- case idl.IDLI16Type.name: return idl.IDLI16Type;
175
- case idl.IDLU16Type.name: return idl.IDLU16Type;
176
- case idl.IDLI32Type.name: return idl.IDLI32Type;
177
- case idl.IDLU32Type.name: return idl.IDLU32Type;
178
- case idl.IDLI64Type.name: return idl.IDLI64Type;
179
- case idl.IDLU64Type.name: return idl.IDLU64Type;
180
- case idl.IDLF32Type.name: return idl.IDLF32Type;
181
- case idl.IDLF64Type.name: return idl.IDLF64Type;
182
- case idl.IDLPointerType.name: return idl.IDLPointerType;
183
- case idl.IDLBufferType.name: return idl.IDLBufferType;
259
+ if (!node.idlType) {
260
+ throw new Error(`method with no type ${toString(node)}`);
184
261
  }
185
- const combinedExtAttrs = ((_a = type.extAttrs) !== null && _a !== void 0 ? _a : []).concat(extAttrs !== null && extAttrs !== void 0 ? extAttrs : []);
186
- const idlRefType = idl.createReferenceType(type.idlType);
187
- idlRefType.fileName = file;
188
- idlRefType.typeArguments = extractTypeArguments(file, combinedExtAttrs, idl.IDLExtendedAttributes.TypeArguments);
189
- idlRefType.extendedAttributes = toExtendedAttributes(combinedExtAttrs);
190
- return idlRefType;
262
+ const generics = this.extractGenerics(node.extAttrs);
263
+ this.genericsScopes.push(generics);
264
+ const returnType = this.toIDLType(file, node.idlType, node.extAttrs);
265
+ if (idl.isReferenceType(returnType))
266
+ returnType.typeArguments = this.extractTypeArguments(file, node.extAttrs, idl.IDLExtendedAttributes.TypeArguments);
267
+ const result = this.withInfo(node, idl.createMethod((_a = node.name) !== null && _a !== void 0 ? _a : "", node.arguments.map(it => this.toIDLParameter(file, it !== null && it !== void 0 ? it : new Map())), returnType, {
268
+ isStatic: node.special === "static",
269
+ isAsync: node.async,
270
+ isOptional: isOptional(node),
271
+ isFree
272
+ }, {
273
+ documentation: this.makeDocs(node),
274
+ extendedAttributes: this.toExtendedAttributes(node.extAttrs),
275
+ }, (_b = this.findExtendedAttribute(node.extAttrs, idl.IDLExtendedAttributes.TypeParameters)) === null || _b === void 0 ? void 0 : _b.split(",")));
276
+ this.genericsScopes.pop();
277
+ return result;
191
278
  }
192
- if (isSequenceTypeDescription(type) || isPromiseTypeDescription(type) || isRecordTypeDescription(type)) {
193
- return idl.createContainerType(type.generic, type.idlType.map(it => toIDLType(file, it)));
279
+ toIDLConstructor(file, node) {
280
+ return this.withInfo(node, idl.createConstructor(node.arguments.map(it => this.toIDLParameter(file, it)), undefined, {
281
+ documentation: this.makeDocs(node),
282
+ }));
194
283
  }
195
- if (isUnspecifiedGenericTypeDescription(type)) {
196
- return idl.createUnspecifiedGenericType(type.generic, type.idlType.map(it => toIDLType(file, it)));
284
+ toIDLParameter(file, node) {
285
+ return this.withInfo(node, idl.createParameter(node.name, this.toIDLType(file, node.idlType, node.extAttrs), node.optional, node.variadic, {
286
+ fileName: file,
287
+ }));
197
288
  }
198
- throw new Error(`unexpected type: ${toString(type)}`);
199
- }
200
- function toIDLCallable(file, node) {
201
- var _a, _b;
202
- if (!node.idlType) {
203
- throw new Error(`method with no type ${toString(node)}`);
204
- }
205
- const returnType = toIDLType(file, node.idlType, node.extAttrs);
206
- if (idl.isReferenceType(returnType)) {
207
- const returnTypeArgs = extractTypeArguments(file, node.extAttrs, idl.IDLExtendedAttributes.TypeArguments);
208
- returnType.typeArguments = returnTypeArgs;
209
- }
210
- return idl.createCallable((_a = node.name) !== null && _a !== void 0 ? _a : "", node.arguments.map(it => toIDLParameter(file, it)), returnType, {
211
- isStatic: node.special === "static",
212
- isAsync: node.async,
213
- }, {
214
- documentation: makeDocs(node),
215
- extendedAttributes: toExtendedAttributes(node.extAttrs),
216
- }, (_b = findExtendedAttribute(node.extAttrs, idl.IDLExtendedAttributes.TypeParameters)) === null || _b === void 0 ? void 0 : _b.split(","));
217
- }
218
- function toIDLMethod(file, node) {
219
- var _a, _b;
220
- if (!node.idlType) {
221
- throw new Error(`method with no type ${toString(node)}`);
222
- }
223
- const returnType = toIDLType(file, node.idlType, node.extAttrs);
224
- if (idl.isReferenceType(returnType))
225
- returnType.typeArguments = extractTypeArguments(file, node.extAttrs, idl.IDLExtendedAttributes.TypeArguments);
226
- return idl.createMethod((_a = node.name) !== null && _a !== void 0 ? _a : "", node.arguments.map(it => toIDLParameter(file, it)), returnType, {
227
- isStatic: node.special === "static",
228
- isAsync: node.async,
229
- isOptional: isOptional(node),
230
- isFree: false, // TODO: namespace-related-to-rework
231
- }, {
232
- documentation: makeDocs(node),
233
- extendedAttributes: toExtendedAttributes(node.extAttrs),
234
- }, (_b = findExtendedAttribute(node.extAttrs, idl.IDLExtendedAttributes.TypeParameters)) === null || _b === void 0 ? void 0 : _b.split(","));
235
- }
236
- function toIDLConstructor(file, node) {
237
- return idl.createConstructor(node.arguments.map(it => toIDLParameter(file, it)), undefined, {
238
- documentation: makeDocs(node),
239
- });
240
- }
241
- function toIDLParameter(file, node) {
242
- return idl.createParameter(node.name, toIDLType(file, node.idlType, node.extAttrs), node.optional, node.variadic, {
243
- fileName: file,
244
- });
245
- }
246
- function toIDLCallback(file, node) {
247
- const result = idl.createCallback(node.name, node.arguments.map(it => toIDLParameter(file, it)), toIDLType(file, node.idlType), {
248
- fileName: file,
249
- extendedAttributes: toExtendedAttributes(node.extAttrs),
250
- documentation: makeDocs(node),
251
- });
252
- if (node.extAttrs.find(it => it.name === "Synthetic"))
253
- addSyntheticType(node.name, result);
254
- return result;
255
- }
256
- function toIDLTypedef(file, node) {
257
- var _a;
258
- return idl.createTypedef(node.name, toIDLType(file, node.idlType), (_a = findExtendedAttribute(node.extAttrs, idl.IDLExtendedAttributes.TypeParameters)) === null || _a === void 0 ? void 0 : _a.split(","), {
259
- extendedAttributes: toExtendedAttributes(node.extAttrs),
260
- documentation: makeDocs(node),
261
- fileName: file,
262
- });
263
- }
264
- function toIDLConstant(file, node) {
265
- return idl.createConstant(node.name, toIDLType(file, node.idlType), constantValue(node));
266
- }
267
- function constantValue(node) {
268
- switch (node.value.type) {
269
- case "string":
270
- return `"${node.value.value}"`;
271
- case "number":
272
- return node.value.value;
273
- case "boolean":
274
- return node.value.value.toString();
275
- case "null":
276
- return "null";
277
- case "Infinity":
278
- return "Infinity";
279
- case "NaN":
280
- return "NaN";
281
- case "sequence":
282
- return `[${node.value.value.join(',')}]`;
283
- case "dictionary":
284
- return `new Map()`;
285
- default:
286
- return "undefined";
289
+ toIDLCallback(file, node) {
290
+ const result = idl.createCallback(node.name, node.arguments.map(it => this.toIDLParameter(file, it)), this.toIDLType(file, node.idlType, undefined), {
291
+ fileName: file,
292
+ extendedAttributes: this.toExtendedAttributes(node.extAttrs),
293
+ documentation: this.makeDocs(node),
294
+ });
295
+ if (node.extAttrs.find(it => it.name === "Synthetic")) {
296
+ const fqName = this.currentPackage.concat(this.namespacePathNames).concat([node.name]).join('.');
297
+ addSyntheticType(fqName, result);
298
+ }
299
+ return this.withInfo(node, result);
287
300
  }
288
- }
289
- function toIDLDictionary(file, node) {
290
- const result = idl.createEnum(node.name, [], {
291
- documentation: makeDocs(node),
292
- extendedAttributes: toExtendedAttributes(node.extAttrs),
293
- fileName: file,
294
- });
295
- result.elements = node.members.map(it => toIDLEnumMember(file, it, result));
296
- return result;
297
- }
298
- function toIDLNamespace(file, node) {
299
- const namespace = idl.createNamespace(node.name, toExtendedAttributes(node.extAttrs), file);
300
- namespace.members = node.members.map(it => toIDLNodeForward(file, it));
301
- return namespace;
302
- }
303
- function toIDLVersion(file, node) {
304
- return idl.createVersion(node.value, toExtendedAttributes(node.extAttrs), file);
305
- }
306
- function toIDLProperty(file, node) {
307
- return idl.createProperty(node.name, toIDLType(file, node.idlType), node.readonly, node.special === "static", isOptional(node), {
308
- documentation: makeDocs(node),
309
- fileName: file,
310
- extendedAttributes: toExtendedAttributes(node.extAttrs)
311
- });
312
- }
313
- function unescapeString(value) {
314
- if (!value.length || value[0] !== '"')
315
- return value;
316
- value = value.slice(1, -1);
317
- value = value.replace(/\\((['"\\bfnrtv])|([0-7]{1-3})|x([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/g, (_, all, c, oct, h2, u4) => {
318
- if (c !== undefined) {
319
- switch (c) {
320
- case "'": return "'";
321
- case '"': return '"';
322
- case "\\": return "\\";
323
- case "b": return "\b";
324
- case "f": return "\f";
325
- case "n": return "\n";
326
- case "r": return "\r";
327
- case "t": return "\t";
328
- case "v": return "\v";
329
- }
301
+ toIDLTypedef(file, node) {
302
+ var _a;
303
+ const generics = this.extractGenerics(node.extAttrs);
304
+ this.genericsScopes.push(generics);
305
+ const result = this.withInfo(node, idl.createTypedef(node.name, this.toIDLType(file, node.idlType, undefined), (_a = this.findExtendedAttribute(node.extAttrs, idl.IDLExtendedAttributes.TypeParameters)) === null || _a === void 0 ? void 0 : _a.split(","), {
306
+ extendedAttributes: this.toExtendedAttributes(node.extAttrs),
307
+ documentation: this.makeDocs(node),
308
+ fileName: file,
309
+ }));
310
+ this.genericsScopes.pop();
311
+ return result;
312
+ }
313
+ toIDLConstant(file, node) {
314
+ return this.withInfo(node, idl.createConstant(node.name, this.toIDLType(file, node.idlType, undefined), this.constantValue(node)));
315
+ }
316
+ toIDLDictionary(file, node) {
317
+ const result = idl.createEnum(node.name, [], {
318
+ documentation: this.makeDocs(node),
319
+ extendedAttributes: this.toExtendedAttributes(node.extAttrs),
320
+ fileName: file,
321
+ });
322
+ result.elements = node.members.map(it => this.toIDLEnumMember(file, it, result));
323
+ return this.withInfo(node, result);
324
+ }
325
+ toIDLNamespace(file, node) {
326
+ const namespace = idl.createNamespace(node.name, [], {
327
+ extendedAttributes: this.toExtendedAttributes(node.extAttrs),
328
+ fileName: file
329
+ });
330
+ this.namespacePathNames.push(node.name);
331
+ namespace.members = node.members.map(it => this.toIDLNodeForward(file, it));
332
+ this.namespacePathNames.pop();
333
+ return this.withInfo(node, namespace);
334
+ }
335
+ toIDLVersion(file, node) {
336
+ return this.withInfo(node, idl.createVersion(node.value, {
337
+ extendedAttributes: this.toExtendedAttributes(node.extAttrs),
338
+ fileName: file
339
+ }));
340
+ }
341
+ toIDLProperty(file, node) {
342
+ return this.withInfo(node, idl.createProperty(node.name, this.toIDLType(file, node.idlType, undefined), node.readonly, node.special === "static", isOptional(node), {
343
+ documentation: this.makeDocs(node),
344
+ fileName: file,
345
+ extendedAttributes: this.toExtendedAttributes(node.extAttrs)
346
+ }));
347
+ }
348
+ toIDLEnumMember(file, node, parent) {
349
+ var _a, _b, _c;
350
+ let initializer = undefined;
351
+ if (((_a = node.default) === null || _a === void 0 ? void 0 : _a.type) == "string") {
352
+ initializer = this.unescapeString(node.default.value);
330
353
  }
331
- else if (oct !== undefined) {
332
- return String.fromCharCode(parseInt(oct, 8));
354
+ else if (((_b = node.default) === null || _b === void 0 ? void 0 : _b.type) == "number") {
355
+ initializer = +((_c = node.default) === null || _c === void 0 ? void 0 : _c.value);
333
356
  }
334
- else if (h2 !== undefined) {
335
- return String.fromCharCode(parseInt(h2, 16));
357
+ else if (node.default == null) {
358
+ initializer = undefined;
336
359
  }
337
- else if (u4 !== undefined) {
338
- return String.fromCharCode(parseInt(u4, 16));
360
+ else {
361
+ throw new Error(`Not representable enum initializer: ${node.default}`);
339
362
  }
340
- throw new Error(`unknown escape sequence: ${_}`);
341
- });
342
- return value;
343
- }
344
- function toIDLEnumMember(file, node, parent) {
345
- var _a, _b, _c;
346
- let initializer = undefined;
347
- if (((_a = node.default) === null || _a === void 0 ? void 0 : _a.type) == "string") {
348
- initializer = unescapeString(node.default.value);
363
+ return this.withInfo(node, idl.createEnumMember(node.name, parent, this.toIDLType(file, node.idlType, undefined), initializer, {
364
+ extendedAttributes: this.toExtendedAttributes(node.extAttrs),
365
+ }));
349
366
  }
350
- else if (((_b = node.default) === null || _b === void 0 ? void 0 : _b.type) == "number") {
351
- initializer = +((_c = node.default) === null || _c === void 0 ? void 0 : _c.value);
367
+ toExtendedAttributes(extAttrs) {
368
+ return extAttrs.map(it => {
369
+ return this.withInfo(it, { name: it.name, value: this.toExtendedAttributeValue(it) });
370
+ });
352
371
  }
353
- else if (node.default == null) {
354
- initializer = undefined;
372
+ toExtendedAttributeValue(attr) {
373
+ var _a, _b;
374
+ // TODO: be smarter about RHS.
375
+ if (((_a = attr.rhs) === null || _a === void 0 ? void 0 : _a.value) instanceof Array)
376
+ return attr.rhs.value.map(v => v.value).join(",");
377
+ if (typeof ((_b = attr.rhs) === null || _b === void 0 ? void 0 : _b.value) === 'string')
378
+ return this.unescapeString(attr.rhs.value);
379
+ return;
355
380
  }
356
- else {
357
- throw new Error(`Not representable enum initializer: ${node.default}`);
381
+ toIDLEnum(file, node) {
382
+ const result = idl.createEnum(node.name, [], {
383
+ fileName: file,
384
+ documentation: this.makeDocs(node),
385
+ extendedAttributes: this.toExtendedAttributes(node.extAttrs),
386
+ });
387
+ result.elements = node.values.map((it) => idl.createEnumMember(it.value, result, idl.IDLNumberType, undefined));
388
+ return this.withInfo(node, result);
358
389
  }
359
- return idl.createEnumMember(node.name, parent, toIDLType(file, node.idlType), initializer, {
360
- extendedAttributes: toExtendedAttributes(node.extAttrs),
361
- });
362
- }
363
- function toExtendedAttributes(extAttrs) {
364
- return extAttrs.map(it => {
365
- return { name: it.name, value: toExtendedAttributeValue(it) };
366
- });
367
- }
368
- function toExtendedAttributeValue(attr) {
369
- var _a, _b;
370
- // TODO: be smarter about RHS.
371
- if (((_a = attr.rhs) === null || _a === void 0 ? void 0 : _a.value) instanceof Array)
372
- return attr.rhs.value.map(v => v.value).join(",");
373
- if (typeof ((_b = attr.rhs) === null || _b === void 0 ? void 0 : _b.value) === 'string')
374
- return unescapeString(attr.rhs.value);
375
- return;
376
- }
377
- function makeDocs(node) {
378
- let docs = undefined;
379
- node.extAttrs.forEach(it => {
390
+ ///
391
+ isNamespace(node) {
392
+ return node.type === 'namespace';
393
+ }
394
+ isVersion(node) {
395
+ return node.type === 'version';
396
+ }
397
+ isPackage(node) {
398
+ return node.type === 'package';
399
+ }
400
+ isImport(node) {
401
+ return node.type === 'import';
402
+ }
403
+ isCallable(node) {
404
+ return node.extAttrs.some(it => it.name == "Invoke");
405
+ }
406
+ ///
407
+ extractTypeArguments(file, extAttrs, attribute) {
380
408
  var _a;
381
- if (it.name == "Documentation")
382
- docs = (_a = it.rhs) === null || _a === void 0 ? void 0 : _a.value;
383
- });
384
- return docs;
385
- }
386
- function toIDLEnum(file, node) {
387
- const result = idl.createEnum(node.name, [], {
388
- fileName: file,
389
- documentation: makeDocs(node),
390
- extendedAttributes: toExtendedAttributes(node.extAttrs),
391
- });
392
- result.elements = node.values.map((it) => idl.createEnumMember(it.value, result, idl.IDLNumberType, undefined));
393
- return result;
394
- }
395
- function findExtendedAttribute(extAttrs, name) {
396
- const attr = extAttrs.find(it => it.name === name);
397
- return attr ? toExtendedAttributeValue(attr) : undefined;
409
+ const attr = extAttrs === null || extAttrs === void 0 ? void 0 : extAttrs.find(it => it.name === attribute);
410
+ if (!attr)
411
+ return undefined;
412
+ let value = this.toExtendedAttributeValue(attr);
413
+ return (_a = value === null || value === void 0 ? void 0 : value.split(",") // TODO need real parsing here. What about "<T, Map<K, Callback<K,R>>, U>"
414
+ ) === null || _a === void 0 ? void 0 : _a.map(it => { var _a; return this.toIDLType(file, (_a = webidl2.parseType(it, file)) !== null && _a !== void 0 ? _a : it); });
415
+ }
416
+ constantValue(node) {
417
+ switch (node.value.type) {
418
+ case "string":
419
+ return `"${node.value.value}"`;
420
+ case "number":
421
+ return node.value.value;
422
+ case "boolean":
423
+ return node.value.value.toString();
424
+ case "null":
425
+ return "null";
426
+ case "Infinity":
427
+ return "Infinity";
428
+ case "NaN":
429
+ return "NaN";
430
+ case "sequence":
431
+ return `[${node.value.value.join(',')}]`;
432
+ case "dictionary":
433
+ return `new Map()`;
434
+ default:
435
+ return "undefined";
436
+ }
437
+ }
438
+ unescapeString(value) {
439
+ if (!value.length || value[0] !== '"')
440
+ return value;
441
+ value = value.slice(1, -1);
442
+ value = value.replace(/\\((['"\\bfnrtv])|([0-7]{1-3})|x([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/g, (_, all, c, oct, h2, u4) => {
443
+ if (c !== undefined) {
444
+ switch (c) {
445
+ case "'": return "'";
446
+ case '"': return '"';
447
+ case "\\": return "\\";
448
+ case "b": return "\b";
449
+ case "f": return "\f";
450
+ case "n": return "\n";
451
+ case "r": return "\r";
452
+ case "t": return "\t";
453
+ case "v": return "\v";
454
+ }
455
+ }
456
+ else if (oct !== undefined) {
457
+ return String.fromCharCode(parseInt(oct, 8));
458
+ }
459
+ else if (h2 !== undefined) {
460
+ return String.fromCharCode(parseInt(h2, 16));
461
+ }
462
+ else if (u4 !== undefined) {
463
+ return String.fromCharCode(parseInt(u4, 16));
464
+ }
465
+ throw new Error(`unknown escape sequence: ${_}`);
466
+ });
467
+ return value;
468
+ }
469
+ makeDocs(node) {
470
+ let docs = undefined;
471
+ node.extAttrs.forEach(it => {
472
+ var _a;
473
+ if (it.name == "Documentation")
474
+ docs = (_a = it.rhs) === null || _a === void 0 ? void 0 : _a.value;
475
+ });
476
+ return docs;
477
+ }
478
+ findExtendedAttribute(extAttrs, name) {
479
+ const attr = extAttrs.find(it => it.name === name);
480
+ return attr ? this.toExtendedAttributeValue(attr) : undefined;
481
+ }
398
482
  }
399
483
  export function toIDLFile(fileName, content) {
484
+ const lexicalInfo = new Map();
485
+ const deserializer = new IDLDeserializer(lexicalInfo);
400
486
  if (undefined === content)
401
487
  content = fs.readFileSync(fileName).toString();
402
488
  let packageClause = [];
@@ -404,14 +490,16 @@ export function toIDLFile(fileName, content) {
404
490
  .filter(it => {
405
491
  if (!it.type)
406
492
  return false;
407
- if (isPackage(it)) {
493
+ if (deserializer.isPackage(it)) {
408
494
  packageClause = it.clause.split(".");
495
+ deserializer.setPackage(packageClause);
409
496
  return false;
410
497
  }
411
498
  return true;
412
499
  })
413
- .map(it => toIDLNode(fileName, it));
500
+ .map(it => deserializer.toIDLNode(fileName, it));
414
501
  const file = idl.createFile(entries, fileName, packageClause);
415
- return idl.linkParentBack(file);
502
+ file.text = content;
503
+ return [idl.linkParentBack(file), lexicalInfo];
416
504
  }
417
505
  //# sourceMappingURL=deserialize.js.map