securemark 0.257.2 → 0.258.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.
Files changed (87) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/index.js +1238 -618
  3. package/markdown.d.ts +1 -12
  4. package/package.json +9 -9
  5. package/src/combinator/control/manipulation/convert.ts +8 -4
  6. package/src/combinator/control/manipulation/scope.ts +10 -2
  7. package/src/combinator/data/parser/context/delimiter.ts +70 -0
  8. package/src/combinator/data/parser/context/memo.ts +34 -0
  9. package/src/combinator/{control/manipulation → data/parser}/context.test.ts +9 -9
  10. package/src/combinator/data/parser/context.ts +158 -0
  11. package/src/combinator/data/parser/inits.ts +4 -3
  12. package/src/combinator/data/parser/sequence.test.ts +1 -1
  13. package/src/combinator/data/parser/sequence.ts +4 -3
  14. package/src/combinator/data/parser/some.test.ts +1 -1
  15. package/src/combinator/data/parser/some.ts +14 -37
  16. package/src/combinator/data/parser/subsequence.test.ts +1 -1
  17. package/src/combinator/data/parser/subsequence.ts +3 -3
  18. package/src/combinator/data/parser/tails.ts +3 -3
  19. package/src/combinator/data/parser/union.test.ts +1 -1
  20. package/src/combinator/data/parser.ts +6 -47
  21. package/src/combinator.ts +1 -2
  22. package/src/parser/api/bind.ts +5 -5
  23. package/src/parser/api/parse.test.ts +11 -8
  24. package/src/parser/api/parse.ts +3 -1
  25. package/src/parser/block/blockquote.ts +1 -1
  26. package/src/parser/block/dlist.ts +4 -10
  27. package/src/parser/block/extension/figure.ts +4 -3
  28. package/src/parser/block/extension/table.ts +2 -2
  29. package/src/parser/block/heading.ts +5 -13
  30. package/src/parser/block/ilist.ts +3 -2
  31. package/src/parser/block/olist.ts +10 -7
  32. package/src/parser/block/paragraph.ts +1 -1
  33. package/src/parser/block/reply/cite.ts +1 -1
  34. package/src/parser/block/reply/quote.ts +1 -1
  35. package/src/parser/block/reply.ts +1 -1
  36. package/src/parser/block/sidefence.ts +1 -1
  37. package/src/parser/block/table.test.ts +5 -0
  38. package/src/parser/block/table.ts +14 -13
  39. package/src/parser/block/ulist.ts +4 -3
  40. package/src/parser/block.ts +1 -1
  41. package/src/parser/context.ts +32 -0
  42. package/src/parser/header.ts +1 -1
  43. package/src/parser/inline/annotation.test.ts +5 -5
  44. package/src/parser/inline/annotation.ts +9 -17
  45. package/src/parser/inline/autolink/email.ts +1 -1
  46. package/src/parser/inline/autolink/url.ts +1 -1
  47. package/src/parser/inline/autolink.ts +5 -3
  48. package/src/parser/inline/bracket.ts +16 -15
  49. package/src/parser/inline/code.ts +1 -1
  50. package/src/parser/inline/comment.ts +4 -3
  51. package/src/parser/inline/deletion.ts +5 -4
  52. package/src/parser/inline/emphasis.ts +5 -4
  53. package/src/parser/inline/emstrong.ts +5 -4
  54. package/src/parser/inline/extension/index.ts +7 -14
  55. package/src/parser/inline/extension/indexee.ts +8 -10
  56. package/src/parser/inline/extension/indexer.ts +4 -3
  57. package/src/parser/inline/extension/label.ts +3 -2
  58. package/src/parser/inline/extension/placeholder.ts +5 -4
  59. package/src/parser/inline/html.ts +5 -4
  60. package/src/parser/inline/htmlentity.ts +1 -1
  61. package/src/parser/inline/insertion.ts +5 -4
  62. package/src/parser/inline/link.test.ts +2 -1
  63. package/src/parser/inline/link.ts +21 -27
  64. package/src/parser/inline/mark.ts +5 -4
  65. package/src/parser/inline/math.ts +1 -1
  66. package/src/parser/inline/media.test.ts +1 -0
  67. package/src/parser/inline/media.ts +8 -7
  68. package/src/parser/inline/reference.test.ts +5 -5
  69. package/src/parser/inline/reference.ts +10 -16
  70. package/src/parser/inline/ruby.test.ts +1 -0
  71. package/src/parser/inline/ruby.ts +4 -3
  72. package/src/parser/inline/shortmedia.ts +3 -2
  73. package/src/parser/inline/strong.ts +5 -4
  74. package/src/parser/inline/template.test.ts +1 -1
  75. package/src/parser/inline/template.ts +9 -6
  76. package/src/parser/inline.test.ts +2 -1
  77. package/src/parser/locale.ts +6 -7
  78. package/src/parser/processor/footnote.ts +5 -3
  79. package/src/parser/source/text.ts +1 -1
  80. package/src/parser/util.ts +0 -220
  81. package/src/parser/visibility.ts +205 -0
  82. package/src/util/info.ts +4 -2
  83. package/src/util/quote.ts +12 -15
  84. package/src/util/toc.ts +14 -17
  85. package/webpack.config.js +1 -0
  86. package/src/combinator/control/manipulation/context.ts +0 -70
  87. package/src/combinator/control/manipulation/resource.ts +0 -54
