@aljazmc/syntax 0.6.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/index.js CHANGED
@@ -594,6 +594,8 @@ var NTH_PSEUDOS = /* @__PURE__ */ new Set([
594
594
  "nth-col",
595
595
  "nth-last-col"
596
596
  ]);
597
+ var BLOCK_AT_RULES = /^(?:-[a-z]+-)?(?:media|supports|font-face|keyframes|page|container|document|property|scope|starting-style|counter-style|font-feature-values|view-transition|position-try|font-palette-values)$/;
598
+ var STATEMENT_AT_RULES = /^(?:import|namespace|charset)$/;
597
599
  var KEYFRAMES = /^(?:-[a-z]+-)?keyframes$/;
598
600
  var Parser = class _Parser {
599
601
  static {
@@ -653,6 +655,10 @@ var Parser = class _Parser {
653
655
  } else if (depth === 0 && type === "semicolon") return;
654
656
  }
655
657
  }
658
+ /** Where the parser is, the start of its current token. */
659
+ get position() {
660
+ return this.#peek().start;
661
+ }
656
662
  parse() {
657
663
  const children = [];
658
664
  for (; ; ) {
@@ -887,13 +893,22 @@ var Parser = class _Parser {
887
893
  let semicolon = false;
888
894
  let end = preludeEnd;
889
895
  const token = this.#peek();
896
+ if (keyword === "charset") this.#charset(prelude, token);
897
+ const needs = BLOCK_AT_RULES.test(keyword) ? "{" : STATEMENT_AT_RULES.test(keyword) ? ";" : void 0;
898
+ const found = token.type === "eof" ? "the file ends" : `found \`${token.type === "semicolon" ? ";" : token.type}\``;
890
899
  if (token.type === "{") {
900
+ if (needs === ";")
901
+ this.#error(`expected \`;\` but instead ${found}`, token.start);
891
902
  block = this.#block(KEYFRAMES.test(keyword));
892
903
  end = block.end;
893
- } else if (token.type === "semicolon") {
904
+ } else if (token.type === "semicolon" && needs !== "{") {
894
905
  end = this.#next().end;
895
906
  semicolon = true;
896
- }
907
+ } else
908
+ this.#error(
909
+ `expected \`${needs ?? ";"}\` but instead ${found}`,
910
+ token.start
911
+ );
897
912
  return {
898
913
  type: "AtRule",
899
914
  start: at.start,
@@ -907,6 +922,16 @@ var Parser = class _Parser {
907
922
  semicolon
908
923
  };
909
924
  }
925
+ /** Like Biome, `@charset` takes one string. */
926
+ #charset(prelude, next) {
927
+ const [first] = prelude;
928
+ if (prelude.length === 1 && first?.type === "String") return;
929
+ const found = first ? `'${this.#source.slice(first.start, prelude.at(-1).end)}'` : next.type === "eof" ? "the end of the file" : `'${next.type === "semicolon" ? ";" : next.type}'`;
930
+ this.#error(
931
+ `Expected a string but instead found ${found}.`,
932
+ first?.start ?? next.start
933
+ );
934
+ }
910
935
  /** A component value, with nested values of functions and blocks. */
911
936
  #value() {
912
937
  const token = this.#next();
