@spyglassmc/core 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. package/README.md +10 -0
  2. package/lib/common/Heap.d.ts +1 -0
  3. package/lib/common/Heap.js +2 -0
  4. package/lib/common/index.d.ts +2 -0
  5. package/lib/common/index.js +14 -0
  6. package/lib/common/util.d.ts +120 -0
  7. package/lib/common/util.js +292 -0
  8. package/lib/index.d.ts +8 -0
  9. package/lib/index.js +20 -0
  10. package/lib/node/AstNode.d.ts +64 -0
  11. package/lib/node/AstNode.js +108 -0
  12. package/lib/node/BooleanNode.d.ts +13 -0
  13. package/lib/node/BooleanNode.js +20 -0
  14. package/lib/node/CommentNode.d.ts +12 -0
  15. package/lib/node/CommentNode.js +11 -0
  16. package/lib/node/ErrorNode.d.ts +8 -0
  17. package/lib/node/ErrorNode.js +12 -0
  18. package/lib/node/FileNode.d.ts +21 -0
  19. package/lib/node/FileNode.js +11 -0
  20. package/lib/node/FloatNode.d.ts +13 -0
  21. package/lib/node/FloatNode.js +21 -0
  22. package/lib/node/IntegerNode.d.ts +13 -0
  23. package/lib/node/IntegerNode.js +20 -0
  24. package/lib/node/ListNode.d.ts +15 -0
  25. package/lib/node/ListNode.js +11 -0
  26. package/lib/node/LiteralNode.d.ts +19 -0
  27. package/lib/node/LiteralNode.js +22 -0
  28. package/lib/node/LongNode.d.ts +13 -0
  29. package/lib/node/LongNode.js +20 -0
  30. package/lib/node/RecordNode.d.ts +20 -0
  31. package/lib/node/RecordNode.js +11 -0
  32. package/lib/node/ResourceLocationNode.d.ts +41 -0
  33. package/lib/node/ResourceLocationNode.js +65 -0
  34. package/lib/node/Sequence.d.ts +22 -0
  35. package/lib/node/Sequence.js +11 -0
  36. package/lib/node/StringNode.d.ts +63 -0
  37. package/lib/node/StringNode.js +43 -0
  38. package/lib/node/SymbolNode.d.ts +21 -0
  39. package/lib/node/SymbolNode.js +22 -0
  40. package/lib/node/TableNode.d.ts +17 -0
  41. package/lib/node/TableNode.js +11 -0
  42. package/lib/node/index.d.ts +16 -0
  43. package/lib/node/index.js +28 -0
  44. package/lib/parser/Parser.d.ts +16 -0
  45. package/lib/parser/Parser.js +5 -0
  46. package/lib/parser/boolean.d.ts +4 -0
  47. package/lib/parser/boolean.js +11 -0
  48. package/lib/parser/comment.d.ts +12 -0
  49. package/lib/parser/comment.js +34 -0
  50. package/lib/parser/empty.d.ts +3 -0
  51. package/lib/parser/empty.js +7 -0
  52. package/lib/parser/error.d.ts +8 -0
  53. package/lib/parser/error.js +22 -0
  54. package/lib/parser/file.d.ts +9 -0
  55. package/lib/parser/file.js +37 -0
  56. package/lib/parser/float.d.ts +33 -0
  57. package/lib/parser/float.js +57 -0
  58. package/lib/parser/index.d.ts +17 -0
  59. package/lib/parser/index.js +36 -0
  60. package/lib/parser/integer.d.ts +33 -0
  61. package/lib/parser/integer.js +46 -0
  62. package/lib/parser/list.d.ts +12 -0
  63. package/lib/parser/list.js +70 -0
  64. package/lib/parser/literal.d.ts +5 -0
  65. package/lib/parser/literal.js +41 -0
  66. package/lib/parser/long.d.ts +33 -0
  67. package/lib/parser/long.js +50 -0
  68. package/lib/parser/record.d.ts +22 -0
  69. package/lib/parser/record.js +97 -0
  70. package/lib/parser/resourceLocation.d.ts +4 -0
  71. package/lib/parser/resourceLocation.js +67 -0
  72. package/lib/parser/string.d.ts +18 -0
  73. package/lib/parser/string.js +129 -0
  74. package/lib/parser/symbol.d.ts +12 -0
  75. package/lib/parser/symbol.js +33 -0
  76. package/lib/parser/table.d.ts +22 -0
  77. package/lib/parser/table.js +97 -0
  78. package/lib/parser/util.d.ts +134 -0
  79. package/lib/parser/util.js +224 -0
  80. package/lib/processor/ColorInfoProvider.d.ts +87 -0
  81. package/lib/processor/ColorInfoProvider.js +141 -0
  82. package/lib/processor/InlayHintProvider.d.ts +8 -0
  83. package/lib/processor/InlayHintProvider.js +3 -0
  84. package/lib/processor/SignatureHelpProvider.d.ts +18 -0
  85. package/lib/processor/SignatureHelpProvider.js +3 -0
  86. package/lib/processor/checker/Checker.d.ts +7 -0
  87. package/lib/processor/checker/Checker.js +7 -0
  88. package/lib/processor/checker/builtin.d.ts +23 -0
  89. package/lib/processor/checker/builtin.js +83 -0
  90. package/lib/processor/checker/index.d.ts +3 -0
  91. package/lib/processor/checker/index.js +28 -0
  92. package/lib/processor/colorizer/Colorizer.d.ts +23 -0
  93. package/lib/processor/colorizer/Colorizer.js +70 -0
  94. package/lib/processor/colorizer/builtin.d.ts +17 -0
  95. package/lib/processor/colorizer/builtin.js +84 -0
  96. package/lib/processor/colorizer/index.d.ts +3 -0
  97. package/lib/processor/colorizer/index.js +28 -0
  98. package/lib/processor/completer/Completer.d.ts +75 -0
  99. package/lib/processor/completer/Completer.js +92 -0
  100. package/lib/processor/completer/builtin.d.ts +30 -0
  101. package/lib/processor/completer/builtin.js +172 -0
  102. package/lib/processor/completer/index.d.ts +3 -0
  103. package/lib/processor/completer/index.js +28 -0
  104. package/lib/processor/formatter/Formatter.d.ts +6 -0
  105. package/lib/processor/formatter/Formatter.js +19 -0
  106. package/lib/processor/formatter/builtin.d.ts +15 -0
  107. package/lib/processor/formatter/builtin.js +60 -0
  108. package/lib/processor/formatter/index.d.ts +3 -0
  109. package/lib/processor/formatter/index.js +28 -0
  110. package/lib/processor/index.d.ts +10 -0
  111. package/lib/processor/index.js +22 -0
  112. package/lib/processor/linter/Linter.d.ts +4 -0
  113. package/lib/processor/linter/Linter.js +3 -0
  114. package/lib/processor/linter/builtin/undeclaredSymbol.d.ts +4 -0
  115. package/lib/processor/linter/builtin/undeclaredSymbol.js +73 -0
  116. package/lib/processor/linter/builtin.d.ts +15 -0
  117. package/lib/processor/linter/builtin.js +120 -0
  118. package/lib/processor/linter/index.d.ts +3 -0
  119. package/lib/processor/linter/index.js +28 -0
  120. package/lib/processor/util.d.ts +17 -0
  121. package/lib/processor/util.js +32 -0
  122. package/lib/service/CacheService.d.ts +54 -0
  123. package/lib/service/CacheService.js +195 -0
  124. package/lib/service/CommandExecutor.d.ts +3 -0
  125. package/lib/service/CommandExecutor.js +7 -0
  126. package/lib/service/Config.d.ts +218 -0
  127. package/lib/service/Config.js +264 -0
  128. package/lib/service/Context.d.ts +143 -0
  129. package/lib/service/Context.js +143 -0
  130. package/lib/service/Dependency.d.ts +10 -0
  131. package/lib/service/Dependency.js +11 -0
  132. package/lib/service/Downloader.d.ts +67 -0
  133. package/lib/service/Downloader.js +185 -0
  134. package/lib/service/ErrorReporter.d.ts +27 -0
  135. package/lib/service/ErrorReporter.js +49 -0
  136. package/lib/service/FileService.d.ts +96 -0
  137. package/lib/service/FileService.js +224 -0
  138. package/lib/service/Hover.d.ts +10 -0
  139. package/lib/service/Hover.js +16 -0
  140. package/lib/service/Logger.d.ts +31 -0
  141. package/lib/service/Logger.js +30 -0
  142. package/lib/service/MetaRegistry.d.ts +106 -0
  143. package/lib/service/MetaRegistry.js +192 -0
  144. package/lib/service/Operations.d.ts +8 -0
  145. package/lib/service/Operations.js +26 -0
  146. package/lib/service/Profiler.d.ts +33 -0
  147. package/lib/service/Profiler.js +76 -0
  148. package/lib/service/Project.d.ts +173 -0
  149. package/lib/service/Project.js +593 -0
  150. package/lib/service/Service.d.ts +55 -0
  151. package/lib/service/Service.js +207 -0
  152. package/lib/service/SymbolLocations.d.ts +17 -0
  153. package/lib/service/SymbolLocations.js +16 -0
  154. package/lib/service/SymbolRegistrar.d.ts +5 -0
  155. package/lib/service/SymbolRegistrar.js +3 -0
  156. package/lib/service/fileUtil.d.ts +121 -0
  157. package/lib/service/fileUtil.js +268 -0
  158. package/lib/service/index.d.ts +18 -0
  159. package/lib/service/index.js +33 -0
  160. package/lib/source/IndexMap.d.ts +16 -0
  161. package/lib/source/IndexMap.js +46 -0
  162. package/lib/source/LanguageError.d.ts +27 -0
  163. package/lib/source/LanguageError.js +15 -0
  164. package/lib/source/Location.d.ts +19 -0
  165. package/lib/source/Location.js +25 -0
  166. package/lib/source/Offset.d.ts +13 -0
  167. package/lib/source/Offset.js +25 -0
  168. package/lib/source/Position.d.ts +23 -0
  169. package/lib/source/Position.js +43 -0
  170. package/lib/source/PositionRange.d.ts +38 -0
  171. package/lib/source/PositionRange.js +83 -0
  172. package/lib/source/Range.d.ts +68 -0
  173. package/lib/source/Range.js +155 -0
  174. package/lib/source/Source.d.ts +110 -0
  175. package/lib/source/Source.js +222 -0
  176. package/lib/source/index.d.ts +9 -0
  177. package/lib/source/index.js +21 -0
  178. package/lib/symbol/Symbol.d.ts +219 -0
  179. package/lib/symbol/Symbol.js +324 -0
  180. package/lib/symbol/SymbolUtil.d.ts +407 -0
  181. package/lib/symbol/SymbolUtil.js +880 -0
  182. package/lib/symbol/UriBinder.d.ts +3 -0
  183. package/lib/symbol/UriBinder.js +3 -0
  184. package/lib/symbol/index.d.ts +4 -0
  185. package/lib/symbol/index.js +16 -0
  186. package/package.json +41 -0