@@ -0,0 +1,205 @@
1
+ import { undefined } from 'spica/global';
2
+ import { MarkdownParser } from '../../markdown';
3
+ import { Parser, eval } from '../combinator/data/parser';
4
+ import { union, some, verify, convert, fmap } from '../combinator';
5
+ import { unsafehtmlentity } from './inline/htmlentity';
6
+ import { linebreak, unescsource } from './source';
7
+ import { State } from './context';
8
+ import { invisibleHTMLEntityNames } from './api/normalize';
9
+ import { reduce } from 'spica/memoize';
10
+ import { push } from 'spica/array';
11
+
12
+ export function visualize<P extends Parser<HTMLElement | string>>(parser: P): P;
13
+ export function visualize<T extends HTMLElement | string>(parser: Parser<T>): Parser<T> {
14
+ const blankline = new RegExp(
15
+ /^(?:\\$|\\?[^\S\n]|&IHN;|<wbr>)+$/.source.replace('IHN', `(?:${invisibleHTMLEntityNames.join('|')})`),
16
+ 'gm');
17
+ return union([
18
+ convert(
19
+ source => source.replace(blankline, line => line.replace(/[\\&<]/g, '\x1B$&')),
20
+ verify(parser, (ns, rest, context) => !rest && hasVisible(ns, context))),
21
+ some(union([linebreak, unescsource])),
22
+ ]);
23
+ }
24
+ function hasVisible(
25
+ nodes: readonly (HTMLElement | string)[],
26
+ { state = 0 }: MarkdownParser.Context = {},
27
+ ): boolean {
28
+ for (let i = 0; i < nodes.length; ++i) {
29
+ const node = nodes[i];
30
+ if (typeof node === 'string') {
31
+ if (node && node.trimStart()) return true;
32
+ }
33
+ else {
34
+ if (node.innerText.trimStart()) return true;
35
+ if (state & State.media ^ State.media &&
36
+ (node.classList.contains('media') || node.getElementsByClassName('media')[0])) return true;
37
+ }
38
+ }
39
+ return false;
40
+ }
41
+
42
+ export const regBlankStart = new RegExp(
43
+ /^(?:\\?[^\S\n]|&IHN;|<wbr>)+/.source.replace('IHN', `(?:${invisibleHTMLEntityNames.join('|')})`));
44
+
45
+ export function blankWith(delimiter: string | RegExp): RegExp;
46
+ export function blankWith(starting: '' | '\n', delimiter: string | RegExp): RegExp;
47
+ export function blankWith(starting: '' | '\n', delimiter?: string | RegExp): RegExp {
48
+ if (delimiter === undefined) return blankWith('', starting);
49
+ return new RegExp(String.raw
50
+ `^(?:(?=${
51
+ starting
52
+ })(?:\\?\s|&(?:${invisibleHTMLEntityNames.join('|')});|<wbr>)${starting && '+'})?${
53
+ typeof delimiter === 'string' ? delimiter.replace(/[*+()\[\]]/g, '\\$&') : delimiter.source
54
+ }`);
55
+ }
56
+
57
+ export function startLoose<P extends Parser<HTMLElement | string>>(parser: P, except?: string): P;
58
+ export function startLoose<T extends HTMLElement | string>(parser: Parser<T>, except?: string): Parser<T> {
59
+ return (source, context) =>
60
+ isStartLoose(source, context, except)
61
+ ? parser(source, context)
62
+ : undefined;
63
+ }
64
+ const isStartLoose = reduce((source: string, context: MarkdownParser.Context, except?: string): boolean => {
65
+ return isStartTight(source.replace(regBlankStart, ''), context, except);
66
+ }, (source, _, except = '') => `${source}\x1E${except}`);
67
+
68
+ export function startTight<P extends Parser<unknown>>(parser: P, except?: string): P;
69
+ export function startTight<T>(parser: Parser<T>, except?: string): Parser<T> {
70
+ return (source, context) =>
71
+ isStartTight(source, context, except)
72
+ ? parser(source, context)
73
+ : undefined;
74
+ }
75
+ const isStartTight = reduce((source: string, context: MarkdownParser.Context, except?: string): boolean => {
76
+ if (source === '') return true;
77
+ if (except && source.slice(0, except.length) === except) return false;
78
+ switch (source[0]) {
79
+ case ' ':
80
+ case ' ':
81
+ case '\t':
82
+ case '\n':
83
+ return false;
84
+ case '\\':
85
+ return source[1]?.trimStart() !== '';
86
+ case '&':
87
+ switch (true) {
88
+ case source.length > 2
89
+ && source[1] !== ' '
90
+ && eval(unsafehtmlentity(source, context))?.[0]?.trimStart() === '':
91
+ return false;
92
+ }
93
+ return true;
94
+ case '<':
95
+ switch (true) {
96
+ case source.length >= 5
97
+ && source[1] === 'w'
98
+ && source.slice(0, 5) === '<wbr>':
99
+ return false;
100
+ }
101
+ return true;
102
+ default:
103
+ return source[0].trimStart() !== '';
104
+ }
105
+ }, (source, _, except = '') => `${source}\x1E${except}`);
106
+
107
+ export function isStartLooseNodes(nodes: readonly (HTMLElement | string)[]): boolean {
108
+ if (nodes.length === 0) return true;
109
+ for (let i = 0; i < nodes.length; ++i) {
110
+ const node = nodes[i];
111
+ if (isVisible(node)) return true;
112
+ if (typeof node === 'object') {
113
+ if (node.tagName === 'BR') break;
114
+ if (node.className === 'linebreak') break;
115
+ }
116
+ }
117
+ return false;
118
+ }
119
+ export function isStartTightNodes(nodes: readonly (HTMLElement | string)[]): boolean {
120
+ if (nodes.length === 0) return true;
121
+ return isVisible(nodes[0], 0);
122
+ }
123
+ //export function isEndTightNodes(nodes: readonly (HTMLElement | string)[]): boolean {
124
+ // if (nodes.length === 0) return true;
125
+ // return isVisible(nodes[nodes.length - 1], -1);
126
+ //}
127
+ function isVisible(node: HTMLElement | string, strpos?: number): boolean {
128
+ switch (typeof node) {
129
+ case 'string':
130
+ const char = node && strpos !== undefined
131
+ ? node[strpos >= 0 ? strpos : node.length + strpos]
132
+ : node;
133
+ switch (char) {
134
+ case '':
135
+ case ' ':
136
+ case '\t':
137
+ case '\n':
138
+ return false;
139
+ default:
140
+ return char.trimStart() !== '';
141
+ }
142
+ default:
143
+ switch (node.tagName) {
144
+ case 'BR':
145
+ case 'WBR':
146
+ return false;
147
+ case 'SPAN':
148
+ return node.className !== 'linebreak';
149
+ default:
150
+ return true;
151
+ }
152
+ }
153
+ }
154
+
155
+ export function trimBlank<P extends Parser<HTMLElement | string>>(parser: P): P;
156
+ export function trimBlank<T extends HTMLElement | string>(parser: Parser<T>): Parser<T> {
157
+ return trimBlankStart(trimBlankEnd(parser));
158
+ }
159
+ export function trimBlankStart<P extends Parser<unknown>>(parser: P): P;
160
+ export function trimBlankStart<T>(parser: Parser<T>): Parser<T> {
161
+ return convert(
162
+ reduce(source => source.replace(regBlankStart, '')),
163
+ parser);
164
+ }
165
+ export function trimBlankEnd<P extends Parser<HTMLElement | string>>(parser: P): P;
166
+ export function trimBlankEnd<T extends HTMLElement | string>(parser: Parser<T>): Parser<T> {
167
+ return fmap(
168
+ parser,
169
+ trimNodeEnd);
170
+ }
171
+ export function trimNode<T extends HTMLElement | string>(nodes: T[]): T[] {
172
+ return trimNodeStart(trimNodeEnd(nodes));
173
+ }
174
+ function trimNodeStart<T extends HTMLElement | string>(nodes: T[]): T[] {
175
+ for (let node = nodes[0]; nodes.length > 0 && !isVisible(node = nodes[0], 0);) {
176
+ if (nodes.length === 1 && typeof node === 'object' && node.className === 'indexer') break;
177
+ if (typeof node === 'string') {
178
+ const pos = node.trimStart().length;
179
+ if (pos > 0) {
180
+ nodes[0] = node.slice(-pos) as T;
181
+ break;
182
+ }
183
+ }
184
+ nodes.shift();
185
+ }
186
+ return nodes;
187
+ }
188
+ function trimNodeEnd<T extends HTMLElement | string>(nodes: T[]): T[] {
189
+ const skip = nodes.length > 0 &&
190
+ typeof nodes[nodes.length - 1] === 'object' &&
191
+ nodes[nodes.length - 1]['className'] === 'indexer'
192
+ ? [nodes.pop()!]
193
+ : [];
194
+ for (let node = nodes[0]; nodes.length > 0 && !isVisible(node = nodes[nodes.length - 1], -1);) {
195
+ if (typeof node === 'string') {
196
+ const pos = node.trimEnd().length;
197
+ if (pos > 0) {
198
+ nodes[nodes.length - 1] = node.slice(0, pos) as T;
199
+ break;
200
+ }
201
+ }
202
+ nodes.pop();
203
+ }
204
+ return push(nodes, skip);
205
+ }
package/src/util/info.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Info } from '../..';
2
2
  import { scope } from './scope';
