yuku-parser 0.5.7 → 0.5.8

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 (3) hide show
  1. package/decode.js +82 -44
  2. package/index.js +1 -21
  3. package/package.json +12 -12
package/decode.js CHANGED
@@ -20,21 +20,33 @@ const TS_METHOD_SIGNATURE_KINDS = ["method", "get", "set"];
20
20
  const TS_MODULE_KINDS = ["namespace", "module"];
21
21
  const TS_MAPPED_OPTIONAL = [false, true, "+", "-"];
22
22
  const TS_MAPPED_READONLY = [null, true, "+", "-"];
23
- function buildPosMap(src, byteLen) {
24
- const m = new Uint32Array(byteLen + 1);
25
- let bp = 0, u16p = 0;
26
- for (let i = 0; i < src.length; i++) {
23
+ // builds a partial pos-map covering bytes [startByte, byteLen]. for any
24
+ // byte position v >= startByte, m[v - startByte] is the corresponding
25
+ // UTF-16 code-unit index into `src`. positions below startByte are an
26
+ // identity map (byte position == UTF-16 position because the prefix is
27
+ // pure ASCII), so the caller checks `v < startByte` and returns v directly.
28
+ function buildPosMap(src, byteLen, startByte) {
29
+ const m = new Uint32Array(byteLen - startByte + 1);
30
+ const len = src.length;
31
+ let bp = 0, u16p = startByte, i = startByte;
32
+ while (i < len) {
33
+ if (i + 16 <= len) {
34
+ let allAscii = true;
35
+ for (let k = 0; k < 16; k++) if (src.charCodeAt(i + k) >= 0x80) { allAscii = false; break; }
36
+ if (allAscii) {
37
+ for (let k = 0; k < 16; k++) m[bp + k] = u16p + k;
38
+ bp += 16; u16p += 16; i += 16;
39
+ continue;
40
+ }
41
+ }
42
+ const cu = src.charCodeAt(i);
27
43
  m[bp] = u16p;
28
- const cp = src.codePointAt(i);
29
- let bytes = 1;
30
- if (cp > 0x7F) { if (cp > 0x7FF) { bytes = cp > 0xFFFF ? 4 : 3; } else { bytes = 2; } }
31
- const units = bytes === 4 ? 2 : 1;
32
- u16p += units;
33
- for (let k = 1; k < bytes; k++) m[bp + k] = u16p;
34
- bp += bytes;
35
- if (bytes === 4) i++;
44
+ if (cu < 0x80) { bp++; u16p++; i++; }
45
+ else if (cu < 0x800) { m[bp + 1] = u16p + 1; bp += 2; u16p++; i++; }
46
+ else if (cu < 0xD800 || cu >= 0xE000) { m[bp + 1] = u16p + 1; m[bp + 2] = u16p + 1; bp += 3; u16p++; i++; }
47
+ else { m[bp + 1] = u16p + 1; m[bp + 2] = u16p + 2; m[bp + 3] = u16p + 2; bp += 4; u16p += 2; i += 2; }
36
48
  }
37
- m[byteLen] = u16p;
49
+ m[byteLen - startByte] = u16p;
38
50
  return m;
39
51
  }
40
52
  function decode(buffer, source) {
@@ -45,10 +57,9 @@ function decode(buffer, source) {
45
57
  const _srcLen = _u32[3];
46
58
  const nodeCount = _u32[0], extraCount = _u32[1], spLen = _u32[2];
47
59
  const commentCount = _u32[4], diagCount = _u32[5], progIdx = _u32[6];
48
- const _isTs = !!(_u32[7] & 2);
49
- const pm = (_u32[7] & 1) ? null : buildPosMap(source, _srcLen);
50
- const _p = pm ? (v => pm[v]) : (v => v);
51
- const _nodesOff = 32;
60
+ const _isTs = !!(_u32[7] & 1);
61
+ const _firstNa = _u32[8];
62
+ const _nodesOff = 36;
52
63
  const eOff = _nodesOff + nodeCount * 48;
53
64
  const _extraBase = eOff >> 2;
54
65
  const _spOff = eOff + extraCount * 4;
@@ -67,9 +78,25 @@ function decode(buffer, source) {
67
78
  }
68
79
  return r;
69
80
  }
70
- const str = pm
71
- ? ((s, e) => s === e ? "" : s < _srcLen ? _src.slice(pm[s], pm[e]) : _poolDecode(s, e))
72
- : ((s, e) => s === e ? "" : s < _srcLen ? _src.slice(s, e) : _poolDecode(s, e));
81
+ // `_p` and `str` translate UTF-8 byte offsets into JS UTF-16 indices.
82
+ // for any offset below `_firstNa` (the prefix of the source that is pure
83
+ // ASCII) the mapping is the identity, so we short-circuit and skip
84
+ // building a pos-map. when offsets cross into non-ASCII territory we
85
+ // lazily build a pos-map for `[_firstNa, _srcLen]` on first use.
86
+ let pm = null;
87
+ const _p = v => {
88
+ if (v < _firstNa) return v;
89
+ if (pm === null) pm = buildPosMap(_src, _srcLen, _firstNa);
90
+ return pm[v - _firstNa];
91
+ };
92
+ const str = (s, e) => {
93
+ if (s === e) return "";
94
+ if (s >= _srcLen) return _poolDecode(s, e);
95
+ if (e <= _firstNa) return _src.slice(s, e);
96
+ if (pm === null) pm = buildPosMap(_src, _srcLen, _firstNa);
97
+ const ss = s < _firstNa ? s : pm[s - _firstNa];
98
+ return _src.slice(ss, pm[e - _firstNa]);
99
+ };
73
100
  function nodeArr(s, len) {
74
101
  const r = new Array(len);
75
102
  for (let j = 0; j < len; j++) r[j] = node(_u32[_extraBase + s + j]);
@@ -274,33 +301,44 @@ function decode(buffer, source) {
274
301
  }
275
302
  const cOff = _spOff + spLen, dOff = cOff + commentCount * 20;
276
303
  const dv = new DataView(buffer);
277
- const comments = new Array(commentCount);
278
- for (let j = 0; j < commentCount; j++) {
279
- const o = cOff + j * 20;
280
- comments[j] = { type: COMMENT_TYPES[_u8[o]], value: str(dv.getUint32(o + 12, true), dv.getUint32(o + 16, true)), start: _p(dv.getUint32(o + 4, true)), end: _p(dv.getUint32(o + 8, true)) };
304
+ function _decodeComments() {
305
+ const out = new Array(commentCount);
306
+ for (let j = 0; j < commentCount; j++) {
307
+ const o = cOff + j * 20;
308
+ out[j] = { type: COMMENT_TYPES[_u8[o]], value: str(dv.getUint32(o + 12, true), dv.getUint32(o + 16, true)), start: _p(dv.getUint32(o + 4, true)), end: _p(dv.getUint32(o + 8, true)) };
309
+ }
310
+ return out;
281
311
  }
282
- const diagnostics = new Array(diagCount);
283
- let dp = dOff;
284
- for (let j = 0; j < diagCount; j++) {
285
- const sev = SEVERITY[_u8[dp]]; dp++;
286
- const ds = _p(dv.getUint32(dp, true)); dp += 4;
287
- const de = _p(dv.getUint32(dp, true)); dp += 4;
288
- const ml = dv.getUint32(dp, true); dp += 4;
289
- const msg = _td.decode(_u8.subarray(dp, dp + ml)); dp += ml;
290
- const hh = _u8[dp]; dp++;
291
- let help = null;
292
- if (hh) { const hl = dv.getUint32(dp, true); dp += 4; help = _td.decode(_u8.subarray(dp, dp + hl)); dp += hl; }
293
- const lc = dv.getUint32(dp, true); dp += 4;
294
- const labels = new Array(lc);
295
- for (let k = 0; k < lc; k++) {
296
- const ls = _p(dv.getUint32(dp, true)); dp += 4;
297
- const le = _p(dv.getUint32(dp, true)); dp += 4;
298
- const lml = dv.getUint32(dp, true); dp += 4;
299
- labels[k] = { start: ls, end: le, message: _td.decode(_u8.subarray(dp, dp + lml)) }; dp += lml;
312
+ function _decodeDiagnostics() {
313
+ const out = new Array(diagCount);
314
+ let dp = dOff;
315
+ for (let j = 0; j < diagCount; j++) {
316
+ const sev = SEVERITY[_u8[dp]]; dp++;
317
+ const ds = _p(dv.getUint32(dp, true)); dp += 4;
318
+ const de = _p(dv.getUint32(dp, true)); dp += 4;
319
+ const ml = dv.getUint32(dp, true); dp += 4;
320
+ const msg = _td.decode(_u8.subarray(dp, dp + ml)); dp += ml;
321
+ const hh = _u8[dp]; dp++;
322
+ let help = null;
323
+ if (hh) { const hl = dv.getUint32(dp, true); dp += 4; help = _td.decode(_u8.subarray(dp, dp + hl)); dp += hl; }
324
+ const lc = dv.getUint32(dp, true); dp += 4;
325
+ const labels = new Array(lc);
326
+ for (let k = 0; k < lc; k++) {
327
+ const ls = _p(dv.getUint32(dp, true)); dp += 4;
328
+ const le = _p(dv.getUint32(dp, true)); dp += 4;
329
+ const lml = dv.getUint32(dp, true); dp += 4;
330
+ labels[k] = { start: ls, end: le, message: _td.decode(_u8.subarray(dp, dp + lml)) }; dp += lml;
331
+ }
332
+ out[j] = { severity: sev, message: msg, start: ds, end: de, help, labels };
300
333
  }
301
- diagnostics[j] = { severity: sev, message: msg, start: ds, end: de, help, labels };
334
+ return out;
302
335
  }
303
336
  function _def(o, k, v) { Object.defineProperty(o, k, { value: v, writable: true, enumerable: true, configurable: true }); }
304
- return { program: node(progIdx), comments, diagnostics };
337
+ let _program, _comments, _diagnostics;
338
+ return {
339
+ get program() { return _program !== undefined ? _program : (_program = node(progIdx)); },
340
+ get comments() { return _comments !== undefined ? _comments : (_comments = _decodeComments()); },
341
+ get diagnostics() { return _diagnostics !== undefined ? _diagnostics : (_diagnostics = _decodeDiagnostics()); },
342
+ };
305
343
  }
306
344
  export { decode };
package/index.js CHANGED
@@ -2,27 +2,7 @@ import binding from "./binding.js";
2
2
  import { decode } from "./decode.js";
3
3
 
4
4
  export function parse(source, options) {
5
- const buffer = binding.parse(source, options ?? {});
6
- let decoded = null;
7
-
8
- const getDecoded = () => {
9
- if (decoded === null) {
10
- decoded = decode(buffer, source);
11
- }
12
- return decoded;
13
- };
14
-
15
- return {
16
- get program() {
17
- return getDecoded().program;
18
- },
19
- get comments() {
20
- return getDecoded().comments;
21
- },
22
- get diagnostics() {
23
- return getDecoded().diagnostics;
24
- },
25
- };
5
+ return decode(binding.parse(source, options ?? {}), source);
26
6
  }
27
7
 
28
8
  export function langFromPath(path) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yuku-parser",
3
- "version": "0.5.7",
3
+ "version": "0.5.8",
4
4
  "description": "High-performance JavaScript/TypeScript parser",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -17,17 +17,17 @@
17
17
  "decode.js"
18
18
  ],
19
19
  "optionalDependencies": {
20
- "@yuku-parser/binding-linux-x64-gnu": "0.5.7",
21
- "@yuku-parser/binding-linux-arm64-gnu": "0.5.7",
22
- "@yuku-parser/binding-linux-arm-gnu": "0.5.7",
23
- "@yuku-parser/binding-linux-x64-musl": "0.5.7",
24
- "@yuku-parser/binding-linux-arm64-musl": "0.5.7",
25
- "@yuku-parser/binding-linux-arm-musl": "0.5.7",
26
- "@yuku-parser/binding-darwin-x64": "0.5.7",
27
- "@yuku-parser/binding-darwin-arm64": "0.5.7",
28
- "@yuku-parser/binding-win32-x64": "0.5.7",
29
- "@yuku-parser/binding-win32-arm64": "0.5.7",
30
- "@yuku-parser/binding-freebsd-x64": "0.5.7"
20
+ "@yuku-parser/binding-linux-x64-gnu": "0.5.8",
21
+ "@yuku-parser/binding-linux-arm64-gnu": "0.5.8",
22
+ "@yuku-parser/binding-linux-arm-gnu": "0.5.8",
23
+ "@yuku-parser/binding-linux-x64-musl": "0.5.8",
24
+ "@yuku-parser/binding-linux-arm64-musl": "0.5.8",
25
+ "@yuku-parser/binding-linux-arm-musl": "0.5.8",
26
+ "@yuku-parser/binding-darwin-x64": "0.5.8",
27
+ "@yuku-parser/binding-darwin-arm64": "0.5.8",
28
+ "@yuku-parser/binding-win32-x64": "0.5.8",
29
+ "@yuku-parser/binding-win32-arm64": "0.5.8",
30
+ "@yuku-parser/binding-freebsd-x64": "0.5.8"
31
31
  },
32
32
  "keywords": [
33
33
  "parser",