@thomasfosterau/effect-parse 0.1.0 → 0.3.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.
Files changed (161) hide show
  1. package/README.md +291 -231
  2. package/dist/Ascii.d.ts +32 -53
  3. package/dist/Ascii.d.ts.map +1 -1
  4. package/dist/Ascii.js +576 -571
  5. package/dist/Ascii.js.map +1 -1
  6. package/dist/Binary.d.ts +6 -29
  7. package/dist/Binary.d.ts.map +1 -1
  8. package/dist/Binary.js +129 -116
  9. package/dist/Binary.js.map +1 -1
  10. package/dist/Bits.d.ts +4 -2
  11. package/dist/Bits.d.ts.map +1 -1
  12. package/dist/Bits.js +121 -123
  13. package/dist/Bits.js.map +1 -1
  14. package/dist/Compiler.d.ts +39 -0
  15. package/dist/Compiler.d.ts.map +1 -0
  16. package/dist/Compiler.js +48 -0
  17. package/dist/Compiler.js.map +1 -0
  18. package/dist/Encoding.d.ts +29 -46
  19. package/dist/Encoding.d.ts.map +1 -1
  20. package/dist/Encoding.js +76 -75
  21. package/dist/Encoding.js.map +1 -1
  22. package/dist/Expression.d.ts +15 -49
  23. package/dist/Expression.d.ts.map +1 -1
  24. package/dist/Expression.js +151 -132
  25. package/dist/Expression.js.map +1 -1
  26. package/dist/Input.d.ts +251 -177
  27. package/dist/Input.d.ts.map +1 -1
  28. package/dist/Input.js +409 -299
  29. package/dist/Input.js.map +1 -1
  30. package/dist/Nom.d.ts +45 -85
  31. package/dist/Nom.d.ts.map +1 -1
  32. package/dist/Nom.js +423 -453
  33. package/dist/Nom.js.map +1 -1
  34. package/dist/ParseError.d.ts +54 -49
  35. package/dist/ParseError.d.ts.map +1 -1
  36. package/dist/ParseError.js +232 -275
  37. package/dist/ParseError.js.map +1 -1
  38. package/dist/Parser.d.ts +345 -313
  39. package/dist/Parser.d.ts.map +1 -1
  40. package/dist/Parser.js +2034 -1802
  41. package/dist/Parser.js.map +1 -1
  42. package/dist/ParserSchema.d.ts +206 -7
  43. package/dist/ParserSchema.d.ts.map +1 -1
  44. package/dist/ParserSchema.js +678 -49
  45. package/dist/ParserSchema.js.map +1 -1
  46. package/dist/ParserStream.d.ts +26 -20
  47. package/dist/ParserStream.d.ts.map +1 -1
  48. package/dist/ParserStream.js +267 -255
  49. package/dist/ParserStream.js.map +1 -1
  50. package/dist/Syntax.d.ts +401 -0
  51. package/dist/Syntax.d.ts.map +1 -0
  52. package/dist/Syntax.js +717 -0
  53. package/dist/Syntax.js.map +1 -0
  54. package/dist/Text.d.ts +98 -134
  55. package/dist/Text.d.ts.map +1 -1
  56. package/dist/Text.js +534 -496
  57. package/dist/Text.js.map +1 -1
  58. package/dist/Token.d.ts +60 -42
  59. package/dist/Token.d.ts.map +1 -1
  60. package/dist/Token.js +331 -265
  61. package/dist/Token.js.map +1 -1
  62. package/dist/index.d.ts +16 -17
  63. package/dist/index.js +16 -17
  64. package/dist/internal/compiler/closure.d.ts +14 -16
  65. package/dist/internal/compiler/closure.d.ts.map +1 -1
  66. package/dist/internal/compiler/closure.js +1458 -1637
  67. package/dist/internal/compiler/closure.js.map +1 -1
  68. package/dist/internal/compiler/entries.d.ts +11 -11
  69. package/dist/internal/compiler/entries.d.ts.map +1 -1
  70. package/dist/internal/compiler/entries.js +36 -26
  71. package/dist/internal/compiler/entries.js.map +1 -1
  72. package/dist/internal/compiler/first.d.ts +19 -19
  73. package/dist/internal/compiler/first.d.ts.map +1 -1
  74. package/dist/internal/compiler/first.js +251 -227
  75. package/dist/internal/compiler/first.js.map +1 -1
  76. package/dist/internal/compiler/jit.d.ts +6 -17
  77. package/dist/internal/compiler/jit.d.ts.map +1 -1
  78. package/dist/internal/compiler/jit.js +860 -988
  79. package/dist/internal/compiler/jit.js.map +1 -1
  80. package/dist/internal/compiler/registry.d.ts +20 -21
  81. package/dist/internal/compiler/registry.d.ts.map +1 -1
  82. package/dist/internal/compiler/registry.js +21 -41
  83. package/dist/internal/compiler/registry.js.map +1 -1
  84. package/dist/internal/compiler/sets.d.ts +9 -13
  85. package/dist/internal/compiler/sets.d.ts.map +1 -1
  86. package/dist/internal/compiler/sets.js +76 -73
  87. package/dist/internal/compiler/sets.js.map +1 -1
  88. package/dist/internal/entries.d.ts +3 -3
  89. package/dist/internal/entries.d.ts.map +1 -1
  90. package/dist/internal/entries.js +38 -30
  91. package/dist/internal/entries.js.map +1 -1
  92. package/dist/internal/node.d.ts +177 -173
  93. package/dist/internal/node.d.ts.map +1 -1
  94. package/dist/internal/node.js +77 -66
  95. package/dist/internal/node.js.map +1 -1
  96. package/dist/internal/numbers.d.ts +7 -8
  97. package/dist/internal/numbers.d.ts.map +1 -1
  98. package/dist/internal/numbers.js +246 -276
  99. package/dist/internal/numbers.js.map +1 -1
  100. package/dist/internal/resume.d.ts +6 -24
  101. package/dist/internal/resume.d.ts.map +1 -1
  102. package/dist/internal/resume.js +30 -29
  103. package/dist/internal/resume.js.map +1 -1
  104. package/dist/internal/run.d.ts +15 -0
  105. package/dist/internal/run.d.ts.map +1 -0
  106. package/dist/internal/run.js +36 -0
  107. package/dist/internal/run.js.map +1 -0
  108. package/dist/internal/stack.d.ts +16 -0
  109. package/dist/internal/stack.d.ts.map +1 -0
  110. package/dist/internal/stack.js +36 -0
  111. package/dist/internal/stack.js.map +1 -0
  112. package/dist/internal/syntax.d.ts +147 -0
  113. package/dist/internal/syntax.d.ts.map +1 -0
  114. package/dist/internal/syntax.js +309 -0
  115. package/dist/internal/syntax.js.map +1 -0
  116. package/dist/internal/text.d.ts +10 -2
  117. package/dist/internal/text.d.ts.map +1 -1
  118. package/dist/internal/text.js +295 -345
  119. package/dist/internal/text.js.map +1 -1
  120. package/dist/internal/ties.d.ts +30 -0
  121. package/dist/internal/ties.d.ts.map +1 -0
  122. package/dist/internal/ties.js +67 -0
  123. package/dist/internal/ties.js.map +1 -0
  124. package/dist/internal/tokens.d.ts +25 -30
  125. package/dist/internal/tokens.d.ts.map +1 -1
  126. package/dist/internal/tokens.js +369 -363
  127. package/dist/internal/tokens.js.map +1 -1
  128. package/package.json +42 -28
  129. package/src/Ascii.ts +278 -187
  130. package/src/Binary.ts +150 -44
  131. package/src/Bits.ts +6 -3
  132. package/src/Compiler.ts +98 -0
  133. package/src/Expression.ts +24 -16
  134. package/src/Input.ts +210 -35
  135. package/src/Nom.ts +4 -3
  136. package/src/ParseError.ts +49 -52
  137. package/src/Parser.ts +1199 -799
  138. package/src/ParserSchema.ts +982 -23
  139. package/src/ParserStream.ts +104 -80
  140. package/src/Syntax.ts +900 -0
  141. package/src/Text.ts +272 -187
  142. package/src/Token.ts +180 -87
  143. package/src/index.ts +2 -0
  144. package/src/internal/compiler/closure.ts +121 -88
  145. package/src/internal/compiler/entries.ts +21 -8
  146. package/src/internal/compiler/first.ts +17 -15
  147. package/src/internal/compiler/jit.ts +360 -157
  148. package/src/internal/compiler/registry.ts +12 -33
  149. package/src/internal/compiler/sets.ts +17 -11
  150. package/src/internal/entries.ts +4 -2
  151. package/src/internal/node.ts +51 -37
  152. package/src/internal/numbers.ts +22 -20
  153. package/src/internal/resume.ts +8 -4
  154. package/src/internal/run.ts +41 -0
  155. package/src/internal/stack.ts +41 -0
  156. package/src/internal/syntax.ts +426 -0
  157. package/src/internal/text.ts +32 -25
  158. package/src/internal/ties.ts +83 -0
  159. package/src/internal/tokens.ts +96 -51
  160. package/dist/index.d.ts.map +0 -1
  161. package/dist/index.js.map +0 -1
