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,20 @@
|
|
|
1
|
+
import { block } from './block';
|
|
2
|
+
import { input } from '../parser';
|
|
3
|
+
import { inspect } from '../../debug.test';
|
|
4
|
+
|
|
5
|
+
describe('Unit: lib/parser/combinator/block', () => {
|
|
6
|
+
describe('block', () => {
|
|
7
|
+
it('invalid', () => {
|
|
8
|
+
assert.throws(() => inspect(block(() => []), (input(' \n'))));
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('valid', () => {
|
|
12
|
+
assert.deepStrictEqual(inspect(block(input => { input.position = input.source.length; return []; }), input('\n')), [[], '']);
|
|
13
|
+
assert.deepStrictEqual(inspect(block(input => { input.position = input.source.length; return []; }), input(' \n')), [[], '']);
|
|
14
|
+
assert.deepStrictEqual(inspect(block(input => { input.position = input.source.length; return []; }), input('\n\n')), [[], '']);
|
|
15
|
+
assert.deepStrictEqual(inspect(block(input => { input.position = input.source.length - 1; return []; }), input('\n\n')), [[], '\n']);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Parser, Result, Segment } from '../parser';
|
|
2
|
+
import { always } from '../control/state';
|
|
3
|
+
import { backtrack } from '../effect/backtrack';
|
|
4
|
+
import { isEmptyline } from './line';
|
|
5
|
+
|
|
6
|
+
export function block<P extends Parser>(parser: P, separation?: boolean, segment?: number): P;
|
|
7
|
+
export function block<T>(parser: Parser<T>, separation = true, segment = 0): Parser<T> {
|
|
8
|
+
assert(parser);
|
|
9
|
+
const cont: Result<T> = [
|
|
10
|
+
parser,
|
|
11
|
+
(input, output) => {
|
|
12
|
+
const { source, position } = input;
|
|
13
|
+
if (!output.state || separation && !isEmptyline(source, position)) return;
|
|
14
|
+
assert(position === source.length || source[position - 1] === '\n');
|
|
15
|
+
if (segment !== 0 && ~input.segment & Segment.write) {
|
|
16
|
+
input.segment = segment;
|
|
17
|
+
}
|
|
18
|
+
return output.context;
|
|
19
|
+
},
|
|
20
|
+
];
|
|
21
|
+
return backtrack(always([
|
|
22
|
+
(input, output) => {
|
|
23
|
+
const { source, position } = input;
|
|
24
|
+
if (position === source.length) return;
|
|
25
|
+
if (segment !== 0 && input.segment & Segment.write) {
|
|
26
|
+
if (input.segment !== (segment | Segment.write)) return;
|
|
27
|
+
input.position = source.length;
|
|
28
|
+
return output.context;
|
|
29
|
+
}
|
|
30
|
+
return cont;
|
|
31
|
+
},
|
|
32
|
+
]));
|
|
33
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Parser, SubParsers, Input } from '../parser';
|
|
2
|
+
import { always } from '../control/state';
|
|
3
|
+
|
|
4
|
+
export function clear<S extends SubParsers<unknown>, I extends Input>(parser: Parser<unknown, I, S>): Parser<never, I, S> {
|
|
5
|
+
return always([
|
|
6
|
+
(_, output) => {
|
|
7
|
+
output.push();
|
|
8
|
+
return output.context;
|
|
9
|
+
},
|
|
10
|
+
parser,
|
|
11
|
+
(_, output) => {
|
|
12
|
+
output.pop();
|
|
13
|
+
return output.context;
|
|
14
|
+
},
|
|
15
|
+
]);
|
|
16
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Parser, Result, Input, Output } from '../parser';
|
|
2
|
+
import { always } from '../control/state';
|
|
3
|
+
import { tester } from '../delimiter';
|
|
4
|
+
|
|
5
|
+
export function contract<P extends Parser>(patterns: string | RegExp, parser: P, cond: (input: Parser.Input<P>, output: Output<Parser.Node<P>>) => boolean): P;
|
|
6
|
+
export function contract<P extends Parser>(precond: (input: Parser.Input<P>, output: Output<Parser.Node<P>>) => boolean, parser: P, postcond: (input: Parser.Input<P>, output: Output<Parser.Node<P>>) => boolean): P;
|
|
7
|
+
export function contract<T>(precond: string | RegExp | ((input: Input, output: Output<T>) => boolean), parser: Parser<T>, postcond: (input: Input, output: Output<T>) => boolean): Parser<T> {
|
|
8
|
+
if (typeof precond !== 'function') {
|
|
9
|
+
precond = tester(precond, false);
|
|
10
|
+
}
|
|
11
|
+
return validate(precond, verify(parser, postcond));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function validate<P extends Parser>(pattern: string | RegExp, parser: P): P;
|
|
15
|
+
export function validate<P extends Parser>(cond: (input: Parser.Input<P>, output: Output<Parser.Node<P>>) => boolean, parser: P): P;
|
|
16
|
+
export function validate<T>(cond: string | RegExp | ((input: Input, output: Output<T>) => boolean), parser: Parser<T>): Parser<T> {
|
|
17
|
+
if (typeof cond !== 'function') {
|
|
18
|
+
cond = tester(cond, false);
|
|
19
|
+
}
|
|
20
|
+
return always([
|
|
21
|
+
(input, output) =>
|
|
22
|
+
output.state && cond(input, output) ? output.context : Result.skip,
|
|
23
|
+
parser,
|
|
24
|
+
]);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function verify<P extends Parser>(parser: P, cond: (input: Parser.Input<P>, output: Output<Parser.Node<P>>) => boolean): P;
|
|
28
|
+
export function verify<T>(parser: Parser<T>, cond: (input: Input, output: Output<T>) => boolean): Parser<T> {
|
|
29
|
+
assert(parser);
|
|
30
|
+
return always([
|
|
31
|
+
parser,
|
|
32
|
+
(input, output) =>
|
|
33
|
+
output.state && cond(input, output) ? output.context : Result.fail,
|
|
34
|
+
]);
|
|
35
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Parser, SubParsers, Input, List, Node } from '../parser';
|
|
2
|
+
import { fmap } from './fmap';
|
|
3
|
+
|
|
4
|
+
export function dup<T, I extends Input, S extends SubParsers<T, I>>(parser: Parser<T, I, S>): Parser<List<Node<T>>, I, S>;
|
|
5
|
+
export function dup<T>(parser: Parser<T>): Parser<List<Node<T>>> {
|
|
6
|
+
return fmap(parser, nodes => new List([new Node(nodes)]));
|
|
7
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Parser } from '../parser';
|
|
2
|
+
import { always } from '../control/state';
|
|
3
|
+
|
|
4
|
+
export function error<P extends Parser>(parser: P): P;
|
|
5
|
+
export function error<T>(parser: Parser<T>): Parser<T> {
|
|
6
|
+
assert(parser);
|
|
7
|
+
return always([
|
|
8
|
+
(input, output) => {
|
|
9
|
+
if (!output.error) return output.context;
|
|
10
|
+
return parser(input, output);
|
|
11
|
+
},
|
|
12
|
+
]);
|
|
13
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Parser } from '
|
|
2
|
-
import { union } from '
|
|
1
|
+
import { Parser, SubParsers } from '../parser';
|
|
2
|
+
import { union } from '../control/union';
|
|
3
3
|
|
|
4
|
-
export function fallback<P extends Parser>(parser: P, otherwise: Parser<Parser.Node<P>, Parser.
|
|
4
|
+
export function fallback<P extends Parser>(parser: P, otherwise: Parser<Parser.Node<P>, Parser.Input<P>, SubParsers<Parser.SubNode<P>, Parser.Input<P>>>): P;
|
|
5
5
|
export function fallback<N>(parser: Parser<N>, otherwise: Parser<N>): Parser<N> {
|
|
6
6
|
return union([parser, otherwise]);
|
|
7
7
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Parser, SubParsers, Input, List, Node } from '../parser';
|
|
2
|
+
import { spend } from '../effect/clock';
|
|
3
|
+
import { firstline, isEmptyline } from './line';
|
|
4
|
+
|
|
5
|
+
export function fence<I extends Input, S extends SubParsers<never, I>>(opener: RegExp, write: boolean, limit: number, separation = true): Parser<string, I, S> {
|
|
6
|
+
assert(!opener.flags.match(/[gm]/) && opener.sticky && !opener.source.startsWith('^'));
|
|
7
|
+
return (input, output) => {
|
|
8
|
+
const { source, position } = input;
|
|
9
|
+
if (position === source.length) return;
|
|
10
|
+
opener.lastIndex = position;
|
|
11
|
+
const matches = opener.exec(source);
|
|
12
|
+
if (!matches) return;
|
|
13
|
+
assert(matches[0] === firstline(source, position));
|
|
14
|
+
spend(input, output, matches[0].length);
|
|
15
|
+
const delim = matches[1];
|
|
16
|
+
assert(delim && delim === delim.trim());
|
|
17
|
+
if (matches[0].includes(delim, delim.length)) return;
|
|
18
|
+
input.position += matches[0].length;
|
|
19
|
+
// Prevent annoying parsing in editing.
|
|
20
|
+
const secondline = firstline(source, input.position);
|
|
21
|
+
if (isEmptyline(secondline, 0) &&
|
|
22
|
+
firstline(source, input.position + secondline.length).trimEnd() !== delim) {
|
|
23
|
+
input.position -= matches[0].length;
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
let body = '';
|
|
27
|
+
let closer = '';
|
|
28
|
+
let overflow = '';
|
|
29
|
+
for (let count = 1; ; ++count) {
|
|
30
|
+
if (input.position === source.length) break;
|
|
31
|
+
const line = firstline(source, input.position);
|
|
32
|
+
if ((closer || count > limit + 1) && isEmptyline(line, 0)) break;
|
|
33
|
+
if(closer) {
|
|
34
|
+
overflow += line;
|
|
35
|
+
}
|
|
36
|
+
if (!closer && count <= limit + 1 && line.startsWith(delim) && line.trimEnd() === delim) {
|
|
37
|
+
closer = line;
|
|
38
|
+
if (isEmptyline(source, input.position + line.length)) {
|
|
39
|
+
input.position += line.length;
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
if (!separation) {
|
|
43
|
+
input.position += line.length;
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
assert(!overflow);
|
|
47
|
+
overflow = line;
|
|
48
|
+
}
|
|
49
|
+
if (!overflow) {
|
|
50
|
+
body += line;
|
|
51
|
+
}
|
|
52
|
+
input.position += line.length;
|
|
53
|
+
}
|
|
54
|
+
write && output.push(
|
|
55
|
+
new List([body, overflow, closer].map(str => new Node(str)))
|
|
56
|
+
.import(new List(matches.map(str => new Node(str)))));
|
|
57
|
+
return output.context;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Parser, Input, Output, List, Node } from '../parser';
|
|
2
|
+
import { bind } from './bind';
|
|
3
|
+
|
|
4
|
+
export function fmap<P extends Parser>(parser: Parser.IntermediateParser<P>, f: (nodes: List<Node<Parser.SubNode<P>>>, input: Parser.Input<P>, output: Output<Parser.Node<P>>) => List<Node<Parser.Node<P>>>): P;
|
|
5
|
+
export function fmap<P extends Parser>(parser: P, f: (nodes: List<Node<Parser.Node<P>>>, input: Parser.Input<P>, output: Output<Parser.Node<P>>) => List<Node<Parser.Node<P>>>): P;
|
|
6
|
+
export function fmap<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>>) => List<Node<Parser.Node<P>>>): P;
|
|
7
|
+
export function fmap<U, P extends Parser>(parser: P, f: (nodes: List<Node<Parser.Node<P>>>, input: Parser.Input<P>, output: Output<Parser.Node<P>>) => List<Node<U>>): Parser<U, Parser.Input<P>, Parser.SubParsers<P>>;
|
|
8
|
+
export function fmap<T>(parser: Parser<T>, f: (nodes: List<Node<T>>, input: Input, output: Output<T>) => List<Node<T>>): Parser<T> {
|
|
9
|
+
return bind(parser, (nodes, input, output) => f(nodes, input, output));
|
|
10
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { indent } from './indent';
|
|
2
|
+
import { Node, input } from '../parser';
|
|
3
|
+
import { inspect } from '../../debug.test';
|
|
4
|
+
|
|
5
|
+
describe('Unit: lib/parser/combinator/indent', () => {
|
|
6
|
+
describe('indent', () => {
|
|
7
|
+
it('valid', () => {
|
|
8
|
+
const parser = indent((input, output) => output.append(new Node(input.source)) && output.context);
|
|
9
|
+
assert.deepStrictEqual(inspect(parser, input('')), undefined);
|
|
10
|
+
assert.deepStrictEqual(inspect(parser, input(' ')), undefined);
|
|
11
|
+
assert.deepStrictEqual(inspect(parser, input(' ')), undefined);
|
|
12
|
+
assert.deepStrictEqual(inspect(parser, input('a ')), undefined);
|
|
13
|
+
assert.deepStrictEqual(inspect(parser, input(' a')), [['a'], '']);
|
|
14
|
+
assert.deepStrictEqual(inspect(parser, input(' a ')), [['a '], '']);
|
|
15
|
+
assert.deepStrictEqual(inspect(parser, input(' a\n')), [['a'], '']);
|
|
16
|
+
assert.deepStrictEqual(inspect(parser, input(' a \n')), [['a '], '']);
|
|
17
|
+
assert.deepStrictEqual(inspect(parser, input(' a')), [['a'], '']);
|
|
18
|
+
assert.deepStrictEqual(inspect(parser, input(' a')), [['a'], '']);
|
|
19
|
+
assert.deepStrictEqual(inspect(parser, input(' a')), [['a'], '']);
|
|
20
|
+
assert.deepStrictEqual(inspect(parser, input(' a')), [[' a'], '']);
|
|
21
|
+
assert.deepStrictEqual(inspect(parser, input(' a\n a')), [['a\na'], '']);
|
|
22
|
+
assert.deepStrictEqual(inspect(parser, input(' a\n a')), [['a\n a'], '']);
|
|
23
|
+
assert.deepStrictEqual(inspect(parser, input(' a\n a')), [['a\n a'], '']);
|
|
24
|
+
assert.deepStrictEqual(inspect(parser, input(' a\n a')), [['a'], ' a']);
|
|
25
|
+
assert.deepStrictEqual(inspect(parser, input(' \ta')), [['\ta'], '']);
|
|
26
|
+
assert.deepStrictEqual(inspect(parser, input('\ta')), [['a'], '']);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Parser, Result, Node } from '../parser';
|
|
2
|
+
import { some } from '../control/some';
|
|
3
|
+
import { always } from '../control/state';
|
|
4
|
+
import { block } from './block';
|
|
5
|
+
import { line } from './line';
|
|
6
|
+
import { match } from './match';
|
|
7
|
+
import { open } from './surround';
|
|
8
|
+
import { memoize } from 'spica/memoize';
|
|
9
|
+
|
|
10
|
+
export function indent<P extends Parser>(parser: P, separation?: boolean): P;
|
|
11
|
+
export function indent<P extends Parser>(opener: RegExp, parser: P, separation?: boolean): P;
|
|
12
|
+
export function indent(opener: RegExp | Parser<string>, parser: Parser<string> | boolean = false, separation = false): Parser<string> {
|
|
13
|
+
if (typeof opener === 'function') {
|
|
14
|
+
separation = parser as boolean;
|
|
15
|
+
parser = opener;
|
|
16
|
+
opener = / {1,4}|\t{1,2}/y;
|
|
17
|
+
}
|
|
18
|
+
assert(!opener.flags.match(/[gm]/) && opener.sticky && !opener.source.startsWith('^'));
|
|
19
|
+
assert(parser = parser as Parser<string>);
|
|
20
|
+
return always([
|
|
21
|
+
(input, output) => {
|
|
22
|
+
if (input.position === input.source.length) return Result.skip;
|
|
23
|
+
output.push();
|
|
24
|
+
return output.context;
|
|
25
|
+
},
|
|
26
|
+
block(match(opener, memoize(
|
|
27
|
+
([indent]) =>
|
|
28
|
+
some(open(indent, line(({ source }, output) =>
|
|
29
|
+
output.append(new Node(source))))),
|
|
30
|
+
([indent]) => indent.length * 2 + -(indent[0] === ' '), [], 2 ** 4 - 1)),
|
|
31
|
+
separation),
|
|
32
|
+
(input, output) => {
|
|
33
|
+
const source = trimBlockEnd(output.pop().foldl((acc, node) => acc + node.value, ''));
|
|
34
|
+
if (source === '') return Result.skip;
|
|
35
|
+
input.scope.focus(source);
|
|
36
|
+
return output.context;
|
|
37
|
+
},
|
|
38
|
+
parser,
|
|
39
|
+
(input, output) => {
|
|
40
|
+
input.scope.unfocus();
|
|
41
|
+
return output.context;
|
|
42
|
+
},
|
|
43
|
+
]);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function trimBlockEnd(block: string): string {
|
|
47
|
+
return block === ''
|
|
48
|
+
|| block.at(-1) !== '\n'
|
|
49
|
+
? block
|
|
50
|
+
: block.slice(0, block.at(-2) === '\r' ? -2 : -1);
|
|
51
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Parser } from '../parser';
|
|
2
|
+
|
|
3
|
+
export function lazy<P extends Parser>(builder: () => P): P;
|
|
4
|
+
export function lazy<T>(builder: () => Parser<T>): Parser<T> {
|
|
5
|
+
let parser: Parser<T> = (input, output) =>
|
|
6
|
+
(parser = builder())(input, output);
|
|
7
|
+
return (input, output) => parser(input, output);
|
|
8
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { input } from '../parser';
|
|
2
|
+
import { line } from './line';
|
|
3
|
+
import { inspect } from '../../debug.test';
|
|
4
|
+
|
|
5
|
+
describe('Unit: lib/parser/combinator/line', () => {
|
|
6
|
+
describe('line', () => {
|
|
7
|
+
it('invalid', () => {
|
|
8
|
+
assert.deepStrictEqual(inspect(line(() => []), input('')), undefined);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('valid', () => {
|
|
12
|
+
assert.deepStrictEqual(inspect(line(input => { input.position = input.source.length; return []; }), input(' ')), [[], '']);
|
|
13
|
+
assert.deepStrictEqual(inspect(line(input => { input.position = input.source.length; return []; }), input('\n')), [[], '']);
|
|
14
|
+
assert.deepStrictEqual(inspect(line(input => { input.position = input.source.length; return []; }), input('\n\n')), [[], '\n']);
|
|
15
|
+
assert.deepStrictEqual(inspect(line(input => { input.position = input.source.length; return []; }), input(' \n')), [[], '']);
|
|
16
|
+
assert.deepStrictEqual(inspect(line(input => { input.position = input.source.length - 1; return []; }), input(' \n')), [[], '']);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Parser, Result, Input } from '../parser';
|
|
2
|
+
import { always } from '../control/state';
|
|
3
|
+
|
|
4
|
+
export function line<P extends Parser>(parser: P, slice?: boolean): P;
|
|
5
|
+
export function line<T>(parser: Parser<T>, slice: boolean = true): Parser<T> {
|
|
6
|
+
assert(parser);
|
|
7
|
+
interface Memory {
|
|
8
|
+
readonly position: number;
|
|
9
|
+
}
|
|
10
|
+
return always<Parser<T, Input<Memory>>>([
|
|
11
|
+
(input, output) => {
|
|
12
|
+
const { source, position } = input;
|
|
13
|
+
if (position === source.length) return Result.skip;
|
|
14
|
+
input.memory = {
|
|
15
|
+
position,
|
|
16
|
+
};
|
|
17
|
+
if (slice) {
|
|
18
|
+
const line = firstline(source, position);
|
|
19
|
+
input.position += line.length;
|
|
20
|
+
input.scope.focus(line);
|
|
21
|
+
}
|
|
22
|
+
return output.context;
|
|
23
|
+
},
|
|
24
|
+
parser,
|
|
25
|
+
(input, output) => {
|
|
26
|
+
if (slice) {
|
|
27
|
+
input.scope.unfocus(output.state, true);
|
|
28
|
+
}
|
|
29
|
+
if (!output.state) return;
|
|
30
|
+
const { source, position, memory } = input;
|
|
31
|
+
if (position === memory.position || position !== source.length && source[position - 1] !== '\n' && isEmptyline(source, position)) {
|
|
32
|
+
input.position = source.indexOf('\n', position) + 1 || source.length;
|
|
33
|
+
}
|
|
34
|
+
return output.state && (input.position === source.length || source[input.position - 1] === '\n')
|
|
35
|
+
? output.context
|
|
36
|
+
: Result.fail;
|
|
37
|
+
},
|
|
38
|
+
]);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function firstline(source: string, position: number): string {
|
|
42
|
+
if (position === source.length) return '';
|
|
43
|
+
const i = source.indexOf('\n', position);
|
|
44
|
+
return i === -1
|
|
45
|
+
? source.slice(position)
|
|
46
|
+
: source.slice(position, i + 1);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const emptyline = /[^\S\r\n]*(?:$|\r?\n)/y;
|
|
50
|
+
export function isEmptyline(source: string, position: number): boolean {
|
|
51
|
+
emptyline.lastIndex = position;
|
|
52
|
+
return source.length === position
|
|
53
|
+
|| source[position] === '\n'
|
|
54
|
+
|| emptyline.test(source);
|
|
55
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Parser, Result, Input } from '../parser';
|
|
2
|
+
import { always } from '../control/state';
|
|
3
|
+
import { spend } from '../effect/clock';
|
|
4
|
+
|
|
5
|
+
export function match<P extends Parser>(pattern: RegExp, f: (matched: RegExpMatchArray) => P): P;
|
|
6
|
+
export function match<T>(pattern: RegExp, f: (matched: RegExpMatchArray) => Parser<T>): Parser<T> {
|
|
7
|
+
assert(!pattern.flags.match(/[gm]/) && pattern.sticky && !pattern.source.startsWith('^'));
|
|
8
|
+
interface Memory {
|
|
9
|
+
readonly position: number;
|
|
10
|
+
readonly range: number;
|
|
11
|
+
}
|
|
12
|
+
return always<Parser<T, Input<Memory>>>([
|
|
13
|
+
(input, output) => {
|
|
14
|
+
const { source, position } = input;
|
|
15
|
+
if (position === source.length) return Result.skip;
|
|
16
|
+
pattern.lastIndex = position;
|
|
17
|
+
const params = pattern.exec(source);
|
|
18
|
+
if (!params) return Result.skip;
|
|
19
|
+
assert(source.startsWith(params[0], position));
|
|
20
|
+
spend(input, output, params[0].length);
|
|
21
|
+
input.memory = {
|
|
22
|
+
position,
|
|
23
|
+
range: params[0].length,
|
|
24
|
+
};
|
|
25
|
+
return f(params)(input, output);
|
|
26
|
+
},
|
|
27
|
+
(input, output) => {
|
|
28
|
+
const { position, memory } = input;
|
|
29
|
+
input.position += output.state
|
|
30
|
+
? position === memory.position
|
|
31
|
+
? memory.range
|
|
32
|
+
: 0
|
|
33
|
+
: position - memory.position;
|
|
34
|
+
return output.context;
|
|
35
|
+
},
|
|
36
|
+
]);
|
|
37
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Parser, List, Node } from '../parser';
|
|
2
|
+
import { fmap } from './fmap';
|
|
3
|
+
|
|
4
|
+
export function reverse<P extends Parser>(parser: P): P;
|
|
5
|
+
export function reverse<T>(parser: Parser<T>): Parser<T> {
|
|
6
|
+
return fmap(parser, nodes => nodes.foldr((node, acc) => acc.push(nodes.delete(node)), new List<Node<T>>()));
|
|
7
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { Parser, Result, Input } from '../parser';
|
|
2
|
+
import { matcher } from '../delimiter';
|
|
3
|
+
import { always } from '../control/state';
|
|
4
|
+
import { clear } from './clear';
|
|
5
|
+
|
|
6
|
+
export function focus<P extends Parser>(scope: string | RegExp, parser: P, slice?: boolean): P;
|
|
7
|
+
export function focus<T>(scope: string | RegExp, parser: Parser<T>, slice = true): Parser<T> {
|
|
8
|
+
assert(parser);
|
|
9
|
+
interface Memory {
|
|
10
|
+
readonly position: number;
|
|
11
|
+
readonly range: number;
|
|
12
|
+
}
|
|
13
|
+
const match = matcher(scope, slice);
|
|
14
|
+
return always<Parser<T, Input<Memory>>>([
|
|
15
|
+
(input, output) => {
|
|
16
|
+
const { source, position } = input;
|
|
17
|
+
if (position === source.length) return Result.skip;
|
|
18
|
+
const src = match(input, output) ?? '';
|
|
19
|
+
assert(source.startsWith(src, position));
|
|
20
|
+
if (src === '') {
|
|
21
|
+
input.position = position;
|
|
22
|
+
return Result.skip;
|
|
23
|
+
}
|
|
24
|
+
input.range = src.length;
|
|
25
|
+
if (slice) {
|
|
26
|
+
input.scope.focus(src);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
input.memory = {
|
|
30
|
+
position,
|
|
31
|
+
range: input.range,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return output.context;
|
|
35
|
+
},
|
|
36
|
+
parser,
|
|
37
|
+
(input, output) => {
|
|
38
|
+
if (slice) {
|
|
39
|
+
input.scope.unfocus(output.state, true);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
input.position += output.state && input.position === input.memory.position
|
|
43
|
+
? input.memory.range
|
|
44
|
+
: 0;
|
|
45
|
+
}
|
|
46
|
+
return output.context;
|
|
47
|
+
},
|
|
48
|
+
]);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function rewrite<P extends Parser>(scope: Parser<unknown, Parser.Input<P>>, parser: P, slice?: boolean): P;
|
|
52
|
+
export function rewrite<T>(scope: Parser, parser: Parser<T>, slice = true): Parser<T> {
|
|
53
|
+
assert(scope);
|
|
54
|
+
assert(parser);
|
|
55
|
+
interface Memory {
|
|
56
|
+
readonly position: number;
|
|
57
|
+
range: number;
|
|
58
|
+
readonly linebreak: number;
|
|
59
|
+
}
|
|
60
|
+
return always<Parser<T, Input<Memory>>>([
|
|
61
|
+
(input, output) => {
|
|
62
|
+
const { source, position, linebreak } = input;
|
|
63
|
+
if (position === source.length) return Result.skip;
|
|
64
|
+
input.memory = {
|
|
65
|
+
position,
|
|
66
|
+
range: 0,
|
|
67
|
+
linebreak,
|
|
68
|
+
};
|
|
69
|
+
return output.context;
|
|
70
|
+
},
|
|
71
|
+
clear(scope),
|
|
72
|
+
(input, output) => {
|
|
73
|
+
const { source, position, memory } = input;
|
|
74
|
+
const range = position - memory.position;
|
|
75
|
+
input.range = memory.range = range;
|
|
76
|
+
input.linebreak = memory.linebreak;
|
|
77
|
+
if (!output.state || range === 0) {
|
|
78
|
+
input.position = memory.position;
|
|
79
|
+
return Result.skip;
|
|
80
|
+
}
|
|
81
|
+
if (slice) {
|
|
82
|
+
input.scope.focus(source.slice(position - range, position));
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
input.position -= range;
|
|
86
|
+
}
|
|
87
|
+
return output.context;
|
|
88
|
+
},
|
|
89
|
+
parser,
|
|
90
|
+
(input, output) => {
|
|
91
|
+
if (slice) {
|
|
92
|
+
input.scope.unfocus(output.state, true);
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
input.position += output.state && input.position === input.memory.position
|
|
96
|
+
? input.memory.range
|
|
97
|
+
: 0;
|
|
98
|
+
}
|
|
99
|
+
return output.context;
|
|
100
|
+
},
|
|
101
|
+
]);
|
|
102
|
+
}
|