smol-toml 1.3.2 → 1.3.3

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.
@@ -0,0 +1,31 @@
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
+ import { type TomlPrimitive } from './util.js';
29
+ export declare function parseKey(str: string, ptr: number, end?: string): [string[], number];
30
+ export declare function parseInlineTable(str: string, ptr: number, depth?: number): [Record<string, TomlPrimitive>, number];
31
+ export declare function parseArray(str: string, ptr: number, depth?: number): [TomlPrimitive[], number];
package/dist/struct.js ADDED
@@ -0,0 +1,202 @@
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
+ import { parseString } from './primitive.js';
29
+ import { extractValue } from './extract.js';
30
+ import { skipComment, indexOfNewline, getStringEnd, skipVoid } from './util.js';
31
+ import { TomlError } from './error.js';
32
+ let KEY_PART_RE = /^[a-zA-Z0-9-_]+[ \t]*$/;
33
+ export function parseKey(str, ptr, end = '=') {
34
+ let dot = ptr - 1;
35
+ let parsed = [];
36
+ let endPtr = str.indexOf(end, ptr);
37
+ if (endPtr < 0) {
38
+ throw new TomlError('incomplete key-value: cannot find end of key', {
39
+ toml: str,
40
+ ptr: ptr
41
+ });
42
+ }
43
+ do {
44
+ let c = str[ptr = ++dot];
45
+ // If it's whitespace, ignore
46
+ if (c !== ' ' && c !== '\t') {
47
+ // If it's a string
48
+ if (c === '"' || c === "'") {
49
+ if (c === str[ptr + 1] && c === str[ptr + 2]) {
50
+ throw new TomlError('multiline strings are not allowed in keys', {
51
+ toml: str,
52
+ ptr: ptr,
53
+ });
54
+ }
55
+ let eos = getStringEnd(str, ptr);
56
+ if (eos < 0) {
57
+ throw new TomlError('unfinished string encountered', {
58
+ toml: str,
59
+ ptr: ptr,
60
+ });
61
+ }
62
+ dot = str.indexOf('.', eos);
63
+ let strEnd = str.slice(eos, dot < 0 || dot > endPtr ? endPtr : dot);
64
+ let newLine = indexOfNewline(strEnd);
65
+ if (newLine > -1) {
66
+ throw new TomlError('newlines are not allowed in keys', {
67
+ toml: str,
68
+ ptr: ptr + dot + newLine,
69
+ });
70
+ }
71
+ if (strEnd.trimStart()) {
72
+ throw new TomlError('found extra tokens after the string part', {
73
+ toml: str,
74
+ ptr: eos,
75
+ });
76
+ }
77
+ if (endPtr < eos) {
78
+ endPtr = str.indexOf(end, eos);
79
+ if (endPtr < 0) {
80
+ throw new TomlError('incomplete key-value: cannot find end of key', {
81
+ toml: str,
82
+ ptr: ptr,
83
+ });
84
+ }
85
+ }
86
+ parsed.push(parseString(str, ptr, eos));
87
+ }
88
+ else {
89
+ // Normal raw key part consumption and validation
90
+ dot = str.indexOf('.', ptr);
91
+ let part = str.slice(ptr, dot < 0 || dot > endPtr ? endPtr : dot);
92
+ if (!KEY_PART_RE.test(part)) {
93
+ throw new TomlError('only letter, numbers, dashes and underscores are allowed in keys', {
94
+ toml: str,
95
+ ptr: ptr,
96
+ });
97
+ }
98
+ parsed.push(part.trimEnd());
99
+ }
100
+ }
101
+ // Until there's no more dot
102
+ } while (dot + 1 && dot < endPtr);
103
+ return [parsed, skipVoid(str, endPtr + 1, true, true)];
104
+ }
105
+ export function parseInlineTable(str, ptr, depth = -1) {
106
+ let res = {};
107
+ let seen = new Set();
108
+ let c;
109
+ let comma = 0;
110
+ ptr++;
111
+ while ((c = str[ptr++]) !== '}' && c) {
112
+ if (c === '\n') {
113
+ throw new TomlError('newlines are not allowed in inline tables', {
114
+ toml: str,
115
+ ptr: ptr - 1
116
+ });
117
+ }
118
+ else if (c === '#') {
119
+ throw new TomlError('inline tables cannot contain comments', {
120
+ toml: str,
121
+ ptr: ptr - 1
122
+ });
123
+ }
124
+ else if (c === ',') {
125
+ throw new TomlError('expected key-value, found comma', {
126
+ toml: str,
127
+ ptr: ptr - 1
128
+ });
129
+ }
130
+ else if (c !== ' ' && c !== '\t') {
131
+ let k;
132
+ let t = res;
133
+ let hasOwn = false;
134
+ let [key, keyEndPtr] = parseKey(str, ptr - 1);
135
+ for (let i = 0; i < key.length; i++) {
136
+ if (i)
137
+ t = hasOwn ? t[k] : (t[k] = {});
138
+ k = key[i];
139
+ if ((hasOwn = Object.hasOwn(t, k)) && (typeof t[k] !== 'object' || seen.has(t[k]))) {
140
+ throw new TomlError('trying to redefine an already defined value', {
141
+ toml: str,
142
+ ptr: ptr
143
+ });
144
+ }
145
+ if (!hasOwn && k === '__proto__') {
146
+ Object.defineProperty(t, k, { enumerable: true, configurable: true, writable: true });
147
+ }
148
+ }
149
+ if (hasOwn) {
150
+ throw new TomlError('trying to redefine an already defined value', {
151
+ toml: str,
152
+ ptr: ptr
153
+ });
154
+ }
155
+ let [value, valueEndPtr] = extractValue(str, keyEndPtr, '}', depth - 1);
156
+ seen.add(value);
157
+ t[k] = value;
158
+ ptr = valueEndPtr;
159
+ comma = str[ptr - 1] === ',' ? ptr - 1 : 0;
160
+ }
161
+ }
162
+ if (comma) {
163
+ throw new TomlError('trailing commas are not allowed in inline tables', {
164
+ toml: str,
165
+ ptr: comma
166
+ });
167
+ }
168
+ if (!c) {
169
+ throw new TomlError('unfinished table encountered', {
170
+ toml: str,
171
+ ptr: ptr
172
+ });
173
+ }
174
+ return [res, ptr];
175
+ }
176
+ export function parseArray(str, ptr, depth = -1) {
177
+ let res = [];
178
+ let c;
179
+ ptr++;
180
+ while ((c = str[ptr++]) !== ']' && c) {
181
+ if (c === ',') {
182
+ throw new TomlError('expected value, found comma', {
183
+ toml: str,
184
+ ptr: ptr - 1
185
+ });
186
+ }
187
+ else if (c === '#')
188
+ ptr = skipComment(str, ptr);
189
+ else if (c !== ' ' && c !== '\t' && c !== '\n' && c !== '\r') {
190
+ let e = extractValue(str, ptr - 1, ']', depth - 1);
191
+ res.push(e[0]);
192
+ ptr = e[1];
193
+ }
194
+ }
195
+ if (!c) {
196
+ throw new TomlError('unfinished array encountered', {
197
+ toml: str,
198
+ ptr: ptr
199
+ });
200
+ }
201
+ return [res, ptr];
202
+ }
package/dist/util.d.ts ADDED
@@ -0,0 +1,36 @@
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
+ import type { TomlDate } from './date.js';
29
+ export type TomlPrimitive = string | number | boolean | TomlDate | {
30
+ [key: string]: TomlPrimitive;
31
+ } | TomlPrimitive[];
32
+ export declare function indexOfNewline(str: string, start?: number, end?: number): number;
33
+ export declare function skipComment(str: string, ptr: number): number;
34
+ export declare function skipVoid(str: string, ptr: number, banNewLines?: boolean, banComments?: boolean): number;
35
+ export declare function skipUntil(str: string, ptr: number, sep: string, end?: string, banNewLines?: boolean): number;
36
+ export declare function getStringEnd(str: string, seek: number): number;
package/dist/util.js ADDED
@@ -0,0 +1,103 @@
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
+ import { TomlError } from './error.js';
29
+ export function indexOfNewline(str, start = 0, end = str.length) {
30
+ let idx = str.indexOf('\n', start);
31
+ if (str[idx - 1] === '\r')
32
+ idx--;
33
+ return idx <= end ? idx : -1;
34
+ }
35
+ export function skipComment(str, ptr) {
36
+ for (let i = ptr; i < str.length; i++) {
37
+ let c = str[i];
38
+ if (c === '\n')
39
+ return i;
40
+ if (c === '\r' && str[i + 1] === '\n')
41
+ return i + 1;
42
+ if ((c < '\x20' && c !== '\t') || c === '\x7f') {
43
+ throw new TomlError('control characters are not allowed in comments', {
44
+ toml: str,
45
+ ptr: ptr,
46
+ });
47
+ }
48
+ }
49
+ return str.length;
50
+ }
51
+ export function skipVoid(str, ptr, banNewLines, banComments) {
52
+ let c;
53
+ while ((c = str[ptr]) === ' ' || c === '\t' || (!banNewLines && (c === '\n' || c === '\r' && str[ptr + 1] === '\n')))
54
+ ptr++;
55
+ return banComments || c !== '#'
56
+ ? ptr
57
+ : skipVoid(str, skipComment(str, ptr), banNewLines);
58
+ }
59
+ export function skipUntil(str, ptr, sep, end, banNewLines = false) {
60
+ if (!end) {
61
+ ptr = indexOfNewline(str, ptr);
62
+ return ptr < 0 ? str.length : ptr;
63
+ }
64
+ for (let i = ptr; i < str.length; i++) {
65
+ let c = str[i];
66
+ if (c === '#') {
67
+ i = indexOfNewline(str, i);
68
+ }
69
+ else if (c === sep) {
70
+ return i + 1;
71
+ }
72
+ else if (c === end) {
73
+ return i;
74
+ }
75
+ else if (banNewLines && (c === '\n' || c === '\r' && str[i + 1] === '\n')) {
76
+ return i;
77
+ }
78
+ }
79
+ throw new TomlError('cannot find end of structure', {
80
+ toml: str,
81
+ ptr: ptr
82
+ });
83
+ }
84
+ export function getStringEnd(str, seek) {
85
+ let first = str[seek];
86
+ let target = first === str[seek + 1] && str[seek + 1] === str[seek + 2]
87
+ ? str.slice(seek, seek + 3)
88
+ : first;
89
+ seek += target.length - 1;
90
+ do
91
+ seek = str.indexOf(target, ++seek);
92
+ while (seek > -1 && first !== "'" && str[seek - 1] === '\\' && (str[seek - 2] !== '\\' || str[seek - 3] === '\\'));
93
+ if (seek > -1) {
94
+ seek += target.length;
95
+ if (target.length > 1) {
96
+ if (str[seek] === first)
97
+ seek++;
98
+ if (str[seek] === first)
99
+ seek++;
100
+ }
101
+ }
102
+ return seek;
103
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "smol-toml",
3
3
  "license": "BSD-3-Clause",
4
- "version": "1.3.2",
4
+ "version": "1.3.3",
5
5
  "description": "A small, fast, and correct TOML parser/serializer",
6
6
  "author": "Cynthia <cyyynthia@borkenware.com>",
7
7
  "repository": "github:squirrelchat/smol-toml",