@shikijs/core 1.16.0 → 1.16.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,4 +1,6 @@
1
1
  import { FontStyle } from './types.mjs';
2
+ import { INITIAL, EncodedTokenMetadata, Registry as Registry$1, Theme } from '@shikijs/vscode-textmate';
3
+ export { EncodedTokenMetadata as StackElementMetadata } from '@shikijs/vscode-textmate';
2
4
 
3
5
  function toArray(x) {
4
6
  return Array.isArray(x) ? x : [x];
@@ -104,7 +106,8 @@ function splitToken(token, offsets) {
104
106
  * Split 2D tokens array by given breakpoints.
105
107
  */
106
108
  function splitTokens(tokens, breakpoints) {
107
- const sorted = Array.from(breakpoints instanceof Set ? breakpoints : new Set(breakpoints))
109
+ const sorted = Array
110
+ .from(breakpoints instanceof Set ? breakpoints : new Set(breakpoints))
108
111
  .sort((a, b) => a - b);
109
112
  if (!sorted.length)
110
113
  return tokens;
@@ -387,3010 +390,6 @@ function getTransformers(options) {
387
390
  ];
388
391
  }
389
392
 
390
- // src/utils.ts
391
- function clone(something) {
392
- return doClone(something);
393
- }
394
- function doClone(something) {
395
- if (Array.isArray(something)) {
396
- return cloneArray(something);
397
- }
398
- if (typeof something === "object") {
399
- return cloneObj(something);
400
- }
401
- return something;
402
- }
403
- function cloneArray(arr) {
404
- let r = [];
405
- for (let i = 0, len = arr.length; i < len; i++) {
406
- r[i] = doClone(arr[i]);
407
- }
408
- return r;
409
- }
410
- function cloneObj(obj) {
411
- let r = {};
412
- for (let key in obj) {
413
- r[key] = doClone(obj[key]);
414
- }
415
- return r;
416
- }
417
- function mergeObjects(target, ...sources) {
418
- sources.forEach((source) => {
419
- for (let key in source) {
420
- target[key] = source[key];
421
- }
422
- });
423
- return target;
424
- }
425
- function basename(path) {
426
- const idx = ~path.lastIndexOf("/") || ~path.lastIndexOf("\\");
427
- if (idx === 0) {
428
- return path;
429
- } else if (~idx === path.length - 1) {
430
- return basename(path.substring(0, path.length - 1));
431
- } else {
432
- return path.substr(~idx + 1);
433
- }
434
- }
435
- var CAPTURING_REGEX_SOURCE = /\$(\d+)|\${(\d+):\/(downcase|upcase)}/g;
436
- var RegexSource = class {
437
- static hasCaptures(regexSource) {
438
- if (regexSource === null) {
439
- return false;
440
- }
441
- CAPTURING_REGEX_SOURCE.lastIndex = 0;
442
- return CAPTURING_REGEX_SOURCE.test(regexSource);
443
- }
444
- static replaceCaptures(regexSource, captureSource, captureIndices) {
445
- return regexSource.replace(CAPTURING_REGEX_SOURCE, (match, index, commandIndex, command) => {
446
- let capture = captureIndices[parseInt(index || commandIndex, 10)];
447
- if (capture) {
448
- let result = captureSource.substring(capture.start, capture.end);
449
- while (result[0] === ".") {
450
- result = result.substring(1);
451
- }
452
- switch (command) {
453
- case "downcase":
454
- return result.toLowerCase();
455
- case "upcase":
456
- return result.toUpperCase();
457
- default:
458
- return result;
459
- }
460
- } else {
461
- return match;
462
- }
463
- });
464
- }
465
- };
466
- function strcmp(a, b) {
467
- if (a < b) {
468
- return -1;
469
- }
470
- if (a > b) {
471
- return 1;
472
- }
473
- return 0;
474
- }
475
- function strArrCmp(a, b) {
476
- if (a === null && b === null) {
477
- return 0;
478
- }
479
- if (!a) {
480
- return -1;
481
- }
482
- if (!b) {
483
- return 1;
484
- }
485
- let len1 = a.length;
486
- let len2 = b.length;
487
- if (len1 === len2) {
488
- for (let i = 0; i < len1; i++) {
489
- let res = strcmp(a[i], b[i]);
490
- if (res !== 0) {
491
- return res;
492
- }
493
- }
494
- return 0;
495
- }
496
- return len1 - len2;
497
- }
498
- function isValidHexColor(hex) {
499
- if (/^#[0-9a-f]{6}$/i.test(hex)) {
500
- return true;
501
- }
502
- if (/^#[0-9a-f]{8}$/i.test(hex)) {
503
- return true;
504
- }
505
- if (/^#[0-9a-f]{3}$/i.test(hex)) {
506
- return true;
507
- }
508
- if (/^#[0-9a-f]{4}$/i.test(hex)) {
509
- return true;
510
- }
511
- return false;
512
- }
513
- function escapeRegExpCharacters(value) {
514
- return value.replace(/[\-\\\{\}\*\+\?\|\^\$\.\,\[\]\(\)\#\s]/g, "\\$&");
515
- }
516
- var CachedFn = class {
517
- constructor(fn) {
518
- this.fn = fn;
519
- this.cache = /* @__PURE__ */ new Map();
520
- }
521
- get(key) {
522
- if (this.cache.has(key)) {
523
- return this.cache.get(key);
524
- }
525
- const value = this.fn(key);
526
- this.cache.set(key, value);
527
- return value;
528
- }
529
- };
530
-
531
- // src/theme.ts
532
- var Theme = class {
533
- constructor(_colorMap, _defaults, _root) {
534
- this._colorMap = _colorMap;
535
- this._defaults = _defaults;
536
- this._root = _root;
537
- this._cachedMatchRoot = new CachedFn(
538
- (scopeName) => this._root.match(scopeName)
539
- );
540
- }
541
- static createFromRawTheme(source, colorMap) {
542
- return this.createFromParsedTheme(parseTheme(source), colorMap);
543
- }
544
- static createFromParsedTheme(source, colorMap) {
545
- return resolveParsedThemeRules(source, colorMap);
546
- }
547
- getColorMap() {
548
- return this._colorMap.getColorMap();
549
- }
550
- getDefaults() {
551
- return this._defaults;
552
- }
553
- match(scopePath) {
554
- if (scopePath === null) {
555
- return this._defaults;
556
- }
557
- const scopeName = scopePath.scopeName;
558
- const matchingTrieElements = this._cachedMatchRoot.get(scopeName);
559
- const effectiveRule = matchingTrieElements.find(
560
- (v) => _scopePathMatchesParentScopes(scopePath.parent, v.parentScopes)
561
- );
562
- if (!effectiveRule) {
563
- return null;
564
- }
565
- return new StyleAttributes(
566
- effectiveRule.fontStyle,
567
- effectiveRule.foreground,
568
- effectiveRule.background
569
- );
570
- }
571
- };
572
- var ScopeStack = class _ScopeStack {
573
- constructor(parent, scopeName) {
574
- this.parent = parent;
575
- this.scopeName = scopeName;
576
- }
577
- static push(path, scopeNames) {
578
- for (const name of scopeNames) {
579
- path = new _ScopeStack(path, name);
580
- }
581
- return path;
582
- }
583
- static from(...segments) {
584
- let result = null;
585
- for (let i = 0; i < segments.length; i++) {
586
- result = new _ScopeStack(result, segments[i]);
587
- }
588
- return result;
589
- }
590
- push(scopeName) {
591
- return new _ScopeStack(this, scopeName);
592
- }
593
- getSegments() {
594
- let item = this;
595
- const result = [];
596
- while (item) {
597
- result.push(item.scopeName);
598
- item = item.parent;
599
- }
600
- result.reverse();
601
- return result;
602
- }
603
- toString() {
604
- return this.getSegments().join(" ");
605
- }
606
- extends(other) {
607
- if (this === other) {
608
- return true;
609
- }
610
- if (this.parent === null) {
611
- return false;
612
- }
613
- return this.parent.extends(other);
614
- }
615
- getExtensionIfDefined(base) {
616
- const result = [];
617
- let item = this;
618
- while (item && item !== base) {
619
- result.push(item.scopeName);
620
- item = item.parent;
621
- }
622
- return item === base ? result.reverse() : void 0;
623
- }
624
- };
625
- function _scopePathMatchesParentScopes(scopePath, parentScopes) {
626
- if (parentScopes.length === 0) {
627
- return true;
628
- }
629
- for (let index = 0; index < parentScopes.length; index++) {
630
- let scopePattern = parentScopes[index];
631
- let scopeMustMatch = false;
632
- if (scopePattern === ">") {
633
- if (index === parentScopes.length - 1) {
634
- return false;
635
- }
636
- scopePattern = parentScopes[++index];
637
- scopeMustMatch = true;
638
- }
639
- while (scopePath) {
640
- if (_matchesScope(scopePath.scopeName, scopePattern)) {
641
- break;
642
- }
643
- if (scopeMustMatch) {
644
- return false;
645
- }
646
- scopePath = scopePath.parent;
647
- }
648
- if (!scopePath) {
649
- return false;
650
- }
651
- scopePath = scopePath.parent;
652
- }
653
- return true;
654
- }
655
- function _matchesScope(scopeName, scopePattern) {
656
- return scopePattern === scopeName || scopeName.startsWith(scopePattern) && scopeName[scopePattern.length] === ".";
657
- }
658
- var StyleAttributes = class {
659
- constructor(fontStyle, foregroundId, backgroundId) {
660
- this.fontStyle = fontStyle;
661
- this.foregroundId = foregroundId;
662
- this.backgroundId = backgroundId;
663
- }
664
- };
665
- function parseTheme(source) {
666
- if (!source) {
667
- return [];
668
- }
669
- if (!source.settings || !Array.isArray(source.settings)) {
670
- return [];
671
- }
672
- let settings = source.settings;
673
- let result = [], resultLen = 0;
674
- for (let i = 0, len = settings.length; i < len; i++) {
675
- let entry = settings[i];
676
- if (!entry.settings) {
677
- continue;
678
- }
679
- let scopes;
680
- if (typeof entry.scope === "string") {
681
- let _scope = entry.scope;
682
- _scope = _scope.replace(/^[,]+/, "");
683
- _scope = _scope.replace(/[,]+$/, "");
684
- scopes = _scope.split(",");
685
- } else if (Array.isArray(entry.scope)) {
686
- scopes = entry.scope;
687
- } else {
688
- scopes = [""];
689
- }
690
- let fontStyle = -1 /* NotSet */;
691
- if (typeof entry.settings.fontStyle === "string") {
692
- fontStyle = 0 /* None */;
693
- let segments = entry.settings.fontStyle.split(" ");
694
- for (let j = 0, lenJ = segments.length; j < lenJ; j++) {
695
- let segment = segments[j];
696
- switch (segment) {
697
- case "italic":
698
- fontStyle = fontStyle | 1 /* Italic */;
699
- break;
700
- case "bold":
701
- fontStyle = fontStyle | 2 /* Bold */;
702
- break;
703
- case "underline":
704
- fontStyle = fontStyle | 4 /* Underline */;
705
- break;
706
- case "strikethrough":
707
- fontStyle = fontStyle | 8 /* Strikethrough */;
708
- break;
709
- }
710
- }
711
- }
712
- let foreground = null;
713
- if (typeof entry.settings.foreground === "string" && isValidHexColor(entry.settings.foreground)) {
714
- foreground = entry.settings.foreground;
715
- }
716
- let background = null;
717
- if (typeof entry.settings.background === "string" && isValidHexColor(entry.settings.background)) {
718
- background = entry.settings.background;
719
- }
720
- for (let j = 0, lenJ = scopes.length; j < lenJ; j++) {
721
- let _scope = scopes[j].trim();
722
- let segments = _scope.split(" ");
723
- let scope = segments[segments.length - 1];
724
- let parentScopes = null;
725
- if (segments.length > 1) {
726
- parentScopes = segments.slice(0, segments.length - 1);
727
- parentScopes.reverse();
728
- }
729
- result[resultLen++] = new ParsedThemeRule(
730
- scope,
731
- parentScopes,
732
- i,
733
- fontStyle,
734
- foreground,
735
- background
736
- );
737
- }
738
- }
739
- return result;
740
- }
741
- var ParsedThemeRule = class {
742
- constructor(scope, parentScopes, index, fontStyle, foreground, background) {
743
- this.scope = scope;
744
- this.parentScopes = parentScopes;
745
- this.index = index;
746
- this.fontStyle = fontStyle;
747
- this.foreground = foreground;
748
- this.background = background;
749
- }
750
- };
751
- function resolveParsedThemeRules(parsedThemeRules, _colorMap) {
752
- parsedThemeRules.sort((a, b) => {
753
- let r = strcmp(a.scope, b.scope);
754
- if (r !== 0) {
755
- return r;
756
- }
757
- r = strArrCmp(a.parentScopes, b.parentScopes);
758
- if (r !== 0) {
759
- return r;
760
- }
761
- return a.index - b.index;
762
- });
763
- let defaultFontStyle = 0 /* None */;
764
- let defaultForeground = "#000000";
765
- let defaultBackground = "#ffffff";
766
- while (parsedThemeRules.length >= 1 && parsedThemeRules[0].scope === "") {
767
- let incomingDefaults = parsedThemeRules.shift();
768
- if (incomingDefaults.fontStyle !== -1 /* NotSet */) {
769
- defaultFontStyle = incomingDefaults.fontStyle;
770
- }
771
- if (incomingDefaults.foreground !== null) {
772
- defaultForeground = incomingDefaults.foreground;
773
- }
774
- if (incomingDefaults.background !== null) {
775
- defaultBackground = incomingDefaults.background;
776
- }
777
- }
778
- let colorMap = new ColorMap(_colorMap);
779
- let defaults = new StyleAttributes(defaultFontStyle, colorMap.getId(defaultForeground), colorMap.getId(defaultBackground));
780
- let root = new ThemeTrieElement(new ThemeTrieElementRule(0, null, -1 /* NotSet */, 0, 0), []);
781
- for (let i = 0, len = parsedThemeRules.length; i < len; i++) {
782
- let rule = parsedThemeRules[i];
783
- root.insert(0, rule.scope, rule.parentScopes, rule.fontStyle, colorMap.getId(rule.foreground), colorMap.getId(rule.background));
784
- }
785
- return new Theme(colorMap, defaults, root);
786
- }
787
- var ColorMap = class {
788
- constructor(_colorMap) {
789
- this._lastColorId = 0;
790
- this._id2color = [];
791
- this._color2id = /* @__PURE__ */ Object.create(null);
792
- if (Array.isArray(_colorMap)) {
793
- this._isFrozen = true;
794
- for (let i = 0, len = _colorMap.length; i < len; i++) {
795
- this._color2id[_colorMap[i]] = i;
796
- this._id2color[i] = _colorMap[i];
797
- }
798
- } else {
799
- this._isFrozen = false;
800
- }
801
- }
802
- getId(color) {
803
- if (color === null) {
804
- return 0;
805
- }
806
- color = color.toUpperCase();
807
- let value = this._color2id[color];
808
- if (value) {
809
- return value;
810
- }
811
- if (this._isFrozen) {
812
- throw new Error(`Missing color in color map - ${color}`);
813
- }
814
- value = ++this._lastColorId;
815
- this._color2id[color] = value;
816
- this._id2color[value] = color;
817
- return value;
818
- }
819
- getColorMap() {
820
- return this._id2color.slice(0);
821
- }
822
- };
823
- var emptyParentScopes = Object.freeze([]);
824
- var ThemeTrieElementRule = class _ThemeTrieElementRule {
825
- constructor(scopeDepth, parentScopes, fontStyle, foreground, background) {
826
- this.scopeDepth = scopeDepth;
827
- this.parentScopes = parentScopes || emptyParentScopes;
828
- this.fontStyle = fontStyle;
829
- this.foreground = foreground;
830
- this.background = background;
831
- }
832
- clone() {
833
- return new _ThemeTrieElementRule(this.scopeDepth, this.parentScopes, this.fontStyle, this.foreground, this.background);
834
- }
835
- static cloneArr(arr) {
836
- let r = [];
837
- for (let i = 0, len = arr.length; i < len; i++) {
838
- r[i] = arr[i].clone();
839
- }
840
- return r;
841
- }
842
- acceptOverwrite(scopeDepth, fontStyle, foreground, background) {
843
- if (this.scopeDepth > scopeDepth) {
844
- console.log("how did this happen?");
845
- } else {
846
- this.scopeDepth = scopeDepth;
847
- }
848
- if (fontStyle !== -1 /* NotSet */) {
849
- this.fontStyle = fontStyle;
850
- }
851
- if (foreground !== 0) {
852
- this.foreground = foreground;
853
- }
854
- if (background !== 0) {
855
- this.background = background;
856
- }
857
- }
858
- };
859
- var ThemeTrieElement = class _ThemeTrieElement {
860
- constructor(_mainRule, rulesWithParentScopes = [], _children = {}) {
861
- this._mainRule = _mainRule;
862
- this._children = _children;
863
- this._rulesWithParentScopes = rulesWithParentScopes;
864
- }
865
- static _cmpBySpecificity(a, b) {
866
- if (a.scopeDepth !== b.scopeDepth) {
867
- return b.scopeDepth - a.scopeDepth;
868
- }
869
- let aParentIndex = 0;
870
- let bParentIndex = 0;
871
- while (true) {
872
- if (a.parentScopes[aParentIndex] === ">") {
873
- aParentIndex++;
874
- }
875
- if (b.parentScopes[bParentIndex] === ">") {
876
- bParentIndex++;
877
- }
878
- if (aParentIndex >= a.parentScopes.length || bParentIndex >= b.parentScopes.length) {
879
- break;
880
- }
881
- const parentScopeLengthDiff = b.parentScopes[bParentIndex].length - a.parentScopes[aParentIndex].length;
882
- if (parentScopeLengthDiff !== 0) {
883
- return parentScopeLengthDiff;
884
- }
885
- aParentIndex++;
886
- bParentIndex++;
887
- }
888
- return b.parentScopes.length - a.parentScopes.length;
889
- }
890
- match(scope) {
891
- if (scope !== "") {
892
- let dotIndex = scope.indexOf(".");
893
- let head;
894
- let tail;
895
- if (dotIndex === -1) {
896
- head = scope;
897
- tail = "";
898
- } else {
899
- head = scope.substring(0, dotIndex);
900
- tail = scope.substring(dotIndex + 1);
901
- }
902
- if (this._children.hasOwnProperty(head)) {
903
- return this._children[head].match(tail);
904
- }
905
- }
906
- const rules = this._rulesWithParentScopes.concat(this._mainRule);
907
- rules.sort(_ThemeTrieElement._cmpBySpecificity);
908
- return rules;
909
- }
910
- insert(scopeDepth, scope, parentScopes, fontStyle, foreground, background) {
911
- if (scope === "") {
912
- this._doInsertHere(scopeDepth, parentScopes, fontStyle, foreground, background);
913
- return;
914
- }
915
- let dotIndex = scope.indexOf(".");
916
- let head;
917
- let tail;
918
- if (dotIndex === -1) {
919
- head = scope;
920
- tail = "";
921
- } else {
922
- head = scope.substring(0, dotIndex);
923
- tail = scope.substring(dotIndex + 1);
924
- }
925
- let child;
926
- if (this._children.hasOwnProperty(head)) {
927
- child = this._children[head];
928
- } else {
929
- child = new _ThemeTrieElement(this._mainRule.clone(), ThemeTrieElementRule.cloneArr(this._rulesWithParentScopes));
930
- this._children[head] = child;
931
- }
932
- child.insert(scopeDepth + 1, tail, parentScopes, fontStyle, foreground, background);
933
- }
934
- _doInsertHere(scopeDepth, parentScopes, fontStyle, foreground, background) {
935
- if (parentScopes === null) {
936
- this._mainRule.acceptOverwrite(scopeDepth, fontStyle, foreground, background);
937
- return;
938
- }
939
- for (let i = 0, len = this._rulesWithParentScopes.length; i < len; i++) {
940
- let rule = this._rulesWithParentScopes[i];
941
- if (strArrCmp(rule.parentScopes, parentScopes) === 0) {
942
- rule.acceptOverwrite(scopeDepth, fontStyle, foreground, background);
943
- return;
944
- }
945
- }
946
- if (fontStyle === -1 /* NotSet */) {
947
- fontStyle = this._mainRule.fontStyle;
948
- }
949
- if (foreground === 0) {
950
- foreground = this._mainRule.foreground;
951
- }
952
- if (background === 0) {
953
- background = this._mainRule.background;
954
- }
955
- this._rulesWithParentScopes.push(new ThemeTrieElementRule(scopeDepth, parentScopes, fontStyle, foreground, background));
956
- }
957
- };
958
-
959
- // src/encodedTokenAttributes.ts
960
- var EncodedTokenAttributes;
961
- ((EncodedTokenAttributes2) => {
962
- function toBinaryStr(encodedTokenAttributes) {
963
- return encodedTokenAttributes.toString(2).padStart(32, "0");
964
- }
965
- EncodedTokenAttributes2.toBinaryStr = toBinaryStr;
966
- function print(encodedTokenAttributes) {
967
- const languageId = EncodedTokenAttributes2.getLanguageId(encodedTokenAttributes);
968
- const tokenType = EncodedTokenAttributes2.getTokenType(encodedTokenAttributes);
969
- const fontStyle = EncodedTokenAttributes2.getFontStyle(encodedTokenAttributes);
970
- const foreground = EncodedTokenAttributes2.getForeground(encodedTokenAttributes);
971
- const background = EncodedTokenAttributes2.getBackground(encodedTokenAttributes);
972
- console.log({
973
- languageId,
974
- tokenType,
975
- fontStyle,
976
- foreground,
977
- background
978
- });
979
- }
980
- EncodedTokenAttributes2.print = print;
981
- function getLanguageId(encodedTokenAttributes) {
982
- return (encodedTokenAttributes & 255 /* LANGUAGEID_MASK */) >>> 0 /* LANGUAGEID_OFFSET */;
983
- }
984
- EncodedTokenAttributes2.getLanguageId = getLanguageId;
985
- function getTokenType(encodedTokenAttributes) {
986
- return (encodedTokenAttributes & 768 /* TOKEN_TYPE_MASK */) >>> 8 /* TOKEN_TYPE_OFFSET */;
987
- }
988
- EncodedTokenAttributes2.getTokenType = getTokenType;
989
- function containsBalancedBrackets(encodedTokenAttributes) {
990
- return (encodedTokenAttributes & 1024 /* BALANCED_BRACKETS_MASK */) !== 0;
991
- }
992
- EncodedTokenAttributes2.containsBalancedBrackets = containsBalancedBrackets;
993
- function getFontStyle(encodedTokenAttributes) {
994
- return (encodedTokenAttributes & 30720 /* FONT_STYLE_MASK */) >>> 11 /* FONT_STYLE_OFFSET */;
995
- }
996
- EncodedTokenAttributes2.getFontStyle = getFontStyle;
997
- function getForeground(encodedTokenAttributes) {
998
- return (encodedTokenAttributes & 16744448 /* FOREGROUND_MASK */) >>> 15 /* FOREGROUND_OFFSET */;
999
- }
1000
- EncodedTokenAttributes2.getForeground = getForeground;
1001
- function getBackground(encodedTokenAttributes) {
1002
- return (encodedTokenAttributes & 4278190080 /* BACKGROUND_MASK */) >>> 24 /* BACKGROUND_OFFSET */;
1003
- }
1004
- EncodedTokenAttributes2.getBackground = getBackground;
1005
- function set(encodedTokenAttributes, languageId, tokenType, containsBalancedBrackets2, fontStyle, foreground, background) {
1006
- let _languageId = EncodedTokenAttributes2.getLanguageId(encodedTokenAttributes);
1007
- let _tokenType = EncodedTokenAttributes2.getTokenType(encodedTokenAttributes);
1008
- let _containsBalancedBracketsBit = EncodedTokenAttributes2.containsBalancedBrackets(encodedTokenAttributes) ? 1 : 0;
1009
- let _fontStyle = EncodedTokenAttributes2.getFontStyle(encodedTokenAttributes);
1010
- let _foreground = EncodedTokenAttributes2.getForeground(encodedTokenAttributes);
1011
- let _background = EncodedTokenAttributes2.getBackground(encodedTokenAttributes);
1012
- if (languageId !== 0) {
1013
- _languageId = languageId;
1014
- }
1015
- if (tokenType !== 8 /* NotSet */) {
1016
- _tokenType = fromOptionalTokenType(tokenType);
1017
- }
1018
- if (containsBalancedBrackets2 !== null) {
1019
- _containsBalancedBracketsBit = containsBalancedBrackets2 ? 1 : 0;
1020
- }
1021
- if (fontStyle !== -1 /* NotSet */) {
1022
- _fontStyle = fontStyle;
1023
- }
1024
- if (foreground !== 0) {
1025
- _foreground = foreground;
1026
- }
1027
- if (background !== 0) {
1028
- _background = background;
1029
- }
1030
- return (_languageId << 0 /* LANGUAGEID_OFFSET */ | _tokenType << 8 /* TOKEN_TYPE_OFFSET */ | _containsBalancedBracketsBit << 10 /* BALANCED_BRACKETS_OFFSET */ | _fontStyle << 11 /* FONT_STYLE_OFFSET */ | _foreground << 15 /* FOREGROUND_OFFSET */ | _background << 24 /* BACKGROUND_OFFSET */) >>> 0;
1031
- }
1032
- EncodedTokenAttributes2.set = set;
1033
- })(EncodedTokenAttributes || (EncodedTokenAttributes = {}));
1034
- function toOptionalTokenType(standardType) {
1035
- return standardType;
1036
- }
1037
- function fromOptionalTokenType(standardType) {
1038
- return standardType;
1039
- }
1040
-
1041
- // src/matcher.ts
1042
- function createMatchers(selector, matchesName) {
1043
- const results = [];
1044
- const tokenizer = newTokenizer(selector);
1045
- let token = tokenizer.next();
1046
- while (token !== null) {
1047
- let priority = 0;
1048
- if (token.length === 2 && token.charAt(1) === ":") {
1049
- switch (token.charAt(0)) {
1050
- case "R":
1051
- priority = 1;
1052
- break;
1053
- case "L":
1054
- priority = -1;
1055
- break;
1056
- default:
1057
- console.log(`Unknown priority ${token} in scope selector`);
1058
- }
1059
- token = tokenizer.next();
1060
- }
1061
- let matcher = parseConjunction();
1062
- results.push({ matcher, priority });
1063
- if (token !== ",") {
1064
- break;
1065
- }
1066
- token = tokenizer.next();
1067
- }
1068
- return results;
1069
- function parseOperand() {
1070
- if (token === "-") {
1071
- token = tokenizer.next();
1072
- const expressionToNegate = parseOperand();
1073
- return (matcherInput) => !!expressionToNegate && !expressionToNegate(matcherInput);
1074
- }
1075
- if (token === "(") {
1076
- token = tokenizer.next();
1077
- const expressionInParents = parseInnerExpression();
1078
- if (token === ")") {
1079
- token = tokenizer.next();
1080
- }
1081
- return expressionInParents;
1082
- }
1083
- if (isIdentifier(token)) {
1084
- const identifiers = [];
1085
- do {
1086
- identifiers.push(token);
1087
- token = tokenizer.next();
1088
- } while (isIdentifier(token));
1089
- return (matcherInput) => matchesName(identifiers, matcherInput);
1090
- }
1091
- return null;
1092
- }
1093
- function parseConjunction() {
1094
- const matchers = [];
1095
- let matcher = parseOperand();
1096
- while (matcher) {
1097
- matchers.push(matcher);
1098
- matcher = parseOperand();
1099
- }
1100
- return (matcherInput) => matchers.every((matcher2) => matcher2(matcherInput));
1101
- }
1102
- function parseInnerExpression() {
1103
- const matchers = [];
1104
- let matcher = parseConjunction();
1105
- while (matcher) {
1106
- matchers.push(matcher);
1107
- if (token === "|" || token === ",") {
1108
- do {
1109
- token = tokenizer.next();
1110
- } while (token === "|" || token === ",");
1111
- } else {
1112
- break;
1113
- }
1114
- matcher = parseConjunction();
1115
- }
1116
- return (matcherInput) => matchers.some((matcher2) => matcher2(matcherInput));
1117
- }
1118
- }
1119
- function isIdentifier(token) {
1120
- return !!token && !!token.match(/[\w\.:]+/);
1121
- }
1122
- function newTokenizer(input) {
1123
- let regex = /([LR]:|[\w\.:][\w\.:\-]*|[\,\|\-\(\)])/g;
1124
- let match = regex.exec(input);
1125
- return {
1126
- next: () => {
1127
- if (!match) {
1128
- return null;
1129
- }
1130
- const res = match[0];
1131
- match = regex.exec(input);
1132
- return res;
1133
- }
1134
- };
1135
- }
1136
- function disposeOnigString(str) {
1137
- if (typeof str.dispose === "function") {
1138
- str.dispose();
1139
- }
1140
- }
1141
-
1142
- // src/grammar/grammarDependencies.ts
1143
- var TopLevelRuleReference = class {
1144
- constructor(scopeName) {
1145
- this.scopeName = scopeName;
1146
- }
1147
- toKey() {
1148
- return this.scopeName;
1149
- }
1150
- };
1151
- var TopLevelRepositoryRuleReference = class {
1152
- constructor(scopeName, ruleName) {
1153
- this.scopeName = scopeName;
1154
- this.ruleName = ruleName;
1155
- }
1156
- toKey() {
1157
- return `${this.scopeName}#${this.ruleName}`;
1158
- }
1159
- };
1160
- var ExternalReferenceCollector = class {
1161
- constructor() {
1162
- this._references = [];
1163
- this._seenReferenceKeys = /* @__PURE__ */ new Set();
1164
- this.visitedRule = /* @__PURE__ */ new Set();
1165
- }
1166
- get references() {
1167
- return this._references;
1168
- }
1169
- add(reference) {
1170
- const key = reference.toKey();
1171
- if (this._seenReferenceKeys.has(key)) {
1172
- return;
1173
- }
1174
- this._seenReferenceKeys.add(key);
1175
- this._references.push(reference);
1176
- }
1177
- };
1178
- var ScopeDependencyProcessor = class {
1179
- constructor(repo, initialScopeName) {
1180
- this.repo = repo;
1181
- this.initialScopeName = initialScopeName;
1182
- this.seenFullScopeRequests = /* @__PURE__ */ new Set();
1183
- this.seenPartialScopeRequests = /* @__PURE__ */ new Set();
1184
- this.seenFullScopeRequests.add(this.initialScopeName);
1185
- this.Q = [new TopLevelRuleReference(this.initialScopeName)];
1186
- }
1187
- processQueue() {
1188
- const q = this.Q;
1189
- this.Q = [];
1190
- const deps = new ExternalReferenceCollector();
1191
- for (const dep of q) {
1192
- collectReferencesOfReference(dep, this.initialScopeName, this.repo, deps);
1193
- }
1194
- for (const dep of deps.references) {
1195
- if (dep instanceof TopLevelRuleReference) {
1196
- if (this.seenFullScopeRequests.has(dep.scopeName)) {
1197
- continue;
1198
- }
1199
- this.seenFullScopeRequests.add(dep.scopeName);
1200
- this.Q.push(dep);
1201
- } else {
1202
- if (this.seenFullScopeRequests.has(dep.scopeName)) {
1203
- continue;
1204
- }
1205
- if (this.seenPartialScopeRequests.has(dep.toKey())) {
1206
- continue;
1207
- }
1208
- this.seenPartialScopeRequests.add(dep.toKey());
1209
- this.Q.push(dep);
1210
- }
1211
- }
1212
- }
1213
- };
1214
- function collectReferencesOfReference(reference, baseGrammarScopeName, repo, result) {
1215
- const selfGrammar = repo.lookup(reference.scopeName);
1216
- if (!selfGrammar) {
1217
- if (reference.scopeName === baseGrammarScopeName) {
1218
- throw new Error(`No grammar provided for <${baseGrammarScopeName}>`);
1219
- }
1220
- return;
1221
- }
1222
- const baseGrammar = repo.lookup(baseGrammarScopeName);
1223
- if (reference instanceof TopLevelRuleReference) {
1224
- collectExternalReferencesInTopLevelRule({ baseGrammar, selfGrammar }, result);
1225
- } else {
1226
- collectExternalReferencesInTopLevelRepositoryRule(
1227
- reference.ruleName,
1228
- { baseGrammar, selfGrammar, repository: selfGrammar.repository },
1229
- result
1230
- );
1231
- }
1232
- const injections = repo.injections(reference.scopeName);
1233
- if (injections) {
1234
- for (const injection of injections) {
1235
- result.add(new TopLevelRuleReference(injection));
1236
- }
1237
- }
1238
- }
1239
- function collectExternalReferencesInTopLevelRepositoryRule(ruleName, context, result) {
1240
- if (context.repository && context.repository[ruleName]) {
1241
- const rule = context.repository[ruleName];
1242
- collectExternalReferencesInRules([rule], context, result);
1243
- }
1244
- }
1245
- function collectExternalReferencesInTopLevelRule(context, result) {
1246
- if (context.selfGrammar.patterns && Array.isArray(context.selfGrammar.patterns)) {
1247
- collectExternalReferencesInRules(
1248
- context.selfGrammar.patterns,
1249
- { ...context, repository: context.selfGrammar.repository },
1250
- result
1251
- );
1252
- }
1253
- if (context.selfGrammar.injections) {
1254
- collectExternalReferencesInRules(
1255
- Object.values(context.selfGrammar.injections),
1256
- { ...context, repository: context.selfGrammar.repository },
1257
- result
1258
- );
1259
- }
1260
- }
1261
- function collectExternalReferencesInRules(rules, context, result) {
1262
- for (const rule of rules) {
1263
- if (result.visitedRule.has(rule)) {
1264
- continue;
1265
- }
1266
- result.visitedRule.add(rule);
1267
- const patternRepository = rule.repository ? mergeObjects({}, context.repository, rule.repository) : context.repository;
1268
- if (Array.isArray(rule.patterns)) {
1269
- collectExternalReferencesInRules(rule.patterns, { ...context, repository: patternRepository }, result);
1270
- }
1271
- const include = rule.include;
1272
- if (!include) {
1273
- continue;
1274
- }
1275
- const reference = parseInclude(include);
1276
- switch (reference.kind) {
1277
- case 0 /* Base */:
1278
- collectExternalReferencesInTopLevelRule({ ...context, selfGrammar: context.baseGrammar }, result);
1279
- break;
1280
- case 1 /* Self */:
1281
- collectExternalReferencesInTopLevelRule(context, result);
1282
- break;
1283
- case 2 /* RelativeReference */:
1284
- collectExternalReferencesInTopLevelRepositoryRule(reference.ruleName, { ...context, repository: patternRepository }, result);
1285
- break;
1286
- case 3 /* TopLevelReference */:
1287
- case 4 /* TopLevelRepositoryReference */:
1288
- const selfGrammar = reference.scopeName === context.selfGrammar.scopeName ? context.selfGrammar : reference.scopeName === context.baseGrammar.scopeName ? context.baseGrammar : void 0;
1289
- if (selfGrammar) {
1290
- const newContext = { baseGrammar: context.baseGrammar, selfGrammar, repository: patternRepository };
1291
- if (reference.kind === 4 /* TopLevelRepositoryReference */) {
1292
- collectExternalReferencesInTopLevelRepositoryRule(reference.ruleName, newContext, result);
1293
- } else {
1294
- collectExternalReferencesInTopLevelRule(newContext, result);
1295
- }
1296
- } else {
1297
- if (reference.kind === 4 /* TopLevelRepositoryReference */) {
1298
- result.add(new TopLevelRepositoryRuleReference(reference.scopeName, reference.ruleName));
1299
- } else {
1300
- result.add(new TopLevelRuleReference(reference.scopeName));
1301
- }
1302
- }
1303
- break;
1304
- }
1305
- }
1306
- }
1307
- var BaseReference = class {
1308
- constructor() {
1309
- this.kind = 0 /* Base */;
1310
- }
1311
- };
1312
- var SelfReference = class {
1313
- constructor() {
1314
- this.kind = 1 /* Self */;
1315
- }
1316
- };
1317
- var RelativeReference = class {
1318
- constructor(ruleName) {
1319
- this.ruleName = ruleName;
1320
- this.kind = 2 /* RelativeReference */;
1321
- }
1322
- };
1323
- var TopLevelReference = class {
1324
- constructor(scopeName) {
1325
- this.scopeName = scopeName;
1326
- this.kind = 3 /* TopLevelReference */;
1327
- }
1328
- };
1329
- var TopLevelRepositoryReference = class {
1330
- constructor(scopeName, ruleName) {
1331
- this.scopeName = scopeName;
1332
- this.ruleName = ruleName;
1333
- this.kind = 4 /* TopLevelRepositoryReference */;
1334
- }
1335
- };
1336
- function parseInclude(include) {
1337
- if (include === "$base") {
1338
- return new BaseReference();
1339
- } else if (include === "$self") {
1340
- return new SelfReference();
1341
- }
1342
- const indexOfSharp = include.indexOf("#");
1343
- if (indexOfSharp === -1) {
1344
- return new TopLevelReference(include);
1345
- } else if (indexOfSharp === 0) {
1346
- return new RelativeReference(include.substring(1));
1347
- } else {
1348
- const scopeName = include.substring(0, indexOfSharp);
1349
- const ruleName = include.substring(indexOfSharp + 1);
1350
- return new TopLevelRepositoryReference(scopeName, ruleName);
1351
- }
1352
- }
1353
-
1354
- // src/rule.ts
1355
- var HAS_BACK_REFERENCES = /\\(\d+)/;
1356
- var BACK_REFERENCING_END = /\\(\d+)/g;
1357
- var endRuleId = -1;
1358
- var whileRuleId = -2;
1359
- function ruleIdFromNumber(id) {
1360
- return id;
1361
- }
1362
- function ruleIdToNumber(id) {
1363
- return id;
1364
- }
1365
- var Rule = class {
1366
- constructor($location, id, name, contentName) {
1367
- this.$location = $location;
1368
- this.id = id;
1369
- this._name = name || null;
1370
- this._nameIsCapturing = RegexSource.hasCaptures(this._name);
1371
- this._contentName = contentName || null;
1372
- this._contentNameIsCapturing = RegexSource.hasCaptures(this._contentName);
1373
- }
1374
- get debugName() {
1375
- const location = this.$location ? `${basename(this.$location.filename)}:${this.$location.line}` : "unknown";
1376
- return `${this.constructor.name}#${this.id} @ ${location}`;
1377
- }
1378
- getName(lineText, captureIndices) {
1379
- if (!this._nameIsCapturing || this._name === null || lineText === null || captureIndices === null) {
1380
- return this._name;
1381
- }
1382
- return RegexSource.replaceCaptures(this._name, lineText, captureIndices);
1383
- }
1384
- getContentName(lineText, captureIndices) {
1385
- if (!this._contentNameIsCapturing || this._contentName === null) {
1386
- return this._contentName;
1387
- }
1388
- return RegexSource.replaceCaptures(this._contentName, lineText, captureIndices);
1389
- }
1390
- };
1391
- var CaptureRule = class extends Rule {
1392
- constructor($location, id, name, contentName, retokenizeCapturedWithRuleId) {
1393
- super($location, id, name, contentName);
1394
- this.retokenizeCapturedWithRuleId = retokenizeCapturedWithRuleId;
1395
- }
1396
- dispose() {
1397
- }
1398
- collectPatterns(grammar, out) {
1399
- throw new Error("Not supported!");
1400
- }
1401
- compile(grammar, endRegexSource) {
1402
- throw new Error("Not supported!");
1403
- }
1404
- compileAG(grammar, endRegexSource, allowA, allowG) {
1405
- throw new Error("Not supported!");
1406
- }
1407
- };
1408
- var MatchRule = class extends Rule {
1409
- constructor($location, id, name, match, captures) {
1410
- super($location, id, name, null);
1411
- this._match = new RegExpSource(match, this.id);
1412
- this.captures = captures;
1413
- this._cachedCompiledPatterns = null;
1414
- }
1415
- dispose() {
1416
- if (this._cachedCompiledPatterns) {
1417
- this._cachedCompiledPatterns.dispose();
1418
- this._cachedCompiledPatterns = null;
1419
- }
1420
- }
1421
- get debugMatchRegExp() {
1422
- return `${this._match.source}`;
1423
- }
1424
- collectPatterns(grammar, out) {
1425
- out.push(this._match);
1426
- }
1427
- compile(grammar, endRegexSource) {
1428
- return this._getCachedCompiledPatterns(grammar).compile(grammar);
1429
- }
1430
- compileAG(grammar, endRegexSource, allowA, allowG) {
1431
- return this._getCachedCompiledPatterns(grammar).compileAG(grammar, allowA, allowG);
1432
- }
1433
- _getCachedCompiledPatterns(grammar) {
1434
- if (!this._cachedCompiledPatterns) {
1435
- this._cachedCompiledPatterns = new RegExpSourceList();
1436
- this.collectPatterns(grammar, this._cachedCompiledPatterns);
1437
- }
1438
- return this._cachedCompiledPatterns;
1439
- }
1440
- };
1441
- var IncludeOnlyRule = class extends Rule {
1442
- constructor($location, id, name, contentName, patterns) {
1443
- super($location, id, name, contentName);
1444
- this.patterns = patterns.patterns;
1445
- this.hasMissingPatterns = patterns.hasMissingPatterns;
1446
- this._cachedCompiledPatterns = null;
1447
- }
1448
- dispose() {
1449
- if (this._cachedCompiledPatterns) {
1450
- this._cachedCompiledPatterns.dispose();
1451
- this._cachedCompiledPatterns = null;
1452
- }
1453
- }
1454
- collectPatterns(grammar, out) {
1455
- for (const pattern of this.patterns) {
1456
- const rule = grammar.getRule(pattern);
1457
- rule.collectPatterns(grammar, out);
1458
- }
1459
- }
1460
- compile(grammar, endRegexSource) {
1461
- return this._getCachedCompiledPatterns(grammar).compile(grammar);
1462
- }
1463
- compileAG(grammar, endRegexSource, allowA, allowG) {
1464
- return this._getCachedCompiledPatterns(grammar).compileAG(grammar, allowA, allowG);
1465
- }
1466
- _getCachedCompiledPatterns(grammar) {
1467
- if (!this._cachedCompiledPatterns) {
1468
- this._cachedCompiledPatterns = new RegExpSourceList();
1469
- this.collectPatterns(grammar, this._cachedCompiledPatterns);
1470
- }
1471
- return this._cachedCompiledPatterns;
1472
- }
1473
- };
1474
- var BeginEndRule = class extends Rule {
1475
- constructor($location, id, name, contentName, begin, beginCaptures, end, endCaptures, applyEndPatternLast, patterns) {
1476
- super($location, id, name, contentName);
1477
- this._begin = new RegExpSource(begin, this.id);
1478
- this.beginCaptures = beginCaptures;
1479
- this._end = new RegExpSource(end ? end : "\uFFFF", -1);
1480
- this.endHasBackReferences = this._end.hasBackReferences;
1481
- this.endCaptures = endCaptures;
1482
- this.applyEndPatternLast = applyEndPatternLast || false;
1483
- this.patterns = patterns.patterns;
1484
- this.hasMissingPatterns = patterns.hasMissingPatterns;
1485
- this._cachedCompiledPatterns = null;
1486
- }
1487
- dispose() {
1488
- if (this._cachedCompiledPatterns) {
1489
- this._cachedCompiledPatterns.dispose();
1490
- this._cachedCompiledPatterns = null;
1491
- }
1492
- }
1493
- get debugBeginRegExp() {
1494
- return `${this._begin.source}`;
1495
- }
1496
- get debugEndRegExp() {
1497
- return `${this._end.source}`;
1498
- }
1499
- getEndWithResolvedBackReferences(lineText, captureIndices) {
1500
- return this._end.resolveBackReferences(lineText, captureIndices);
1501
- }
1502
- collectPatterns(grammar, out) {
1503
- out.push(this._begin);
1504
- }
1505
- compile(grammar, endRegexSource) {
1506
- return this._getCachedCompiledPatterns(grammar, endRegexSource).compile(grammar);
1507
- }
1508
- compileAG(grammar, endRegexSource, allowA, allowG) {
1509
- return this._getCachedCompiledPatterns(grammar, endRegexSource).compileAG(grammar, allowA, allowG);
1510
- }
1511
- _getCachedCompiledPatterns(grammar, endRegexSource) {
1512
- if (!this._cachedCompiledPatterns) {
1513
- this._cachedCompiledPatterns = new RegExpSourceList();
1514
- for (const pattern of this.patterns) {
1515
- const rule = grammar.getRule(pattern);
1516
- rule.collectPatterns(grammar, this._cachedCompiledPatterns);
1517
- }
1518
- if (this.applyEndPatternLast) {
1519
- this._cachedCompiledPatterns.push(this._end.hasBackReferences ? this._end.clone() : this._end);
1520
- } else {
1521
- this._cachedCompiledPatterns.unshift(this._end.hasBackReferences ? this._end.clone() : this._end);
1522
- }
1523
- }
1524
- if (this._end.hasBackReferences) {
1525
- if (this.applyEndPatternLast) {
1526
- this._cachedCompiledPatterns.setSource(this._cachedCompiledPatterns.length() - 1, endRegexSource);
1527
- } else {
1528
- this._cachedCompiledPatterns.setSource(0, endRegexSource);
1529
- }
1530
- }
1531
- return this._cachedCompiledPatterns;
1532
- }
1533
- };
1534
- var BeginWhileRule = class extends Rule {
1535
- constructor($location, id, name, contentName, begin, beginCaptures, _while, whileCaptures, patterns) {
1536
- super($location, id, name, contentName);
1537
- this._begin = new RegExpSource(begin, this.id);
1538
- this.beginCaptures = beginCaptures;
1539
- this.whileCaptures = whileCaptures;
1540
- this._while = new RegExpSource(_while, whileRuleId);
1541
- this.whileHasBackReferences = this._while.hasBackReferences;
1542
- this.patterns = patterns.patterns;
1543
- this.hasMissingPatterns = patterns.hasMissingPatterns;
1544
- this._cachedCompiledPatterns = null;
1545
- this._cachedCompiledWhilePatterns = null;
1546
- }
1547
- dispose() {
1548
- if (this._cachedCompiledPatterns) {
1549
- this._cachedCompiledPatterns.dispose();
1550
- this._cachedCompiledPatterns = null;
1551
- }
1552
- if (this._cachedCompiledWhilePatterns) {
1553
- this._cachedCompiledWhilePatterns.dispose();
1554
- this._cachedCompiledWhilePatterns = null;
1555
- }
1556
- }
1557
- get debugBeginRegExp() {
1558
- return `${this._begin.source}`;
1559
- }
1560
- get debugWhileRegExp() {
1561
- return `${this._while.source}`;
1562
- }
1563
- getWhileWithResolvedBackReferences(lineText, captureIndices) {
1564
- return this._while.resolveBackReferences(lineText, captureIndices);
1565
- }
1566
- collectPatterns(grammar, out) {
1567
- out.push(this._begin);
1568
- }
1569
- compile(grammar, endRegexSource) {
1570
- return this._getCachedCompiledPatterns(grammar).compile(grammar);
1571
- }
1572
- compileAG(grammar, endRegexSource, allowA, allowG) {
1573
- return this._getCachedCompiledPatterns(grammar).compileAG(grammar, allowA, allowG);
1574
- }
1575
- _getCachedCompiledPatterns(grammar) {
1576
- if (!this._cachedCompiledPatterns) {
1577
- this._cachedCompiledPatterns = new RegExpSourceList();
1578
- for (const pattern of this.patterns) {
1579
- const rule = grammar.getRule(pattern);
1580
- rule.collectPatterns(grammar, this._cachedCompiledPatterns);
1581
- }
1582
- }
1583
- return this._cachedCompiledPatterns;
1584
- }
1585
- compileWhile(grammar, endRegexSource) {
1586
- return this._getCachedCompiledWhilePatterns(grammar, endRegexSource).compile(grammar);
1587
- }
1588
- compileWhileAG(grammar, endRegexSource, allowA, allowG) {
1589
- return this._getCachedCompiledWhilePatterns(grammar, endRegexSource).compileAG(grammar, allowA, allowG);
1590
- }
1591
- _getCachedCompiledWhilePatterns(grammar, endRegexSource) {
1592
- if (!this._cachedCompiledWhilePatterns) {
1593
- this._cachedCompiledWhilePatterns = new RegExpSourceList();
1594
- this._cachedCompiledWhilePatterns.push(this._while.hasBackReferences ? this._while.clone() : this._while);
1595
- }
1596
- if (this._while.hasBackReferences) {
1597
- this._cachedCompiledWhilePatterns.setSource(0, endRegexSource ? endRegexSource : "\uFFFF");
1598
- }
1599
- return this._cachedCompiledWhilePatterns;
1600
- }
1601
- };
1602
- var RuleFactory = class _RuleFactory {
1603
- static createCaptureRule(helper, $location, name, contentName, retokenizeCapturedWithRuleId) {
1604
- return helper.registerRule((id) => {
1605
- return new CaptureRule($location, id, name, contentName, retokenizeCapturedWithRuleId);
1606
- });
1607
- }
1608
- static getCompiledRuleId(desc, helper, repository) {
1609
- if (!desc.id) {
1610
- helper.registerRule((id) => {
1611
- desc.id = id;
1612
- if (desc.match) {
1613
- return new MatchRule(
1614
- desc.$vscodeTextmateLocation,
1615
- desc.id,
1616
- desc.name,
1617
- desc.match,
1618
- _RuleFactory._compileCaptures(desc.captures, helper, repository)
1619
- );
1620
- }
1621
- if (typeof desc.begin === "undefined") {
1622
- if (desc.repository) {
1623
- repository = mergeObjects({}, repository, desc.repository);
1624
- }
1625
- let patterns = desc.patterns;
1626
- if (typeof patterns === "undefined" && desc.include) {
1627
- patterns = [{ include: desc.include }];
1628
- }
1629
- return new IncludeOnlyRule(
1630
- desc.$vscodeTextmateLocation,
1631
- desc.id,
1632
- desc.name,
1633
- desc.contentName,
1634
- _RuleFactory._compilePatterns(patterns, helper, repository)
1635
- );
1636
- }
1637
- if (desc.while) {
1638
- return new BeginWhileRule(
1639
- desc.$vscodeTextmateLocation,
1640
- desc.id,
1641
- desc.name,
1642
- desc.contentName,
1643
- desc.begin,
1644
- _RuleFactory._compileCaptures(desc.beginCaptures || desc.captures, helper, repository),
1645
- desc.while,
1646
- _RuleFactory._compileCaptures(desc.whileCaptures || desc.captures, helper, repository),
1647
- _RuleFactory._compilePatterns(desc.patterns, helper, repository)
1648
- );
1649
- }
1650
- return new BeginEndRule(
1651
- desc.$vscodeTextmateLocation,
1652
- desc.id,
1653
- desc.name,
1654
- desc.contentName,
1655
- desc.begin,
1656
- _RuleFactory._compileCaptures(desc.beginCaptures || desc.captures, helper, repository),
1657
- desc.end,
1658
- _RuleFactory._compileCaptures(desc.endCaptures || desc.captures, helper, repository),
1659
- desc.applyEndPatternLast,
1660
- _RuleFactory._compilePatterns(desc.patterns, helper, repository)
1661
- );
1662
- });
1663
- }
1664
- return desc.id;
1665
- }
1666
- static _compileCaptures(captures, helper, repository) {
1667
- let r = [];
1668
- if (captures) {
1669
- let maximumCaptureId = 0;
1670
- for (const captureId in captures) {
1671
- if (captureId === "$vscodeTextmateLocation") {
1672
- continue;
1673
- }
1674
- const numericCaptureId = parseInt(captureId, 10);
1675
- if (numericCaptureId > maximumCaptureId) {
1676
- maximumCaptureId = numericCaptureId;
1677
- }
1678
- }
1679
- for (let i = 0; i <= maximumCaptureId; i++) {
1680
- r[i] = null;
1681
- }
1682
- for (const captureId in captures) {
1683
- if (captureId === "$vscodeTextmateLocation") {
1684
- continue;
1685
- }
1686
- const numericCaptureId = parseInt(captureId, 10);
1687
- let retokenizeCapturedWithRuleId = 0;
1688
- if (captures[captureId].patterns) {
1689
- retokenizeCapturedWithRuleId = _RuleFactory.getCompiledRuleId(captures[captureId], helper, repository);
1690
- }
1691
- r[numericCaptureId] = _RuleFactory.createCaptureRule(helper, captures[captureId].$vscodeTextmateLocation, captures[captureId].name, captures[captureId].contentName, retokenizeCapturedWithRuleId);
1692
- }
1693
- }
1694
- return r;
1695
- }
1696
- static _compilePatterns(patterns, helper, repository) {
1697
- let r = [];
1698
- if (patterns) {
1699
- for (let i = 0, len = patterns.length; i < len; i++) {
1700
- const pattern = patterns[i];
1701
- let ruleId = -1;
1702
- if (pattern.include) {
1703
- const reference = parseInclude(pattern.include);
1704
- switch (reference.kind) {
1705
- case 0 /* Base */:
1706
- case 1 /* Self */:
1707
- ruleId = _RuleFactory.getCompiledRuleId(repository[pattern.include], helper, repository);
1708
- break;
1709
- case 2 /* RelativeReference */:
1710
- let localIncludedRule = repository[reference.ruleName];
1711
- if (localIncludedRule) {
1712
- ruleId = _RuleFactory.getCompiledRuleId(localIncludedRule, helper, repository);
1713
- }
1714
- break;
1715
- case 3 /* TopLevelReference */:
1716
- case 4 /* TopLevelRepositoryReference */:
1717
- const externalGrammarName = reference.scopeName;
1718
- const externalGrammarInclude = reference.kind === 4 /* TopLevelRepositoryReference */ ? reference.ruleName : null;
1719
- const externalGrammar = helper.getExternalGrammar(externalGrammarName, repository);
1720
- if (externalGrammar) {
1721
- if (externalGrammarInclude) {
1722
- let externalIncludedRule = externalGrammar.repository[externalGrammarInclude];
1723
- if (externalIncludedRule) {
1724
- ruleId = _RuleFactory.getCompiledRuleId(externalIncludedRule, helper, externalGrammar.repository);
1725
- }
1726
- } else {
1727
- ruleId = _RuleFactory.getCompiledRuleId(externalGrammar.repository.$self, helper, externalGrammar.repository);
1728
- }
1729
- }
1730
- break;
1731
- }
1732
- } else {
1733
- ruleId = _RuleFactory.getCompiledRuleId(pattern, helper, repository);
1734
- }
1735
- if (ruleId !== -1) {
1736
- const rule = helper.getRule(ruleId);
1737
- let skipRule = false;
1738
- if (rule instanceof IncludeOnlyRule || rule instanceof BeginEndRule || rule instanceof BeginWhileRule) {
1739
- if (rule.hasMissingPatterns && rule.patterns.length === 0) {
1740
- skipRule = true;
1741
- }
1742
- }
1743
- if (skipRule) {
1744
- continue;
1745
- }
1746
- r.push(ruleId);
1747
- }
1748
- }
1749
- }
1750
- return {
1751
- patterns: r,
1752
- hasMissingPatterns: (patterns ? patterns.length : 0) !== r.length
1753
- };
1754
- }
1755
- };
1756
- var RegExpSource = class _RegExpSource {
1757
- constructor(regExpSource, ruleId) {
1758
- if (regExpSource) {
1759
- const len = regExpSource.length;
1760
- let lastPushedPos = 0;
1761
- let output = [];
1762
- let hasAnchor = false;
1763
- for (let pos = 0; pos < len; pos++) {
1764
- const ch = regExpSource.charAt(pos);
1765
- if (ch === "\\") {
1766
- if (pos + 1 < len) {
1767
- const nextCh = regExpSource.charAt(pos + 1);
1768
- if (nextCh === "z") {
1769
- output.push(regExpSource.substring(lastPushedPos, pos));
1770
- output.push("$(?!\\n)(?<!\\n)");
1771
- lastPushedPos = pos + 2;
1772
- } else if (nextCh === "A" || nextCh === "G") {
1773
- hasAnchor = true;
1774
- }
1775
- pos++;
1776
- }
1777
- }
1778
- }
1779
- this.hasAnchor = hasAnchor;
1780
- if (lastPushedPos === 0) {
1781
- this.source = regExpSource;
1782
- } else {
1783
- output.push(regExpSource.substring(lastPushedPos, len));
1784
- this.source = output.join("");
1785
- }
1786
- } else {
1787
- this.hasAnchor = false;
1788
- this.source = regExpSource;
1789
- }
1790
- if (this.hasAnchor) {
1791
- this._anchorCache = this._buildAnchorCache();
1792
- } else {
1793
- this._anchorCache = null;
1794
- }
1795
- this.ruleId = ruleId;
1796
- this.hasBackReferences = HAS_BACK_REFERENCES.test(this.source);
1797
- }
1798
- clone() {
1799
- return new _RegExpSource(this.source, this.ruleId);
1800
- }
1801
- setSource(newSource) {
1802
- if (this.source === newSource) {
1803
- return;
1804
- }
1805
- this.source = newSource;
1806
- if (this.hasAnchor) {
1807
- this._anchorCache = this._buildAnchorCache();
1808
- }
1809
- }
1810
- resolveBackReferences(lineText, captureIndices) {
1811
- let capturedValues = captureIndices.map((capture) => {
1812
- return lineText.substring(capture.start, capture.end);
1813
- });
1814
- BACK_REFERENCING_END.lastIndex = 0;
1815
- return this.source.replace(BACK_REFERENCING_END, (match, g1) => {
1816
- return escapeRegExpCharacters(capturedValues[parseInt(g1, 10)] || "");
1817
- });
1818
- }
1819
- _buildAnchorCache() {
1820
- let A0_G0_result = [];
1821
- let A0_G1_result = [];
1822
- let A1_G0_result = [];
1823
- let A1_G1_result = [];
1824
- let pos, len, ch, nextCh;
1825
- for (pos = 0, len = this.source.length; pos < len; pos++) {
1826
- ch = this.source.charAt(pos);
1827
- A0_G0_result[pos] = ch;
1828
- A0_G1_result[pos] = ch;
1829
- A1_G0_result[pos] = ch;
1830
- A1_G1_result[pos] = ch;
1831
- if (ch === "\\") {
1832
- if (pos + 1 < len) {
1833
- nextCh = this.source.charAt(pos + 1);
1834
- if (nextCh === "A") {
1835
- A0_G0_result[pos + 1] = "\uFFFF";
1836
- A0_G1_result[pos + 1] = "\uFFFF";
1837
- A1_G0_result[pos + 1] = "A";
1838
- A1_G1_result[pos + 1] = "A";
1839
- } else if (nextCh === "G") {
1840
- A0_G0_result[pos + 1] = "\uFFFF";
1841
- A0_G1_result[pos + 1] = "G";
1842
- A1_G0_result[pos + 1] = "\uFFFF";
1843
- A1_G1_result[pos + 1] = "G";
1844
- } else {
1845
- A0_G0_result[pos + 1] = nextCh;
1846
- A0_G1_result[pos + 1] = nextCh;
1847
- A1_G0_result[pos + 1] = nextCh;
1848
- A1_G1_result[pos + 1] = nextCh;
1849
- }
1850
- pos++;
1851
- }
1852
- }
1853
- }
1854
- return {
1855
- A0_G0: A0_G0_result.join(""),
1856
- A0_G1: A0_G1_result.join(""),
1857
- A1_G0: A1_G0_result.join(""),
1858
- A1_G1: A1_G1_result.join("")
1859
- };
1860
- }
1861
- resolveAnchors(allowA, allowG) {
1862
- if (!this.hasAnchor || !this._anchorCache) {
1863
- return this.source;
1864
- }
1865
- if (allowA) {
1866
- if (allowG) {
1867
- return this._anchorCache.A1_G1;
1868
- } else {
1869
- return this._anchorCache.A1_G0;
1870
- }
1871
- } else {
1872
- if (allowG) {
1873
- return this._anchorCache.A0_G1;
1874
- } else {
1875
- return this._anchorCache.A0_G0;
1876
- }
1877
- }
1878
- }
1879
- };
1880
- var RegExpSourceList = class {
1881
- constructor() {
1882
- this._items = [];
1883
- this._hasAnchors = false;
1884
- this._cached = null;
1885
- this._anchorCache = {
1886
- A0_G0: null,
1887
- A0_G1: null,
1888
- A1_G0: null,
1889
- A1_G1: null
1890
- };
1891
- }
1892
- dispose() {
1893
- this._disposeCaches();
1894
- }
1895
- _disposeCaches() {
1896
- if (this._cached) {
1897
- this._cached.dispose();
1898
- this._cached = null;
1899
- }
1900
- if (this._anchorCache.A0_G0) {
1901
- this._anchorCache.A0_G0.dispose();
1902
- this._anchorCache.A0_G0 = null;
1903
- }
1904
- if (this._anchorCache.A0_G1) {
1905
- this._anchorCache.A0_G1.dispose();
1906
- this._anchorCache.A0_G1 = null;
1907
- }
1908
- if (this._anchorCache.A1_G0) {
1909
- this._anchorCache.A1_G0.dispose();
1910
- this._anchorCache.A1_G0 = null;
1911
- }
1912
- if (this._anchorCache.A1_G1) {
1913
- this._anchorCache.A1_G1.dispose();
1914
- this._anchorCache.A1_G1 = null;
1915
- }
1916
- }
1917
- push(item) {
1918
- this._items.push(item);
1919
- this._hasAnchors = this._hasAnchors || item.hasAnchor;
1920
- }
1921
- unshift(item) {
1922
- this._items.unshift(item);
1923
- this._hasAnchors = this._hasAnchors || item.hasAnchor;
1924
- }
1925
- length() {
1926
- return this._items.length;
1927
- }
1928
- setSource(index, newSource) {
1929
- if (this._items[index].source !== newSource) {
1930
- this._disposeCaches();
1931
- this._items[index].setSource(newSource);
1932
- }
1933
- }
1934
- compile(onigLib) {
1935
- if (!this._cached) {
1936
- let regExps = this._items.map((e) => e.source);
1937
- this._cached = new CompiledRule(onigLib, regExps, this._items.map((e) => e.ruleId));
1938
- }
1939
- return this._cached;
1940
- }
1941
- compileAG(onigLib, allowA, allowG) {
1942
- if (!this._hasAnchors) {
1943
- return this.compile(onigLib);
1944
- } else {
1945
- if (allowA) {
1946
- if (allowG) {
1947
- if (!this._anchorCache.A1_G1) {
1948
- this._anchorCache.A1_G1 = this._resolveAnchors(onigLib, allowA, allowG);
1949
- }
1950
- return this._anchorCache.A1_G1;
1951
- } else {
1952
- if (!this._anchorCache.A1_G0) {
1953
- this._anchorCache.A1_G0 = this._resolveAnchors(onigLib, allowA, allowG);
1954
- }
1955
- return this._anchorCache.A1_G0;
1956
- }
1957
- } else {
1958
- if (allowG) {
1959
- if (!this._anchorCache.A0_G1) {
1960
- this._anchorCache.A0_G1 = this._resolveAnchors(onigLib, allowA, allowG);
1961
- }
1962
- return this._anchorCache.A0_G1;
1963
- } else {
1964
- if (!this._anchorCache.A0_G0) {
1965
- this._anchorCache.A0_G0 = this._resolveAnchors(onigLib, allowA, allowG);
1966
- }
1967
- return this._anchorCache.A0_G0;
1968
- }
1969
- }
1970
- }
1971
- }
1972
- _resolveAnchors(onigLib, allowA, allowG) {
1973
- let regExps = this._items.map((e) => e.resolveAnchors(allowA, allowG));
1974
- return new CompiledRule(onigLib, regExps, this._items.map((e) => e.ruleId));
1975
- }
1976
- };
1977
- var CompiledRule = class {
1978
- constructor(onigLib, regExps, rules) {
1979
- this.regExps = regExps;
1980
- this.rules = rules;
1981
- this.scanner = onigLib.createOnigScanner(regExps);
1982
- }
1983
- dispose() {
1984
- if (typeof this.scanner.dispose === "function") {
1985
- this.scanner.dispose();
1986
- }
1987
- }
1988
- toString() {
1989
- const r = [];
1990
- for (let i = 0, len = this.rules.length; i < len; i++) {
1991
- r.push(" - " + this.rules[i] + ": " + this.regExps[i]);
1992
- }
1993
- return r.join("\n");
1994
- }
1995
- findNextMatchSync(string, startPosition, options) {
1996
- const result = this.scanner.findNextMatchSync(string, startPosition, options);
1997
- if (!result) {
1998
- return null;
1999
- }
2000
- return {
2001
- ruleId: this.rules[result.index],
2002
- captureIndices: result.captureIndices
2003
- };
2004
- }
2005
- };
2006
-
2007
- // src/grammar/basicScopesAttributeProvider.ts
2008
- var BasicScopeAttributes = class {
2009
- constructor(languageId, tokenType) {
2010
- this.languageId = languageId;
2011
- this.tokenType = tokenType;
2012
- }
2013
- };
2014
- var _BasicScopeAttributesProvider = class _BasicScopeAttributesProvider {
2015
- constructor(initialLanguageId, embeddedLanguages) {
2016
- this._getBasicScopeAttributes = new CachedFn((scopeName) => {
2017
- const languageId = this._scopeToLanguage(scopeName);
2018
- const standardTokenType = this._toStandardTokenType(scopeName);
2019
- return new BasicScopeAttributes(languageId, standardTokenType);
2020
- });
2021
- this._defaultAttributes = new BasicScopeAttributes(initialLanguageId, 8 /* NotSet */);
2022
- this._embeddedLanguagesMatcher = new ScopeMatcher(Object.entries(embeddedLanguages || {}));
2023
- }
2024
- getDefaultAttributes() {
2025
- return this._defaultAttributes;
2026
- }
2027
- getBasicScopeAttributes(scopeName) {
2028
- if (scopeName === null) {
2029
- return _BasicScopeAttributesProvider._NULL_SCOPE_METADATA;
2030
- }
2031
- return this._getBasicScopeAttributes.get(scopeName);
2032
- }
2033
- /**
2034
- * Given a produced TM scope, return the language that token describes or null if unknown.
2035
- * e.g. source.html => html, source.css.embedded.html => css, punctuation.definition.tag.html => null
2036
- */
2037
- _scopeToLanguage(scope) {
2038
- return this._embeddedLanguagesMatcher.match(scope) || 0;
2039
- }
2040
- _toStandardTokenType(scopeName) {
2041
- const m = scopeName.match(_BasicScopeAttributesProvider.STANDARD_TOKEN_TYPE_REGEXP);
2042
- if (!m) {
2043
- return 8 /* NotSet */;
2044
- }
2045
- switch (m[1]) {
2046
- case "comment":
2047
- return 1 /* Comment */;
2048
- case "string":
2049
- return 2 /* String */;
2050
- case "regex":
2051
- return 3 /* RegEx */;
2052
- case "meta.embedded":
2053
- return 0 /* Other */;
2054
- }
2055
- throw new Error("Unexpected match for standard token type!");
2056
- }
2057
- };
2058
- _BasicScopeAttributesProvider._NULL_SCOPE_METADATA = new BasicScopeAttributes(0, 0);
2059
- _BasicScopeAttributesProvider.STANDARD_TOKEN_TYPE_REGEXP = /\b(comment|string|regex|meta\.embedded)\b/;
2060
- var BasicScopeAttributesProvider = _BasicScopeAttributesProvider;
2061
- var ScopeMatcher = class {
2062
- constructor(values) {
2063
- if (values.length === 0) {
2064
- this.values = null;
2065
- this.scopesRegExp = null;
2066
- } else {
2067
- this.values = new Map(values);
2068
- const escapedScopes = values.map(
2069
- ([scopeName, value]) => escapeRegExpCharacters(scopeName)
2070
- );
2071
- escapedScopes.sort();
2072
- escapedScopes.reverse();
2073
- this.scopesRegExp = new RegExp(
2074
- `^((${escapedScopes.join(")|(")}))($|\\.)`,
2075
- ""
2076
- );
2077
- }
2078
- }
2079
- match(scope) {
2080
- if (!this.scopesRegExp) {
2081
- return void 0;
2082
- }
2083
- const m = scope.match(this.scopesRegExp);
2084
- if (!m) {
2085
- return void 0;
2086
- }
2087
- return this.values.get(m[1]);
2088
- }
2089
- };
2090
-
2091
- // src/debug.ts
2092
- ({
2093
- InDebugMode: typeof process !== "undefined" && !!process.env["VSCODE_TEXTMATE_DEBUG"]
2094
- });
2095
-
2096
- // src/grammar/tokenizeString.ts
2097
- var TokenizeStringResult = class {
2098
- constructor(stack, stoppedEarly) {
2099
- this.stack = stack;
2100
- this.stoppedEarly = stoppedEarly;
2101
- }
2102
- };
2103
- function _tokenizeString(grammar, lineText, isFirstLine, linePos, stack, lineTokens, checkWhileConditions, timeLimit) {
2104
- const lineLength = lineText.content.length;
2105
- let STOP = false;
2106
- let anchorPosition = -1;
2107
- if (checkWhileConditions) {
2108
- const whileCheckResult = _checkWhileConditions(
2109
- grammar,
2110
- lineText,
2111
- isFirstLine,
2112
- linePos,
2113
- stack,
2114
- lineTokens
2115
- );
2116
- stack = whileCheckResult.stack;
2117
- linePos = whileCheckResult.linePos;
2118
- isFirstLine = whileCheckResult.isFirstLine;
2119
- anchorPosition = whileCheckResult.anchorPosition;
2120
- }
2121
- const startTime = Date.now();
2122
- while (!STOP) {
2123
- if (timeLimit !== 0) {
2124
- const elapsedTime = Date.now() - startTime;
2125
- if (elapsedTime > timeLimit) {
2126
- return new TokenizeStringResult(stack, true);
2127
- }
2128
- }
2129
- scanNext();
2130
- }
2131
- return new TokenizeStringResult(stack, false);
2132
- function scanNext() {
2133
- const r = matchRuleOrInjections(
2134
- grammar,
2135
- lineText,
2136
- isFirstLine,
2137
- linePos,
2138
- stack,
2139
- anchorPosition
2140
- );
2141
- if (!r) {
2142
- lineTokens.produce(stack, lineLength);
2143
- STOP = true;
2144
- return;
2145
- }
2146
- const captureIndices = r.captureIndices;
2147
- const matchedRuleId = r.matchedRuleId;
2148
- const hasAdvanced = captureIndices && captureIndices.length > 0 ? captureIndices[0].end > linePos : false;
2149
- if (matchedRuleId === endRuleId) {
2150
- const poppedRule = stack.getRule(grammar);
2151
- lineTokens.produce(stack, captureIndices[0].start);
2152
- stack = stack.withContentNameScopesList(stack.nameScopesList);
2153
- handleCaptures(
2154
- grammar,
2155
- lineText,
2156
- isFirstLine,
2157
- stack,
2158
- lineTokens,
2159
- poppedRule.endCaptures,
2160
- captureIndices
2161
- );
2162
- lineTokens.produce(stack, captureIndices[0].end);
2163
- const popped = stack;
2164
- stack = stack.parent;
2165
- anchorPosition = popped.getAnchorPos();
2166
- if (!hasAdvanced && popped.getEnterPos() === linePos) {
2167
- stack = popped;
2168
- lineTokens.produce(stack, lineLength);
2169
- STOP = true;
2170
- return;
2171
- }
2172
- } else {
2173
- const _rule = grammar.getRule(matchedRuleId);
2174
- lineTokens.produce(stack, captureIndices[0].start);
2175
- const beforePush = stack;
2176
- const scopeName = _rule.getName(lineText.content, captureIndices);
2177
- const nameScopesList = stack.contentNameScopesList.pushAttributed(
2178
- scopeName,
2179
- grammar
2180
- );
2181
- stack = stack.push(
2182
- matchedRuleId,
2183
- linePos,
2184
- anchorPosition,
2185
- captureIndices[0].end === lineLength,
2186
- null,
2187
- nameScopesList,
2188
- nameScopesList
2189
- );
2190
- if (_rule instanceof BeginEndRule) {
2191
- const pushedRule = _rule;
2192
- handleCaptures(
2193
- grammar,
2194
- lineText,
2195
- isFirstLine,
2196
- stack,
2197
- lineTokens,
2198
- pushedRule.beginCaptures,
2199
- captureIndices
2200
- );
2201
- lineTokens.produce(stack, captureIndices[0].end);
2202
- anchorPosition = captureIndices[0].end;
2203
- const contentName = pushedRule.getContentName(
2204
- lineText.content,
2205
- captureIndices
2206
- );
2207
- const contentNameScopesList = nameScopesList.pushAttributed(
2208
- contentName,
2209
- grammar
2210
- );
2211
- stack = stack.withContentNameScopesList(contentNameScopesList);
2212
- if (pushedRule.endHasBackReferences) {
2213
- stack = stack.withEndRule(
2214
- pushedRule.getEndWithResolvedBackReferences(
2215
- lineText.content,
2216
- captureIndices
2217
- )
2218
- );
2219
- }
2220
- if (!hasAdvanced && beforePush.hasSameRuleAs(stack)) {
2221
- stack = stack.pop();
2222
- lineTokens.produce(stack, lineLength);
2223
- STOP = true;
2224
- return;
2225
- }
2226
- } else if (_rule instanceof BeginWhileRule) {
2227
- const pushedRule = _rule;
2228
- handleCaptures(
2229
- grammar,
2230
- lineText,
2231
- isFirstLine,
2232
- stack,
2233
- lineTokens,
2234
- pushedRule.beginCaptures,
2235
- captureIndices
2236
- );
2237
- lineTokens.produce(stack, captureIndices[0].end);
2238
- anchorPosition = captureIndices[0].end;
2239
- const contentName = pushedRule.getContentName(
2240
- lineText.content,
2241
- captureIndices
2242
- );
2243
- const contentNameScopesList = nameScopesList.pushAttributed(
2244
- contentName,
2245
- grammar
2246
- );
2247
- stack = stack.withContentNameScopesList(contentNameScopesList);
2248
- if (pushedRule.whileHasBackReferences) {
2249
- stack = stack.withEndRule(
2250
- pushedRule.getWhileWithResolvedBackReferences(
2251
- lineText.content,
2252
- captureIndices
2253
- )
2254
- );
2255
- }
2256
- if (!hasAdvanced && beforePush.hasSameRuleAs(stack)) {
2257
- stack = stack.pop();
2258
- lineTokens.produce(stack, lineLength);
2259
- STOP = true;
2260
- return;
2261
- }
2262
- } else {
2263
- const matchingRule = _rule;
2264
- handleCaptures(
2265
- grammar,
2266
- lineText,
2267
- isFirstLine,
2268
- stack,
2269
- lineTokens,
2270
- matchingRule.captures,
2271
- captureIndices
2272
- );
2273
- lineTokens.produce(stack, captureIndices[0].end);
2274
- stack = stack.pop();
2275
- if (!hasAdvanced) {
2276
- stack = stack.safePop();
2277
- lineTokens.produce(stack, lineLength);
2278
- STOP = true;
2279
- return;
2280
- }
2281
- }
2282
- }
2283
- if (captureIndices[0].end > linePos) {
2284
- linePos = captureIndices[0].end;
2285
- isFirstLine = false;
2286
- }
2287
- }
2288
- }
2289
- function _checkWhileConditions(grammar, lineText, isFirstLine, linePos, stack, lineTokens) {
2290
- let anchorPosition = stack.beginRuleCapturedEOL ? 0 : -1;
2291
- const whileRules = [];
2292
- for (let node = stack; node; node = node.pop()) {
2293
- const nodeRule = node.getRule(grammar);
2294
- if (nodeRule instanceof BeginWhileRule) {
2295
- whileRules.push({
2296
- rule: nodeRule,
2297
- stack: node
2298
- });
2299
- }
2300
- }
2301
- for (let whileRule = whileRules.pop(); whileRule; whileRule = whileRules.pop()) {
2302
- const { ruleScanner, findOptions } = prepareRuleWhileSearch(whileRule.rule, grammar, whileRule.stack.endRule, isFirstLine, linePos === anchorPosition);
2303
- const r = ruleScanner.findNextMatchSync(lineText, linePos, findOptions);
2304
- if (r) {
2305
- const matchedRuleId = r.ruleId;
2306
- if (matchedRuleId !== whileRuleId) {
2307
- stack = whileRule.stack.pop();
2308
- break;
2309
- }
2310
- if (r.captureIndices && r.captureIndices.length) {
2311
- lineTokens.produce(whileRule.stack, r.captureIndices[0].start);
2312
- handleCaptures(grammar, lineText, isFirstLine, whileRule.stack, lineTokens, whileRule.rule.whileCaptures, r.captureIndices);
2313
- lineTokens.produce(whileRule.stack, r.captureIndices[0].end);
2314
- anchorPosition = r.captureIndices[0].end;
2315
- if (r.captureIndices[0].end > linePos) {
2316
- linePos = r.captureIndices[0].end;
2317
- isFirstLine = false;
2318
- }
2319
- }
2320
- } else {
2321
- stack = whileRule.stack.pop();
2322
- break;
2323
- }
2324
- }
2325
- return { stack, linePos, anchorPosition, isFirstLine };
2326
- }
2327
- function matchRuleOrInjections(grammar, lineText, isFirstLine, linePos, stack, anchorPosition) {
2328
- const matchResult = matchRule(grammar, lineText, isFirstLine, linePos, stack, anchorPosition);
2329
- const injections = grammar.getInjections();
2330
- if (injections.length === 0) {
2331
- return matchResult;
2332
- }
2333
- const injectionResult = matchInjections(injections, grammar, lineText, isFirstLine, linePos, stack, anchorPosition);
2334
- if (!injectionResult) {
2335
- return matchResult;
2336
- }
2337
- if (!matchResult) {
2338
- return injectionResult;
2339
- }
2340
- const matchResultScore = matchResult.captureIndices[0].start;
2341
- const injectionResultScore = injectionResult.captureIndices[0].start;
2342
- if (injectionResultScore < matchResultScore || injectionResult.priorityMatch && injectionResultScore === matchResultScore) {
2343
- return injectionResult;
2344
- }
2345
- return matchResult;
2346
- }
2347
- function matchRule(grammar, lineText, isFirstLine, linePos, stack, anchorPosition) {
2348
- const rule = stack.getRule(grammar);
2349
- const { ruleScanner, findOptions } = prepareRuleSearch(rule, grammar, stack.endRule, isFirstLine, linePos === anchorPosition);
2350
- const r = ruleScanner.findNextMatchSync(lineText, linePos, findOptions);
2351
- if (r) {
2352
- return {
2353
- captureIndices: r.captureIndices,
2354
- matchedRuleId: r.ruleId
2355
- };
2356
- }
2357
- return null;
2358
- }
2359
- function matchInjections(injections, grammar, lineText, isFirstLine, linePos, stack, anchorPosition) {
2360
- let bestMatchRating = Number.MAX_VALUE;
2361
- let bestMatchCaptureIndices = null;
2362
- let bestMatchRuleId;
2363
- let bestMatchResultPriority = 0;
2364
- const scopes = stack.contentNameScopesList.getScopeNames();
2365
- for (let i = 0, len = injections.length; i < len; i++) {
2366
- const injection = injections[i];
2367
- if (!injection.matcher(scopes)) {
2368
- continue;
2369
- }
2370
- const rule = grammar.getRule(injection.ruleId);
2371
- const { ruleScanner, findOptions } = prepareRuleSearch(rule, grammar, null, isFirstLine, linePos === anchorPosition);
2372
- const matchResult = ruleScanner.findNextMatchSync(lineText, linePos, findOptions);
2373
- if (!matchResult) {
2374
- continue;
2375
- }
2376
- const matchRating = matchResult.captureIndices[0].start;
2377
- if (matchRating >= bestMatchRating) {
2378
- continue;
2379
- }
2380
- bestMatchRating = matchRating;
2381
- bestMatchCaptureIndices = matchResult.captureIndices;
2382
- bestMatchRuleId = matchResult.ruleId;
2383
- bestMatchResultPriority = injection.priority;
2384
- if (bestMatchRating === linePos) {
2385
- break;
2386
- }
2387
- }
2388
- if (bestMatchCaptureIndices) {
2389
- return {
2390
- priorityMatch: bestMatchResultPriority === -1,
2391
- captureIndices: bestMatchCaptureIndices,
2392
- matchedRuleId: bestMatchRuleId
2393
- };
2394
- }
2395
- return null;
2396
- }
2397
- function prepareRuleSearch(rule, grammar, endRegexSource, allowA, allowG) {
2398
- const ruleScanner = rule.compileAG(grammar, endRegexSource, allowA, allowG);
2399
- return { ruleScanner, findOptions: 0 /* None */ };
2400
- }
2401
- function prepareRuleWhileSearch(rule, grammar, endRegexSource, allowA, allowG) {
2402
- const ruleScanner = rule.compileWhileAG(grammar, endRegexSource, allowA, allowG);
2403
- return { ruleScanner, findOptions: 0 /* None */ };
2404
- }
2405
- function handleCaptures(grammar, lineText, isFirstLine, stack, lineTokens, captures, captureIndices) {
2406
- if (captures.length === 0) {
2407
- return;
2408
- }
2409
- const lineTextContent = lineText.content;
2410
- const len = Math.min(captures.length, captureIndices.length);
2411
- const localStack = [];
2412
- const maxEnd = captureIndices[0].end;
2413
- for (let i = 0; i < len; i++) {
2414
- const captureRule = captures[i];
2415
- if (captureRule === null) {
2416
- continue;
2417
- }
2418
- const captureIndex = captureIndices[i];
2419
- if (captureIndex.length === 0) {
2420
- continue;
2421
- }
2422
- if (captureIndex.start > maxEnd) {
2423
- break;
2424
- }
2425
- while (localStack.length > 0 && localStack[localStack.length - 1].endPos <= captureIndex.start) {
2426
- lineTokens.produceFromScopes(localStack[localStack.length - 1].scopes, localStack[localStack.length - 1].endPos);
2427
- localStack.pop();
2428
- }
2429
- if (localStack.length > 0) {
2430
- lineTokens.produceFromScopes(localStack[localStack.length - 1].scopes, captureIndex.start);
2431
- } else {
2432
- lineTokens.produce(stack, captureIndex.start);
2433
- }
2434
- if (captureRule.retokenizeCapturedWithRuleId) {
2435
- const scopeName = captureRule.getName(lineTextContent, captureIndices);
2436
- const nameScopesList = stack.contentNameScopesList.pushAttributed(scopeName, grammar);
2437
- const contentName = captureRule.getContentName(lineTextContent, captureIndices);
2438
- const contentNameScopesList = nameScopesList.pushAttributed(contentName, grammar);
2439
- const stackClone = stack.push(captureRule.retokenizeCapturedWithRuleId, captureIndex.start, -1, false, null, nameScopesList, contentNameScopesList);
2440
- const onigSubStr = grammar.createOnigString(lineTextContent.substring(0, captureIndex.end));
2441
- _tokenizeString(
2442
- grammar,
2443
- onigSubStr,
2444
- isFirstLine && captureIndex.start === 0,
2445
- captureIndex.start,
2446
- stackClone,
2447
- lineTokens,
2448
- false,
2449
- /* no time limit */
2450
- 0
2451
- );
2452
- disposeOnigString(onigSubStr);
2453
- continue;
2454
- }
2455
- const captureRuleScopeName = captureRule.getName(lineTextContent, captureIndices);
2456
- if (captureRuleScopeName !== null) {
2457
- const base = localStack.length > 0 ? localStack[localStack.length - 1].scopes : stack.contentNameScopesList;
2458
- const captureRuleScopesList = base.pushAttributed(captureRuleScopeName, grammar);
2459
- localStack.push(new LocalStackElement(captureRuleScopesList, captureIndex.end));
2460
- }
2461
- }
2462
- while (localStack.length > 0) {
2463
- lineTokens.produceFromScopes(localStack[localStack.length - 1].scopes, localStack[localStack.length - 1].endPos);
2464
- localStack.pop();
2465
- }
2466
- }
2467
- var LocalStackElement = class {
2468
- constructor(scopes, endPos) {
2469
- this.scopes = scopes;
2470
- this.endPos = endPos;
2471
- }
2472
- };
2473
-
2474
- // src/grammar/grammar.ts
2475
- function createGrammar(scopeName, grammar, initialLanguage, embeddedLanguages, tokenTypes, balancedBracketSelectors, grammarRepository, onigLib) {
2476
- return new Grammar(
2477
- scopeName,
2478
- grammar,
2479
- initialLanguage,
2480
- embeddedLanguages,
2481
- tokenTypes,
2482
- balancedBracketSelectors,
2483
- grammarRepository,
2484
- onigLib
2485
- );
2486
- }
2487
- function collectInjections(result, selector, rule, ruleFactoryHelper, grammar) {
2488
- const matchers = createMatchers(selector, nameMatcher);
2489
- const ruleId = RuleFactory.getCompiledRuleId(rule, ruleFactoryHelper, grammar.repository);
2490
- for (const matcher of matchers) {
2491
- result.push({
2492
- debugSelector: selector,
2493
- matcher: matcher.matcher,
2494
- ruleId,
2495
- grammar,
2496
- priority: matcher.priority
2497
- });
2498
- }
2499
- }
2500
- function nameMatcher(identifers, scopes) {
2501
- if (scopes.length < identifers.length) {
2502
- return false;
2503
- }
2504
- let lastIndex = 0;
2505
- return identifers.every((identifier) => {
2506
- for (let i = lastIndex; i < scopes.length; i++) {
2507
- if (scopesAreMatching(scopes[i], identifier)) {
2508
- lastIndex = i + 1;
2509
- return true;
2510
- }
2511
- }
2512
- return false;
2513
- });
2514
- }
2515
- function scopesAreMatching(thisScopeName, scopeName) {
2516
- if (!thisScopeName) {
2517
- return false;
2518
- }
2519
- if (thisScopeName === scopeName) {
2520
- return true;
2521
- }
2522
- const len = scopeName.length;
2523
- return thisScopeName.length > len && thisScopeName.substr(0, len) === scopeName && thisScopeName[len] === ".";
2524
- }
2525
- var Grammar = class {
2526
- constructor(_rootScopeName, grammar, initialLanguage, embeddedLanguages, tokenTypes, balancedBracketSelectors, grammarRepository, _onigLib) {
2527
- this._rootScopeName = _rootScopeName;
2528
- this.balancedBracketSelectors = balancedBracketSelectors;
2529
- this._onigLib = _onigLib;
2530
- this._basicScopeAttributesProvider = new BasicScopeAttributesProvider(
2531
- initialLanguage,
2532
- embeddedLanguages
2533
- );
2534
- this._rootId = -1;
2535
- this._lastRuleId = 0;
2536
- this._ruleId2desc = [null];
2537
- this._includedGrammars = {};
2538
- this._grammarRepository = grammarRepository;
2539
- this._grammar = initGrammar(grammar, null);
2540
- this._injections = null;
2541
- this._tokenTypeMatchers = [];
2542
- if (tokenTypes) {
2543
- for (const selector of Object.keys(tokenTypes)) {
2544
- const matchers = createMatchers(selector, nameMatcher);
2545
- for (const matcher of matchers) {
2546
- this._tokenTypeMatchers.push({
2547
- matcher: matcher.matcher,
2548
- type: tokenTypes[selector]
2549
- });
2550
- }
2551
- }
2552
- }
2553
- }
2554
- get themeProvider() {
2555
- return this._grammarRepository;
2556
- }
2557
- dispose() {
2558
- for (const rule of this._ruleId2desc) {
2559
- if (rule) {
2560
- rule.dispose();
2561
- }
2562
- }
2563
- }
2564
- createOnigScanner(sources) {
2565
- return this._onigLib.createOnigScanner(sources);
2566
- }
2567
- createOnigString(sources) {
2568
- return this._onigLib.createOnigString(sources);
2569
- }
2570
- getMetadataForScope(scope) {
2571
- return this._basicScopeAttributesProvider.getBasicScopeAttributes(scope);
2572
- }
2573
- _collectInjections() {
2574
- const grammarRepository = {
2575
- lookup: (scopeName2) => {
2576
- if (scopeName2 === this._rootScopeName) {
2577
- return this._grammar;
2578
- }
2579
- return this.getExternalGrammar(scopeName2);
2580
- },
2581
- injections: (scopeName2) => {
2582
- return this._grammarRepository.injections(scopeName2);
2583
- }
2584
- };
2585
- const result = [];
2586
- const scopeName = this._rootScopeName;
2587
- const grammar = grammarRepository.lookup(scopeName);
2588
- if (grammar) {
2589
- const rawInjections = grammar.injections;
2590
- if (rawInjections) {
2591
- for (let expression in rawInjections) {
2592
- collectInjections(
2593
- result,
2594
- expression,
2595
- rawInjections[expression],
2596
- this,
2597
- grammar
2598
- );
2599
- }
2600
- }
2601
- const injectionScopeNames = this._grammarRepository.injections(scopeName);
2602
- if (injectionScopeNames) {
2603
- injectionScopeNames.forEach((injectionScopeName) => {
2604
- const injectionGrammar = this.getExternalGrammar(injectionScopeName);
2605
- if (injectionGrammar) {
2606
- const selector = injectionGrammar.injectionSelector;
2607
- if (selector) {
2608
- collectInjections(
2609
- result,
2610
- selector,
2611
- injectionGrammar,
2612
- this,
2613
- injectionGrammar
2614
- );
2615
- }
2616
- }
2617
- });
2618
- }
2619
- }
2620
- result.sort((i1, i2) => i1.priority - i2.priority);
2621
- return result;
2622
- }
2623
- getInjections() {
2624
- if (this._injections === null) {
2625
- this._injections = this._collectInjections();
2626
- }
2627
- return this._injections;
2628
- }
2629
- registerRule(factory) {
2630
- const id = ++this._lastRuleId;
2631
- const result = factory(ruleIdFromNumber(id));
2632
- this._ruleId2desc[id] = result;
2633
- return result;
2634
- }
2635
- getRule(ruleId) {
2636
- return this._ruleId2desc[ruleIdToNumber(ruleId)];
2637
- }
2638
- getExternalGrammar(scopeName, repository) {
2639
- if (this._includedGrammars[scopeName]) {
2640
- return this._includedGrammars[scopeName];
2641
- } else if (this._grammarRepository) {
2642
- const rawIncludedGrammar = this._grammarRepository.lookup(scopeName);
2643
- if (rawIncludedGrammar) {
2644
- this._includedGrammars[scopeName] = initGrammar(
2645
- rawIncludedGrammar,
2646
- repository && repository.$base
2647
- );
2648
- return this._includedGrammars[scopeName];
2649
- }
2650
- }
2651
- return void 0;
2652
- }
2653
- tokenizeLine(lineText, prevState, timeLimit = 0) {
2654
- const r = this._tokenize(lineText, prevState, false, timeLimit);
2655
- return {
2656
- tokens: r.lineTokens.getResult(r.ruleStack, r.lineLength),
2657
- ruleStack: r.ruleStack,
2658
- stoppedEarly: r.stoppedEarly
2659
- };
2660
- }
2661
- tokenizeLine2(lineText, prevState, timeLimit = 0) {
2662
- const r = this._tokenize(lineText, prevState, true, timeLimit);
2663
- return {
2664
- tokens: r.lineTokens.getBinaryResult(r.ruleStack, r.lineLength),
2665
- ruleStack: r.ruleStack,
2666
- stoppedEarly: r.stoppedEarly
2667
- };
2668
- }
2669
- _tokenize(lineText, prevState, emitBinaryTokens, timeLimit) {
2670
- if (this._rootId === -1) {
2671
- this._rootId = RuleFactory.getCompiledRuleId(
2672
- this._grammar.repository.$self,
2673
- this,
2674
- this._grammar.repository
2675
- );
2676
- this.getInjections();
2677
- }
2678
- let isFirstLine;
2679
- if (!prevState || prevState === StateStackImpl.NULL) {
2680
- isFirstLine = true;
2681
- const rawDefaultMetadata = this._basicScopeAttributesProvider.getDefaultAttributes();
2682
- const defaultStyle = this.themeProvider.getDefaults();
2683
- const defaultMetadata = EncodedTokenAttributes.set(
2684
- 0,
2685
- rawDefaultMetadata.languageId,
2686
- rawDefaultMetadata.tokenType,
2687
- null,
2688
- defaultStyle.fontStyle,
2689
- defaultStyle.foregroundId,
2690
- defaultStyle.backgroundId
2691
- );
2692
- const rootScopeName = this.getRule(this._rootId).getName(
2693
- null,
2694
- null
2695
- );
2696
- let scopeList;
2697
- if (rootScopeName) {
2698
- scopeList = AttributedScopeStack.createRootAndLookUpScopeName(
2699
- rootScopeName,
2700
- defaultMetadata,
2701
- this
2702
- );
2703
- } else {
2704
- scopeList = AttributedScopeStack.createRoot(
2705
- "unknown",
2706
- defaultMetadata
2707
- );
2708
- }
2709
- prevState = new StateStackImpl(
2710
- null,
2711
- this._rootId,
2712
- -1,
2713
- -1,
2714
- false,
2715
- null,
2716
- scopeList,
2717
- scopeList
2718
- );
2719
- } else {
2720
- isFirstLine = false;
2721
- prevState.reset();
2722
- }
2723
- lineText = lineText + "\n";
2724
- const onigLineText = this.createOnigString(lineText);
2725
- const lineLength = onigLineText.content.length;
2726
- const lineTokens = new LineTokens(
2727
- emitBinaryTokens,
2728
- lineText,
2729
- this._tokenTypeMatchers,
2730
- this.balancedBracketSelectors
2731
- );
2732
- const r = _tokenizeString(
2733
- this,
2734
- onigLineText,
2735
- isFirstLine,
2736
- 0,
2737
- prevState,
2738
- lineTokens,
2739
- true,
2740
- timeLimit
2741
- );
2742
- disposeOnigString(onigLineText);
2743
- return {
2744
- lineLength,
2745
- lineTokens,
2746
- ruleStack: r.stack,
2747
- stoppedEarly: r.stoppedEarly
2748
- };
2749
- }
2750
- };
2751
- function initGrammar(grammar, base) {
2752
- grammar = clone(grammar);
2753
- grammar.repository = grammar.repository || {};
2754
- grammar.repository.$self = {
2755
- $vscodeTextmateLocation: grammar.$vscodeTextmateLocation,
2756
- patterns: grammar.patterns,
2757
- name: grammar.scopeName
2758
- };
2759
- grammar.repository.$base = base || grammar.repository.$self;
2760
- return grammar;
2761
- }
2762
- var AttributedScopeStack = class _AttributedScopeStack {
2763
- /**
2764
- * Invariant:
2765
- * ```
2766
- * if (parent && !scopePath.extends(parent.scopePath)) {
2767
- * throw new Error();
2768
- * }
2769
- * ```
2770
- */
2771
- constructor(parent, scopePath, tokenAttributes) {
2772
- this.parent = parent;
2773
- this.scopePath = scopePath;
2774
- this.tokenAttributes = tokenAttributes;
2775
- }
2776
- static fromExtension(namesScopeList, contentNameScopesList) {
2777
- let current = namesScopeList;
2778
- let scopeNames = namesScopeList?.scopePath ?? null;
2779
- for (const frame of contentNameScopesList) {
2780
- scopeNames = ScopeStack.push(scopeNames, frame.scopeNames);
2781
- current = new _AttributedScopeStack(current, scopeNames, frame.encodedTokenAttributes);
2782
- }
2783
- return current;
2784
- }
2785
- static createRoot(scopeName, tokenAttributes) {
2786
- return new _AttributedScopeStack(null, new ScopeStack(null, scopeName), tokenAttributes);
2787
- }
2788
- static createRootAndLookUpScopeName(scopeName, tokenAttributes, grammar) {
2789
- const rawRootMetadata = grammar.getMetadataForScope(scopeName);
2790
- const scopePath = new ScopeStack(null, scopeName);
2791
- const rootStyle = grammar.themeProvider.themeMatch(scopePath);
2792
- const resolvedTokenAttributes = _AttributedScopeStack.mergeAttributes(
2793
- tokenAttributes,
2794
- rawRootMetadata,
2795
- rootStyle
2796
- );
2797
- return new _AttributedScopeStack(null, scopePath, resolvedTokenAttributes);
2798
- }
2799
- get scopeName() {
2800
- return this.scopePath.scopeName;
2801
- }
2802
- toString() {
2803
- return this.getScopeNames().join(" ");
2804
- }
2805
- equals(other) {
2806
- return _AttributedScopeStack.equals(this, other);
2807
- }
2808
- static equals(a, b) {
2809
- do {
2810
- if (a === b) {
2811
- return true;
2812
- }
2813
- if (!a && !b) {
2814
- return true;
2815
- }
2816
- if (!a || !b) {
2817
- return false;
2818
- }
2819
- if (a.scopeName !== b.scopeName || a.tokenAttributes !== b.tokenAttributes) {
2820
- return false;
2821
- }
2822
- a = a.parent;
2823
- b = b.parent;
2824
- } while (true);
2825
- }
2826
- static mergeAttributes(existingTokenAttributes, basicScopeAttributes, styleAttributes) {
2827
- let fontStyle = -1 /* NotSet */;
2828
- let foreground = 0;
2829
- let background = 0;
2830
- if (styleAttributes !== null) {
2831
- fontStyle = styleAttributes.fontStyle;
2832
- foreground = styleAttributes.foregroundId;
2833
- background = styleAttributes.backgroundId;
2834
- }
2835
- return EncodedTokenAttributes.set(
2836
- existingTokenAttributes,
2837
- basicScopeAttributes.languageId,
2838
- basicScopeAttributes.tokenType,
2839
- null,
2840
- fontStyle,
2841
- foreground,
2842
- background
2843
- );
2844
- }
2845
- pushAttributed(scopePath, grammar) {
2846
- if (scopePath === null) {
2847
- return this;
2848
- }
2849
- if (scopePath.indexOf(" ") === -1) {
2850
- return _AttributedScopeStack._pushAttributed(this, scopePath, grammar);
2851
- }
2852
- const scopes = scopePath.split(/ /g);
2853
- let result = this;
2854
- for (const scope of scopes) {
2855
- result = _AttributedScopeStack._pushAttributed(result, scope, grammar);
2856
- }
2857
- return result;
2858
- }
2859
- static _pushAttributed(target, scopeName, grammar) {
2860
- const rawMetadata = grammar.getMetadataForScope(scopeName);
2861
- const newPath = target.scopePath.push(scopeName);
2862
- const scopeThemeMatchResult = grammar.themeProvider.themeMatch(newPath);
2863
- const metadata = _AttributedScopeStack.mergeAttributes(
2864
- target.tokenAttributes,
2865
- rawMetadata,
2866
- scopeThemeMatchResult
2867
- );
2868
- return new _AttributedScopeStack(target, newPath, metadata);
2869
- }
2870
- getScopeNames() {
2871
- return this.scopePath.getSegments();
2872
- }
2873
- getExtensionIfDefined(base) {
2874
- const result = [];
2875
- let self = this;
2876
- while (self && self !== base) {
2877
- result.push({
2878
- encodedTokenAttributes: self.tokenAttributes,
2879
- scopeNames: self.scopePath.getExtensionIfDefined(self.parent?.scopePath ?? null)
2880
- });
2881
- self = self.parent;
2882
- }
2883
- return self === base ? result.reverse() : void 0;
2884
- }
2885
- };
2886
- var _StateStackImpl = class _StateStackImpl {
2887
- /**
2888
- * Invariant:
2889
- * ```
2890
- * if (contentNameScopesList !== nameScopesList && contentNameScopesList?.parent !== nameScopesList) {
2891
- * throw new Error();
2892
- * }
2893
- * if (this.parent && !nameScopesList.extends(this.parent.contentNameScopesList)) {
2894
- * throw new Error();
2895
- * }
2896
- * ```
2897
- */
2898
- constructor(parent, ruleId, enterPos, anchorPos, beginRuleCapturedEOL, endRule, nameScopesList, contentNameScopesList) {
2899
- this.parent = parent;
2900
- this.ruleId = ruleId;
2901
- this.beginRuleCapturedEOL = beginRuleCapturedEOL;
2902
- this.endRule = endRule;
2903
- this.nameScopesList = nameScopesList;
2904
- this.contentNameScopesList = contentNameScopesList;
2905
- this._stackElementBrand = void 0;
2906
- this.depth = this.parent ? this.parent.depth + 1 : 1;
2907
- this._enterPos = enterPos;
2908
- this._anchorPos = anchorPos;
2909
- }
2910
- equals(other) {
2911
- if (other === null) {
2912
- return false;
2913
- }
2914
- return _StateStackImpl._equals(this, other);
2915
- }
2916
- static _equals(a, b) {
2917
- if (a === b) {
2918
- return true;
2919
- }
2920
- if (!this._structuralEquals(a, b)) {
2921
- return false;
2922
- }
2923
- return AttributedScopeStack.equals(a.contentNameScopesList, b.contentNameScopesList);
2924
- }
2925
- /**
2926
- * A structural equals check. Does not take into account `scopes`.
2927
- */
2928
- static _structuralEquals(a, b) {
2929
- do {
2930
- if (a === b) {
2931
- return true;
2932
- }
2933
- if (!a && !b) {
2934
- return true;
2935
- }
2936
- if (!a || !b) {
2937
- return false;
2938
- }
2939
- if (a.depth !== b.depth || a.ruleId !== b.ruleId || a.endRule !== b.endRule) {
2940
- return false;
2941
- }
2942
- a = a.parent;
2943
- b = b.parent;
2944
- } while (true);
2945
- }
2946
- clone() {
2947
- return this;
2948
- }
2949
- static _reset(el) {
2950
- while (el) {
2951
- el._enterPos = -1;
2952
- el._anchorPos = -1;
2953
- el = el.parent;
2954
- }
2955
- }
2956
- reset() {
2957
- _StateStackImpl._reset(this);
2958
- }
2959
- pop() {
2960
- return this.parent;
2961
- }
2962
- safePop() {
2963
- if (this.parent) {
2964
- return this.parent;
2965
- }
2966
- return this;
2967
- }
2968
- push(ruleId, enterPos, anchorPos, beginRuleCapturedEOL, endRule, nameScopesList, contentNameScopesList) {
2969
- return new _StateStackImpl(
2970
- this,
2971
- ruleId,
2972
- enterPos,
2973
- anchorPos,
2974
- beginRuleCapturedEOL,
2975
- endRule,
2976
- nameScopesList,
2977
- contentNameScopesList
2978
- );
2979
- }
2980
- getEnterPos() {
2981
- return this._enterPos;
2982
- }
2983
- getAnchorPos() {
2984
- return this._anchorPos;
2985
- }
2986
- getRule(grammar) {
2987
- return grammar.getRule(this.ruleId);
2988
- }
2989
- toString() {
2990
- const r = [];
2991
- this._writeString(r, 0);
2992
- return "[" + r.join(",") + "]";
2993
- }
2994
- _writeString(res, outIndex) {
2995
- if (this.parent) {
2996
- outIndex = this.parent._writeString(res, outIndex);
2997
- }
2998
- res[outIndex++] = `(${this.ruleId}, ${this.nameScopesList?.toString()}, ${this.contentNameScopesList?.toString()})`;
2999
- return outIndex;
3000
- }
3001
- withContentNameScopesList(contentNameScopeStack) {
3002
- if (this.contentNameScopesList === contentNameScopeStack) {
3003
- return this;
3004
- }
3005
- return this.parent.push(
3006
- this.ruleId,
3007
- this._enterPos,
3008
- this._anchorPos,
3009
- this.beginRuleCapturedEOL,
3010
- this.endRule,
3011
- this.nameScopesList,
3012
- contentNameScopeStack
3013
- );
3014
- }
3015
- withEndRule(endRule) {
3016
- if (this.endRule === endRule) {
3017
- return this;
3018
- }
3019
- return new _StateStackImpl(
3020
- this.parent,
3021
- this.ruleId,
3022
- this._enterPos,
3023
- this._anchorPos,
3024
- this.beginRuleCapturedEOL,
3025
- endRule,
3026
- this.nameScopesList,
3027
- this.contentNameScopesList
3028
- );
3029
- }
3030
- // Used to warn of endless loops
3031
- hasSameRuleAs(other) {
3032
- let el = this;
3033
- while (el && el._enterPos === other._enterPos) {
3034
- if (el.ruleId === other.ruleId) {
3035
- return true;
3036
- }
3037
- el = el.parent;
3038
- }
3039
- return false;
3040
- }
3041
- toStateStackFrame() {
3042
- return {
3043
- ruleId: ruleIdToNumber(this.ruleId),
3044
- beginRuleCapturedEOL: this.beginRuleCapturedEOL,
3045
- endRule: this.endRule,
3046
- nameScopesList: this.nameScopesList?.getExtensionIfDefined(this.parent?.nameScopesList ?? null) ?? [],
3047
- contentNameScopesList: this.contentNameScopesList?.getExtensionIfDefined(this.nameScopesList) ?? []
3048
- };
3049
- }
3050
- static pushFrame(self, frame) {
3051
- const namesScopeList = AttributedScopeStack.fromExtension(self?.nameScopesList ?? null, frame.nameScopesList);
3052
- return new _StateStackImpl(
3053
- self,
3054
- ruleIdFromNumber(frame.ruleId),
3055
- frame.enterPos ?? -1,
3056
- frame.anchorPos ?? -1,
3057
- frame.beginRuleCapturedEOL,
3058
- frame.endRule,
3059
- namesScopeList,
3060
- AttributedScopeStack.fromExtension(namesScopeList, frame.contentNameScopesList)
3061
- );
3062
- }
3063
- };
3064
- // TODO remove me
3065
- _StateStackImpl.NULL = new _StateStackImpl(
3066
- null,
3067
- 0,
3068
- 0,
3069
- 0,
3070
- false,
3071
- null,
3072
- null,
3073
- null
3074
- );
3075
- var StateStackImpl = _StateStackImpl;
3076
- var BalancedBracketSelectors = class {
3077
- constructor(balancedBracketScopes, unbalancedBracketScopes) {
3078
- this.allowAny = false;
3079
- this.balancedBracketScopes = balancedBracketScopes.flatMap(
3080
- (selector) => {
3081
- if (selector === "*") {
3082
- this.allowAny = true;
3083
- return [];
3084
- }
3085
- return createMatchers(selector, nameMatcher).map((m) => m.matcher);
3086
- }
3087
- );
3088
- this.unbalancedBracketScopes = unbalancedBracketScopes.flatMap(
3089
- (selector) => createMatchers(selector, nameMatcher).map((m) => m.matcher)
3090
- );
3091
- }
3092
- get matchesAlways() {
3093
- return this.allowAny && this.unbalancedBracketScopes.length === 0;
3094
- }
3095
- get matchesNever() {
3096
- return this.balancedBracketScopes.length === 0 && !this.allowAny;
3097
- }
3098
- match(scopes) {
3099
- for (const excluder of this.unbalancedBracketScopes) {
3100
- if (excluder(scopes)) {
3101
- return false;
3102
- }
3103
- }
3104
- for (const includer of this.balancedBracketScopes) {
3105
- if (includer(scopes)) {
3106
- return true;
3107
- }
3108
- }
3109
- return this.allowAny;
3110
- }
3111
- };
3112
- var LineTokens = class {
3113
- constructor(emitBinaryTokens, lineText, tokenTypeOverrides, balancedBracketSelectors) {
3114
- this.balancedBracketSelectors = balancedBracketSelectors;
3115
- this._emitBinaryTokens = emitBinaryTokens;
3116
- this._tokenTypeOverrides = tokenTypeOverrides;
3117
- {
3118
- this._lineText = null;
3119
- }
3120
- this._tokens = [];
3121
- this._binaryTokens = [];
3122
- this._lastTokenEndIndex = 0;
3123
- }
3124
- produce(stack, endIndex) {
3125
- this.produceFromScopes(stack.contentNameScopesList, endIndex);
3126
- }
3127
- produceFromScopes(scopesList, endIndex) {
3128
- if (this._lastTokenEndIndex >= endIndex) {
3129
- return;
3130
- }
3131
- if (this._emitBinaryTokens) {
3132
- let metadata = scopesList?.tokenAttributes ?? 0;
3133
- let containsBalancedBrackets = false;
3134
- if (this.balancedBracketSelectors?.matchesAlways) {
3135
- containsBalancedBrackets = true;
3136
- }
3137
- if (this._tokenTypeOverrides.length > 0 || this.balancedBracketSelectors && !this.balancedBracketSelectors.matchesAlways && !this.balancedBracketSelectors.matchesNever) {
3138
- const scopes2 = scopesList?.getScopeNames() ?? [];
3139
- for (const tokenType of this._tokenTypeOverrides) {
3140
- if (tokenType.matcher(scopes2)) {
3141
- metadata = EncodedTokenAttributes.set(
3142
- metadata,
3143
- 0,
3144
- toOptionalTokenType(tokenType.type),
3145
- null,
3146
- -1 /* NotSet */,
3147
- 0,
3148
- 0
3149
- );
3150
- }
3151
- }
3152
- if (this.balancedBracketSelectors) {
3153
- containsBalancedBrackets = this.balancedBracketSelectors.match(scopes2);
3154
- }
3155
- }
3156
- if (containsBalancedBrackets) {
3157
- metadata = EncodedTokenAttributes.set(
3158
- metadata,
3159
- 0,
3160
- 8 /* NotSet */,
3161
- containsBalancedBrackets,
3162
- -1 /* NotSet */,
3163
- 0,
3164
- 0
3165
- );
3166
- }
3167
- if (this._binaryTokens.length > 0 && this._binaryTokens[this._binaryTokens.length - 1] === metadata) {
3168
- this._lastTokenEndIndex = endIndex;
3169
- return;
3170
- }
3171
- this._binaryTokens.push(this._lastTokenEndIndex);
3172
- this._binaryTokens.push(metadata);
3173
- this._lastTokenEndIndex = endIndex;
3174
- return;
3175
- }
3176
- const scopes = scopesList?.getScopeNames() ?? [];
3177
- this._tokens.push({
3178
- startIndex: this._lastTokenEndIndex,
3179
- endIndex,
3180
- // value: lineText.substring(lastTokenEndIndex, endIndex),
3181
- scopes
3182
- });
3183
- this._lastTokenEndIndex = endIndex;
3184
- }
3185
- getResult(stack, lineLength) {
3186
- if (this._tokens.length > 0 && this._tokens[this._tokens.length - 1].startIndex === lineLength - 1) {
3187
- this._tokens.pop();
3188
- }
3189
- if (this._tokens.length === 0) {
3190
- this._lastTokenEndIndex = -1;
3191
- this.produce(stack, lineLength);
3192
- this._tokens[this._tokens.length - 1].startIndex = 0;
3193
- }
3194
- return this._tokens;
3195
- }
3196
- getBinaryResult(stack, lineLength) {
3197
- if (this._binaryTokens.length > 0 && this._binaryTokens[this._binaryTokens.length - 2] === lineLength - 1) {
3198
- this._binaryTokens.pop();
3199
- this._binaryTokens.pop();
3200
- }
3201
- if (this._binaryTokens.length === 0) {
3202
- this._lastTokenEndIndex = -1;
3203
- this.produce(stack, lineLength);
3204
- this._binaryTokens[this._binaryTokens.length - 2] = 0;
3205
- }
3206
- const result = new Uint32Array(this._binaryTokens.length);
3207
- for (let i = 0, len = this._binaryTokens.length; i < len; i++) {
3208
- result[i] = this._binaryTokens[i];
3209
- }
3210
- return result;
3211
- }
3212
- };
3213
-
3214
- // src/registry.ts
3215
- var SyncRegistry = class {
3216
- constructor(theme, _onigLib) {
3217
- this._onigLib = _onigLib;
3218
- this._grammars = /* @__PURE__ */ new Map();
3219
- this._rawGrammars = /* @__PURE__ */ new Map();
3220
- this._injectionGrammars = /* @__PURE__ */ new Map();
3221
- this._theme = theme;
3222
- }
3223
- dispose() {
3224
- for (const grammar of this._grammars.values()) {
3225
- grammar.dispose();
3226
- }
3227
- }
3228
- setTheme(theme) {
3229
- this._theme = theme;
3230
- }
3231
- getColorMap() {
3232
- return this._theme.getColorMap();
3233
- }
3234
- /**
3235
- * Add `grammar` to registry and return a list of referenced scope names
3236
- */
3237
- addGrammar(grammar, injectionScopeNames) {
3238
- this._rawGrammars.set(grammar.scopeName, grammar);
3239
- if (injectionScopeNames) {
3240
- this._injectionGrammars.set(grammar.scopeName, injectionScopeNames);
3241
- }
3242
- }
3243
- /**
3244
- * Lookup a raw grammar.
3245
- */
3246
- lookup(scopeName) {
3247
- return this._rawGrammars.get(scopeName);
3248
- }
3249
- /**
3250
- * Returns the injections for the given grammar
3251
- */
3252
- injections(targetScope) {
3253
- return this._injectionGrammars.get(targetScope);
3254
- }
3255
- /**
3256
- * Get the default theme settings
3257
- */
3258
- getDefaults() {
3259
- return this._theme.getDefaults();
3260
- }
3261
- /**
3262
- * Match a scope in the theme.
3263
- */
3264
- themeMatch(scopePath) {
3265
- return this._theme.match(scopePath);
3266
- }
3267
- /**
3268
- * Lookup a grammar.
3269
- */
3270
- grammarForScopeName(scopeName, initialLanguage, embeddedLanguages, tokenTypes, balancedBracketSelectors) {
3271
- if (!this._grammars.has(scopeName)) {
3272
- let rawGrammar = this._rawGrammars.get(scopeName);
3273
- if (!rawGrammar) {
3274
- return null;
3275
- }
3276
- this._grammars.set(scopeName, createGrammar(
3277
- scopeName,
3278
- rawGrammar,
3279
- initialLanguage,
3280
- embeddedLanguages,
3281
- tokenTypes,
3282
- balancedBracketSelectors,
3283
- this,
3284
- this._onigLib
3285
- ));
3286
- }
3287
- return this._grammars.get(scopeName);
3288
- }
3289
- };
3290
-
3291
- // src/main.ts
3292
- var Registry$1 = class Registry {
3293
- constructor(options) {
3294
- this._options = options;
3295
- this._syncRegistry = new SyncRegistry(
3296
- Theme.createFromRawTheme(options.theme, options.colorMap),
3297
- options.onigLib
3298
- );
3299
- this._ensureGrammarCache = /* @__PURE__ */ new Map();
3300
- }
3301
- dispose() {
3302
- this._syncRegistry.dispose();
3303
- }
3304
- /**
3305
- * Change the theme. Once called, no previous `ruleStack` should be used anymore.
3306
- */
3307
- setTheme(theme, colorMap) {
3308
- this._syncRegistry.setTheme(Theme.createFromRawTheme(theme, colorMap));
3309
- }
3310
- /**
3311
- * Returns a lookup array for color ids.
3312
- */
3313
- getColorMap() {
3314
- return this._syncRegistry.getColorMap();
3315
- }
3316
- /**
3317
- * Load the grammar for `scopeName` and all referenced included grammars asynchronously.
3318
- * Please do not use language id 0.
3319
- */
3320
- loadGrammarWithEmbeddedLanguages(initialScopeName, initialLanguage, embeddedLanguages) {
3321
- return this.loadGrammarWithConfiguration(initialScopeName, initialLanguage, { embeddedLanguages });
3322
- }
3323
- /**
3324
- * Load the grammar for `scopeName` and all referenced included grammars asynchronously.
3325
- * Please do not use language id 0.
3326
- */
3327
- loadGrammarWithConfiguration(initialScopeName, initialLanguage, configuration) {
3328
- return this._loadGrammar(
3329
- initialScopeName,
3330
- initialLanguage,
3331
- configuration.embeddedLanguages,
3332
- configuration.tokenTypes,
3333
- new BalancedBracketSelectors(
3334
- configuration.balancedBracketSelectors || [],
3335
- configuration.unbalancedBracketSelectors || []
3336
- )
3337
- );
3338
- }
3339
- /**
3340
- * Load the grammar for `scopeName` and all referenced included grammars asynchronously.
3341
- */
3342
- loadGrammar(initialScopeName) {
3343
- return this._loadGrammar(initialScopeName, 0, null, null, null);
3344
- }
3345
- _loadGrammar(initialScopeName, initialLanguage, embeddedLanguages, tokenTypes, balancedBracketSelectors) {
3346
- const dependencyProcessor = new ScopeDependencyProcessor(this._syncRegistry, initialScopeName);
3347
- while (dependencyProcessor.Q.length > 0) {
3348
- dependencyProcessor.Q.map((request) => this._loadSingleGrammar(request.scopeName));
3349
- dependencyProcessor.processQueue();
3350
- }
3351
- return this._grammarForScopeName(
3352
- initialScopeName,
3353
- initialLanguage,
3354
- embeddedLanguages,
3355
- tokenTypes,
3356
- balancedBracketSelectors
3357
- );
3358
- }
3359
- _loadSingleGrammar(scopeName) {
3360
- if (!this._ensureGrammarCache.has(scopeName)) {
3361
- this._doLoadSingleGrammar(scopeName);
3362
- this._ensureGrammarCache.set(scopeName, true);
3363
- }
3364
- }
3365
- _doLoadSingleGrammar(scopeName) {
3366
- const grammar = this._options.loadGrammar(scopeName);
3367
- if (grammar) {
3368
- const injections = typeof this._options.getInjections === "function" ? this._options.getInjections(scopeName) : void 0;
3369
- this._syncRegistry.addGrammar(grammar, injections);
3370
- }
3371
- }
3372
- /**
3373
- * Adds a rawGrammar.
3374
- */
3375
- addGrammar(rawGrammar, injections = [], initialLanguage = 0, embeddedLanguages = null) {
3376
- this._syncRegistry.addGrammar(rawGrammar, injections);
3377
- return this._grammarForScopeName(rawGrammar.scopeName, initialLanguage, embeddedLanguages);
3378
- }
3379
- /**
3380
- * Get the grammar for `scopeName`. The grammar must first be created via `loadGrammar` or `addGrammar`.
3381
- */
3382
- _grammarForScopeName(scopeName, initialLanguage = 0, embeddedLanguages = null, tokenTypes = null, balancedBracketSelectors = null) {
3383
- return this._syncRegistry.grammarForScopeName(
3384
- scopeName,
3385
- initialLanguage,
3386
- embeddedLanguages,
3387
- tokenTypes,
3388
- balancedBracketSelectors
3389
- );
3390
- }
3391
- };
3392
- var INITIAL = StateStackImpl.NULL;
3393
-
3394
393
  /**
3395
394
  * GrammarState is a special reference object that holds the state of a grammar.
3396
395
  *
@@ -3445,110 +444,6 @@ function getGrammarStack(state) {
3445
444
  return state._stack;
3446
445
  }
3447
446
 
3448
- /**
3449
- * Helpers to manage the "collapsed" metadata of an entire StackElement stack.
3450
- * The following assumptions have been made:
3451
- * - languageId < 256 => needs 8 bits
3452
- * - unique color count < 512 => needs 9 bits
3453
- *
3454
- * The binary format is:
3455
- * - -------------------------------------------
3456
- * 3322 2222 2222 1111 1111 1100 0000 0000
3457
- * 1098 7654 3210 9876 5432 1098 7654 3210
3458
- * - -------------------------------------------
3459
- * xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx
3460
- * bbbb bbbb bfff ffff ffFF FTTT LLLL LLLL
3461
- * - -------------------------------------------
3462
- * - L = LanguageId (8 bits)
3463
- * - T = StandardTokenType (3 bits)
3464
- * - F = FontStyle (3 bits)
3465
- * - f = foreground color (9 bits)
3466
- * - b = background color (9 bits)
3467
- */
3468
- const MetadataConsts = {
3469
- LANGUAGEID_MASK: 0b00000000000000000000000011111111,
3470
- TOKEN_TYPE_MASK: 0b00000000000000000000001100000000,
3471
- BALANCED_BRACKETS_MASK: 0b00000000000000000000010000000000,
3472
- FONT_STYLE_MASK: 0b00000000000000000011100000000000,
3473
- FOREGROUND_MASK: 0b00000000011111111100000000000000,
3474
- BACKGROUND_MASK: 0b11111111100000000000000000000000,
3475
- LANGUAGEID_OFFSET: 0,
3476
- TOKEN_TYPE_OFFSET: 8,
3477
- BALANCED_BRACKETS_OFFSET: 10,
3478
- FONT_STYLE_OFFSET: 11,
3479
- FOREGROUND_OFFSET: 15,
3480
- BACKGROUND_OFFSET: 24,
3481
- };
3482
- class StackElementMetadata {
3483
- static toBinaryStr(metadata) {
3484
- let r = metadata.toString(2);
3485
- while (r.length < 32)
3486
- r = `0${r}`;
3487
- return r;
3488
- }
3489
- // public static printMetadata(metadata: number): void {
3490
- // const languageId = StackElementMetadata.getLanguageId(metadata)
3491
- // const tokenType = StackElementMetadata.getTokenType(metadata)
3492
- // const fontStyle = StackElementMetadata.getFontStyle(metadata)
3493
- // const foreground = StackElementMetadata.getForeground(metadata)
3494
- // const background = StackElementMetadata.getBackground(metadata)
3495
- // console.log({
3496
- // languageId,
3497
- // tokenType,
3498
- // fontStyle,
3499
- // foreground,
3500
- // background,
3501
- // })
3502
- // }
3503
- static getLanguageId(metadata) {
3504
- return (metadata & MetadataConsts.LANGUAGEID_MASK) >>> MetadataConsts.LANGUAGEID_OFFSET;
3505
- }
3506
- static getTokenType(metadata) {
3507
- return (metadata & MetadataConsts.TOKEN_TYPE_MASK) >>> MetadataConsts.TOKEN_TYPE_OFFSET;
3508
- }
3509
- static getFontStyle(metadata) {
3510
- return (metadata & MetadataConsts.FONT_STYLE_MASK) >>> MetadataConsts.FONT_STYLE_OFFSET;
3511
- }
3512
- static getForeground(metadata) {
3513
- return (metadata & MetadataConsts.FOREGROUND_MASK) >>> MetadataConsts.FOREGROUND_OFFSET;
3514
- }
3515
- static getBackground(metadata) {
3516
- return (metadata & MetadataConsts.BACKGROUND_MASK) >>> MetadataConsts.BACKGROUND_OFFSET;
3517
- }
3518
- static containsBalancedBrackets(metadata) {
3519
- return (metadata & MetadataConsts.BALANCED_BRACKETS_MASK) !== 0;
3520
- }
3521
- static set(metadata, languageId, tokenType, fontStyle, foreground, background) {
3522
- let _languageId = StackElementMetadata.getLanguageId(metadata);
3523
- let _tokenType = StackElementMetadata.getTokenType(metadata);
3524
- let _fontStyle = StackElementMetadata.getFontStyle(metadata);
3525
- let _foreground = StackElementMetadata.getForeground(metadata);
3526
- let _background = StackElementMetadata.getBackground(metadata);
3527
- const _containsBalancedBracketsBit = StackElementMetadata.containsBalancedBrackets(metadata)
3528
- ? 1
3529
- : 0;
3530
- if (languageId !== 0)
3531
- _languageId = languageId;
3532
- if (tokenType !== 0 /* TemporaryStandardTokenType.Other */) {
3533
- _tokenType
3534
- = tokenType === 8 /* TemporaryStandardTokenType.MetaEmbedded */ ? 0 /* StandardTokenType.Other */ : tokenType;
3535
- }
3536
- if (fontStyle !== FontStyle.NotSet)
3537
- _fontStyle = fontStyle;
3538
- if (foreground !== 0)
3539
- _foreground = foreground;
3540
- if (background !== 0)
3541
- _background = background;
3542
- return (((_languageId << MetadataConsts.LANGUAGEID_OFFSET)
3543
- | (_tokenType << MetadataConsts.TOKEN_TYPE_OFFSET)
3544
- | (_fontStyle << MetadataConsts.FONT_STYLE_OFFSET)
3545
- | (_containsBalancedBracketsBit << MetadataConsts.BALANCED_BRACKETS_OFFSET)
3546
- | (_foreground << MetadataConsts.FOREGROUND_OFFSET)
3547
- | (_background << MetadataConsts.BACKGROUND_OFFSET))
3548
- >>> 0);
3549
- }
3550
- }
3551
-
3552
447
  // src/colors.ts
3553
448
  var namedColors = [
3554
449
  "black",
@@ -3858,7 +753,8 @@ function dimColor(color) {
3858
753
  if (hexMatch) {
3859
754
  if (hexMatch[3]) {
3860
755
  // convert from #rrggbbaa to #rrggbb(aa/2)
3861
- const alpha = Math.round(Number.parseInt(hexMatch[3], 16) / 2)
756
+ const alpha = Math
757
+ .round(Number.parseInt(hexMatch[3], 16) / 2)
3862
758
  .toString(16)
3863
759
  .padStart(2, '0');
3864
760
  return `#${hexMatch[1]}${hexMatch[2]}${alpha}`;
@@ -3869,7 +765,8 @@ function dimColor(color) {
3869
765
  }
3870
766
  else {
3871
767
  // convert from #rgb to #rrggbb80
3872
- return `#${Array.from(hexMatch[1])
768
+ return `#${Array
769
+ .from(hexMatch[1])
3873
770
  .map(x => `${x}${x}`)
3874
771
  .join('')}80`;
3875
772
  }
@@ -3966,8 +863,8 @@ function _tokenizeWithTheme(code, grammar, theme, colorMap, options) {
3966
863
  if (startIndex === nextStartIndex)
3967
864
  continue;
3968
865
  const metadata = result.tokens[2 * j + 1];
3969
- const color = applyColorReplacements(colorMap[StackElementMetadata.getForeground(metadata)], colorReplacements);
3970
- const fontStyle = StackElementMetadata.getFontStyle(metadata);
866
+ const color = applyColorReplacements(colorMap[EncodedTokenMetadata.getForeground(metadata)], colorReplacements);
867
+ const fontStyle = EncodedTokenMetadata.getFontStyle(metadata);
3971
868
  const token = {
3972
869
  content: line.substring(startIndex, nextStartIndex),
3973
870
  offset: lineOffset + startIndex,
@@ -4070,7 +967,8 @@ function explainThemeScope(themeSettingsSelectors, scope, parentScopes) {
4070
967
  * Get tokens with multiple themes
4071
968
  */
4072
969
  function codeToTokensWithThemes(internal, code, options) {
4073
- const themes = Object.entries(options.themes)
970
+ const themes = Object
971
+ .entries(options.themes)
4074
972
  .filter(i => i[1])
4075
973
  .map(i => ({ color: i[0], theme: i[1] }));
4076
974
  const tokens = syncThemesTokenization(...themes.map(t => codeToTokensBase(internal, code, {
@@ -4157,7 +1055,8 @@ function codeToTokens(internal, code, options) {
4157
1055
  let rootStyle;
4158
1056
  if ('themes' in options) {
4159
1057
  const { defaultColor = 'light', cssVariablePrefix = '--shiki-', } = options;
4160
- const themes = Object.entries(options.themes)
1058
+ const themes = Object
1059
+ .entries(options.themes)
4161
1060
  .filter(i => i[1])
4162
1061
  .map(i => ({ color: i[0], theme: i[1] }))
4163
1062
  .sort((a, b) => a.color === defaultColor ? -1 : b.color === defaultColor ? 1 : 0);
@@ -8968,6 +5867,8 @@ class RegExpConversionError extends SyntaxError {
8968
5867
  }
8969
5868
  }
8970
5869
 
5870
+ const UNNECESSARY_ESCAPE_CHAR_CLASS = new Set("!?:=+$(){}_><# ");
5871
+ const UNNECESSARY_ESCAPE = new Set("-!:=_>< ");
8971
5872
  const TABLE_POSIX = {
8972
5873
  alnum: "0-9A-Za-z",
8973
5874
  alpha: "A-Za-z",
@@ -8984,13 +5885,23 @@ const TABLE_POSIX = {
8984
5885
  xdigit: "0-9A-Fa-f",
8985
5886
  word: "\\w"
8986
5887
  };
5888
+ const TABLE_SLASH_P = {
5889
+ alnum: "0-9A-Za-z",
5890
+ alpha: "A-Za-z",
5891
+ alphabetic: "A-Za-z",
5892
+ blank: "\\s",
5893
+ greek: "\\p{Script=Greek}",
5894
+ print: "\\p{L}\\p{N}\\p{P}\\p{S}\\p{Zs}",
5895
+ word: "\\w"
5896
+ };
8987
5897
  const KNOWN_FLAGS = /* @__PURE__ */ new Set("gimsuyx");
8988
5898
  function syntaxLowering(input, options = {}) {
8989
5899
  const {
8990
5900
  preserveFlags = false,
8991
5901
  removePossessiveQuantifier = false,
8992
5902
  removeAtomicGroup = false,
8993
- convertHexDigitsShorthand = false
5903
+ convertHexDigitsShorthand = false,
5904
+ convertUnicodeCategory = false
8994
5905
  } = options;
8995
5906
  let output = "";
8996
5907
  const flags = /* @__PURE__ */ new Set();
@@ -9005,7 +5916,7 @@ function syntaxLowering(input, options = {}) {
9005
5916
  freeSpacingLocal.shift();
9006
5917
  }
9007
5918
  const head = stack[0];
9008
- const wsEscape = freeSpacingGlobal || freeSpacingLocal.length;
5919
+ const freeSpacing = freeSpacingGlobal || freeSpacingLocal.length;
9009
5920
  if (char === "\\") {
9010
5921
  if (convertHexDigitsShorthand) {
9011
5922
  if (input[i + 1] === "h") {
@@ -9031,11 +5942,41 @@ function syntaxLowering(input, options = {}) {
9031
5942
  continue;
9032
5943
  }
9033
5944
  }
5945
+ if (convertUnicodeCategory && input[i + 1] === "p" && input[i + 2] === "{") {
5946
+ const end = input.indexOf("}", i + 3);
5947
+ if (end === -1) {
5948
+ throw new RegExpConversionError(
5949
+ "Unmatched \\p{...}",
5950
+ { pattern: input, converted: output, cursor: i }
5951
+ );
5952
+ }
5953
+ const name = input.slice(i + 3, end);
5954
+ const resolved = TABLE_SLASH_P[name.toLowerCase()];
5955
+ if (resolved) {
5956
+ if (head === "[") {
5957
+ output += resolved;
5958
+ } else {
5959
+ output += `[${resolved}]`;
5960
+ }
5961
+ i = end + 1;
5962
+ continue;
5963
+ }
5964
+ }
5965
+ if (head === "[" && UNNECESSARY_ESCAPE_CHAR_CLASS.has(input[i + 1])) {
5966
+ output += input[i + 1];
5967
+ i += 2;
5968
+ continue;
5969
+ }
5970
+ if (head !== "[" && UNNECESSARY_ESCAPE.has(input[i + 1])) {
5971
+ output += input[i + 1];
5972
+ i += 2;
5973
+ continue;
5974
+ }
9034
5975
  output += char + input[i + 1];
9035
5976
  i += 2;
9036
5977
  continue;
9037
5978
  }
9038
- if (char === "#" && wsEscape && input[i - 1].match(/\s/) && head !== "[") {
5979
+ if (char === "#" && freeSpacing && input[i - 1].match(/\s/) && head !== "[") {
9039
5980
  for (let j = i + 1; j <= input.length; j++) {
9040
5981
  if (input[j] === "\n" || j === input.length) {
9041
5982
  i = j;
@@ -9174,7 +6115,7 @@ function syntaxLowering(input, options = {}) {
9174
6115
  continue;
9175
6116
  }
9176
6117
  }
9177
- if (!(wsEscape && head !== "[" && char.match(/\s/))) {
6118
+ if (!(freeSpacing && head !== "[" && char.match(/\s/))) {
9178
6119
  output += char;
9179
6120
  }
9180
6121
  i += 1;
@@ -9214,7 +6155,9 @@ function construct(pattern, options = {}) {
9214
6155
  if (options.ignoreContiguousAnchors) {
9215
6156
  pattern = pattern.replace(/\\G/g, "");
9216
6157
  }
9217
- pattern = pattern.replace(/\\A/g, "^").replace(/\\x\{([^}]*)\}/g, (m, hex) => `\\u${hex.padStart(4, "0")}`).replace(/\(\?(-)?(\w+):/g, (_, neg, flagStr) => {
6158
+ if (pattern.includes("\\p{"))
6159
+ flagSet.add("u");
6160
+ pattern = pattern.replace(/\\A/g, "^").replace(/\\Z/gi, "$").replace(/\\x\{([^}]*)\}/g, (m, hex) => `\\u${hex.padStart(4, "0")}`).replace(/\(\?(-)?(\w+):/g, (_, neg, flagStr) => {
9218
6161
  if (neg) {
9219
6162
  for (const flag of flagStr)
9220
6163
  flagSet.delete(flag);
@@ -9261,6 +6204,7 @@ function onigurumaToRegexp(pattern, options = {}) {
9261
6204
  removePossessiveQuantifier: true,
9262
6205
  removeAtomicGroup: true,
9263
6206
  convertHexDigitsShorthand: true,
6207
+ convertUnicodeCategory: true,
9264
6208
  ...options
9265
6209
  });
9266
6210
  return construct(converted, {
@@ -9271,15 +6215,30 @@ function onigurumaToRegexp(pattern, options = {}) {
9271
6215
  }
9272
6216
 
9273
6217
  const MAX = 4294967295;
6218
+ /**
6219
+ * The default RegExp constructor for JavaScript regex engine.
6220
+ */
6221
+ function defaultJavaScriptRegexConstructor(pattern) {
6222
+ return onigurumaToRegexp(pattern
6223
+ .replace(/\|\\G(\||\))/g, '$1')
6224
+ .replace(/(\(|\|)\\G\|/g, '$1')
6225
+ // YAML specific handling; TODO: move to tm-grammars
6226
+ .replaceAll('[^\\s[-?:,\\[\\]{}#&*!|>\'"%@`]]', '[^\\s\\-?:,\\[\\]{}#&*!|>\'"%@`]'), {
6227
+ flags: 'dgm',
6228
+ ignoreContiguousAnchors: true,
6229
+ });
6230
+ }
9274
6231
  class JavaScriptScanner {
9275
6232
  patterns;
9276
6233
  cache;
9277
6234
  forgiving;
6235
+ regexConstructor;
9278
6236
  regexps;
9279
- constructor(patterns, cache, forgiving) {
6237
+ constructor(patterns, cache, forgiving, regexConstructor = defaultJavaScriptRegexConstructor) {
9280
6238
  this.patterns = patterns;
9281
6239
  this.cache = cache;
9282
6240
  this.forgiving = forgiving;
6241
+ this.regexConstructor = regexConstructor;
9283
6242
  this.regexps = patterns.map((p) => {
9284
6243
  const cached = cache?.get(p);
9285
6244
  if (cached) {
@@ -9291,14 +6250,7 @@ class JavaScriptScanner {
9291
6250
  throw cached;
9292
6251
  }
9293
6252
  try {
9294
- const regex = onigurumaToRegexp(p
9295
- .replace(/\|\\G(\||\))/g, '$1')
9296
- .replace(/(\(|\|)\\G\|/g, '$1')
9297
- // YAML specific handling; TODO: move to tm-grammars
9298
- .replaceAll('[^\\s[-?:,\\[\\]{}#&*!|>\'"%@`]]', '[^\\s\\-?:,\\[\\]{}#&*!|>\'"%@`]'), {
9299
- flags: 'dgm',
9300
- ignoreContiguousAnchors: true,
9301
- });
6253
+ const regex = regexConstructor(p);
9302
6254
  cache?.set(p, regex);
9303
6255
  return regex;
9304
6256
  }
@@ -9382,7 +6334,7 @@ function createJavaScriptRegexEngine(options = {}) {
9382
6334
  const { forgiving = false, cache = new Map(), } = options;
9383
6335
  return {
9384
6336
  createScanner(patterns) {
9385
- return new JavaScriptScanner(patterns, cache, forgiving);
6337
+ return new JavaScriptScanner(patterns, cache, forgiving, options.regexConstructor);
9386
6338
  },
9387
6339
  createString(s) {
9388
6340
  return {
@@ -9392,4 +6344,4 @@ function createJavaScriptRegexEngine(options = {}) {
9392
6344
  };
9393
6345
  }
9394
6346
 
9395
- export { FontStyle, ShikiError, StackElementMetadata, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createHighlighterCore, createHighlighterCoreSync, createJavaScriptRegexEngine, createPositionConverter, createShikiInternal, createShikiInternalSync, createSingletonShorthands, createWasmOnigEngine, createdBundledHighlighter, getHighlighterCore, getShikiInternal, getSingletonHighlighterCore, getTokenStyleObject, toHtml as hastToHtml, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, loadWasm, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeGetter, normalizeTheme, resolveColorReplacements, setDefaultWasmLoader, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations };
6347
+ export { FontStyle, ShikiError, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createHighlighterCore, createHighlighterCoreSync, createJavaScriptRegexEngine, createPositionConverter, createShikiInternal, createShikiInternalSync, createSingletonShorthands, createWasmOnigEngine, createdBundledHighlighter, defaultJavaScriptRegexConstructor, getHighlighterCore, getShikiInternal, getSingletonHighlighterCore, getTokenStyleObject, toHtml as hastToHtml, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, loadWasm, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeGetter, normalizeTheme, resolveColorReplacements, setDefaultWasmLoader, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations };