@agentshouse/mdmodel 0.1.0-ccf1367190878 → 0.1.0-cd5ace6e0b27a

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 (177) hide show
  1. package/API.md +232 -182
  2. package/README.md +21 -22
  3. package/dist/definition.d.ts +18 -1
  4. package/dist/definition.d.ts.map +1 -1
  5. package/dist/definition.js +176 -48
  6. package/dist/divide.d.ts.map +1 -1
  7. package/dist/divide.js +32 -21
  8. package/dist/document.d.ts +10 -6
  9. package/dist/document.d.ts.map +1 -1
  10. package/dist/document.js +424 -207
  11. package/dist/event.d.ts +78 -0
  12. package/dist/event.d.ts.map +1 -0
  13. package/dist/event.js +201 -0
  14. package/dist/facts.d.ts +15 -0
  15. package/dist/facts.d.ts.map +1 -0
  16. package/dist/facts.js +41 -0
  17. package/dist/form.d.ts +4 -0
  18. package/dist/form.d.ts.map +1 -0
  19. package/dist/form.js +139 -0
  20. package/dist/index.d.ts +25 -11
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +15 -8
  23. package/dist/kinds.d.ts +18 -0
  24. package/dist/kinds.d.ts.map +1 -0
  25. package/dist/kinds.js +150 -0
  26. package/dist/markdown.d.ts +51 -0
  27. package/dist/markdown.d.ts.map +1 -0
  28. package/dist/markdown.js +271 -0
  29. package/dist/paths.d.ts +18 -2
  30. package/dist/paths.d.ts.map +1 -1
  31. package/dist/paths.js +69 -15
  32. package/dist/pattern.d.ts +10 -0
  33. package/dist/pattern.d.ts.map +1 -0
  34. package/dist/pattern.js +356 -0
  35. package/dist/project.d.ts +1 -0
  36. package/dist/project.d.ts.map +1 -1
  37. package/dist/project.js +44 -26
  38. package/dist/query/language.d.ts +254 -0
  39. package/dist/query/language.d.ts.map +1 -0
  40. package/dist/query/language.js +620 -0
  41. package/dist/query/match.d.ts +14 -0
  42. package/dist/query/match.d.ts.map +1 -0
  43. package/dist/query/match.js +150 -0
  44. package/dist/query/rows.d.ts +32 -0
  45. package/dist/query/rows.d.ts.map +1 -0
  46. package/dist/query/rows.js +161 -0
  47. package/dist/query/sql.d.ts +39 -0
  48. package/dist/query/sql.d.ts.map +1 -0
  49. package/dist/query/sql.js +285 -0
  50. package/dist/references.d.ts.map +1 -1
  51. package/dist/references.js +6 -1
  52. package/dist/reserved.d.ts +6 -0
  53. package/dist/reserved.d.ts.map +1 -0
  54. package/dist/reserved.js +21 -0
  55. package/dist/schema.d.ts +1 -0
  56. package/dist/schema.d.ts.map +1 -1
  57. package/dist/schema.js +55 -4
  58. package/dist/types.d.ts +46 -5
  59. package/dist/types.d.ts.map +1 -1
  60. package/dist/validate.d.ts +31 -4
  61. package/dist/validate.d.ts.map +1 -1
  62. package/dist/validate.js +387 -129
  63. package/dist/yaml.d.ts.map +1 -1
  64. package/dist/yaml.js +40 -14
  65. package/fixtures/README.md +34 -7
  66. package/fixtures/cases/conditional-section/context.json +1 -0
  67. package/fixtures/cases/conditional-section/expected.json +6 -0
  68. package/fixtures/cases/conditional-section/library/questions/hosting.md +9 -0
  69. package/fixtures/cases/conditional-section/library/questions/payments.md +12 -0
  70. package/fixtures/cases/conditional-section/schema.yaml +14 -0
  71. package/fixtures/cases/cross-room-link/context.json +1 -0
  72. package/fixtures/cases/cross-room-link/expected.json +1 -0
  73. package/fixtures/cases/cross-room-link/library/clients/acme.md +6 -0
  74. package/fixtures/cases/cross-room-link/schema.yaml +3 -0
  75. package/fixtures/cases/cross-room-reference/context.json +1 -0
  76. package/fixtures/cases/cross-room-reference/expected.json +1 -0
  77. package/fixtures/cases/cross-room-reference/library/clients/acme.md +9 -0
  78. package/fixtures/cases/cross-room-reference/schema.yaml +9 -0
  79. package/fixtures/cases/equal-labels/context.json +1 -0
  80. package/fixtures/cases/equal-labels/expected.json +5 -0
  81. package/fixtures/cases/equal-labels/schema.yaml +4 -0
  82. package/fixtures/cases/labels-in-the-rooms-language/context.json +1 -0
  83. package/fixtures/cases/labels-in-the-rooms-language/expected.json +1 -0
  84. package/fixtures/cases/labels-in-the-rooms-language/library/deals/atlas.md +6 -0
  85. package/fixtures/cases/labels-in-the-rooms-language/schema.yaml +9 -0
  86. package/fixtures/cases/lazy-list-item/context.json +1 -0
  87. package/fixtures/cases/lazy-list-item/expected.json +3 -0
  88. package/fixtures/cases/lazy-list-item/library/item.md +8 -0
  89. package/fixtures/cases/lazy-list-item/schema.yaml +3 -0
  90. package/fixtures/cases/link-into-a-reserved-name/context.json +1 -0
  91. package/fixtures/cases/link-into-a-reserved-name/expected.json +5 -0
  92. package/fixtures/cases/link-into-a-reserved-name/library/notes/a.md +6 -0
  93. package/fixtures/cases/link-into-a-reserved-name/schema.yaml +3 -0
  94. package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/context.json +1 -0
  95. package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/expected.json +5 -0
  96. package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/library/notes/b.md +5 -0
  97. package/fixtures/cases/long-value-past-six-levels/context.json +1 -0
  98. package/fixtures/cases/long-value-past-six-levels/expected.json +5 -0
  99. package/fixtures/cases/long-value-past-six-levels/schema.yaml +15 -0
  100. package/fixtures/cases/member-on-an-untyped-document/context.json +1 -0
  101. package/fixtures/cases/member-on-an-untyped-document/expected.json +6 -0
  102. package/fixtures/cases/member-on-an-untyped-document/library/note.md +4 -0
  103. package/fixtures/cases/member-on-another-type/context.json +1 -0
  104. package/fixtures/cases/member-on-another-type/expected.json +6 -0
  105. package/fixtures/cases/member-on-another-type/library/note.md +4 -0
  106. package/fixtures/cases/member-on-another-type/schema.yaml +7 -0
  107. package/fixtures/cases/name-not-a-string/context.json +1 -0
  108. package/fixtures/cases/name-not-a-string/expected.json +5 -0
  109. package/fixtures/cases/name-not-a-string/schema.yaml +5 -0
  110. package/fixtures/cases/one-key-two-labels/context.json +1 -0
  111. package/fixtures/cases/one-key-two-labels/expected.json +5 -0
  112. package/fixtures/cases/one-key-two-labels/schema.yaml +5 -0
  113. package/fixtures/cases/one-key-two-types/context.json +1 -0
  114. package/fixtures/cases/one-key-two-types/expected.json +5 -0
  115. package/fixtures/cases/one-key-two-types/schema.yaml +4 -0
  116. package/fixtures/cases/reference-into-a-reserved-name/context.json +1 -0
  117. package/fixtures/cases/reference-into-a-reserved-name/expected.json +6 -0
  118. package/fixtures/cases/reference-into-a-reserved-name/library/notes/a.md +4 -0
  119. package/fixtures/cases/reference-into-a-reserved-name/library/settings/brief.md +1 -0
  120. package/fixtures/cases/reference-into-a-reserved-name/schema.yaml +3 -0
  121. package/fixtures/cases/room-md-declares-a-type/context.json +1 -0
  122. package/fixtures/cases/room-md-declares-a-type/expected.json +6 -0
  123. package/fixtures/cases/room-md-declares-a-type/library/ROOM.md +5 -0
  124. package/fixtures/cases/thematic-break-in-a-list-section/context.json +1 -0
  125. package/fixtures/cases/thematic-break-in-a-list-section/expected.json +6 -0
  126. package/fixtures/cases/thematic-break-in-a-list-section/library/item.md +7 -0
  127. package/fixtures/cases/thematic-break-in-a-list-section/schema.yaml +3 -0
  128. package/fixtures/cases/typed-path-names-another-type/context.json +11 -0
  129. package/fixtures/cases/typed-path-names-another-type/expected.json +6 -0
  130. package/fixtures/cases/typed-path-names-another-type/library/settings/prefs/one.md +3 -0
  131. package/fixtures/cases/typed-path-names-another-type/schema.yaml +6 -0
  132. package/fixtures/cases/typed-path-names-its-type/context.json +11 -0
  133. package/fixtures/cases/typed-path-names-its-type/expected.json +3 -0
  134. package/fixtures/cases/typed-path-names-its-type/library/settings/prefs/deeper/two.md +1 -0
  135. package/fixtures/cases/typed-path-names-its-type/library/settings/prefs/one.md +4 -0
  136. package/fixtures/cases/typed-path-names-its-type/schema.yaml +6 -0
  137. package/fixtures/cases/typed-path-names-no-type/context.json +11 -0
  138. package/fixtures/cases/typed-path-names-no-type/expected.json +6 -0
  139. package/fixtures/cases/typed-path-names-no-type/library/settings/prefs/one.md +1 -0
  140. package/fixtures/cases/typed-path-names-no-type/schema.yaml +6 -0
  141. package/fixtures/cases/unnamed-typed-path-checks-its-fields/context.json +11 -0
  142. package/fixtures/cases/unnamed-typed-path-checks-its-fields/expected.json +6 -0
  143. package/fixtures/cases/unnamed-typed-path-checks-its-fields/library/rooms/team/tasks/plan.md +4 -0
  144. package/fixtures/cases/unnamed-typed-path-checks-its-fields/schema.yaml +5 -0
  145. package/fixtures/cases/unnamed-typed-path-names-a-type/context.json +11 -0
  146. package/fixtures/cases/unnamed-typed-path-names-a-type/expected.json +6 -0
  147. package/fixtures/cases/unnamed-typed-path-names-a-type/library/rooms/team/tasks/plan.md +6 -0
  148. package/fixtures/cases/unnamed-typed-path-names-a-type/schema.yaml +5 -0
  149. package/fixtures/cases/unnamed-typed-path-reads-its-type/context.json +11 -0
  150. package/fixtures/cases/unnamed-typed-path-reads-its-type/expected.json +3 -0
  151. package/fixtures/cases/unnamed-typed-path-reads-its-type/library/rooms/team/plan.md +1 -0
  152. package/fixtures/cases/unnamed-typed-path-reads-its-type/library/rooms/team/tasks/plan.md +5 -0
  153. package/fixtures/cases/unnamed-typed-path-reads-its-type/schema.yaml +5 -0
  154. package/fixtures/cases/unnamed-typed-path-without-a-body/context.json +11 -0
  155. package/fixtures/cases/unnamed-typed-path-without-a-body/expected.json +5 -0
  156. package/fixtures/cases/unnamed-typed-path-without-a-body/library/rooms/team/tasks/plan.md +5 -0
  157. package/fixtures/cases/unnamed-typed-path-without-a-body/schema.yaml +5 -0
  158. package/fixtures/division.json +73 -28
  159. package/fixtures/preamble.json +24 -3
  160. package/fixtures/projection.json +105 -2
  161. package/fixtures/queries.json +964 -0
  162. package/package.json +16 -2
  163. package/dist/blocks.d.ts +0 -11
  164. package/dist/blocks.d.ts.map +0 -1
  165. package/dist/blocks.js +0 -76
  166. package/dist/condition.d.ts +0 -149
  167. package/dist/condition.d.ts.map +0 -1
  168. package/dist/condition.js +0 -500
  169. package/dist/links.d.ts +0 -27
  170. package/dist/links.d.ts.map +0 -1
  171. package/dist/links.js +0 -774
  172. package/dist/profile.d.ts +0 -6
  173. package/dist/profile.d.ts.map +0 -1
  174. package/dist/profile.js +0 -5
  175. package/dist/sections.d.ts +0 -11
  176. package/dist/sections.d.ts.map +0 -1
  177. package/dist/sections.js +0 -57
