@givelotus/compiler 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +331 -2
- package/artifact-v2.schema.json +58 -0
- package/artifact.schema.json +19 -0
- package/dist/ast.d.ts +292 -0
- package/dist/ast.js +150 -0
- package/dist/bindings.d.ts +6 -0
- package/dist/bindings.js +134 -0
- package/dist/call-runtime.d.ts +84 -0
- package/dist/call-runtime.js +249 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +22 -0
- package/dist/compiler.d.ts +79 -0
- package/dist/compiler.js +125 -0
- package/dist/diagnostics.d.ts +163 -0
- package/dist/diagnostics.js +498 -0
- package/dist/ecash.d.ts +41 -0
- package/dist/ecash.js +190 -0
- package/dist/fees.d.ts +164 -0
- package/dist/fees.js +208 -0
- package/dist/fixture-cli.d.ts +2 -0
- package/dist/fixture-cli.js +41 -0
- package/dist/fixtures.d.ts +68 -0
- package/dist/fixtures.js +46 -0
- package/dist/forth-ir.d.ts +73 -0
- package/dist/forth-ir.js +147 -0
- package/dist/generic-cli.d.ts +2 -0
- package/dist/generic-cli.js +140 -0
- package/dist/generic.d.ts +48 -0
- package/dist/generic.js +2566 -0
- package/dist/lints.d.ts +8 -0
- package/dist/lints.js +204 -0
- package/dist/names.d.ts +1 -0
- package/dist/names.js +7 -0
- package/dist/opcodes.d.ts +154 -0
- package/dist/opcodes.js +162 -0
- package/dist/optimizer.d.ts +58 -0
- package/dist/optimizer.js +786 -0
- package/dist/package-fetch-cli.d.ts +2 -0
- package/dist/package-fetch-cli.js +17 -0
- package/dist/package-lock-cli.d.ts +2 -0
- package/dist/package-lock-cli.js +28 -0
- package/dist/package-sources.d.ts +82 -0
- package/dist/package-sources.js +785 -0
- package/dist/package-update-cli.d.ts +2 -0
- package/dist/package-update-cli.js +20 -0
- package/dist/packages.d.ts +231 -0
- package/dist/packages.js +1307 -0
- package/dist/parser.d.ts +32 -0
- package/dist/parser.js +575 -0
- package/dist/policy.d.ts +83 -0
- package/dist/policy.js +207 -0
- package/dist/runtime.d.ts +58 -0
- package/dist/runtime.js +126 -0
- package/dist/scheduler.d.ts +38 -0
- package/dist/scheduler.js +76 -0
- package/dist/semantic-ir.d.ts +76 -0
- package/dist/semantic-ir.js +231 -0
- package/dist/std.d.ts +27 -0
- package/dist/std.js +225 -0
- package/dist/target-compiler.d.ts +9 -0
- package/dist/target-compiler.js +139 -0
- package/dist/targets.d.ts +53 -0
- package/dist/targets.js +67 -0
- package/dist/toml-subset.d.ts +19 -0
- package/dist/toml-subset.js +81 -0
- package/dist/transaction-runtime.d.ts +182 -0
- package/dist/transaction-runtime.js +646 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.js +3 -0
- package/dist/vm-cost.d.ts +11 -0
- package/dist/vm-cost.js +541 -0
- package/package.json +71 -4
- package/policy-artifact.schema.json +20 -0
- package/std/custody/arbiter-escrow.lotus +20 -0
- package/std/custody/htlc.lotus +15 -0
- package/std/custody/multisig-2of3.lotus +13 -0
- package/std/custody/single-owner-wallet.lotus +9 -0
- package/std/custody/vault-with-recovery.lotus +14 -0
- package/std/defi/amm-pool-v2.lotus +97 -0
- package/std/defi/indexed-limit-order.lotus +37 -0
- package/std/nft/nft-collection.lotus +51 -0
- package/std/nft/royalty-transfer-rules.lotus +38 -0
- package/std/nft/unique-nft.lotus +30 -0
- package/std/tokens/capped-supply-token.lotus +76 -0
- package/std/tokens/fixed-supply-token.lotus +29 -0
- package/std/tokens/standard-transfer-rules.lotus +15 -0
- package/std/tokens/voucher-mint-token.lotus +31 -0
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
/** Maximum diagnostics reported by one compile before recovery stops. */
|
|
2
|
+
export const MAX_DIAGNOSTICS = 20;
|
|
3
|
+
/** Stable codes. Messages may be refined; codes are not reused. */
|
|
4
|
+
export const DIAGNOSTIC_CODES = {
|
|
5
|
+
E0000: 'fatal input/usage error outside source (CLI)',
|
|
6
|
+
E0001: 'unsupported character',
|
|
7
|
+
E0002: 'source exceeds the UTF-8 size limit',
|
|
8
|
+
E0003: 'unterminated block comment',
|
|
9
|
+
E0004: 'unterminated string literal',
|
|
10
|
+
E0101: 'unexpected token (expected-token list)',
|
|
11
|
+
E0102: 'expected identifier',
|
|
12
|
+
E0103: 'unsupported type',
|
|
13
|
+
E0104: 'Bytes(N) bound outside 1..8192',
|
|
14
|
+
E0105: 'expression nesting depth exceeded',
|
|
15
|
+
E0106: 'control nesting depth exceeded',
|
|
16
|
+
E0107: 'statement budget exceeded',
|
|
17
|
+
E0108: 'loop nesting exceeds two',
|
|
18
|
+
E0109: 'unsupported loop collection',
|
|
19
|
+
E0110: 'unsupported assignment operator',
|
|
20
|
+
E0111: 'unsupported statement',
|
|
21
|
+
E0112: 'malformed import',
|
|
22
|
+
E0113: 'import after declarations',
|
|
23
|
+
E0114: 'visibility modifier on a non-declaration',
|
|
24
|
+
E0115: 'reserved or invalid binding name',
|
|
25
|
+
E0116: 'source has no contract',
|
|
26
|
+
E0117: 'more than 16 methods',
|
|
27
|
+
E0118: 'token method first parameter must be Amount',
|
|
28
|
+
E0119: 'empty contract or method',
|
|
29
|
+
E0120: 'qualified name must be called',
|
|
30
|
+
E0121: 'import requires a package build',
|
|
31
|
+
E0122: 'for inside a helper',
|
|
32
|
+
E0123: 'helper return is missing or not final',
|
|
33
|
+
E0124: 'break outside of a for loop',
|
|
34
|
+
E0125: 'invalid tuple destructuring target or initializer',
|
|
35
|
+
E0201: 'duplicate parameter',
|
|
36
|
+
E0202: 'duplicate helper',
|
|
37
|
+
E0203: 'duplicate method (case-insensitive)',
|
|
38
|
+
E0204: 'duplicate contract',
|
|
39
|
+
E0205: 'method parameter shadows constructor parameter',
|
|
40
|
+
E0206: 'loop variable shadows a binding',
|
|
41
|
+
E0207: 'duplicate method leaf (identical scripts)',
|
|
42
|
+
E0208: 'let shadows a binding',
|
|
43
|
+
E0209: 'duplicate state declaration',
|
|
44
|
+
E0301: 'unbound name',
|
|
45
|
+
E0302: 'unknown function',
|
|
46
|
+
E0303: 'unknown contract',
|
|
47
|
+
E0304: 'unknown method',
|
|
48
|
+
E0305: 'unbound or invalid record',
|
|
49
|
+
E0401: 'operand type mismatch',
|
|
50
|
+
E0402: 'invalid unary operand',
|
|
51
|
+
E0403: 'invalid binary operator for operand types',
|
|
52
|
+
E0404: 'condition is not Bool',
|
|
53
|
+
E0405: 'builtin argument type mismatch',
|
|
54
|
+
E0406: 'helper argument type mismatch',
|
|
55
|
+
E0407: 'helper result type mismatch',
|
|
56
|
+
E0408: 'numeric local required',
|
|
57
|
+
E0409: 'invalid assignment target',
|
|
58
|
+
E0410: 'local must be initialized at method top level',
|
|
59
|
+
E0411: 'mint method has no coin',
|
|
60
|
+
E0412: 'introspection field misuse',
|
|
61
|
+
E0413: 'Int literal outside signed 128-bit range',
|
|
62
|
+
E0414: 'let binding type mismatch',
|
|
63
|
+
E0415: 'lock kind is not a compile-time constant',
|
|
64
|
+
E0416: 'invalid lock kind',
|
|
65
|
+
E0417: 'lock bound out of range',
|
|
66
|
+
E0418: 'relative lock in a mint',
|
|
67
|
+
E0501: 'helper arity mismatch',
|
|
68
|
+
E0502: 'builtin arity or argument form',
|
|
69
|
+
E0503: 'recursive helper',
|
|
70
|
+
E0601: 'compile options require contract and bindings',
|
|
71
|
+
E0602: 'invalid constructor binding map',
|
|
72
|
+
E0603: 'missing constructor binding',
|
|
73
|
+
E0604: 'invalid constructor binding value',
|
|
74
|
+
E0605: 'selected contract does not exist',
|
|
75
|
+
E0701: 'MAX_SCRIPT_BYTES exceeded',
|
|
76
|
+
E0702: 'opcode budget exceeded',
|
|
77
|
+
E0703: 'helper expansion budget exceeded',
|
|
78
|
+
E0704: 'type inference visit budget exceeded',
|
|
79
|
+
E0705: 'dependency scan budget exceeded',
|
|
80
|
+
E0706: 'helper dependency depth exceeded',
|
|
81
|
+
E0707: 'MAX_ELEMENT_BYTES exceeded',
|
|
82
|
+
E0801: 'leaf/root dependency cycle',
|
|
83
|
+
E0802: 'compiler scheduler limit exceeded',
|
|
84
|
+
E0901: 'package manifest error',
|
|
85
|
+
E0902: 'path escapes its root',
|
|
86
|
+
E0903: 'symlink escapes its root',
|
|
87
|
+
E0904: 'path collision under case/Unicode folding',
|
|
88
|
+
E0905: 'undeclared file in a source root',
|
|
89
|
+
E0906: 'missing module or file',
|
|
90
|
+
E0907: 'lotus.lock missing',
|
|
91
|
+
E0908: 'lotus.lock digest mismatch',
|
|
92
|
+
E0909: 'lotus.lock stale',
|
|
93
|
+
E0910: 'package version conflict',
|
|
94
|
+
E0911: 'package dependency cycle',
|
|
95
|
+
E0912: 'module parse error',
|
|
96
|
+
E0913: 'unknown dependency alias',
|
|
97
|
+
E0914: 'module not exported',
|
|
98
|
+
E0915: 'missing imported symbol',
|
|
99
|
+
E0916: 'helper not visible',
|
|
100
|
+
E0917: 'duplicate or ambiguous local name',
|
|
101
|
+
E0918: 'module import cycle',
|
|
102
|
+
E0919: 'declaration-only module rule',
|
|
103
|
+
E0920: 'package graph limit exceeded',
|
|
104
|
+
E0921: 'branch or range selector outside package update',
|
|
105
|
+
E0922: 'module not present in the offline cache',
|
|
106
|
+
E0923: 'invalid git locator',
|
|
107
|
+
E0924: 'git fetch failed',
|
|
108
|
+
E0130: 'explicit invocation marker required',
|
|
109
|
+
E0131: 'language version mismatch',
|
|
110
|
+
E0132: 'unsupported language version',
|
|
111
|
+
E0133: 'unsupported or conflicting compiler edition',
|
|
112
|
+
E0134: 'unsupported or conflicting optimizer version',
|
|
113
|
+
E0135: 'unsupported or misplaced pragma',
|
|
114
|
+
E0136: 'duplicate pragma',
|
|
115
|
+
E0708: 'optimizer budget exceeded',
|
|
116
|
+
E0930: 'invalid program instance binding',
|
|
117
|
+
E0931: 'program root dependency cycle',
|
|
118
|
+
E1200: 'unknown compiler target',
|
|
119
|
+
E1201: 'unsupported target capability',
|
|
120
|
+
W0701: 'payout checked against this coin alone can absorb another input of the same policy',
|
|
121
|
+
W0702: 'method always succeeds',
|
|
122
|
+
W0703: 'Signature argument never used',
|
|
123
|
+
W0704: 'division or modulo by a dynamic divisor without an obvious non-zero guard',
|
|
124
|
+
E1202: 'invalid target entrypoint',
|
|
125
|
+
E1203: 'source feature not implemented for target',
|
|
126
|
+
E1204: 'invalid portable semantic program',
|
|
127
|
+
E1210: 'target script resource limit exceeded',
|
|
128
|
+
E9001: 'internal compiler invariant failed',
|
|
129
|
+
};
|
|
130
|
+
const encoder = new TextEncoder();
|
|
131
|
+
const decoder = new TextDecoder();
|
|
132
|
+
/** One source file: text, UTF-8 bytes and line starts (byte offsets). */
|
|
133
|
+
export class SourceFile {
|
|
134
|
+
name;
|
|
135
|
+
text;
|
|
136
|
+
bytes;
|
|
137
|
+
lineStarts;
|
|
138
|
+
constructor(name, text) {
|
|
139
|
+
this.name = name;
|
|
140
|
+
this.text = text;
|
|
141
|
+
this.bytes = encoder.encode(text);
|
|
142
|
+
this.lineStarts = [0];
|
|
143
|
+
for (let i = 0; i < this.bytes.length; i++)
|
|
144
|
+
if (this.bytes[i] === 0x0a)
|
|
145
|
+
this.lineStarts.push(i + 1);
|
|
146
|
+
}
|
|
147
|
+
/** 0-based line index containing byte offset. */
|
|
148
|
+
lineIndex(offset) {
|
|
149
|
+
let lo = 0, hi = this.lineStarts.length - 1;
|
|
150
|
+
while (lo < hi) {
|
|
151
|
+
const mid = (lo + hi + 1) >> 1;
|
|
152
|
+
if (this.lineStarts[mid] <= offset)
|
|
153
|
+
lo = mid;
|
|
154
|
+
else
|
|
155
|
+
hi = mid - 1;
|
|
156
|
+
}
|
|
157
|
+
return lo;
|
|
158
|
+
}
|
|
159
|
+
/** Line text without its terminator (`\n` or `\r\n`). */
|
|
160
|
+
lineText(index) {
|
|
161
|
+
const start = this.lineStarts[index], next = this.lineStarts[index + 1];
|
|
162
|
+
let end = next === undefined ? this.bytes.length : next - 1;
|
|
163
|
+
if (end > start && this.bytes[end - 1] === 0x0d)
|
|
164
|
+
end--;
|
|
165
|
+
return decoder.decode(this.bytes.subarray(start, end));
|
|
166
|
+
}
|
|
167
|
+
get lineCount() { return this.lineStarts.length; }
|
|
168
|
+
/** 1-based line and Unicode-scalar column of a byte offset (clamped into the file). */
|
|
169
|
+
position(offset) {
|
|
170
|
+
const at = Math.max(0, Math.min(offset, this.bytes.length));
|
|
171
|
+
const index = this.lineIndex(at), start = this.lineStarts[index];
|
|
172
|
+
return { line: index + 1, column: scalarCount(this.bytes, start, at) + 1, byteColumn: at - start + 1 };
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
function scalarCount(bytes, from, to) {
|
|
176
|
+
let n = 0;
|
|
177
|
+
for (let i = from; i < to; i++)
|
|
178
|
+
if ((bytes[i] & 0xc0) !== 0x80)
|
|
179
|
+
n++;
|
|
180
|
+
return n;
|
|
181
|
+
}
|
|
182
|
+
/** Display cells of one code point in an excerpt (tab handled by the caller). */
|
|
183
|
+
function cellWidth(cp) {
|
|
184
|
+
if (cp < 0x20 || (cp >= 0x7f && cp < 0xa0))
|
|
185
|
+
return cp === 0 ? 0 : 1;
|
|
186
|
+
if (/\p{M}/u.test(String.fromCodePoint(cp)) || cp === 0x200b || cp === 0x200d)
|
|
187
|
+
return 0;
|
|
188
|
+
if ((cp >= 0x1100 && cp <= 0x115f) || (cp >= 0x2e80 && cp <= 0xa4cf) || (cp >= 0xac00 && cp <= 0xd7a3) || (cp >= 0xf900 && cp <= 0xfaff) ||
|
|
189
|
+
(cp >= 0xfe30 && cp <= 0xfe4f) || (cp >= 0xff00 && cp <= 0xff60) || (cp >= 0xffe0 && cp <= 0xffe6) || (cp >= 0x1f300 && cp <= 0x1faff) ||
|
|
190
|
+
(cp >= 0x20000 && cp <= 0x3fffd))
|
|
191
|
+
return 2;
|
|
192
|
+
return 1;
|
|
193
|
+
}
|
|
194
|
+
const TAB = 4;
|
|
195
|
+
/** Expand a line for display and return display-cell offsets for each UTF-8 byte offset in it. */
|
|
196
|
+
function layout(line) {
|
|
197
|
+
let text = '', cells = 0;
|
|
198
|
+
const marks = [];
|
|
199
|
+
let byte = 0;
|
|
200
|
+
for (const ch of line) {
|
|
201
|
+
const cp = ch.codePointAt(0);
|
|
202
|
+
marks.push([byte, cells]);
|
|
203
|
+
if (cp === 9) {
|
|
204
|
+
const n = TAB - (cells % TAB);
|
|
205
|
+
text += ' '.repeat(n);
|
|
206
|
+
cells += n;
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
text += ch;
|
|
210
|
+
cells += cellWidth(cp);
|
|
211
|
+
}
|
|
212
|
+
byte += encoder.encode(ch).length;
|
|
213
|
+
}
|
|
214
|
+
marks.push([byte, cells]);
|
|
215
|
+
const cellAt = (b) => { let c = 0; for (const [mb, mc] of marks) {
|
|
216
|
+
if (mb > b)
|
|
217
|
+
break;
|
|
218
|
+
c = mc;
|
|
219
|
+
} return c; };
|
|
220
|
+
return { text, cellAt, width: cells };
|
|
221
|
+
}
|
|
222
|
+
/** Source registry keyed by the file names used in diagnostics. */
|
|
223
|
+
export class SourceManager {
|
|
224
|
+
files = new Map();
|
|
225
|
+
add(name, text) { const file = new SourceFile(name, text); this.files.set(name, file); return file; }
|
|
226
|
+
get(name) { return name === undefined ? undefined : this.files.get(name); }
|
|
227
|
+
merge(other) { if (other)
|
|
228
|
+
for (const [name, file] of other.files)
|
|
229
|
+
if (!this.files.has(name))
|
|
230
|
+
this.files.set(name, file); return this; }
|
|
231
|
+
}
|
|
232
|
+
export function diagnostic(code, message, primary, extra = {}) {
|
|
233
|
+
return { severity: extra.severity ?? 'error', code, message, ...(primary ? { primary } : {}), labels: extra.labels ?? [], notes: extra.notes ?? [],
|
|
234
|
+
...(extra.help !== undefined ? { help: extra.help } : {}), related: extra.related ?? [] };
|
|
235
|
+
}
|
|
236
|
+
/** Location of a span, taking the file from the span's own identity when present. */
|
|
237
|
+
export function at(span, fallbackFile = '<source>') {
|
|
238
|
+
return span ? { file: span.file ?? fallbackFile, span } : { file: fallbackFile };
|
|
239
|
+
}
|
|
240
|
+
function key(d) {
|
|
241
|
+
return `${d.code}|${d.primary?.file ?? ''}|${d.primary?.span?.start ?? ''}|${d.primary?.span?.end ?? ''}|${d.message}`;
|
|
242
|
+
}
|
|
243
|
+
/** Order-preserving de-duplication (same code, primary location and message) and capping. */
|
|
244
|
+
export function dedupe(list, cap = MAX_DIAGNOSTICS) {
|
|
245
|
+
const seen = new Set();
|
|
246
|
+
const out = [];
|
|
247
|
+
for (const d of list) {
|
|
248
|
+
const k = key(d);
|
|
249
|
+
if (seen.has(k))
|
|
250
|
+
continue;
|
|
251
|
+
seen.add(k);
|
|
252
|
+
out.push(d);
|
|
253
|
+
}
|
|
254
|
+
return out.length > cap ? out.slice(0, cap) : out;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Error thrown by the compiler APIs. `diagnostics` holds every collected diagnostic
|
|
258
|
+
* (first = the one that stopped compilation); `message` is a one-line
|
|
259
|
+
* `file:line:col: error[CODE]: message` header for the first.
|
|
260
|
+
*/
|
|
261
|
+
export class CompileError extends Error {
|
|
262
|
+
diagnostics;
|
|
263
|
+
sources;
|
|
264
|
+
constructor(diagnostics, sources) {
|
|
265
|
+
const list = dedupe(diagnostics);
|
|
266
|
+
super(headline(list[0], sources) + (list.length > 1 ? ` (and ${list.length - 1} more error${list.length > 2 ? 's' : ''})` : ''));
|
|
267
|
+
this.name = 'CompileError';
|
|
268
|
+
this.diagnostics = list;
|
|
269
|
+
this.sources = sources ?? new SourceManager();
|
|
270
|
+
Object.defineProperty(this, 'sources', { enumerable: false });
|
|
271
|
+
}
|
|
272
|
+
get code() { return this.diagnostics[0].code; }
|
|
273
|
+
/** Full rendered text of all diagnostics. */
|
|
274
|
+
render(options = {}) { return renderDiagnostics(this.diagnostics, this.sources, options); }
|
|
275
|
+
toJSON() { return this.diagnostics.map(d => diagnosticToJson(d, this.sources)); }
|
|
276
|
+
}
|
|
277
|
+
/** Merge diagnostics from CompileErrors (others rethrown as-is by callers). */
|
|
278
|
+
export function mergeErrors(errors, sources) {
|
|
279
|
+
const merged = new SourceManager();
|
|
280
|
+
if (sources)
|
|
281
|
+
merged.merge(sources);
|
|
282
|
+
for (const e of errors)
|
|
283
|
+
merged.merge(e.sources);
|
|
284
|
+
return new CompileError(errors.flatMap(e => e.diagnostics), merged);
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Collects diagnostics for one compile. Recovery may continue until
|
|
288
|
+
* `MAX_DIAGNOSTICS`; the next report then throws, and the first diagnostic is kept.
|
|
289
|
+
* `raise` records one more diagnostic (unless the cap is already full) and stops.
|
|
290
|
+
*/
|
|
291
|
+
export class DiagnosticBag {
|
|
292
|
+
sources;
|
|
293
|
+
fallbackFile;
|
|
294
|
+
diagnostics = [];
|
|
295
|
+
constructor(sources, fallbackFile = '<source>') {
|
|
296
|
+
this.sources = sources;
|
|
297
|
+
this.fallbackFile = fallbackFile;
|
|
298
|
+
}
|
|
299
|
+
report(code, message, span, extra = {}) {
|
|
300
|
+
if (this.diagnostics.length >= MAX_DIAGNOSTICS)
|
|
301
|
+
this.finish();
|
|
302
|
+
this.diagnostics.push(diagnostic(code, message, span ? at(span, this.fallbackFile) : undefined, extra));
|
|
303
|
+
if (this.diagnostics.length >= MAX_DIAGNOSTICS)
|
|
304
|
+
this.finish();
|
|
305
|
+
}
|
|
306
|
+
raise(code, message, span, extra = {}) {
|
|
307
|
+
this.report(code, message, span, extra);
|
|
308
|
+
this.finish();
|
|
309
|
+
}
|
|
310
|
+
finish() { throw new CompileError(this.diagnostics, this.sources); }
|
|
311
|
+
/** Throw if any diagnostic was recorded and compilation continued. */
|
|
312
|
+
checkpoint() { if (this.diagnostics.length)
|
|
313
|
+
this.finish(); }
|
|
314
|
+
}
|
|
315
|
+
function where(loc, sources) {
|
|
316
|
+
if (!loc)
|
|
317
|
+
return '';
|
|
318
|
+
if (!loc.span)
|
|
319
|
+
return `${loc.file}: `;
|
|
320
|
+
const file = sources?.get(loc.file);
|
|
321
|
+
if (!file)
|
|
322
|
+
return `${loc.file}:${loc.span.start}..${loc.span.end}: `;
|
|
323
|
+
const p = file.position(loc.span.start);
|
|
324
|
+
return `${loc.file}:${p.line}:${p.column}: `;
|
|
325
|
+
}
|
|
326
|
+
export function headline(d, sources) {
|
|
327
|
+
return `${where(d.primary, sources)}${d.severity}[${d.code}]: ${d.message}`;
|
|
328
|
+
}
|
|
329
|
+
const ANSI = { error: '1;31', warning: '1;33', note: '1;36', help: '1;32', gutter: '1;34', bold: '1', secondary: '1;34' };
|
|
330
|
+
/** Whether to colour output for a stream: TTY and NO_COLOR unset (FORCE_COLOR forces). */
|
|
331
|
+
export function shouldColor(stream, env = process.env) {
|
|
332
|
+
if (env.NO_COLOR !== undefined && env.NO_COLOR !== '')
|
|
333
|
+
return false;
|
|
334
|
+
if (env.FORCE_COLOR !== undefined && env.FORCE_COLOR !== '0')
|
|
335
|
+
return true;
|
|
336
|
+
return !!stream.isTTY;
|
|
337
|
+
}
|
|
338
|
+
export function renderDiagnostics(list, sources = new SourceManager(), options = {}) {
|
|
339
|
+
const out = list.map(d => renderDiagnostic(d, sources, options));
|
|
340
|
+
const errors = list.filter(d => d.severity === 'error').length;
|
|
341
|
+
if (errors > 1)
|
|
342
|
+
out.push(paintWith(options)('bold', `${errors} errors generated.`) + '\n');
|
|
343
|
+
return out.join('');
|
|
344
|
+
}
|
|
345
|
+
function paintWith(options) {
|
|
346
|
+
return (style, text) => options.color ? `\x1b[${ANSI[style]}m${text}\x1b[0m` : text;
|
|
347
|
+
}
|
|
348
|
+
export function renderDiagnostic(d, sources = new SourceManager(), options = {}) {
|
|
349
|
+
const paint = paintWith(options);
|
|
350
|
+
const lines = [];
|
|
351
|
+
lines.push(`${paint('bold', where(d.primary, sources))}${paint(d.severity, `${d.severity}[${d.code}]`)}${paint('bold', `: ${d.message}`)}`);
|
|
352
|
+
const marks = [];
|
|
353
|
+
if (d.primary?.span) {
|
|
354
|
+
const own = d.labels.find(l => l.primary);
|
|
355
|
+
marks.push({
|
|
356
|
+
file: own?.file ?? own?.span.file ?? d.primary.file,
|
|
357
|
+
span: own?.span ?? d.primary.span,
|
|
358
|
+
message: own?.message ?? '',
|
|
359
|
+
primary: true,
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
for (const l of d.labels)
|
|
363
|
+
if (!l.primary)
|
|
364
|
+
marks.push({ file: l.file ?? l.span.file ?? d.primary?.file ?? '<source>', span: l.span, message: l.message, primary: false });
|
|
365
|
+
const files = [...new Set(marks.map(m => m.file))];
|
|
366
|
+
const gutter = Math.max(1, ...marks.map(m => { const f = sources.get(m.file); return f ? String(f.position(m.span.end).line).length : 1; }));
|
|
367
|
+
for (const name of files) {
|
|
368
|
+
const file = sources.get(name);
|
|
369
|
+
const group = marks.filter(m => m.file === name);
|
|
370
|
+
if (!file) {
|
|
371
|
+
for (const m of group)
|
|
372
|
+
if (m.message || !m.primary)
|
|
373
|
+
lines.push(`${' '.repeat(gutter)} ${paint('gutter', '=')} ${m.primary ? 'at' : 'see'} ${name}:${m.span.start}..${m.span.end}${m.message ? `: ${m.message}` : ''}`);
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
if (name !== d.primary?.file) {
|
|
377
|
+
const p = file.position(group[0].span.start);
|
|
378
|
+
lines.push(`${' '.repeat(gutter)}${paint('gutter', '::: ')}${name}:${p.line}:${p.column}`);
|
|
379
|
+
}
|
|
380
|
+
lines.push(...excerpt(file, group, gutter, paint));
|
|
381
|
+
}
|
|
382
|
+
for (const n of d.notes)
|
|
383
|
+
lines.push(`${' '.repeat(gutter)} ${paint('gutter', '=')} ${paint('bold', 'note')}: ${n}`);
|
|
384
|
+
if (d.help !== undefined)
|
|
385
|
+
lines.push(`${' '.repeat(gutter)} ${paint('gutter', '=')} ${paint('help', 'help')}: ${d.help}`);
|
|
386
|
+
let text = lines.join('\n') + '\n';
|
|
387
|
+
for (const r of d.related)
|
|
388
|
+
text += renderDiagnostic({ severity: 'note', code: '', message: r.message, primary: { file: r.file, ...(r.span ? { span: r.span } : {}) }, labels: [], notes: [], related: [] }, sources, options).replace('note[]', 'note');
|
|
389
|
+
return text;
|
|
390
|
+
}
|
|
391
|
+
const MAX_SPAN_LINES = 6;
|
|
392
|
+
function excerpt(file, marks, gutter, paint) {
|
|
393
|
+
const rows = [];
|
|
394
|
+
const shown = new Set();
|
|
395
|
+
for (const m of marks) {
|
|
396
|
+
const first = file.lineIndex(m.span.start);
|
|
397
|
+
const lastLine = m.span.end > m.span.start ? file.lineIndex(m.span.end - 1) : first;
|
|
398
|
+
const touched = [];
|
|
399
|
+
for (let l = first; l <= lastLine; l++)
|
|
400
|
+
if (lastLine - first < MAX_SPAN_LINES || l < first + 2 || l > lastLine - 2)
|
|
401
|
+
touched.push(l);
|
|
402
|
+
for (const l of touched) {
|
|
403
|
+
const start = file.lineStarts[l], text = file.lineText(l), lineEnd = start + encoder.encode(text).length;
|
|
404
|
+
let from = l === first ? m.span.start - start : encoder.encode(text).length - encoder.encode(text.trimStart()).length;
|
|
405
|
+
let to = l === lastLine ? Math.min(m.span.end, lineEnd) - start : lineEnd - start;
|
|
406
|
+
if (to < from)
|
|
407
|
+
to = from;
|
|
408
|
+
rows.push({ line: l, mark: m, from, to, last: l === lastLine });
|
|
409
|
+
shown.add(l);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
const out = [];
|
|
413
|
+
const bar = paint('gutter', '|');
|
|
414
|
+
out.push(`${' '.repeat(gutter)} ${bar}`);
|
|
415
|
+
let previous = -1;
|
|
416
|
+
for (const l of [...shown].sort((a, b) => a - b)) {
|
|
417
|
+
if (previous >= 0 && l > previous + 1)
|
|
418
|
+
out.push(paint('gutter', '...'));
|
|
419
|
+
previous = l;
|
|
420
|
+
const view = layout(file.lineText(l));
|
|
421
|
+
out.push(`${paint('gutter', String(l + 1).padStart(gutter))} ${bar} ${view.text}`.trimEnd());
|
|
422
|
+
const here = rows.filter(r => r.line === l).sort((a, b) => Number(b.mark.primary) - Number(a.mark.primary) || a.from - b.from);
|
|
423
|
+
for (const r of here) {
|
|
424
|
+
const startCell = view.cellAt(r.from), endCell = Math.max(view.cellAt(r.to), startCell + 1);
|
|
425
|
+
const len = endCell - startCell;
|
|
426
|
+
const firstRow = r.line === file.lineIndex(r.mark.span.start);
|
|
427
|
+
const underline = r.mark.primary ? (firstRow ? '^' : '~') + '~'.repeat(len - 1) : '-'.repeat(len);
|
|
428
|
+
const message = r.last && r.mark.message ? ` ${r.mark.message}` : '';
|
|
429
|
+
out.push(`${' '.repeat(gutter)} ${bar} ${' '.repeat(startCell)}${paint(r.mark.primary ? 'error' : 'secondary', underline + message)}`);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return out;
|
|
433
|
+
}
|
|
434
|
+
// ---------------------------------------------------------------------------
|
|
435
|
+
// JSON form (schema lotus/diagnostics/1)
|
|
436
|
+
export const DIAGNOSTICS_SCHEMA = 'lotus/diagnostics/1';
|
|
437
|
+
function jsonLocation(file, span, sources) {
|
|
438
|
+
if (!span)
|
|
439
|
+
return { file };
|
|
440
|
+
const src = sources?.get(file);
|
|
441
|
+
const base = { file, start: span.start, end: span.end };
|
|
442
|
+
if (src) {
|
|
443
|
+
const a = src.position(span.start), b = src.position(span.end);
|
|
444
|
+
Object.assign(base, { line: a.line, column: a.column, endLine: b.line, endColumn: b.column });
|
|
445
|
+
}
|
|
446
|
+
if (span.module !== undefined)
|
|
447
|
+
base.module = span.module;
|
|
448
|
+
return base;
|
|
449
|
+
}
|
|
450
|
+
/** Stable JSON (fixed key order). `column`/`endColumn` are 1-based Unicode-scalar columns; `end` is exclusive. */
|
|
451
|
+
export function diagnosticToJson(d, sources) {
|
|
452
|
+
return {
|
|
453
|
+
severity: d.severity, code: d.code, message: d.message,
|
|
454
|
+
primary: d.primary ? jsonLocation(d.primary.file, d.primary.span, sources) : null,
|
|
455
|
+
labels: d.labels.map(l => ({ ...jsonLocation(l.file ?? l.span.file ?? d.primary?.file ?? '<source>', l.span, sources), message: l.message, primary: !!l.primary })),
|
|
456
|
+
notes: [...d.notes], help: d.help ?? null,
|
|
457
|
+
related: d.related.map(r => ({ ...jsonLocation(r.file, r.span, sources), message: r.message })),
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
export function diagnosticsJson(list, sources) {
|
|
461
|
+
return JSON.stringify({ schema: DIAGNOSTICS_SCHEMA, diagnostics: list.map(d => diagnosticToJson(d, sources)) }, null, 2) + '\n';
|
|
462
|
+
}
|
|
463
|
+
/** Levenshtein-based suggestion: closest candidate within distance max(1, len/3). Ties break lexicographically. */
|
|
464
|
+
export function suggest(name, candidates) {
|
|
465
|
+
const limit = Math.max(1, Math.floor(name.length / 3));
|
|
466
|
+
let best, bestDistance = Infinity;
|
|
467
|
+
for (const c of [...new Set(candidates)].sort()) {
|
|
468
|
+
if (c === name || Math.abs(c.length - name.length) > limit)
|
|
469
|
+
continue;
|
|
470
|
+
// A case-only difference ranks ahead of any real edit.
|
|
471
|
+
const score = name.toLowerCase() === c.toLowerCase() ? 0.5 : distance(name, c);
|
|
472
|
+
if (score <= limit && score < bestDistance) {
|
|
473
|
+
best = c;
|
|
474
|
+
bestDistance = score;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
return best;
|
|
478
|
+
}
|
|
479
|
+
function distance(a, b) {
|
|
480
|
+
const row = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
481
|
+
for (let i = 1; i <= a.length; i++) {
|
|
482
|
+
let prev = row[0];
|
|
483
|
+
row[0] = i;
|
|
484
|
+
for (let j = 1; j <= b.length; j++) {
|
|
485
|
+
const tmp = row[j];
|
|
486
|
+
row[j] = Math.min(row[j] + 1, row[j - 1] + 1, prev + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
487
|
+
prev = tmp;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
return row[b.length];
|
|
491
|
+
}
|
|
492
|
+
/** Render any thrown value (CompileError or plain Error) as diagnostics text or JSON. */
|
|
493
|
+
export function errorDiagnostics(error, fallbackFile) {
|
|
494
|
+
if (error instanceof CompileError)
|
|
495
|
+
return { diagnostics: error.diagnostics, sources: error.sources };
|
|
496
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
497
|
+
return { diagnostics: [diagnostic('E0000', message, fallbackFile !== undefined ? { file: fallbackFile } : undefined)], sources: new SourceManager() };
|
|
498
|
+
}
|
package/dist/ecash.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type SourceSpan, type TargetDiagnostic } from './targets.js';
|
|
2
|
+
export type EcashArtifact = {
|
|
3
|
+
schema: 'ecash/script-artifact/1';
|
|
4
|
+
target: 'ecash';
|
|
5
|
+
targetRevision: string;
|
|
6
|
+
targetProfile: string;
|
|
7
|
+
entrypoint: string;
|
|
8
|
+
args: {
|
|
9
|
+
name: string;
|
|
10
|
+
type: {
|
|
11
|
+
kind: 'bytes';
|
|
12
|
+
length: number;
|
|
13
|
+
} | {
|
|
14
|
+
kind: 'bool';
|
|
15
|
+
};
|
|
16
|
+
span: SourceSpan;
|
|
17
|
+
}[];
|
|
18
|
+
scriptHex: string;
|
|
19
|
+
sourceMap: {
|
|
20
|
+
op: string;
|
|
21
|
+
source: SourceSpan;
|
|
22
|
+
byteStart: number;
|
|
23
|
+
byteEnd: number;
|
|
24
|
+
}[];
|
|
25
|
+
/** Bounds assume exactly the declared initial args; CLEANSTACK rejects extras. */
|
|
26
|
+
staticResources: {
|
|
27
|
+
scriptBytes: number;
|
|
28
|
+
opCount: number;
|
|
29
|
+
maxStackItems: number;
|
|
30
|
+
maxElementBytes: number;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
export type EcashResult = {
|
|
34
|
+
ok: true;
|
|
35
|
+
artifact: EcashArtifact;
|
|
36
|
+
} | {
|
|
37
|
+
ok: false;
|
|
38
|
+
diagnostics: TargetDiagnostic[];
|
|
39
|
+
};
|
|
40
|
+
/** Validate the complete input and capabilities before creating any wire bytes. */
|
|
41
|
+
export declare function compileEcash(input: unknown): EcashResult;
|