@@ -1,391 +1,341 @@
1
- // -----------------------------------------------------------------------------
2
- // Decoding results
3
- // -----------------------------------------------------------------------------
1
+ //#region src/internal/text.ts
4
2
  /** `Encoding.decode`: the input is malformed. */
5
- export const Invalid = -1;
3
+ const Invalid = -1;
6
4
  /** `Encoding.decode`: the input ends inside a sequence. */
7
- export const Truncated = -2;
5
+ const Truncated = -2;
8
6
  /** `next`: the input has ended. */
9
- export const End = -3;
10
- // -----------------------------------------------------------------------------
11
- // Encodings
12
- // -----------------------------------------------------------------------------
7
+ const End = -3;
13
8
  /**
14
- * Units below this value decode to themselves with width 1, so hot loops can
15
- * skip `decode`. Defaults to 0x80 for ASCII compatible encodings.
16
- */
17
- const FastLimit = Symbol.for("effect-parse/Encoding/FastLimit");
9
+ * Units below this value decode to themselves with width 1, so hot loops can
10
+ * skip `decode`. Defaults to 0x80 for ASCII compatible encodings.
11
+ */
12
+ const FastLimit = /*#__PURE__*/ Symbol.for("effect-parse/Encoding/FastLimit");
18
13
  /** @internal */