@@ -1801,6 +1826,10 @@ var Parser2 = class {
1801
1826
  constructor(source) {
1802
1827
  this.#source = source;
1803
1828
  }
1829
+ /** Where the parser is. */
1830
+ get position() {
1831
+ return this.#position;
1832
+ }
1804
1833
  parse() {
1805
1834
  const source = this.#source;
1806
1835
  const bom = source.startsWith("\uFEFF");
@@ -2822,6 +2851,10 @@ var Blocks = class _Blocks {
2822
2851
  if (start < source.length)
2823
2852
  this.#lines.push({ text: source.slice(start), start });
2824
2853
  }
2854
+ /** Where the parser is, the start of its current line. */
2855
+ get position() {
2856
+ return this.#lineStart;
2857
+ }
2825
2858
  parse(frontmatter = false) {
2826
2859
  const bom = this.#source.startsWith("\uFEFF");
2827
2860
  let first = 0;
@@ -3642,6 +3675,41 @@ var Blocks = class _Blocks {
3642
3675
  }
3643
3676
  };
3644
3677
 
3678
+ // shared/syntax/src/lines.ts
3679
+ var Lines = class {
3680
+ static {
3681
+ __name(this, "Lines");
3682
+ }
3683
+ #starts = [0];
3684
+ #text;
3685
+ constructor(text) {
3686
+ this.#text = text;
3687
+ for (const match of text.matchAll(/\r\n|\r|\n/g))
3688
+ this.#starts.push(match.index + match[0].length);
3689
+ }
3690
+ line(offset) {
3691
+ let low = 0;
3692
+ let high = this.#starts.length - 1;
3693
+ while (low < high) {
3694
+ const middle = Math.ceil((low + high) / 2);
3695
+ if (this.#starts[middle] <= offset) low = middle;
3696
+ else high = middle - 1;
3697
+ }
3698
+ return low + 1;
3699
+ }
3700
+ column(offset) {
3701
+ const start = this.#starts[this.line(offset) - 1];
3702
+ let column = 1;
3703
+ for (let index = start; index < offset; index++) {
3704
+ const code = this.#text.charCodeAt(index);
3705
+ if (code >= 55296 && code <= 56319 && index + 1 < offset && (this.#text.charCodeAt(index + 1) & 64512) === 56320)
3706
+ index++;
3707
+ column++;
3708
+ }
3709
+ return column;
3710
+ }
3711
+ };
3712
+
3645
3713
  // shared/syntax/src/parser/base.ts
3646
3714
  var BaseParser = class {
3647
3715
  static {
@@ -3651,6 +3719,10 @@ var BaseParser = class {
3651
3719
  scanner;
3652
3720
  options;
3653
3721
  token;
3722
+ /** Where the parser is, the start of its current token. */
3723
+ get position() {
3724
+ return this.token.start;
3725
+ }
3654
3726
  /** End of the last consumed token. */
3655
3727
  previous = 0;
3656
3728
  /** Whether `in` is a binary operator here (not in a for-in head). */
@@ -3747,10 +3819,11 @@ var BaseParser = class {
3747
3819
  );
3748
3820
  }
3749
3821
  raise(message, position) {
3750
- const before = this.source.slice(0, position);
3751
- const line = before.split("\n").length;
3752
- const column = position - before.lastIndexOf("\n");
3753
- throw new ParseError(`${message} (${line}:${column})`, position);
3822
+ const lines = new Lines(this.source);
3823
+ throw new ParseError(
3824
+ `${message} (${lines.line(position)}:${lines.column(position)})`,
3825
+ position
3826
+ );
3754
3827
  }
3755
3828
  };
3756
3829
 
@@ -6372,11 +6445,12 @@ var Parser3 = class extends ExpressionParser {
6372
6445
  });
6373
6446
  }
6374
6447
  #module(start, declare) {
6375
- const keyword = this.next().value;
6448
+ const token = this.next();
6449
+ const keyword = token.value;
6376
6450
  const kind = keyword === "global" ? "global" : keyword === "module" ? "module" : "namespace";
6377
6451
  let id;
6378
6452
  if (kind === "global")
6379
- id = this.finish("Identifier", start, { name: "global" });
6453
+ id = this.finish("Identifier", token.start, { name: "global" });
6380
6454
  else if (this.token.type === "string") id = this.parseString();
6381
6455
  else id = this.parseEntityName();
6382
6456
  if (!this.is("{")) {
@@ -6401,28 +6475,908 @@ var Parser3 = class extends ExpressionParser {
6401
6475
  }
6402
6476
  };
6403
6477
 
6404
- // shared/syntax/src/lines.ts
6405
- var Lines = class {
6478
+ // shared/syntax/src/yaml/parser.ts
6479
+ var flowIndicators = ",[]{}";
6480
+ var YamlParser = class {
6406
6481
  static {
6407
- __name(this, "Lines");
6482
+ __name(this, "YamlParser");
6408
6483
  }
6409
- #starts = [0];
6410
- constructor(text) {
6411
- for (const match of text.matchAll(/\r\n|\r|\n/g))
6412
- this.#starts.push(match.index + match[0].length);
6484
+ position = 0;
6485
+ #source;
6486
+ constructor(source) {
6487
+ this.#source = source;
6413
6488
  }
6414
- line(offset) {
6415
- let low = 0;
6416
- let high = this.#starts.length - 1;
6417
- while (low < high) {
6418
- const middle = Math.ceil((low + high) / 2);
6419
- if (this.#starts[middle] <= offset) low = middle;
6420
- else high = middle - 1;
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);
6421
6498
  }
6422
- return low + 1;
6499
+ return { type: "Stream", documents };
6423
6500
  }
6424
- column(offset) {
6425
- return offset - this.#starts[this.line(offset) - 1] + 1;
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);
6426
7380
  }
6427
7381
  };
6428
7382
 
@@ -6451,6 +7405,19 @@ var Walker = class _Walker {
6451
7405
  };
6452
7406
 
6453
7407
  // shared/syntax/src/index.ts
7408
+ function shallow(run, parser) {
7409
+ try {
7410
+ return run();
7411
+ } catch (error) {
7412
+ if (error instanceof RangeError && /call stack/i.test(error.message))
7413
+ throw new ParseError(
7414
+ "The nesting is too deep to parse.",
7415
+ parser.position
7416
+ );
7417
+ throw error;
7418
+ }
7419
+ }
7420
+ __name(shallow, "shallow");
6454
7421
  function language(file) {
6455
7422
  if (/\.jsonc?$/.test(file))
6456
7423
  return { json: true, typescript: false, jsx: false };
@@ -6470,40 +7437,54 @@ function isHtml(file) {
6470
7437
  return /\.html?$/i.test(file);
6471
7438
  }
6472
7439
  __name(isHtml, "isHtml");
7440
+ function isYaml(file) {
7441
+ return /\.ya?ml$/i.test(file);
7442
+ }
7443
+ __name(isYaml, "isYaml");
6473
7444
  function isCss(file) {
6474
7445
  return /\.css$/i.test(file);
6475
7446
  }
6476
7447
  __name(isCss, "isCss");
6477
7448
  function parseCss(source, options = {}) {
6478
- return new Parser(
7449
+ const parser = new Parser(
6479
7450
  source,
6480
7451
  void 0,
6481
7452
  void 0,
6482
7453
  options.recover ?? false
6483
- ).parse();
7454
+ );
7455
+ return shallow(() => parser.parse(), parser);
6484
7456
  }
6485
7457
  __name(parseCss, "parseCss");
6486
7458
  function parseCssValues(source, start = 0, end = source.length) {
6487
- return Parser.values(source, start, end);
7459
+ return shallow(() => Parser.values(source, start, end), {
7460
+ position: start
7461
+ });
6488
7462
  }
6489
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");
6490
7469
  function parseHtml(source) {
6491
- return new Parser2(source).parse();
7470
+ const parser = new Parser2(source);
7471
+ return shallow(() => parser.parse(), parser);
6492
7472
  }
6493
7473
  __name(parseHtml, "parseHtml");
6494
7474
  function parseMarkdown(source, options = {}) {
6495
- return new Blocks(source, options).parse(options.frontmatter);
7475
+ const parser = new Blocks(source, options);
7476
+ return shallow(() => parser.parse(options.frontmatter), parser);
6496
7477
  }
6497
7478
  __name(parseMarkdown, "parseMarkdown");
6498
7479
  function parse(source, options) {
6499
7480
  const parser = new Parser3(source, options);
6500
- const program = parser.parseProgram();
7481
+ const program = shallow(() => parser.parseProgram(), parser);
6501
7482
  return { program, comments: parser.comments };
6502
7483
  }
6503
7484
  __name(parse, "parse");
6504
7485
  function parseExpression(source) {
6505
7486
  const parser = new Parser3(source, { typescript: false, jsx: false });
6506
- const expression = parser.parseDocument();
7487
+ const expression = shallow(() => parser.parseDocument(), parser);
6507
7488
  const statement = {
6508
7489
  type: "ExpressionStatement",
6509
7490
  start: expression.start,
@@ -6527,6 +7508,7 @@ export {
6527
7508
  isCss,
6528
7509
  isHtml,
6529
7510
  isMarkdown,
7511
+ isYaml,
6530
7512
  language,
6531
7513
  parse,
6532
7514
  parseCss,
@@ -6534,5 +7516,6 @@ export {
6534
7516
  parseExpression,
6535
7517
  parseHtml,
6536
7518
  parseMarkdown,
7519
+ parseYaml,
6537
7520
  tagOf
6538
7521
  };