@@ -0,0 +1,356 @@
1
+ const NativeRegExp = RegExp;
2
+ export const PATTERN_STATES = 1_000;
3
+ const VISIT_BUDGET = 100_000_000;
4
+ const HELD_PATTERNS = 512;
5
+ export class PatternTooCostly extends Error {
6
+ }
7
+ let scope = null;
8
+ const ASCII = 128;
9
+ class Unsupported extends Error {
10
+ }
11
+ const ATOM = 0;
12
+ const SPLIT = 1;
13
+ const ASSERT = 2;
14
+ const MATCH = 3;
15
+ class Atom {
16
+ literal;
17
+ ascii = new Int8Array(ASCII);
18
+ matcher;
19
+ constructor(source, literal) {
20
+ this.literal = literal;
21
+ this.matcher = new NativeRegExp(`^(?:${source})$`, 'u');
22
+ }
23
+ holds(point) {
24
+ if (this.literal !== null)
25
+ return point === this.literal;
26
+ if (point >= ASCII)
27
+ return this.matcher.test(String.fromCodePoint(point));
28
+ const known = this.ascii[point];
29
+ if (known !== 0)
30
+ return known > 0;
31
+ const held = this.matcher.test(String.fromCharCode(point));
32
+ this.ascii[point] = held ? 1 : -1;
33
+ return held;
34
+ }
35
+ }
36
+ class Reader {
37
+ source;
38
+ at = 0;
39
+ constructor(source) {
40
+ this.source = source;
41
+ }
42
+ peek(offset = 0) {
43
+ return this.source[this.at + offset];
44
+ }
45
+ starts(text) {
46
+ return this.source.startsWith(text, this.at);
47
+ }
48
+ }
49
+ function digits(reader) {
50
+ const from = reader.at;
51
+ while (/[0-9]/.test(reader.peek() ?? ''))
52
+ reader.at += 1;
53
+ return reader.at === from ? null : Number(reader.source.slice(from, reader.at));
54
+ }
55
+ function escapeEnd(source, at) {
56
+ const next = source[at + 1];
57
+ if (next === 'c')
58
+ return at + 3;
59
+ if (next === 'x')
60
+ return at + 4;
61
+ if ((next === 'u' && source[at + 2] === '{') || next === 'p' || next === 'P')
62
+ return source.indexOf('}', at + 2) + 1;
63
+ if (next !== 'u')
64
+ return at + 2;
65
+ const end = at + 6;
66
+ const lead = Number.parseInt(source.slice(at + 2, end), 16);
67
+ const trail = /^\\u([0-9a-fA-F]{4})$/.exec(source.slice(end, end + 6))?.[1];
68
+ const paired = lead >= 0xd800 &&
69
+ lead <= 0xdbff &&
70
+ trail !== undefined &&
71
+ Number.parseInt(trail, 16) >= 0xdc00 &&
72
+ Number.parseInt(trail, 16) <= 0xdfff;
73
+ return paired ? end + 6 : end;
74
+ }
75
+ function classEnd(source, at) {
76
+ let i = at + 1;
77
+ while (source[i] !== ']')
78
+ i += source[i] === '\\' ? 2 : 1;
79
+ return i + 1;
80
+ }
81
+ function escaped(reader) {
82
+ const next = reader.peek(1);
83
+ if (next === 'b' || next === 'B') {
84
+ reader.at += 2;
85
+ return { kind: 'assert', assertion: next === 'b' ? 'boundary' : 'inside' };
86
+ }
87
+ if (/[1-9k]/.test(next))
88
+ throw new Unsupported();
89
+ const end = escapeEnd(reader.source, reader.at);
90
+ const source = reader.source.slice(reader.at, end);
91
+ reader.at = end;
92
+ return { kind: 'atom', source, literal: false };
93
+ }
94
+ function group(reader) {
95
+ reader.at += 1;
96
+ if (reader.starts('?:'))
97
+ reader.at += 2;
98
+ else if (reader.starts('?<') && !reader.starts('?<=') && !reader.starts('?<!'))
99
+ reader.at = reader.source.indexOf('>', reader.at) + 1;
100
+ else if (reader.peek() === '?')
101
+ throw new Unsupported();
102
+ const alternatives = disjunction(reader);
103
+ reader.at += 1;
104
+ return { kind: 'group', alternatives };
105
+ }
106
+ function atom(reader) {
107
+ const character = reader.peek();
108
+ if (character === '^' || character === '$') {
109
+ reader.at += 1;
110
+ return { kind: 'assert', assertion: character === '^' ? 'start' : 'end' };
111
+ }
112
+ if (character === '\\')
113
+ return escaped(reader);
114
+ if (character === '(')
115
+ return group(reader);
116
+ if (character === '[' || character === '.') {
117
+ const end = character === '[' ? classEnd(reader.source, reader.at) : reader.at + 1;
118
+ const source = reader.source.slice(reader.at, end);
119
+ reader.at = end;
120
+ return { kind: 'atom', source, literal: false };
121
+ }
122
+ const source = String.fromCodePoint(reader.source.codePointAt(reader.at));
123
+ reader.at += source.length;
124
+ return { kind: 'atom', source, literal: true };
125
+ }
126
+ function quantified(reader, node) {
127
+ const sign = reader.peek();
128
+ let least = 1;
129
+ let most = 1;
130
+ if (sign === '*' || sign === '+' || sign === '?') {
131
+ reader.at += 1;
132
+ least = sign === '+' ? 1 : 0;
133
+ most = sign === '?' ? 1 : Infinity;
134
+ }
135
+ else if (sign === '{') {
136
+ reader.at += 1;
137
+ least = digits(reader);
138
+ most = least;
139
+ if (reader.peek() === ',') {
140
+ reader.at += 1;
141
+ most = digits(reader) ?? Infinity;
142
+ }
143
+ reader.at += 1;
144
+ }
145
+ else
146
+ return { node, least, most };
147
+ if (reader.peek() === '?')
148
+ reader.at += 1;
149
+ if (least > PATTERN_STATES || (Number.isFinite(most) && most > PATTERN_STATES))
150
+ throw new Unsupported();
151
+ return { node, least, most };
152
+ }
153
+ function disjunction(reader) {
154
+ const alternatives = [[]];
155
+ while (reader.at < reader.source.length && reader.peek() !== ')') {
156
+ if (reader.peek() === '|') {
157
+ reader.at += 1;
158
+ alternatives.push([]);
159
+ continue;
160
+ }
161
+ alternatives.at(-1).push(quantified(reader, atom(reader)));
162
+ }
163
+ return alternatives;
164
+ }
165
+ class Machine {
166
+ kinds = [];
167
+ outs = [];
168
+ alternates = [];
169
+ atoms = [];
170
+ assertions = [];
171
+ atomed = new Map();
172
+ state(kind, out, alternate = -1) {
173
+ if (this.kinds.length >= PATTERN_STATES)
174
+ throw new Unsupported();
175
+ this.kinds.push(kind);
176
+ this.outs.push(out);
177
+ this.alternates.push(alternate);
178
+ return this.kinds.length - 1;
179
+ }
180
+ atom(source, literal) {
181
+ const known = this.atomed.get(source);
182
+ if (known !== undefined)
183
+ return known;
184
+ this.atoms.push(new Atom(source, literal ? source.codePointAt(0) : null));
185
+ this.atomed.set(source, this.atoms.length - 1);
186
+ return this.atoms.length - 1;
187
+ }
188
+ node(node, next) {
189
+ if (node.kind === 'atom')
190
+ return this.state(ATOM, next, this.atom(node.source, node.literal));
191
+ if (node.kind === 'assert') {
192
+ this.assertions.push(node.assertion);
193
+ return this.state(ASSERT, next, this.assertions.length - 1);
194
+ }
195
+ return this.alternatives(node.alternatives, next);
196
+ }
197
+ alternatives(alternatives, next) {
198
+ let entry = this.sequence(alternatives.at(-1), next);
199
+ for (let index = alternatives.length - 2; index >= 0; index -= 1)
200
+ entry = this.state(SPLIT, this.sequence(alternatives[index], next), entry);
201
+ return entry;
202
+ }
203
+ sequence(terms, next) {
204
+ let entry = next;
205
+ for (let index = terms.length - 1; index >= 0; index -= 1)
206
+ entry = this.term(terms[index], entry);
207
+ return entry;
208
+ }
209
+ term({ node, least, most }, next) {
210
+ if (least === 1 && most === 1)
211
+ return this.node(node, next);
212
+ let entry = next;
213
+ if (most === Infinity) {
214
+ const loop = this.state(SPLIT, -1, next);
215
+ const body = this.node(node, loop);
216
+ if (body === loop)
217
+ return next;
218
+ this.outs[loop] = body;
219
+ entry = loop;
220
+ }
221
+ else
222
+ for (let copy = least; copy < most; copy += 1) {
223
+ const body = this.node(node, entry);
224
+ if (body === entry)
225
+ return next;
226
+ entry = this.state(SPLIT, body, entry);
227
+ }
228
+ for (let copy = 0; copy < least; copy += 1) {
229
+ const body = this.node(node, entry);
230
+ if (body === entry)
231
+ return next;
232
+ entry = body;
233
+ }
234
+ return entry;
235
+ }
236
+ }
237
+ function word(point) {
238
+ return ((point >= 0x30 && point <= 0x39) ||
239
+ (point >= 0x41 && point <= 0x5a) ||
240
+ (point >= 0x61 && point <= 0x7a) ||
241
+ point === 0x5f);
242
+ }
243
+ function holds(assertion, at, point, across) {
244
+ if (assertion === 'start')
245
+ return at === 0;
246
+ if (assertion === 'end')
247
+ return point === -1;
248
+ return assertion === 'boundary' ? across : !across;
249
+ }
250
+ class Linear {
251
+ machine;
252
+ start;
253
+ marks;
254
+ generation = 0;
255
+ constructor(machine, start) {
256
+ this.machine = machine;
257
+ this.start = start;
258
+ this.marks = new Float64Array(machine.kinds.length);
259
+ }
260
+ test(text) {
261
+ const { kinds, outs, alternates, atoms, assertions } = this.machine;
262
+ const marks = this.marks;
263
+ const budget = scope ?? { left: VISIT_BUDGET };
264
+ let seeds = [];
265
+ let wordBefore = false;
266
+ for (let at = 0;;) {
267
+ const point = at < text.length ? text.codePointAt(at) : -1;
268
+ const wordAt = word(point);
269
+ const across = wordBefore !== wordAt;
270
+ const generation = (this.generation += 1);
271
+ const active = [];
272
+ seeds.push(this.start);
273
+ while (seeds.length > 0) {
274
+ const state = seeds.pop();
275
+ if (marks[state] === generation)
276
+ continue;
277
+ marks[state] = generation;
278
+ if ((budget.left -= 1) < 0)
279
+ throw new PatternTooCostly();
280
+ const kind = kinds[state];
281
+ if (kind === MATCH)
282
+ return true;
283
+ if (kind === ATOM)
284
+ active.push(state);
285
+ else if (kind === SPLIT)
286
+ seeds.push(alternates[state], outs[state]);
287
+ else if (holds(assertions[alternates[state]], at, point, across))
288
+ seeds.push(outs[state]);
289
+ }
290
+ if (point === -1)
291
+ return false;
292
+ for (const state of active)
293
+ if (atoms[alternates[state]].holds(point))
294
+ seeds.push(outs[state]);
295
+ wordBefore = wordAt;
296
+ at += point > 0xffff ? 2 : 1;
297
+ }
298
+ }
299
+ }
300
+ function compiled(source) {
301
+ try {
302
+ new NativeRegExp(source, 'u');
303
+ }
304
+ catch {
305
+ return null;
306
+ }
307
+ try {
308
+ const alternatives = disjunction(new Reader(source));
309
+ const machine = new Machine();
310
+ const start = machine.alternatives(alternatives, machine.state(MATCH, -1));
311
+ return new Linear(machine, start);
312
+ }
313
+ catch (error) {
314
+ if (error instanceof Unsupported)
315
+ return null;
316
+ throw error;
317
+ }
318
+ }
319
+ const held = new Map();
320
+ export function linearPattern(source) {
321
+ if (held.has(source))
322
+ return held.get(source);
323
+ const made = compiled(source);
324
+ if (held.size >= HELD_PATTERNS)
325
+ held.delete(held.keys().next().value);
326
+ held.set(source, made);
327
+ return made;
328
+ }
329
+ export function withLinearRegExp(run) {
330
+ const native = globalThis.RegExp;
331
+ function LinearRegExp(source) {
332
+ new NativeRegExp(source, 'u');
333
+ return {
334
+ test(text) {
335
+ const pattern = linearPattern(source);
336
+ if (pattern === null)
337
+ throw new SyntaxError(`the pattern ${source} is not linear`);
338
+ return pattern.test(text);
339
+ },
340
+ };
341
+ }
342
+ globalThis.RegExp = LinearRegExp;
343
+ try {
344
+ return run();
345
+ }
346
+ finally {
347
+ globalThis.RegExp = native;
348
+ }
349
+ }
350
+ export function openPatternBudget() {
351
+ const outer = scope;
352
+ scope = outer ?? { left: VISIT_BUDGET };
353
+ return () => {
354
+ scope = outer;
355
+ };
356
+ }
package/dist/project.d.ts CHANGED
@@ -15,6 +15,7 @@ export type InputResult = {
15
15
  readonly ok: false;
16
16
  readonly refusal: Refusal;
17
17
  };
