libyay 1.0.0 → 1.1.1
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 -1
- package/package.json +1 -1
- package/yay.js +47 -25
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# libyay
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/libyay)
|
|
4
|
+
|
|
5
|
+
A parser for the [YAY](https://kriskowal.com/yay) data format, implemented in JavaScript (ES2024+).
|
|
4
6
|
|
|
5
7
|
## Requirements
|
|
6
8
|
|
|
@@ -401,6 +403,31 @@ next: 1
|
|
|
401
403
|
})
|
|
402
404
|
```
|
|
403
405
|
|
|
406
|
+
In canonical YAY, a block string is followed by one blank line, unless it is
|
|
407
|
+
the last thing in the document.
|
|
408
|
+
The blank line is not part of the value, since trailing empty lines never are,
|
|
409
|
+
so a document reads the same with or without it.
|
|
410
|
+
A string whose last line is empty, like `"a\n\n"`, cannot be a block string
|
|
411
|
+
and is written with double quotes.
|
|
412
|
+
|
|
413
|
+
[string-block-separated-property.yay](https://github.com/kriskowal/yay/blob/main/test/yay/string-block-separated-property.yay)
|
|
414
|
+
```yay
|
|
415
|
+
message: `
|
|
416
|
+
We shall fight on the beaches.
|
|
417
|
+
|
|
418
|
+
We shall never surrender.
|
|
419
|
+
|
|
420
|
+
next: 1
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
[string-block-separated-property.js](https://github.com/kriskowal/yay/blob/main/test/js/string-block-separated-property.js)
|
|
424
|
+
```js
|
|
425
|
+
({
|
|
426
|
+
"message": "We shall fight on the beaches.\n\nWe shall never surrender.\n",
|
|
427
|
+
"next": 1n,
|
|
428
|
+
})
|
|
429
|
+
```
|
|
430
|
+
|
|
404
431
|
## Inline Strings
|
|
405
432
|
|
|
406
433
|
Strings may be quoted with double or single quotes.
|
package/package.json
CHANGED
package/yay.js
CHANGED
|
@@ -155,6 +155,10 @@ function scan(source, ctx = {}) {
|
|
|
155
155
|
}
|
|
156
156
|
const lines = [];
|
|
157
157
|
const lineStrings = source.split(/\n/);
|
|
158
|
+
// While inside a block string, the column that ends it: the indent of a
|
|
159
|
+
// bare opener, or the key's column for a property opener. Its content
|
|
160
|
+
// lines are literal: no list leaders, no "-" or "*" checks, no comments.
|
|
161
|
+
let blockOpener = -1;
|
|
158
162
|
for (let i = 0; i < lineStrings.length; i++) {
|
|
159
163
|
const lineStr = lineStrings[i];
|
|
160
164
|
if (lineStr.length > 0 && lineStr.charCodeAt(lineStr.length - 1) === 0x20) {
|
|
@@ -167,6 +171,13 @@ function scan(source, ctx = {}) {
|
|
|
167
171
|
indent++;
|
|
168
172
|
}
|
|
169
173
|
const rest = lineStr.slice(indent);
|
|
174
|
+
if (blockOpener >= 0) {
|
|
175
|
+
if (lineStr.length === 0 || indent > blockOpener) {
|
|
176
|
+
lines.push({ line: rest, indent, leader: "", lineNum: i });
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
blockOpener = -1;
|
|
180
|
+
}
|
|
170
181
|
if (rest.startsWith("#") && indent === 0) {
|
|
171
182
|
continue; // comment only at column 0
|
|
172
183
|
}
|
|
@@ -204,11 +215,25 @@ function scan(source, ctx = {}) {
|
|
|
204
215
|
) {
|
|
205
216
|
throw new Error('Unexpected character "*"' + locSuffix(ctx, i, indent));
|
|
206
217
|
}
|
|
218
|
+
if (isBareBlockOpener(line)) {
|
|
219
|
+
blockOpener = indent;
|
|
220
|
+
} else if (line.endsWith(": `")) {
|
|
221
|
+
blockOpener = indent + (rest.length - line.length);
|
|
222
|
+
}
|
|
207
223
|
lines.push({ line, indent, leader, lineNum: i });
|
|
208
224
|
}
|
|
209
225
|
return lines;
|
|
210
226
|
}
|
|
211
227
|
|
|
228
|
+
/**
|
|
229
|
+
* Whether a line's content (after any list leader) is a bare "`" or
|
|
230
|
+
* "` text" block string opener.
|
|
231
|
+
* @param {string} line
|
|
232
|
+
*/
|
|
233
|
+
function isBareBlockOpener(line) {
|
|
234
|
+
return line === "`" || line.startsWith("` ");
|
|
235
|
+
}
|
|
236
|
+
|
|
212
237
|
// --- Outline Lexer: lines -> tokens { type, text, indent?, lineNum?, col? } ---
|
|
213
238
|
|
|
214
239
|
/**
|
|
@@ -219,10 +244,10 @@ function outlineLex(lines) {
|
|
|
219
244
|
const tokens = [];
|
|
220
245
|
let stack = [0];
|
|
221
246
|
let top = 0;
|
|
222
|
-
let broken = false;
|
|
223
247
|
for (const { line, indent, leader, lineNum } of lines) {
|
|
224
|
-
// Close blocks on dedent.
|
|
225
|
-
|
|
248
|
+
// Close blocks on dedent. A blank line has no indent of its own, so it
|
|
249
|
+
// closes nothing.
|
|
250
|
+
while (line.length > 0 && indent < top) {
|
|
226
251
|
tokens.push({ type: "stop", text: "" });
|
|
227
252
|
stack.pop();
|
|
228
253
|
top = stack[stack.length - 1];
|
|
@@ -237,7 +262,6 @@ function outlineLex(lines) {
|
|
|
237
262
|
});
|
|
238
263
|
stack.push(indent);
|
|
239
264
|
top = indent;
|
|
240
|
-
broken = false;
|
|
241
265
|
} else if (leader.length > 0 && indent === top) {
|
|
242
266
|
tokens.push({ type: "stop", text: "" });
|
|
243
267
|
tokens.push({
|
|
@@ -247,14 +271,13 @@ function outlineLex(lines) {
|
|
|
247
271
|
lineNum,
|
|
248
272
|
col: indent,
|
|
249
273
|
});
|
|
250
|
-
broken = false;
|
|
251
274
|
}
|
|
275
|
+
// Every blank line is a break, so that a block string keeps a run of
|
|
276
|
+
// blank lines.
|
|
252
277
|
if (line.length > 0) {
|
|
253
278
|
tokens.push({ type: "text", text: line, indent, lineNum, col: indent });
|
|
254
|
-
|
|
255
|
-
} else if (!broken) {
|
|
279
|
+
} else {
|
|
256
280
|
tokens.push({ type: "break", text: "", lineNum, col: indent });
|
|
257
|
-
broken = true;
|
|
258
281
|
}
|
|
259
282
|
}
|
|
260
283
|
while (stack.length > 1) {
|
|
@@ -1730,8 +1753,8 @@ function parseObjectOrNamedArray(tokens, i, key, ctx = {}) {
|
|
|
1730
1753
|
}
|
|
1731
1754
|
}
|
|
1732
1755
|
if (first && first.type === "start" && first.text === "-") {
|
|
1733
|
-
const [arr] = parseListArray(tokens, i);
|
|
1734
|
-
return [{ [key]: arr },
|
|
1756
|
+
const [arr, next] = parseListArray(tokens, i);
|
|
1757
|
+
return [{ [key]: arr }, next];
|
|
1735
1758
|
}
|
|
1736
1759
|
// Note: '*' syntax for multiline bytes is rejected by the scanner
|
|
1737
1760
|
// Note: "key: <" without closing ">" is invalid - inline byte arrays must be closed on the same line
|
|
@@ -1751,14 +1774,19 @@ function parseObjectOrNamedArray(tokens, i, key, ctx = {}) {
|
|
|
1751
1774
|
"Unexpected indent" + locSuffix(ctx, first.lineNum ?? 0, 0),
|
|
1752
1775
|
);
|
|
1753
1776
|
}
|
|
1754
|
-
// Concatenated quoted strings (multiple quoted strings on consecutive lines)
|
|
1777
|
+
// Concatenated quoted strings (multiple quoted strings on consecutive lines).
|
|
1778
|
+
// A quoted line with a colon outside its quotes is a property with a quoted
|
|
1779
|
+
// key, which opens a nested object.
|
|
1755
1780
|
if (first && first.type === "text") {
|
|
1756
1781
|
const trimmed = first.text.trim();
|
|
1757
1782
|
if (
|
|
1758
|
-
(trimmed.startsWith('"') &&
|
|
1783
|
+
((trimmed.startsWith('"') &&
|
|
1759
1784
|
trimmed.endsWith('"') &&
|
|
1760
1785
|
trimmed.length >= 2) ||
|
|
1761
|
-
|
|
1786
|
+
(trimmed.startsWith("'") &&
|
|
1787
|
+
trimmed.endsWith("'") &&
|
|
1788
|
+
trimmed.length >= 2)) &&
|
|
1789
|
+
!splitKeyValue(first.text, first.col ?? 0, ctx, first.lineNum ?? 0)
|
|
1762
1790
|
) {
|
|
1763
1791
|
const result = parseConcatenatedStrings(tokens, i, baseIndent, ctx);
|
|
1764
1792
|
if (result !== null) {
|
|
@@ -1884,17 +1912,6 @@ function parseObjectOrNamedArray(tokens, i, key, ctx = {}) {
|
|
|
1884
1912
|
return [{ [key]: Object.keys(obj).length ? obj : undefined }, i];
|
|
1885
1913
|
}
|
|
1886
1914
|
|
|
1887
|
-
function skipToNextKey(tokens, i, baseIndent) {
|
|
1888
|
-
while (
|
|
1889
|
-
i < tokens.length &&
|
|
1890
|
-
tokens[i].type !== "stop" &&
|
|
1891
|
-
(tokens[i].indent ?? 0) > baseIndent
|
|
1892
|
-
)
|
|
1893
|
-
i++;
|
|
1894
|
-
while (i < tokens.length && tokens[i].type === "stop") i++;
|
|
1895
|
-
return i;
|
|
1896
|
-
}
|
|
1897
|
-
|
|
1898
1915
|
/**
|
|
1899
1916
|
* @param {Token[]} tokens
|
|
1900
1917
|
* @param {number} i
|
|
@@ -1916,6 +1933,9 @@ function parseObjectBlock(tokens, i, baseIndent, ctx = {}, inlineIndent) {
|
|
|
1916
1933
|
i++;
|
|
1917
1934
|
continue;
|
|
1918
1935
|
}
|
|
1936
|
+
// A list leader outdented past this object begins the next item of an
|
|
1937
|
+
// enclosing list.
|
|
1938
|
+
if (t.type === "start" && (t.indent ?? 0) < baseIndent) break;
|
|
1919
1939
|
if (t.type !== "text") {
|
|
1920
1940
|
i++;
|
|
1921
1941
|
continue;
|
|
@@ -1954,12 +1974,14 @@ function parseObjectBlock(tokens, i, baseIndent, ctx = {}, inlineIndent) {
|
|
|
1954
1974
|
}
|
|
1955
1975
|
// Block string in property context: backtick alone on line
|
|
1956
1976
|
if (vPart.trim() === "`") {
|
|
1977
|
+
// The block ends at the key's column, which for "- key: `" is past
|
|
1978
|
+
// the list leader.
|
|
1957
1979
|
const [body, next] = parseBlockStringWithIndent(
|
|
1958
1980
|
tokens,
|
|
1959
1981
|
i,
|
|
1960
1982
|
"",
|
|
1961
1983
|
true,
|
|
1962
|
-
|
|
1984
|
+
indent,
|
|
1963
1985
|
);
|
|
1964
1986
|
obj[k] = body;
|
|
1965
1987
|
i = next;
|