@thomasfosterau/effect-parse 0.1.0 → 0.2.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 (155) hide show
  1. package/README.md +247 -230
  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 +143 -131
  25. package/dist/Expression.js.map +1 -1
  26. package/dist/Input.d.ts +206 -177
  27. package/dist/Input.d.ts.map +1 -1
  28. package/dist/Input.js +317 -299
  29. package/dist/Input.js.map +1 -1
  30. package/dist/Nom.d.ts +44 -84
  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 +233 -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 +2010 -1802
  41. package/dist/Parser.js.map +1 -1
  42. package/dist/ParserSchema.d.ts +8 -7
  43. package/dist/ParserSchema.d.ts.map +1 -1
  44. package/dist/ParserSchema.js +49 -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 +26 -0
  111. package/dist/internal/stack.js.map +1 -0
  112. package/dist/internal/syntax.d.ts +146 -0
  113. package/dist/internal/syntax.d.ts.map +1 -0
  114. package/dist/internal/syntax.js +305 -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/tokens.d.ts +25 -30
  121. package/dist/internal/tokens.d.ts.map +1 -1
  122. package/dist/internal/tokens.js +369 -363
  123. package/dist/internal/tokens.js.map +1 -1
  124. package/package.json +42 -28
  125. package/src/Ascii.ts +278 -187
  126. package/src/Binary.ts +150 -44
  127. package/src/Bits.ts +6 -3
  128. package/src/Compiler.ts +98 -0
  129. package/src/Expression.ts +10 -5
  130. package/src/Input.ts +106 -28
  131. package/src/Nom.ts +4 -3
  132. package/src/ParseError.ts +48 -51
  133. package/src/Parser.ts +1163 -795
  134. package/src/ParserStream.ts +104 -80
  135. package/src/Syntax.ts +900 -0
  136. package/src/Text.ts +272 -187
  137. package/src/Token.ts +180 -87
  138. package/src/index.ts +2 -0
  139. package/src/internal/compiler/closure.ts +121 -88
  140. package/src/internal/compiler/entries.ts +21 -8
  141. package/src/internal/compiler/first.ts +17 -15
  142. package/src/internal/compiler/jit.ts +360 -157
  143. package/src/internal/compiler/registry.ts +12 -33
  144. package/src/internal/compiler/sets.ts +17 -11
  145. package/src/internal/entries.ts +4 -2
  146. package/src/internal/node.ts +51 -37
  147. package/src/internal/numbers.ts +22 -20
  148. package/src/internal/resume.ts +8 -4
  149. package/src/internal/run.ts +41 -0
  150. package/src/internal/stack.ts +36 -0
  151. package/src/internal/syntax.ts +421 -0
  152. package/src/internal/text.ts +32 -25
  153. package/src/internal/tokens.ts +96 -51
  154. package/dist/index.d.ts.map +0 -1
  155. package/dist/index.js.map +0 -1
@@ -1,9 +1,7 @@
1
- /**
2
- * Number conversions shared by the `Ascii` and `Text` parsers and the
3
- * compilers.
4
- */
5
- import type { Input } from "../Input.js";
6
- import { Fail } from "../Input.js";
1
+ import { Fail, Input } from "../Input.js";
2
+ //#region src/internal/numbers.d.ts
3
+ /** The text of an input slice: as is for a string, Latin-1 for bytes, joined for tokens. */
4
+ export declare const toText: (s: Input) => string;
7
5
  /** The value of the text matched by `Ascii.float` and `Text.float`. */
8
6
  export declare const toFloat: (s: Input) => number;
9
7
  /**
@@ -20,8 +18,8 @@ export declare const floatText: <P extends object>(p: P) => P;
20
18
  * when that is near the end of a partial input.
21
19
  */
22
20
  export declare const floatScan: {
23
- end: number;
24
- read: number;
21
+ end: number;
22
+ read: number;
25
23
  };
26
24
  /**
27
25
  * Matches and converts what `Ascii.float` matches at `i`, when it is in
@@ -38,4 +36,5 @@ export declare const floatScan: {
38
36
  export declare const scanFloatString: (s: string, i: number, end: number) => number | Fail | undefined;
39
37
  /** {@link scanFloatString} on bytes, or the units of an ASCII compatible encoding. */