19
- export const fastLimit = (enc) => enc[FastLimit] ?? (enc.asciiCompatible && enc.unit === 1 ? 0x80 : 0);
20
- const asciiChars = Array.from({ length: 128 }, (_, i) => String.fromCharCode(i));
14
+ const fastLimit = (enc) => enc[FastLimit] ?? (enc.asciiCompatible && enc.unit === 1 ? 128 : 0);
15
+ const asciiChars = /*#__PURE__*/ Array.from({ length: 128 }, (_, i) => String.fromCharCode(i));
16
+ /**
17
+ * The code point that starts `s`: for a string known to hold one, such as
18
+ * one element of `Array.from(string)`. On an empty string it is `NaN`, which,
19
+ * like the `undefined` of `codePointAt(0)`, matches no code point.
20
+ */
21
+ const firstCodePoint = (s) => s.codePointAt(0) ?? NaN;
21
22
  /** `String.fromCodePoint`, with the ASCII characters cached. */
22
- export const charOf = (cp) => cp < 128 ? asciiChars[cp] : cp < 0x10000 ? String.fromCharCode(cp) : String.fromCodePoint(cp);
23
+ const charOf = (cp) => cp < 128 ? asciiChars[cp] : cp < 65536 ? String.fromCharCode(cp) : String.fromCodePoint(cp);
23
24
  const decodeString = (s, i, end) => {
24
- const c = s.charCodeAt(i);
25
- if ((c & 0xf800) !== 0xd800)
26
- return c;
27
- if (c <= 0xdbff) {
28
- if (i + 1 >= end)
29
- return Truncated;
30
- const d = s.charCodeAt(i + 1);
31
- if ((d & 0xfc00) === 0xdc00)
32
- return ((c - 0xd800) << 10) + (d - 0xdc00) + 0x10000;
33
- }
34
- return Invalid;
25
+ const c = s.charCodeAt(i);
26
+ if ((c & 63488) !== 55296) return c;
27
+ if (c <= 56319) {
28
+ if (i + 1 >= end) return -2;
29
+ const d = s.charCodeAt(i + 1);
30
+ if ((d & 64512) === 56320) return (c - 55296 << 10) + (d - 56320) + 65536;
31
+ }
32
+ return -1;
35
33
  };
