@aljazmc/syntax 0.6.2 → 0.7.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 +4 -1
- package/bin/index.js +916 -0
- package/bin/types/index.d.ts +9 -0
- package/bin/types/yaml/ast.d.ts +140 -0
- package/bin/types/yaml/parser.d.ts +11 -0
- package/package.json +16 -2
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# > syntax
|
|
2
2
|
|
|
3
3
|
Shared, dependency-free scanner and recursive descent parser for CSS,
|
|
4
|
-
HTML, JavaScript, JSON, JSX, Markdown and
|
|
4
|
+
HTML, JavaScript, JSON, JSX, Markdown, TypeScript and YAML, bundled into
|
|
5
5
|
`@aljazmc/formatero` and `@aljazmc/lintero` and published for their
|
|
6
6
|
types. Invalid sources throw a `ParseError` with the position of the
|
|
7
7
|
problem.
|
|
@@ -15,6 +15,7 @@ problem.
|
|
|
15
15
|
| JavaScript, JSX, TypeScript | `language`, `parse` | ESTree, with Babel-style literals and TypeScript nodes. |
|
|
16
16
|
| JSON | `language`, `parse` | ESTree expressions. |
|
|
17
17
|
| Markdown | `isMarkdown`, `parseMarkdown` | CommonMark blocks and inlines, with GitHub's tables, task lists and strikethrough. |
|
|
18
|
+
| YAML | `isYaml`, `parseYaml` | Documents of block and flow collections and scalars, with comments in place. |
|
|
18
19
|
|
|
19
20
|
| Helper | Description |
|
|
20
21
|
| -------------------------------- | ----------------------------------------------------------- |
|
|
@@ -33,12 +34,14 @@ import {
|
|
|
33
34
|
parseCss,
|
|
34
35
|
parseHtml,
|
|
35
36
|
parseMarkdown,
|
|
37
|
+
parseYaml,
|
|
36
38
|
} from "@aljazmc/syntax";
|
|
37
39
|
|
|
38
40
|
const { program, comments } = parse("let a = <b />;", language("a.tsx"));
|
|
39
41
|
const sheet = parseCss("a > .b:hover { color: red; }\n");
|
|
40
42
|
const page = parseHtml('<p class="a">Hi <b>there</b></p>\n');
|
|
41
43
|
const doc = parseMarkdown("# Title\n\n- [x] done\n");
|
|
44
|
+
const stream = parseYaml("a:\n - b: 1 # c\n");
|
|
42
45
|
```
|
|
43
46
|
|
|
44
47
|
## > License
|
package/bin/index.js
CHANGED
|
@@ -6475,6 +6475,911 @@ var Parser3 = class extends ExpressionParser {
|
|
|
6475
6475
|
}
|
|
6476
6476
|
};
|
|
6477
6477
|
|
|
6478
|
+
// shared/syntax/src/yaml/parser.ts
|
|
6479
|
+
var flowIndicators = ",[]{}";
|
|
6480
|
+
var YamlParser = class {
|
|
6481
|
+
static {
|
|
6482
|
+
__name(this, "YamlParser");
|
|
6483
|
+
}
|
|
6484
|
+
position = 0;
|
|
6485
|
+
#source;
|
|
6486
|
+
constructor(source) {
|
|
6487
|
+
this.#source = source;
|
|
6488
|
+
}
|
|
6489
|
+
parse() {
|
|
6490
|
+
const documents = [];
|
|
6491
|
+
for (; ; ) {
|
|
6492
|
+
const previous = documents.at(-1);
|
|
6493
|
+
const document = this.#document(
|
|
6494
|
+
previous === void 0 || previous.explicitEnd
|
|
6495
|
+
);
|
|
6496
|
+
if (!document) break;
|
|
6497
|
+
documents.push(document);
|
|
6498
|
+
}
|
|
6499
|
+
return { type: "Stream", documents };
|
|
6500
|
+
}
|
|
6501
|
+
#document(bare) {
|
|
6502
|
+
const source = this.#source;
|
|
6503
|
+
const start = this.position;
|
|
6504
|
+
const prelude = [];
|
|
6505
|
+
let blank = false;
|
|
6506
|
+
for (; ; ) {
|
|
6507
|
+
const gap2 = this.#gap();
|
|
6508
|
+
for (const line of gap2.lines) {
|
|
6509
|
+
if (!line.comment) blank = prelude.length > 0;
|
|
6510
|
+
else {
|
|
6511
|
+
prelude.push({ text: line.comment.value, blank });
|
|
6512
|
+
blank = false;
|
|
6513
|
+
}
|
|
6514
|
+
}
|
|
6515
|
+
this.position = gap2.start;
|
|
6516
|
+
if (gap2.column !== 0 || source[gap2.start] !== "%") break;
|
|
6517
|
+
const end = this.#lineEnd(gap2.start);
|
|
6518
|
+
prelude.push({
|
|
6519
|
+
text: source.slice(gap2.start, end).trimEnd(),
|
|
6520
|
+
blank
|
|
6521
|
+
});
|
|
6522
|
+
blank = false;
|
|
6523
|
+
this.position = end;
|
|
6524
|
+
this.#nextLine();
|
|
6525
|
+
}
|
|
6526
|
+
const document = {
|
|
6527
|
+
type: "Document",
|
|
6528
|
+
start,
|
|
6529
|
+
end: start,
|
|
6530
|
+
prelude,
|
|
6531
|
+
explicitStart: false,
|
|
6532
|
+
startBlank: false,
|
|
6533
|
+
leading: [],
|
|
6534
|
+
blank: false,
|
|
6535
|
+
root: null,
|
|
6536
|
+
trailing: [],
|
|
6537
|
+
explicitEnd: false
|
|
6538
|
+
};
|
|
6539
|
+
if (this.position >= source.length) {
|
|
6540
|
+
if (prelude.length === 0) return void 0;
|
|
6541
|
+
document.end = this.position;
|
|
6542
|
+
return document;
|
|
6543
|
+
}
|
|
6544
|
+
if (this.#marker(this.position, "---")) {
|
|
6545
|
+
document.explicitStart = true;
|
|
6546
|
+
document.startBlank = blank;
|
|
6547
|
+
this.position += 3;
|
|
6548
|
+
this.#spaces();
|
|
6549
|
+
if (source[this.position] === "#") {
|
|
6550
|
+
const comment = this.#rest();
|
|
6551
|
+
if (comment) document.startComment = comment;
|
|
6552
|
+
} else if (this.#lineEnded()) this.#nextLine();
|
|
6553
|
+
else {
|
|
6554
|
+
document.root = this.#node(-1, "block");
|
|
6555
|
+
this.#finish(document.root);
|
|
6556
|
+
}
|
|
6557
|
+
} else if (!bare && !this.#marker(this.position, "..."))
|
|
6558
|
+
throw new ParseError(
|
|
6559
|
+
"Expected `---` before another document.",
|
|
6560
|
+
this.position
|
|
6561
|
+
);
|
|
6562
|
+
if (!document.root) {
|
|
6563
|
+
const value = this.#blockValue(-1, false);
|
|
6564
|
+
document.root = value.node;
|
|
6565
|
+
document.leading = value.leading;
|
|
6566
|
+
document.blank = value.leading.length > 0 || document.explicitStart ? value.blank : blank;
|
|
6567
|
+
}
|
|
6568
|
+
const gap = this.#gap();
|
|
6569
|
+
let trailingBlank = false;
|
|
6570
|
+
for (const line of gap.lines)
|
|
6571
|
+
if (!line.comment) trailingBlank = true;
|
|
6572
|
+
else {
|
|
6573
|
+
document.trailing.push({
|
|
6574
|
+
comment: line.comment,
|
|
6575
|
+
blank: trailingBlank
|
|
6576
|
+
});
|
|
6577
|
+
trailingBlank = false;
|
|
6578
|
+
}
|
|
6579
|
+
this.position = gap.start;
|
|
6580
|
+
if (gap.column >= 0)
|
|
6581
|
+
throw new ParseError(
|
|
6582
|
+
"Unexpected content, it is not part of the document.",
|
|
6583
|
+
gap.start + gap.column
|
|
6584
|
+
);
|
|
6585
|
+
if (this.#marker(this.position, "...")) {
|
|
6586
|
+
document.explicitEnd = true;
|
|
6587
|
+
this.position += 3;
|
|
6588
|
+
const comment = this.#rest();
|
|
6589
|
+
if (comment) document.endComment = comment;
|
|
6590
|
+
}
|
|
6591
|
+
document.end = this.position;
|
|
6592
|
+
return document;
|
|
6593
|
+
}
|
|
6594
|
+
/**
|
|
6595
|
+
* The node on the lines after an indicator, indented deeper than `n`,
|
|
6596
|
+
* or a sequence at `n` after a mapping key; null when there is none.
|
|
6597
|
+
*/
|
|
6598
|
+
#blockValue(n, sequence) {
|
|
6599
|
+
const gap = this.#gap();
|
|
6600
|
+
const content = gap.start + gap.column;
|
|
6601
|
+
if (!(gap.column > n || sequence && n >= 0 && gap.column === n && this.#indicator(content, "-")))
|
|
6602
|
+
return { node: null, leading: [], blank: false };
|
|
6603
|
+
const leading = [];
|
|
6604
|
+
let blank = false;
|
|
6605
|
+
for (const line of gap.lines)
|
|
6606
|
+
if (!line.comment) blank = true;
|
|
6607
|
+
else {
|
|
6608
|
+
leading.push({ comment: line.comment, blank });
|
|
6609
|
+
blank = false;
|
|
6610
|
+
}
|
|
6611
|
+
this.position = content;
|
|
6612
|
+
const node = this.#node(n, "block");
|
|
6613
|
+
this.#finish(node);
|
|
6614
|
+
return { node, leading, blank };
|
|
6615
|
+
}
|
|
6616
|
+
/**
|
|
6617
|
+
* A node at the position, belonging to a node indented by `n`. Single
|
|
6618
|
+
* line nodes leave the position after them, others at the start of the
|
|
6619
|
+
* line after them.
|
|
6620
|
+
*/
|
|
6621
|
+
#node(n, context) {
|
|
6622
|
+
const source = this.#source;
|
|
6623
|
+
const start = this.position;
|
|
6624
|
+
const column = this.#column(start);
|
|
6625
|
+
const properties = this.#properties(false);
|
|
6626
|
+
if (properties !== void 0 && (this.#lineEnded() || source[this.position] === "#")) {
|
|
6627
|
+
const comment = this.#rest();
|
|
6628
|
+
const value = this.#blockValue(n, context === "value");
|
|
6629
|
+
const node2 = value.node ?? {
|
|
6630
|
+
type: "Scalar",
|
|
6631
|
+
style: "plain",
|
|
6632
|
+
lines: [],
|
|
6633
|
+
start,
|
|
6634
|
+
end: start + properties.length
|
|
6635
|
+
};
|
|
6636
|
+
node2.properties = properties;
|
|
6637
|
+
if (comment) node2.propertiesComment = comment;
|
|
6638
|
+
if (value.leading.length > 0) node2.leading = value.leading;
|
|
6639
|
+
node2.start = start;
|
|
6640
|
+
return node2;
|
|
6641
|
+
}
|
|
6642
|
+
const char = source[this.position];
|
|
6643
|
+
if (char === "-" && this.#separated(this.position + 1)) {
|
|
6644
|
+
if (context === "value")
|
|
6645
|
+
throw new ParseError(
|
|
6646
|
+
"A sequence cannot start on the line of a mapping key.",
|
|
6647
|
+
this.position
|
|
6648
|
+
);
|
|
6649
|
+
return this.#sequence(
|
|
6650
|
+
this.#column(this.position),
|
|
6651
|
+
properties,
|
|
6652
|
+
start
|
|
6653
|
+
);
|
|
6654
|
+
}
|
|
6655
|
+
if (char === "?" && this.#separated(this.position + 1)) {
|
|
6656
|
+
if (context === "value")
|
|
6657
|
+
throw new ParseError(
|
|
6658
|
+
"A mapping cannot start on the line of a mapping key.",
|
|
6659
|
+
this.position
|
|
6660
|
+
);
|
|
6661
|
+
const mapping = this.#mapping(column, void 0, start);
|
|
6662
|
+
if (properties) mapping.properties = properties;
|
|
6663
|
+
return mapping;
|
|
6664
|
+
}
|
|
6665
|
+
if (char === "|" || char === ">") {
|
|
6666
|
+
const scalar = this.#blockScalar(n);
|
|
6667
|
+
if (properties) {
|
|
6668
|
+
scalar.properties = properties;
|
|
6669
|
+
scalar.start = start;
|
|
6670
|
+
}
|
|
6671
|
+
return scalar;
|
|
6672
|
+
}
|
|
6673
|
+
if (properties !== void 0 && char === "*")
|
|
6674
|
+
throw new ParseError(
|
|
6675
|
+
"An alias cannot have a tag or an anchor.",
|
|
6676
|
+
this.position
|
|
6677
|
+
);
|
|
6678
|
+
const node = this.#inline(n);
|
|
6679
|
+
if (properties) {
|
|
6680
|
+
node.properties = properties;
|
|
6681
|
+
node.start = start;
|
|
6682
|
+
}
|
|
6683
|
+
const end = this.position;
|
|
6684
|
+
this.#spaces();
|
|
6685
|
+
if (source[this.position] === ":" && this.#separated(this.position + 1)) {
|
|
6686
|
+
if (context === "value")
|
|
6687
|
+
throw new ParseError(
|
|
6688
|
+
"A mapping cannot start on the line of a mapping key.",
|
|
6689
|
+
this.position
|
|
6690
|
+
);
|
|
6691
|
+
if (source.slice(start, end).includes("\n"))
|
|
6692
|
+
throw new ParseError(
|
|
6693
|
+
"A mapping key must be on a single line.",
|
|
6694
|
+
start
|
|
6695
|
+
);
|
|
6696
|
+
return this.#mapping(column, node, start);
|
|
6697
|
+
}
|
|
6698
|
+
this.position = end;
|
|
6699
|
+
return node;
|
|
6700
|
+
}
|
|
6701
|
+
/** A scalar, alias or flow collection on a line, maybe a mapping key. */
|
|
6702
|
+
#inline(n) {
|
|
6703
|
+
const char = this.#source[this.position];
|
|
6704
|
+
if (char === "[" || char === "{") return this.#flow();
|
|
6705
|
+
if (char === '"' || char === "'") return this.#quoted();
|
|
6706
|
+
if (char === "*") return this.#alias(false);
|
|
6707
|
+
if (char !== void 0 && ",]}%@`".includes(char))
|
|
6708
|
+
throw new ParseError(
|
|
6709
|
+
`A plain scalar cannot start with \`${char}\`.`,
|
|
6710
|
+
this.position
|
|
6711
|
+
);
|
|
6712
|
+
return this.#plain(n);
|
|
6713
|
+
}
|
|
6714
|
+
#mapping(column, first, start) {
|
|
6715
|
+
const mapping = {
|
|
6716
|
+
type: "Mapping",
|
|
6717
|
+
entries: [],
|
|
6718
|
+
endComments: [],
|
|
6719
|
+
start,
|
|
6720
|
+
end: start
|
|
6721
|
+
};
|
|
6722
|
+
let key = first;
|
|
6723
|
+
let leading = [];
|
|
6724
|
+
let blank = false;
|
|
6725
|
+
for (; ; ) {
|
|
6726
|
+
const entry = this.#entry(column, key);
|
|
6727
|
+
entry.leading = leading;
|
|
6728
|
+
entry.blank = blank;
|
|
6729
|
+
mapping.entries.push(entry);
|
|
6730
|
+
key = void 0;
|
|
6731
|
+
const after = this.#after(
|
|
6732
|
+
column,
|
|
6733
|
+
entry,
|
|
6734
|
+
(content) => !this.#indicator(content, "-")
|
|
6735
|
+
);
|
|
6736
|
+
if (!after.more) {
|
|
6737
|
+
mapping.endComments = after.comments;
|
|
6738
|
+
break;
|
|
6739
|
+
}
|
|
6740
|
+
leading = after.comments;
|
|
6741
|
+
blank = after.blank;
|
|
6742
|
+
}
|
|
6743
|
+
mapping.end = this.position;
|
|
6744
|
+
return mapping;
|
|
6745
|
+
}
|
|
6746
|
+
/** An entry of a block mapping, after its key when it is known. */
|
|
6747
|
+
#entry(column, first) {
|
|
6748
|
+
const source = this.#source;
|
|
6749
|
+
const entry = {
|
|
6750
|
+
key: null,
|
|
6751
|
+
explicit: false,
|
|
6752
|
+
keyBetween: [],
|
|
6753
|
+
value: null,
|
|
6754
|
+
leading: [],
|
|
6755
|
+
blank: false,
|
|
6756
|
+
between: [],
|
|
6757
|
+
betweenBlank: false,
|
|
6758
|
+
inner: []
|
|
6759
|
+
};
|
|
6760
|
+
if (!first && this.#indicator(this.position, "?")) {
|
|
6761
|
+
entry.explicit = true;
|
|
6762
|
+
this.position++;
|
|
6763
|
+
this.#spaces();
|
|
6764
|
+
if (this.#lineEnded() || source[this.position] === "#") {
|
|
6765
|
+
const comment = this.#rest();
|
|
6766
|
+
if (comment) entry.keyComment = comment;
|
|
6767
|
+
const value = this.#blockValue(column, true);
|
|
6768
|
+
entry.key = value.node;
|
|
6769
|
+
entry.keyBetween = value.leading;
|
|
6770
|
+
} else {
|
|
6771
|
+
entry.key = this.#node(column, "block");
|
|
6772
|
+
this.#finish(entry.key);
|
|
6773
|
+
}
|
|
6774
|
+
const gap = this.#gap();
|
|
6775
|
+
if (gap.column === column && this.#indicator(gap.start + column, ":")) {
|
|
6776
|
+
for (const line of gap.lines)
|
|
6777
|
+
if (line.comment)
|
|
6778
|
+
entry.keyBetween.push({
|
|
6779
|
+
comment: line.comment,
|
|
6780
|
+
blank: false
|
|
6781
|
+
});
|
|
6782
|
+
this.position = gap.start + column + 1;
|
|
6783
|
+
this.#value(entry, column, "block");
|
|
6784
|
+
}
|
|
6785
|
+
return entry;
|
|
6786
|
+
}
|
|
6787
|
+
let key = first;
|
|
6788
|
+
if (!key) {
|
|
6789
|
+
const start = this.position;
|
|
6790
|
+
const properties = this.#properties(false);
|
|
6791
|
+
if (properties !== void 0 && source[this.position] === "*")
|
|
6792
|
+
throw new ParseError(
|
|
6793
|
+
"An alias cannot have a tag or an anchor.",
|
|
6794
|
+
this.position
|
|
6795
|
+
);
|
|
6796
|
+
key = this.#inline(column);
|
|
6797
|
+
if (properties) {
|
|
6798
|
+
key.properties = properties;
|
|
6799
|
+
key.start = start;
|
|
6800
|
+
}
|
|
6801
|
+
if (source.slice(start, this.position).includes("\n"))
|
|
6802
|
+
throw new ParseError(
|
|
6803
|
+
"A mapping key must be on a single line.",
|
|
6804
|
+
start
|
|
6805
|
+
);
|
|
6806
|
+
this.#spaces();
|
|
6807
|
+
if (!(source[this.position] === ":" && this.#separated(this.position + 1)))
|
|
6808
|
+
throw new ParseError(
|
|
6809
|
+
"Expected a mapping entry, a key and `:`.",
|
|
6810
|
+
start
|
|
6811
|
+
);
|
|
6812
|
+
}
|
|
6813
|
+
entry.key = key;
|
|
6814
|
+
this.position++;
|
|
6815
|
+
this.#value(entry, column, "value");
|
|
6816
|
+
return entry;
|
|
6817
|
+
}
|
|
6818
|
+
/** The value of a mapping entry, after its `:`. */
|
|
6819
|
+
#value(entry, column, context) {
|
|
6820
|
+
this.#spaces();
|
|
6821
|
+
if (this.#lineEnded() || this.#source[this.position] === "#") {
|
|
6822
|
+
const comment = this.#rest();
|
|
6823
|
+
if (comment) entry.comment = comment;
|
|
6824
|
+
const value = this.#blockValue(column, true);
|
|
6825
|
+
entry.value = value.node;
|
|
6826
|
+
entry.between = value.leading;
|
|
6827
|
+
entry.betweenBlank = value.blank;
|
|
6828
|
+
return;
|
|
6829
|
+
}
|
|
6830
|
+
entry.value = this.#node(column, context);
|
|
6831
|
+
this.#finish(entry.value);
|
|
6832
|
+
}
|
|
6833
|
+
#sequence(column, properties, start) {
|
|
6834
|
+
const source = this.#source;
|
|
6835
|
+
const sequence = {
|
|
6836
|
+
type: "Sequence",
|
|
6837
|
+
items: [],
|
|
6838
|
+
endComments: [],
|
|
6839
|
+
start,
|
|
6840
|
+
end: start
|
|
6841
|
+
};
|
|
6842
|
+
if (properties) sequence.properties = properties;
|
|
6843
|
+
let leading = [];
|
|
6844
|
+
let blank = false;
|
|
6845
|
+
for (; ; ) {
|
|
6846
|
+
const item = {
|
|
6847
|
+
value: null,
|
|
6848
|
+
leading,
|
|
6849
|
+
blank,
|
|
6850
|
+
between: [],
|
|
6851
|
+
betweenBlank: false,
|
|
6852
|
+
inner: []
|
|
6853
|
+
};
|
|
6854
|
+
this.position++;
|
|
6855
|
+
this.#spaces();
|
|
6856
|
+
if (this.#lineEnded() || source[this.position] === "#") {
|
|
6857
|
+
const comment = this.#rest();
|
|
6858
|
+
if (comment) item.comment = comment;
|
|
6859
|
+
const value = this.#blockValue(column, false);
|
|
6860
|
+
item.value = value.node;
|
|
6861
|
+
item.between = value.leading;
|
|
6862
|
+
item.betweenBlank = value.blank;
|
|
6863
|
+
} else {
|
|
6864
|
+
item.value = this.#node(column, "block");
|
|
6865
|
+
this.#finish(item.value);
|
|
6866
|
+
}
|
|
6867
|
+
sequence.items.push(item);
|
|
6868
|
+
const after = this.#after(
|
|
6869
|
+
column,
|
|
6870
|
+
item,
|
|
6871
|
+
(content) => this.#indicator(content, "-")
|
|
6872
|
+
);
|
|
6873
|
+
if (!after.more) {
|
|
6874
|
+
sequence.endComments = after.comments;
|
|
6875
|
+
break;
|
|
6876
|
+
}
|
|
6877
|
+
leading = after.comments;
|
|
6878
|
+
blank = after.blank;
|
|
6879
|
+
}
|
|
6880
|
+
sequence.end = this.position;
|
|
6881
|
+
return sequence;
|
|
6882
|
+
}
|
|
6883
|
+
/**
|
|
6884
|
+
* Takes the blank and comment lines after an entry of a collection at a
|
|
6885
|
+
* column. Comments deeper than the entry go into it, unless its value is
|
|
6886
|
+
* a block scalar they could be taken for, other comments at least as deep as the collection
|
|
6887
|
+
* stay in it, before its next entry or at its end, as do ones before its
|
|
6888
|
+
* next entry. Once a comment is at the collection's level, the ones
|
|
6889
|
+
* after it are too. The rest is left to the collections around it.
|
|
6890
|
+
*/
|
|
6891
|
+
#after(column, entry, continues) {
|
|
6892
|
+
const gap = this.#gap();
|
|
6893
|
+
const next = gap.column;
|
|
6894
|
+
if (next > column)
|
|
6895
|
+
throw new ParseError(
|
|
6896
|
+
"Unexpected indentation.",
|
|
6897
|
+
gap.start + gap.column
|
|
6898
|
+
);
|
|
6899
|
+
const more = next === column && continues(gap.start + column);
|
|
6900
|
+
const scalar = entry.value?.type === "BlockScalar";
|
|
6901
|
+
const comments = [];
|
|
6902
|
+
let level = false;
|
|
6903
|
+
let blank = false;
|
|
6904
|
+
let position = this.position;
|
|
6905
|
+
for (const line of gap.lines) {
|
|
6906
|
+
if (!line.comment) {
|
|
6907
|
+
blank = true;
|
|
6908
|
+
continue;
|
|
6909
|
+
}
|
|
6910
|
+
const item = { comment: line.comment, blank };
|
|
6911
|
+
if (line.column > next) {
|
|
6912
|
+
if (line.column < column) break;
|
|
6913
|
+
if (line.column > column && !level && !scalar)
|
|
6914
|
+
entry.inner.push(item);
|
|
6915
|
+
else {
|
|
6916
|
+
level = true;
|
|
6917
|
+
comments.push(item);
|
|
6918
|
+
}
|
|
6919
|
+
} else if (more) {
|
|
6920
|
+
level = true;
|
|
6921
|
+
comments.push(item);
|
|
6922
|
+
} else break;
|
|
6923
|
+
blank = false;
|
|
6924
|
+
position = line.next;
|
|
6925
|
+
}
|
|
6926
|
+
if (more) {
|
|
6927
|
+
this.position = gap.start + column;
|
|
6928
|
+
return { more, comments, blank };
|
|
6929
|
+
}
|
|
6930
|
+
this.position = position;
|
|
6931
|
+
return { more, comments, blank: false };
|
|
6932
|
+
}
|
|
6933
|
+
/** A plain scalar in a block, which continues on lines indented deeper than `n`. */
|
|
6934
|
+
#plain(n) {
|
|
6935
|
+
const source = this.#source;
|
|
6936
|
+
const start = this.position;
|
|
6937
|
+
const char = source[start];
|
|
6938
|
+
if (char !== void 0 && "-?:".includes(char) && this.#separated(start + 1))
|
|
6939
|
+
throw new ParseError(`Unexpected \`${char}\`.`, start);
|
|
6940
|
+
const first = this.#plainLine(start);
|
|
6941
|
+
const lines = [source.slice(start, first.end)];
|
|
6942
|
+
let end = first.end;
|
|
6943
|
+
let stop = first.stop;
|
|
6944
|
+
while (stop === "\n") {
|
|
6945
|
+
let line = this.#lineEnd(end) + 1;
|
|
6946
|
+
let blanks = 0;
|
|
6947
|
+
let continued = false;
|
|
6948
|
+
while (line < source.length) {
|
|
6949
|
+
let index = line;
|
|
6950
|
+
while (source[index] === " ") index++;
|
|
6951
|
+
const indentation = index - line;
|
|
6952
|
+
while (source[index] === " " || source[index] === " ") index++;
|
|
6953
|
+
const next = source[index];
|
|
6954
|
+
if (next === void 0 || next === "\n") {
|
|
6955
|
+
blanks++;
|
|
6956
|
+
line = this.#lineEnd(line) + 1;
|
|
6957
|
+
continue;
|
|
6958
|
+
}
|
|
6959
|
+
if (next === "#" || indentation <= n || indentation === 0 && (this.#marker(line, "---") || this.#marker(line, "...")))
|
|
6960
|
+
break;
|
|
6961
|
+
const scanned = this.#plainLine(index);
|
|
6962
|
+
if (scanned.stop === ":")
|
|
6963
|
+
throw new ParseError(
|
|
6964
|
+
"A mapping cannot start inside a multi-line scalar.",
|
|
6965
|
+
index
|
|
6966
|
+
);
|
|
6967
|
+
lines.push(
|
|
6968
|
+
...Array.from({ length: blanks }, () => ""),
|
|
6969
|
+
source.slice(index, scanned.end)
|
|
6970
|
+
);
|
|
6971
|
+
end = scanned.end;
|
|
6972
|
+
stop = scanned.stop;
|
|
6973
|
+
continued = true;
|
|
6974
|
+
break;
|
|
6975
|
+
}
|
|
6976
|
+
if (!continued) break;
|
|
6977
|
+
}
|
|
6978
|
+
this.position = end;
|
|
6979
|
+
return { type: "Scalar", style: "plain", lines, start, end };
|
|
6980
|
+
}
|
|
6981
|
+
/** Where the text of a plain scalar ends on a line, and what stops it there. */
|
|
6982
|
+
#plainLine(from) {
|
|
6983
|
+
const source = this.#source;
|
|
6984
|
+
let index = from;
|
|
6985
|
+
let end = from;
|
|
6986
|
+
for (; ; ) {
|
|
6987
|
+
const char = source[index];
|
|
6988
|
+
if (char === void 0 || char === "\n") return { end, stop: "\n" };
|
|
6989
|
+
if (char === ":" && this.#separated(index + 1))
|
|
6990
|
+
return { end, stop: ":" };
|
|
6991
|
+
if (char === "#" && (source[index - 1] === " " || source[index - 1] === " "))
|
|
6992
|
+
return { end, stop: "#" };
|
|
6993
|
+
index++;
|
|
6994
|
+
if (char !== " " && char !== " ") end = index;
|
|
6995
|
+
}
|
|
6996
|
+
}
|
|
6997
|
+
#quoted() {
|
|
6998
|
+
const source = this.#source;
|
|
6999
|
+
const start = this.position;
|
|
7000
|
+
const quote = source[start];
|
|
7001
|
+
let index = start + 1;
|
|
7002
|
+
for (; ; ) {
|
|
7003
|
+
const char = source[index];
|
|
7004
|
+
if (char === void 0)
|
|
7005
|
+
throw new ParseError(
|
|
7006
|
+
"Unterminated string, the closing quote is missing.",
|
|
7007
|
+
start
|
|
7008
|
+
);
|
|
7009
|
+
if (quote === '"' && char === "\\") index += 2;
|
|
7010
|
+
else if (char === quote) {
|
|
7011
|
+
if (quote === "'" && source[index + 1] === "'") index += 2;
|
|
7012
|
+
else break;
|
|
7013
|
+
} else index++;
|
|
7014
|
+
}
|
|
7015
|
+
const end = index + 1;
|
|
7016
|
+
this.position = end;
|
|
7017
|
+
const raw = source.slice(start, end).split("\n");
|
|
7018
|
+
const lines = raw.map((line, index2) => {
|
|
7019
|
+
let text = index2 > 0 ? line.replace(/^[ \t]+/, "") : line;
|
|
7020
|
+
if (index2 < raw.length - 1) {
|
|
7021
|
+
const trimmed = text.replace(/[ \t]+$/, "");
|
|
7022
|
+
text = quote === '"' && trimmed.length < text.length && /(?:^|[^\\])(?:\\\\)*\\$/.test(trimmed) ? text.slice(0, trimmed.length + 1) : trimmed;
|
|
7023
|
+
}
|
|
7024
|
+
return text;
|
|
7025
|
+
});
|
|
7026
|
+
return {
|
|
7027
|
+
type: "Scalar",
|
|
7028
|
+
style: quote === "'" ? "single" : "double",
|
|
7029
|
+
lines,
|
|
7030
|
+
start,
|
|
7031
|
+
end
|
|
7032
|
+
};
|
|
7033
|
+
}
|
|
7034
|
+
#alias(flow) {
|
|
7035
|
+
const source = this.#source;
|
|
7036
|
+
const start = this.position;
|
|
7037
|
+
let index = start + 1;
|
|
7038
|
+
while (index < source.length && !" \n".includes(source[index]) && !(flow && flowIndicators.includes(source[index])))
|
|
7039
|
+
index++;
|
|
7040
|
+
this.position = index;
|
|
7041
|
+
return {
|
|
7042
|
+
type: "Alias",
|
|
7043
|
+
name: source.slice(start, index),
|
|
7044
|
+
start,
|
|
7045
|
+
end: index
|
|
7046
|
+
};
|
|
7047
|
+
}
|
|
7048
|
+
/** The tag and anchor at the position, the tag first, separated by a single space. */
|
|
7049
|
+
#properties(flow) {
|
|
7050
|
+
const source = this.#source;
|
|
7051
|
+
const tags = [];
|
|
7052
|
+
const anchors = [];
|
|
7053
|
+
for (; ; ) {
|
|
7054
|
+
const start = this.position;
|
|
7055
|
+
const char = source[start];
|
|
7056
|
+
if (char !== "&" && char !== "!") break;
|
|
7057
|
+
let index = start + 1;
|
|
7058
|
+
if (char === "!" && source[index] === "<") {
|
|
7059
|
+
const close = source.indexOf(">", index);
|
|
7060
|
+
if (close < 0 || close > this.#lineEnd(start))
|
|
7061
|
+
throw new ParseError("Unterminated verbatim tag.", start);
|
|
7062
|
+
index = close + 1;
|
|
7063
|
+
} else
|
|
7064
|
+
while (index < source.length && !" \n".includes(source[index]) && !(flow && flowIndicators.includes(source[index])))
|
|
7065
|
+
index++;
|
|
7066
|
+
(char === "!" ? tags : anchors).push(source.slice(start, index));
|
|
7067
|
+
this.position = index;
|
|
7068
|
+
this.#spaces();
|
|
7069
|
+
}
|
|
7070
|
+
const parts = [...tags, ...anchors];
|
|
7071
|
+
return parts.length > 0 ? parts.join(" ") : void 0;
|
|
7072
|
+
}
|
|
7073
|
+
/**
|
|
7074
|
+
* A literal or folded scalar. Its content is indented deeper than `n`,
|
|
7075
|
+
* by the indentation indicator or as its first line. Trailing blank lines
|
|
7076
|
+
* are its own only when it keeps them, with `+`.
|
|
7077
|
+
*/
|
|
7078
|
+
#blockScalar(n) {
|
|
7079
|
+
const source = this.#source;
|
|
7080
|
+
const start = this.position;
|
|
7081
|
+
let index = start + 1;
|
|
7082
|
+
let indicator = 0;
|
|
7083
|
+
while (index < start + 3 && /[1-9+-]/.test(source[index] ?? "")) {
|
|
7084
|
+
const char = source[index];
|
|
7085
|
+
if (/\d/.test(char)) indicator = Number(char);
|
|
7086
|
+
index++;
|
|
7087
|
+
}
|
|
7088
|
+
const header = source.slice(start, index);
|
|
7089
|
+
this.position = index;
|
|
7090
|
+
if (!this.#separated(index))
|
|
7091
|
+
throw new ParseError("Invalid block scalar header.", index);
|
|
7092
|
+
const scalar = {
|
|
7093
|
+
type: "BlockScalar",
|
|
7094
|
+
header,
|
|
7095
|
+
indicator,
|
|
7096
|
+
parent: n,
|
|
7097
|
+
indentation: 0,
|
|
7098
|
+
lines: [],
|
|
7099
|
+
start,
|
|
7100
|
+
end: index
|
|
7101
|
+
};
|
|
7102
|
+
const comment = this.#rest();
|
|
7103
|
+
if (comment) scalar.comment = comment;
|
|
7104
|
+
let indentation = indicator > 0 ? Math.max(n + indicator, 0) : -1;
|
|
7105
|
+
const lines = [];
|
|
7106
|
+
let pending = [];
|
|
7107
|
+
let pendingStart = this.position;
|
|
7108
|
+
let line = this.position;
|
|
7109
|
+
while (line < source.length) {
|
|
7110
|
+
const end = this.#lineEnd(line);
|
|
7111
|
+
const text = source.slice(line, end);
|
|
7112
|
+
const next = end < source.length ? end + 1 : end;
|
|
7113
|
+
if (/^[ \t]*$/.test(text)) {
|
|
7114
|
+
if (pending.length === 0) pendingStart = line;
|
|
7115
|
+
pending.push(text);
|
|
7116
|
+
line = next;
|
|
7117
|
+
continue;
|
|
7118
|
+
}
|
|
7119
|
+
const spaces = /^ */.exec(text)?.[0].length ?? 0;
|
|
7120
|
+
if (indentation < 0) {
|
|
7121
|
+
if (spaces <= n) break;
|
|
7122
|
+
indentation = spaces;
|
|
7123
|
+
}
|
|
7124
|
+
if (spaces < indentation || spaces === 0 && (this.#marker(line, "---") || this.#marker(line, "...")))
|
|
7125
|
+
break;
|
|
7126
|
+
lines.push(...pending, text);
|
|
7127
|
+
pending = [];
|
|
7128
|
+
line = next;
|
|
7129
|
+
}
|
|
7130
|
+
if (header.includes("+")) {
|
|
7131
|
+
lines.push(...pending);
|
|
7132
|
+
this.position = line;
|
|
7133
|
+
} else this.position = pending.length > 0 ? pendingStart : line;
|
|
7134
|
+
scalar.indentation = indentation < 0 ? n + 1 : indentation;
|
|
7135
|
+
scalar.lines = lines;
|
|
7136
|
+
scalar.end = this.position;
|
|
7137
|
+
return scalar;
|
|
7138
|
+
}
|
|
7139
|
+
#flow() {
|
|
7140
|
+
const source = this.#source;
|
|
7141
|
+
const start = this.position;
|
|
7142
|
+
const mapping = source[start] === "{";
|
|
7143
|
+
const close = mapping ? "}" : "]";
|
|
7144
|
+
this.position++;
|
|
7145
|
+
const items = [];
|
|
7146
|
+
const opening = [];
|
|
7147
|
+
for (; ; ) {
|
|
7148
|
+
(items.at(-1)?.comments ?? opening).push(...this.#flowSpace());
|
|
7149
|
+
const char = source[this.position];
|
|
7150
|
+
if (char === void 0)
|
|
7151
|
+
throw new ParseError(
|
|
7152
|
+
`Unterminated flow collection, \`${close}\` is missing.`,
|
|
7153
|
+
start
|
|
7154
|
+
);
|
|
7155
|
+
if (char === close) {
|
|
7156
|
+
this.position++;
|
|
7157
|
+
break;
|
|
7158
|
+
}
|
|
7159
|
+
const item = this.#flowItem(close);
|
|
7160
|
+
items.push(item);
|
|
7161
|
+
item.comments.push(...this.#flowSpace());
|
|
7162
|
+
const after = source[this.position];
|
|
7163
|
+
if (after === void 0)
|
|
7164
|
+
throw new ParseError(
|
|
7165
|
+
`Unterminated flow collection, \`${close}\` is missing.`,
|
|
7166
|
+
start
|
|
7167
|
+
);
|
|
7168
|
+
if (after === ",") this.position++;
|
|
7169
|
+
else if (after === close) {
|
|
7170
|
+
this.position++;
|
|
7171
|
+
break;
|
|
7172
|
+
} else
|
|
7173
|
+
throw new ParseError(
|
|
7174
|
+
`Expected \`,\` or \`${close}\`.`,
|
|
7175
|
+
this.position
|
|
7176
|
+
);
|
|
7177
|
+
}
|
|
7178
|
+
return {
|
|
7179
|
+
type: mapping ? "FlowMapping" : "FlowSequence",
|
|
7180
|
+
items,
|
|
7181
|
+
opening,
|
|
7182
|
+
start,
|
|
7183
|
+
end: this.position
|
|
7184
|
+
};
|
|
7185
|
+
}
|
|
7186
|
+
/** A pair or a single node of a flow collection, a single node is its key. */
|
|
7187
|
+
#flowItem(close) {
|
|
7188
|
+
const source = this.#source;
|
|
7189
|
+
const comments = [];
|
|
7190
|
+
if (this.#indicator(this.position, "?")) {
|
|
7191
|
+
this.position++;
|
|
7192
|
+
comments.push(...this.#flowSpace());
|
|
7193
|
+
}
|
|
7194
|
+
const empty = source[this.position] === ":" && this.#flowSeparated(this.position + 1);
|
|
7195
|
+
const key = empty ? null : this.#flowNode(close);
|
|
7196
|
+
comments.push(...this.#flowSpace());
|
|
7197
|
+
if (source[this.position] !== ":")
|
|
7198
|
+
return { key, value: null, pair: false, comments };
|
|
7199
|
+
this.position++;
|
|
7200
|
+
comments.push(...this.#flowSpace());
|
|
7201
|
+
const next = source[this.position];
|
|
7202
|
+
const value = next === "," || next === close ? null : this.#flowNode(close);
|
|
7203
|
+
return { key, value, pair: true, comments };
|
|
7204
|
+
}
|
|
7205
|
+
#flowNode(close) {
|
|
7206
|
+
const source = this.#source;
|
|
7207
|
+
const start = this.position;
|
|
7208
|
+
const properties = this.#properties(true);
|
|
7209
|
+
const char = source[this.position];
|
|
7210
|
+
let node;
|
|
7211
|
+
if (properties !== void 0 && (char === "," || char === close || char === ":" && this.#flowSeparated(this.position + 1)))
|
|
7212
|
+
node = {
|
|
7213
|
+
type: "Scalar",
|
|
7214
|
+
style: "plain",
|
|
7215
|
+
lines: [],
|
|
7216
|
+
start,
|
|
7217
|
+
end: this.position
|
|
7218
|
+
};
|
|
7219
|
+
else if (char === "[" || char === "{") node = this.#flow();
|
|
7220
|
+
else if (char === '"' || char === "'") node = this.#quoted();
|
|
7221
|
+
else if (char === "*") {
|
|
7222
|
+
if (properties !== void 0)
|
|
7223
|
+
throw new ParseError(
|
|
7224
|
+
"An alias cannot have a tag or an anchor.",
|
|
7225
|
+
this.position
|
|
7226
|
+
);
|
|
7227
|
+
node = this.#alias(true);
|
|
7228
|
+
} else if (char === void 0 || "]}#".includes(char))
|
|
7229
|
+
throw new ParseError(
|
|
7230
|
+
`Unexpected \`${char ?? "end"}\` in a flow collection.`,
|
|
7231
|
+
this.position
|
|
7232
|
+
);
|
|
7233
|
+
else node = this.#flowPlain();
|
|
7234
|
+
if (properties) {
|
|
7235
|
+
node.properties = properties;
|
|
7236
|
+
node.start = start;
|
|
7237
|
+
}
|
|
7238
|
+
return node;
|
|
7239
|
+
}
|
|
7240
|
+
/** A plain scalar in a flow collection, which may continue on the lines after it. */
|
|
7241
|
+
#flowPlain() {
|
|
7242
|
+
const source = this.#source;
|
|
7243
|
+
const start = this.position;
|
|
7244
|
+
let index = start;
|
|
7245
|
+
for (; ; ) {
|
|
7246
|
+
const char = source[index];
|
|
7247
|
+
if (char === void 0 || flowIndicators.includes(char) || char === ":" && this.#flowSeparated(index + 1) || char === "#" && " \n".includes(source[index - 1] ?? ""))
|
|
7248
|
+
break;
|
|
7249
|
+
index++;
|
|
7250
|
+
}
|
|
7251
|
+
const raw = source.slice(start, index).replace(/\s+$/, "");
|
|
7252
|
+
this.position = start + raw.length;
|
|
7253
|
+
const lines = raw.split("\n").map((line, index2) => index2 === 0 ? line.trimEnd() : line.trim());
|
|
7254
|
+
return {
|
|
7255
|
+
type: "Scalar",
|
|
7256
|
+
style: "plain",
|
|
7257
|
+
lines,
|
|
7258
|
+
start,
|
|
7259
|
+
end: this.position
|
|
7260
|
+
};
|
|
7261
|
+
}
|
|
7262
|
+
/** Skips whitespace and line breaks in a flow collection, returning its comments. */
|
|
7263
|
+
#flowSpace() {
|
|
7264
|
+
const source = this.#source;
|
|
7265
|
+
const comments = [];
|
|
7266
|
+
for (; ; ) {
|
|
7267
|
+
const char = source[this.position];
|
|
7268
|
+
if (char === " " || char === " " || char === "\n") this.position++;
|
|
7269
|
+
else if (char === "#") comments.push(this.#comment());
|
|
7270
|
+
else return comments;
|
|
7271
|
+
}
|
|
7272
|
+
}
|
|
7273
|
+
/** Reads the comment at the position, up to the end of its line. */
|
|
7274
|
+
#comment() {
|
|
7275
|
+
const start = this.position;
|
|
7276
|
+
const end = this.#lineEnd(start);
|
|
7277
|
+
this.position = end;
|
|
7278
|
+
return {
|
|
7279
|
+
start,
|
|
7280
|
+
end,
|
|
7281
|
+
value: this.#source.slice(start, end).trimEnd()
|
|
7282
|
+
};
|
|
7283
|
+
}
|
|
7284
|
+
/** Finishes a single line node with the rest of its line. */
|
|
7285
|
+
#finish(node) {
|
|
7286
|
+
if (this.#atLineStart()) return;
|
|
7287
|
+
const comment = this.#rest();
|
|
7288
|
+
if (comment) node.comment = comment;
|
|
7289
|
+
}
|
|
7290
|
+
/** The rest of a line, only spaces and a comment, then moves to the next line. */
|
|
7291
|
+
#rest() {
|
|
7292
|
+
this.#spaces();
|
|
7293
|
+
let comment;
|
|
7294
|
+
if (this.#source[this.position] === "#") comment = this.#comment();
|
|
7295
|
+
if (!this.#lineEnded())
|
|
7296
|
+
throw new ParseError(
|
|
7297
|
+
"Unexpected content after a node.",
|
|
7298
|
+
this.position
|
|
7299
|
+
);
|
|
7300
|
+
this.#nextLine();
|
|
7301
|
+
return comment;
|
|
7302
|
+
}
|
|
7303
|
+
/** The blank and comment lines from the line at the position to the next line with content. */
|
|
7304
|
+
#gap() {
|
|
7305
|
+
const source = this.#source;
|
|
7306
|
+
const lines = [];
|
|
7307
|
+
let start = this.position;
|
|
7308
|
+
while (start < source.length) {
|
|
7309
|
+
let index = start;
|
|
7310
|
+
while (source[index] === " ") index++;
|
|
7311
|
+
const indentation = index - start;
|
|
7312
|
+
while (source[index] === " " || source[index] === " ") index++;
|
|
7313
|
+
const char = source[index];
|
|
7314
|
+
const end = this.#lineEnd(start);
|
|
7315
|
+
const next = end < source.length ? end + 1 : end;
|
|
7316
|
+
if (char === void 0 || char === "\n") {
|
|
7317
|
+
lines.push({ next, column: -1 });
|
|
7318
|
+
start = next;
|
|
7319
|
+
continue;
|
|
7320
|
+
}
|
|
7321
|
+
if (char === "#") {
|
|
7322
|
+
lines.push({
|
|
7323
|
+
next,
|
|
7324
|
+
column: index - start,
|
|
7325
|
+
comment: {
|
|
7326
|
+
start: index,
|
|
7327
|
+
end,
|
|
7328
|
+
value: source.slice(index, end).trimEnd()
|
|
7329
|
+
}
|
|
7330
|
+
});
|
|
7331
|
+
start = next;
|
|
7332
|
+
continue;
|
|
7333
|
+
}
|
|
7334
|
+
if (index !== start + indentation)
|
|
7335
|
+
throw new ParseError(
|
|
7336
|
+
"Tabs are not allowed for indentation in YAML.",
|
|
7337
|
+
start + indentation
|
|
7338
|
+
);
|
|
7339
|
+
const marker = indentation === 0 && (this.#marker(start, "---") || this.#marker(start, "..."));
|
|
7340
|
+
return { lines, start, column: marker ? -1 : indentation };
|
|
7341
|
+
}
|
|
7342
|
+
return { lines, start: source.length, column: -1 };
|
|
7343
|
+
}
|
|
7344
|
+
/** Whether a document marker, `---` or `...`, starts a line at a position. */
|
|
7345
|
+
#marker(position, marker) {
|
|
7346
|
+
return this.#source.startsWith(marker, position) && this.#separated(position + 3);
|
|
7347
|
+
}
|
|
7348
|
+
/** Whether an indicator like `-` or `?` is at a position, followed by a space or a line end. */
|
|
7349
|
+
#indicator(position, char) {
|
|
7350
|
+
return this.#source[position] === char && this.#separated(position + 1);
|
|
7351
|
+
}
|
|
7352
|
+
#separated(position) {
|
|
7353
|
+
const char = this.#source[position];
|
|
7354
|
+
return char === void 0 || char === " " || char === " " || char === "\n";
|
|
7355
|
+
}
|
|
7356
|
+
/** Whether a `:` before a position separates a key in a flow collection. */
|
|
7357
|
+
#flowSeparated(position) {
|
|
7358
|
+
return this.#separated(position) || flowIndicators.includes(this.#source[position]);
|
|
7359
|
+
}
|
|
7360
|
+
#spaces() {
|
|
7361
|
+
while (this.#source[this.position] === " " || this.#source[this.position] === " ")
|
|
7362
|
+
this.position++;
|
|
7363
|
+
}
|
|
7364
|
+
#lineEnded() {
|
|
7365
|
+
return this.position >= this.#source.length || this.#source[this.position] === "\n";
|
|
7366
|
+
}
|
|
7367
|
+
#atLineStart() {
|
|
7368
|
+
return this.position >= this.#source.length || this.position === 0 || this.#source[this.position - 1] === "\n";
|
|
7369
|
+
}
|
|
7370
|
+
#nextLine() {
|
|
7371
|
+
const end = this.#lineEnd(this.position);
|
|
7372
|
+
this.position = end < this.#source.length ? end + 1 : end;
|
|
7373
|
+
}
|
|
7374
|
+
#lineEnd(position) {
|
|
7375
|
+
const end = this.#source.indexOf("\n", position);
|
|
7376
|
+
return end < 0 ? this.#source.length : end;
|
|
7377
|
+
}
|
|
7378
|
+
#column(position) {
|
|
7379
|
+
return position - (this.#source.lastIndexOf("\n", position - 1) + 1);
|
|
7380
|
+
}
|
|
7381
|
+
};
|
|
7382
|
+
|
|
6478
7383
|
// shared/syntax/src/walker.ts
|
|
6479
7384
|
var Walker = class _Walker {
|
|
6480
7385
|
static {
|
|
@@ -6532,6 +7437,10 @@ function isHtml(file) {
|
|
|
6532
7437
|
return /\.html?$/i.test(file);
|
|
6533
7438
|
}
|
|
6534
7439
|
__name(isHtml, "isHtml");
|
|
7440
|
+
function isYaml(file) {
|
|
7441
|
+
return /\.ya?ml$/i.test(file);
|
|
7442
|
+
}
|
|
7443
|
+
__name(isYaml, "isYaml");
|
|
6535
7444
|
function isCss(file) {
|
|
6536
7445
|
return /\.css$/i.test(file);
|
|
6537
7446
|
}
|
|
@@ -6552,6 +7461,11 @@ function parseCssValues(source, start = 0, end = source.length) {
|
|
|
6552
7461
|
});
|
|
6553
7462
|
}
|
|
6554
7463
|
__name(parseCssValues, "parseCssValues");
|
|
7464
|
+
function parseYaml(source) {
|
|
7465
|
+
const parser = new YamlParser(source);
|
|
7466
|
+
return shallow(() => parser.parse(), parser);
|
|
7467
|
+
}
|
|
7468
|
+
__name(parseYaml, "parseYaml");
|
|
6555
7469
|
function parseHtml(source) {
|
|
6556
7470
|
const parser = new Parser2(source);
|
|
6557
7471
|
return shallow(() => parser.parse(), parser);
|
|
@@ -6594,6 +7508,7 @@ export {
|
|
|
6594
7508
|
isCss,
|
|
6595
7509
|
isHtml,
|
|
6596
7510
|
isMarkdown,
|
|
7511
|
+
isYaml,
|
|
6597
7512
|
language,
|
|
6598
7513
|
parse,
|
|
6599
7514
|
parseCss,
|
|
@@ -6601,5 +7516,6 @@ export {
|
|
|
6601
7516
|
parseExpression,
|
|
6602
7517
|
parseHtml,
|
|
6603
7518
|
parseMarkdown,
|
|
7519
|
+
parseYaml,
|
|
6604
7520
|
tagOf
|
|
6605
7521
|
};
|
package/bin/types/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type { Root } from "./markdown/ast.js";
|
|
|
5
5
|
import { type MarkdownOptions } from "./markdown/blocks.js";
|
|
6
6
|
import type { Options } from "./parser/base.js";
|
|
7
7
|
import { type Comment } from "./scanner.js";
|
|
8
|
+
import type { Stream } from "./yaml/ast.js";
|
|
8
9
|
export type * from "./ast.js";
|
|
9
10
|
export type * as Css from "./css/ast.js";
|
|
10
11
|
export type * as Html from "./html/ast.js";
|
|
@@ -14,6 +15,7 @@ export type * as Markdown from "./markdown/ast.js";
|
|
|
14
15
|
export type { Comment, Token } from "./scanner.js";
|
|
15
16
|
export { ParseError } from "./scanner.js";
|
|
16
17
|
export { default as Walker } from "./walker.js";
|
|
18
|
+
export type * as Yaml from "./yaml/ast.js";
|
|
17
19
|
export type { MarkdownOptions, Options };
|
|
18
20
|
export interface Parsed {
|
|
19
21
|
program: Program;
|
|
@@ -31,6 +33,8 @@ export declare function language(file: string): Language | undefined;
|
|
|
31
33
|
export declare function isMarkdown(file: string): boolean;
|
|
32
34
|
/** Whether a file is HTML, by its extension. */
|
|
33
35
|
export declare function isHtml(file: string): boolean;
|
|
36
|
+
/** Whether a file is YAML, by its extension. */
|
|
37
|
+
export declare function isYaml(file: string): boolean;
|
|
34
38
|
/** Whether a file is CSS, by its extension. */
|
|
35
39
|
export declare function isCss(file: string): boolean;
|
|
36
40
|
/**
|
|
@@ -43,6 +47,11 @@ export declare function parseCss(source: string, options?: {
|
|
|
43
47
|
}): Stylesheet;
|
|
44
48
|
/** Parses the component values of a range of CSS, like a `style` attribute value. */
|
|
45
49
|
export declare function parseCssValues(source: string, start?: number, end?: number): import("./css/ast.js").Value[];
|
|
50
|
+
/**
|
|
51
|
+
* Parses YAML, with line feeds as line endings, into documents, throwing a
|
|
52
|
+
* ParseError at the first error.
|
|
53
|
+
*/
|
|
54
|
+
export declare function parseYaml(source: string): Stream;
|
|
46
55
|
/** Parses HTML, throwing a ParseError at the first error. */
|
|
47
56
|
export declare function parseHtml(source: string): HtmlRoot;
|
|
48
57
|
/** Parses CommonMark, with GitHub's tables, task lists and strikethrough unless `gfm` is false. */
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/** A `#` comment, its value starting with the `#`. */
|
|
2
|
+
export interface Comment {
|
|
3
|
+
start: number;
|
|
4
|
+
end: number;
|
|
5
|
+
value: string;
|
|
6
|
+
}
|
|
7
|
+
/** A comment on a line of its own, after a blank line or not. */
|
|
8
|
+
export interface CommentLine {
|
|
9
|
+
comment: Comment;
|
|
10
|
+
blank: boolean;
|
|
11
|
+
}
|
|
12
|
+
interface Base {
|
|
13
|
+
start: number;
|
|
14
|
+
end: number;
|
|
15
|
+
/** Tag and anchor, like `!!str &a`, the tag first, separated by a single space. */
|
|
16
|
+
properties?: string;
|
|
17
|
+
/** The comment after properties standing alone on their line. */
|
|
18
|
+
propertiesComment?: Comment;
|
|
19
|
+
/** Comments on their own lines between such properties and the node. */
|
|
20
|
+
leading?: CommentLine[];
|
|
21
|
+
/** The comment after the node on its last line. */
|
|
22
|
+
comment?: Comment;
|
|
23
|
+
}
|
|
24
|
+
/** A plain, single or double quoted scalar. */
|
|
25
|
+
export interface Scalar extends Base {
|
|
26
|
+
type: "Scalar";
|
|
27
|
+
style: "plain" | "single" | "double";
|
|
28
|
+
/** Its lines without their indentation and trailing spaces, quotes included, blank lines empty. */
|
|
29
|
+
lines: string[];
|
|
30
|
+
}
|
|
31
|
+
/** A literal `|` or folded `>` scalar. */
|
|
32
|
+
export interface BlockScalar extends Base {
|
|
33
|
+
type: "BlockScalar";
|
|
34
|
+
/** Like `|`, `>-` or `|2+`. */
|
|
35
|
+
header: string;
|
|
36
|
+
/** The explicit indentation indicator, 0 without one. */
|
|
37
|
+
indicator: number;
|
|
38
|
+
/** The indentation of the node the scalar belongs to, -1 for a document's own. */
|
|
39
|
+
parent: number;
|
|
40
|
+
/** The indentation of its content. */
|
|
41
|
+
indentation: number;
|
|
42
|
+
/** Its content lines as written, with their indentation. */
|
|
43
|
+
lines: string[];
|
|
44
|
+
}
|
|
45
|
+
export interface Alias extends Base {
|
|
46
|
+
type: "Alias";
|
|
47
|
+
/** Like `*a`. */
|
|
48
|
+
name: string;
|
|
49
|
+
}
|
|
50
|
+
/** An entry of a flow collection, a pair like `a: 1` or a single node, which is its key. */
|
|
51
|
+
export interface FlowItem {
|
|
52
|
+
key: Node | null;
|
|
53
|
+
value: Node | null;
|
|
54
|
+
/** Whether it has a `:`. */
|
|
55
|
+
pair: boolean;
|
|
56
|
+
/** Comments after it, before the next one. */
|
|
57
|
+
comments: Comment[];
|
|
58
|
+
}
|
|
59
|
+
export interface FlowMapping extends Base {
|
|
60
|
+
type: "FlowMapping";
|
|
61
|
+
items: FlowItem[];
|
|
62
|
+
/** Comments before the first item. */
|
|
63
|
+
opening: Comment[];
|
|
64
|
+
}
|
|
65
|
+
export interface FlowSequence extends Base {
|
|
66
|
+
type: "FlowSequence";
|
|
67
|
+
items: FlowItem[];
|
|
68
|
+
opening: Comment[];
|
|
69
|
+
}
|
|
70
|
+
/** What block mapping entries and sequence items share. */
|
|
71
|
+
interface Entry {
|
|
72
|
+
/** Comments on their own lines before it, at its indentation. */
|
|
73
|
+
leading: CommentLine[];
|
|
74
|
+
/** Whether a blank line comes before it, after its leading comments. */
|
|
75
|
+
blank: boolean;
|
|
76
|
+
value: Node | null;
|
|
77
|
+
/** The comment after its indicator, `:` or `-`, when the value is on the lines after it. */
|
|
78
|
+
comment?: Comment;
|
|
79
|
+
/** Comments on their own lines between the indicator and a value on the lines after it. */
|
|
80
|
+
between: CommentLine[];
|
|
81
|
+
/** Whether a blank line comes before such a value, after those comments. */
|
|
82
|
+
betweenBlank: boolean;
|
|
83
|
+
/** Comments indented deeper than it after its value, which is not a block scalar. */
|
|
84
|
+
inner: CommentLine[];
|
|
85
|
+
}
|
|
86
|
+
export interface MappingEntry extends Entry {
|
|
87
|
+
key: Node | null;
|
|
88
|
+
/** Whether the key is written after `?`. */
|
|
89
|
+
explicit: boolean;
|
|
90
|
+
/** The comment after `?` when the key is on the lines after it. */
|
|
91
|
+
keyComment?: Comment;
|
|
92
|
+
/** Comments on their own lines around an explicit key. */
|
|
93
|
+
keyBetween: CommentLine[];
|
|
94
|
+
}
|
|
95
|
+
export type SequenceItem = Entry;
|
|
96
|
+
export interface Mapping extends Base {
|
|
97
|
+
type: "Mapping";
|
|
98
|
+
entries: MappingEntry[];
|
|
99
|
+
/** Comments at its indentation after its last entry. */
|
|
100
|
+
endComments: CommentLine[];
|
|
101
|
+
}
|
|
102
|
+
export interface Sequence extends Base {
|
|
103
|
+
type: "Sequence";
|
|
104
|
+
items: SequenceItem[];
|
|
105
|
+
endComments: CommentLine[];
|
|
106
|
+
}
|
|
107
|
+
export type Node = Scalar | BlockScalar | Alias | FlowMapping | FlowSequence | Mapping | Sequence;
|
|
108
|
+
/** A comment or a directive, like `%YAML 1.2`, before a document starts. */
|
|
109
|
+
export interface Prelude {
|
|
110
|
+
text: string;
|
|
111
|
+
blank: boolean;
|
|
112
|
+
}
|
|
113
|
+
export interface Document {
|
|
114
|
+
type: "Document";
|
|
115
|
+
start: number;
|
|
116
|
+
end: number;
|
|
117
|
+
prelude: Prelude[];
|
|
118
|
+
/** Whether it starts with `---`. */
|
|
119
|
+
explicitStart: boolean;
|
|
120
|
+
/** Whether a blank line comes before `---`, after the prelude. */
|
|
121
|
+
startBlank: boolean;
|
|
122
|
+
/** The comment after `---`. */
|
|
123
|
+
startComment?: Comment;
|
|
124
|
+
/** Comments before its node, after `---`. */
|
|
125
|
+
leading: CommentLine[];
|
|
126
|
+
/** Whether a blank line comes before its node, after those comments. */
|
|
127
|
+
blank: boolean;
|
|
128
|
+
root: Node | null;
|
|
129
|
+
/** Comments after its node. */
|
|
130
|
+
trailing: CommentLine[];
|
|
131
|
+
/** Whether it ends with `...`. */
|
|
132
|
+
explicitEnd: boolean;
|
|
133
|
+
/** The comment after `...`. */
|
|
134
|
+
endComment?: Comment;
|
|
135
|
+
}
|
|
136
|
+
export interface Stream {
|
|
137
|
+
type: "Stream";
|
|
138
|
+
documents: Document[];
|
|
139
|
+
}
|
|
140
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Stream } from "./ast.js";
|
|
2
|
+
/**
|
|
3
|
+
* Parses YAML, with line feeds as line endings, into documents of block
|
|
4
|
+
* and flow nodes, keeping comments and blank lines where they are.
|
|
5
|
+
*/
|
|
6
|
+
export default class YamlParser {
|
|
7
|
+
#private;
|
|
8
|
+
position: number;
|
|
9
|
+
constructor(source: string);
|
|
10
|
+
parse(): Stream;
|
|
11
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aljazmc/syntax",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Shared, dependency-free
|
|
3
|
+
"version": "0.7.1",
|
|
4
|
+
"description": "Shared, dependency-free parser for CSS, HTML, JavaScript, JSON, JSX, Markdown, TypeScript and YAML, bundled into @aljazmc/formatero and @aljazmc/lintero.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"parser",
|
|
7
|
+
"ast",
|
|
8
|
+
"estree",
|
|
9
|
+
"javascript",
|
|
10
|
+
"typescript",
|
|
11
|
+
"jsx",
|
|
12
|
+
"json",
|
|
13
|
+
"css",
|
|
14
|
+
"html",
|
|
15
|
+
"markdown",
|
|
16
|
+
"yaml",
|
|
17
|
+
"zero-dependency"
|
|
18
|
+
],
|
|
5
19
|
"author": "aljazmc",
|
|
6
20
|
"license": "MIT",
|
|
7
21
|
"type": "module",
|