3
+ import { duffReduce } from 'spica/duff';
3
4
  import { push } from 'spica/array';
4
5
 
5
6
  export function info(source: DocumentFragment | HTMLElement | ShadowRoot): Info {
@@ -20,7 +21,8 @@ export function info(source: DocumentFragment | HTMLElement | ShadowRoot): Info
20
21
  };
21
22
 
22
23
  function find<T extends HTMLElement>(selector: string): T[] {
23
- return push([], source.querySelectorAll<T>(selector))
24
- .filter(match);
24
+ return duffReduce(source.querySelectorAll<T>(selector), (acc, el) =>
25
+ match(el) ? push(acc, [el]) : acc
26
+ , [] as T[]);
25
27
  }
26
28
  }
package/src/util/quote.ts CHANGED
@@ -2,32 +2,30 @@ import { Element } from 'spica/global';
2
2
  import { exec } from '../combinator/data/parser';
3
3
  import { cite } from '../parser/block/reply/cite';
4
4
  import { define } from 'typed-dom/dom';
5
+ import { duffEach } from 'spica/duff';
5
6
 
6
7
  export function quote(anchor: string, range: Range): string {
7
8
  if (exec(cite(`>>${anchor}`, {})) !== '') throw new Error(`Invalid anchor: ${anchor}`);
8
9
  fit(range);
9
10
  const node = trim(range.cloneContents());
10
11
  if (!node.firstChild) return '';
11
- for (
12
- let es = node.querySelectorAll('code[data-src], .math[data-src], .media[data-src], rt, rp'),
13
- i = 0, len = es.length; i < len; ++i) {
14
- const el = es[i];
12
+ duffEach(node.querySelectorAll('code[data-src], .math[data-src], .media[data-src], rt, rp'), el => {
15
13
  switch (true) {
16
14
  case el.matches('code'):
17
15
  case el.matches('.math'):
18
16
  define(el, el.getAttribute('data-src')!);
19
- continue;
17
+ return;
20
18
  case el.matches('.media'):
21
19
  el.replaceWith(
22
20
  /[\s{}]/.test(el.getAttribute('data-src')!)
23
21
  ? `!{ ${el.getAttribute('data-src')} }`
24
22
  : `!{${el.getAttribute('data-src')}}`);
25
- continue;
23
+ return;
26
24
  case el.matches('rt, rp'):
27
25
  el.remove();
28
- continue;
26
+ return;
29
27
  }
30
- }
28
+ });
31
29
  if (range.startOffset === 0 &&
32
30
  range.startContainer.parentElement?.matches('.cite, .quote') &&
33
31
  (!range.startContainer.previousSibling || range.startContainer.previousSibling.nodeName === 'BR')) {
@@ -37,26 +35,25 @@ export function quote(anchor: string, range: Range): string {
37
35
  node.prepend(`>>${anchor}\n> `);
38
36
  anchor = '';
39
37
  }
40
- for (let es = node.querySelectorAll('br'), i = 0, len = es.length; i < len; ++i) {
41
- const el = es[i];
38
+ duffEach(node.querySelectorAll('br'), el => {
42
39
  if (anchor && el.nextSibling instanceof Element && el.nextSibling.matches('.cite, .quote')) {
43
40
  el.replaceWith(`\n>${el.nextSibling.matches('.quote.invalid') ? ' ' : ''}`);
44
- continue;
41
+ return;
45
42
  }
46
43
  if (anchor && el.parentElement?.closest('.cite, .quote')) {
47
44
  el.replaceWith(`\n>${el.parentElement.closest('.quote.invalid') ? ' ' : ''}`);
48
- continue;
45
+ return;
49
46
  }
50
47
  if (anchor) {
51
48
  el.replaceWith(`\n>>${anchor}\n> `);
52
49
  anchor = '';
53
- continue;
50
+ return;
54
51
  }
55
52
  else {
56
53
  el.replaceWith(`\n> `);
57
- continue;
54
+ return;
58
55
  }
59
- }
56
+ });
60
57
  anchor && node.append(`\n>>${anchor}`);
61
58
  return node.textContent!;
62
59
  }
package/src/util/toc.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { undefined } from 'spica/global';
2
2
  import { html } from 'typed-dom/dom';
3
+ import { duffEach, duffReduce } from 'spica/duff';
3
4
  import { push } from 'spica/array';
4
5
 
5
6
  // Bug: Firefox
@@ -7,16 +8,15 @@ import { push } from 'spica/array';
7
8
  const selector = ':is(h1, h2, h3, h4, h5, h6, aside.aside)[id]';
8
9
 
9
10
  export function toc(source: DocumentFragment | HTMLElement | ShadowRoot): HTMLUListElement {
10
- const hs = push([], source.querySelectorAll(selector))
11
- .map(el => {
12
- assert(el.parentNode === source);
13
- switch (el.tagName) {
14
- case 'ASIDE':
15
- return html(el.firstElementChild!.tagName.toLowerCase() as 'h1', { id: el.id, class: 'aside' }, el.firstElementChild!.cloneNode(true).childNodes);
16
- default:
17
- return el as HTMLHeadingElement;
18
- }
19
- });
11
+ const hs = duffReduce(source.querySelectorAll(selector), (acc, el) => {
12
+ assert(el.parentNode === source);
13
+ switch (el.tagName) {
14
+ case 'ASIDE':
15
+ return push(acc, [html(el.firstElementChild!.tagName.toLowerCase() as 'h1', { id: el.id, class: 'aside' }, el.firstElementChild!.cloneNode(true).childNodes)]);
16
+ default:
17
+ return push(acc, [el as HTMLHeadingElement]);
18
+ }
19
+ }, [] as HTMLHeadingElement[]);
20
20
  return parse(cons(hs));
21
21
  }
22
22
 
@@ -35,7 +35,7 @@ function parse(node: Tree, index: string = ''): HTMLUListElement {
35
35
  : `${index}.${i}`;
36
36
  return html('li',
37
37
  push(
38
- [html('a', { href: `#${el.id}`, 'data-index': isHeading ? idx : undefined }, fix(el))],
38
+ [html('a', { href: `#${el.id}`, 'data-index': isHeading ? idx : undefined }, unlink(el.cloneNode(true)))],
39
39
  cs.length > 0 ? [parse(cs, idx)] : []));
40
40
  }));