36
34
  /**
37
- * JavaScript strings: UTF-16 code units. Offsets are code-unit indices and
38
- * slices are `String.prototype.slice`s, so no decoding is needed.
39
- */
40
- export const string = {
41
- name: "UTF-16",
42
- unit: 1,
43
- asciiCompatible: true,
44
- decode: decodeString,
45
- width: (cp) => cp > 0xffff ? 2 : 1,
46
- encode: (text) => text,
47
- text: (s, start, end) => s.slice(start, end),
48
- [FastLimit]: 0xd800
35
+ * JavaScript strings: UTF-16 code units. Offsets are code-unit indices and
36
+ * slices are `String.prototype.slice`s, so no decoding is needed.
37
+ */
38
+ const string = {
39
+ name: "UTF-16",
40
+ unit: 1,
41
+ asciiCompatible: true,
42
+ decode: decodeString,
43
+ width: (cp) => cp > 65535 ? 2 : 1,
44
+ encode: (text) => text,
45
+ text: (s, start, end) => s.slice(start, end),
46
+ [FastLimit]: 55296
49
47
  };
50
48
  const decodeUtf8 = (b, i, end) => {
51
- const c = b[i];
52
- if (c < 0x80)
53
- return c;
54
- if (c < 0xc2)
55
- return Invalid;
56
- if (c < 0xe0) {
57
- if (i + 1 >= end)
58
- return Truncated;
59
- const d = b[i + 1];
60
- return (d & 0xc0) === 0x80 ? ((c & 0x1f) << 6) | (d & 0x3f) : Invalid;
61
- }
62
- if (c < 0xf0) {
63
- if (i + 1 >= end)
64
- return Truncated;
65
- const d = b[i + 1];
66
- // Reject overlong encodings (E0 80..9F) and surrogates (ED A0..BF)
67
- if (c === 0xe0 ? d < 0xa0 || d > 0xbf : c === 0xed ? d < 0x80 || d > 0x9f : (d & 0xc0) !== 0x80)
68
- return Invalid;
69
- if (i + 2 >= end)
70
- return Truncated;
71
- const e = b[i + 2];
72
- return (e & 0xc0) === 0x80 ? ((c & 0x0f) << 12) | ((d & 0x3f) << 6) | (e & 0x3f) : Invalid;
73
- }
74
- if (c < 0xf5) {
75
- if (i + 1 >= end)
76
- return Truncated;
77
- const d = b[i + 1];
78
- // Reject overlong encodings (F0 80..8F) and code points above U+10FFFF (F4 90..BF)
79
- if (c === 0xf0 ? d < 0x90 || d > 0xbf : c === 0xf4 ? d < 0x80 || d > 0x8f : (d & 0xc0) !== 0x80)
80
- return Invalid;
81
- if (i + 2 >= end)
82
- return Truncated;
83
- const e = b[i + 2];
84
- if ((e & 0xc0) !== 0x80)
85
- return Invalid;
86
- if (i + 3 >= end)
87
- return Truncated;
88
- const f = b[i + 3];
89
- return (f & 0xc0) === 0x80 ? ((c & 0x07) << 18) | ((d & 0x3f) << 12) | ((e & 0x3f) << 6) | (f & 0x3f) : Invalid;
90
- }
91
- return Invalid;
49
+ const c = b[i];
50
+ if (c < 128) return c;
51
+ if (c < 194) return -1;
52
+ if (c < 224) {
53
+ if (i + 1 >= end) return -2;
54
+ const d = b[i + 1];
55
+ return (d & 192) === 128 ? (c & 31) << 6 | d & 63 : -1;
56
+ }
57
+ if (c < 240) {
58
+ if (i + 1 >= end) return -2;
59
+ const d = b[i + 1];
60
+ if (c === 224 ? d < 160 || d > 191 : c === 237 ? d < 128 || d > 159 : (d & 192) !== 128) return -1;
61
+ if (i + 2 >= end) return -2;
62
+ const e = b[i + 2];
63
+ return (e & 192) === 128 ? (c & 15) << 12 | (d & 63) << 6 | e & 63 : -1;
64
+ }
65
+ if (c < 245) {
66
+ if (i + 1 >= end) return -2;
67
+ const d = b[i + 1];
68
+ if (c === 240 ? d < 144 || d > 191 : c === 244 ? d < 128 || d > 143 : (d & 192) !== 128) return -1;
69
+ if (i + 2 >= end) return -2;
70
+ const e = b[i + 2];
71
+ if ((e & 192) !== 128) return -1;
72
+ if (i + 3 >= end) return -2;
73
+ const f = b[i + 3];
74
+ return (f & 192) === 128 ? (c & 7) << 18 | (d & 63) << 12 | (e & 63) << 6 | f & 63 : -1;
75
+ }
76
+ return -1;
92
77
  };
93
- // Building short strings by hand is several times faster than a
94
- // `TextDecoder` call, which has a high fixed cost.
95
78
  const SHORT = 24;
96
- const utf8Decoder = new TextDecoder("utf-8", { ignoreBOM: true });
79
+ const utf8Decoder = /*#__PURE__*/ new TextDecoder("utf-8", { ignoreBOM: true });
97
80
  /**
98
- * Decodes `b[start, end)`. Some platforms' `TextDecoder` rejects views of a
99
- * `SharedArrayBuffer`, so those are copied first.
100
- */
81
+ * Decodes `b[start, end)`. Some platforms' `TextDecoder` rejects views of a
82
+ * `SharedArrayBuffer`, so those are copied first.
83
+ */
101
84
  const decodeRange = (decoder, b, start, end) => {
102
- const view = b.subarray(start, end);
103
- return decoder.decode(b.buffer instanceof ArrayBuffer ? view : view.slice());
85
+ const view = b.subarray(start, end);
86
+ return decoder.decode(b.buffer instanceof ArrayBuffer ? view : view.slice());
104
87
  };
105
- const utf8Encoder = new TextEncoder();
88
+ const utf8Encoder = /*#__PURE__*/ new TextEncoder();
106
89
  /** UTF-8 bytes. */
107
- export const utf8 = {
108
- name: "UTF-8",
109
- unit: 1,
110
- asciiCompatible: true,
111
- decode: decodeUtf8,
112
- width: (cp) => cp < 0x80 ? 1 : cp < 0x800 ? 2 : cp < 0x10000 ? 3 : 4,
113
- encode: (text) => utf8Encoder.encode(text),
114
- text: (b, start, end) => {
115
- if (end - start > SHORT)
116
- return decodeRange(utf8Decoder, b, start, end);
117
- let out = "";
118
- for (let i = start; i < end;) {
119
- const c = b[i];
120
- if (c < 0x80) {
121
- out += asciiChars[c];
122
- i++;
123
- }
124
- else {
125
- const cp = decodeUtf8(b, i, end);
126
- out += cp < 0x10000 ? String.fromCharCode(cp) : String.fromCodePoint(cp);
127
- i += cp < 0x800 ? 2 : cp < 0x10000 ? 3 : 4;
128
- }
129
- }
130
- return out;
131
- }
90
+ const utf8 = {
91
+ name: "UTF-8",
92
+ unit: 1,
93
+ asciiCompatible: true,
94
+ decode: decodeUtf8,
95
+ width: (cp) => cp < 128 ? 1 : cp < 2048 ? 2 : cp < 65536 ? 3 : 4,
96
+ encode: (text) => utf8Encoder.encode(text),
97
+ text: (b, start, end) => {
98
+ if (end - start > SHORT) return decodeRange(utf8Decoder, b, start, end);
99
+ let out = "";
100
+ for (let i = start; i < end;) {
101
+ const c = b[i];
102
+ if (c < 128) {
103
+ out += asciiChars[c];
104
+ i++;
105
+ } else {
106
+ const cp = decodeUtf8(b, i, end);
107
+ out += cp < 65536 ? String.fromCharCode(cp) : String.fromCodePoint(cp);
108
+ i += cp < 2048 ? 2 : cp < 65536 ? 3 : 4;
109
+ }
110
+ }
111
+ return out;
112
+ }
132
113
  };
133
114
  const makeUtf16 = (littleEndian) => {
134
- const lo = littleEndian ? 0 : 1;
135
- const hi = 1 - lo;
136
- let decoder;
137
- return {
138
- name: littleEndian ? "UTF-16LE" : "UTF-16BE",
139
- unit: 2,
140
- asciiCompatible: false,
141
- decode: (b, i, end) => {
142
- if (i + 1 >= end)
143
- return Truncated;
144
- const c = b[i + lo] | (b[i + hi] << 8);
145
- if ((c & 0xf800) !== 0xd800)
146
- return c;
147
- if (c > 0xdbff)
148
- return Invalid;
149
- if (i + 3 >= end)
150
- return Truncated;
151
- const d = b[i + 2 + lo] | (b[i + 2 + hi] << 8);
152
- return (d & 0xfc00) === 0xdc00 ? ((c - 0xd800) << 10) + (d - 0xdc00) + 0x10000 : Invalid;
153
- },
154
- width: (cp) => cp > 0xffff ? 4 : 2,
155
- encode: (text) => {
156
- const out = new Uint8Array(text.length * 2);
157
- for (let i = 0; i < text.length; i++) {
158
- const c = text.charCodeAt(i);
159
- out[2 * i + lo] = c & 0xff;
160
- out[2 * i + hi] = c >> 8;
161
- }
162
- return out;
163
- },
164
- text: (b, start, end) => {
165
- if (end - start <= SHORT * 2) {
166
- // The range is well formed, so surrogate pairs can be copied unit by unit
167
- let out = "";
168
- for (let i = start; i < end; i += 2)
169
- out += String.fromCharCode(b[i + lo] | (b[i + hi] << 8));
170
- return out;
171
- }
172
- decoder ??= new TextDecoder(littleEndian ? "utf-16le" : "utf-16be", { ignoreBOM: true });
173
- return decodeRange(decoder, b, start, end);
174
- }
175
- };
115
+ const lo = littleEndian ? 0 : 1;
116
+ const hi = 1 - lo;
117
+ let decoder;
118
+ return {
119
+ name: littleEndian ? "UTF-16LE" : "UTF-16BE",
120
+ unit: 2,
121
+ asciiCompatible: false,
122
+ decode: (b, i, end) => {
123
+ if (i + 1 >= end) return -2;
124
+ const c = b[i + lo] | b[i + hi] << 8;
125
+ if ((c & 63488) !== 55296) return c;
126
+ if (c > 56319) return -1;
127
+ if (i + 3 >= end) return -2;
128
+ const d = b[i + 2 + lo] | b[i + 2 + hi] << 8;
129
+ return (d & 64512) === 56320 ? (c - 55296 << 10) + (d - 56320) + 65536 : -1;
130
+ },
131
+ width: (cp) => cp > 65535 ? 4 : 2,
132
+ encode: (text) => {
133
+ const out = new Uint8Array(text.length * 2);
134
+ for (let i = 0; i < text.length; i++) {
135
+ const c = text.charCodeAt(i);
136
+ out[2 * i + lo] = c & 255;
137
+ out[2 * i + hi] = c >> 8;
138
+ }
139
+ return out;
140
+ },
141
+ text: (b, start, end) => {
142
+ if (end - start <= 48) {
143
+ let out = "";
144
+ for (let i = start; i < end; i += 2) out += String.fromCharCode(b[i + lo] | b[i + hi] << 8);
145
+ return out;
146
+ }
147
+ decoder ??= new TextDecoder(littleEndian ? "utf-16le" : "utf-16be", { ignoreBOM: true });
148
+ return decodeRange(decoder, b, start, end);
149
+ }
150
+ };
176
151
  };
177
152
  /** UTF-16 bytes, little endian. */
178
- export const utf16le = makeUtf16(true);
153
+ const utf16le = /*#__PURE__*/ makeUtf16(true);
179
154
  /** UTF-16 bytes, big endian. */
180
- export const utf16be = makeUtf16(false);
155
+ const utf16be = /*#__PURE__*/ makeUtf16(false);
181
156
  /** ISO-8859-1: every byte is the code point of the same value. */
182
- export const latin1 = {
183
- name: "ISO-8859-1",
184
- unit: 1,
185
- asciiCompatible: true,
186
- decode: (b, i) => b[i],
187
- width: () => 1,
188
- encode: (text) => {
189
- const out = new Uint8Array(text.length);
190
- for (let i = 0; i < text.length; i++) {
191
- const c = text.charCodeAt(i);
192
- if (c > 0xff)
193
- throw new RangeError(`${JSON.stringify(text[i])} can't be encoded as ISO-8859-1`);
194
- out[i] = c;
195
- }
196
- return out;
197
- },
198
- text: (b, start, end) => {
199
- let out = "";
200
- for (let i = start; i < end; i += 8192) {
201
- out += String.fromCharCode.apply(null, b.subarray(i, Math.min(end, i + 8192)));
202
- }
203
- return out;
204
- },
205
- [FastLimit]: 0x100
157
+ const latin1 = {
158
+ name: "ISO-8859-1",
159
+ unit: 1,
160
+ asciiCompatible: true,
161
+ decode: (b, i) => b[i],
162
+ width: () => 1,
163
+ encode: (text) => {
164
+ const out = new Uint8Array(text.length);
165
+ for (let i = 0; i < text.length; i++) {
166
+ const c = text.charCodeAt(i);
167
+ if (c > 255) throw new RangeError(`${JSON.stringify(text[i])} can't be encoded as ISO-8859-1`);
168
+ out[i] = c;
169
+ }
170
+ return out;
171
+ },
172
+ text: (b, start, end) => {
173
+ let out = "";
174
+ for (let i = start; i < end; i += 8192) out += String.fromCharCode.apply(null, b.subarray(i, Math.min(end, i + 8192)));
175
+ return out;
176
+ },
177
+ [FastLimit]: 256
206
178
  };
207
- // -----------------------------------------------------------------------------
208
- // Reading code points from a State
209
- // -----------------------------------------------------------------------------
210
179
  /** The width of the code point returned by the latest {@link next}. */
211
- export let width = 0;
180
+ let width = 0;
212
181
  /**
213
- * Decodes the code point at the cursor without consuming it. Returns the
214
- * code point (setting {@link width}), {@link End}, {@link Invalid}, or
215
- * {@link Truncated} (only for partial input; on complete input a truncated
216
- * sequence is {@link Invalid}).
217
- */
218
- export const next = (enc, state, pos = state.pos) => {
219
- const end = state.end;
220
- if (pos >= end)
221
- return End;
222
- const cp = enc.decode(state.input, pos, end);
223
- if (cp >= 0) {
224
- width = enc.width(cp);
225
- return cp;
226
- }
227
- return cp === Truncated && !state.partial ? Invalid : cp;
182
+ * Decodes the code point at the cursor without consuming it. Returns the
183
+ * code point (setting {@link width}), {@link End}, {@link Invalid}, or
184
+ * {@link Truncated} (only for partial input; on complete input a truncated
185
+ * sequence is {@link Invalid}).
186
+ */
187
+ const next = (enc, state, pos = state.pos) => {
188
+ const end = state.end;
189
+ if (pos >= end) return -3;
190
+ const cp = enc.decode(state.input, pos, end);
191
+ if (cp >= 0) {
192
+ width = enc.width(cp);
193
+ return cp;
194
+ }
195
+ return cp === -2 && !state.partial ? -1 : cp;
228
196
  };
229
197
  /**
230
- * Fails a parser that expected a code point but found `cp` (one of the
231
- * negative results of {@link next}, or a code point that did not match).
232
- */
233
- export const failAt = (state, cp, kind, expected, offset = state.pos) => {
234
- if (cp === End)
235
- return state.eoi(kind, expected, 1);
236
- if (cp === Truncated)
237
- return state.incomplete(1, expected);
238
- return state.backtrack(kind, expected, offset);
198
+ * Fails a parser that expected a code point but found `cp` (one of the
199
+ * negative results of {@link next}, or a code point that did not match).
200
+ */
201
+ const failAt = (state, cp, kind, expected, offset = state.pos) => {
202
+ if (cp === -3) return state.eoi(kind, expected, 1);
203
+ if (cp === -2) return state.incomplete(1, expected);
204
+ return state.backtrack(kind, expected, offset);
239
205
  };
240
- // Results of `scan`, kept in module variables to avoid allocating
241
206
  /** Offset reached by the latest {@link scan}. */
242
- export let scanOffset = 0;
207
+ let scanOffset = 0;
243
208
  /** Number of code points matched by the latest {@link scan}. */
244
- export let scanCount = 0;
209
+ let scanCount = 0;
245
210
  /**
246
- * Why the latest {@link scan} stopped: 0 = a non-matching code point or the
247
- * maximum count, or one of {@link End}, {@link Invalid}, {@link Truncated}.
248
- */
249
- export let scanStop = 0;
211
+ * Why the latest {@link scan} stopped: 0 = a non-matching code point or the
212
+ * maximum count, or one of {@link End}, {@link Invalid}, {@link Truncated}.
213
+ */
214
+ let scanStop = 0;
250
215
  /**
251
- * Scans code points from `pos` while `test(cp) !== negate`, up to `max`
252
- * code points. `fast` is `fastLimit(enc)`, which callers can precompute.
253
- */
254
- export const scan = (enc, state, test, negate, max, pos = state.pos, fast = fastLimit(enc)) => {
255
- const end = state.end;
256
- const decode = enc.decode;
257
- let i = pos;
258
- let count = 0;
259
- let stop = 0;
260
- if (state.inputKind === 0) {
261
- const s = state.input;
262
- while (count < max) {
263
- if (i >= end) {
264
- stop = End;
265
- break;
266
- }
267
- const c = s.charCodeAt(i);
268
- if (c < fast) {
269
- if (test(c) === negate)
270
- break;
271
- i++;
272
- }
273
- else {
274
- const cp = decode(s, i, end);
275
- if (cp < 0) {
276
- stop = cp === Truncated && state.partial ? Truncated : Invalid;
277
- break;
278
- }
279
- if (test(cp) === negate)
280
- break;
281
- i += enc.width(cp);
282
- }
283
- count++;
284
- }
285
- }
286
- else {
287
- const b = state.input;
288
- while (count < max) {
289
- if (i >= end) {
290
- stop = End;
291
- break;
292
- }
293
- const c = b[i];
294
- if (c < fast) {
295
- if (test(c) === negate)
296
- break;
297
- i++;
298
- }
299
- else {
300
- const cp = decode(b, i, end);
301
- if (cp < 0) {
302
- stop = cp === Truncated && state.partial ? Truncated : Invalid;
303
- break;
304
- }
305
- if (test(cp) === negate)
306
- break;
307
- i += enc.width(cp);
308
- }
309
- count++;
310
- }
311
- }
312
- scanOffset = i;
313
- scanCount = count;
314
- scanStop = stop;
216
+ * Scans code points from `pos` while `test(cp) !== negate`, up to `max`
217
+ * code points. `fast` is `fastLimit(enc)`, which callers can precompute.
218
+ */
219
+ const scan = (enc, state, test, negate, max, pos = state.pos, fast = fastLimit(enc)) => {
220
+ const end = state.end;
221
+ const decode = enc.decode;
222
+ let i = pos;
223
+ let count = 0;
224
+ let stop = 0;
225
+ if (state.inputKind === 0) {
226
+ const s = state.input;
227
+ while (count < max) {
228
+ if (i >= end) {
229
+ stop = -3;
230
+ break;
231
+ }
232
+ const c = s.charCodeAt(i);
233
+ if (c < fast) {
234
+ if (test(c) === negate) break;
235
+ i++;
236
+ } else {
237
+ const cp = decode(s, i, end);
238
+ if (cp < 0) {
239
+ stop = cp === -2 && state.partial ? -2 : -1;
240
+ break;
241
+ }
242
+ if (test(cp) === negate) break;
243
+ i += enc.width(cp);
244
+ }
245
+ count++;
246
+ }
247
+ } else {
248
+ const b = state.input;
249
+ while (count < max) {
250
+ if (i >= end) {
251
+ stop = -3;
252
+ break;
253
+ }
254
+ const c = b[i];
255
+ if (c < fast) {
256
+ if (test(c) === negate) break;
257
+ i++;
258
+ } else {
259
+ const cp = decode(b, i, end);
260
+ if (cp < 0) {
261
+ stop = cp === -2 && state.partial ? -2 : -1;
262
+ break;
263
+ }
264
+ if (test(cp) === negate) break;
265
+ i += enc.width(cp);
266
+ }
267
+ count++;
268
+ }
269
+ }
270
+ scanOffset = i;
271
+ scanCount = count;
272
+ scanStop = stop;
315
273
  };
316
274
  const always = () => true;
317
275
  /**
318
- * Checks that `[start, end)` is well formed, counting its code points.
319
- * Returns the count, or `-1 - offset` of the first malformed sequence.
320
- */
321
- export const validate = (enc, state, start, end) => {
322
- const savedEnd = state.end;
323
- const savedPartial = state.partial;
324
- state.end = end;
325
- state.partial = false;
326
- scan(enc, state, always, false, Infinity, start);
327
- state.end = savedEnd;
328
- state.partial = savedPartial;
329
- return scanStop === Invalid ? -1 - scanOffset : scanCount;
276
+ * Checks that `[start, end)` is well formed, counting its code points.
277
+ * Returns the count, or `-1 - offset` of the first malformed sequence.
278
+ */
279
+ const validate = (enc, state, start, end) => {
280
+ const savedEnd = state.end;
281
+ const savedPartial = state.partial;
282
+ state.end = end;
283
+ state.partial = false;
284
+ scan(enc, state, always, false, Infinity, start);
285
+ state.end = savedEnd;
286
+ state.partial = savedPartial;
287
+ return scanStop === -1 ? -1 - scanOffset : scanCount;
330
288
  };
331
- // -----------------------------------------------------------------------------
332
- // Case folding
333
- // -----------------------------------------------------------------------------
334
- const foldCache = new Map();
289
+ const foldCache = /*#__PURE__*/ new Map();
335
290
  const single = (s) => {
336
- const cp = s.codePointAt(0);
337
- return s.length === (cp > 0xffff ? 2 : 1) ? cp : -1;
291
+ const cp = firstCodePoint(s);
292
+ return s.length === (cp > 65535 ? 2 : 1) ? cp : -1;
338
293
  };
339
294
  /**
340
- * Simple case folding of a code point: the lowercase of its uppercase, when
341
- * both map to a single code point (so `"K"` (Kelvin) and `"ſ"` fold to `"k"`
342
- * and `"s"`), otherwise the code point itself.
343
- */
344
- export const fold = (cp) => {
345
- if (cp < 128)
346
- return cp >= 65 && cp <= 90 ? cp + 32 : cp;
347
- let f = foldCache.get(cp);
348
- if (f === undefined) {
349
- const c = String.fromCodePoint(cp);
350
- const upper = c.toUpperCase();
351
- const viaUpper = single(upper) >= 0 ? single(upper.toLowerCase()) : -1;
352
- f = viaUpper >= 0 ? viaUpper : single(c.toLowerCase());
353
- if (f < 0)
354
- f = cp;
355
- foldCache.set(cp, f);
356
- }
357
- return f;
295
+ * Simple case folding of a code point: the lowercase of its uppercase, when
296
+ * both map to a single code point (so `"K"` (Kelvin) and `"ſ"` fold to `"k"`
297
+ * and `"s"`), otherwise the code point itself.
298
+ */
299
+ const fold = (cp) => {
300
+ if (cp < 128) return cp >= 65 && cp <= 90 ? cp + 32 : cp;
301
+ let f = foldCache.get(cp);
302
+ if (f === void 0) {
303
+ const c = String.fromCodePoint(cp);
304
+ const upper = c.toUpperCase();
305
+ const viaUpper = single(upper) >= 0 ? single(upper.toLowerCase()) : -1;
306
+ f = viaUpper >= 0 ? viaUpper : single(c.toLowerCase());
307
+ if (f < 0) f = cp;
308
+ foldCache.set(cp, f);
309
+ }
310
+ return f;
358
311
  };
359
- // -----------------------------------------------------------------------------
360
- // Error rendering
361
- // -----------------------------------------------------------------------------
362
312
  /**
363
- * Walks the code points of `[start, end)` leniently: malformed sequences
364
- * are reported as `-1` and skipped one code unit at a time.
365
- */
366
- export const walk = (enc, input, start, end, f) => {
367
- let i = start;
368
- while (i < end) {
369
- let cp = enc.decode(input, i, end);
370
- let w;
371
- if (cp >= 0) {
372
- w = enc.width(cp);
373
- }
374
- else {
375
- cp = -1;
376
- w = Math.min(enc.unit, end - i);
377
- }
378
- if (f(cp, i, w) === false)
379
- return;
380
- i += w;
381
- }
313
+ * Walks the code points of `[start, end)` leniently: malformed sequences
314
+ * are reported as `-1` and skipped one code unit at a time.
315
+ */
316
+ const walk = (enc, input, start, end, f) => {
317
+ let i = start;
318
+ while (i < end) {
319
+ let cp = enc.decode(input, i, end);
320
+ let w;
321
+ if (cp >= 0) w = enc.width(cp);
322
+ else {
323
+ cp = -1;
324
+ w = Math.min(enc.unit, end - i);
325
+ }
326
+ if (f(cp, i, w) === false) return;
327
+ i += w;
328
+ }
382
329
  };
383
330
  /** Decodes `[start, end)`, replacing malformed sequences with U+FFFD. */
384
- export const lenientText = (enc, input, start, end) => {
385
- let out = "";
386
- walk(enc, input, start, end, (cp) => {
387
- out += cp < 0 ? "�" : charOf(cp);
388
- });
389
- return out;
331
+ const lenientText = (enc, input, start, end) => {
332
+ let out = "";
333
+ walk(enc, input, start, end, (cp) => {
334
+ out += cp < 0 ? "�" : charOf(cp);
335
+ });
336
+ return out;
390
337
  };
338
+ //#endregion
339
+ export { End, Invalid, Truncated, charOf, failAt, fastLimit, firstCodePoint, fold, latin1, lenientText, next, scan, scanCount, scanOffset, scanStop, string, utf16be, utf16le, utf8, validate, walk, width };
340
+
391
341
  //# sourceMappingURL=text.js.map