@kontor/kontor-sdk 1.0.0-alpha.19 → 1.0.0-alpha.20
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/dist/src/exports/index.test.js +27 -0
- package/dist/src/exports/index.test.js.map +1 -0
- package/dist/src/sdk/accounts/private-key-to-account.js +42 -0
- package/dist/src/sdk/accounts/private-key-to-account.js.map +1 -0
- package/dist/src/sdk/actions/get-contract.js +50 -0
- package/dist/src/sdk/actions/get-contract.js.map +1 -0
- package/dist/src/sdk/actions/get-contract.test-d.js +43 -0
- package/dist/src/sdk/actions/get-contract.test-d.js.map +1 -0
- package/dist/src/sdk/actions/kontor/public/call-view.js +21 -0
- package/dist/src/sdk/actions/kontor/public/call-view.js.map +1 -0
- package/dist/src/sdk/actions/kontor/public/inspect.js +20 -0
- package/dist/src/sdk/actions/kontor/public/inspect.js.map +1 -0
- package/dist/src/sdk/actions/kontor/public/proc-contract.test.js +47 -0
- package/dist/src/sdk/actions/kontor/public/proc-contract.test.js.map +1 -0
- package/dist/src/sdk/actions/kontor/public/view-contract.js +47 -0
- package/dist/src/sdk/actions/kontor/public/view-contract.js.map +1 -0
- package/dist/src/sdk/actions/kontor/public/view-contract.test-d.js +29 -0
- package/dist/src/sdk/actions/kontor/public/view-contract.test-d.js.map +1 -0
- package/dist/src/sdk/actions/kontor/wallet/sign-reveal.js +59 -0
- package/dist/src/sdk/actions/kontor/wallet/sign-reveal.js.map +1 -0
- package/dist/src/sdk/actions/wallet/get-addresses.js +20 -0
- package/dist/src/sdk/actions/wallet/get-addresses.js.map +1 -0
- package/dist/src/sdk/actions/wallet/sign-psbt.js +28 -0
- package/dist/src/sdk/actions/wallet/sign-psbt.js.map +1 -0
- package/dist/src/sdk/clients/base/create-http-client.js +41 -0
- package/dist/src/sdk/clients/base/create-http-client.js.map +1 -0
- package/dist/src/sdk/clients/base/create-rpc-client.js +41 -0
- package/dist/src/sdk/clients/base/create-rpc-client.js.map +1 -0
- package/dist/src/sdk/clients/base/create-rpc-client.test.js +151 -0
- package/dist/src/sdk/clients/base/create-rpc-client.test.js.map +1 -0
- package/dist/src/sdk/clients/create-public-client.js +13 -0
- package/dist/src/sdk/clients/create-public-client.js.map +1 -0
- package/dist/src/sdk/clients/create-public-client.test.js +121 -0
- package/dist/src/sdk/clients/create-public-client.test.js.map +1 -0
- package/dist/src/sdk/clients/create-wallet-client.js +14 -0
- package/dist/src/sdk/clients/create-wallet-client.js.map +1 -0
- package/dist/src/sdk/clients/kontor/create-public-client.js +14 -0
- package/dist/src/sdk/clients/kontor/create-public-client.js.map +1 -0
- package/dist/src/sdk/clients/kontor/create-public-client.test.js +45 -0
- package/dist/src/sdk/clients/kontor/create-public-client.test.js.map +1 -0
- package/dist/src/sdk/clients/transports/create-rpc-transport.js +36 -0
- package/dist/src/sdk/clients/transports/create-rpc-transport.js.map +1 -0
- package/dist/src/sdk/contracts/wits.js +26 -0
- package/dist/src/sdk/contracts/wits.js.map +1 -0
- package/dist/src/sdk/types/chain.js +2 -0
- package/dist/src/sdk/types/chain.js.map +1 -0
- package/dist/src/sdk/types/contract.js +2 -0
- package/dist/src/sdk/types/contract.js.map +1 -0
- package/dist/src/sdk/types/contract.test-d.js +34 -0
- package/dist/src/sdk/types/contract.test-d.js.map +1 -0
- package/dist/src/sdk/types/http-schema.js +73 -0
- package/dist/src/sdk/types/http-schema.js.map +1 -0
- package/dist/src/sdk/types/rpc-schema.js +2 -0
- package/dist/src/sdk/types/rpc-schema.js.map +1 -0
- package/dist/src/sdk/utils/chain/defineChain.js +9 -0
- package/dist/src/sdk/utils/chain/defineChain.js.map +1 -0
- package/dist/src/sdk/utils/wit/codecs/u64.js +63 -0
- package/dist/src/sdk/utils/wit/codecs/u64.js.map +1 -0
- package/dist/src/sdk/utils/wit/decode-wit-parameter.js +289 -0
- package/dist/src/sdk/utils/wit/decode-wit-parameter.js.map +1 -0
- package/dist/src/sdk/utils/wit/encode-function-data.test-d.js +24 -0
- package/dist/src/sdk/utils/wit/encode-function-data.test-d.js.map +1 -0
- package/dist/src/sdk/utils/wit/encode-function-data.test.js +91 -0
- package/dist/src/sdk/utils/wit/encode-function-data.test.js.map +1 -0
- package/dist/src/sdk/utils/wit/encode-wit-parameters.js +261 -0
- package/dist/src/sdk/utils/wit/encode-wit-parameters.js.map +1 -0
- package/dist/src/sdk/utils/wit/get-wit-item.test.js +167 -0
- package/dist/src/sdk/utils/wit/get-wit-item.test.js.map +1 -0
- package/dist/src/wit/wit-parser/core/records.test.js +63 -0
- package/dist/src/wit/wit-parser/core/records.test.js.map +1 -0
- package/dist/src/wit/wit-parser/core/types/records.test-d.js +19 -0
- package/dist/src/wit/wit-parser/core/types/records.test-d.js.map +1 -0
- package/dist/src/wit/wit-parser/core/types/utils.js +2 -0
- package/dist/src/wit/wit-parser/core/types/utils.js.map +1 -0
- package/dist/src/wit/wit-parser/core/types/utils.test-d.js +159 -0
- package/dist/src/wit/wit-parser/core/types/utils.test-d.js.map +1 -0
- package/dist/src/wit/wit-parser/core/utils.js +213 -0
- package/dist/src/wit/wit-parser/core/utils.js.map +1 -0
- package/dist/src/wit/wit-parser/core/utils.test.js +316 -0
- package/dist/src/wit/wit-parser/core/utils.test.js.map +1 -0
- package/dist/src/wit/wit-parser/parse-wit-parameter.test.js +93 -0
- package/dist/src/wit/wit-parser/parse-wit-parameter.test.js.map +1 -0
- package/dist/src/wit/wit-parser/parse-wit.test.js +80 -0
- package/dist/src/wit/wit-parser/parse-wit.test.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +1 -1
- package/src/exports/index.test.ts +18 -1
- package/src/sdk/actions/kontor/public/call-view.ts +6 -14
- package/src/sdk/actions/kontor/public/proc-contract.test.ts +1 -1
- package/src/sdk/actions/kontor/public/view-contract.test-d.ts +2 -17
- package/src/sdk/actions/kontor/public/view-contract.ts +8 -3
- package/src/sdk/actions/kontor/wallet/sign-reveal.ts +3 -35
- package/src/sdk/clients/base/create-rpc-client.test.ts +10 -4
- package/src/sdk/clients/create-public-client.test.ts +11 -3
- package/src/sdk/clients/kontor/create-public-client.test.ts +4 -3
- package/src/sdk/contracts/wits.ts +1 -1
- package/src/sdk/types/contract.test-d.ts +29 -24
- package/src/sdk/utils/wit/codecs/u64.ts +73 -0
- package/src/sdk/utils/wit/decode-wit-parameter.ts +5 -0
- package/src/sdk/utils/wit/encode-function-data.test-d.ts +15 -11
- package/src/sdk/utils/wit/encode-function-data.test.ts +1 -1
- package/src/sdk/utils/wit/encode-wit-parameters.ts +16 -1
- package/src/sdk/utils/wit/get-wit-item.test.ts +58 -9
- package/src/wit/wit-parser/core/records.test.ts +24 -14
- package/src/wit/wit-parser/core/types/records.test-d.ts +141 -28
- package/src/wit/wit-parser/core/types/utils.test-d.ts +23 -2
- package/src/wit/wit-parser/core/types/utils.ts +8 -41
- package/src/wit/wit-parser/core/utils.test.ts +14 -0
- package/src/wit/wit-parser/core/utils.ts +1 -0
- package/src/wit/wit-parser/parse-wit-parameter.test.ts +30 -3
- package/src/wit/wit-parser/parse-wit.test.ts +93 -102
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { assertType, test } from "vitest";
|
|
2
|
+
test("ParseWitParameter", () => {
|
|
3
|
+
// everything in fib contrct
|
|
4
|
+
assertType({
|
|
5
|
+
name: "ctx",
|
|
6
|
+
type: "borrow<proc-context>",
|
|
7
|
+
internalType: "borrow<proc-context>",
|
|
8
|
+
});
|
|
9
|
+
assertType({
|
|
10
|
+
name: "arith-address",
|
|
11
|
+
type: "contract-address",
|
|
12
|
+
internalType: "contract-address",
|
|
13
|
+
});
|
|
14
|
+
assertType({
|
|
15
|
+
name: "n",
|
|
16
|
+
type: "u64",
|
|
17
|
+
internalType: "u64",
|
|
18
|
+
});
|
|
19
|
+
assertType({
|
|
20
|
+
type: "u64",
|
|
21
|
+
internalType: "u64",
|
|
22
|
+
});
|
|
23
|
+
assertType({
|
|
24
|
+
name: "x",
|
|
25
|
+
type: "string",
|
|
26
|
+
internalType: "string",
|
|
27
|
+
});
|
|
28
|
+
assertType({
|
|
29
|
+
name: "y",
|
|
30
|
+
type: "string",
|
|
31
|
+
internalType: "string",
|
|
32
|
+
});
|
|
33
|
+
assertType({
|
|
34
|
+
name: "option-string",
|
|
35
|
+
type: "option<string>",
|
|
36
|
+
});
|
|
37
|
+
assertType({
|
|
38
|
+
name: "option-list-string",
|
|
39
|
+
type: "option<list<string>>",
|
|
40
|
+
});
|
|
41
|
+
assertType({
|
|
42
|
+
name: "option-list-result-string-contract-address",
|
|
43
|
+
type: "option<list<result<string, contract-address>>>",
|
|
44
|
+
});
|
|
45
|
+
assertType({
|
|
46
|
+
type: "contract-address",
|
|
47
|
+
internalType: "contract-address",
|
|
48
|
+
});
|
|
49
|
+
assertType({
|
|
50
|
+
name: "ctx",
|
|
51
|
+
type: "borrow<view-context>",
|
|
52
|
+
internalType: "borrow<view-context>",
|
|
53
|
+
});
|
|
54
|
+
assertType({
|
|
55
|
+
name: "arith-address",
|
|
56
|
+
type: "contract-address",
|
|
57
|
+
internalType: "contract-address",
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
test("_ParseFunctionParametersAndContext", () => {
|
|
61
|
+
assertType({
|
|
62
|
+
Inputs: "arith-address: contract-address, n: u64",
|
|
63
|
+
Context: "proc",
|
|
64
|
+
});
|
|
65
|
+
assertType({
|
|
66
|
+
Inputs: "",
|
|
67
|
+
Context: "view",
|
|
68
|
+
});
|
|
69
|
+
assertType({
|
|
70
|
+
Inputs: "",
|
|
71
|
+
Context: "view",
|
|
72
|
+
});
|
|
73
|
+
assertType({
|
|
74
|
+
Inputs: "",
|
|
75
|
+
Context: "core",
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
test("SplitParameters", () => {
|
|
79
|
+
assertType(["u64"]);
|
|
80
|
+
assertType([
|
|
81
|
+
"arith-address: contract-address",
|
|
82
|
+
"n: list<u64>",
|
|
83
|
+
]);
|
|
84
|
+
});
|
|
85
|
+
test("Parse Signature", () => {
|
|
86
|
+
assertType({
|
|
87
|
+
type: "function",
|
|
88
|
+
name: "fib",
|
|
89
|
+
context: "proc",
|
|
90
|
+
inputs: [
|
|
91
|
+
{
|
|
92
|
+
name: "arith-address",
|
|
93
|
+
type: "contract-address",
|
|
94
|
+
internalType: "contract-address",
|
|
95
|
+
},
|
|
96
|
+
{ name: "n", type: "list<u64>" },
|
|
97
|
+
],
|
|
98
|
+
outputs: [
|
|
99
|
+
{
|
|
100
|
+
type: "u64",
|
|
101
|
+
internalType: "u64",
|
|
102
|
+
},
|
|
103
|
+
],
|
|
104
|
+
});
|
|
105
|
+
assertType({
|
|
106
|
+
type: "function",
|
|
107
|
+
name: "fib",
|
|
108
|
+
context: "proc",
|
|
109
|
+
inputs: [
|
|
110
|
+
{
|
|
111
|
+
name: "arith-address",
|
|
112
|
+
type: "contract-address",
|
|
113
|
+
internalType: "contract-address",
|
|
114
|
+
},
|
|
115
|
+
{ name: "n", type: "list<u64>" },
|
|
116
|
+
],
|
|
117
|
+
outputs: [
|
|
118
|
+
{
|
|
119
|
+
type: "contract-address",
|
|
120
|
+
internalType: "contract-address",
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
});
|
|
124
|
+
assertType({
|
|
125
|
+
type: "function",
|
|
126
|
+
name: "fib",
|
|
127
|
+
context: "proc",
|
|
128
|
+
inputs: [
|
|
129
|
+
{
|
|
130
|
+
name: "arith-address",
|
|
131
|
+
type: "contract-address",
|
|
132
|
+
internalType: "contract-address",
|
|
133
|
+
},
|
|
134
|
+
{ name: "n", type: "list<u64>" },
|
|
135
|
+
],
|
|
136
|
+
outputs: [
|
|
137
|
+
{
|
|
138
|
+
type: "contract-address",
|
|
139
|
+
internalType: "contract-address",
|
|
140
|
+
},
|
|
141
|
+
],
|
|
142
|
+
});
|
|
143
|
+
assertType({
|
|
144
|
+
type: "function",
|
|
145
|
+
name: "get-dogs",
|
|
146
|
+
context: "proc",
|
|
147
|
+
inputs: [],
|
|
148
|
+
outputs: [
|
|
149
|
+
{
|
|
150
|
+
type: "list<tuple>",
|
|
151
|
+
components: [
|
|
152
|
+
{ type: "string", name: "name" },
|
|
153
|
+
{ type: "string", name: "breed" },
|
|
154
|
+
],
|
|
155
|
+
},
|
|
156
|
+
],
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
//# sourceMappingURL=utils.test-d.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.test-d.js","sourceRoot":"","sources":["../../../../../../src/wit/wit-parser/core/types/utils.test-d.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AA2C1C,IAAI,CAAC,mBAAmB,EAAE,GAAG,EAAE;IAC7B,4BAA4B;IAC5B,UAAU,CAAiD;QACzD,IAAI,EAAE,KAAK;QACX,IAAI,EAAE,sBAAsB;QAC5B,YAAY,EAAE,sBAAsB;KACrC,CAAC,CAAC;IAEH,UAAU,CAAuD;QAC/D,IAAI,EAAE,eAAe;QACrB,IAAI,EAAE,kBAAkB;QACxB,YAAY,EAAE,kBAAkB;KACjC,CAAC,CAAC;IAEH,UAAU,CAA8B;QACtC,IAAI,EAAE,GAAG;QACT,IAAI,EAAE,KAAK;QACX,YAAY,EAAE,KAAK;KACpB,CAAC,CAAC;IAEH,UAAU,CAA2B;QACnC,IAAI,EAAE,KAAK;QACX,YAAY,EAAE,KAAK;KACpB,CAAC,CAAC;IAEH,UAAU,CAAiC;QACzC,IAAI,EAAE,GAAG;QACT,IAAI,EAAE,QAAQ;QACd,YAAY,EAAE,QAAQ;KACvB,CAAC,CAAC;IAEH,UAAU,CAAiC;QACzC,IAAI,EAAE,GAAG;QACT,IAAI,EAAE,QAAQ;QACd,YAAY,EAAE,QAAQ;KACvB,CAAC,CAAC;IAEH,UAAU,CAAqD;QAC7D,IAAI,EAAE,eAAe;QACrB,IAAI,EAAE,gBAAgB;KACvB,CAAC,CAAC;IAEH,UAAU,CAAgE;QACx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AQ,EAAE,IAAI,EAAE,OAAO,EAAE;iBAClC;aACF;SACF;KACF,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { execTyped, isGenericRegex } from "../../regex.js";
|
|
2
|
+
import { InvalidWitParameterError, WitProtectedKeywordError, } from "./errors/wit-parameter.js";
|
|
3
|
+
import { InvalidSignatureError, UnknownSignatureError, } from "./errors/signature.js";
|
|
4
|
+
import { InvalidGenericDepthError } from "./errors/splitParameters.js";
|
|
5
|
+
import { execFunctionSignature, isFunctionSignature } from "./signatures.js";
|
|
6
|
+
//
|
|
7
|
+
// export const witParameterRegex =
|
|
8
|
+
// /^(?:(?<name>[a-z][a-z0-9-]*)\s*:\s*)?(?<type>(?:borrow|list|option|result|tuple)\s*<[\s\S]+>|[a-z][a-z0-9-]*|(?:record|variant|flags|enum)\s*\{[\s\S]*\})$/;
|
|
9
|
+
//
|
|
10
|
+
export const witParameterRegex = /^(?:(?<name>[a-z][a-z0-9-]*)\s*:\s*)?(?<type>_|(?:borrow|list|option|result|tuple)\s*<[\s\S]+>|[a-z][a-z0-9-]*|(?:record|variant|flags|enum)\s*\{[\s\S]*\})$/;
|
|
11
|
+
export function isGeneric(type) {
|
|
12
|
+
return isGenericRegex.test(type.trim());
|
|
13
|
+
}
|
|
14
|
+
export function execGeneric(type) {
|
|
15
|
+
const m = type.trim().match(isGenericRegex);
|
|
16
|
+
if (!m || !m.groups)
|
|
17
|
+
return null;
|
|
18
|
+
return m.groups;
|
|
19
|
+
}
|
|
20
|
+
export function parseWitParameter(param, options) {
|
|
21
|
+
// TODO: cache
|
|
22
|
+
// const parameterCacheKey = getParameterCacheKey(
|
|
23
|
+
// param,
|
|
24
|
+
// options?.type,
|
|
25
|
+
// options?.structs,
|
|
26
|
+
// );
|
|
27
|
+
// if (parameterCache.has(parameterCacheKey))
|
|
28
|
+
// return parameterCache.get(parameterCacheKey)!;
|
|
29
|
+
//
|
|
30
|
+
//
|
|
31
|
+
const match = execTyped(witParameterRegex, param);
|
|
32
|
+
if (!match)
|
|
33
|
+
throw new InvalidWitParameterError({ param });
|
|
34
|
+
const isGenericType = isGeneric(match.type);
|
|
35
|
+
if (match.name && isWitKeyword(match.name))
|
|
36
|
+
throw new WitProtectedKeywordError({ param, name: match.name });
|
|
37
|
+
// TODO: smell
|
|
38
|
+
const name = match.name ? { name: match.name } : {};
|
|
39
|
+
const records = options?.records ?? {};
|
|
40
|
+
let type;
|
|
41
|
+
let components = {};
|
|
42
|
+
if (isGenericType) {
|
|
43
|
+
const g = execGeneric(match.type);
|
|
44
|
+
const parts = splitParameters(g.inner);
|
|
45
|
+
const parsed = parts.map((p) => parseWitParameter(p, { records }));
|
|
46
|
+
// normalize inner types for the outer type string
|
|
47
|
+
const innerTypeSig = parsed.map((x) => x.type).join(", ");
|
|
48
|
+
switch (g.outer) {
|
|
49
|
+
case "list":
|
|
50
|
+
case "option": {
|
|
51
|
+
if (parsed.length !== 1) {
|
|
52
|
+
throw new InvalidWitParameterError({
|
|
53
|
+
param,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
const t = parsed[0];
|
|
57
|
+
type = `${g.outer}<${t.type}>`;
|
|
58
|
+
// flatten record fields for list/option only
|
|
59
|
+
components = t.components ? { components: t.components } : {};
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
case "result": {
|
|
63
|
+
if (parsed.length !== 2) {
|
|
64
|
+
throw new InvalidWitParameterError({
|
|
65
|
+
param,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
const left = parsed[0];
|
|
69
|
+
// @ts-ignore
|
|
70
|
+
const right = parsed[1];
|
|
71
|
+
type = `result<${innerTypeSig}>`;
|
|
72
|
+
components = {
|
|
73
|
+
...(left.components && { components: left.components }),
|
|
74
|
+
// components: [
|
|
75
|
+
//
|
|
76
|
+
// {
|
|
77
|
+
// name: "left",
|
|
78
|
+
// type: left.type,
|
|
79
|
+
// ...(left.components && { components: left.components }),
|
|
80
|
+
// },
|
|
81
|
+
// {
|
|
82
|
+
// name: "right",
|
|
83
|
+
// type: right.type,
|
|
84
|
+
// ...(right.components && { components: right.components }),
|
|
85
|
+
// },
|
|
86
|
+
// ],
|
|
87
|
+
};
|
|
88
|
+
// the right / error channel is always assumed to be wit built-in `error` primitive
|
|
89
|
+
// components = {
|
|
90
|
+
// components: [
|
|
91
|
+
// {
|
|
92
|
+
// name: "left",
|
|
93
|
+
// type: left.type,
|
|
94
|
+
// ...(left.components && { components: left.components }),
|
|
95
|
+
// },
|
|
96
|
+
// {
|
|
97
|
+
// name: "right",
|
|
98
|
+
// type: right.type,
|
|
99
|
+
// ...(right.components && { components: right.components }),
|
|
100
|
+
// },
|
|
101
|
+
// ],
|
|
102
|
+
// };
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
default: {
|
|
106
|
+
type = `${g.outer}<${innerTypeSig}>`;
|
|
107
|
+
components = {};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else if (match.type in records) {
|
|
112
|
+
// scalar that maps to a record in `records`
|
|
113
|
+
type = "tuple";
|
|
114
|
+
components = { components: records[match.type] };
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
type = match.type;
|
|
118
|
+
}
|
|
119
|
+
const witParameter = {
|
|
120
|
+
type: `${type}${match.array ?? ""}`,
|
|
121
|
+
internalType: match.type,
|
|
122
|
+
...name,
|
|
123
|
+
...components,
|
|
124
|
+
};
|
|
125
|
+
// parameterCache.set(parameterCacheKey, witParameter);
|
|
126
|
+
//
|
|
127
|
+
return witParameter;
|
|
128
|
+
}
|
|
129
|
+
export const witKeywordRegex = /^(?:package|world|interface|contract|include|use|import|export|func|borrow|record|list|enum|flags|variant|option|result|tuple|bool|s8|s16|s32|s64|u8|u16|u32|u64|f32|f64|char|string)$/;
|
|
130
|
+
export function isWitKeyword(word) {
|
|
131
|
+
return witKeywordRegex.test(word);
|
|
132
|
+
}
|
|
133
|
+
// class InvalidParenthesisError extends Error { constructor(public info: { current: string; depth: number }) { super('Unbalanced <>'); } }
|
|
134
|
+
export function splitParameters(params, result = []) {
|
|
135
|
+
const s = params.trim();
|
|
136
|
+
if (s.length === 0)
|
|
137
|
+
return result;
|
|
138
|
+
let current = "";
|
|
139
|
+
let depth = 0; // nesting depth for <...>
|
|
140
|
+
for (let i = 0; i < s.length; i++) {
|
|
141
|
+
const ch = s[i];
|
|
142
|
+
if (ch === "<") {
|
|
143
|
+
depth += 1;
|
|
144
|
+
current += ch;
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (ch === ">") {
|
|
148
|
+
depth -= 1;
|
|
149
|
+
if (depth < 0)
|
|
150
|
+
throw new InvalidGenericDepthError({
|
|
151
|
+
current: s.slice(0, i + 1),
|
|
152
|
+
depth,
|
|
153
|
+
});
|
|
154
|
+
current += ch;
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (ch === "," && depth === 0) {
|
|
158
|
+
const tok = current.trim();
|
|
159
|
+
if (tok.length)
|
|
160
|
+
result.push(tok);
|
|
161
|
+
current = "";
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
current += ch;
|
|
165
|
+
}
|
|
166
|
+
if (depth !== 0)
|
|
167
|
+
throw new InvalidGenericDepthError({ current, depth });
|
|
168
|
+
const tail = current.trim();
|
|
169
|
+
if (tail.length)
|
|
170
|
+
result.push(tail);
|
|
171
|
+
return result;
|
|
172
|
+
}
|
|
173
|
+
export function parseSignature(signature, structs = {}) {
|
|
174
|
+
if (isFunctionSignature(signature))
|
|
175
|
+
return parseFunctionSignature(signature, structs);
|
|
176
|
+
throw new UnknownSignatureError({ signature });
|
|
177
|
+
}
|
|
178
|
+
export function parseFunctionSignature(signature, records = {}) {
|
|
179
|
+
const match = execFunctionSignature(signature);
|
|
180
|
+
if (!match)
|
|
181
|
+
throw new InvalidSignatureError({ signature, type: "func" });
|
|
182
|
+
const inputParams = splitParameters(match.parameters);
|
|
183
|
+
const inputs = [];
|
|
184
|
+
const inputLength = inputParams.length;
|
|
185
|
+
for (let i = 0; i < inputLength; i++) {
|
|
186
|
+
inputs.push(parseWitParameter(inputParams[i], {
|
|
187
|
+
records,
|
|
188
|
+
}));
|
|
189
|
+
}
|
|
190
|
+
const outputs = [];
|
|
191
|
+
if (match.returns) {
|
|
192
|
+
const outputParams = splitParameters(match.returns);
|
|
193
|
+
const outputLength = outputParams.length;
|
|
194
|
+
for (let i = 0; i < outputLength; i++) {
|
|
195
|
+
outputs.push(parseWitParameter(outputParams[i], {
|
|
196
|
+
records,
|
|
197
|
+
}));
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
const contextInput = inputs.shift();
|
|
201
|
+
return {
|
|
202
|
+
name: match.name,
|
|
203
|
+
type: "function",
|
|
204
|
+
context: contextInput?.type === "borrow<proc-context>"
|
|
205
|
+
? "proc"
|
|
206
|
+
: contextInput?.type === "borrow<view-context>"
|
|
207
|
+
? "view"
|
|
208
|
+
: "core",
|
|
209
|
+
inputs,
|
|
210
|
+
outputs,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../../../src/wit/wit-parser/core/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAC3D,OAAO,EACL,wBAAwB,EACxB,wBAAwB,GACzB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EACL,qBAAqB,EACrB,qBAAqB,GACtB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,wBAAwB,EAAE,MAAM,6BAA6B,CAAC;AACvE,OAAO,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAO7E,EAAE;AACF,mCAAmC;AACnC,kKAAkK;AAClK,EAAE;AACF,MAAM,CAAC,MAAM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