@@ -0,0 +1,22 @@
1
+ import type { AstNode } from '../node';
2
+ import type { RecordNode } from '../node/RecordNode';
3
+ import type { InfallibleParser, Parser } from './Parser';
4
+ /** @internal For test only */
5
+ export interface Options<K extends AstNode, V extends AstNode> {
6
+ start: string;
7
+ pair: {
8
+ key: Parser<K>;
9
+ sep: string;
10
+ value: Parser<V> | {
11
+ get: (recordResult: RecordNode<K, V>, keyResult: K | undefined) => Parser<V>;
12
+ };
13
+ end: string;
14
+ trailingEnd: boolean;
15
+ };
16
+ end: string;
17
+ }
18
+ /**
19
+ * @returns A parser that parses something coming in a key-value pair form. e.g. SNBT objects, entity selector arguments.
20
+ */
21
+ export declare function record<K extends AstNode, V extends AstNode>({ start, pair, end }: Options<K, V>): InfallibleParser<RecordNode<K, V>>;
22
+ //# sourceMappingURL=record.d.ts.map
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.record = void 0;
4
+ const locales_1 = require("@spyglassmc/locales");
5
+ const source_1 = require("../source");
6
+ const Parser_1 = require("./Parser");
7
+ const util_1 = require("./util");
8
+ /**
9
+ * @returns A parser that parses something coming in a key-value pair form. e.g. SNBT objects, entity selector arguments.
10
+ */
11
+ function record({ start, pair, end }) {
12
+ return (src, ctx) => {
13
+ const ans = {
14
+ type: 'record',
15
+ range: source_1.Range.create(src),
16
+ children: [],
17
+ };
18
+ if (src.trySkip(start)) {
19
+ src.skipWhitespace();
20
+ let requiresPairEnd = false;
21
+ let hasPairEnd = false;
22
+ while (src.canRead() && src.peek(end.length) !== end) {
23
+ const pairStart = src.cursor;
24
+ let key;
25
+ let value;
26
+ // Pair end of the last pair.
27
+ if (requiresPairEnd && !hasPairEnd) {
28
+ ctx.err.report((0, locales_1.localize)('expected', (0, locales_1.localeQuote)(pair.end)), src);
29
+ }
30
+ // Key.
31
+ const keyStart = src.cursor;
32
+ const { result: keyResult, updateSrcAndCtx: updateForKey, endCursor: keyEnd } = (0, util_1.attempt)(pair.key, src, ctx);
33
+ if (keyResult === Parser_1.Failure || (keyEnd - keyStart === 0 && ![pair.sep, pair.end, end, '\r', '\n', '\t', ' '].includes(src.peek()))) {
34
+ ctx.err.report((0, locales_1.localize)('expected', (0, locales_1.localize)('parser.record.key')), source_1.Range.create(src, () => src.skipUntilOrEnd(pair.sep, pair.end, end, '\r', '\n')));
35
+ }
36
+ else {
37
+ updateForKey();
38
+ key = keyResult;
39
+ }
40
+ // K-V sep.
41
+ let sepCharRange = undefined;
42
+ src.skipWhitespace();
43
+ if (src.peek(pair.sep.length) === pair.sep) {
44
+ sepCharRange = source_1.Range.create(src, () => src.skip(pair.sep.length));
45
+ }
46
+ else {
47
+ ctx.err.report((0, locales_1.localize)('expected', (0, locales_1.localeQuote)(pair.sep)), src);
48
+ }
49
+ // Value.
50
+ src.skipWhitespace();
51
+ const valueParser = typeof pair.value === 'function' ? pair.value : pair.value.get(ans, key);
52
+ const valueStart = src.cursor;
53
+ const { result: valueResult, updateSrcAndCtx: updateForValue, endCursor: valueEnd } = (0, util_1.attempt)(valueParser, src, ctx);
54
+ if (valueResult === Parser_1.Failure || (valueEnd - valueStart === 0 && ![pair.sep, pair.end, end, '\r', '\n', '\t', ' '].includes(src.peek()))) {
55
+ ctx.err.report((0, locales_1.localize)('expected', (0, locales_1.localize)('parser.record.value')), source_1.Range.create(src, () => src.skipUntilOrEnd(pair.sep, pair.end, end, '\r', '\n')));
56
+ }
57
+ else {
58
+ updateForValue();
59
+ value = valueResult;
60
+ }
61
+ // Pair end.
62
+ let endCharRange = undefined;
63
+ src.skipWhitespace();
64
+ requiresPairEnd = true;
65
+ if (hasPairEnd = src.peek(pair.end.length) === pair.end) {
66
+ endCharRange = source_1.Range.create(src, () => src.skip(pair.end.length));
67
+ }
68
+ // Create pair.
69
+ ans.children.push({
70
+ type: 'pair',
71
+ range: source_1.Range.create(pairStart, src),
72
+ ...(key || value) ? { children: [key, value].filter(v => !!v) } : {},
73
+ key,
74
+ sep: sepCharRange,
75
+ value,
76
+ end: endCharRange,
77
+ });
78
+ src.skipWhitespace();
79
+ }
80
+ // Trailing pair end.
81
+ if (hasPairEnd && !pair.trailingEnd) {
82
+ ctx.err.report((0, locales_1.localize)('parser.record.trailing-end'), ans.children[ans.children.length - 1].end);
83
+ }
84
+ // End.
85
+ if (!src.trySkip(end)) {
86
+ ctx.err.report((0, locales_1.localize)('expected', (0, locales_1.localeQuote)(end)), src);
87
+ }
88
+ }
89
+ else {
90
+ ctx.err.report((0, locales_1.localize)('expected', (0, locales_1.localeQuote)(start)), src);
91
+ }
92
+ ans.range.end = src.cursor;
93
+ return ans;
94
+ };
95
+ }
96
+ exports.record = record;
97
+ //# sourceMappingURL=record.js.map
@@ -0,0 +1,4 @@
1
+ import type { ResourceLocationNode, ResourceLocationOptions } from '../node';
2
+ import type { InfallibleParser } from './Parser';
3
+ export declare function resourceLocation(options: ResourceLocationOptions): InfallibleParser<ResourceLocationNode>;
4
+ //# sourceMappingURL=resourceLocation.d.ts.map
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resourceLocation = void 0;
4
+ const locales_1 = require("@spyglassmc/locales");
5
+ const common_1 = require("../common");
6
+ const source_1 = require("../source");
7
+ const Terminators = new Set([' ', '\r', '\n', '=', ',', '"', "'", '{', '}', '[', ']', '(', ')', ';']);
8
+ const LegalCharacters = new Set([
9
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
10
+ 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
11
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_', '-', '.',
12
+ ]);
13
+ function resourceLocation(options) {
14
+ return (src, ctx) => {
15
+ const ans = {
16
+ type: 'resource_location',
17
+ range: source_1.Range.create(src),
18
+ options,
19
+ };
20
+ if (src.trySkip(common_1.ResourceLocation.TagPrefix)) {
21
+ ans.isTag = true;
22
+ }
23
+ const start = src.cursor;
24
+ while (src.canReadInLine() && !Terminators.has(src.peek())) {
25
+ src.skip();
26
+ }
27
+ const raw = src.sliceToCursor(start);
28
+ ans.range.end = src.cursor;
29
+ if (raw.length === 0) {
30
+ ctx.err.report((0, locales_1.localize)('expected', (0, locales_1.localize)('resource-location')), ans);
31
+ }
32
+ else {
33
+ const sepIndex = raw.indexOf(options.namespacePathSep ?? common_1.ResourceLocation.NamespacePathSep);
34
+ if (sepIndex >= 0) {
35
+ ans.namespace = raw.slice(0, sepIndex);
36
+ }
37
+ const rawPath = raw.slice(sepIndex + 1);
38
+ ans.path = rawPath.split(common_1.ResourceLocation.PathSep);
39
+ // Check characters.
40
+ /* istanbul ignore next */
41
+ const illegalChars = [...new Set([
42
+ ...[...ans.namespace ?? []].filter(c => !LegalCharacters.has(c)),
43
+ ...[...rawPath].filter(c => c !== '/' && !LegalCharacters.has(c)),
44
+ ])];
45
+ if (illegalChars.length) {
46
+ ctx.err.report((0, locales_1.localize)('parser.resource-location.illegal', (0, locales_1.arrayToMessage)(illegalChars, true, 'and')), ans);
47
+ }
48
+ if (ans.isTag && !options.allowTag) {
49
+ ctx.err.report((0, locales_1.localize)('parser.resource-location.tag-diallowed'), ans);
50
+ }
51
+ if (!ans.namespace && options.isPredicate) {
52
+ ctx.err.report((0, locales_1.localize)('parser.resource-location.namespace-expected'), ans);
53
+ }
54
+ if (options.category) {
55
+ const fullRaw = common_1.ResourceLocation.lengthen(options.namespacePathSep === '.'
56
+ ? raw.replace('.', common_1.ResourceLocation.NamespacePathSep)
57
+ : raw);
58
+ ctx.symbols
59
+ .query(ctx.doc, ans.isTag ? `tag/${options.category}` : options.category, fullRaw)
60
+ .enter({ usage: { type: options.usageType, node: ans, accessType: options.accessType } });
61
+ }
62
+ }
63
+ return ans;
64
+ };
65
+ }
66
+ exports.resourceLocation = resourceLocation;
67
+ //# sourceMappingURL=resourceLocation.js.map
@@ -0,0 +1,18 @@
1
+ import type { StringNode, StringOptions } from '../node';
2
+ import type { InfallibleParser } from '../parser';
3
+ import type { ParserContext } from '../service';
4
+ import type { IndexMap } from '../source';
5
+ import type { Parser, Result, Returnable } from './Parser';
6
+ export declare function string(options: StringOptions): InfallibleParser<StringNode>;
7
+ export declare function parseStringValue<T extends Returnable>(parser: Parser<T>, value: string, map: IndexMap, ctx: ParserContext): Result<T>;
8
+ export declare const BrigadierUnquotableCharacters: readonly ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "_", ".", "+", "-"];
9
+ export declare const BrigadierUnquotableCharacterSet: Set<"0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "b" | "B" | "d" | "D" | "f" | "F" | "l" | "L" | "s" | "S" | "." | "i" | "p" | "-" | "+" | "e" | "a" | "c" | "g" | "h" | "j" | "k" | "m" | "n" | "o" | "q" | "r" | "t" | "u" | "v" | "w" | "x" | "y" | "z" | "_" | "A" | "C" | "E" | "G" | "H" | "I" | "J" | "K" | "M" | "N" | "O" | "P" | "Q" | "R" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z">;
10
+ export declare const BrigadierUnquotablePattern: RegExp;
11
+ export declare const BrigadierUnquotableOption: {
12
+ allowEmpty: boolean;
13
+ allowList: Set<"0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "b" | "B" | "d" | "D" | "f" | "F" | "l" | "L" | "s" | "S" | "." | "i" | "p" | "-" | "+" | "e" | "a" | "c" | "g" | "h" | "j" | "k" | "m" | "n" | "o" | "q" | "r" | "t" | "u" | "v" | "w" | "x" | "y" | "z" | "_" | "A" | "C" | "E" | "G" | "H" | "I" | "J" | "K" | "M" | "N" | "O" | "P" | "Q" | "R" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z">;
14
+ };
15
+ export declare const BrigadierStringOptions: StringOptions;
16
+ export declare const brigadierString: InfallibleParser<StringNode>;
17
+ export declare function isAllowedCharacter(c: string, options: Exclude<StringOptions['unquotable'], false | undefined>): boolean;
18
+ //# sourceMappingURL=string.d.ts.map
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isAllowedCharacter = exports.brigadierString = exports.BrigadierStringOptions = exports.BrigadierUnquotableOption = exports.BrigadierUnquotablePattern = exports.BrigadierUnquotableCharacterSet = exports.BrigadierUnquotableCharacters = exports.parseStringValue = exports.string = void 0;
4
+ const locales_1 = require("@spyglassmc/locales");
5
+ const vscode_languageserver_textdocument_1 = require("vscode-languageserver-textdocument");
6
+ const node_1 = require("../node");
7
+ const source_1 = require("../source");
8
+ const Parser_1 = require("./Parser");
9
+ function string(options) {
10
+ return (src, ctx) => {
11
+ const ans = {
12
+ type: 'string',
13
+ range: source_1.Range.create(src),
14
+ options,
15
+ value: '',
16
+ valueMap: [],
17
+ };
18
+ let start = src.cursor;
19
+ if (options.quotes?.length && (src.peek() === '"' || src.peek() === "'")) {
20
+ const currentQuote = src.read();
21
+ const contentStart = src.cursor;
22
+ while (src.canRead() && src.peek() !== currentQuote) {
23
+ const cStart = src.cursor;
24
+ const c = src.read();
25
+ if (options.escapable && c === '\\') {
26
+ const c2 = src.read();
27
+ if (c2 === '\\' || c2 === currentQuote || node_1.EscapeChar.is(options.escapable.characters, c2)) {
28
+ ans.valueMap.push({
29
+ inner: source_1.Range.create(ans.value.length, ans.value.length + 1),
30
+ outer: source_1.Range.create(cStart, src),
31
+ });
32
+ ans.value += node_1.EscapeTable.get(c2);
33
+ }
34
+ else if (options.escapable.unicode && c2 === 'u') {
35
+ const hex = src.peek(4);
36
+ if (/^[0-9a-f]{4}$/i.test(hex)) {
37
+ src.skip(4);
38
+ ans.valueMap.push({
39
+ inner: source_1.Range.create(ans.value.length, ans.value.length + 1),
40
+ outer: source_1.Range.create(cStart, src),
41
+ });
42
+ ans.value += String.fromCharCode(parseInt(hex, 16));
43
+ }
44
+ else {
45
+ ctx.err.report((0, locales_1.localize)('parser.string.illegal-unicode-escape'), source_1.Range.create(src, src.getCharRange(3).end));
46
+ ans.valueMap.push({
47
+ inner: source_1.Range.create(ans.value.length, ans.value.length + 1),
48
+ outer: source_1.Range.create(cStart, src),
49
+ });
50
+ ans.value += c2;
51
+ }
52
+ }
53
+ else {
54
+ if (!options.escapable.allowUnknown) {
55
+ ctx.err.report((0, locales_1.localize)('parser.string.illegal-escape', (0, locales_1.localeQuote)(c2)), src.getCharRange(-1));
56
+ }
57
+ ans.valueMap.push({
58
+ inner: source_1.Range.create(ans.value.length, ans.value.length + 1),
59
+ outer: source_1.Range.create(cStart, src),
60
+ });
61
+ ans.value += c2;
62
+ }
63
+ }
64
+ else {
65
+ ans.value += c;
66
+ }
67
+ }
68
+ if (!src.trySkip(currentQuote)) {
69
+ ctx.err.report((0, locales_1.localize)('expected', (0, locales_1.localeQuote)(currentQuote)), src);
70
+ }
71
+ if (!options.quotes.includes(currentQuote)) {
72
+ ctx.err.report((0, locales_1.localize)('parser.string.illegal-quote', options.quotes), ans);
73
+ }
74
+ start = contentStart;
75
+ }
76
+ else if (options.unquotable) {
77
+ while (src.canRead() && isAllowedCharacter(src.peek(), options.unquotable)) {
78
+ ans.value += src.read();
79
+ }
80
+ if (!ans.value && !options.unquotable.allowEmpty) {
81
+ ctx.err.report((0, locales_1.localize)('expected', (0, locales_1.localize)('string')), src);
82
+ }
83
+ }
84
+ else {
85
+ ctx.err.report((0, locales_1.localize)('expected', options.quotes), src);
86
+ }
87
+ ans.valueMap.unshift({ inner: source_1.Range.create(0), outer: source_1.Range.create(start) });
88
+ if (options.value?.parser) {
89
+ const valueResult = parseStringValue(options.value.parser, ans.value, ans.valueMap, ctx);
90
+ /* istanbul ignore else */
91
+ if (valueResult !== Parser_1.Failure) {
92
+ ans.children = [valueResult];
93
+ }
94
+ }
95
+ ans.range.end = src.cursor;
96
+ return ans;
97
+ };
98
+ }
99
+ exports.string = string;
100
+ function parseStringValue(parser, value, map, ctx) {
101
+ const valueSrc = new source_1.Source(value, map);
102
+ const valueCtx = {
103
+ ...ctx,
104
+ doc: vscode_languageserver_textdocument_1.TextDocument.create(ctx.doc.uri, ctx.doc.languageId, ctx.doc.version, value),
105
+ };
106
+ // TODO: Mark trailing string as errors.
107
+ return parser(valueSrc, valueCtx);
108
+ }
109
+ exports.parseStringValue = parseStringValue;
110
+ exports.BrigadierUnquotableCharacters = Object.freeze([
111
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
112
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
113
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
114
+ '_', '.', '+', '-',
115
+ ]);
116
+ exports.BrigadierUnquotableCharacterSet = new Set(exports.BrigadierUnquotableCharacters);
117
+ exports.BrigadierUnquotablePattern = /^[0-9A-Za-z_\.\+\-]*$/;
118
+ exports.BrigadierUnquotableOption = { allowEmpty: true, allowList: exports.BrigadierUnquotableCharacterSet };
119
+ exports.BrigadierStringOptions = {
120
+ escapable: {},
121
+ quotes: ['"', "'"],
122
+ unquotable: exports.BrigadierUnquotableOption,
123
+ };
124
+ exports.brigadierString = string(exports.BrigadierStringOptions);
125
+ function isAllowedCharacter(c, options) {
126
+ return options.allowList?.has(c) ?? !options.blockList?.has(c);
127
+ }
128
+ exports.isAllowedCharacter = isAllowedCharacter;
129
+ //# sourceMappingURL=string.js.map
@@ -0,0 +1,12 @@
1
+ import type { SymbolNode, SymbolOptions } from '../node/SymbolNode';
2
+ import type { AllCategory } from '../symbol';
3
+ import type { InfallibleParser } from './Parser';
4
+ /**
5
+ * This parser reads _everything_ until the end of the {@link Source}.
6
+ *
7
+ * You might want to use {@link acceptOnly}, {@link stopBefore}, or {@link string} to restrict what this parser can read.
8
+ */
9
+ export declare function symbol(category: AllCategory): InfallibleParser<SymbolNode>;
10
+ export declare function symbol(category: string): InfallibleParser<SymbolNode>;
11
+ export declare function symbol(options: SymbolOptions): InfallibleParser<SymbolNode>;
12
+ //# sourceMappingURL=symbol.d.ts.map
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.symbol = void 0;
4
+ const source_1 = require("../source");
5
+ function symbol(param) {
6
+ const options = getOptions(param);
7
+ return (src, ctx) => {
8
+ const ans = {
9
+ type: 'symbol',
10
+ range: source_1.Range.create(src),
11
+ options,
12
+ value: src.readRemaining(),
13
+ };
14
+ if (ans.value) {
15
+ const path = options.parentPath ? [...options.parentPath, ans.value] : [ans.value];
16
+ ctx.symbols
17
+ .query(ctx.doc, options.category, ...path)
18
+ .enter({ usage: { type: options.usageType, node: ans, accessType: options.accessType } });
19
+ }
20
+ ans.range.end = src.cursor;
21
+ return ans;
22
+ };
23
+ }
24
+ exports.symbol = symbol;
25
+ function getOptions(param) {
26
+ if (typeof param === 'string') {
27
+ return { category: param };
28
+ }
29
+ else {
30
+ return param;
31
+ }
32
+ }
33
+ //# sourceMappingURL=symbol.js.map
@@ -0,0 +1,22 @@
1
+ import type { AstNode } from '../node';
2
+ import type { TableNode } from '../node/RecordNode';
3
+ import type { InfallibleParser, Parser } from './Parser';
4
+ /** @internal For test only */
5
+ export interface Options<K extends AstNode, V extends AstNode> {
6
+ start: string;
7
+ pair: {
8
+ key: Parser<K>;
9
+ sep: string;
10
+ value: Parser<V> | {
11
+ get: (tableResult: TableNode<K, V>, keyResult: K | undefined) => Parser<V>;
12
+ };
13
+ end: string;
14
+ trailingEnd: boolean;
15
+ };
16
+ end: string;
17
+ }
18
+ /**
19
+ * @returns A parser that parses something coming in a key-value pair form. e.g. SNBT objects, entity selector arguments.
20
+ */
21
+ export declare function table<K extends AstNode, V extends AstNode>({ start, pair, end }: Options<K, V>): InfallibleParser<TableNode<K, V>>;
22
+ //# sourceMappingURL=table.d.ts.map
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.table = void 0;
4
+ const locales_1 = require("@spyglassmc/locales");
5
+ const source_1 = require("../source");
6
+ const Parser_1 = require("./Parser");
7
+ const util_1 = require("./util");
8
+ /**
9
+ * @returns A parser that parses something coming in a key-value pair form. e.g. SNBT objects, entity selector arguments.
10
+ */
11
+ function table({ start, pair, end }) {
12
+ return (src, ctx) => {
13
+ const ans = {
14
+ type: 'table',
15
+ range: source_1.Range.create(src),
16
+ children: [],
17
+ };
18
+ if (src.trySkip(start)) {
19
+ src.skipWhitespace();
20
+ let requiresPairEnd = false;
21
+ let hasPairEnd = false;
22
+ while (src.canRead() && src.peek(end.length) !== end) {
23
+ const pairStart = src.cursor;
24
+ let key;
25
+ let value;
26
+ // Pair end of the last pair.
27
+ if (requiresPairEnd && !hasPairEnd) {
28
+ ctx.err.report((0, locales_1.localize)('expected', (0, locales_1.localeQuote)(pair.end)), src);
29
+ }
30
+ // Key.
31
+ const keyStart = src.cursor;
32
+ const { result: keyResult, updateSrcAndCtx: updateForKey, endCursor: keyEnd } = (0, util_1.attempt)(pair.key, src, ctx);
33
+ if (keyResult === Parser_1.Failure || (keyEnd - keyStart === 0 && ![pair.sep, pair.end, end, '\r', '\n', '\t', ' '].includes(src.peek()))) {
34
+ ctx.err.report((0, locales_1.localize)('expected', (0, locales_1.localize)('parser.table.key')), source_1.Range.create(src, () => src.skipUntilOrEnd(pair.sep, pair.end, end, '\r', '\n')));
35
+ }
36
+ else {
37
+ updateForKey();
38
+ key = keyResult;
39
+ }
40
+ // K-V sep.
41
+ let sepCharRange = undefined;
42
+ src.skipWhitespace();
43
+ if (src.peek(pair.sep.length) === pair.sep) {
44
+ sepCharRange = source_1.Range.create(src, () => src.skip(pair.sep.length));
45
+ }
46
+ else {
47
+ ctx.err.report((0, locales_1.localize)('expected', (0, locales_1.localeQuote)(pair.sep)), src);
48
+ }
49
+ // Value.
50
+ src.skipWhitespace();
51
+ const valueParser = typeof pair.value === 'function' ? pair.value : pair.value.get(ans, key);
52
+ const valueStart = src.cursor;
53
+ const { result: valueResult, updateSrcAndCtx: updateForValue, endCursor: valueEnd } = (0, util_1.attempt)(valueParser, src, ctx);
54
+ if (valueResult === Parser_1.Failure || (valueEnd - valueStart === 0 && ![pair.sep, pair.end, end, '\r', '\n', '\t', ' '].includes(src.peek()))) {
55
+ ctx.err.report((0, locales_1.localize)('expected', (0, locales_1.localize)('parser.table.value')), source_1.Range.create(src, () => src.skipUntilOrEnd(pair.sep, pair.end, end, '\r', '\n')));
56
+ }
57
+ else {
58
+ updateForValue();
59
+ value = valueResult;
60
+ }
61
+ // Pair end.
62
+ let endCharRange = undefined;
63
+ src.skipWhitespace();
64
+ requiresPairEnd = true;
65
+ if (hasPairEnd = src.peek(pair.end.length) === pair.end) {
66
+ endCharRange = source_1.Range.create(src, () => src.skip(pair.end.length));
67
+ }
68
+ // Create pair.
69
+ ans.children.push({
70
+ type: 'pair',
71
+ range: source_1.Range.create(pairStart, src),
72
+ ...(key || value) ? { children: [key, value].filter(v => !!v) } : {},
73
+ key,
74
+ sep: sepCharRange,
75
+ value,
76
+ end: endCharRange,
77
+ });
78
+ src.skipWhitespace();
79
+ }
80
+ // Trailing pair end.
81
+ if (hasPairEnd && !pair.trailingEnd) {
82
+ ctx.err.report((0, locales_1.localize)('parser.table.trailing-end'), ans.children[ans.children.length - 1].end);
83
+ }
84
+ // End.
85
+ if (!src.trySkip(end)) {
86
+ ctx.err.report((0, locales_1.localize)('expected', (0, locales_1.localeQuote)(end)), src);
87
+ }
88
+ }
89
+ else {
90
+ ctx.err.report((0, locales_1.localize)('expected', (0, locales_1.localeQuote)(start)), src);
91
+ }
92
+ ans.range.end = src.cursor;
93
+ return ans;
94
+ };
95
+ }
96
+ exports.table = table;
97
+ //# sourceMappingURL=table.js.map
@@ -0,0 +1,134 @@
1
+ import type { AstNode } from '../node';
2
+ import { SequenceUtil } from '../node';
3
+ import type { ParserContext } from '../service';
4
+ import type { ErrorSeverity, ReadonlySource, Source } from '../source';
5
+ import type { InfallibleParser, Parser, Result, Returnable } from './Parser';
6
+ declare type ExtractNodeType<P extends Parser<Returnable>> = P extends Parser<infer V> ? V : never;
7
+ /**
8
+ * @template PA Parser array.
9
+ */
10
+ declare type ExtractNodeTypes<PA extends Parser<Returnable>[]> = ExtractNodeType<PA[number]>;
11
+ export interface AttemptResult<N extends Returnable = AstNode> {
12
+ result: Result<N>;
13
+ updateSrcAndCtx: () => void;
14
+ endCursor: number;
15
+ errorAmount: number;
16
+ }
17
+ interface InfallibleAttemptResult<N extends Returnable = AstNode> extends AttemptResult<N> {
18
+ result: N;
19
+ }
20
+ /**
21
+ * Attempts to parse using the given `parser`.
22
+ * @returns
23
+ * - `result`: The result returned by the `parser`.
24
+ * - `updateSrcAndCtx`: A function that will update the passed-in `src` and `ctx` to the state where `parser` has been executed.
25
+ * - `endCursor`: The offset where the `parser` stopped parsing.
26
+ * - `errorAmount`: The amount of errors created by the `parser`.
27
+ */
28
+ export declare function attempt<N extends Returnable = AstNode>(parser: InfallibleParser<N>, src: Source, ctx: ParserContext): InfallibleAttemptResult<N>;
29
+ export declare function attempt<N extends Returnable = AstNode>(parser: Parser<N>, src: Source, ctx: ParserContext): AttemptResult<N>;
30
+ declare type SP<CN extends AstNode> = SIP<CN> | Parser<CN | SequenceUtil<CN> | undefined> | {
31
+ get: (result: SequenceUtil<CN>) => Parser<CN | SequenceUtil<CN> | undefined> | undefined;
32
+ };
33
+ declare type SIP<CN extends AstNode> = InfallibleParser<CN | SequenceUtil<CN> | undefined> | {
34
+ get: (result: SequenceUtil<CN>) => InfallibleParser<CN | SequenceUtil<CN> | undefined> | undefined;
35
+ };
36
+ /**
37
+ * @template GN Gap node.
38
+ * @template PA Parser array.
39
+ *
40
+ * @param parseGap A parser that parses gaps between nodes in the sequence.
41
+ *
42
+ * @returns A parser that takes a sequence built with the passed-in parsers.
43
+ *
44
+ * `Failure` when any of the `parsers` returns a `Failure`.
45
+ */
46
+ export declare function sequence<GN extends AstNode = never, PA extends SIP<AstNode>[] = SIP<AstNode>[]>(parsers: PA, parseGap?: InfallibleParser<GN[]>): InfallibleParser<SequenceUtil<GN | {
47
+ [K in number]: PA[K] extends SP<infer V> ? V : never;
48
+ }[number]>>;
49
+ export declare function sequence<GN extends AstNode = never, PA extends SP<AstNode>[] = SP<AstNode>[]>(parsers: PA, parseGap?: InfallibleParser<GN[]>): Parser<SequenceUtil<GN | {
50
+ [K in number]: PA[K] extends SP<infer V> ? V : never;
51
+ }[number]>>;
52
+ /**
53
+ * @template CN Child node.
54
+ *
55
+ * @param parser Must be fallible, as an infallible parser being repeated will result in an infinite loop.
56
+ * @param parseGap A parser that parses gaps between nodes in the sequence.
57
+ *
58
+ * @returns A parser that takes a sequence with the passed-in parser being repeated zero or more times.
59
+ */
60
+ export declare function repeat<CN extends AstNode>(parser: InfallibleParser<CN | SequenceUtil<CN>>, parseGap?: InfallibleParser<CN[]>): void;
61
+ export declare function repeat<CN extends AstNode>(parser: Parser<CN | SequenceUtil<CN>>, parseGap?: InfallibleParser<CN[]>): InfallibleParser<SequenceUtil<CN>>;
62
+ export interface AnyOutObject {
63
+ /**
64
+ * The index of the parser in the provided `parsers` array that was ultimately taken. `-1` if all parsers failed.
65
+ */
66
+ index: number;
67
+ }
68
+ /**
69
+ * @param out An optional object that will be modified with additional information if provided.
70
+ *
71
+ * @returns A parser that returns the result of the passed-in parser which produces the least amount of error.
72
+ * If there are more than one `parsers` produced the same amount of errors, it then takes the parser that went the furthest in `Source`.
73
+ * If there is still a tie, it takes the one closer to the beginning of the `parsers` array.
74
+ *
75
+ * `Failure` when all of the `parsers` failed.
76
+ */
77
+ export declare function any<PA extends [...Parser<Returnable>[], InfallibleParser<Returnable>]>(parsers: PA, out?: AnyOutObject): InfallibleParser<ExtractNodeTypes<PA>>;
78
+ export declare function any<PA extends Parser<Returnable>[]>(parsers: PA, out?: AnyOutObject): Parser<ExtractNodeTypes<PA>>;
79
+ /**
80
+ * @returns A parser that fails when the passed-in parser didn't move the cursor at all.
81
+ */
82
+ export declare function failOnEmpty<T extends Returnable>(parser: Parser<T>): Parser<T>;
83
+ /**
84
+ * @returns A parser that takes an optional syntax component.
85
+ */
86
+ export declare function optional<N extends Returnable>(parser: InfallibleParser<N>): void;
87
+ export declare function optional<N extends Returnable>(parser: Parser<N>): InfallibleParser<N | undefined>;
88
+ /**
89
+ * @param parser Must be fallible.
90
+ *
91
+ * @returns A parser that returns the return value of the `parser`, or the return value of `defaultValue` it it's a `Failure`.
92
+ */
93
+ export declare function recover<N extends Returnable>(parser: InfallibleParser<N>, defaultValue: (src: Source, ctx: ParserContext) => N): void;
94
+ export declare function recover<N extends Returnable>(parser: Parser<N>, defaultValue: (src: Source, ctx: ParserContext) => N): InfallibleParser<N>;
95
+ declare type Case = {
96
+ predicate?: (this: void, src: ReadonlySource) => boolean;
97
+ parser: Parser<Returnable>;
98
+ };
99
+ /**
100
+ * @template CA Case array.
101
+ */
102
+ export declare function select<CA extends readonly Case[]>(cases: CA): Parser<ExtractNodeType<CA[number]['parser']>>;
103
+ /**
104
+ * @returns A parser that returns the return value of `fn`.
105
+ *
106
+ * `Failure` when the `parser` returns a `Failure`.
107
+ */
108
+ export declare function map<NA extends Returnable, NB extends Returnable = NA>(parser: InfallibleParser<NA>, fn: (res: NA, src: Source, ctx: ParserContext) => NB): InfallibleParser<NB>;
109
+ export declare function map<NA extends Returnable, NB extends Returnable = NA>(parser: Parser<NA>, fn: (res: NA, src: Source, ctx: ParserContext) => NB): Parser<NB>;
110
+ export declare function setType<N extends AstNode, T extends string>(type: T, parser: InfallibleParser<N>): InfallibleParser<Omit<N, 'type'> & {
111
+ type: T;
112
+ }>;
113
+ export declare function setType<N extends AstNode, T extends string>(type: T, parser: Parser<N>): Parser<Omit<N, 'type'> & {
114
+ type: T;
115
+ }>;
116
+ /**
117
+ * Checks if the result of `parser` is valid, and reports an error if it's not.
118
+ *
119
+ * `Failure` when the `parser` returns a `Failure`.
120
+ */
121
+ export declare function validate<N extends AstNode>(parser: InfallibleParser<N>, validator: (res: N, src: Source, ctx: ParserContext) => boolean, message: string, severity?: ErrorSeverity): InfallibleParser<N>;
122
+ export declare function validate<N extends AstNode>(parser: Parser<N>, validator: (res: N, src: Source, ctx: ParserContext) => boolean, message: string, severity?: ErrorSeverity): Parser<N>;
123
+ /**
124
+ * @returns A parser that is based on the passed-in `parser`, but will never read to any of the terminator strings.
125
+ */
126
+ export declare function stopBefore<N extends Returnable>(parser: InfallibleParser<N>, ...teminators: (string | readonly string[])[]): InfallibleParser<N>;
127
+ export declare function stopBefore<N extends Returnable>(parser: Parser<N>, ...teminators: (string | readonly string[])[]): Parser<N>;
128
+ /**
129
+ * @returns A parser that is based on the passed-in `parser`, but will only read the acceptable characters.
130
+ */
131
+ export declare function acceptOnly<N extends Returnable>(parser: InfallibleParser<N>, ...characters: (string | readonly string[])[]): InfallibleParser<N>;
132
+ export declare function acceptOnly<N extends Returnable>(parser: Parser<N>, ...characters: (string | readonly string[])[]): Parser<N>;
133
+ export {};
134
+ //# sourceMappingURL=util.d.ts.map