@decimalturn/toml-patch 3.2.0 → 3.2.1-dev
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/dist/dev/comment-alignment.js +372 -0
- package/dist/dev/comment-alignment.js.map +1 -0
- package/dist/dev/comment-ownership.js +660 -0
- package/dist/dev/comment-ownership.js.map +1 -0
- package/dist/dev/cst-source.js +118 -0
- package/dist/dev/cst-source.js.map +1 -0
- package/dist/dev/cst.js +68 -0
- package/dist/dev/cst.js.map +1 -0
- package/dist/dev/cursor.js +66 -0
- package/dist/dev/cursor.js.map +1 -0
- package/dist/dev/date-format.js +196 -0
- package/dist/dev/date-format.js.map +1 -0
- package/dist/dev/decode-utf8.js +27 -0
- package/dist/dev/decode-utf8.js.map +1 -0
- package/dist/dev/diff-lite.js +123 -0
- package/dist/dev/diff-lite.js.map +1 -0
- package/dist/dev/diff.js +265 -0
- package/dist/dev/diff.js.map +1 -0
- package/dist/dev/encode-lite.js +193 -0
- package/dist/dev/encode-lite.js.map +1 -0
- package/dist/dev/escape-preference.js +166 -0
- package/dist/dev/escape-preference.js.map +1 -0
- package/dist/dev/find-by-path.js +111 -0
- package/dist/dev/find-by-path.js.map +1 -0
- package/dist/dev/formatter.js +298 -0
- package/dist/dev/formatter.js.map +1 -0
- package/dist/dev/generate.js +465 -0
- package/dist/dev/generate.js.map +1 -0
- package/dist/dev/inline-comma-space.js +13 -0
- package/dist/dev/inline-comma-space.js.map +1 -0
- package/dist/dev/inline-format.js +106 -0
- package/dist/dev/inline-format.js.map +1 -0
- package/dist/dev/inline-layout.js +107 -0
- package/dist/dev/inline-layout.js.map +1 -0
- package/dist/dev/line-ending-backslash.js +320 -0
- package/dist/dev/line-ending-backslash.js.map +1 -0
- package/dist/dev/literal-string.js +46 -0
- package/dist/dev/literal-string.js.map +1 -0
- package/dist/dev/location.js +104 -0
- package/dist/dev/location.js.map +1 -0
- package/dist/dev/parse-error.js +27 -0
- package/dist/dev/parse-error.js.map +1 -0
- package/dist/dev/parse-js.js +150 -0
- package/dist/dev/parse-js.js.map +1 -0
- package/dist/dev/parse-string.js +122 -0
- package/dist/dev/parse-string.js.map +1 -0
- package/dist/dev/parse-toml.js +714 -0
- package/dist/dev/parse-toml.js.map +1 -0
- package/dist/dev/patch-lite.js +4 -0
- package/dist/dev/patch-lite2.js +96 -0
- package/dist/dev/patch-lite2.js.map +1 -0
- package/dist/dev/patch.js +2652 -0
- package/dist/dev/patch.js.map +1 -0
- package/dist/dev/smol-toml-date.js +61 -0
- package/dist/dev/smol-toml-date.js.map +1 -0
- package/dist/dev/to-js.js +303 -0
- package/dist/dev/to-js.js.map +1 -0
- package/dist/dev/to-toml.js +874 -0
- package/dist/dev/to-toml.js.map +1 -0
- package/dist/dev/tokenizer.js +225 -0
- package/dist/dev/tokenizer.js.map +1 -0
- package/dist/dev/toml-document.js +220 -0
- package/dist/dev/toml-document.js.map +1 -0
- package/dist/dev/toml-format.js +528 -0
- package/dist/dev/toml-format.js.map +1 -0
- package/dist/dev/toml-patch.js +94 -0
- package/dist/dev/toml-patch.js.map +1 -0
- package/dist/dev/traverse.js +61 -0
- package/dist/dev/traverse.js.map +1 -0
- package/dist/dev/truncate.js +64 -0
- package/dist/dev/truncate.js.map +1 -0
- package/dist/dev/update-order.js +235 -0
- package/dist/dev/update-order.js.map +1 -0
- package/dist/dev/utils.js +244 -0
- package/dist/dev/utils.js.map +1 -0
- package/dist/dev/writer.js +758 -0
- package/dist/dev/writer.js.map +1 -0
- package/package.json +13 -104
- package/LICENSE +0 -22
- package/README.md +0 -200
- package/dist/patch-lite.js +0 -20
- package/dist/toml-patch.js +0 -84
|
@@ -0,0 +1,714 @@
|
|
|
1
|
+
//! @decimalturn/toml-patch v3.2.1 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
|
+
import { clearCachedLines, cloneLocation, clonePosition, getLine } from "./location.js";
|
|
3
|
+
import ParseError from "./parse-error.js";
|
|
4
|
+
import { DOUBLE_QUOTE, SINGLE_QUOTE, tokenize } from "./tokenizer.js";
|
|
5
|
+
import { parseString } from "./parse-string.js";
|
|
6
|
+
import Cursor from "./cursor.js";
|
|
7
|
+
import { setCommaSpace } from "./inline-comma-space.js";
|
|
8
|
+
import { DateFormatHelper, LocalDate, LocalDateTime, LocalTime, OffsetDateTime } from "./date-format.js";
|
|
9
|
+
|
|
10
|
+
//#region src/parse-toml.ts
|
|
11
|
+
const dateFormatHelper = DateFormatHelper;
|
|
12
|
+
const TRUE = "true";
|
|
13
|
+
const FALSE = "false";
|
|
14
|
+
const HAS_E = /e/i;
|
|
15
|
+
const IS_DIVIDER = /_/g;
|
|
16
|
+
const IS_INF = /^[+-]?inf$/;
|
|
17
|
+
const IS_NAN = /^[+-]?nan$/;
|
|
18
|
+
const IS_HEX = /^[+-]?0x/i;
|
|
19
|
+
/**
|
|
20
|
+
* Check if a character code is a valid bare key character (A-Za-z0-9_-).
|
|
21
|
+
* Uses charCode range checks instead of regex for speed.
|
|
22
|
+
*/
|
|
23
|
+
function isBareKeyCode(code) {
|
|
24
|
+
return code >= 65 && code <= 90 || code >= 97 && code <= 122 || code >= 48 && code <= 57 || code === 95 || code === 45;
|
|
25
|
+
}
|
|
26
|
+
const IS_OCTAL = /^[+-]?0o/i;
|
|
27
|
+
const IS_BINARY = /^[+-]?0b/i;
|
|
28
|
+
/** Validate bare key characters (A-Za-z0-9_- only). Throws ParseError on invalid char. */
|
|
29
|
+
function validateBareKeyChars(raw, input, startLoc) {
|
|
30
|
+
for (let i = 0; i < raw.length; i++) if (!isBareKeyCode(raw.charCodeAt(i))) throw new ParseError(input, {
|
|
31
|
+
line: startLoc.line,
|
|
32
|
+
column: startLoc.column + i
|
|
33
|
+
}, `Invalid bare key char '${raw[i]}'`);
|
|
34
|
+
}
|
|
35
|
+
/** Reject multiline strings used as keys. */
|
|
36
|
+
function rejectMultilineKey(raw, input, loc) {
|
|
37
|
+
if (raw.startsWith("\"\"\"") || raw.startsWith("'''")) throw new ParseError(input, loc, "Multiline strings cannot be keys");
|
|
38
|
+
}
|
|
39
|
+
/** Parse a string key, wrapping parse errors as ParseErrors. */
|
|
40
|
+
function parseKeyString(raw, input, loc) {
|
|
41
|
+
try {
|
|
42
|
+
return parseString(raw);
|
|
43
|
+
} catch (err) {
|
|
44
|
+
throw new ParseError(input, loc, err.message);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** Validate underscore placement in a numeric token. */
|
|
48
|
+
function validateUnderscores(str, input, loc, signed = true) {
|
|
49
|
+
if (str.endsWith("_")) throw new ParseError(input, loc, "Underscore must be between digits");
|
|
50
|
+
if (signed ? /^[+-]?_/.test(str) : str.startsWith("_")) throw new ParseError(input, loc, "Underscore must be between digits");
|
|
51
|
+
if (/__/.test(str)) throw new ParseError(input, loc, "Consecutive underscores not allowed");
|
|
52
|
+
}
|
|
53
|
+
/** Validate no leading zeros in a numeric token (skips hex/octal/binary). */
|
|
54
|
+
function validateLeadingZeros(raw, input, loc, label) {
|
|
55
|
+
const withoutUnderscores = raw.replace(IS_DIVIDER, "");
|
|
56
|
+
if (/^[+-]?0\d/.test(withoutUnderscores) && !IS_HEX.test(raw) && !IS_OCTAL.test(raw) && !IS_BINARY.test(raw)) throw new ParseError(input, loc, `Leading zeros are not allowed in ${label}`);
|
|
57
|
+
}
|
|
58
|
+
/** Validate exponent syntax in a float token. */
|
|
59
|
+
function validateExponent(str, fullRaw, input, loc) {
|
|
60
|
+
if (/_[eE]/.test(str)) throw new ParseError(input, loc, "Underscore before exponent is not allowed");
|
|
61
|
+
if (/[eE][+-]?_/.test(str)) throw new ParseError(input, loc, "Underscore at start of exponent is not allowed");
|
|
62
|
+
if (/[eE][+-]?$/.test(str)) throw new ParseError(input, loc, `Invalid float "${fullRaw}": incomplete exponent`);
|
|
63
|
+
if (/[eE][+-]?.*\./.test(str)) throw new ParseError(input, loc, `Invalid float "${fullRaw}": decimal point not allowed in exponent`);
|
|
64
|
+
}
|
|
65
|
+
/** Validate time-component digit counts (must be exactly 2 each). */
|
|
66
|
+
function validateTimeDigits(hour, minute, second, raw, input, loc) {
|
|
67
|
+
if (hour.length !== 2) throw new ParseError(input, loc, `"${raw}": hour must be 2 digits`);
|
|
68
|
+
if (minute.length !== 2) throw new ParseError(input, loc, `"${raw}": minute must be 2 digits`);
|
|
69
|
+
if (second && second.length !== 2) throw new ParseError(input, loc, `"${raw}": second must be 2 digits`);
|
|
70
|
+
}
|
|
71
|
+
/** Validate time-component value ranges. */
|
|
72
|
+
function validateTimeRange(hour, minute, second, raw, input, loc) {
|
|
73
|
+
if (hour !== void 0) {
|
|
74
|
+
const h = parseInt(hour, 10);
|
|
75
|
+
if (h < 0 || h > 23) throw new ParseError(input, loc, `"${raw}": hour must be 00-23`);
|
|
76
|
+
}
|
|
77
|
+
if (minute !== void 0) {
|
|
78
|
+
const m = parseInt(minute, 10);
|
|
79
|
+
if (m < 0 || m > 59) throw new ParseError(input, loc, `"${raw}": minute must be 00-59`);
|
|
80
|
+
}
|
|
81
|
+
if (second !== void 0) {
|
|
82
|
+
const s = parseInt(second, 10);
|
|
83
|
+
if (s < 0 || s > 60) throw new ParseError(input, loc, `"${raw}": second must be 00-60`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** Validate that decimal integer contains only ASCII digits (0-9) and underscores.
|
|
87
|
+
Only validates decimal integers, not prefixed (hex/octal/binary) ones. */
|
|
88
|
+
function validateDecimalDigits(raw, input, loc) {
|
|
89
|
+
if (IS_HEX.test(raw) || IS_OCTAL.test(raw) || IS_BINARY.test(raw)) return;
|
|
90
|
+
const digitsOnly = raw.replace(/^[+-]/, "").replace(/_/g, "");
|
|
91
|
+
if (!/^[0-9]+$/.test(digitsOnly)) throw new ParseError(input, loc, `Invalid integer "${raw}"`);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Validate a prefixed integer (hex, octal, or binary).
|
|
95
|
+
* @param prefix - lowercase prefix like "0x", "0o", "0b"
|
|
96
|
+
* @param validDigits - regex matching valid digit characters (e.g. /^[0-9a-fA-F]+$/)
|
|
97
|
+
* @param name - display name like "Hexadecimal", "Octal", "Binary"
|
|
98
|
+
*/
|
|
99
|
+
function validatePrefixedInt(raw, input, loc, prefix, validDigits, name, invalidMsg) {
|
|
100
|
+
const upper = prefix[0] + prefix[1].toUpperCase();
|
|
101
|
+
if (new RegExp("^[+\\-]?" + upper.replace(/([[\]])/g, "\\$1")).test(raw)) throw new ParseError(input, loc, `${name} prefix must be lowercase "${prefix}"`);
|
|
102
|
+
if (new RegExp("^[+\\-]?" + prefix + "_", "i").test(raw)) throw new ParseError(input, loc, "Underscore must be between digits");
|
|
103
|
+
const stripRe = new RegExp("^[+\\-]?" + prefix, "i");
|
|
104
|
+
const numericPart = raw.replace(stripRe, "");
|
|
105
|
+
if (!numericPart || numericPart === "_" || numericPart.startsWith("_")) throw new ParseError(input, loc, `Incomplete ${name.toLowerCase()} number`);
|
|
106
|
+
const digits = numericPart.replace(/_/g, "");
|
|
107
|
+
if (!validDigits.test(digits)) throw new ParseError(input, loc, invalidMsg ?? `Invalid ${name.toLowerCase()} digits`);
|
|
108
|
+
if (/^[+-]/.test(raw)) throw new ParseError(input, loc, `${name} numbers cannot have a sign prefix`);
|
|
109
|
+
}
|
|
110
|
+
function* parseTOML(input, newlineState) {
|
|
111
|
+
const cursor = new Cursor(tokenize(input, newlineState));
|
|
112
|
+
try {
|
|
113
|
+
while (!cursor.next().done) {
|
|
114
|
+
const blocks = walkBlock(cursor, input);
|
|
115
|
+
for (const block of blocks) yield block;
|
|
116
|
+
}
|
|
117
|
+
} finally {
|
|
118
|
+
clearCachedLines();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Continues parsing TOML from a remaining string and appends the results to an existing CST.
|
|
123
|
+
*
|
|
124
|
+
* @param existingAst - The existing CST to append to
|
|
125
|
+
* @param remainingString - The remaining TOML string to parse
|
|
126
|
+
* @param startPosition - Absolute source position of the first character of
|
|
127
|
+
* `remainingString`. When provided, the newly parsed blocks' locations are
|
|
128
|
+
* rebased from suffix-relative to absolute positions.
|
|
129
|
+
* @returns A new complete CST with both the existing and newly parsed items
|
|
130
|
+
*/
|
|
131
|
+
function* continueParsingTOML(existingAst, remainingString, startPosition) {
|
|
132
|
+
for (const item of existingAst) yield item;
|
|
133
|
+
const cursor = new Cursor(tokenize(remainingString));
|
|
134
|
+
try {
|
|
135
|
+
while (!cursor.next().done) {
|
|
136
|
+
const blocks = walkBlock(cursor, remainingString);
|
|
137
|
+
for (const block of blocks) {
|
|
138
|
+
if (startPosition) rebaseBlock(block, startPosition);
|
|
139
|
+
yield block;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
} finally {
|
|
143
|
+
clearCachedLines();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Offsets a suffix-relative position to its absolute position in the full
|
|
148
|
+
* source. The first line of the suffix is shifted by `start.column` while
|
|
149
|
+
* subsequent lines are only shifted by `start.line - 1`.
|
|
150
|
+
*/
|
|
151
|
+
function offsetPosition(position, start) {
|
|
152
|
+
return {
|
|
153
|
+
line: position.line + start.line - 1,
|
|
154
|
+
column: position.line === 1 ? position.column + start.column : position.column
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Rebases every node location in a block parsed from a suffix back to absolute
|
|
159
|
+
* positions, so source attachment and the source-aware writer see consistent
|
|
160
|
+
* offsets after an incremental `update()`.
|
|
161
|
+
*/
|
|
162
|
+
function rebaseBlock(block, start) {
|
|
163
|
+
const stack = [block];
|
|
164
|
+
while (stack.length > 0) {
|
|
165
|
+
const node = stack.pop();
|
|
166
|
+
node.loc.start = offsetPosition(node.loc.start, start);
|
|
167
|
+
node.loc.end = offsetPosition(node.loc.end, start);
|
|
168
|
+
if (node.type === "KeyValue") {
|
|
169
|
+
const keyValue = node;
|
|
170
|
+
stack.push(keyValue.value, keyValue.key);
|
|
171
|
+
} else if (node.type === "TableKey" || node.type === "TableArrayKey" || node.type === "InlineItem") stack.push(node.item);
|
|
172
|
+
else if (node.type === "Table" || node.type === "TableArray") {
|
|
173
|
+
const table = node;
|
|
174
|
+
stack.push(table.key);
|
|
175
|
+
for (let index = table.items.length - 1; index >= 0; index--) stack.push(table.items[index]);
|
|
176
|
+
} else if (node.type === "InlineTable" || node.type === "InlineArray") {
|
|
177
|
+
const items = node.items;
|
|
178
|
+
for (let index = items.length - 1; index >= 0; index--) stack.push(items[index]);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function comment(cursor) {
|
|
183
|
+
return {
|
|
184
|
+
type: "Comment",
|
|
185
|
+
loc: cursor.value.loc,
|
|
186
|
+
raw: cursor.value.raw
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
function table(cursor, input) {
|
|
190
|
+
const type = !cursor.peek().done && cursor.peek().value.type === "Bracket" ? "TableArray" : "Table";
|
|
191
|
+
const is_table = type === "Table";
|
|
192
|
+
if (is_table && cursor.value.raw !== "[") throw new ParseError(input, cursor.value.loc.start, `Expected table opening "[", found ${cursor.value.raw}`);
|
|
193
|
+
if (!is_table) {
|
|
194
|
+
const next = cursor.peek();
|
|
195
|
+
if (next.done) throw new ParseError(input, cursor.value.loc.start, "Expected \"[[\" for Array of Tables, found end of input");
|
|
196
|
+
if (cursor.value.raw !== "[" || next.value.raw !== "[") throw new ParseError(input, cursor.value.loc.start, `Expected "[[", found ${cursor.value.raw + next.value.raw}`);
|
|
197
|
+
const firstBracket = cursor.value;
|
|
198
|
+
const secondBracket = next.value;
|
|
199
|
+
if (firstBracket.loc.end.line !== secondBracket.loc.start.line || firstBracket.loc.end.column !== secondBracket.loc.start.column) throw new ParseError(input, firstBracket.loc.start, "\"[[\" brackets must be adjacent (no whitespace)");
|
|
200
|
+
}
|
|
201
|
+
const key = is_table ? {
|
|
202
|
+
type: "TableKey",
|
|
203
|
+
loc: cursor.value.loc
|
|
204
|
+
} : {
|
|
205
|
+
type: "TableArrayKey",
|
|
206
|
+
loc: cursor.value.loc
|
|
207
|
+
};
|
|
208
|
+
cursor.next();
|
|
209
|
+
if (type === "TableArray") cursor.next();
|
|
210
|
+
if (cursor.done) throw new ParseError(input, key.loc.start, `Expected table key, reached end of file`);
|
|
211
|
+
if (cursor.value.type === "Bracket" && cursor.value.raw === "]") throw new ParseError(input, cursor.value.loc.start, type === "TableArray" ? "Array of Tables header [[]] requires a table name" : "Table header [] requires a table name");
|
|
212
|
+
const raw = cursor.value.raw;
|
|
213
|
+
rejectMultilineKey(raw, input, cursor.value.loc.start);
|
|
214
|
+
if (!(raw.startsWith("\"") || raw.startsWith("'"))) validateBareKeyChars(raw, input, cursor.value.loc.start);
|
|
215
|
+
let keyValue;
|
|
216
|
+
keyValue = [parseKeyString(cursor.value.raw, input, cursor.value.loc.start)];
|
|
217
|
+
key.item = {
|
|
218
|
+
type: "Key",
|
|
219
|
+
loc: cursor.value.loc,
|
|
220
|
+
raw: cursor.value.raw,
|
|
221
|
+
value: keyValue
|
|
222
|
+
};
|
|
223
|
+
while (!cursor.peek().done && cursor.peek().value.type === "Dot") {
|
|
224
|
+
cursor.next();
|
|
225
|
+
const dot = cursor.value;
|
|
226
|
+
cursor.next();
|
|
227
|
+
const partRaw = cursor.value.raw;
|
|
228
|
+
if (!(partRaw.startsWith("\"") || partRaw.startsWith("'"))) validateBareKeyChars(partRaw, input, cursor.value.loc.start);
|
|
229
|
+
const before = " ".repeat(dot.loc.start.column - key.item.loc.end.column);
|
|
230
|
+
const after = " ".repeat(cursor.value.loc.start.column - dot.loc.end.column);
|
|
231
|
+
key.item.loc.end = cursor.value.loc.end;
|
|
232
|
+
key.item.raw += `${before}.${after}${cursor.value.raw}`;
|
|
233
|
+
try {
|
|
234
|
+
key.item.value.push(parseString(cursor.value.raw));
|
|
235
|
+
} catch (err) {
|
|
236
|
+
const e = err;
|
|
237
|
+
throw new ParseError(input, cursor.value.loc.start, e.message);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
cursor.next();
|
|
241
|
+
if (!cursor.done) {
|
|
242
|
+
const headerStartLine = is_table ? key.loc.start.line : key.loc.start.line;
|
|
243
|
+
if (cursor.value.loc.start.line !== headerStartLine) throw new ParseError(input, cursor.value.loc.start, is_table ? `Table header must be single-line` : `Array of Tables header must be single-line`);
|
|
244
|
+
}
|
|
245
|
+
if (is_table && (cursor.done || cursor.value.raw !== "]")) throw new ParseError(input, cursor.done ? key.item.loc.end : cursor.value.loc.start, `Expected table closing "]", found ${cursor.done ? "end of file" : cursor.value.raw}`);
|
|
246
|
+
if (!is_table && (cursor.done || cursor.peek().done || cursor.value.raw !== "]" || cursor.peek().value.raw !== "]")) throw new ParseError(input, cursor.done || cursor.peek().done ? key.item.loc.end : cursor.value.loc.start, `Expected "]]" closing, found ${cursor.done || cursor.peek().done ? "end of file" : cursor.value.raw + cursor.peek().value.raw}`);
|
|
247
|
+
if (!is_table) {
|
|
248
|
+
const firstBracket = cursor.value;
|
|
249
|
+
const secondBracket = cursor.peek().value;
|
|
250
|
+
if (firstBracket.loc.end.line !== secondBracket.loc.start.line || firstBracket.loc.end.column !== secondBracket.loc.start.column) throw new ParseError(input, firstBracket.loc.start, "\"]]\" brackets must be adjacent (no whitespace)");
|
|
251
|
+
}
|
|
252
|
+
if (!is_table) cursor.next();
|
|
253
|
+
key.loc.end = cursor.value.loc.end;
|
|
254
|
+
if (!cursor.peek().done) {
|
|
255
|
+
const nextToken = cursor.peek().value;
|
|
256
|
+
if (nextToken.loc.start.line === key.loc.end.line && nextToken.type !== "Comment") throw new ParseError(input, nextToken.loc.start, `Extra content after ${is_table ? "table" : "Array of Tables"} header`);
|
|
257
|
+
}
|
|
258
|
+
let items = [];
|
|
259
|
+
while (!cursor.peek().done && cursor.peek().value.type !== "Bracket") {
|
|
260
|
+
cursor.next();
|
|
261
|
+
const blocks = walkBlock(cursor, input);
|
|
262
|
+
for (let bi = 0; bi < blocks.length; bi++) items.push(blocks[bi]);
|
|
263
|
+
}
|
|
264
|
+
return {
|
|
265
|
+
type: is_table ? "Table" : "TableArray",
|
|
266
|
+
loc: {
|
|
267
|
+
start: clonePosition(key.loc.start),
|
|
268
|
+
end: items.length ? clonePosition(items[items.length - 1].loc.end) : clonePosition(key.loc.end)
|
|
269
|
+
},
|
|
270
|
+
key,
|
|
271
|
+
items
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
function string(cursor, input) {
|
|
275
|
+
const value = parseKeyString(cursor.value.raw, input, cursor.value.loc.start);
|
|
276
|
+
return {
|
|
277
|
+
type: "String",
|
|
278
|
+
loc: cursor.value.loc,
|
|
279
|
+
raw: cursor.value.raw,
|
|
280
|
+
value
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
function boolean(cursor) {
|
|
284
|
+
return {
|
|
285
|
+
type: "Boolean",
|
|
286
|
+
loc: cursor.value.loc,
|
|
287
|
+
value: cursor.value.raw === TRUE
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
function datetime(cursor, input) {
|
|
291
|
+
let loc = cursor.value.loc;
|
|
292
|
+
let raw = cursor.value.raw;
|
|
293
|
+
let value;
|
|
294
|
+
if (!cursor.peek().done && cursor.peek().value.type === "Literal" && dateFormatHelper.IS_FULL_DATE.test(raw) && /^\d/.test(cursor.peek().value.raw) && dateFormatHelper.IS_FULL_TIME.test(cursor.peek().value.raw)) {
|
|
295
|
+
const start = loc.start;
|
|
296
|
+
cursor.next();
|
|
297
|
+
loc = {
|
|
298
|
+
start,
|
|
299
|
+
end: cursor.value.loc.end
|
|
300
|
+
};
|
|
301
|
+
raw += ` ${cursor.value.raw}`;
|
|
302
|
+
}
|
|
303
|
+
if (!cursor.peek().done && cursor.peek().value.type === "Dot") {
|
|
304
|
+
const start = loc.start;
|
|
305
|
+
cursor.next();
|
|
306
|
+
if (cursor.peek().done || cursor.peek().value.type !== "Literal") throw new ParseError(input, cursor.value.loc.end, `Expected fractional value for DateTime`);
|
|
307
|
+
cursor.next();
|
|
308
|
+
loc = {
|
|
309
|
+
start,
|
|
310
|
+
end: cursor.value.loc.end
|
|
311
|
+
};
|
|
312
|
+
raw += `.${cursor.value.raw}`;
|
|
313
|
+
}
|
|
314
|
+
validateDateTimeFormat(raw, input, loc.start);
|
|
315
|
+
if (!dateFormatHelper.IS_FULL_DATE.test(raw)) {
|
|
316
|
+
if (dateFormatHelper.IS_TIME_ONLY.test(raw)) value = new LocalTime(raw, raw);
|
|
317
|
+
else {
|
|
318
|
+
const [local_date] = (/* @__PURE__ */ new Date()).toISOString().split("T");
|
|
319
|
+
value = /* @__PURE__ */ new Date(`${local_date}T${raw}`);
|
|
320
|
+
}
|
|
321
|
+
} else if (dateFormatHelper.IS_DATE_ONLY.test(raw)) value = new LocalDate(raw);
|
|
322
|
+
else if (dateFormatHelper.IS_LOCAL_DATETIME_T.test(raw)) value = new LocalDateTime(raw, false);
|
|
323
|
+
else if (dateFormatHelper.IS_LOCAL_DATETIME_SPACE.test(raw)) value = new LocalDateTime(raw, true);
|
|
324
|
+
else if (dateFormatHelper.IS_OFFSET_DATETIME_T.test(raw)) value = new OffsetDateTime(raw, false);
|
|
325
|
+
else if (dateFormatHelper.IS_OFFSET_DATETIME_SPACE.test(raw)) value = new OffsetDateTime(raw, true);
|
|
326
|
+
else value = new Date(raw.replace(" ", "T"));
|
|
327
|
+
return {
|
|
328
|
+
type: "DateTime",
|
|
329
|
+
loc,
|
|
330
|
+
raw,
|
|
331
|
+
value
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
function getDaysInMonth(year, month) {
|
|
335
|
+
const daysPerMonth = [
|
|
336
|
+
31,
|
|
337
|
+
28,
|
|
338
|
+
31,
|
|
339
|
+
30,
|
|
340
|
+
31,
|
|
341
|
+
30,
|
|
342
|
+
31,
|
|
343
|
+
31,
|
|
344
|
+
30,
|
|
345
|
+
31,
|
|
346
|
+
30,
|
|
347
|
+
31
|
|
348
|
+
];
|
|
349
|
+
if (month === 2) return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0 ? 29 : 28;
|
|
350
|
+
return daysPerMonth[month - 1];
|
|
351
|
+
}
|
|
352
|
+
function validateDateTimeFormat(raw, input, loc) {
|
|
353
|
+
if (/\.([Zz]|[+-])/.test(raw)) throw new ParseError(input, loc, `"${raw}": fractional seconds needs digit after dot`);
|
|
354
|
+
if (/[+-]$/.test(raw)) throw new ParseError(input, loc, `"${raw}": offset needs HH:MM`);
|
|
355
|
+
const offsetMatch = /\d{2}:\d{2}/.test(raw) ? raw.match(/([+-])(\d+)(:?)(\d*)$/) : null;
|
|
356
|
+
if (offsetMatch) {
|
|
357
|
+
const fullOffset = offsetMatch[0];
|
|
358
|
+
const hours = offsetMatch[2];
|
|
359
|
+
const colon = offsetMatch[3];
|
|
360
|
+
const minutes = offsetMatch[4];
|
|
361
|
+
if (colon !== ":") throw new ParseError(input, loc, `Offset "${fullOffset}": missing colon separator`);
|
|
362
|
+
if (hours.length !== 2) throw new ParseError(input, loc, `Offset "${fullOffset}": hour must be 2 digits`);
|
|
363
|
+
if (!minutes || minutes.length === 0) throw new ParseError(input, loc, `Offset "${fullOffset}": minute required`);
|
|
364
|
+
if (minutes.length !== 2) throw new ParseError(input, loc, `Offset "${fullOffset}": minute must be 2 digits`);
|
|
365
|
+
const hourNum = parseInt(hours, 10);
|
|
366
|
+
if (hourNum < 0 || hourNum > 23) throw new ParseError(input, loc, `Offset "${fullOffset}": hour must be 00-23`);
|
|
367
|
+
const minuteNum = parseInt(minutes, 10);
|
|
368
|
+
if (minuteNum < 0 || minuteNum > 59) throw new ParseError(input, loc, `Offset "${fullOffset}": minute must be 00-59`);
|
|
369
|
+
}
|
|
370
|
+
if (!/^\d{4}-\d{2}-\d{2}(?:[Tt ]\d{2}:\d{2}(?::\d{2})?(?:\.\d+)?(?:[Zz]|[+-]\d{2}:\d{2})?)?$/.test(raw) && !/^\d{2}:\d{2}(?::\d{2})?(?:\.\d+)?$/.test(raw)) {
|
|
371
|
+
if (/^\d{4}-\d{2}-\d{2}T$/.test(raw)) throw new ParseError(input, loc, `"${raw}": 'T' requires time component`);
|
|
372
|
+
if (/^\d{4}-\d{2}-\d{2}[a-su-zA-SU-Z]/.test(raw)) throw new ParseError(input, loc, `Invalid date "${raw}": unexpected character after date`);
|
|
373
|
+
if (/^\d{4}-\d{2}-\d{2}\d{2}:\d{2}/.test(raw)) throw new ParseError(input, loc, `"${raw}": missing 'T' or space separator`);
|
|
374
|
+
throw new ParseError(input, loc, `Invalid datetime "${raw}"`);
|
|
375
|
+
}
|
|
376
|
+
const yearMatch = raw.match(/^(\d+)-/);
|
|
377
|
+
if (yearMatch && yearMatch[1].length !== 4) throw new ParseError(input, loc, `"${raw}": year must be 4 digits`);
|
|
378
|
+
const dateMatch = raw.match(/^(\d+)-(\d+)-(\d+)/);
|
|
379
|
+
if (dateMatch) {
|
|
380
|
+
const [, , month, day] = dateMatch;
|
|
381
|
+
if (month.length !== 2) throw new ParseError(input, loc, `"${raw}": month must be 2 digits`);
|
|
382
|
+
if (day.length !== 2) throw new ParseError(input, loc, `"${raw}": day must be 2 digits`);
|
|
383
|
+
}
|
|
384
|
+
const timeMatch = raw.match(/[T ](\d+):(\d+)(?::(\d+))?/);
|
|
385
|
+
if (timeMatch) {
|
|
386
|
+
const [, hour, minute, second] = timeMatch;
|
|
387
|
+
validateTimeDigits(hour, minute, second, raw, input, loc);
|
|
388
|
+
}
|
|
389
|
+
const timeOnlyMatch = raw.match(/^(\d+):(\d+)(?::(\d+))?/);
|
|
390
|
+
if (timeOnlyMatch && !dateMatch) {
|
|
391
|
+
const [, hour, minute, second] = timeOnlyMatch;
|
|
392
|
+
validateTimeDigits(hour, minute, second, raw, input, loc);
|
|
393
|
+
}
|
|
394
|
+
const dateTimeMatch = raw.match(/^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?)?/);
|
|
395
|
+
const timeOnlyMatchExact = raw.match(/^(\d{2}):(\d{2})(?::(\d{2}))?/);
|
|
396
|
+
if (dateTimeMatch) {
|
|
397
|
+
const [, year, month, day, hour, minute, second] = dateTimeMatch;
|
|
398
|
+
const monthNum = parseInt(month, 10);
|
|
399
|
+
if (monthNum < 1 || monthNum > 12) throw new ParseError(input, loc, `"${raw}": month must be 01-12`);
|
|
400
|
+
const dayNum = parseInt(day, 10);
|
|
401
|
+
if (dayNum < 1 || dayNum > 31) throw new ParseError(input, loc, `"${raw}": day must be 01-31`);
|
|
402
|
+
if (dayNum > getDaysInMonth(parseInt(year, 10), monthNum)) throw new ParseError(input, loc, `"${raw}": day ${day} invalid for ${year}-${month}`);
|
|
403
|
+
validateTimeRange(hour, minute, second, raw, input, loc);
|
|
404
|
+
} else if (timeOnlyMatchExact) {
|
|
405
|
+
const [, hour, minute, second] = timeOnlyMatchExact;
|
|
406
|
+
validateTimeRange(hour, minute, second, raw, input, loc);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
function float(cursor, input) {
|
|
410
|
+
let loc = cursor.value.loc;
|
|
411
|
+
let raw = cursor.value.raw;
|
|
412
|
+
let value;
|
|
413
|
+
if (IS_INF.test(raw)) value = raw.startsWith("-") ? -Infinity : Infinity;
|
|
414
|
+
else if (IS_NAN.test(raw)) {
|
|
415
|
+
const buf = /* @__PURE__ */ new Float64Array(1);
|
|
416
|
+
const view = new DataView(buf.buffer);
|
|
417
|
+
if (raw.startsWith("-")) {
|
|
418
|
+
view.setUint32(0, 0, true);
|
|
419
|
+
view.setUint32(4, 4294443008, true);
|
|
420
|
+
} else {
|
|
421
|
+
view.setUint32(0, 0, true);
|
|
422
|
+
view.setUint32(4, 2146959360, true);
|
|
423
|
+
}
|
|
424
|
+
value = buf[0];
|
|
425
|
+
const sign = raw.startsWith("+") ? "+" : raw.startsWith("-") ? "-" : void 0;
|
|
426
|
+
return {
|
|
427
|
+
type: "Float",
|
|
428
|
+
loc,
|
|
429
|
+
raw,
|
|
430
|
+
value,
|
|
431
|
+
nanSign: sign
|
|
432
|
+
};
|
|
433
|
+
} else if (!cursor.peek().done && cursor.peek().value.type === "Dot") {
|
|
434
|
+
const start = loc.start;
|
|
435
|
+
{
|
|
436
|
+
if (HAS_E.test(raw) && !IS_HEX.test(raw)) throw new ParseError(input, loc.start, `Float "${raw}": dot after exponent`);
|
|
437
|
+
const intPart = raw;
|
|
438
|
+
validateLeadingZeros(intPart, input, loc.start, "the integer part of a float");
|
|
439
|
+
const withoutSign = intPart.replace(/^[+-]/, "");
|
|
440
|
+
if (withoutSign === "" || withoutSign === "_") throw new ParseError(input, loc.start, `Float: digit required before dot`);
|
|
441
|
+
validateUnderscores(intPart, input, loc.start);
|
|
442
|
+
}
|
|
443
|
+
cursor.next();
|
|
444
|
+
if (cursor.peek().done || cursor.peek().value.type !== "Literal") throw new ParseError(input, cursor.value.loc.end, `Expected fraction value for Float`);
|
|
445
|
+
cursor.next();
|
|
446
|
+
raw += `.${cursor.value.raw}`;
|
|
447
|
+
loc = {
|
|
448
|
+
start,
|
|
449
|
+
end: cursor.value.loc.end
|
|
450
|
+
};
|
|
451
|
+
{
|
|
452
|
+
const fracPart = cursor.value.raw;
|
|
453
|
+
if (!/^\d/.test(fracPart)) throw new ParseError(input, cursor.value.loc.start, `Float: fraction must start with digit, found "${fracPart}"`);
|
|
454
|
+
validateUnderscores(fracPart, input, cursor.value.loc.start, false);
|
|
455
|
+
validateExponent(fracPart, raw, input, cursor.value.loc.start);
|
|
456
|
+
}
|
|
457
|
+
value = Number(raw.replace(IS_DIVIDER, ""));
|
|
458
|
+
} else {
|
|
459
|
+
validateUnderscores(raw, input, loc.start);
|
|
460
|
+
validateExponent(raw, raw, input, loc.start);
|
|
461
|
+
if (!cursor.peek().done && cursor.peek().value.type === "Dot") throw new ParseError(input, cursor.peek().value.loc.start, `Float "${raw}.": dot after exponent`);
|
|
462
|
+
validateLeadingZeros(raw, input, loc.start, "the integer part of a float");
|
|
463
|
+
value = Number(raw.replace(IS_DIVIDER, ""));
|
|
464
|
+
}
|
|
465
|
+
if (Number.isNaN(value) && !IS_NAN.test(raw)) throw new ParseError(input, loc.start, `Invalid float "${raw}"`);
|
|
466
|
+
return {
|
|
467
|
+
type: "Float",
|
|
468
|
+
loc,
|
|
469
|
+
raw,
|
|
470
|
+
value
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
function integer(cursor, input) {
|
|
474
|
+
const raw = cursor.value.raw;
|
|
475
|
+
const loc = cursor.value.loc;
|
|
476
|
+
if (/^\d{1,}-\d{1,}/.test(raw) || /^\d{1,}:\d{1,}/.test(raw) || /^\d{6}-\d{2}$/.test(raw)) throw new ParseError(input, loc.start, `Invalid integer "${raw}"`);
|
|
477
|
+
if (raw === "-0" || raw === "+0") return {
|
|
478
|
+
type: "Integer",
|
|
479
|
+
loc,
|
|
480
|
+
raw,
|
|
481
|
+
value: 0
|
|
482
|
+
};
|
|
483
|
+
if (/^[+-]{2,}/.test(raw)) throw new ParseError(input, loc.start, "Double sign is not allowed in integers");
|
|
484
|
+
validateLeadingZeros(raw, input, loc.start, "decimal integers");
|
|
485
|
+
validateUnderscores(raw, input, loc.start);
|
|
486
|
+
validateDecimalDigits(raw, input, loc.start);
|
|
487
|
+
let radix = 10;
|
|
488
|
+
if (IS_HEX.test(raw)) {
|
|
489
|
+
radix = 16;
|
|
490
|
+
validatePrefixedInt(raw, input, loc.start, "0x", /^[0-9a-fA-F]+$/, "Hexadecimal");
|
|
491
|
+
} else if (IS_OCTAL.test(raw)) {
|
|
492
|
+
radix = 8;
|
|
493
|
+
validatePrefixedInt(raw, input, loc.start, "0o", /^[0-7]+$/, "Octal", "Invalid octal digits (must be 0-7)");
|
|
494
|
+
} else if (IS_BINARY.test(raw)) {
|
|
495
|
+
radix = 2;
|
|
496
|
+
validatePrefixedInt(raw, input, loc.start, "0b", /^[01]+$/, "Binary", "Invalid binary digits (must be 0 or 1)");
|
|
497
|
+
}
|
|
498
|
+
const value = parseInt(raw.replace(IS_DIVIDER, "").replace(IS_OCTAL, "").replace(IS_BINARY, ""), radix);
|
|
499
|
+
if (Number.isNaN(value)) throw new ParseError(input, loc.start, `Invalid integer "${raw}"`);
|
|
500
|
+
return {
|
|
501
|
+
type: "Integer",
|
|
502
|
+
loc,
|
|
503
|
+
raw,
|
|
504
|
+
value
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Walk a Block (Comment, Table, or KeyValue)
|
|
509
|
+
* This new version avoids recursion for key-value pairs to improve performance on large files.
|
|
510
|
+
* @param cursor Cursor<Token>
|
|
511
|
+
* @param input string
|
|
512
|
+
* @returns Block[]
|
|
513
|
+
*/
|
|
514
|
+
function walkBlock(cursor, input) {
|
|
515
|
+
if (cursor.value.type === "Comment") return [comment(cursor)];
|
|
516
|
+
else if (cursor.value.type === "Bracket") return [table(cursor, input)];
|
|
517
|
+
else if (cursor.value.type === "Literal") return keyValue(cursor, input);
|
|
518
|
+
else if (cursor.value.type === "Equal") throw new ParseError(input, cursor.value.loc.start, `Missing key before '='`);
|
|
519
|
+
else throw new ParseError(input, cursor.value.loc.start, `Unexpected token "${cursor.value.type}"`);
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Walk a KeyValue pair or Comment
|
|
523
|
+
* This new version avoids recursion for key-value pairs to improve performance on large files.
|
|
524
|
+
* @param cursor Cursor<Token>
|
|
525
|
+
* @param input string
|
|
526
|
+
* @returns Array<KeyValue | Comment>
|
|
527
|
+
*/
|
|
528
|
+
function keyValue(cursor, input) {
|
|
529
|
+
const rawKeyToken = cursor.value.raw;
|
|
530
|
+
if (rawKeyToken.endsWith(":")) throw new ParseError(input, {
|
|
531
|
+
line: cursor.value.loc.start.line,
|
|
532
|
+
column: cursor.value.loc.start.column + [...rawKeyToken].length - 1
|
|
533
|
+
}, `Use '=' to separate keys and values, not ':'`);
|
|
534
|
+
rejectMultilineKey(rawKeyToken, input, cursor.value.loc.start);
|
|
535
|
+
if (!(rawKeyToken.startsWith("\"") || rawKeyToken.startsWith("'"))) validateBareKeyChars(rawKeyToken, input, cursor.value.loc.start);
|
|
536
|
+
let keyValue2;
|
|
537
|
+
keyValue2 = [parseKeyString(cursor.value.raw, input, cursor.value.loc.start)];
|
|
538
|
+
const key = {
|
|
539
|
+
type: "Key",
|
|
540
|
+
loc: cursor.value.loc,
|
|
541
|
+
raw: cursor.value.raw,
|
|
542
|
+
value: keyValue2
|
|
543
|
+
};
|
|
544
|
+
while (!cursor.peek().done && cursor.peek().value.type === "Dot") {
|
|
545
|
+
cursor.next();
|
|
546
|
+
const dot = cursor.value;
|
|
547
|
+
cursor.next();
|
|
548
|
+
const partRaw = cursor.value.raw;
|
|
549
|
+
rejectMultilineKey(partRaw, input, cursor.value.loc.start);
|
|
550
|
+
if (!(partRaw.startsWith("\"") || partRaw.startsWith("'"))) validateBareKeyChars(partRaw, input, cursor.value.loc.start);
|
|
551
|
+
const keyLine = getLine(input, key.loc.end);
|
|
552
|
+
const dotLine = getLine(input, dot.loc.end);
|
|
553
|
+
const before = keyLine.slice(key.loc.end.column, dot.loc.start.column);
|
|
554
|
+
const after = dotLine.slice(dot.loc.end.column, cursor.value.loc.start.column);
|
|
555
|
+
key.loc.end = cursor.value.loc.end;
|
|
556
|
+
key.raw += `${before}.${after}${cursor.value.raw}`;
|
|
557
|
+
key.value.push(parseKeyString(cursor.value.raw, input, cursor.value.loc.start));
|
|
558
|
+
}
|
|
559
|
+
cursor.next();
|
|
560
|
+
if (!cursor.done && cursor.value.loc.start.line !== key.loc.end.line) throw new ParseError(input, cursor.value.loc.start, `"=" must be on same line as key`);
|
|
561
|
+
if (cursor.done || cursor.value.type !== "Equal") {
|
|
562
|
+
if (!cursor.done && cursor.value.raw === ":") throw new ParseError(input, cursor.value.loc.start, `Use '=' to separate keys and values, not ':'`);
|
|
563
|
+
throw new ParseError(input, cursor.done ? key.loc.end : cursor.value.loc.start, `Expected "=" for key-value`);
|
|
564
|
+
}
|
|
565
|
+
const equals = cursor.value.loc.start.column;
|
|
566
|
+
const equalsLine = cursor.value.loc.start.line;
|
|
567
|
+
cursor.next();
|
|
568
|
+
if (cursor.done) throw new ParseError(input, key.loc.start, `Expected value, reached EOF`);
|
|
569
|
+
if (cursor.value.loc.start.line !== equalsLine) throw new ParseError(input, cursor.value.loc.start, `Value must be on same line as '='`);
|
|
570
|
+
if (cursor.done) throw new ParseError(input, key.loc.start, `Expected value for key-value`);
|
|
571
|
+
const results = walkValue(cursor, input);
|
|
572
|
+
const value = results[0];
|
|
573
|
+
if (!cursor.peek().done) {
|
|
574
|
+
const nextToken = cursor.peek().value;
|
|
575
|
+
if (nextToken.type === "Dot" && nextToken.loc.start.line === value.loc.end.line && (value.type === "Float" || value.type === "Integer")) throw new ParseError(input, nextToken.loc.start, "Invalid number: multiple decimal points not allowed");
|
|
576
|
+
if ((nextToken.type === "Literal" || nextToken.type === "Bracket") && nextToken.loc.start.line === value.loc.end.line) throw new ParseError(input, nextToken.loc.start, "Key/value pairs must be separated by a newline");
|
|
577
|
+
}
|
|
578
|
+
results[0] = {
|
|
579
|
+
type: "KeyValue",
|
|
580
|
+
key,
|
|
581
|
+
value,
|
|
582
|
+
loc: {
|
|
583
|
+
start: clonePosition(key.loc.start),
|
|
584
|
+
end: clonePosition(value.loc.end)
|
|
585
|
+
},
|
|
586
|
+
equals
|
|
587
|
+
};
|
|
588
|
+
return results;
|
|
589
|
+
}
|
|
590
|
+
function walkValue(cursor, input) {
|
|
591
|
+
if (cursor.value.type === "Literal") {
|
|
592
|
+
const raw = cursor.value.raw;
|
|
593
|
+
if (raw[0] === DOUBLE_QUOTE || raw[0] === SINGLE_QUOTE) return [string(cursor, input)];
|
|
594
|
+
else if (raw === TRUE || raw === FALSE) return [boolean(cursor)];
|
|
595
|
+
else if (/^\d/.test(raw) && (/^\d{1,}-\d{1,}/.test(raw) || /^\d{1,}:\d{1,}/.test(raw))) return [datetime(cursor, input)];
|
|
596
|
+
else if (!cursor.peek().done && cursor.peek().value.type === "Dot" || IS_INF.test(raw) || IS_NAN.test(raw) || HAS_E.test(raw) && !IS_HEX.test(raw)) return [float(cursor, input)];
|
|
597
|
+
else return [integer(cursor, input)];
|
|
598
|
+
} else if (cursor.value.type === "Curly") {
|
|
599
|
+
const [inline_table, comments] = inlineTable(cursor, input);
|
|
600
|
+
return [inline_table, ...comments];
|
|
601
|
+
} else if (cursor.value.type === "Bracket") {
|
|
602
|
+
const [inline_array, comments] = inlineArray(cursor, input);
|
|
603
|
+
return [inline_array, ...comments];
|
|
604
|
+
} else if (cursor.value.type === "Dot") throw new ParseError(input, cursor.value.loc.start, `Number cannot start with a dot`);
|
|
605
|
+
else throw new ParseError(input, cursor.value.loc.start, `Unrecognized token type`);
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Measures the horizontal gap a separating comma occupies between two adjacent
|
|
609
|
+
* inline siblings: 1 for a compact list (`[1,2]`), 2 for a spaced list
|
|
610
|
+
* (`[1, 2]`). Stored on the container at parse time because the writer needs
|
|
611
|
+
* this original spacing later, long after the items' own positions have been
|
|
612
|
+
* shifted by pending edits.
|
|
613
|
+
*/
|
|
614
|
+
function detectCommaSpace(items) {
|
|
615
|
+
for (let i = 1; i < items.length; i++) {
|
|
616
|
+
const prev = items[i - 1];
|
|
617
|
+
const next = items[i];
|
|
618
|
+
if (prev.loc.end.line === next.loc.start.line) {
|
|
619
|
+
const gap = next.loc.start.column - prev.loc.end.column;
|
|
620
|
+
if (gap >= 1) return gap;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
function inlineTable(cursor, input) {
|
|
625
|
+
if (cursor.value.raw !== "{") throw new ParseError(input, cursor.value.loc.start, `Expected "{" for inline table`);
|
|
626
|
+
const value = {
|
|
627
|
+
type: "InlineTable",
|
|
628
|
+
loc: cursor.value.loc,
|
|
629
|
+
items: []
|
|
630
|
+
};
|
|
631
|
+
const comments = [];
|
|
632
|
+
cursor.next();
|
|
633
|
+
while (!cursor.done && !(cursor.value.type === "Curly" && cursor.value.raw === "}")) {
|
|
634
|
+
if (cursor.value.type === "Comment") {
|
|
635
|
+
comments.push(comment(cursor));
|
|
636
|
+
cursor.next();
|
|
637
|
+
continue;
|
|
638
|
+
}
|
|
639
|
+
if (cursor.value.type === "Comma") {
|
|
640
|
+
const previous = value.items[value.items.length - 1];
|
|
641
|
+
if (!previous) throw new ParseError(input, cursor.value.loc.start, "Leading comma in inline table");
|
|
642
|
+
if (previous.comma) throw new ParseError(input, cursor.value.loc.start, "Consecutive commas in inline table");
|
|
643
|
+
previous.comma = true;
|
|
644
|
+
previous.loc.end = cursor.value.loc.start;
|
|
645
|
+
cursor.next();
|
|
646
|
+
continue;
|
|
647
|
+
}
|
|
648
|
+
const previous = value.items[value.items.length - 1];
|
|
649
|
+
if (previous && !previous.comma) throw new ParseError(input, cursor.value.loc.start, "Missing comma between inline table items");
|
|
650
|
+
const blocks = walkBlock(cursor, input);
|
|
651
|
+
const item = blocks[0];
|
|
652
|
+
if (item.type === "KeyValue") {
|
|
653
|
+
value.items.push({
|
|
654
|
+
type: "InlineItem",
|
|
655
|
+
loc: cloneLocation(item.loc),
|
|
656
|
+
item,
|
|
657
|
+
comma: false
|
|
658
|
+
});
|
|
659
|
+
for (let ci = 1; ci < blocks.length; ci++) comments.push(blocks[ci]);
|
|
660
|
+
}
|
|
661
|
+
cursor.next();
|
|
662
|
+
}
|
|
663
|
+
if (cursor.done || cursor.value.type !== "Curly" || cursor.value.raw !== "}") throw new ParseError(input, cursor.done ? value.loc.start : cursor.value.loc.start, `Expected "}"`);
|
|
664
|
+
value.loc.end = cursor.value.loc.end;
|
|
665
|
+
{
|
|
666
|
+
const gap = detectCommaSpace(value.items);
|
|
667
|
+
if (gap !== void 0) setCommaSpace(value, gap);
|
|
668
|
+
}
|
|
669
|
+
return [value, comments];
|
|
670
|
+
}
|
|
671
|
+
function inlineArray(cursor, input) {
|
|
672
|
+
if (cursor.value.raw !== "[") throw new ParseError(input, cursor.value.loc.start, `Expected "[" for inline array`);
|
|
673
|
+
const value = {
|
|
674
|
+
type: "InlineArray",
|
|
675
|
+
loc: cursor.value.loc,
|
|
676
|
+
items: []
|
|
677
|
+
};
|
|
678
|
+
const comments = [];
|
|
679
|
+
cursor.next();
|
|
680
|
+
while (!cursor.done && !(cursor.value.type === "Bracket" && cursor.value.raw === "]")) {
|
|
681
|
+
if (cursor.value.type === "Comma") {
|
|
682
|
+
const previous = value.items[value.items.length - 1];
|
|
683
|
+
if (!previous) throw new ParseError(input, cursor.value.loc.start, "Leading comma in array");
|
|
684
|
+
if (previous.comma) throw new ParseError(input, cursor.value.loc.start, "Consecutive commas in array");
|
|
685
|
+
previous.comma = true;
|
|
686
|
+
previous.loc.end = cursor.value.loc.start;
|
|
687
|
+
} else if (cursor.value.type === "Comment") comments.push(comment(cursor));
|
|
688
|
+
else {
|
|
689
|
+
const previous = value.items[value.items.length - 1];
|
|
690
|
+
if (previous && !previous.comma) throw new ParseError(input, cursor.value.loc.start, "Missing comma between array elements");
|
|
691
|
+
const results = walkValue(cursor, input);
|
|
692
|
+
const item = results[0];
|
|
693
|
+
value.items.push({
|
|
694
|
+
type: "InlineItem",
|
|
695
|
+
loc: cloneLocation(item.loc),
|
|
696
|
+
item,
|
|
697
|
+
comma: false
|
|
698
|
+
});
|
|
699
|
+
for (let ci = 1; ci < results.length; ci++) comments.push(results[ci]);
|
|
700
|
+
}
|
|
701
|
+
cursor.next();
|
|
702
|
+
}
|
|
703
|
+
if (cursor.done || cursor.value.type !== "Bracket" || cursor.value.raw !== "]") throw new ParseError(input, cursor.done ? value.loc.start : cursor.value.loc.start, `Expected "]"`);
|
|
704
|
+
value.loc.end = cursor.value.loc.end;
|
|
705
|
+
{
|
|
706
|
+
const gap = detectCommaSpace(value.items);
|
|
707
|
+
if (gap !== void 0) setCommaSpace(value, gap);
|
|
708
|
+
}
|
|
709
|
+
return [value, comments];
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
//#endregion
|
|
713
|
+
export { LocalDate, LocalDateTime, LocalTime, OffsetDateTime, continueParsingTOML, parseTOML as default };
|
|
714
|
+
//# sourceMappingURL=parse-toml.js.map
|