40
38
  export declare const scanFloatBytes: (b: Uint8Array, i: number, end: number) => number | Fail | undefined;
39
+ //#endregion
41
40
  //# sourceMappingURL=numbers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"numbers.d.ts","sourceRoot":"","sources":["../../src/internal/numbers.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AAMlC,uEAAuE;AACvE,eAAO,MAAM,OAAO,MAAO,KAAK,KAAG,MAYlC,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,UAAU,iBAAwB,CAAA;AAE/C,2CAA2C;AAC3C,eAAO,MAAM,SAAS,GAAI,CAAC,SAAS,MAAM,KAAK,CAAC,KAAG,CAGlD,CAAA;AAKD;;;;GAIG;AACH,eAAO,MAAM,SAAS;IAAK,GAAG;IAAK,IAAI;CAAK,CAAA;AAqE5C;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,eAAe,MAAO,MAAM,KAAK,MAAM,OAAO,MAAM,KAAG,MAAM,GAAG,IAAI,GAAG,SAgFnF,CAAA;AAED,sFAAsF;AACtF,eAAO,MAAM,cAAc,MAAO,UAAU,KAAK,MAAM,OAAO,MAAM,KAAG,MAAM,GAAG,IAAI,GAAG,SA2EtF,CAAA"}
1
+ {"version":3,"file":"numbers.d.ts","names":[],"sources":["../../src/internal/numbers.ts"],"mappings":";;;qBAUa,SAAU,GAAG;;qBAIb,UAAW,GAAG;;;;;;qBAmBd,YAAU;;qBAGV,YAAa,kBAAkB,GAAG,MAAI;;;;;;qBAatC;;;;;;;;;;;;;;;;qBAiFA,kBAAmB,WAAW,WAAW,yBAAuB;;qBAmFhE,iBAAkB,GAAG,YAAY,WAAW,yBAAuB"}
@@ -1,302 +1,272 @@
1
+ import * as Predicate from "effect/Predicate";
1
2
  import { Fail } from "../Input.js";
2
3
  import { latin1 } from "./text.js";
3
- const toText = (s) => typeof s === "string" ? s : s instanceof Uint8Array ? latin1.text(s, 0, s.length) : s.join("");
4
+ //#region src/internal/numbers.ts
5
+ /**
6
+ * Number conversions shared by the `Ascii` and `Text` parsers and the
7
+ * compilers.
8
+ */
9
+ /** The text of an input slice: as is for a string, Latin-1 for bytes, joined for tokens. */
10
+ const toText = (s) => Predicate.isString(s) ? s : s instanceof Uint8Array ? latin1.text(s, 0, s.length) : s.join("");
4
11
  /** The value of the text matched by `Ascii.float` and `Text.float`. */