41
41
  }
@@ -57,11 +57,8 @@ function level(h: HTMLHeadingElement): number {
57
57
  return +h.tagName[1];
58
58
  }
59
59
 
60
- function fix(h: HTMLHeadingElement): Iterable<Node> {
61
- h = h.cloneNode(true);
62
- for (let es = h.getElementsByTagName('a'), i = 0, len = es.length; i < len; ++i) {
63
- const el = es[i];
64
- el.replaceWith(...el.childNodes);
65
- }
60
+ function unlink(h: HTMLHeadingElement): Iterable<Node> {
61
+ duffEach(h.getElementsByTagName('a'), el =>
62
+ void el.replaceWith(...el.childNodes));
66
63
  return h.childNodes;
67
64
  }
package/webpack.config.js CHANGED
@@ -56,6 +56,7 @@ module.exports = env => {
56
56
  loader: 'ts-loader',
57
57
  options: {
58
58
  onlyCompileBundledFiles: true,
59
+ allowTsInNodeModules: true,
59
60
  },
60
61
  },
61
62
  ],
@@ -1,70 +0,0 @@
1
- import { undefined, WeakMap } from 'spica/global';
2
- import { hasOwnProperty, ObjectCreate } from 'spica/alias';
3
- import { Parser, Ctx, Context } from '../../data/parser';
4
- import { template } from 'spica/assign';
5
- import { type } from 'spica/type';
6
- import { memoize } from 'spica/memoize';
7
-
8
- export function guard<P extends Parser<unknown>>(f: (context: Context<P>) => boolean, parser: P): P;
9
- export function guard<T>(f: (context: Ctx) => boolean, parser: Parser<T>): Parser<T> {
10
- return (source, context) =>
11
- f(context)
12
- ? parser(source, context)
13
- : undefined;
14
- }
15
-
16
- export function reset<P extends Parser<unknown>>(base: Context<P>, parser: P): P;
17
- export function reset<T>(base: Ctx, parser: Parser<T>): Parser<T> {
18
- assert(Object.getPrototypeOf(base) === Object.prototype);
19
- assert(Object.freeze(base));
20
- return (source, context) =>
21
- parser(source, inherit(ObjectCreate(context), base));
22
- }
23
-
24
- export function context<P extends Parser<unknown>>(base: Context<P>, parser: P): P;
25
- export function context<T>(base: Ctx, parser: Parser<T>): Parser<T> {
26
- assert(Object.getPrototypeOf(base) === Object.prototype);
27
- assert(Object.freeze(base));
28
- const override = memoize<Ctx, Ctx>(context => inherit(ObjectCreate(context), base), new WeakMap());
29
- return (source, context) =>
30
- parser(source, override(context));
31
- }
32
-
33
- const inherit = template((prop, target, source) => {
34
- assert(!Object.isSealed(target));
35
- assert(Object.isExtensible(target));
36
- //assert(Object.freeze(source));
37
- if (target[prop] === source[prop]) return;
38
- switch (prop) {
39
- case 'resources':
40
- assert(typeof source[prop] === 'object');
41
- assert(target[prop] || !(prop in target));
42
- if (prop in target && !hasOwnProperty(target, prop)) return;
43
- return target[prop] = ObjectCreate(source[prop]);
44
- }
45
- assert(!target.hasOwnProperty(prop));
46
- switch (type(source[prop])) {
47
- case 'Object':
48
- assert(Object.getPrototypeOf(source[prop]) === Object.prototype);
49
- //assert(Object.freeze(source[prop]));
50
- switch (type(target[prop])) {
51
- case 'Object':
52
- return target[prop] = inherit(ObjectCreate(target[prop]), source[prop]);
53
- default:
54
- return target[prop] = source[prop];
55
- }
56
- default:
57
- return target[prop] = source[prop];
58
- }
59
- });
60
-
61
- export function precedence<P extends Parser<unknown>>(precedence: number, parser: P): P;
62
- export function precedence<T>(precedence: number, parser: Parser<T>): Parser<T> {
63
- return (source, context) => {
64
- const p = context.precedence;
65
- context.precedence = precedence;
66
- const result = parser(source, context);
67
- context.precedence = p;
68
- return result;
69
- };
70
- }
@@ -1,54 +0,0 @@
1
- import { Parser } from '../../data/parser';
2
-
3
- export function creator<P extends Parser<unknown>>(parser: P): P;
4
- export function creator<P extends Parser<unknown>>(cost: number, parser: P): P;
5
- export function creator(cost: number | Parser<unknown>, parser?: Parser<unknown>): Parser<unknown> {
6
- if (typeof cost === 'function') return creator(1, cost);
7
- assert(cost >= 0);
8
- return (source, context) => {
9
- const { resources = { budget: 1, recursion: 1 } } = context;
10
- if (resources.budget <= 0) throw new Error('Too many creations');
11
- if (resources.recursion <= 0) throw new Error('Too much recursion');
12
- --resources.recursion;
13
- const result = parser!(source, context);
14
- ++resources.recursion;
15
- if (result) {
16
- resources.budget -= cost;
17
- }
18
- return result;
19
- };
20
- }
21
-
22
- export function uncreator<P extends Parser<unknown>>(parser: P): P;
23
- export function uncreator<P extends Parser<unknown>>(cost: number, parser: P): P;
24
- export function uncreator(cost: number | Parser<unknown>, parser?: Parser<unknown>): Parser<unknown> {
25
- if (typeof cost === 'function') return uncreator(1, cost);
26
- assert(cost >= 0);
27
- return (source, context) => {
28
- const { resources = { budget: 1, recursion: 1 } } = context;
29
- if (resources.budget <= 0) throw new Error('Too many creations');
30
- if (resources.recursion <= 0) throw new Error('Too much recursion');
31
- ++resources.recursion;
32
- const result = parser!(source, context);
33
- --resources.recursion;
34
- if (result) {
35
- resources.budget += cost;
36
- }
37
- return result;
38
- };
39
- }
40
-
41
- export function recursion<P extends Parser<unknown>>(parser: P): P;
42
- export function recursion<P extends Parser<unknown>>(cost: number, parser: P): P;
43
- export function recursion(cost: number | Parser<unknown>, parser?: Parser<unknown>): Parser<unknown> {
44
- if (typeof cost === 'function') return recursion(1, cost);
45
- assert(cost >= 0);
46
- return (source, context) => {
47
- const { resources = { recursion: 1 } } = context;
48
- if (resources.recursion <= 0) throw new Error('Too much recursion');
49
- --resources.recursion;
50
- const result = parser!(source, context);
51
- ++resources.recursion;
52
- return result;
53
- };
54
- }