securemark 0.299.4 → 0.300.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.
- package/CHANGELOG.md +4 -0
- package/design.md +0 -6
- package/dist/index.js +2749 -1979
- package/index.d.ts +2 -1
- package/markdown.d.ts +209 -183
- package/package.json +1 -1
- package/src/api/bind.test.ts +0 -22
- package/src/api/bind.ts +15 -10
- package/src/api/header.ts +8 -5
- package/src/api/parse.test.ts +118 -122
- package/src/api/parse.ts +13 -31
- package/src/api/run.ts +6 -0
- package/src/api.ts +1 -0
- package/src/combinator/control/inits.ts +26 -0
- package/src/combinator/control/sequence.test.ts +38 -0
- package/src/combinator/control/sequence.ts +17 -0
- package/src/combinator/control/some.test.ts +41 -0
- package/src/combinator/{data/parser → control}/some.ts +39 -26
- package/src/combinator/control/state.ts +42 -0
- package/src/combinator/control/subsequence.test.ts +47 -0
- package/src/combinator/control/subsequence.ts +16 -0
- package/src/combinator/control/tails.ts +8 -0
- package/src/combinator/control/union.test.ts +37 -0
- package/src/combinator/control/union.ts +18 -0
- package/src/combinator/{data/delimiter.ts → delimiter.ts} +40 -60
- package/src/combinator/effect/backtrack.ts +64 -0
- package/src/combinator/effect/clock.ts +10 -0
- package/src/combinator/effect/precedence.ts +50 -0
- package/src/combinator/effect/recursion.ts +30 -0
- package/src/combinator/effect/scope.ts +100 -0
- package/src/combinator/effect/state.ts +72 -0
- package/src/combinator/{data/list → parser}/list.ts +35 -10
- package/src/combinator/parser.ts +293 -0
- package/src/combinator/process/bind.ts +34 -0
- package/src/combinator/process/block.test.ts +20 -0
- package/src/combinator/process/block.ts +33 -0
- package/src/combinator/process/clear.ts +16 -0
- package/src/combinator/process/contract.ts +35 -0
- package/src/combinator/process/duplicate.ts +7 -0
- package/src/combinator/process/error.ts +13 -0
- package/src/combinator/{control/manipulation → process}/fallback.ts +3 -3
- package/src/combinator/process/fence.ts +59 -0
- package/src/combinator/process/fmap.ts +10 -0
- package/src/combinator/process/indent.test.ts +31 -0
- package/src/combinator/process/indent.ts +51 -0
- package/src/combinator/process/lazy.ts +8 -0
- package/src/combinator/process/line.test.ts +21 -0
- package/src/combinator/process/line.ts +55 -0
- package/src/combinator/process/match.ts +37 -0
- package/src/combinator/process/reverse.ts +7 -0
- package/src/combinator/process/scope.ts +102 -0
- package/src/combinator/process/surround.ts +271 -0
- package/src/combinator.ts +28 -24
- package/src/debug.test.ts +11 -8
- package/src/parser/autolink.test.ts +17 -18
- package/src/parser/block/blockquote.test.ts +78 -79
- package/src/parser/block/blockquote.ts +32 -25
- package/src/parser/block/codeblock.test.ts +56 -57
- package/src/parser/block/codeblock.ts +44 -26
- package/src/parser/block/dlist.test.ts +56 -57
- package/src/parser/block/dlist.ts +5 -5
- package/src/parser/block/extension/aside.test.ts +8 -9
- package/src/parser/block/extension/aside.ts +76 -47
- package/src/parser/block/extension/example.test.ts +18 -19
- package/src/parser/block/extension/example.ts +88 -48
- package/src/parser/block/extension/fig.test.ts +37 -36
- package/src/parser/block/extension/fig.ts +20 -25
- package/src/parser/block/extension/figbase.test.ts +18 -19
- package/src/parser/block/extension/figbase.ts +3 -3
- package/src/parser/block/extension/figure.test.ts +62 -63
- package/src/parser/block/extension/figure.ts +23 -21
- package/src/parser/block/extension/message.test.ts +13 -14
- package/src/parser/block/extension/message.ts +52 -39
- package/src/parser/block/extension/placeholder.test.ts +13 -13
- package/src/parser/block/extension/placeholder.ts +23 -21
- package/src/parser/block/extension/table.test.ts +70 -71
- package/src/parser/block/extension/table.ts +43 -31
- package/src/parser/block/extension.test.ts +24 -24
- package/src/parser/block/extension.ts +3 -3
- package/src/parser/block/heading.test.ts +58 -59
- package/src/parser/block/heading.ts +19 -18
- package/src/parser/block/ilist.test.ts +8 -8
- package/src/parser/block/ilist.ts +9 -7
- package/src/parser/block/mathblock.test.ts +31 -32
- package/src/parser/block/mathblock.ts +24 -23
- package/src/parser/block/mediablock.ts +7 -7
- package/src/parser/block/olist.test.ts +102 -103
- package/src/parser/block/olist.ts +11 -12
- package/src/parser/block/pagebreak.test.ts +15 -16
- package/src/parser/block/pagebreak.ts +5 -5
- package/src/parser/block/paragraph.test.ts +57 -58
- package/src/parser/block/paragraph.ts +1 -1
- package/src/parser/block/reply/cite.test.ts +39 -40
- package/src/parser/block/reply/cite.ts +5 -5
- package/src/parser/block/reply/quote.test.ts +50 -51
- package/src/parser/block/reply/quote.ts +8 -7
- package/src/parser/block/reply.test.ts +19 -20
- package/src/parser/block/reply.ts +2 -2
- package/src/parser/block/sidefence.test.ts +41 -48
- package/src/parser/block/sidefence.ts +17 -11
- package/src/parser/block/table.test.ts +48 -49
- package/src/parser/block/table.ts +10 -9
- package/src/parser/block/ulist.test.ts +52 -53
- package/src/parser/block/ulist.ts +9 -8
- package/src/parser/block.ts +63 -51
- package/src/parser/context.ts +35 -32
- package/src/parser/document.ts +48 -0
- package/src/parser/header.test.ts +19 -20
- package/src/parser/header.ts +31 -25
- package/src/parser/inline/annotation.test.ts +49 -50
- package/src/parser/inline/annotation.ts +14 -16
- package/src/parser/inline/autolink/account.test.ts +32 -33
- package/src/parser/inline/autolink/account.ts +18 -19
- package/src/parser/inline/autolink/anchor.test.ts +21 -22
- package/src/parser/inline/autolink/anchor.ts +7 -8
- package/src/parser/inline/autolink/channel.test.ts +14 -15
- package/src/parser/inline/autolink/email.test.ts +36 -37
- package/src/parser/inline/autolink/email.ts +6 -6
- package/src/parser/inline/autolink/hashnum.test.ts +32 -33
- package/src/parser/inline/autolink/hashnum.ts +7 -8
- package/src/parser/inline/autolink/hashtag.test.ts +59 -60
- package/src/parser/inline/autolink/hashtag.ts +8 -9
- package/src/parser/inline/autolink/url.test.ts +75 -76
- package/src/parser/inline/autolink/url.ts +13 -14
- package/src/parser/inline/autolink.ts +24 -11
- package/src/parser/inline/bracket.test.ts +73 -74
- package/src/parser/inline/bracket.ts +88 -63
- package/src/parser/inline/code.test.ts +30 -31
- package/src/parser/inline/code.ts +6 -6
- package/src/parser/inline/deletion.test.ts +27 -28
- package/src/parser/inline/deletion.ts +5 -5
- package/src/parser/inline/emphasis.test.ts +39 -40
- package/src/parser/inline/emphasis.ts +5 -5
- package/src/parser/inline/emstrong.test.ts +101 -102
- package/src/parser/inline/emstrong.ts +103 -85
- package/src/parser/inline/extension/index.test.ts +91 -92
- package/src/parser/inline/extension/index.ts +17 -13
- package/src/parser/inline/extension/indexee.ts +4 -4
- package/src/parser/inline/extension/indexer.test.ts +23 -24
- package/src/parser/inline/extension/indexer.ts +6 -5
- package/src/parser/inline/extension/label.test.ts +32 -33
- package/src/parser/inline/extension/label.ts +14 -5
- package/src/parser/inline/extension/placeholder.test.ts +42 -43
- package/src/parser/inline/extension/placeholder.ts +8 -9
- package/src/parser/inline/html.test.ts +109 -109
- package/src/parser/inline/html.ts +27 -27
- package/src/parser/inline/htmlentity.test.ts +37 -38
- package/src/parser/inline/htmlentity.ts +6 -7
- package/src/parser/inline/insertion.test.ts +27 -28
- package/src/parser/inline/insertion.ts +5 -5
- package/src/parser/inline/italic.test.ts +55 -56
- package/src/parser/inline/italic.ts +5 -5
- package/src/parser/inline/link.test.ts +186 -187
- package/src/parser/inline/link.ts +29 -30
- package/src/parser/inline/mark.test.ts +31 -32
- package/src/parser/inline/mark.ts +6 -6
- package/src/parser/inline/math.test.ts +140 -141
- package/src/parser/inline/math.ts +6 -7
- package/src/parser/inline/media.test.ts +92 -93
- package/src/parser/inline/media.ts +31 -37
- package/src/parser/inline/reference.test.ts +111 -112
- package/src/parser/inline/reference.ts +61 -32
- package/src/parser/inline/remark.test.ts +49 -50
- package/src/parser/inline/remark.ts +13 -13
- package/src/parser/inline/ruby.test.ts +49 -50
- package/src/parser/inline/ruby.ts +60 -49
- package/src/parser/inline/shortmedia.test.ts +9 -10
- package/src/parser/inline/shortmedia.ts +11 -9
- package/src/parser/inline/strong.test.ts +36 -37
- package/src/parser/inline/strong.ts +5 -5
- package/src/parser/inline/template.test.ts +22 -23
- package/src/parser/inline/template.ts +13 -16
- package/src/parser/inline.test.ts +225 -226
- package/src/parser/inline.ts +68 -34
- package/src/parser/parser.ts +51 -0
- package/src/parser/repeat.ts +118 -91
- package/src/parser/segment.test.ts +0 -11
- package/src/parser/segment.ts +25 -28
- package/src/parser/source/escapable.test.ts +23 -24
- package/src/parser/source/escapable.ts +20 -20
- package/src/parser/source/line.test.ts +17 -18
- package/src/parser/source/line.ts +19 -24
- package/src/parser/source/str.ts +17 -10
- package/src/parser/source/text.test.ts +88 -89
- package/src/parser/source/text.ts +19 -20
- package/src/parser/source/unescapable.test.ts +23 -24
- package/src/parser/source/unescapable.ts +16 -16
- package/src/parser/util.ts +1 -1
- package/src/parser/visibility.ts +35 -14
- package/src/processor/figure.test.ts +20 -20
- package/src/processor/figure.ts +18 -10
- package/src/processor/note.test.ts +13 -13
- package/src/processor/note.ts +4 -2
- package/src/renderer/render/media/pdf.ts +2 -2
- package/src/renderer/render/media/twitter.ts +2 -2
- package/src/renderer/render/media.test.ts +12 -12
- package/src/renderer/render.test.ts +11 -11
- package/src/util/info.test.ts +2 -2
- package/src/util/quote.test.ts +3 -3
- package/src/util/quote.ts +6 -5
- package/src/util/toc.test.ts +12 -12
- package/src/combinator/control/constraint/block.test.ts +0 -20
- package/src/combinator/control/constraint/block.ts +0 -28
- package/src/combinator/control/constraint/contract.ts +0 -27
- package/src/combinator/control/constraint/line.test.ts +0 -21
- package/src/combinator/control/constraint/line.ts +0 -42
- package/src/combinator/control/manipulation/clear.ts +0 -5
- package/src/combinator/control/manipulation/convert.ts +0 -22
- package/src/combinator/control/manipulation/duplicate.ts +0 -7
- package/src/combinator/control/manipulation/fence.ts +0 -54
- package/src/combinator/control/manipulation/indent.test.ts +0 -31
- package/src/combinator/control/manipulation/indent.ts +0 -39
- package/src/combinator/control/manipulation/lazy.ts +0 -8
- package/src/combinator/control/manipulation/match.ts +0 -27
- package/src/combinator/control/manipulation/recovery.ts +0 -18
- package/src/combinator/control/manipulation/reverse.ts +0 -8
- package/src/combinator/control/manipulation/scope.ts +0 -61
- package/src/combinator/control/manipulation/surround.ts +0 -223
- package/src/combinator/control/monad/bind.ts +0 -26
- package/src/combinator/control/monad/fmap.ts +0 -10
- package/src/combinator/data/parser/context.ts +0 -96
- package/src/combinator/data/parser/inits.ts +0 -20
- package/src/combinator/data/parser/sequence.test.ts +0 -33
- package/src/combinator/data/parser/sequence.ts +0 -20
- package/src/combinator/data/parser/some.test.ts +0 -37
- package/src/combinator/data/parser/subsequence.test.ts +0 -41
- package/src/combinator/data/parser/subsequence.ts +0 -13
- package/src/combinator/data/parser/tails.ts +0 -8
- package/src/combinator/data/parser/union.test.ts +0 -33
- package/src/combinator/data/parser/union.ts +0 -18
- package/src/combinator/data/parser.ts +0 -144
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Input, Output } from '../parser';
|
|
2
|
+
|
|
3
|
+
export function spend(input: Input, output: Output<unknown>, cost: number): void {
|
|
4
|
+
assert(cost >= 0);
|
|
5
|
+
const resources = input.resources ?? { clock: cost, recursions: [1] };
|
|
6
|
+
if (resources.clock >= 0 && resources.clock - cost < 0) {
|
|
7
|
+
output.error ??= new Error('Too many creations');
|
|
8
|
+
}
|
|
9
|
+
resources.clock -= cost;
|
|
10
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Parser, Input } from '../parser';
|
|
2
|
+
import { Delimiters } from '../delimiter';
|
|
3
|
+
import { always } from '../control/state';
|
|
4
|
+
|
|
5
|
+
export class Precedence {
|
|
6
|
+
constructor(input: Input) {
|
|
7
|
+
this.delimiters = input.delimiters;
|
|
8
|
+
}
|
|
9
|
+
private readonly stack: number[] = [];
|
|
10
|
+
private readonly delimiters: Delimiters;
|
|
11
|
+
public get(): number {
|
|
12
|
+
return this.stack.at(-1) ?? 0;
|
|
13
|
+
}
|
|
14
|
+
public modify(precedence: number): void {
|
|
15
|
+
const p = this.get();
|
|
16
|
+
this.stack.push(precedence);
|
|
17
|
+
precedence > p && this.delimiters.shift(precedence);
|
|
18
|
+
}
|
|
19
|
+
public revert(precedence: number): void {
|
|
20
|
+
assert(this.stack.length > 0);
|
|
21
|
+
const p = this.stack.pop()!;
|
|
22
|
+
precedence > p && this.delimiters.unshift();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function precedence<P extends Parser>(precedence: number, parser: P): P;
|
|
27
|
+
export function precedence<T>(precedence: number, parser: Parser<T>): Parser<T> {
|
|
28
|
+
assert(precedence >= 0);
|
|
29
|
+
interface Memory {
|
|
30
|
+
readonly precedence: number;
|
|
31
|
+
}
|
|
32
|
+
return always<Parser<T, Input<Memory>>>([
|
|
33
|
+
(input, output) => {
|
|
34
|
+
const p = input.precedence;
|
|
35
|
+
precedence > p && input.delimiters.shift(precedence);
|
|
36
|
+
input.memory = {
|
|
37
|
+
precedence: p,
|
|
38
|
+
};
|
|
39
|
+
input.precedence = precedence;
|
|
40
|
+
return output.context;
|
|
41
|
+
},
|
|
42
|
+
parser,
|
|
43
|
+
(input, output) => {
|
|
44
|
+
const p = input.memory.precedence;
|
|
45
|
+
input.precedence = p;
|
|
46
|
+
precedence > p && input.delimiters.unshift();
|
|
47
|
+
return output.context;
|
|
48
|
+
},
|
|
49
|
+
]);
|
|
50
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Parser, Output } from '../parser';
|
|
2
|
+
import { always } from '../control/state';
|
|
3
|
+
import { min } from 'spica/alias';
|
|
4
|
+
|
|
5
|
+
export function recursion<P extends Parser>(index: number, parser: P): P;
|
|
6
|
+
export function recursion<T>(index: number, parser: Parser<T>): Parser<T> {
|
|
7
|
+
assert(index >= 0);
|
|
8
|
+
return always([
|
|
9
|
+
(input, output) => {
|
|
10
|
+
const resources = input.resources ?? { clock: 1, recursions: [1] };
|
|
11
|
+
const { recursions } = resources;
|
|
12
|
+
recur(output, recursions, index, 1);
|
|
13
|
+
return output.context;
|
|
14
|
+
},
|
|
15
|
+
parser,
|
|
16
|
+
(input, output) => {
|
|
17
|
+
const resources = input.resources ?? { clock: 1, recursions: [1] };
|
|
18
|
+
const { recursions } = resources;
|
|
19
|
+
recur(output, recursions, index, -1);
|
|
20
|
+
return output.context;
|
|
21
|
+
},
|
|
22
|
+
]);
|
|
23
|
+
}
|
|
24
|
+
export function recur(output: Output<unknown>, recursions: number[], index: number, size: number, force: boolean = false): void {
|
|
25
|
+
index = min(index, recursions.length && recursions.length - 1);
|
|
26
|
+
if (recursions[index] < size - +force) {
|
|
27
|
+
output.error ??= new Error('Too much recursion');
|
|
28
|
+
}
|
|
29
|
+
recursions[index] -= size;
|
|
30
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { Parser, Input, Output, subinput } from '../parser';
|
|
2
|
+
import { always } from '../control/state';
|
|
3
|
+
|
|
4
|
+
interface Memory {
|
|
5
|
+
readonly SID: number;
|
|
6
|
+
readonly source: string;
|
|
7
|
+
readonly position: number;
|
|
8
|
+
readonly offset: number;
|
|
9
|
+
readonly linebreak: number;
|
|
10
|
+
readonly range: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class Scope<I extends Input> {
|
|
14
|
+
constructor(input: I) {
|
|
15
|
+
this.inputs.push(input);
|
|
16
|
+
}
|
|
17
|
+
private readonly inputs: I[] = [];
|
|
18
|
+
private readonly memories: Memory[] = [];
|
|
19
|
+
public peek(): I {
|
|
20
|
+
//assert(this.inputs.length > 0);
|
|
21
|
+
return this.inputs.at(-1)!;
|
|
22
|
+
}
|
|
23
|
+
public focus(subsource: string): void {
|
|
24
|
+
const input = this.peek();
|
|
25
|
+
assert(subsource.length <= input.source.length);
|
|
26
|
+
assert(input.position - subsource.length >= 0);
|
|
27
|
+
input.position -= subsource.length;
|
|
28
|
+
const { SID, source, position, offset, linebreak, range } = input;
|
|
29
|
+
this.memories.push({
|
|
30
|
+
SID,
|
|
31
|
+
source,
|
|
32
|
+
position,
|
|
33
|
+
offset,
|
|
34
|
+
linebreak,
|
|
35
|
+
range,
|
|
36
|
+
});
|
|
37
|
+
subinput(subsource, input);
|
|
38
|
+
}
|
|
39
|
+
public unfocus(state = true, continuous = false): void {
|
|
40
|
+
assert(this.memories.length > 0);
|
|
41
|
+
const input = this.peek();
|
|
42
|
+
const { source, position } = input;
|
|
43
|
+
const memory = this.memories.pop()!;
|
|
44
|
+
input.SID = memory.SID;
|
|
45
|
+
input.position = state
|
|
46
|
+
? continuous && position > 0
|
|
47
|
+
? memory.position + position
|
|
48
|
+
: memory.position + source.length
|
|
49
|
+
: memory.position;
|
|
50
|
+
input.source = memory.source;
|
|
51
|
+
assert(position <= source.length);
|
|
52
|
+
input.offset = memory.offset;
|
|
53
|
+
input.linebreak = memory.linebreak;
|
|
54
|
+
input.range = memory.range;
|
|
55
|
+
}
|
|
56
|
+
public push(source: string): I {
|
|
57
|
+
const input = this.peek().clone(source);
|
|
58
|
+
this.inputs.push(input);
|
|
59
|
+
return input;
|
|
60
|
+
}
|
|
61
|
+
public pop(): I {
|
|
62
|
+
this.inputs.pop();
|
|
63
|
+
return this.peek();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function scope<P extends Parser>(
|
|
68
|
+
conv: (input: Parser.Input<P>, output: Output<Parser.Node<P>>) => string,
|
|
69
|
+
parser: P,
|
|
70
|
+
isolation: boolean,
|
|
71
|
+
): P;
|
|
72
|
+
export function scope<T>(
|
|
73
|
+
conv: (input: Input, output: Output<T>) => string,
|
|
74
|
+
parser: Parser<T>,
|
|
75
|
+
isolation: boolean,
|
|
76
|
+
): Parser<T> {
|
|
77
|
+
assert(parser);
|
|
78
|
+
return always([
|
|
79
|
+
(input, output) => {
|
|
80
|
+
const { source, position } = input;
|
|
81
|
+
const src = conv(input, output);
|
|
82
|
+
!isolation && input.position === position
|
|
83
|
+
? input.position = source.length
|
|
84
|
+
: 0;
|
|
85
|
+
isolation
|
|
86
|
+
? input.scope.push(src)
|
|
87
|
+
: input.scope.focus(src);
|
|
88
|
+
return output.context;
|
|
89
|
+
},
|
|
90
|
+
parser,
|
|
91
|
+
(input, output) => {
|
|
92
|
+
isolation
|
|
93
|
+
? input.scope.pop()
|
|
94
|
+
: input.scope.unfocus();
|
|
95
|
+
if (!output.state) return;
|
|
96
|
+
assert(input.position <= input.source.length);
|
|
97
|
+
return output.context;
|
|
98
|
+
},
|
|
99
|
+
]);
|
|
100
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Parser, Result, Input } from '../parser';
|
|
2
|
+
import { always } from '../control/state';
|
|
3
|
+
|
|
4
|
+
export class State<T> {
|
|
5
|
+
constructor(private readonly initial: T) {
|
|
6
|
+
}
|
|
7
|
+
private readonly stack: T[] = [];
|
|
8
|
+
public get(): T {
|
|
9
|
+
return this.stack.at(-1) ?? this.initial;
|
|
10
|
+
}
|
|
11
|
+
public put(state: T): void {
|
|
12
|
+
this.stack.push(state);
|
|
13
|
+
}
|
|
14
|
+
public delete(): T {
|
|
15
|
+
assert(this.stack.length > 0);
|
|
16
|
+
const state = this.stack.pop()!;
|
|
17
|
+
return state;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function state<P extends Parser>(state: number, parser: P): P;
|
|
22
|
+
export function state<P extends Parser>(state: number, positive: boolean, parser: P): P;
|
|
23
|
+
export function state<T>(state: number, positive: boolean | Parser<T>, parser?: Parser<T>): Parser<T> {
|
|
24
|
+
if (typeof positive === 'function') {
|
|
25
|
+
parser = positive;
|
|
26
|
+
positive = true;
|
|
27
|
+
}
|
|
28
|
+
assert(state);
|
|
29
|
+
assert(parser = parser!);
|
|
30
|
+
interface Memory {
|
|
31
|
+
readonly state: number;
|
|
32
|
+
}
|
|
33
|
+
return always<Parser<T, Input<Memory>>>([
|
|
34
|
+
(input, output) => {
|
|
35
|
+
const s = input.state;
|
|
36
|
+
input.state = positive
|
|
37
|
+
? s | state
|
|
38
|
+
: s & ~state;
|
|
39
|
+
input.memory = {
|
|
40
|
+
state: s,
|
|
41
|
+
};
|
|
42
|
+
return output.context;
|
|
43
|
+
},
|
|
44
|
+
parser,
|
|
45
|
+
(input, output) => {
|
|
46
|
+
input.state = input.memory.state;
|
|
47
|
+
return output.context;
|
|
48
|
+
},
|
|
49
|
+
]);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function constraint<P extends Parser>(state: number, parser: P): P;
|
|
53
|
+
//export function constraint<P extends Parser>(state: number, positive: boolean, parser: P): P;
|
|
54
|
+
export function constraint<T>(state: number, positive: boolean | Parser<T>, parser?: Parser<T>): Parser<T> {
|
|
55
|
+
if (typeof positive === 'function') {
|
|
56
|
+
parser = positive;
|
|
57
|
+
positive = false;
|
|
58
|
+
}
|
|
59
|
+
assert(state);
|
|
60
|
+
assert(parser = parser!);
|
|
61
|
+
return always([
|
|
62
|
+
(input, output) => {
|
|
63
|
+
const s = positive
|
|
64
|
+
? state & input.state
|
|
65
|
+
: state & ~input.state;
|
|
66
|
+
return s === state
|
|
67
|
+
? output.context
|
|
68
|
+
: Result.skip;
|
|
69
|
+
},
|
|
70
|
+
parser,
|
|
71
|
+
]);
|
|
72
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Parser,
|
|
1
|
+
import { Parser, Input } from '../parser';
|
|
2
2
|
|
|
3
|
-
export class List<N extends List.Node = List.Node,
|
|
3
|
+
export class List<N extends List.Node = List.Node, I extends Input = Input, S extends readonly Parser<unknown, I>[] = Parser<unknown, I, any[]>[]> {
|
|
4
4
|
constructor(nodes?: ArrayLike<N>) {
|
|
5
5
|
if (nodes === undefined) return;
|
|
6
6
|
for (let i = 0; i < nodes.length; ++i) {
|
|
@@ -15,8 +15,8 @@ export class List<N extends List.Node = List.Node, C extends Context = Context,
|
|
|
15
15
|
}
|
|
16
16
|
public insert(node: N, before?: N): N {
|
|
17
17
|
assert(!node.next && !node.prev);
|
|
18
|
-
if (before === undefined) return this.push(node);
|
|
19
|
-
if (before === this.head) return this.unshift(node);
|
|
18
|
+
if (before === undefined) return this.push(node), node;
|
|
19
|
+
if (before === this.head) return this.unshift(node), node;
|
|
20
20
|
if (++this.length === 1) {
|
|
21
21
|
return this.head = this.last = node;
|
|
22
22
|
}
|
|
@@ -43,21 +43,25 @@ export class List<N extends List.Node = List.Node, C extends Context = Context,
|
|
|
43
43
|
node.next = node.prev = undefined;
|
|
44
44
|
return node;
|
|
45
45
|
}
|
|
46
|
-
public unshift(node: N):
|
|
46
|
+
public unshift(node: N): this {
|
|
47
47
|
assert(!node.next && !node.prev);
|
|
48
48
|
if (++this.length === 1) {
|
|
49
|
-
|
|
49
|
+
this.head = this.last = node;
|
|
50
|
+
return this;
|
|
50
51
|
}
|
|
51
52
|
node.next = this.head;
|
|
52
|
-
|
|
53
|
+
this.head = this.head!.prev = node;
|
|
54
|
+
return this;
|
|
53
55
|
}
|
|
54
|
-
public push(node: N):
|
|
56
|
+
public push(node: N): this {
|
|
55
57
|
assert(!node.next && !node.prev);
|
|
56
58
|
if (++this.length === 1) {
|
|
57
|
-
|
|
59
|
+
this.head = this.last = node;
|
|
60
|
+
return this;
|
|
58
61
|
}
|
|
59
62
|
node.prev = this.last;
|
|
60
|
-
|
|
63
|
+
this.last = this.last!.next = node;
|
|
64
|
+
return this;
|
|
61
65
|
}
|
|
62
66
|
public shift(): N | undefined {
|
|
63
67
|
if (this.length === 0) return;
|
|
@@ -89,6 +93,18 @@ export class List<N extends List.Node = List.Node, C extends Context = Context,
|
|
|
89
93
|
list.clear();
|
|
90
94
|
return this;
|
|
91
95
|
}
|
|
96
|
+
public truncateBefore(node: N): void {
|
|
97
|
+
assert(node.next || node.prev || this.head === this.last);
|
|
98
|
+
if (node.prev === undefined) return;
|
|
99
|
+
this.delete(node.prev);
|
|
100
|
+
this.head = node;
|
|
101
|
+
}
|
|
102
|
+
public truncateAfter(node: N): void {
|
|
103
|
+
assert(node.next || node.prev || this.head === this.last);
|
|
104
|
+
if (node.next === undefined) return;
|
|
105
|
+
this.delete(node.next);
|
|
106
|
+
this.last = node;
|
|
107
|
+
}
|
|
92
108
|
public clear(): void {
|
|
93
109
|
this.length = 0;
|
|
94
110
|
this.head = this.last = undefined;
|
|
@@ -139,3 +155,12 @@ export namespace List {
|
|
|
139
155
|
prev?: this;
|
|
140
156
|
}
|
|
141
157
|
}
|
|
158
|
+
export class Node<N> implements List.Node {
|
|
159
|
+
constructor(
|
|
160
|
+
public value: N,
|
|
161
|
+
public flags: number = 0,
|
|
162
|
+
) {
|
|
163
|
+
}
|
|
164
|
+
public next?: this = undefined;
|
|
165
|
+
public prev?: this = undefined;
|
|
166
|
+
}
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { Delimiters } from './delimiter';
|
|
2
|
+
import { Backtrack } from './effect/backtrack';
|
|
3
|
+
import { Scope } from './effect/scope';
|
|
4
|
+
import { List, Node } from './parser/list';
|
|
5
|
+
|
|
6
|
+
// パーサーは基本的に成否の文脈に関わらず実行され成否による処理の分岐は可能な限り
|
|
7
|
+
// 文脈の検査でなく継続の分岐により行わなければならない。
|
|
8
|
+
export type Parser<T = unknown, I extends Input = Input, S extends SubParsers<unknown, I> = SubParsers<never, I>> =
|
|
9
|
+
(input: I, output: Output<T>) => Result<T, I, S>;
|
|
10
|
+
export type SubParsers<T, I extends Input = Input> = readonly Parser<T, I, SubParsers<never, I>>[];
|
|
11
|
+
export namespace Parser {
|
|
12
|
+
export type Node<P extends Parser> = P extends Parser<infer T> ? T : never;
|
|
13
|
+
export type Input<P extends Parser> = P extends Parser<unknown, infer I> ? I : never;
|
|
14
|
+
export type SubParsers<P extends Parser> = P extends Parser<unknown, any, infer S> ? S : never;
|
|
15
|
+
export type IntermediateParser<P extends Parser> = Parser<SubNode<P>, Input<P>, SubParsers<P>>;
|
|
16
|
+
export type SubNode<P extends Parser> = NonNever<ExtractSubNode<SubParsers<P>>, Node<P>>;
|
|
17
|
+
type NonNever<T, U> = [T] extends [never] ? U : T;
|
|
18
|
+
type ExtractSubNode<S extends readonly Parser[]> = ExtractSubParser<S> extends infer P ? P extends Parser<infer T> ? T : never : never;
|
|
19
|
+
type ExtractSubParser<S extends readonly Parser[]> = S extends readonly (infer P)[] ? P : never;
|
|
20
|
+
}
|
|
21
|
+
export type Result<T, I extends Input = Input, S extends SubParsers<unknown, I> = SubParsers<never, I>> =
|
|
22
|
+
| Result.Cont<T, I, S>
|
|
23
|
+
| Result.Fail
|
|
24
|
+
| Result.Skip;
|
|
25
|
+
export namespace Result {
|
|
26
|
+
export type Cont<T, I extends Input = Input, S extends SubParsers<unknown, I> = SubParsers<never, I>> =
|
|
27
|
+
readonly Parser<T, I, S>[];
|
|
28
|
+
export type Succ = readonly [];
|
|
29
|
+
export type Fail = undefined;
|
|
30
|
+
export type Skip = null;
|
|
31
|
+
export const succ = [] as const;
|
|
32
|
+
export const fail = undefined;
|
|
33
|
+
export const skip = null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let SID = 0;
|
|
37
|
+
function sid(): number {
|
|
38
|
+
return SID = ++SID >>> 0 || 1;
|
|
39
|
+
}
|
|
40
|
+
export class Input<M extends object = object> {
|
|
41
|
+
constructor(
|
|
42
|
+
{
|
|
43
|
+
source,
|
|
44
|
+
position,
|
|
45
|
+
resources,
|
|
46
|
+
linebreak,
|
|
47
|
+
range,
|
|
48
|
+
offset,
|
|
49
|
+
delimiters,
|
|
50
|
+
backtracks,
|
|
51
|
+
scope,
|
|
52
|
+
backtrack,
|
|
53
|
+
precedence,
|
|
54
|
+
state,
|
|
55
|
+
memory,
|
|
56
|
+
}: Options = {},
|
|
57
|
+
) {
|
|
58
|
+
this.source = source ?? '';
|
|
59
|
+
this.position = position ?? 0;
|
|
60
|
+
this.resources = resources;
|
|
61
|
+
this.linebreak = linebreak ?? 0;
|
|
62
|
+
this.range = range ?? 0;
|
|
63
|
+
this.offset = offset ?? 0;
|
|
64
|
+
this.delimiters = delimiters ?? new Delimiters();
|
|
65
|
+
this.backtracks = backtracks ?? {};
|
|
66
|
+
this.scope = scope as Scope<this> ?? new Scope(this);
|
|
67
|
+
this.backtrack = backtrack ?? new Backtrack(this.scope);
|
|
68
|
+
this.precedence = precedence ?? 0;
|
|
69
|
+
this.state = state ?? 0;
|
|
70
|
+
this.memory = memory ?? Queue.none;
|
|
71
|
+
}
|
|
72
|
+
public SID: number = sid();
|
|
73
|
+
public source: string;
|
|
74
|
+
public segment: number = Segment.unknown;
|
|
75
|
+
public position: number;
|
|
76
|
+
public readonly resources?: {
|
|
77
|
+
clock: number;
|
|
78
|
+
recursions: number[];
|
|
79
|
+
interval?: number;
|
|
80
|
+
};
|
|
81
|
+
public linebreak: number;
|
|
82
|
+
public range: number;
|
|
83
|
+
public offset: number;
|
|
84
|
+
public delimiters: Delimiters;
|
|
85
|
+
// Objectの内部実装を利用する。
|
|
86
|
+
// 探索木を直接使用する場合は探索速度が重要で挿入は相対的に少なく削除は不要かつ不確実であるため
|
|
87
|
+
// AVL木が適当と思われる。
|
|
88
|
+
// メモリの局所性を得るために木ごとに最初の数十から数百byte分のノードをプールしノードが不足した場合は
|
|
89
|
+
// 使い捨てノードを追加またはテーブルに移行するとよいだろう。
|
|
90
|
+
// 最大セグメントサイズ10KB内で探索コストが平均実行性能を圧迫するほど大きくなるとは考えにくいが
|
|
91
|
+
// 探索コストを減らすにはバックトラック位置数が規定数を超えた場合一定区間ごとに探索木を分割する方法が考えられる。
|
|
92
|
+
// 10KBの入力すべてを保持する探索木を1024文字ごとに分割するために必要なテーブルサイズは64bit*98=784byteとなる。
|
|
93
|
+
// 128文字ごとでもテーブルサイズは入力全体の1%未満であるため無視でき16文字ごとでさえ6.25%に過ぎない。
|
|
94
|
+
// 探索木のポインタによるオーバーヘッドを考慮すれば一定サイズ以上ではテーブルのほうが効率的となる。
|
|
95
|
+
// 区間別テーブルは固定サイズであるためプールして再使用できる。
|
|
96
|
+
// 従って分割時のデータ構造は区間ごとに探索木を動的に生成しデータ数に応じてテーブルに移行するのが最も効率的である。
|
|
97
|
+
// これにより最悪時間計算量線形化に要する最悪空間計算量が+1nに局限される。
|
|
98
|
+
// またはテーブルの参照が高速なら変換せず併用してもよい。
|
|
99
|
+
// 木とテーブルいずれにおいてもバックトラックデータとオーバーヘッドを合わせた追加データサイズの最大値は
|
|
100
|
+
// セグメントサイズに制約されるため入力サイズに対する最大追加データサイズの平均比率はかなり小さくなる。
|
|
101
|
+
// 必要なテーブルの最大サイズは最大セグメントサイズであるため最大追加データサイズは入力サイズにかかわらず
|
|
102
|
+
// 10KB*並列数に留まり最大数百文字以下の短文ならば数百byte*並列数となる。
|
|
103
|
+
//
|
|
104
|
+
// 1. データ数が規定数を超えたら区間テーブルを生成しデータを振り分ける。
|
|
105
|
+
// - 子ノードのポインタだけ保持するとしても1ノード複数データ保持で圧縮できるかは微妙。
|
|
106
|
+
// - 1ノードに2データ保持すれば2連続データを1/2の確率で捕捉し1バックトラックあたりの平均追加データサイズは
|
|
107
|
+
// -7byte(((16+1)*2-(16+2))*2+((16+1)*2-(16+2)*2)*2)/4=(32-4)/4=7の10byteに減少する。
|
|
108
|
+
// 2連続データの発生確率が1/5なら-3.2byteの13.8byte、1/10なら+0.4byteの17.4byteに増加する。
|
|
109
|
+
// - 1ノードに4データ保持すれば2連続データを3/4の確率で捕捉し1バックトラックあたりの平均追加データサイズは
|
|
110
|
+
// -9byte(((16+1)*2-(16+4))*3+((16+1)*2-(16+4)*2))/4=(42-6)/4=9の8byteに減少する。
|
|
111
|
+
// 2連続データの発生確率が1/5なら-3.6byteの13.4byte、1/10なら+1.2byteの18.2byteに増加する。
|
|
112
|
+
// 2. 区間内のデータ構造は探索木から開始しデータ数が規定数を超えたらテーブルに変換する。
|
|
113
|
+
// - 1ノード1データ1区間1024文字ならば1024<(64/8*2+1)*61から1区間61データ以上でテーブルのほうが小さくなる。
|
|
114
|
+
// - 64/8*2+1=17文字に1か所以下のバックトラックでテーブル以上の効率となる。
|
|
115
|
+
// - 通常の入力でバックトラックが17文字に平均1か所以上となることは考えられず
|
|
116
|
+
// 1段落数百文字あたり平均2、3か所以下が妥当な頻度でありこの場合の最大追加データサイズは
|
|
117
|
+
// 入力内の最大セグメントサイズの10%前後である。
|
|
118
|
+
//
|
|
119
|
+
public backtracks: Record<number, number>;
|
|
120
|
+
public readonly scope: Scope<this>;
|
|
121
|
+
public readonly backtrack: Backtrack;
|
|
122
|
+
public precedence: number;
|
|
123
|
+
public state: number;
|
|
124
|
+
// 必ず自分の継続ブロックの中で使い汚染を防ぐ
|
|
125
|
+
// 本来はスタックのほうが使い捨てオブジェクトを作らず効率的だが全体としては誤差か
|
|
126
|
+
public memory: M;
|
|
127
|
+
public clone(source: string): this {
|
|
128
|
+
// @ts-ignore
|
|
129
|
+
const i = input(source, new this.constructor(this)) as this;
|
|
130
|
+
i.offset = 0;
|
|
131
|
+
i.backtracks = {};
|
|
132
|
+
return i;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
export type Options = Partial<Input>;
|
|
136
|
+
export class Output<T> {
|
|
137
|
+
constructor(
|
|
138
|
+
public readonly data: [List<Node<T>>, ...List<Node<T>>[]] = [new List()],
|
|
139
|
+
) {
|
|
140
|
+
assert(data.length > 0);
|
|
141
|
+
}
|
|
142
|
+
public state: boolean = true;
|
|
143
|
+
public context: Result.Succ | Result.Fail = Result.succ;
|
|
144
|
+
public error?: Error = undefined;
|
|
145
|
+
public peek(): List<Node<T>> {
|
|
146
|
+
assert(this.data.length > 0);
|
|
147
|
+
return this.data.at(-1)!;
|
|
148
|
+
}
|
|
149
|
+
public prepend(node: Node<T>): Result.Succ | Result.Fail {
|
|
150
|
+
this.data.at(-1)!.unshift(node);
|
|
151
|
+
return Result.succ;
|
|
152
|
+
}
|
|
153
|
+
public append(node: Node<T>): Result.Succ | Result.Fail {
|
|
154
|
+
this.data.at(-1)!.push(node);
|
|
155
|
+
return Result.succ;
|
|
156
|
+
}
|
|
157
|
+
public import(list: List<Node<T>>): Result.Succ | Result.Fail {
|
|
158
|
+
this.data.at(-1)!.import(list);
|
|
159
|
+
return Result.succ;
|
|
160
|
+
}
|
|
161
|
+
public replace(list: List<Node<T>>): Result.Succ | Result.Fail {
|
|
162
|
+
assert(this.data.length > 1);
|
|
163
|
+
this.data[this.data.length - 1] = list;
|
|
164
|
+
return Result.succ;
|
|
165
|
+
}
|
|
166
|
+
public flat(): Result.Succ | Result.Fail {
|
|
167
|
+
assert(this.data.length > 1);
|
|
168
|
+
this.import(this.data.pop()!);
|
|
169
|
+
return Result.succ;
|
|
170
|
+
}
|
|
171
|
+
public push(list: List<Node<T>> = new List()): void {
|
|
172
|
+
this.data.push(list);
|
|
173
|
+
}
|
|
174
|
+
public pop(): List<Node<T>> {
|
|
175
|
+
//assert(this.data.length > +!force);
|
|
176
|
+
return this.data.pop()!;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
export const enum Segment {
|
|
180
|
+
unknown = 0,
|
|
181
|
+
read = 0,
|
|
182
|
+
write = 1,
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export { List, Node };
|
|
186
|
+
|
|
187
|
+
export function input<I extends Input>(source: string, input: I): I;
|
|
188
|
+
export function input(source: string, input?: Input): Input;
|
|
189
|
+
export function input(source: string, input: Input = new Input()): Input {
|
|
190
|
+
input = subinput(source, input);
|
|
191
|
+
input.segment &= Segment.write;
|
|
192
|
+
return input;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function subinput<I extends Input>(source: string, input: I): I;
|
|
196
|
+
export function subinput(source: string, input?: Input): Input;
|
|
197
|
+
export function subinput(source: string, input: Input = new Input()): Input {
|
|
198
|
+
input.SID = sid();
|
|
199
|
+
input.source = source;
|
|
200
|
+
input.offset += input.position;
|
|
201
|
+
input.position = 0;
|
|
202
|
+
input.linebreak = 0;
|
|
203
|
+
input.range = 0;
|
|
204
|
+
return input;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// 全継続を末尾再帰にすればスタック不要になるがパーサーを複合すると後処理の予約のスタックを
|
|
208
|
+
// 避けられないため不可能。
|
|
209
|
+
export function* run
|
|
210
|
+
<T, I extends Input, O extends Output<T>>
|
|
211
|
+
(parser: Parser<T, I>, input: I, output: O)
|
|
212
|
+
: Generator<void, void, void> {
|
|
213
|
+
assert(output.data.length > 0);
|
|
214
|
+
const { scope, resources = { clock: 0, interval: 0 } } = input;
|
|
215
|
+
const { interval = 0 } = resources;
|
|
216
|
+
const stack: Queue<Parser<T, I>>[] = [];
|
|
217
|
+
let index = stack.length;
|
|
218
|
+
let time = interval && Date.now();
|
|
219
|
+
for (let queue = Queue.from([parser]); ;) {
|
|
220
|
+
if (interval && resources.clock << 32 - 16 === 0 && Date.now() - time > interval) {
|
|
221
|
+
yield;
|
|
222
|
+
time = Date.now();
|
|
223
|
+
}
|
|
224
|
+
if (output.state && output.error) {
|
|
225
|
+
output.state = false;
|
|
226
|
+
output.context = Result.fail;
|
|
227
|
+
}
|
|
228
|
+
const input = scope.peek();
|
|
229
|
+
//assert(input.position <= input.source.length);
|
|
230
|
+
const result = queue.pop()(input, output);
|
|
231
|
+
|
|
232
|
+
if (result) {
|
|
233
|
+
//assert(result.every(f => f));
|
|
234
|
+
output.state = true;
|
|
235
|
+
output.context = Result.succ;
|
|
236
|
+
if (result.length !== 0) {
|
|
237
|
+
if (queue.length !== 0) {
|
|
238
|
+
queue.memory = input.memory;
|
|
239
|
+
stack[index++] = queue;
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
Queue.dispose(queue);
|
|
243
|
+
}
|
|
244
|
+
queue = Queue.from(result);
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
if (result === Result.skip) {
|
|
250
|
+
queue.length = 0;
|
|
251
|
+
}
|
|
252
|
+
output.state = false;
|
|
253
|
+
output.context = Result.fail;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (queue.length !== 0) continue;
|
|
257
|
+
Queue.dispose(queue);
|
|
258
|
+
if (index === 0) break;
|
|
259
|
+
queue = stack[--index];
|
|
260
|
+
scope.peek().memory = queue.memory;
|
|
261
|
+
//stack.length - index === 256 && stack.pop();
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
class Queue<T> {
|
|
266
|
+
private static readonly pool: Queue<any>[] = Array(256).fill(null);
|
|
267
|
+
private static index = 0;
|
|
268
|
+
public static from<T>(items: readonly T[]): Queue<T> {
|
|
269
|
+
if (this.index === 0) return new Queue(items);
|
|
270
|
+
const queue = this.pool[--this.index];
|
|
271
|
+
queue.items = items;
|
|
272
|
+
queue.length = items.length;
|
|
273
|
+
return queue;
|
|
274
|
+
}
|
|
275
|
+
public static dispose(queue: Queue<any>): void {
|
|
276
|
+
queue.items = Result.succ;
|
|
277
|
+
queue.memory = this.none;
|
|
278
|
+
if (this.index === this.pool.length) return;
|
|
279
|
+
this.pool[this.index++] = queue;
|
|
280
|
+
}
|
|
281
|
+
private constructor(
|
|
282
|
+
public items: readonly T[],
|
|
283
|
+
) {
|
|
284
|
+
}
|
|
285
|
+
public static readonly none = Object.freeze(Object.create(null));
|
|
286
|
+
public memory: object = Queue.none;
|
|
287
|
+
public length = this.items.length;
|
|
288
|
+
public pop(): T {
|
|
289
|
+
//assert(this.length > 0);
|
|
290
|
+
const { items } = this;
|
|
291
|
+
return items[items.length - this.length--];
|
|
292
|
+
}
|
|
293
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Parser, Result, Input, Output, List, Node } from '../parser';
|
|
2
|
+
import { always } from '../control/state';
|
|
3
|
+
|
|
4
|
+
type ResultM<T> = List<Node<T>> | undefined;
|
|
5
|
+
|
|
6
|
+
export function bind<P extends Parser>(parser: Parser.IntermediateParser<P>, f: (nodes: List<Node<Parser.SubNode<P>>>, Input: Parser.Input<P>, output: Output<Parser.Node<P>>) => ResultM<Parser.Node<P>>): P;
|
|
7
|
+
export function bind<P extends Parser>(parser: P, f: (nodes: List<Node<Parser.Node<P>>>, Input: Parser.Input<P>, output: Output<Parser.Node<P>>) => ResultM<Parser.Node<P>>): P;
|
|
8
|
+
export function bind<T, P extends Parser>(parser: Parser<T, Parser.Input<P>, Parser.SubParsers<P>>, f: (nodes: List<Node<T>>, Input: Parser.Input<P>, output: Output<Parser.Node<P>>) => ResultM<Parser.Node<P>>): P;
|
|
9
|
+
export function bind<U, P extends Parser>(parser: P, f: (nodes: List<Node<Parser.Node<P>>>, Input: Parser.Input<P>, output: Output<Parser.Node<P>>) => ResultM<U>): Parser<U, Parser.Input<P>, Parser.SubParsers<P>>;
|
|
10
|
+
export function bind<T>(parser: Parser<T>, f: (nodes: List<Node<T>>, Input: Input, output: Output<T>) => ResultM<T>): Parser<T> {
|
|
11
|
+
assert(parser);
|
|
12
|
+
interface Memory {
|
|
13
|
+
readonly position: number;
|
|
14
|
+
}
|
|
15
|
+
return always<Parser<T, Input<Memory>>>([
|
|
16
|
+
(input, output) => {
|
|
17
|
+
input.memory = {
|
|
18
|
+
position: input.position,
|
|
19
|
+
};
|
|
20
|
+
output.push();
|
|
21
|
+
return output.context;
|
|
22
|
+
},
|
|
23
|
+
parser,
|
|
24
|
+
(input, output) => {
|
|
25
|
+
const nodes = output.pop();
|
|
26
|
+
if (!output.state) return;
|
|
27
|
+
input.range = input.position - input.memory.position;
|
|
28
|
+
const result = f(nodes, input, output);
|
|
29
|
+
return result
|
|
30
|
+
? output.import(result)
|
|
31
|
+
: Result.fail;
|
|
32
|
+
},
|
|
33
|
+
]);
|
|
34
|
+
}
|