@spyglassmc/mcdoc 0.1.1 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,32 +1,43 @@
1
- import type { FullResourceLocation, ProcessorContext, SymbolPath } from '@spyglassmc/core';
2
- import type { EnumKind } from '../node';
1
+ import type { FullResourceLocation, ProcessorContext } from '@spyglassmc/core';
2
+ import type { EnumKind, RangeKind } from '../node/index.js';
3
3
  export interface Attribute {
4
4
  name: string;
5
- value: AttributeValue;
5
+ value?: AttributeValue;
6
6
  }
7
- export declare type AttributeValue = string | {
7
+ export declare type AttributeValue = McdocType | {
8
+ kind: 'tree';
9
+ values: AttributeTree;
10
+ };
11
+ export declare type AttributeTree = {
8
12
  [key: string | number]: AttributeValue;
9
13
  };
10
- export declare type NumericRange = [number | undefined, number | undefined];
11
- interface StaticIndex {
14
+ export declare type NumericRange = {
15
+ kind: RangeKind;
16
+ min?: number;
17
+ max?: number;
18
+ };
19
+ export declare const StaticIndexKeywords: readonly ["fallback", "none", "unknown"];
20
+ export declare type StaticIndexKeyword = typeof StaticIndexKeywords[number];
21
+ export interface StaticIndex {
12
22
  kind: 'static';
13
- value: string | {
14
- keyword: 'fallback' | 'none' | 'unknown';
15
- };
23
+ value: string;
16
24
  }
17
- interface DynamicIndex {
25
+ export interface DynamicIndex {
18
26
  kind: 'dynamic';
19
27
  accessor: (string | {
20
28
  keyword: 'key' | 'parent';
21
29
  })[];
22
30
  }
23
31
  export declare type Index = StaticIndex | DynamicIndex;
24
- export declare type ParallelIndices = [Index, ...Index[]];
32
+ /**
33
+ * Corresponds to the IndexBodyNode
34
+ */
35
+ export declare type ParallelIndices = Index[];
25
36
  export interface DispatcherData {
26
- registry: FullResourceLocation | undefined;
37
+ registry: FullResourceLocation;
27
38
  index: ParallelIndices;
28
39
  }
29
- interface TypeBase {
40
+ export interface TypeBase {
30
41
  kind: string;
31
42
  attributes?: Attribute[];
32
43
  indices?: ParallelIndices[];
@@ -36,28 +47,35 @@ export interface DispatcherType extends TypeBase, DispatcherData {
36
47
  }
37
48
  export interface StructType extends TypeBase {
38
49
  kind: 'struct';
39
- fields: ({
40
- kind: 'field';
41
- key: string;
42
- type: McdocType;
43
- symbol: SymbolPath;
44
- } | {
45
- kind: 'spread';
46
- type: McdocType;
47
- })[];
50
+ fields: StructTypeField[];
51
+ }
52
+ export declare type StructTypeField = StructTypePairField | StructTypeSpreadField;
53
+ export interface StructTypePairField {
54
+ kind: 'pair';
55
+ attributes?: Attribute[];
56
+ key: string | McdocType;
57
+ type: McdocType;
58
+ optional?: boolean;
59
+ }
60
+ export interface StructTypeSpreadField {
61
+ kind: 'spread';
62
+ attributes?: Attribute[];
63
+ type: McdocType;
48
64
  }
49
65
  export interface EnumType extends TypeBase {
50
66
  kind: 'enum';
51
- enumKind: EnumKind;
52
- values: {
53
- identifier: string;
54
- value: string | number | bigint;
55
- }[];
67
+ enumKind?: EnumKind;
68
+ values: EnumTypeField[];
69
+ }
70
+ export interface EnumTypeField {
71
+ attributes?: Attribute[];
72
+ identifier: string;
73
+ value: string | number | bigint;
56
74
  }
57
75
  export interface ReferenceType extends TypeBase {
58
76
  kind: 'reference';
59
- symbol?: SymbolPath;
60
- index?: Index;
77
+ path?: string;
78
+ typeParameters?: McdocType[];
61
79
  }
62
80
  export interface UnionType<T extends McdocType = McdocType> extends TypeBase {
63
81
  kind: 'union';
@@ -65,14 +83,14 @@ export interface UnionType<T extends McdocType = McdocType> extends TypeBase {
65
83
  }
66
84
  export declare const EmptyUnion: UnionType<never> & NoIndices;
67
85
  export declare function createEmptyUnion(attributes?: Attribute[]): UnionType<never> & NoIndices;
68
- interface KeywordType extends TypeBase {
86
+ export interface KeywordType extends TypeBase {
69
87
  kind: 'any' | 'boolean';
70
88
  }
71
- interface StringType extends TypeBase {
89
+ export interface StringType extends TypeBase {
72
90
  kind: 'string';
73
91
  lengthRange?: NumericRange;
74
92
  }
75
- declare type LiteralValue = {
93
+ export declare type LiteralValue = {
76
94
  kind: 'boolean';
77
95
  value: boolean;
78
96
  } | {
@@ -81,21 +99,35 @@ declare type LiteralValue = {
81
99
  } | {
82
100
  kind: 'number';
83
101
  value: number;
84
- suffix: 'b' | 's' | 'L' | 'f' | 'd' | undefined;
102
+ suffix: 'b' | 's' | 'l' | 'f' | 'd' | undefined;
85
103
  };
86
- interface LiteralType extends TypeBase {
104
+ export interface LiteralType extends TypeBase {
87
105
  kind: 'literal';
88
106
  value: LiteralValue;
89
107
  }
90
- interface NumericType extends TypeBase {
91
- kind: 'byte' | 'short' | 'int' | 'long' | 'float' | 'double';
108
+ export declare const LiteralNumberSuffixes: readonly ["b", "s", "l", "f", "d"];
109
+ export declare type LiteralNumberSuffix = typeof LiteralNumberSuffixes[number];
110
+ export declare const LiteralNumberCaseInsensitiveSuffixes: readonly ["b", "s", "l", "f", "d", "B", "S", "L", "F", "D"];
111
+ export declare type LiteralNumberCaseInsensitiveSuffix = typeof LiteralNumberCaseInsensitiveSuffixes[number];
112
+ export interface NumericType extends TypeBase {
113
+ kind: NumericTypeKind;
92
114
  valueRange?: NumericRange;
93
115
  }
94
- interface PrimitiveArrayType extends TypeBase {
116
+ export declare const NumericTypeIntKinds: readonly ["byte", "short", "int", "long"];
117
+ export declare type NumericTypeIntKind = typeof NumericTypeIntKinds[number];
118
+ export declare const NumericTypeFloatKinds: readonly ["float", "double"];
119
+ export declare type NumericTypeFloatKind = typeof NumericTypeFloatKinds[number];
120
+ export declare const NumericTypeKinds: readonly ["byte", "short", "int", "long", "float", "double"];
121
+ export declare type NumericTypeKind = typeof NumericTypeKinds[number];
122
+ export interface PrimitiveArrayType extends TypeBase {
95
123
  kind: 'byte_array' | 'int_array' | 'long_array';
96
124
  valueRange?: NumericRange;
97
125
  lengthRange?: NumericRange;
98
126
  }
127
+ export declare const PrimitiveArrayValueKinds: readonly ["byte", "int", "long"];
128
+ export declare type PrimitiveArrayValueKind = typeof PrimitiveArrayValueKinds[number];
129
+ export declare const PrimitiveArrayKinds: readonly ("byte_array" | "int_array" | "long_array")[];
130
+ export declare type PrimitiveArrayKind = typeof PrimitiveArrayKinds[number];
99
131
  export interface ListType extends TypeBase {
100
132
  kind: 'list';
101
133
  item: McdocType;
@@ -130,10 +162,7 @@ declare type NoIndices = {
130
162
  };
131
163
  export interface FlatStructType extends TypeBase {
132
164
  kind: 'flat_struct';
133
- fields: Record<string, {
134
- type: McdocType;
135
- symbol: SymbolPath;
136
- }>;
165
+ fields: Record<string, McdocType>;
137
166
  }
138
167
  export declare const flattenUnionType: (union: UnionType) => UnionType;
139
168
  export declare const unionTypes: (a: McdocType, b: McdocType) => McdocType;
package/lib/type/index.js CHANGED
@@ -1,29 +1,34 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolveType = exports.checkAssignability = exports.simplifyType = exports.simplifyListType = exports.simplifyUnionType = exports.unionTypes = exports.flattenUnionType = exports.McdocType = exports.createEmptyUnion = exports.EmptyUnion = void 0;
4
- const core_1 = require("@spyglassmc/core");
5
- const locales_1 = require("@spyglassmc/locales");
6
- exports.EmptyUnion = Object.freeze({ kind: 'union', members: [] });
7
- function createEmptyUnion(attributes) {
1
+ import { Arrayable } from '@spyglassmc/core';
2
+ import { localeQuote, localize } from '@spyglassmc/locales';
3
+ import { getRangeDelimiter } from '../node/index.js';
4
+ export const StaticIndexKeywords = Object.freeze(['fallback', 'none', 'unknown']);
5
+ export const EmptyUnion = Object.freeze({ kind: 'union', members: [] });
6
+ export function createEmptyUnion(attributes) {
8
7
  return {
9
- ...exports.EmptyUnion,
8
+ ...EmptyUnion,
10
9
  attributes,
11
10
  };
12
11
  }
13
- exports.createEmptyUnion = createEmptyUnion;
14
- var McdocType;
12
+ export const LiteralNumberSuffixes = Object.freeze(['b', 's', 'l', 'f', 'd']);
13
+ export const LiteralNumberCaseInsensitiveSuffixes = Object.freeze([...LiteralNumberSuffixes, 'B', 'S', 'L', 'F', 'D']);
14
+ export const NumericTypeIntKinds = Object.freeze(['byte', 'short', 'int', 'long']);
15
+ export const NumericTypeFloatKinds = Object.freeze(['float', 'double']);
16
+ export const NumericTypeKinds = Object.freeze([...NumericTypeIntKinds, ...NumericTypeFloatKinds]);
17
+ export const PrimitiveArrayValueKinds = Object.freeze(['byte', 'int', 'long']);
18
+ export const PrimitiveArrayKinds = Object.freeze(PrimitiveArrayValueKinds.map(kind => `${kind}_array`));
19
+ export var McdocType;
15
20
  (function (McdocType) {
16
21
  function toString(type) {
17
22
  const rangeToString = (range) => {
18
23
  if (!range) {
19
24
  return '';
20
25
  }
21
- const [min, max] = range;
22
- return min === max ? ` @ ${min}` : ` @ ${min ?? ''}..${max ?? ''}`;
26
+ const { kind, min, max } = range;
27
+ return min === max ? ` @ ${min}` : ` @ ${min ?? ''}${getRangeDelimiter(kind)}${max ?? ''}`;
23
28
  };
24
29
  const indicesToString = (indices) => {
25
30
  const strings = [];
26
- for (const index of core_1.Arrayable.toArray(indices)) {
31
+ for (const index of Arrayable.toArray(indices)) {
27
32
  if (index === undefined) {
28
33
  strings.push('()');
29
34
  }
@@ -67,7 +72,7 @@ var McdocType;
67
72
  case 'long_array':
68
73
  return `long${rangeToString(type.valueRange)}[]${rangeToString(type.lengthRange)}`;
69
74
  case 'reference':
70
- return type.symbol?.path.join('::') ?? '<unknown_reference>';
75
+ return type.path ?? '<unknown_reference>';
71
76
  case 'short':
72
77
  return `short${rangeToString(type.valueRange)}`;
73
78
  case 'string':
@@ -81,7 +86,7 @@ var McdocType;
81
86
  }
82
87
  }
83
88
  McdocType.toString = toString;
84
- })(McdocType = exports.McdocType || (exports.McdocType = {}));
89
+ })(McdocType || (McdocType = {}));
85
90
  var CheckResult;
86
91
  (function (CheckResult) {
87
92
  CheckResult[CheckResult["Nah"] = 0] = "Nah";
@@ -95,12 +100,12 @@ const areRangesMatch = (s, t) => {
95
100
  if (!s) {
96
101
  return false;
97
102
  }
98
- const [sMin, sMax] = s;
99
- const [tMin, tMax] = t;
103
+ const { min: sMin, max: sMax } = s;
104
+ const { min: tMin, max: tMax } = t;
100
105
  return (tMin === undefined || (sMin !== undefined && sMin >= tMin)) &&
101
106
  (tMax === undefined || (sMax !== undefined && sMax <= tMax));
102
107
  };
103
- const flattenUnionType = (union) => {
108
+ export const flattenUnionType = (union) => {
104
109
  const set = new Set();
105
110
  const add = (data) => {
106
111
  for (const existingMember of set) {
@@ -115,7 +120,7 @@ const flattenUnionType = (union) => {
115
120
  };
116
121
  for (const member of union.members) {
117
122
  if (member.kind === 'union') {
118
- (0, exports.flattenUnionType)(member).members.forEach(add);
123
+ flattenUnionType(member).members.forEach(add);
119
124
  }
120
125
  else {
121
126
  add(member);
@@ -126,8 +131,7 @@ const flattenUnionType = (union) => {
126
131
  members: [...set],
127
132
  };
128
133
  };
129
- exports.flattenUnionType = flattenUnionType;
130
- const unionTypes = (a, b) => {
134
+ export const unionTypes = (a, b) => {
131
135
  if ((check(a, b) & CheckResult.StrictlyAssignable) === CheckResult.StrictlyAssignable) {
132
136
  return b;
133
137
  }
@@ -143,37 +147,33 @@ const unionTypes = (a, b) => {
143
147
  };
144
148
  return ans;
145
149
  };
146
- exports.unionTypes = unionTypes;
147
- const simplifyUnionType = (union) => {
148
- union = (0, exports.flattenUnionType)(union);
150
+ export const simplifyUnionType = (union) => {
151
+ union = flattenUnionType(union);
149
152
  if (union.members.length === 1) {
150
153
  return union.members[0];
151
154
  }
152
155
  return union;
153
156
  };
154
- exports.simplifyUnionType = simplifyUnionType;
155
- const simplifyListType = (list) => ({
157
+ export const simplifyListType = (list) => ({
156
158
  kind: 'list',
157
- item: (0, exports.simplifyType)(list.item),
158
- ...list.lengthRange ? { lengthRange: [...list.lengthRange] } : {},
159
+ item: simplifyType(list.item),
160
+ ...list.lengthRange ? { lengthRange: { ...list.lengthRange } } : {},
159
161
  });
160
- exports.simplifyListType = simplifyListType;
161
- const simplifyType = (data) => {
162
+ export const simplifyType = (data) => {
162
163
  if (data.kind === 'union') {
163
- data = (0, exports.simplifyUnionType)(data);
164
+ data = simplifyUnionType(data);
164
165
  }
165
166
  else if (data.kind === 'list') {
166
- data = (0, exports.simplifyListType)(data);
167
+ data = simplifyListType(data);
167
168
  }
168
169
  return data;
169
170
  };
170
- exports.simplifyType = simplifyType;
171
171
  const check = (s, t, errors = []) => {
172
172
  const strictlyAssignableIfTrue = (value) => value ? CheckResult.StrictlyAssignable : CheckResult.Nah;
173
173
  const assignableIfTrue = (value) => value ? CheckResult.Assignable : CheckResult.Nah;
174
174
  let ans;
175
- s = (0, exports.simplifyType)(s);
176
- t = (0, exports.simplifyType)(t);
175
+ s = simplifyType(s);
176
+ t = simplifyType(t);
177
177
  if (s.kind === 'any' || s.kind === 'reference' || t.kind === 'reference') {
178
178
  // Reference types are treated as any for now.
179
179
  ans = CheckResult.Assignable;
@@ -192,7 +192,7 @@ const check = (s, t, errors = []) => {
192
192
  }
193
193
  else if (s.kind === 'byte') {
194
194
  if (t.kind === 'boolean') {
195
- ans = check(s, { kind: 'byte', valueRange: [0, 1] }, errors);
195
+ ans = check(s, { kind: 'byte', valueRange: { kind: 0b00, min: 0, max: 1 } }, errors);
196
196
  }
197
197
  else if (t.kind === 'byte') {
198
198
  ans = strictlyAssignableIfTrue(areRangesMatch(s.valueRange, t.valueRange));
@@ -244,11 +244,11 @@ const check = (s, t, errors = []) => {
244
244
  ans = CheckResult.Nah;
245
245
  }
246
246
  if (!ans) {
247
- errors.push((0, locales_1.localize)('mcdoc.checker.type-not-assignable', (0, locales_1.localeQuote)(McdocType.toString(s)), (0, locales_1.localeQuote)(McdocType.toString(t))));
247
+ errors.push(localize('mcdoc.checker.type-not-assignable', localeQuote(McdocType.toString(s)), localeQuote(McdocType.toString(t))));
248
248
  }
249
249
  return ans;
250
250
  };
251
- const checkAssignability = ({ source, target }) => {
251
+ export const checkAssignability = ({ source, target }) => {
252
252
  if (source === undefined || target === undefined) {
253
253
  return { isAssignable: true };
254
254
  }
@@ -259,8 +259,7 @@ const checkAssignability = ({ source, target }) => {
259
259
  ...errors.length ? { errorMessage: errors.reverse().map((m, i) => `${' '.repeat(i)}${m}`).join('\n') } : {},
260
260
  };
261
261
  };
262
- exports.checkAssignability = checkAssignability;
263
- function resolveType(inputType, ctx, value) {
262
+ export function resolveType(inputType, ctx, value) {
264
263
  const type = getTangibleType(inputType, ctx, value);
265
264
  let ans = (() => {
266
265
  if (type.kind === 'union') {
@@ -282,7 +281,6 @@ function resolveType(inputType, ctx, value) {
282
281
  }
283
282
  return ans;
284
283
  }
285
- exports.resolveType = resolveType;
286
284
  function dispatchType(type, ctx) {
287
285
  throw '// TODO';
288
286
  }
@@ -328,7 +326,7 @@ function navigateIndex(type, index, ctx, value) {
328
326
  return createEmptyUnion(type.attributes);
329
327
  }
330
328
  const flatStruct = flattenStruct(type, ctx, value);
331
- return resolveType(flatStruct.fields[key].type, ctx, value);
329
+ return resolveType(flatStruct.fields[key], ctx, value);
332
330
  }
333
331
  else if (type.kind === 'union') {
334
332
  return mapUnion(type, t => navigateIndex(t, index, ctx, value));
@@ -382,14 +380,19 @@ function flattenStruct(type, ctx, value) {
382
380
  }
383
381
  }
384
382
  else {
385
- ans.fields[field.key] = { type: field.type, symbol: field.symbol };
383
+ if (typeof field.key === 'string') {
384
+ ans.fields[field.key] = field.type;
385
+ }
386
+ else {
387
+ // TODO: Handle map keys
388
+ }
386
389
  }
387
390
  }
388
391
  return ans;
389
392
  }
390
393
  function addAttributes(type, ...attributes) {
391
394
  for (const attr of attributes) {
392
- type.attributes ?? (type.attributes = []);
395
+ type.attributes ??= [];
393
396
  if (!type.attributes.some(a => a.name === attr.name)) {
394
397
  type.attributes.push(attr);
395
398
  }
@@ -0,0 +1,4 @@
1
+ import type { UriBinder, UriSorterRegistration } from '@spyglassmc/core';
2
+ export declare const uriBinder: UriBinder;
3
+ export declare const uriSorter: UriSorterRegistration;
4
+ //# sourceMappingURL=uri_processors.d.ts.map
@@ -1,17 +1,14 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.uriBinder = void 0;
4
- const core_1 = require("@spyglassmc/core");
5
- const util_1 = require("./util");
1
+ import { fileUtil } from '@spyglassmc/core';
2
+ import { segToIdentifier } from './common.js';
6
3
  const Extension = '.mcdoc';
7
4
  const McdocRootPrefix = 'mcdoc/';
8
- const uriBinder = (uris, ctx) => {
5
+ export const uriBinder = (uris, ctx) => {
9
6
  let urisAndRels = [];
10
7
  for (const uri of uris) {
11
8
  if (!uri.endsWith(Extension)) {
12
9
  continue;
13
10
  }
14
- let rel = core_1.fileUtil.getRel(uri, ctx.roots);
11
+ let rel = fileUtil.getRel(uri, ctx.roots);
15
12
  if (!rel) {
16
13
  continue;
17
14
  }
@@ -34,17 +31,23 @@ const uriBinder = (uris, ctx) => {
34
31
  // file:///root/mcdoc/foo/bar.mcdoc -> foo/bar
35
32
  for (const [uri, rel] of urisAndRels) {
36
33
  ctx.symbols
37
- .query(uri, 'mcdoc', (0, util_1.segToIdentifier)(rel.split('/')))
34
+ .query(uri, 'mcdoc', segToIdentifier(rel.split('/')))
38
35
  .ifKnown(() => { })
39
36
  .elseEnter({
40
- data: {
41
- subcategory: 'module',
42
- },
43
- usage: {
44
- type: 'implementation',
45
- },
37
+ data: { subcategory: 'module' },
38
+ usage: { type: 'definition' },
46
39
  });
47
40
  }
48
41
  };
49
- exports.uriBinder = uriBinder;
50
- //# sourceMappingURL=uriBinder.js.map
42
+ export const uriSorter = (a, b, next) => {
43
+ if (a.endsWith(Extension) && !b.endsWith(Extension)) {
44
+ return -1;
45
+ }
46
+ else if (!a.endsWith(Extension) && b.endsWith(Extension)) {
47
+ return 1;
48
+ }
49
+ else {
50
+ return next(a, b);
51
+ }
52
+ };
53
+ //# sourceMappingURL=uri_processors.js.map
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@spyglassmc/mcdoc",
3
- "version": "0.1.1",
3
+ "version": "0.3.1",
4
+ "type": "module",
4
5
  "main": "lib/index.js",
5
6
  "types": "lib/index.d.ts",
6
7
  "author": "SPGoding",
@@ -24,7 +25,7 @@
24
25
  "url": "https://github.com/SpyglassMC/Spyglass/issues"
25
26
  },
26
27
  "dependencies": {
27
- "@spyglassmc/core": "0.1.2",
28
- "@spyglassmc/locales": "0.1.2"
28
+ "@spyglassmc/core": "0.4.0",
29
+ "@spyglassmc/locales": "0.3.0"
29
30
  }
30
31
  }
@@ -1,3 +0,0 @@
1
- import type { UriBinder } from '@spyglassmc/core';
2
- export declare const uriBinder: UriBinder;
3
- //# sourceMappingURL=uriBinder.d.ts.map
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.segToIdentifier = exports.identifierToSeg = void 0;
4
- function identifierToSeg(identifier) {
5
- const ans = identifier.slice(2).split('::');
6
- if (ans.length === 1 && ans[0] === '') {
7
- return [];
8
- }
9
- return ans;
10
- }
11
- exports.identifierToSeg = identifierToSeg;
12
- function segToIdentifier(seg) {
13
- return `::${seg.join('::')}`;
14
- }
15
- exports.segToIdentifier = segToIdentifier;
16
- //# sourceMappingURL=util.js.map
@@ -1,18 +0,0 @@
1
- import type * as core from '@spyglassmc/core';
2
- import type { Symbol } from '@spyglassmc/core';
3
- import type { Segments } from '../binder';
4
- export interface CheckerContext extends core.CheckerContext {
5
- /**
6
- * The current module's identifier.
7
- */
8
- modIdentifier: string;
9
- /**
10
- * The current module's segments.
11
- */
12
- modSeg: Segments;
13
- /**
14
- * The current module's symbol.
15
- */
16
- modSymbol: Symbol;
17
- }
18
- //# sourceMappingURL=CheckerContext.d.ts.map
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=CheckerContext.js.map
@@ -1,4 +0,0 @@
1
- import type { Checker } from '@spyglassmc/core';
2
- import type { ModuleNode } from '../node';
3
- export declare const module_: Checker<ModuleNode>;
4
- //# sourceMappingURL=entry.d.ts.map