18
+ export declare function firstEditFrom(edits: readonly Edit[], start: number): number;
18
19
  export declare function rangeText(content: string, range: Extent, edits: readonly Edit[]): string;
19
20
  export declare function projectionInput(document: Document, file: string, context: ProjectionContext): InputResult;
20
21
  export declare function project(document: Document, file: string, context?: ProjectionContext): Projection;
@@ -1 +1 @@
1
- {"version":3,"file":"project.d.ts","sourceRoot":"","sources":["../src/project.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAG3F,MAAM,MAAM,KAAK,GAAG,MAAM,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvD,MAAM,MAAM,IAAI,GAAG,KAAK,CAAC;AAEzB,MAAM,MAAM,KAAK,GAAG;IAClB,QAAQ,CAAC,MAAM,EAAE,SAAS,KAAK,EAAE,CAAC;IAClC,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;IACnC,QAAQ,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,CAAC;CACjC,CAAC;AAEF,MAAM,MAAM,WAAW,GACnB;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAA;CAAE,GAC5C;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC;AAmCtD,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,IAAI,EAAE,GAAG,MAAM,CASxF;AA4BD,wBAAgB,eAAe,CAC7B,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,iBAAiB,GACzB,WAAW,CAyBb;AAED,wBAAgB,OAAO,CACrB,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,iBAA2C,GACnD,UAAU,CAMZ"}
1
+ {"version":3,"file":"project.d.ts","sourceRoot":"","sources":["../src/project.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAG3F,MAAM,MAAM,KAAK,GAAG,MAAM,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvD,MAAM,MAAM,IAAI,GAAG,KAAK,CAAC;AAEzB,MAAM,MAAM,KAAK,GAAG;IAClB,QAAQ,CAAC,MAAM,EAAE,SAAS,KAAK,EAAE,CAAC;IAClC,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;IACnC,QAAQ,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,CAAC;CACjC,CAAC;AAEF,MAAM,MAAM,WAAW,GACnB;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAA;CAAE,GAC5C;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC;AAqCtD,wBAAgB,aAAa,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAS3E;AAED,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,IAAI,EAAE,GAAG,MAAM,CAWxF;AA4BD,wBAAgB,eAAe,CAC7B,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,iBAAiB,GACzB,WAAW,CA0Bb;AAED,wBAAgB,OAAO,CACrB,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,iBAA2C,GACnD,UAAU,CAMZ"}
package/dist/project.js CHANGED
@@ -1,41 +1,58 @@
1
1
  import { memberFacts } from './definition.js';
2
- import { frontmatter } from './document.js';
3
- import { linkInventory } from './links.js';
4
- import { target } from './paths.js';
2
+ import { frontmatter, readingOf } from './document.js';
3
+ import { sectionSpans } from './markdown.js';
4
+ import { classifyHref } from './paths.js';
5
5
  import { refusal } from './refuse.js';
6
- import { headingSpans } from './sections.js';
7
6
  import { bodyStartOf } from './yaml.js';
8
- function internalEdits(content, bodyStart, file, documentTargets) {
9
- const inventory = linkInventory(content.slice(bodyStart));
7
+ function internalEdits(content, reading, file, documentTargets) {
10
8
  const internal = new Set();
9
+ const uses = reading.uses
10
+ .filter((use) => {
11
+ const resolved = classifyHref(file, use.destination.href);
12
+ return (resolved.kind === 'room' || (resolved.kind === 'path' && documentTargets.has(resolved.path)));
13
+ })
14
+ .sort((left, right) => left.start - right.start);
11
15
  const edits = [];
12
- const uses = [...inventory.uses].sort((left, right) => left.end - left.start - (right.end - right.start));
13
- for (const use of uses) {
14
- const resolved = target(file, use.destination.href);
15
- if (resolved === null || !documentTargets.has(resolved))
16
- continue;
16
+ for (let index = uses.length - 1; index >= 0; index -= 1) {
17
+ const use = uses[index];
17
18
  internal.add(use.destination.start);
18
- const label = { start: bodyStart + use.labelStart, end: bodyStart + use.labelEnd };
19
- const inner = edits
20
- .filter((edit) => edit.start >= label.start && edit.end <= label.end)
21
- .sort((left, right) => left.start - right.start);
22
- edits.push({
23
- start: bodyStart + use.start,
24
- end: bodyStart + use.end,
25
- text: rangeText(content, label, inner),
26
- });
19
+ const label = { start: use.labelStart, end: use.labelEnd };
20
+ const inner = [];
21
+ for (let nested = index + 1; nested < uses.length; nested += 1) {
22
+ const edit = edits[uses.length - 1 - nested];
23
+ if (edit.start >= label.end)
24
+ break;
25
+ if (edit.end <= label.end)
26
+ inner.push(edit);
27
+ }
28
+ edits.push({ start: use.start, end: use.end, text: rangeText(content, label, inner) });
27
29
  }
28
- for (const block of inventory.blocks) {
30
+ for (const block of reading.definitions) {
29
31
  if (!internal.has(block.destination.start))
30
32
  continue;
31
- edits.push({ start: bodyStart + block.start, end: bodyStart + block.end, text: '' });
33
+ edits.push({ start: block.start, end: block.end, text: '' });
32
34
  }
33
35
  return edits.sort((left, right) => left.start - right.start);
34
36
  }
37
+ export function firstEditFrom(edits, start) {
38
+ let low = 0;
39
+ let high = edits.length;
40
+ while (low < high) {
41
+ const middle = (low + high) >>> 1;
42
+ if (edits[middle].start < start)
43
+ low = middle + 1;
44
+ else
45
+ high = middle;
46
+ }
47
+ return low;
48
+ }
35
49
  export function rangeText(content, range, edits) {
36
50
  let text = '';
37
51
  let cursor = range.start;
38
- for (const edit of edits) {
52
+ for (let index = firstEditFrom(edits, range.start); index < edits.length; index += 1) {
53
+ const edit = edits[index];
54
+ if (edit.start > range.end)
55
+ break;
39
56
  if (edit.start < cursor || edit.end > range.end)
40
57
  continue;
41
58
  text += content.slice(cursor, edit.start) + edit.text;
@@ -73,7 +90,8 @@ function freeTextValues(definition, document) {
73
90
  export function projectionInput(document, file, context) {
74
91
  const content = document.content;
75
92
  const bodyStart = bodyStartOf(content);
76
- const edits = internalEdits(content, bodyStart, file, new Set(context.documentTargets));
93
+ const reading = readingOf(document);
94
+ const edits = internalEdits(content, reading, file, new Set(context.documentTargets));
77
95
  const declared = document.type;
78
96
  if (declared === undefined || declared === 'any') {
79
97
  return {
@@ -83,10 +101,10 @@ export function projectionInput(document, file, context) {
83
101
  }
84
102
  const definition = context.definition;
85
103
  if (definition === undefined) {
86
- return { ok: false, refusal: refusal(file, 'unknown-type', { location: 'type' }) };
104
+ return { ok: false, refusal: refusal(file, 'unknown-type') };
87
105
  }
88
106
  const structure = new Set(Object.entries(definition.sections).flatMap(([name, spec]) => (structural(spec) ? [name] : [])));
89
- const headings = headingSpans(content, bodyStart);
107
+ const headings = sectionSpans(reading, content.length);
90
108
  const ranges = [{ start: bodyStart, end: headings[0]?.headingStart ?? content.length }];
91
109
  for (const heading of headings) {
92
110
  if (structure.has(heading.name))