@hyperscale0/hsx 1.0.0-alpha.6 → 1.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +48 -10
- package/LICENSING.md +3 -3
- package/README.md +37 -149
- package/TRADEMARKS.md +4 -4
- package/dist/src/ast.d.ts +129 -10
- package/dist/src/ast.d.ts.map +1 -1
- package/dist/src/cli.d.ts +2 -1
- package/dist/src/cli.d.ts.map +1 -1
- package/dist/src/cli.js +148 -12
- package/dist/src/cli.js.map +1 -1
- package/dist/src/compile.d.ts +41 -4
- package/dist/src/compile.d.ts.map +1 -1
- package/dist/src/compile.js +82 -15
- package/dist/src/compile.js.map +1 -1
- package/dist/src/cost.d.ts +78 -0
- package/dist/src/cost.d.ts.map +1 -0
- package/dist/src/cost.js +369 -0
- package/dist/src/cost.js.map +1 -0
- package/dist/src/diagnostics.d.ts +11 -0
- package/dist/src/diagnostics.d.ts.map +1 -0
- package/dist/src/diagnostics.js +420 -0
- package/dist/src/diagnostics.js.map +1 -0
- package/dist/src/emit.d.ts +35 -0
- package/dist/src/emit.d.ts.map +1 -0
- package/dist/src/emit.js +359 -0
- package/dist/src/emit.js.map +1 -0
- package/dist/src/entry-overrides.d.ts +17 -3
- package/dist/src/entry-overrides.d.ts.map +1 -1
- package/dist/src/entry-overrides.js +180 -35
- package/dist/src/entry-overrides.js.map +1 -1
- package/dist/src/format.d.ts +10 -0
- package/dist/src/format.d.ts.map +1 -0
- package/dist/src/format.js +183 -0
- package/dist/src/format.js.map +1 -0
- package/dist/src/index.d.ts +10 -4
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +8 -3
- package/dist/src/index.js.map +1 -1
- package/dist/src/ir.d.ts +66 -0
- package/dist/src/ir.d.ts.map +1 -0
- package/dist/src/ir.js +15 -0
- package/dist/src/ir.js.map +1 -0
- package/dist/src/lex.d.ts +9 -1
- package/dist/src/lex.d.ts.map +1 -1
- package/dist/src/lex.js +50 -4
- package/dist/src/lex.js.map +1 -1
- package/dist/src/limits.d.ts +2 -3
- package/dist/src/limits.d.ts.map +1 -1
- package/dist/src/limits.js +2 -3
- package/dist/src/limits.js.map +1 -1
- package/dist/src/modules.d.ts +36 -0
- package/dist/src/modules.d.ts.map +1 -0
- package/dist/src/modules.js +372 -0
- package/dist/src/modules.js.map +1 -0
- package/dist/src/parse.d.ts.map +1 -1
- package/dist/src/parse.js +590 -17
- package/dist/src/parse.js.map +1 -1
- package/dist/src/typecheck.d.ts +8 -0
- package/dist/src/typecheck.d.ts.map +1 -0
- package/dist/src/typecheck.js +2958 -0
- package/dist/src/typecheck.js.map +1 -0
- package/dist/src/version.d.ts +4 -15
- package/dist/src/version.d.ts.map +1 -1
- package/dist/src/version.js +4 -15
- package/dist/src/version.js.map +1 -1
- package/docs/README.md +23 -0
- package/docs/guide/01-first-program.md +24 -0
- package/docs/guide/02-money.md +25 -0
- package/docs/guide/03-instruments.md +22 -0
- package/docs/guide/04-lifecycles.md +25 -0
- package/docs/guide/05-fees-and-splits.md +45 -0
- package/docs/guide/06-schedules.md +22 -0
- package/docs/guide/07-composition.md +46 -0
- package/docs/guide/08-writing-a-module.md +26 -0
- package/docs/guide/09-cost.md +24 -0
- package/docs/guide/10-diagnostics.md +27 -0
- package/docs/llms-full.txt +1738 -0
- package/docs/llms.txt +45 -0
- package/docs/reference/cli.md +33 -0
- package/docs/reference/diagnostics.md +619 -0
- package/docs/reference/grammar.md +39 -0
- package/docs/reference/std/advance.md +35 -0
- package/docs/reference/std/cancellable_booking.md +35 -0
- package/docs/reference/std/captured_payment.md +43 -0
- package/docs/reference/std/conditional_disbursement.md +33 -0
- package/docs/reference/std/credit_facility.md +35 -0
- package/docs/reference/std/held_payment.md +64 -0
- package/docs/reference/std/instant_transfer.md +30 -0
- package/docs/reference/std/metered.md +27 -0
- package/docs/reference/std/pooled_split.md +26 -0
- package/docs/reference/std/premium_forward.md +39 -0
- package/docs/reference/std/reconciled_payout.md +33 -0
- package/docs/reference/std/recurring_collection.md +25 -0
- package/docs/reference/std/rotating_pool.md +42 -0
- package/docs/reference/std/scheduled.md +51 -0
- package/docs/reference/std/security_deposit.md +46 -0
- package/docs/reference/std/settlement_batch.md +42 -0
- package/docs/reference/std/swap.md +46 -0
- package/docs/reference/std/threshold_pool.md +47 -0
- package/docs/reference/std/weighted_distribution.md +39 -0
- package/docs/reference/types.md +20 -0
- package/docs/reference/udl-output.md +14 -0
- package/docs/sessions/2026-09-02-two-instruments.hsx +21 -0
- package/docs/sessions/2026-09-02-two-instruments.md +41 -0
- package/examples/01-first-program/README.md +45 -0
- package/examples/01-first-program/tip-jar.hsx +17 -0
- package/examples/02-imports-and-archetypes/README.md +99 -0
- package/examples/02-imports-and-archetypes/photo-booth.hsx +37 -0
- package/examples/03-diagnostics/README.md +68 -0
- package/examples/03-diagnostics/corner-shop-fixed.hsx +22 -0
- package/examples/03-diagnostics/corner-shop.hsx +19 -0
- package/examples/04-complete-product/README.md +87 -0
- package/examples/04-complete-product/study-hall.hsx +67 -0
- package/examples/05-watch-club/README.md +9 -0
- package/examples/05-watch-club/watch-club.hsx +124 -0
- package/examples/README.md +18 -0
- package/examples/advance/README.md +3 -0
- package/examples/advance/advance.hsx +13 -0
- package/examples/advance/advance.udl +339 -0
- package/examples/cancellable_booking/README.md +3 -0
- package/examples/cancellable_booking/cancellable_booking.hsx +14 -0
- package/examples/cancellable_booking/cancellable_booking.udl +391 -0
- package/examples/captured_payment/README.md +3 -0
- package/examples/captured_payment/captured_payment.hsx +21 -0
- package/examples/captured_payment/captured_payment.udl +677 -0
- package/examples/conditional_disbursement/README.md +3 -0
- package/examples/conditional_disbursement/conditional_disbursement.hsx +17 -0
- package/examples/conditional_disbursement/conditional_disbursement.udl +412 -0
- package/examples/cost-table.json +386 -0
- package/examples/credit_facility/README.md +3 -0
- package/examples/credit_facility/credit_facility.hsx +27 -0
- package/examples/credit_facility/credit_facility.udl +1373 -0
- package/examples/held_payment/README.md +3 -0
- package/examples/held_payment/held_payment.hsx +13 -0
- package/examples/held_payment/held_payment.udl +507 -0
- package/examples/instant_transfer/README.md +3 -0
- package/examples/instant_transfer/instant_transfer.hsx +16 -0
- package/examples/instant_transfer/instant_transfer.udl +364 -0
- package/examples/metered/README.md +3 -0
- package/examples/metered/metered.hsx +13 -0
- package/examples/metered/metered.udl +247 -0
- package/examples/pooled_split/README.md +3 -0
- package/examples/pooled_split/pooled_split.hsx +15 -0
- package/examples/pooled_split/pooled_split.udl +425 -0
- package/examples/premium_forward/README.md +3 -0
- package/examples/premium_forward/premium_forward.hsx +19 -0
- package/examples/premium_forward/premium_forward.udl +594 -0
- package/examples/reconciled_payout/README.md +3 -0
- package/examples/reconciled_payout/reconciled_payout.hsx +13 -0
- package/examples/reconciled_payout/reconciled_payout.udl +303 -0
- package/examples/recurring_collection/README.md +3 -0
- package/examples/recurring_collection/recurring_collection.hsx +21 -0
- package/examples/recurring_collection/recurring_collection.udl +1130 -0
- package/examples/rotating_pool/README.md +3 -0
- package/examples/rotating_pool/rotating_pool.hsx +18 -0
- package/examples/rotating_pool/rotating_pool.udl +4352 -0
- package/examples/scheduled/README.md +3 -0
- package/examples/scheduled/scheduled.hsx +12 -0
- package/examples/scheduled/scheduled.udl +335 -0
- package/examples/security_deposit/README.md +3 -0
- package/examples/security_deposit/security_deposit.hsx +21 -0
- package/examples/security_deposit/security_deposit.udl +293 -0
- package/examples/settlement_batch/README.md +3 -0
- package/examples/settlement_batch/settlement_batch.hsx +19 -0
- package/examples/settlement_batch/settlement_batch.udl +753 -0
- package/examples/swap/README.md +3 -0
- package/examples/swap/swap.hsx +19 -0
- package/examples/swap/swap.udl +876 -0
- package/examples/threshold_pool/README.md +3 -0
- package/examples/threshold_pool/threshold_pool.hsx +16 -0
- package/examples/threshold_pool/threshold_pool.udl +667 -0
- package/examples/weighted_distribution/README.md +3 -0
- package/examples/weighted_distribution/weighted_distribution.hsx +20 -0
- package/examples/weighted_distribution/weighted_distribution.udl +337 -0
- package/package.json +19 -6
- package/skills/hsx/SKILL.md +483 -0
- package/src/ast.ts +163 -9
- package/src/cli.ts +164 -12
- package/src/compile.ts +160 -20
- package/src/cost.ts +579 -0
- package/src/diagnostics.ts +442 -0
- package/src/emit.ts +447 -0
- package/src/entry-overrides.ts +263 -48
- package/src/format.ts +261 -0
- package/src/index.ts +32 -4
- package/src/ir.ts +99 -0
- package/src/lex.ts +59 -4
- package/src/limits.ts +2 -3
- package/src/modules.ts +444 -0
- package/src/parse.ts +655 -21
- package/src/typecheck.ts +3876 -0
- package/src/version.ts +4 -16
- package/std/SEMANTICS.md +130 -0
- package/std/settlements/advance.hsx +177 -0
- package/std/settlements/cancellable_booking.hsx +87 -0
- package/std/settlements/captured_payment.hsx +144 -0
- package/std/settlements/conditional_disbursement.hsx +185 -0
- package/std/settlements/credit_facility.hsx +167 -0
- package/std/settlements/held_payment.hsx +578 -0
- package/std/settlements/index.hsx +3 -0
- package/std/settlements/instant_transfer.hsx +190 -0
- package/std/settlements/metered.hsx +67 -0
- package/std/settlements/pooled_split.hsx +68 -0
- package/std/settlements/premium_forward.hsx +257 -0
- package/std/settlements/reconciled_payout.hsx +77 -0
- package/std/settlements/recurring_collection.hsx +27 -0
- package/std/settlements/rotating_pool.hsx +252 -0
- package/std/settlements/scheduled.hsx +641 -0
- package/std/settlements/security_deposit.hsx +166 -0
- package/std/settlements/settlement_batch.hsx +122 -0
- package/std/settlements/swap.hsx +1114 -0
- package/std/settlements/threshold_pool.hsx +221 -0
- package/std/settlements/weighted_distribution.hsx +141 -0
- package/dist/src/archetypes.d.ts +0 -17
- package/dist/src/archetypes.d.ts.map +0 -1
- package/dist/src/archetypes.js +0 -327
- package/dist/src/archetypes.js.map +0 -1
- package/dist/src/check.d.ts +0 -12
- package/dist/src/check.d.ts.map +0 -1
- package/dist/src/check.js +0 -1910
- package/dist/src/check.js.map +0 -1
- package/dist/src/lower.d.ts +0 -146
- package/dist/src/lower.d.ts.map +0 -1
- package/dist/src/lower.js +0 -4570
- package/dist/src/lower.js.map +0 -1
- package/dist/src/model.d.ts +0 -467
- package/dist/src/model.d.ts.map +0 -1
- package/dist/src/model.js +0 -15
- package/dist/src/model.js.map +0 -1
- package/spec/hsx-ir.schema.json +0 -920
- package/src/archetypes.ts +0 -340
- package/src/check.ts +0 -2938
- package/src/lower.ts +0 -5416
- package/src/model.ts +0 -519
package/src/compile.ts
CHANGED
|
@@ -11,31 +11,75 @@
|
|
|
11
11
|
* Every diagnostic points at SOURCE coordinates, never at lowered IR paths.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
+
import type { UdlIssueCode } from "@hyperscale0/udl";
|
|
14
15
|
import { lineColIn, lineIndex } from "./ast.ts";
|
|
15
|
-
import {
|
|
16
|
-
|
|
16
|
+
import {
|
|
17
|
+
buildCostManifest,
|
|
18
|
+
type UdlCostManifest,
|
|
19
|
+
type UdlCostTable,
|
|
20
|
+
} from "./cost.ts";
|
|
21
|
+
import { lowerGeneralProgram, type OriginMapEntry } from "./emit.ts";
|
|
22
|
+
import {
|
|
23
|
+
resolveProgramModules,
|
|
24
|
+
type HsxCompilerHost,
|
|
25
|
+
type ModuleOrigin,
|
|
26
|
+
} from "./modules.ts";
|
|
17
27
|
import { parseProgram } from "./parse.ts";
|
|
28
|
+
import { checkGeneralProgram } from "./typecheck.ts";
|
|
18
29
|
|
|
19
30
|
type Json = Record<string, unknown>;
|
|
20
31
|
|
|
21
32
|
type CompileVerdict = "invalid" | "valid" | "warning";
|
|
22
33
|
|
|
23
|
-
interface CompileDiagnostic {
|
|
34
|
+
export interface CompileDiagnostic {
|
|
24
35
|
/** 1-indexed source column. */
|
|
25
36
|
readonly column: number;
|
|
37
|
+
/** Imported module file when the diagnostic belongs to module source. */
|
|
38
|
+
readonly file?: string;
|
|
26
39
|
/** 1-indexed source line. */
|
|
27
40
|
readonly line: number;
|
|
28
41
|
readonly message: string;
|
|
42
|
+
/** Source offsets in UTF-16 code units, matching parser and checker spans. */
|
|
43
|
+
readonly span: { readonly end: number; readonly start: number };
|
|
44
|
+
readonly code?: string;
|
|
45
|
+
readonly fix?: string;
|
|
46
|
+
/** Canonical UDL path when lowering rejected an emitted clause. */
|
|
47
|
+
readonly path?: string;
|
|
48
|
+
/** UDL validator category when the emitter rejected its candidate document. */
|
|
49
|
+
readonly udlCode?: UdlIssueCode;
|
|
29
50
|
readonly severity: "error" | "warning";
|
|
30
51
|
/** The stage that raised it: parse, check, or lower. */
|
|
31
|
-
readonly stage: "check" | "lower" | "parse";
|
|
52
|
+
readonly stage: "bind" | "check" | "lower" | "parse" | "typecheck";
|
|
32
53
|
}
|
|
33
54
|
|
|
34
55
|
interface CompileArtifacts {
|
|
35
|
-
/** The
|
|
56
|
+
/** The canonical UDL document. */
|
|
36
57
|
readonly document: Json;
|
|
37
58
|
/** The congruent Business Frame. */
|
|
38
59
|
readonly frame: Json;
|
|
60
|
+
/** Canonical UDL paths bound to their narrowest authored source terms. */
|
|
61
|
+
readonly originMap: readonly CompileOriginMapEntry[];
|
|
62
|
+
/** Deterministic compile-time cost, pinned to one versioned table. */
|
|
63
|
+
readonly costManifest: UdlCostManifest;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface CompileOptions extends HsxCompilerHost {
|
|
67
|
+
readonly composesCatalogBlueprint?: boolean;
|
|
68
|
+
readonly costTable?: UdlCostTable;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface CompileOriginMapEntry {
|
|
72
|
+
readonly path: string;
|
|
73
|
+
readonly span: {
|
|
74
|
+
/** Exclusive UTF-8 byte offset. */
|
|
75
|
+
readonly end: number;
|
|
76
|
+
/** 1-indexed source column. */
|
|
77
|
+
readonly column: number;
|
|
78
|
+
/** 1-indexed source line. */
|
|
79
|
+
readonly line: number;
|
|
80
|
+
/** Inclusive UTF-8 byte offset. */
|
|
81
|
+
readonly start: number;
|
|
82
|
+
};
|
|
39
83
|
}
|
|
40
84
|
|
|
41
85
|
export interface CompileResult {
|
|
@@ -45,23 +89,37 @@ export interface CompileResult {
|
|
|
45
89
|
readonly verdict: CompileVerdict;
|
|
46
90
|
}
|
|
47
91
|
|
|
48
|
-
export function compile(
|
|
92
|
+
export function compile(
|
|
93
|
+
source: string,
|
|
94
|
+
options: CompileOptions = {},
|
|
95
|
+
): CompileResult {
|
|
49
96
|
const diagnostics: CompileDiagnostic[] = [];
|
|
50
97
|
// Scanned once here rather than per diagnostic: a source can carry one
|
|
51
98
|
// diagnostic per two bytes, and rescanning from offset zero for each one
|
|
52
99
|
// made a 128 KB file cost 2.4 seconds of coordinate arithmetic.
|
|
53
100
|
const lines = lineIndex(source);
|
|
101
|
+
const byteOffsets = utf8OffsetIndex(source);
|
|
54
102
|
const at = (
|
|
55
103
|
stage: CompileDiagnostic["stage"],
|
|
56
104
|
severity: CompileDiagnostic["severity"],
|
|
57
105
|
message: string,
|
|
58
|
-
|
|
106
|
+
span: { readonly end: number; readonly start: number },
|
|
107
|
+
details?: {
|
|
108
|
+
readonly code: string;
|
|
109
|
+
readonly fix: string;
|
|
110
|
+
readonly path?: string;
|
|
111
|
+
readonly udlCode?: UdlIssueCode;
|
|
112
|
+
},
|
|
113
|
+
origin?: ModuleOrigin,
|
|
59
114
|
): void => {
|
|
60
|
-
const position = lineColIn(lines,
|
|
115
|
+
const position = origin ?? lineColIn(lines, span.start);
|
|
61
116
|
diagnostics.push({
|
|
62
117
|
column: position.column,
|
|
118
|
+
...(origin ? { file: origin.moduleName } : {}),
|
|
63
119
|
line: position.line,
|
|
64
120
|
message,
|
|
121
|
+
span: { ...span },
|
|
122
|
+
...(details ? details : {}),
|
|
65
123
|
severity,
|
|
66
124
|
stage,
|
|
67
125
|
});
|
|
@@ -69,34 +127,116 @@ export function compile(source: string): CompileResult {
|
|
|
69
127
|
|
|
70
128
|
const parsed = parseProgram(source);
|
|
71
129
|
for (const diagnostic of parsed.diagnostics) {
|
|
72
|
-
at(
|
|
130
|
+
at(
|
|
131
|
+
"parse",
|
|
132
|
+
"error",
|
|
133
|
+
diagnostic.message,
|
|
134
|
+
diagnostic.span,
|
|
135
|
+
diagnostic.code && diagnostic.fix
|
|
136
|
+
? { code: diagnostic.code, fix: diagnostic.fix }
|
|
137
|
+
: undefined,
|
|
138
|
+
);
|
|
73
139
|
}
|
|
74
140
|
if (parsed.diagnostics.length > 0) {
|
|
75
141
|
return { diagnostics, verdict: "invalid" };
|
|
76
142
|
}
|
|
77
143
|
|
|
78
|
-
const
|
|
144
|
+
const resolved = resolveProgramModules(parsed.program, options);
|
|
145
|
+
if (!resolved.ok) {
|
|
146
|
+
for (const issue of resolved.issues) {
|
|
147
|
+
at("bind", "error", issue.message, issue.span, {
|
|
148
|
+
code: issue.code,
|
|
149
|
+
fix: issue.fix,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
return { diagnostics, verdict: "invalid" };
|
|
153
|
+
}
|
|
154
|
+
const checked = checkGeneralProgram(
|
|
155
|
+
resolved.program,
|
|
156
|
+
options.publishedCatalog
|
|
157
|
+
? { publishedCatalog: options.publishedCatalog }
|
|
158
|
+
: {},
|
|
159
|
+
);
|
|
79
160
|
for (const diagnostic of checked.diagnostics) {
|
|
80
|
-
|
|
161
|
+
const moduleOrigin = resolved.origins.get(diagnostic.span);
|
|
162
|
+
at(
|
|
163
|
+
diagnostic.code === "HSX1001" ? "bind" : "typecheck",
|
|
164
|
+
diagnostic.severity,
|
|
165
|
+
diagnostic.message,
|
|
166
|
+
diagnostic.span,
|
|
167
|
+
{ code: diagnostic.code, fix: diagnostic.fix },
|
|
168
|
+
moduleOrigin,
|
|
169
|
+
);
|
|
81
170
|
}
|
|
82
|
-
if (!checked.program) {
|
|
171
|
+
if (!checked.program) return { diagnostics, verdict: "invalid" };
|
|
172
|
+
const cost = buildCostManifest(
|
|
173
|
+
checked.program,
|
|
174
|
+
options.costTable,
|
|
175
|
+
options.composesCatalogBlueprint ?? false,
|
|
176
|
+
);
|
|
177
|
+
if (!cost.ok) {
|
|
178
|
+
for (const diagnostic of cost.diagnostics) {
|
|
179
|
+
at(
|
|
180
|
+
"typecheck",
|
|
181
|
+
diagnostic.severity,
|
|
182
|
+
diagnostic.message,
|
|
183
|
+
diagnostic.span,
|
|
184
|
+
{ code: diagnostic.code, fix: diagnostic.fix },
|
|
185
|
+
);
|
|
186
|
+
}
|
|
83
187
|
return { diagnostics, verdict: "invalid" };
|
|
84
188
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
189
|
+
const lowered = lowerGeneralProgram(checked.program);
|
|
190
|
+
if (!lowered.ok) {
|
|
191
|
+
for (const issue of lowered.issues) {
|
|
192
|
+
at("lower", "error", issue.message, issue.span, {
|
|
193
|
+
code: issue.code,
|
|
194
|
+
fix: issue.fix,
|
|
195
|
+
path: issue.path,
|
|
196
|
+
...(issue.udlCode ? { udlCode: issue.udlCode } : {}),
|
|
197
|
+
});
|
|
90
198
|
}
|
|
91
199
|
return { diagnostics, verdict: "invalid" };
|
|
92
200
|
}
|
|
93
|
-
|
|
201
|
+
const compileOrigin = (entry: OriginMapEntry): CompileOriginMapEntry => {
|
|
202
|
+
const position = lineColIn(lines, entry.span.start);
|
|
203
|
+
return {
|
|
204
|
+
path: entry.path,
|
|
205
|
+
span: {
|
|
206
|
+
column: position.column,
|
|
207
|
+
end:
|
|
208
|
+
byteOffsets[entry.span.end] ?? byteOffsets[byteOffsets.length - 1]!,
|
|
209
|
+
line: position.line,
|
|
210
|
+
start: byteOffsets[entry.span.start] ?? 0,
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
};
|
|
94
214
|
return {
|
|
95
215
|
artifacts: {
|
|
96
|
-
document:
|
|
97
|
-
|
|
216
|
+
document: lowered.value.document,
|
|
217
|
+
costManifest: cost.manifest,
|
|
218
|
+
frame: lowered.value.frame,
|
|
219
|
+
originMap: lowered.value.originMap.map(compileOrigin),
|
|
98
220
|
},
|
|
99
221
|
diagnostics,
|
|
100
222
|
verdict: diagnostics.length > 0 ? "warning" : "valid",
|
|
101
223
|
};
|
|
102
224
|
}
|
|
225
|
+
|
|
226
|
+
function utf8OffsetIndex(source: string): Uint32Array {
|
|
227
|
+
const offsets = new Uint32Array(source.length + 1);
|
|
228
|
+
let bytes = 0;
|
|
229
|
+
for (let index = 0; index < source.length; index += 1) {
|
|
230
|
+
offsets[index] = bytes;
|
|
231
|
+
const codePoint = source.codePointAt(index) as number;
|
|
232
|
+
if (codePoint > 0xffff) {
|
|
233
|
+
offsets[index + 1] = bytes;
|
|
234
|
+
bytes += 4;
|
|
235
|
+
index += 1;
|
|
236
|
+
} else if (codePoint > 0x7ff) bytes += 3;
|
|
237
|
+
else if (codePoint > 0x7f) bytes += 2;
|
|
238
|
+
else bytes += 1;
|
|
239
|
+
}
|
|
240
|
+
offsets[source.length] = bytes;
|
|
241
|
+
return offsets;
|
|
242
|
+
}
|