smol-toml 0.1.0 → 1.0.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 +28 -20
- package/dist/date.d.ts +41 -0
- package/dist/date.js +115 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +1 -1
- package/dist/parse.d.ts +3 -2
- package/dist/parse.js +4 -4
- package/dist/primitive.d.ts +2 -1
- package/dist/primitive.js +16 -9
- package/dist/struct.d.ts +3 -2
- package/dist/struct.js +2 -2
- package/dist/util.d.ts +5 -0
- package/dist/util.js +16 -1
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
[](https://github.com/squirrelchat/smol-toml/blob/mistress/LICENSE)
|
|
3
3
|
[](https://npm.im/smol-toml)
|
|
4
4
|
|
|
5
|
-
A small, fast, and correct TOML parser. smol-toml is
|
|
5
|
+
A small, fast, and correct TOML parser. smol-toml is fully spec-compliant with TOML v1.0.0.
|
|
6
6
|
|
|
7
7
|
Why yet another TOML parser? Well, the ecosystem of TOML parsers in JavaScript is quite underwhelming, most likely due
|
|
8
8
|
to a lack of interest. With most parsers being outdated, unmaintained, non-compliant, or a combination of these, a new
|
|
@@ -10,12 +10,7 @@ parser didn't feel too out of place.
|
|
|
10
10
|
|
|
11
11
|
*[insert xkcd 927]*
|
|
12
12
|
|
|
13
|
-
smol-toml
|
|
14
|
-
- comments are not validated (absence of control characters)
|
|
15
|
-
- integers can only be between -9_007_199_254_740_991 and 9_007_199_254_740_991 instead of the recommended full 64-bit range
|
|
16
|
-
- integers outside of this range do raise an error, complying with the specification on this regard
|
|
17
|
-
|
|
18
|
-
Otherwise, smol-toml produces valid results (or errors) for all the test TOML files in https://github.com/iarna/toml-spec-tests
|
|
13
|
+
smol-toml produces valid results (or errors) for all the test TOML files in https://github.com/iarna/toml-spec-tests.
|
|
19
14
|
|
|
20
15
|
## Installation
|
|
21
16
|
```
|
|
@@ -43,28 +38,41 @@ The large TOML generator can be found [here](https://gist.github.com/cyyynthia/e
|
|
|
43
38
|
|
|
44
39
|
| | smol-toml | @iarna/toml@3.0.0 | @ltd/j-toml | fast-toml |
|
|
45
40
|
|----------------|---------------------|-------------------|----------------|----------------|
|
|
46
|
-
| Spec example | **
|
|
47
|
-
| ~5MB test file | **4.
|
|
41
|
+
| Spec example | **60,733.91 op/s** | 32,565.20 op/s | 16,781.03 op/s | 31,336.67 op/s |
|
|
42
|
+
| ~5MB test file | **4.2567 op/s** | *DNF* | 2.4873 op/s | 2.5790 op/s |
|
|
48
43
|
|
|
49
44
|
<details>
|
|
50
45
|
<summary>Detailed benchmark data</summary>
|
|
51
46
|
|
|
52
|
-
Tests ran using Vitest v0.31.0 on commit
|
|
47
|
+
Tests ran using Vitest v0.31.0 on commit 361089f3dbc30d994494bf6ec1e8e2f135531247
|
|
53
48
|
|
|
54
49
|
CPU: Intel Core i7 7700K (4.2GHz)
|
|
55
50
|
|
|
56
51
|
```
|
|
57
|
-
|
|
58
|
-
|
|
52
|
+
RUN v0.31.0
|
|
53
|
+
|
|
54
|
+
✓ bench/parseSpecExample.bench.ts (4) 2466ms
|
|
59
55
|
name hz min max mean p75 p99 p995 p999 rme samples
|
|
60
|
-
· smol-toml
|
|
61
|
-
· @iarna/toml
|
|
62
|
-
· @ltd/j-toml 16,
|
|
63
|
-
· fast-toml
|
|
64
|
-
|
|
56
|
+
· smol-toml 60,733.91 0.0145 0.2580 0.0165 0.0152 0.0319 0.0345 0.1383 ±0.46% 30367 fastest
|
|
57
|
+
· @iarna/toml 32,565.20 0.0268 0.3208 0.0307 0.0284 0.0580 0.0619 0.1699 ±0.54% 16283
|
|
58
|
+
· @ltd/j-toml 16,781.03 0.0505 1.0392 0.0596 0.0540 0.1147 0.1360 0.7657 ±1.52% 8391 slowest
|
|
59
|
+
· fast-toml 31,336.67 0.0298 0.3357 0.0319 0.0305 0.0578 0.0622 0.1580 ±0.41% 15669
|
|
60
|
+
✓ bench/parseLargeMixed.bench.ts (3) 15752ms
|
|
65
61
|
name hz min max mean p75 p99 p995 p999 rme samples
|
|
66
|
-
· smol-toml 4.
|
|
67
|
-
· @ltd/j-toml 2.
|
|
68
|
-
· fast-toml 2.
|
|
62
|
+
· smol-toml 4.2567 225.85 257.42 234.92 242.74 257.42 257.42 257.42 ±3.35% 10 fastest
|
|
63
|
+
· @ltd/j-toml 2.4873 382.66 441.12 402.05 416.25 441.12 441.12 441.12 ±3.40% 10 slowest
|
|
64
|
+
· fast-toml 2.5790 377.86 409.32 387.75 392.90 409.32 409.32 409.32 ±2.07% 10
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
BENCH Summary
|
|
68
|
+
|
|
69
|
+
smol-toml - bench/parseLargeMixed.bench.ts >
|
|
70
|
+
1.65x faster than fast-toml
|
|
71
|
+
1.71x faster than @ltd/j-toml
|
|
72
|
+
|
|
73
|
+
smol-toml - bench/parseSpecExample.bench.ts >
|
|
74
|
+
1.86x faster than @iarna/toml
|
|
75
|
+
1.94x faster than fast-toml
|
|
76
|
+
3.62x faster than @ltd/j-toml
|
|
69
77
|
```
|
|
70
78
|
</details>
|
package/dist/date.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Squirrel Chat et al., All rights reserved.
|
|
3
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
4
|
+
*
|
|
5
|
+
* Redistribution and use in source and binary forms, with or without
|
|
6
|
+
* modification, are permitted provided that the following conditions are met:
|
|
7
|
+
*
|
|
8
|
+
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
* list of conditions and the following disclaimer.
|
|
10
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
11
|
+
* this list of conditions and the following disclaimer in the
|
|
12
|
+
* documentation and/or other materials provided with the distribution.
|
|
13
|
+
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
14
|
+
* may be used to endorse or promote products derived from this software without
|
|
15
|
+
* specific prior written permission.
|
|
16
|
+
*
|
|
17
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
18
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
19
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
20
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
21
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
22
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
23
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
24
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
25
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
26
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27
|
+
*/
|
|
28
|
+
export declare let DATE_TIME_RE: RegExp;
|
|
29
|
+
export default class TomlDate extends Date {
|
|
30
|
+
#private;
|
|
31
|
+
constructor(date: string | Date);
|
|
32
|
+
isDateTime(): boolean;
|
|
33
|
+
isLocal(): boolean;
|
|
34
|
+
isDate(): boolean;
|
|
35
|
+
isTime(): boolean;
|
|
36
|
+
toISOString(): string;
|
|
37
|
+
static wrapAsOffsetDateTime(jsDate: Date, offset?: string): TomlDate;
|
|
38
|
+
static wrapAsLocalDateTime(jsDate: Date): TomlDate;
|
|
39
|
+
static wrapAsLocalDate(jsDate: Date): TomlDate;
|
|
40
|
+
static wrapAsLocalTime(jsDate: Date): TomlDate;
|
|
41
|
+
}
|
package/dist/date.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Squirrel Chat et al., All rights reserved.
|
|
3
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
4
|
+
*
|
|
5
|
+
* Redistribution and use in source and binary forms, with or without
|
|
6
|
+
* modification, are permitted provided that the following conditions are met:
|
|
7
|
+
*
|
|
8
|
+
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
* list of conditions and the following disclaimer.
|
|
10
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
11
|
+
* this list of conditions and the following disclaimer in the
|
|
12
|
+
* documentation and/or other materials provided with the distribution.
|
|
13
|
+
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
14
|
+
* may be used to endorse or promote products derived from this software without
|
|
15
|
+
* specific prior written permission.
|
|
16
|
+
*
|
|
17
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
18
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
19
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
20
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
21
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
22
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
23
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
24
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
25
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
26
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27
|
+
*/
|
|
28
|
+
export let DATE_TIME_RE = /^(\d{4}-\d{2}-\d{2})?[Tt ]?(\d{2}:\d{2}:\d{2}(?:\.\d{3,})?)?(Z|[-+]\d{2}:\d{2})?$/;
|
|
29
|
+
export default class TomlDate extends Date {
|
|
30
|
+
#hasDate;
|
|
31
|
+
#hasTime;
|
|
32
|
+
#offset;
|
|
33
|
+
constructor(date) {
|
|
34
|
+
let hasDate = true;
|
|
35
|
+
let hasTime = true;
|
|
36
|
+
let offset = 'Z';
|
|
37
|
+
if (typeof date === 'string') {
|
|
38
|
+
let match = date.match(DATE_TIME_RE);
|
|
39
|
+
if (match) {
|
|
40
|
+
if (!match[1]) {
|
|
41
|
+
hasDate = false;
|
|
42
|
+
date = `0000-01-01T${date}`;
|
|
43
|
+
}
|
|
44
|
+
hasTime = !!match[2];
|
|
45
|
+
offset = match[3] || null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
super(date);
|
|
49
|
+
if (isNaN(this.getTime())) {
|
|
50
|
+
this.#hasDate = false;
|
|
51
|
+
this.#hasTime = false;
|
|
52
|
+
this.#offset = null;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
this.#hasDate = hasDate;
|
|
56
|
+
this.#hasTime = hasTime;
|
|
57
|
+
this.#offset = offset;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
isDateTime() {
|
|
61
|
+
return this.#hasDate && this.#hasTime;
|
|
62
|
+
}
|
|
63
|
+
isLocal() {
|
|
64
|
+
return !this.#hasTime || !this.#hasTime || !this.#offset;
|
|
65
|
+
}
|
|
66
|
+
isDate() {
|
|
67
|
+
return this.#hasDate && !this.#hasTime;
|
|
68
|
+
}
|
|
69
|
+
isTime() {
|
|
70
|
+
return this.#hasTime && !this.#hasDate;
|
|
71
|
+
}
|
|
72
|
+
toISOString() {
|
|
73
|
+
let iso = super.toISOString();
|
|
74
|
+
// Local Date
|
|
75
|
+
if (this.isDate())
|
|
76
|
+
return iso.slice(0, 10);
|
|
77
|
+
// Local Time
|
|
78
|
+
if (this.isTime())
|
|
79
|
+
return iso.slice(11, 23);
|
|
80
|
+
// Local DateTime
|
|
81
|
+
if (this.#offset === null)
|
|
82
|
+
return iso.slice(0, -1);
|
|
83
|
+
// Offset DateTime
|
|
84
|
+
if (this.#offset === 'Z')
|
|
85
|
+
return iso;
|
|
86
|
+
// This part is quite annoying: JS strips the original timezone from the ISO string representation
|
|
87
|
+
// Instead of using a "modified" date and "Z", we restore the representation "as authored"
|
|
88
|
+
let offset = (+(this.#offset.slice(1, 3)) * 60) + +(this.#offset.slice(4, 6));
|
|
89
|
+
offset = this.#offset[0] === '-' ? offset : -offset;
|
|
90
|
+
let offsetDate = new Date(this.getTime() - (offset * 60e3));
|
|
91
|
+
return offsetDate.toISOString().slice(0, -1) + this.#offset;
|
|
92
|
+
}
|
|
93
|
+
static wrapAsOffsetDateTime(jsDate, offset = 'Z') {
|
|
94
|
+
let date = new TomlDate(jsDate);
|
|
95
|
+
date.#offset = offset;
|
|
96
|
+
return date;
|
|
97
|
+
}
|
|
98
|
+
static wrapAsLocalDateTime(jsDate) {
|
|
99
|
+
let date = new TomlDate(jsDate);
|
|
100
|
+
date.#offset = null;
|
|
101
|
+
return date;
|
|
102
|
+
}
|
|
103
|
+
static wrapAsLocalDate(jsDate) {
|
|
104
|
+
let date = new TomlDate(jsDate);
|
|
105
|
+
date.#hasTime = false;
|
|
106
|
+
date.#offset = null;
|
|
107
|
+
return date;
|
|
108
|
+
}
|
|
109
|
+
static wrapAsLocalTime(jsDate) {
|
|
110
|
+
let date = new TomlDate(jsDate);
|
|
111
|
+
date.#hasDate = false;
|
|
112
|
+
date.#offset = null;
|
|
113
|
+
return date;
|
|
114
|
+
}
|
|
115
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -25,4 +25,6 @@
|
|
|
25
25
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
26
26
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27
27
|
*/
|
|
28
|
-
|
|
28
|
+
import { type TomlPrimitive } from './util.js';
|
|
29
|
+
export { default as TomlDate } from './date.js';
|
|
30
|
+
export declare function parse(toml: string): Record<string, TomlPrimitive>;
|
package/dist/index.js
CHANGED
|
@@ -29,12 +29,12 @@ import { parseKey } from './struct.js';
|
|
|
29
29
|
import { extractKeyValue } from './parse.js';
|
|
30
30
|
import { skipVoid, peekTable } from './util.js';
|
|
31
31
|
import TomlError from './error.js';
|
|
32
|
+
export { default as TomlDate } from './date.js';
|
|
32
33
|
export function parse(toml) {
|
|
33
34
|
let res = {};
|
|
34
35
|
let tbl = res;
|
|
35
36
|
let seenTables = new Set();
|
|
36
37
|
let seenValues = new Set();
|
|
37
|
-
toml = toml.trim();
|
|
38
38
|
for (let ptr = skipVoid(toml, 0); ptr < toml.length;) {
|
|
39
39
|
if (toml[ptr] === '[') {
|
|
40
40
|
let isTableArray = toml[ptr + 1] === '[';
|
package/dist/parse.d.ts
CHANGED
|
@@ -25,5 +25,6 @@
|
|
|
25
25
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
26
26
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27
27
|
*/
|
|
28
|
-
|
|
29
|
-
export declare function
|
|
28
|
+
import { type TomlPrimitive } from './util.js';
|
|
29
|
+
export declare function extractValue(str: string, ptr: number, end?: string): [TomlPrimitive, number];
|
|
30
|
+
export declare function extractKeyValue(str: string, ptr: number, table: Record<string, TomlPrimitive>, seen: Set<any>, isInline?: boolean): number;
|
package/dist/parse.js
CHANGED
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
*/
|
|
28
28
|
import { parseString, parseValue } from './primitive.js';
|
|
29
29
|
import { parseKey, parseArray, parseInlineTable } from './struct.js';
|
|
30
|
-
import { peekTable, indexOfNewline, skipUntil, skipVoid } from './util.js';
|
|
30
|
+
import { peekTable, indexOfNewline, skipUntil, skipComment, skipVoid } from './util.js';
|
|
31
31
|
import TomlError from './error.js';
|
|
32
32
|
function getStringEnd(str, seek) {
|
|
33
33
|
let first = str[seek];
|
|
@@ -52,6 +52,7 @@ function sliceAndTrimEndOf(str, startPtr, endPtr, allowNewLines) {
|
|
|
52
52
|
let newlineIdx;
|
|
53
53
|
let commentIdx = value.indexOf('#');
|
|
54
54
|
if (commentIdx > -1) {
|
|
55
|
+
skipComment(str, commentIdx);
|
|
55
56
|
value = value.slice(0, commentIdx);
|
|
56
57
|
}
|
|
57
58
|
let trimmed = value.trimEnd();
|
|
@@ -70,7 +71,7 @@ function sliceAndTrimEndOf(str, startPtr, endPtr, allowNewLines) {
|
|
|
70
71
|
return trimmed;
|
|
71
72
|
}
|
|
72
73
|
export function extractValue(str, ptr, end) {
|
|
73
|
-
let c = str[ptr]
|
|
74
|
+
let c = str[ptr];
|
|
74
75
|
if (c === '[' || c === '{') {
|
|
75
76
|
let [value, endPtr] = c === '['
|
|
76
77
|
? parseArray(str, ptr)
|
|
@@ -90,8 +91,7 @@ export function extractValue(str, ptr, end) {
|
|
|
90
91
|
let endPtr;
|
|
91
92
|
if (c === '"' || c === "'") {
|
|
92
93
|
endPtr = getStringEnd(str, ptr);
|
|
93
|
-
|
|
94
|
-
return [parseString(str, ptr, endPtr), endPtr + +offset];
|
|
94
|
+
return [parseString(str, ptr, endPtr), endPtr + +(!!end && str[endPtr] === ',')];
|
|
95
95
|
}
|
|
96
96
|
endPtr = skipUntil(str, ptr, end);
|
|
97
97
|
let valStr = sliceAndTrimEndOf(str, ptr, endPtr - (+(str[endPtr - 1] === ',')), end === ']');
|
package/dist/primitive.d.ts
CHANGED
|
@@ -25,5 +25,6 @@
|
|
|
25
25
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
26
26
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27
27
|
*/
|
|
28
|
+
import TomlDate from './date.js';
|
|
28
29
|
export declare function parseString(str: string, ptr?: number, endPtr?: number): string;
|
|
29
|
-
export declare function parseValue(value: string, toml: string, ptr: number): boolean | number |
|
|
30
|
+
export declare function parseValue(value: string, toml: string, ptr: number): boolean | number | TomlDate;
|
package/dist/primitive.js
CHANGED
|
@@ -26,10 +26,11 @@
|
|
|
26
26
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27
27
|
*/
|
|
28
28
|
import { skipVoid } from './util.js';
|
|
29
|
+
import TomlDate, { DATE_TIME_RE } from './date.js';
|
|
29
30
|
import TomlError from './error.js';
|
|
30
31
|
let NUM_REGEX = /^(0x|0b|0o|[+-])?[0-9a-f_]+(\.[0-9a-f_]+)?([e][+-]?[0-9a-f_]+)?$/i;
|
|
31
32
|
let INT_REGEX = /^(0x|0b|0o|[+-])?[0-9a-f]+$/;
|
|
32
|
-
let LEADING_ZERO = /^[+-]?0\d
|
|
33
|
+
let LEADING_ZERO = /^[+-]?0\d/;
|
|
33
34
|
let ESCAPE_REGEX = /^[0-9a-f]{4,8}$/i;
|
|
34
35
|
let ESC_MAP = {
|
|
35
36
|
b: '\b',
|
|
@@ -91,7 +92,7 @@ export function parseString(str, ptr = 0, endPtr = str.length) {
|
|
|
91
92
|
});
|
|
92
93
|
}
|
|
93
94
|
try {
|
|
94
|
-
parsed += String.fromCodePoint(
|
|
95
|
+
parsed += String.fromCodePoint(parseInt(code, 16));
|
|
95
96
|
}
|
|
96
97
|
catch {
|
|
97
98
|
throw new TomlError('invalid unicode escape', {
|
|
@@ -153,12 +154,18 @@ export function parseValue(value, toml, ptr) {
|
|
|
153
154
|
return numeric;
|
|
154
155
|
}
|
|
155
156
|
// Date
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
157
|
+
if (DATE_TIME_RE.test(value)) {
|
|
158
|
+
let date = new TomlDate(value);
|
|
159
|
+
if (isNaN(date.getTime())) {
|
|
160
|
+
throw new TomlError('invalid date', {
|
|
161
|
+
toml: toml,
|
|
162
|
+
ptr: ptr
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
return date;
|
|
162
166
|
}
|
|
163
|
-
|
|
167
|
+
throw new TomlError('invalid value', {
|
|
168
|
+
toml: toml,
|
|
169
|
+
ptr: ptr
|
|
170
|
+
});
|
|
164
171
|
}
|
package/dist/struct.d.ts
CHANGED
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
26
26
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27
27
|
*/
|
|
28
|
+
import { type TomlPrimitive } from './util.js';
|
|
28
29
|
export declare function parseKey(str: string, startPtr?: number, endPtr?: number): string[];
|
|
29
|
-
export declare function parseInlineTable(str: string, ptr: number): [Record<string,
|
|
30
|
-
export declare function parseArray(str: string, ptr: number): [
|
|
30
|
+
export declare function parseInlineTable(str: string, ptr: number): [Record<string, TomlPrimitive>, number];
|
|
31
|
+
export declare function parseArray(str: string, ptr: number): [TomlPrimitive[], number];
|
package/dist/struct.js
CHANGED
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
*/
|
|
28
28
|
import { parseString } from './primitive.js';
|
|
29
29
|
import { extractValue, extractKeyValue } from './parse.js';
|
|
30
|
-
import { indexOfNewline } from './util.js';
|
|
30
|
+
import { indexOfNewline, skipComment } from './util.js';
|
|
31
31
|
import TomlError from './error.js';
|
|
32
32
|
let KEY_PART_RE = /^[a-zA-Z0-9-_ \t]+$/;
|
|
33
33
|
export function parseKey(str, startPtr = 0, endPtr = str.length) {
|
|
@@ -138,7 +138,7 @@ export function parseArray(str, ptr) {
|
|
|
138
138
|
});
|
|
139
139
|
}
|
|
140
140
|
else if (c === '#')
|
|
141
|
-
ptr =
|
|
141
|
+
ptr = skipComment(str, ptr);
|
|
142
142
|
else if (c !== ' ' && c !== '\t' && c !== '\n' && c !== '\r') {
|
|
143
143
|
let e = extractValue(str, ptr - 1, ']');
|
|
144
144
|
res.push(e[0]);
|
package/dist/util.d.ts
CHANGED
|
@@ -25,7 +25,12 @@
|
|
|
25
25
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
26
26
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27
27
|
*/
|
|
28
|
+
import TomlDate from './date.js';
|
|
29
|
+
export type TomlPrimitive = string | number | boolean | TomlDate | {
|
|
30
|
+
[key: string]: TomlPrimitive;
|
|
31
|
+
} | TomlPrimitive[];
|
|
28
32
|
export declare function indexOfNewline(str: string, start?: number, end?: number): number;
|
|
33
|
+
export declare function skipComment(str: string, ptr: number): number;
|
|
29
34
|
export declare function skipVoid(str: string, ptr: number, banNewLines?: boolean, banComments?: boolean): number;
|
|
30
35
|
export declare function skipUntil(str: string, ptr: number, end?: string): number;
|
|
31
36
|
export declare function peekTable(table: Record<string, any>, key: string[], seen: Set<any>, allowSuper?: boolean): [string, Record<string, any>] | null;
|
package/dist/util.js
CHANGED
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
26
26
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27
27
|
*/
|
|
28
|
+
import TomlDate from './date.js';
|
|
28
29
|
import TomlError from './error.js';
|
|
29
30
|
export function indexOfNewline(str, start = 0, end = str.length) {
|
|
30
31
|
for (let i = start; i < end; i++) {
|
|
@@ -33,13 +34,27 @@ export function indexOfNewline(str, start = 0, end = str.length) {
|
|
|
33
34
|
}
|
|
34
35
|
return -1;
|
|
35
36
|
}
|
|
37
|
+
export function skipComment(str, ptr) {
|
|
38
|
+
for (let i = ptr; i < str.length; i++) {
|
|
39
|
+
let c = str[i];
|
|
40
|
+
if (c < '\t' || c === '\x0b' || c === '\x0c' || c === '\x7f' || (c > '\x0d' && c < '\x20')) {
|
|
41
|
+
throw new TomlError('control characters are not allowed in commebts', {
|
|
42
|
+
toml: str,
|
|
43
|
+
ptr: ptr,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
if (c === '\n' || c === '\r')
|
|
47
|
+
return i;
|
|
48
|
+
}
|
|
49
|
+
return -1;
|
|
50
|
+
}
|
|
36
51
|
export function skipVoid(str, ptr, banNewLines, banComments) {
|
|
37
52
|
let c;
|
|
38
53
|
while ((c = str[ptr]) === ' ' || c === '\t' || (!banNewLines && (c === '\n' || c === '\r')))
|
|
39
54
|
ptr++;
|
|
40
55
|
if (banComments || c !== '#')
|
|
41
56
|
return ptr;
|
|
42
|
-
ptr =
|
|
57
|
+
ptr = skipComment(str, ptr);
|
|
43
58
|
return ptr < 0 ? str.length : skipVoid(str, ptr, banNewLines);
|
|
44
59
|
}
|
|
45
60
|
export function skipUntil(str, ptr, end) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "smol-toml",
|
|
3
|
-
"version": "
|
|
4
|
-
"keywords": [
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"toml",
|
|
6
|
+
"parser"
|
|
7
|
+
],
|
|
5
8
|
"description": "A small, fast, and correct TOML parser",
|
|
6
9
|
"repository": "git@github.com:squirrelchat/smol-toml.git",
|
|
7
10
|
"author": "Cynthia <cyyynthia@borkenware.com>",
|