5
- export const toFloat = (s) => {
6
- if (typeof s === "string") {
7
- // Plain decimal notation (as opposed to `inf` or `nan`) ends in a digit or "."
8
- const last = s.charCodeAt(s.length - 1);
9
- if ((last >= 48 && last <= 57) || last === 46)
10
- return Number(s);
11
- }
12
- const t = toText(s).toLowerCase();
13
- const negative = t.startsWith("-");
14
- const body = t.startsWith("-") || t.startsWith("+") ? t.slice(1) : t;
15
- if (body === "nan")
16
- return NaN;
17
- if (body.startsWith("inf"))
18
- return negative ? -Infinity : Infinity;
19
- return Number(t);
12
+ const toFloat = (s) => {
13
+ if (Predicate.isString(s)) {
14
+ const last = s.charCodeAt(s.length - 1);
15
+ if (last >= 48 && last <= 57 || last === 46) return Number(s);
16
+ }
17
+ const t = toText(s).toLowerCase();
18
+ const negative = t.startsWith("-");
19
+ const body = t.startsWith("-") || t.startsWith("+") ? t.slice(1) : t;
20
+ if (body === "nan") return NaN;
21
+ if (body.startsWith("inf")) return negative ? -Infinity : Infinity;
22
+ return Number(t);
20
23
  };
21
24
  /**
22
- * The parsers that `Ascii.float` and `Text.float` map with `toFloat`: the
23
- * compilers replace them by {@link scanFloatString} and
24
- * {@link scanFloatBytes} when the text is in decimal notation.
25
- */
26
- export const floatTexts = new WeakSet();
25
+ * The parsers that `Ascii.float` and `Text.float` map with `toFloat`: the
26
+ * compilers replace them by {@link scanFloatString} and
27
+ * {@link scanFloatBytes} when the text is in decimal notation.
28
+ */
29
+ const floatTexts = /*#__PURE__*/ new WeakSet();
27
30
  /** Adds a parser to {@link floatTexts}. */
28
- export const floatText = (p) => {
29
- floatTexts.add(p);
30
- return p;
31
+ const floatText = (p) => {
32
+ floatTexts.add(p);
33
+ return p;
31
34
  };
32
35
  /** The powers of ten that doubles represent exactly. */
33
- const exact = Array.from({ length: 23 }, (_, i) => Number(`1e${i}`));
36
+ const exact = /*#__PURE__*/ Array.from({ length: 23 }, (_, i) => Number(`1e${i}`));
34
37
  /**
35
- * Where the latest scan ended, when it succeeded, and the furthest offset it
36
- * read (when it succeeded or failed): streaming builds re-run the parser
37
- * when that is near the end of a partial input.
38
- */
39
- export const floatScan = { end: 0, read: 0 };
38
+ * Where the latest scan ended, when it succeeded, and the furthest offset it
39
+ * read (when it succeeded or failed): streaming builds re-run the parser
40
+ * when that is near the end of a partial input.
41
+ */
42
+ const floatScan = {
43
+ end: 0,
44
+ read: 0
45
+ };
40
46
  const isDigit = (c) => c >= 48 && c <= 57;
41
- // Exact products of doubles (Dekker), without allocating: `a * b` is
42
- // `productHi + productLo` exactly, barring overflow and underflow
43
- const Split = 134217729; // 2^27 + 1
47
+ const Split = 134217729;
44
48
  let productHi = 0;
45
49
  let productLo = 0;
46
50
  const twoProduct = (a, b) => {
47
- const p = a * b;
48
- let t = Split * a;
49
- const ah = t - (t - a);
50
- const al = a - ah;
51
- t = Split * b;
52
- const bh = t - (t - b);
53
- const bl = b - bh;
54
- productHi = p;
55
- productLo = ((ah * bh - p) + ah * bl + al * bh) + al * bl;
51
+ const p = a * b;
52
+ let t = Split * a;
53
+ const ah = t - (t - a);
54
+ const al = a - ah;
55
+ t = Split * b;
56
+ const bh = t - (t - b);
57
+ const bl = b - bh;
58
+ productHi = p;
59
+ productLo = ah * bh - p + ah * bl + al * bh + al * bl;
56
60
  };
57
61
  /** 2^-96: a bound on the relative error of {@link decimal}'s 19-digit path. */
58
62
  const Error19 = 1 / 2 ** 96;
59
63
  /**
60
- * The double nearest to `±(high * 10^low digits + low) * 10^e10`, where
61
- * `high` holds the first 15 significant digits, and `low` the next `lowDigits`
62
- * (at most 4). Returns `undefined` when this can't tell it apart from the
63
- * neighbouring doubles, for the caller to use `Number`.
64
- *
65
- * Up to 15 digits, the digits and a power of ten up to 10^22 are exact
66
- * doubles, so one multiplication or division rounds correctly (Clinger's
67
- * fast path). Up to 19 digits, the digits are an exact sum of two doubles,
68
- * and multiplying or dividing that by an exact power of ten gives the value
69
- * to within a relative 2^-96 (double-double arithmetic). It is then rounded
70
- * once, and kept only if the bounds of that error round to the same double:
71
- * this is never the case for an exact tie, so ties go to `Number` too.
72
- */
64
+ * The double nearest to `±(high * 10^low digits + low) * 10^e10`, where
65
+ * `high` holds the first 15 significant digits, and `low` the next `lowDigits`
66
+ * (at most 4). Returns `undefined` when this can't tell it apart from the
67
+ * neighbouring doubles, for the caller to use `Number`.
68
+ *
69
+ * Up to 15 digits, the digits and a power of ten up to 10^22 are exact
70
+ * doubles, so one multiplication or division rounds correctly (Clinger's
71
+ * fast path). Up to 19 digits, the digits are an exact sum of two doubles,
72
+ * and multiplying or dividing that by an exact power of ten gives the value
73
+ * to within a relative 2^-96 (double-double arithmetic). It is then rounded
74
+ * once, and kept only if the bounds of that error round to the same double:
75
+ * this is never the case for an exact tie, so ties go to `Number` too.
76
+ */
73
77
  const decimal = (negative, high, low, lowDigits, e10) => {
74
- let v;
75
- if (lowDigits === 0) {
76
- if (e10 > 22 || e10 < -22)
77
- return undefined;
78
- v = e10 === 0 ? high : e10 > 0 ? high * exact[e10] : high / exact[-e10];
79
- }
80
- else {
81
- if (e10 > 22 || e10 < -22)
82
- return undefined;
83
- // The digits: high * 10^lowDigits + low = wh + wl, exactly
84
- twoProduct(high, exact[lowDigits]);
85
- const wh = productHi + low;
86
- const wl = productLo + (low - (wh - productHi));
87
- let xh;
88
- let xl;
89
- if (e10 >= 0) {
90
- const p = exact[e10];
91
- twoProduct(wh, p);
92
- xh = productHi;
93
- xl = productLo + wl * p;
94
- }
95
- else {
96
- const p = exact[-e10];
97
- xh = wh / p;
98
- twoProduct(xh, p);
99
- xl = ((wh - productHi) - productLo + wl) / p;
100
- }
101
- v = xh + xl;
102
- const error = (xh < 0 ? -xh : xh) * Error19;
103
- if (xh + (xl + error) !== v || xh + (xl - error) !== v)
104
- return undefined;
105
- }
106
- return negative ? -v : v;
78
+ let v;
79
+ if (lowDigits === 0) {
80
+ if (e10 > 22 || e10 < -22) return void 0;
81
+ v = e10 === 0 ? high : e10 > 0 ? high * exact[e10] : high / exact[-e10];
82
+ } else {
83
+ if (e10 > 22 || e10 < -22) return void 0;
84
+ twoProduct(high, exact[lowDigits]);
85
+ const wh = productHi + low;
86
+ const wl = productLo + (low - (wh - productHi));
87
+ let xh;
88
+ let xl;
89
+ if (e10 >= 0) {
90
+ const p = exact[e10];
91
+ twoProduct(wh, p);
92
+ xh = productHi;
93
+ xl = productLo + wl * p;
94
+ } else {
95
+ const p = exact[-e10];
96
+ xh = wh / p;
97
+ twoProduct(xh, p);
98
+ xl = (wh - productHi - productLo + wl) / p;
99
+ }
100
+ v = xh + xl;
101
+ const error = (xh < 0 ? -xh : xh) * Error19;
102
+ if (xh + (xl + error) !== v || xh + (xl - error) !== v) return void 0;
103
+ }
104
+ return negative ? -v : v;
107
105
  };
108
106
  /**
109
- * Matches and converts what `Ascii.float` matches at `i`, when it is in
110
- * decimal notation: an optional sign, then digits or "." (the other forms,
111
- * `nan` and `inf`, start with a letter). Returns the value, setting
112
- * `floatScan.end`; `Fail` if the float parser fails there (always with
113
- * `Backtrack`); or `undefined` if the text does not start like a decimal
114
- * number, for the caller to run the parser.
115
- *
116
- * Numbers with up to 19 significant digits and a decimal exponent up to 22
117
- * are converted by {@link decimal}, the rest by `Number`, so the value is
118
- * always what `Number` gives for the text.
119
- */
120
- export const scanFloatString = (s, i, end) => {
121
- const start = i;
122
- let c = i < end ? s.charCodeAt(i) : -1;
123
- if (c === 45 || c === 43) {
124
- c = i + 1 < end ? s.charCodeAt(i + 1) : -1;
125
- if (!isDigit(c) && c !== 46)
126
- return undefined;
127
- i++;
128
- }
129
- else if (!isDigit(c) && c !== 46) {
130
- return undefined;
131
- }
132
- // The first 15 significant digits, the next 4, how many there are in all,
133
- // and the decimal exponent of the last one kept
134
- let high = 0;
135
- let low = 0;
136
- let digits = 0;
137
- let e10 = 0;
138
- if (c !== 46) {
139
- do {
140
- if (digits < 15) {
141
- if (high !== 0 || c !== 48) {
142
- high = high * 10 + (c - 48);
143
- digits++;
144
- }
145
- }
146
- else {
147
- if (digits < 19)
148
- low = low * 10 + (c - 48);
149
- else
150
- e10++;
151
- digits++;
152
- }
153
- c = ++i < end ? s.charCodeAt(i) : -1;
154
- } while (isDigit(c));
155
- if (c === 46)
156
- c = ++i < end ? s.charCodeAt(i) : -1;
157
- }
158
- else {
159
- // "." must be followed by a digit
160
- c = ++i < end ? s.charCodeAt(i) : -1;
161
- if (!isDigit(c)) {
162
- floatScan.read = i;
163
- return Fail;
164
- }
165
- }
166
- while (isDigit(c)) {
167
- if (digits < 15) {
168
- if (high !== 0 || c !== 48) {
169
- high = high * 10 + (c - 48);
170
- digits++;
171
- }
172
- e10--;
173
- }
174
- else {
175
- if (digits < 19) {
176
- low = low * 10 + (c - 48);
177
- e10--;
178
- }
179
- digits++;
180
- }
181
- c = ++i < end ? s.charCodeAt(i) : -1;
182
- }
183
- let read = i;
184
- if (c === 101 || c === 69) {
185
- // The exponent is optional: without digits, it is not consumed
186
- let j = i + 1;
187
- c = j < end ? s.charCodeAt(j) : -1;
188
- const negative = c === 45;
189
- if (negative || c === 43)
190
- c = ++j < end ? s.charCodeAt(j) : -1;
191
- if (isDigit(c)) {
192
- let e = 0;
193
- do {
194
- // Past this, the number can only overflow or underflow
195
- if (e < 1e6)
196
- e = e * 10 + (c - 48);
197
- c = ++j < end ? s.charCodeAt(j) : -1;
198
- } while (isDigit(c));
199
- e10 += negative ? -e : e;
200
- i = j;
201
- }
202
- read = j;
203
- }
204
- floatScan.end = i;
205
- floatScan.read = read;
206
- const negative = s.charCodeAt(start) === 45;
207
- if (high === 0)
208
- return negative ? -0 : 0;
209
- const v = digits > 19 ? undefined : decimal(negative, high, low, digits > 15 ? digits - 15 : 0, e10);
210
- return v === undefined ? Number(s.slice(start, i)) : v;
107
+ * Matches and converts what `Ascii.float` matches at `i`, when it is in
108
+ * decimal notation: an optional sign, then digits or "." (the other forms,
109
+ * `nan` and `inf`, start with a letter). Returns the value, setting
110
+ * `floatScan.end`; `Fail` if the float parser fails there (always with
111
+ * `Backtrack`); or `undefined` if the text does not start like a decimal
112
+ * number, for the caller to run the parser.
113
+ *
114
+ * Numbers with up to 19 significant digits and a decimal exponent up to 22
115
+ * are converted by {@link decimal}, the rest by `Number`, so the value is
116
+ * always what `Number` gives for the text.
117
+ */
118
+ const scanFloatString = (s, i, end) => {
119
+ const start = i;
120
+ let c = i < end ? s.charCodeAt(i) : -1;
121
+ if (c === 45 || c === 43) {
122
+ c = i + 1 < end ? s.charCodeAt(i + 1) : -1;
123
+ if (!isDigit(c) && c !== 46) return void 0;
124
+ i++;
125
+ } else if (!isDigit(c) && c !== 46) return;
126
+ let high = 0;
127
+ let low = 0;
128
+ let digits = 0;
129
+ let e10 = 0;
130
+ if (c !== 46) {
131
+ do {
132
+ if (digits < 15) {
133
+ if (high !== 0 || c !== 48) {
134
+ high = high * 10 + (c - 48);
135
+ digits++;
136
+ }
137
+ } else {
138
+ if (digits < 19) low = low * 10 + (c - 48);
139
+ else e10++;
140
+ digits++;
141
+ }
142
+ c = ++i < end ? s.charCodeAt(i) : -1;
143
+ } while (isDigit(c));
144
+ if (c === 46) c = ++i < end ? s.charCodeAt(i) : -1;
145
+ } else {
146
+ c = ++i < end ? s.charCodeAt(i) : -1;
147
+ if (!isDigit(c)) {
148
+ floatScan.read = i;
149
+ return Fail;
150
+ }
151
+ }
152
+ while (isDigit(c)) {
153
+ if (digits < 15) {
154
+ if (high !== 0 || c !== 48) {
155
+ high = high * 10 + (c - 48);
156
+ digits++;
157
+ }
158
+ e10--;
159
+ } else {
160
+ if (digits < 19) {
161
+ low = low * 10 + (c - 48);
162
+ e10--;
163
+ }
164
+ digits++;
165
+ }
166
+ c = ++i < end ? s.charCodeAt(i) : -1;
167
+ }
168
+ let read = i;
169
+ if (c === 101 || c === 69) {
170
+ let j = i + 1;
171
+ c = j < end ? s.charCodeAt(j) : -1;
172
+ const negative = c === 45;
173
+ if (negative || c === 43) c = ++j < end ? s.charCodeAt(j) : -1;
174
+ if (isDigit(c)) {
175
+ let e = 0;
176
+ do {
177
+ if (e < 1e6) e = e * 10 + (c - 48);
178
+ c = ++j < end ? s.charCodeAt(j) : -1;
179
+ } while (isDigit(c));
180
+ e10 += negative ? -e : e;
181
+ i = j;
182
+ }
183
+ read = j;
184
+ }
185
+ floatScan.end = i;
186
+ floatScan.read = read;
187
+ const negative = s.charCodeAt(start) === 45;
188
+ if (high === 0) return negative ? -0 : 0;
189
+ const v = digits > 19 ? void 0 : decimal(negative, high, low, digits > 15 ? digits - 15 : 0, e10);
190
+ return v === void 0 ? Number(s.slice(start, i)) : v;
211
191
  };
212
192
  /** {@link scanFloatString} on bytes, or the units of an ASCII compatible encoding. */
213
- export const scanFloatBytes = (b, i, end) => {
214
- const start = i;
215
- let c = i < end ? b[i] : -1;
216
- if (c === 45 || c === 43) {
217
- c = i + 1 < end ? b[i + 1] : -1;
218
- if (!isDigit(c) && c !== 46)
219
- return undefined;
220
- i++;
221
- }
222
- else if (!isDigit(c) && c !== 46) {
223
- return undefined;
224
- }
225
- let high = 0;
226
- let low = 0;
227
- let digits = 0;
228
- let e10 = 0;
229
- if (c !== 46) {
230
- do {
231
- if (digits < 15) {
232
- if (high !== 0 || c !== 48) {
233
- high = high * 10 + (c - 48);
234
- digits++;
235
- }
236
- }
237
- else {
238
- if (digits < 19)
239
- low = low * 10 + (c - 48);
240
- else
241
- e10++;
242
- digits++;
243
- }
244
- c = ++i < end ? b[i] : -1;
245
- } while (isDigit(c));
246
- if (c === 46)
247
- c = ++i < end ? b[i] : -1;
248
- }
249
- else {
250
- c = ++i < end ? b[i] : -1;
251
- if (!isDigit(c)) {
252
- floatScan.read = i;
253
- return Fail;
254
- }
255
- }
256
- while (isDigit(c)) {
257
- if (digits < 15) {
258
- if (high !== 0 || c !== 48) {
259
- high = high * 10 + (c - 48);
260
- digits++;
261
- }
262
- e10--;
263
- }
264
- else {
265
- if (digits < 19) {
266
- low = low * 10 + (c - 48);
267
- e10--;
268
- }
269
- digits++;
270
- }
271
- c = ++i < end ? b[i] : -1;
272
- }
273
- let read = i;
274
- if (c === 101 || c === 69) {
275
- let j = i + 1;
276
- c = j < end ? b[j] : -1;
277
- const negative = c === 45;
278
- if (negative || c === 43)
279
- c = ++j < end ? b[j] : -1;
280
- if (isDigit(c)) {
281
- let e = 0;
282
- do {
283
- if (e < 1e6)
284
- e = e * 10 + (c - 48);
285
- c = ++j < end ? b[j] : -1;
286
- } while (isDigit(c));
287
- e10 += negative ? -e : e;
288
- i = j;
289
- }
290
- read = j;
291
- }
292
- floatScan.end = i;
293
- floatScan.read = read;
294
- const negative = b[start] === 45;
295
- if (high === 0)
296
- return negative ? -0 : 0;
297
- const v = digits > 19 ? undefined : decimal(negative, high, low, digits > 15 ? digits - 15 : 0, e10);
298
- return v === undefined ? Number(asciiText(b, start, i)) : v;
193
+ const scanFloatBytes = (b, i, end) => {
194
+ const start = i;
195
+ let c = i < end ? b[i] : -1;
196
+ if (c === 45 || c === 43) {
197
+ c = i + 1 < end ? b[i + 1] : -1;
198
+ if (!isDigit(c) && c !== 46) return void 0;
199
+ i++;
200
+ } else if (!isDigit(c) && c !== 46) return;
201
+ let high = 0;
202
+ let low = 0;
203
+ let digits = 0;
204
+ let e10 = 0;
205
+ if (c !== 46) {
206
+ do {
207
+ if (digits < 15) {
208
+ if (high !== 0 || c !== 48) {
209
+ high = high * 10 + (c - 48);
210
+ digits++;
211
+ }
212
+ } else {
213
+ if (digits < 19) low = low * 10 + (c - 48);
214
+ else e10++;
215
+ digits++;
216
+ }
217
+ c = ++i < end ? b[i] : -1;
218
+ } while (isDigit(c));
219
+ if (c === 46) c = ++i < end ? b[i] : -1;
220
+ } else {
221
+ c = ++i < end ? b[i] : -1;
222
+ if (!isDigit(c)) {
223
+ floatScan.read = i;
224
+ return Fail;
225
+ }
226
+ }
227
+ while (isDigit(c)) {
228
+ if (digits < 15) {
229
+ if (high !== 0 || c !== 48) {
230
+ high = high * 10 + (c - 48);
231
+ digits++;
232
+ }
233
+ e10--;
234
+ } else {
235
+ if (digits < 19) {
236
+ low = low * 10 + (c - 48);
237
+ e10--;
238
+ }
239
+ digits++;
240
+ }
241
+ c = ++i < end ? b[i] : -1;
242
+ }
243
+ let read = i;
244
+ if (c === 101 || c === 69) {
245
+ let j = i + 1;
246
+ c = j < end ? b[j] : -1;
247
+ const negative = c === 45;
248
+ if (negative || c === 43) c = ++j < end ? b[j] : -1;
249
+ if (isDigit(c)) {
250
+ let e = 0;
251
+ do {
252
+ if (e < 1e6) e = e * 10 + (c - 48);
253
+ c = ++j < end ? b[j] : -1;
254
+ } while (isDigit(c));
255
+ e10 += negative ? -e : e;
256
+ i = j;
257
+ }
258
+ read = j;
259
+ }
260
+ floatScan.end = i;
261
+ floatScan.read = read;
262
+ const negative = b[start] === 45;
263
+ if (high === 0) return negative ? -0 : 0;
264
+ const v = digits > 19 ? void 0 : decimal(negative, high, low, digits > 15 ? digits - 15 : 0, e10);
265
+ return v === void 0 ? Number(asciiText(b, start, i)) : v;
299
266
  };
300
267
  /** The text of ASCII bytes. */
301
268
  const asciiText = (b, start, end) => String.fromCharCode.apply(null, b.subarray(start, end));
269
+ //#endregion
270
+ export { floatScan, floatText, floatTexts, scanFloatBytes, scanFloatString, toFloat, toText };
271
+
302
272
  //# sourceMappingURL=numbers.js.map