securemark 0.299.4 → 0.300.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 (233) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/design.md +0 -6
  3. package/dist/index.js +2847 -1993
  4. package/index.d.ts +2 -1
  5. package/markdown.d.ts +209 -183
  6. package/package.json +1 -1
  7. package/src/api/bind.test.ts +0 -22
  8. package/src/api/bind.ts +15 -10
  9. package/src/api/header.ts +8 -5
  10. package/src/api/parse.test.ts +118 -122
  11. package/src/api/parse.ts +13 -31
  12. package/src/api/run.ts +6 -0
  13. package/src/api.ts +1 -0
  14. package/src/combinator/control/inits.ts +35 -0
  15. package/src/combinator/control/sequence.test.ts +38 -0
  16. package/src/combinator/control/sequence.ts +19 -0
  17. package/src/combinator/control/some.test.ts +41 -0
  18. package/src/combinator/{data/parser → control}/some.ts +39 -26
  19. package/src/combinator/control/state.ts +33 -0
  20. package/src/combinator/control/subsequence.test.ts +47 -0
  21. package/src/combinator/control/subsequence.ts +16 -0
  22. package/src/combinator/control/tails.ts +8 -0
  23. package/src/combinator/control/union.test.ts +37 -0
  24. package/src/combinator/control/union.ts +31 -0
  25. package/src/combinator/{data/delimiter.ts → delimiter.ts} +40 -60
  26. package/src/combinator/effect/backtrack.ts +64 -0
  27. package/src/combinator/effect/clock.ts +10 -0
  28. package/src/combinator/effect/precedence.ts +50 -0
  29. package/src/combinator/effect/recursion.ts +30 -0
  30. package/src/combinator/effect/scope.ts +100 -0
  31. package/src/combinator/effect/state.ts +72 -0
  32. package/src/combinator/{data/list → parser}/list.ts +35 -10
  33. package/src/combinator/parser.ts +303 -0
  34. package/src/combinator/process/bind.ts +34 -0
  35. package/src/combinator/process/block.test.ts +20 -0
  36. package/src/combinator/process/block.ts +33 -0
  37. package/src/combinator/process/clear.ts +16 -0
  38. package/src/combinator/process/contract.ts +35 -0
  39. package/src/combinator/process/duplicate.ts +7 -0
  40. package/src/combinator/process/error.ts +13 -0
  41. package/src/combinator/{control/manipulation → process}/fallback.ts +3 -3
  42. package/src/combinator/process/fence.ts +59 -0
  43. package/src/combinator/process/fmap.ts +10 -0
  44. package/src/combinator/process/indent.test.ts +31 -0
  45. package/src/combinator/process/indent.ts +51 -0
  46. package/src/combinator/process/lazy.ts +8 -0
  47. package/src/combinator/process/line.test.ts +21 -0
  48. package/src/combinator/process/line.ts +55 -0
  49. package/src/combinator/process/match.ts +37 -0
  50. package/src/combinator/process/reverse.ts +7 -0
  51. package/src/combinator/process/scope.ts +102 -0
  52. package/src/combinator/process/surround.ts +273 -0
  53. package/src/combinator.ts +28 -24
  54. package/src/debug.test.ts +11 -8
  55. package/src/parser/autolink.test.ts +17 -18
  56. package/src/parser/block/blockquote.test.ts +78 -79
  57. package/src/parser/block/blockquote.ts +32 -25
  58. package/src/parser/block/codeblock.test.ts +54 -57
  59. package/src/parser/block/codeblock.ts +44 -26
  60. package/src/parser/block/dlist.test.ts +56 -57
  61. package/src/parser/block/dlist.ts +5 -5
  62. package/src/parser/block/extension/aside.test.ts +7 -9
  63. package/src/parser/block/extension/aside.ts +76 -47
  64. package/src/parser/block/extension/example.test.ts +16 -19
  65. package/src/parser/block/extension/example.ts +88 -48
  66. package/src/parser/block/extension/fig.test.ts +37 -36
  67. package/src/parser/block/extension/fig.ts +20 -25
  68. package/src/parser/block/extension/figbase.test.ts +18 -19
  69. package/src/parser/block/extension/figbase.ts +3 -3
  70. package/src/parser/block/extension/figure.test.ts +58 -63
  71. package/src/parser/block/extension/figure.ts +23 -21
  72. package/src/parser/block/extension/message.test.ts +12 -14
  73. package/src/parser/block/extension/message.ts +52 -39
  74. package/src/parser/block/extension/placeholder.test.ts +13 -13
  75. package/src/parser/block/extension/placeholder.ts +23 -21
  76. package/src/parser/block/extension/table.test.ts +69 -71
  77. package/src/parser/block/extension/table.ts +43 -31
  78. package/src/parser/block/extension.test.ts +24 -24
  79. package/src/parser/block/extension.ts +3 -3
  80. package/src/parser/block/heading.test.ts +58 -59
  81. package/src/parser/block/heading.ts +19 -18
  82. package/src/parser/block/ilist.test.ts +8 -8
  83. package/src/parser/block/ilist.ts +9 -7
  84. package/src/parser/block/mathblock.test.ts +29 -32
  85. package/src/parser/block/mathblock.ts +24 -23
  86. package/src/parser/block/mediablock.ts +7 -7
  87. package/src/parser/block/olist.test.ts +102 -103
  88. package/src/parser/block/olist.ts +11 -12
  89. package/src/parser/block/pagebreak.test.ts +15 -16
  90. package/src/parser/block/pagebreak.ts +5 -5
  91. package/src/parser/block/paragraph.test.ts +57 -58
  92. package/src/parser/block/paragraph.ts +1 -1
  93. package/src/parser/block/reply/cite.test.ts +39 -40
  94. package/src/parser/block/reply/cite.ts +5 -5
  95. package/src/parser/block/reply/quote.test.ts +50 -51
  96. package/src/parser/block/reply/quote.ts +8 -7
  97. package/src/parser/block/reply.test.ts +19 -20
  98. package/src/parser/block/reply.ts +2 -2
  99. package/src/parser/block/sidefence.test.ts +41 -48
  100. package/src/parser/block/sidefence.ts +17 -11
  101. package/src/parser/block/table.test.ts +48 -49
  102. package/src/parser/block/table.ts +10 -9
  103. package/src/parser/block/ulist.test.ts +52 -53
  104. package/src/parser/block/ulist.ts +9 -8
  105. package/src/parser/block.ts +63 -51
  106. package/src/parser/context.ts +40 -40
  107. package/src/parser/document.ts +48 -0
  108. package/src/parser/header.test.ts +19 -20
  109. package/src/parser/header.ts +31 -25
  110. package/src/parser/inline/annotation.test.ts +49 -50
  111. package/src/parser/inline/annotation.ts +14 -16
  112. package/src/parser/inline/autolink/account.test.ts +32 -33
  113. package/src/parser/inline/autolink/account.ts +18 -19
  114. package/src/parser/inline/autolink/anchor.test.ts +21 -22
  115. package/src/parser/inline/autolink/anchor.ts +7 -8
  116. package/src/parser/inline/autolink/channel.test.ts +14 -15
  117. package/src/parser/inline/autolink/email.test.ts +36 -37
  118. package/src/parser/inline/autolink/email.ts +6 -6
  119. package/src/parser/inline/autolink/hashnum.test.ts +32 -33
  120. package/src/parser/inline/autolink/hashnum.ts +7 -8
  121. package/src/parser/inline/autolink/hashtag.test.ts +59 -60
  122. package/src/parser/inline/autolink/hashtag.ts +8 -9
  123. package/src/parser/inline/autolink/url.test.ts +75 -76
  124. package/src/parser/inline/autolink/url.ts +17 -18
  125. package/src/parser/inline/autolink.ts +24 -11
  126. package/src/parser/inline/bracket.test.ts +73 -74
  127. package/src/parser/inline/bracket.ts +88 -63
  128. package/src/parser/inline/code.test.ts +30 -31
  129. package/src/parser/inline/code.ts +6 -6
  130. package/src/parser/inline/deletion.test.ts +27 -28
  131. package/src/parser/inline/deletion.ts +5 -5
  132. package/src/parser/inline/emphasis.test.ts +39 -40
  133. package/src/parser/inline/emphasis.ts +5 -5
  134. package/src/parser/inline/emstrong.test.ts +101 -102
  135. package/src/parser/inline/emstrong.ts +103 -85
  136. package/src/parser/inline/extension/index.test.ts +91 -92
  137. package/src/parser/inline/extension/index.ts +17 -13
  138. package/src/parser/inline/extension/indexee.ts +4 -4
  139. package/src/parser/inline/extension/indexer.test.ts +23 -24
  140. package/src/parser/inline/extension/indexer.ts +6 -5
  141. package/src/parser/inline/extension/label.test.ts +32 -33
  142. package/src/parser/inline/extension/label.ts +14 -5
  143. package/src/parser/inline/extension/placeholder.test.ts +42 -43
  144. package/src/parser/inline/extension/placeholder.ts +8 -9
  145. package/src/parser/inline/html.test.ts +109 -109
  146. package/src/parser/inline/html.ts +27 -27
  147. package/src/parser/inline/htmlentity.test.ts +37 -38
  148. package/src/parser/inline/htmlentity.ts +6 -7
  149. package/src/parser/inline/insertion.test.ts +27 -28
  150. package/src/parser/inline/insertion.ts +5 -5
  151. package/src/parser/inline/italic.test.ts +55 -56
  152. package/src/parser/inline/italic.ts +5 -5
  153. package/src/parser/inline/link.test.ts +186 -187
  154. package/src/parser/inline/link.ts +29 -30
  155. package/src/parser/inline/mark.test.ts +31 -32
  156. package/src/parser/inline/mark.ts +6 -6
  157. package/src/parser/inline/math.test.ts +140 -141
  158. package/src/parser/inline/math.ts +7 -8
  159. package/src/parser/inline/media.test.ts +92 -93
  160. package/src/parser/inline/media.ts +35 -41
  161. package/src/parser/inline/reference.test.ts +111 -112
  162. package/src/parser/inline/reference.ts +61 -32
  163. package/src/parser/inline/remark.test.ts +49 -50
  164. package/src/parser/inline/remark.ts +13 -13
  165. package/src/parser/inline/ruby.test.ts +49 -50
  166. package/src/parser/inline/ruby.ts +100 -52
  167. package/src/parser/inline/shortmedia.test.ts +9 -10
  168. package/src/parser/inline/shortmedia.ts +11 -9
  169. package/src/parser/inline/strong.test.ts +36 -37
  170. package/src/parser/inline/strong.ts +5 -5
  171. package/src/parser/inline/template.test.ts +22 -23
  172. package/src/parser/inline/template.ts +17 -20
  173. package/src/parser/inline.test.ts +225 -226
  174. package/src/parser/inline.ts +68 -34
  175. package/src/parser/parser.ts +51 -0
  176. package/src/parser/repeat.ts +118 -91
  177. package/src/parser/segment.test.ts +0 -11
  178. package/src/parser/segment.ts +25 -28
  179. package/src/parser/source/escapable.test.ts +23 -24
  180. package/src/parser/source/escapable.ts +19 -20
  181. package/src/parser/source/line.test.ts +17 -18
  182. package/src/parser/source/line.ts +19 -24
  183. package/src/parser/source/str.ts +17 -10
  184. package/src/parser/source/text.test.ts +88 -89
  185. package/src/parser/source/text.ts +32 -62
  186. package/src/parser/source/unescapable.test.ts +23 -24
  187. package/src/parser/source/unescapable.ts +15 -16
  188. package/src/parser/source/whitespace.ts +36 -0
  189. package/src/parser/source.ts +1 -0
  190. package/src/parser/util.ts +1 -1
  191. package/src/parser/visibility.ts +37 -15
  192. package/src/processor/figure.test.ts +20 -20
  193. package/src/processor/figure.ts +18 -10
  194. package/src/processor/note.test.ts +13 -13
  195. package/src/processor/note.ts +4 -2
  196. package/src/renderer/render/media/pdf.ts +2 -2
  197. package/src/renderer/render/media/twitter.ts +2 -2
  198. package/src/renderer/render/media.test.ts +12 -12
  199. package/src/renderer/render.test.ts +11 -11
  200. package/src/util/info.test.ts +2 -2
  201. package/src/util/quote.test.ts +3 -3
  202. package/src/util/quote.ts +6 -5
  203. package/src/util/toc.test.ts +12 -12
  204. package/src/combinator/control/constraint/block.test.ts +0 -20
  205. package/src/combinator/control/constraint/block.ts +0 -28
  206. package/src/combinator/control/constraint/contract.ts +0 -27
  207. package/src/combinator/control/constraint/line.test.ts +0 -21
  208. package/src/combinator/control/constraint/line.ts +0 -42
  209. package/src/combinator/control/manipulation/clear.ts +0 -5
  210. package/src/combinator/control/manipulation/convert.ts +0 -22
  211. package/src/combinator/control/manipulation/duplicate.ts +0 -7
  212. package/src/combinator/control/manipulation/fence.ts +0 -54
  213. package/src/combinator/control/manipulation/indent.test.ts +0 -31
  214. package/src/combinator/control/manipulation/indent.ts +0 -39
  215. package/src/combinator/control/manipulation/lazy.ts +0 -8
  216. package/src/combinator/control/manipulation/match.ts +0 -27
  217. package/src/combinator/control/manipulation/recovery.ts +0 -18
  218. package/src/combinator/control/manipulation/reverse.ts +0 -8
  219. package/src/combinator/control/manipulation/scope.ts +0 -61
  220. package/src/combinator/control/manipulation/surround.ts +0 -223
  221. package/src/combinator/control/monad/bind.ts +0 -26
  222. package/src/combinator/control/monad/fmap.ts +0 -10
  223. package/src/combinator/data/parser/context.ts +0 -96
  224. package/src/combinator/data/parser/inits.ts +0 -20
  225. package/src/combinator/data/parser/sequence.test.ts +0 -33
  226. package/src/combinator/data/parser/sequence.ts +0 -20
  227. package/src/combinator/data/parser/some.test.ts +0 -37
  228. package/src/combinator/data/parser/subsequence.test.ts +0 -41
  229. package/src/combinator/data/parser/subsequence.ts +0 -13
  230. package/src/combinator/data/parser/tails.ts +0 -8
  231. package/src/combinator/data/parser/union.test.ts +0 -33
  232. package/src/combinator/data/parser/union.ts +0 -18
  233. package/src/combinator/data/parser.ts +0 -144
@@ -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
+ }
@@ -0,0 +1,273 @@
1
+ import { Parser, Result, Input, Output, List, Node } from '../parser';
2
+ import { tester } from '../delimiter';
3
+
4
+ export function surround<P extends Parser, S = string>(
5
+ opener: string | RegExp | Parser<S, Parser.Input<P>>,
6
+ parser: Parser.IntermediateParser<P>,
7
+ closer: string | RegExp | Parser<S, Parser.Input<P>>,
8
+ optional?: false,
9
+ backtracks?: readonly number[],
10
+ f?: (rss: [List<Node<S>>, List<Node<Parser.SubNode<P>>>, List<Node<S>>], input: Parser.Input<P>, output: Output<Parser.Node<P>>) => Result<Parser.Node<P>, Parser.Input<P>, Parser.SubParsers<P>>,
11
+ g?: (rss: [List<Node<S>>, List<Node<Parser.SubNode<P>>> | undefined], input: Parser.Input<P>, output: Output<Parser.Node<P>>) => Result<Parser.Node<P>, Parser.Input<P>, Parser.SubParsers<P>>,
12
+ ): P;
13
+ export function surround<P extends Parser, S = string>(
14
+ opener: string | RegExp | Parser<S, Parser.Input<P>>,
15
+ parser: Parser.IntermediateParser<P>,
16
+ closer: string | RegExp | Parser<S, Parser.Input<P>>,
17
+ optional?: boolean,
18
+ backtracks?: readonly number[],
19
+ f?: (rss: [List<Node<S>>, List<Node<Parser.SubNode<P>>> | undefined, List<Node<S>>], input: Parser.Input<P>, output: Output<Parser.Node<P>>) => Result<Parser.Node<P>, Parser.Input<P>, Parser.SubParsers<P>>,
20
+ g?: (rss: [List<Node<S>>, List<Node<Parser.SubNode<P>>> | undefined], input: Parser.Input<P>, output: Output<Parser.Node<P>>) => Result<Parser.Node<P>, Parser.Input<P>, Parser.SubParsers<P>>,
21
+ ): P;
22
+ export function surround<P extends Parser, S = string>(
23
+ opener: string | RegExp | Parser<S, Parser.Input<P>>,
24
+ parser: P,
25
+ closer: string | RegExp | Parser<S, Parser.Input<P>>,
26
+ optional?: false,
27
+ backtracks?: readonly number[],
28
+ f?: (rss: [List<Node<S>>, List<Node<Parser.Node<P>>>, List<Node<S>>], input: Parser.Input<P>, output: Output<Parser.Node<P>>) => Result<Parser.Node<P>, Parser.Input<P>, Parser.SubParsers<P>>,
29
+ g?: (rss: [List<Node<S>>, List<Node<Parser.Node<P>>> | undefined], input: Parser.Input<P>, output: Output<Parser.Node<P>>) => Result<Parser.Node<P>, Parser.Input<P>, Parser.SubParsers<P>>,
30
+ ): P;
31
+ export function surround<P extends Parser, S = string>(
32
+ opener: string | RegExp | Parser<S, Parser.Input<P>>,
33
+ parser: P,
34
+ closer: string | RegExp | Parser<S, Parser.Input<P>>,
35
+ optional?: boolean,
36
+ backtracks?: readonly number[],
37
+ f?: (rss: [List<Node<S>>, List<Node<Parser.Node<P>>> | undefined, List<Node<S>>], input: Parser.Input<P>, output: Output<Parser.Node<P>>) => Result<Parser.Node<P>, Parser.Input<P>, Parser.SubParsers<P>>,
38
+ g?: (rss: [List<Node<S>>, List<Node<Parser.Node<P>>> | undefined], input: Parser.Input<P>, output: Output<Parser.Node<P>>) => Result<Parser.Node<P>, Parser.Input<P>, Parser.SubParsers<P>>,
39
+ ): P;
40
+ export function surround<P extends Parser<string>, S = string>(
41
+ opener: string | RegExp | Parser<S, Parser.Input<P>>,
42
+ parser: string | RegExp | P,
43
+ closer: string | RegExp | Parser<S, Parser.Input<P>>,
44
+ optional?: false,
45
+ backtracks?: readonly number[],
46
+ f?: (rss: [List<Node<S>>, List<Node<Parser.Node<P>>>, List<Node<S>>], input: Parser.Input<P>, output: Output<Parser.Node<P>>) => Result<Parser.Node<P>, Parser.Input<P>, Parser.SubParsers<P>>,
47
+ g?: (rss: [List<Node<S>>, List<Node<Parser.Node<P>>> | undefined], input: Parser.Input<P>, output: Output<Parser.Node<P>>) => Result<Parser.Node<P>, Parser.Input<P>, Parser.SubParsers<P>>,
48
+ ): P;
49
+ export function surround<P extends Parser<string>, S = string>(
50
+ opener: string | RegExp | Parser<S, Parser.Input<P>>,
51
+ parser: string | RegExp | P,
52
+ closer: string | RegExp | Parser<S, Parser.Input<P>>,
53
+ optional?: boolean,
54
+ backtracks?: readonly number[],
55
+ f?: (rss: [List<Node<S>>, List<Node<Parser.Node<P>>> | undefined, List<Node<S>>], input: Parser.Input<P>, output: Output<Parser.Node<P>>) => Result<Parser.Node<P>, Parser.Input<P>, Parser.SubParsers<P>>,
56
+ g?: (rss: [List<Node<S>>, List<Node<Parser.Node<P>>> | undefined], input: Parser.Input<P>, output: Output<Parser.Node<P>>) => Result<Parser.Node<P>, Parser.Input<P>, Parser.SubParsers<P>>,
57
+ ): P;
58
+ export function surround<T>(
59
+ opener: string | RegExp | Parser<T>,
60
+ parser: string | RegExp | Parser<T>,
61
+ closer: string | RegExp | Parser<T>,
62
+ optional: boolean = false,
63
+ backtracks: readonly number[] = [],
64
+ f?: (rss: [List<Node<T>>, List<Node<T>>, List<Node<T>>], input: Input, output: Output<T>) => Result<T>,
65
+ g?: (rss: [List<Node<T>>, List<Node<T>> | undefined], input: Input, output: Output<T>) => Result<T>,
66
+ ): Parser<T> {
67
+ switch (typeof opener) {
68
+ case 'string':
69
+ case 'object':
70
+ opener = wrap(opener);
71
+ }
72
+ assert(opener);
73
+ switch (typeof parser) {
74
+ case 'string':
75
+ case 'object':
76
+ parser = wrap(parser);
77
+ }
78
+ assert(parser);
79
+ switch (typeof closer) {
80
+ case 'string':
81
+ case 'object':
82
+ closer = wrap(closer);
83
+ }
84
+ assert(closer);
85
+ const [blen, rbs, wbs] = reduce(backtracks);
86
+ interface Memory {
87
+ readonly position: number;
88
+ readonly linebreak: number;
89
+ state: boolean;
90
+ }
91
+ const cont1: Result<T, Input<Memory>> = [
92
+ (input, output) => {
93
+ const { source, position, linebreak } = input;
94
+ if (position === source.length) return Result.skip;
95
+ input.linebreak = 0;
96
+ input.memory = {
97
+ position,
98
+ linebreak,
99
+ state: false,
100
+ };
101
+ output.push();
102
+ return cont2;
103
+ },
104
+ (input, output) => {
105
+ const { memory: { linebreak } } = input;
106
+ if (output.state) {
107
+ input.linebreak ||= linebreak;
108
+ }
109
+ else {
110
+ input.linebreak = linebreak;
111
+ }
112
+ return output.context;
113
+ },
114
+ ];
115
+ const cont2: Result<T, Input<Memory>> = [
116
+ opener,
117
+ (input, output) => {
118
+ const { memory: { position } } = input;
119
+ if (!output.state || rbs && isBacktrack(input, rbs, position, blen)) {
120
+ output.pop();
121
+ return;
122
+ }
123
+ output.push();
124
+ return cont3;
125
+ },
126
+ ];
127
+ const cont3: Result<T, Input<Memory>> = [
128
+ parser,
129
+ (input, output) => {
130
+ const { state } = output;
131
+ const { memory: { position } } = input;
132
+ input.range = input.position - position;
133
+ if (!output.state && !optional) {
134
+ wbs && setBacktrack(input, wbs, position);
135
+ const m = output.pop();
136
+ const o = output.pop();
137
+ if (!g) return;
138
+ output.state = true;
139
+ // @ts-expect-error
140
+ output.context = Result.succ;
141
+ return g([o, state ? m : undefined], input, output);
142
+ }
143
+ input.memory.state = output.state;
144
+ output.push();
145
+ return cont4;
146
+ },
147
+ ];
148
+ const cont4: Result<T, Input<Memory>> = [
149
+ closer,
150
+ (input, output) => {
151
+ const { memory: { position, state } } = input;
152
+ const c = output.pop();
153
+ const m = output.pop();
154
+ const o = output.pop();
155
+ input.range = input.position - position;
156
+ if (!output.state) {
157
+ wbs && setBacktrack(input, wbs, position);
158
+ if (!g) return;
159
+ output.state = true;
160
+ // @ts-expect-error
161
+ output.context = Result.succ;
162
+ return g([o, state ? m : undefined], input, output);
163
+ }
164
+ if (input.range === 0) return;
165
+ if (f) {
166
+ return f([o, state ? m : undefined as any, c], input, output);
167
+ }
168
+ else {
169
+ output.import(o.import(m).import(c));
170
+ return output.context;
171
+ }
172
+ },
173
+ ];
174
+ return () => cont1;
175
+ }
176
+ export function open<P extends Parser>(
177
+ opener: string | RegExp | Parser<Parser.Node<P>, Parser.Input<P>>,
178
+ parser: P,
179
+ optional?: boolean,
180
+ backtracks?: readonly number[],
181
+ ): P;
182
+ export function open<P extends Parser<string>>(
183
+ opener: string | RegExp | Parser<Parser.Node<P>, Parser.Input<P>>,
184
+ parser: string | RegExp | P,
185
+ optional?: boolean,
186
+ backtracks?: readonly number[],
187
+ ): P;
188
+ export function open<N>(
189
+ opener: string | RegExp | Parser<N, Input>,
190
+ parser: string | RegExp | Parser<N>,
191
+ optional?: boolean,
192
+ backtracks: readonly number[] = [],
193
+ ): Parser<N> {
194
+ return surround(opener, parser as Parser<N>, '', optional, backtracks);
195
+ }
196
+ export function close<P extends Parser>(
197
+ parser: P,
198
+ closer: string | RegExp | Parser<Parser.Node<P>, Parser.Input<P>>,
199
+ optional?: boolean,
200
+ backtracks?: readonly number[],
201
+ ): P;
202
+ export function close<P extends Parser<string>>(
203
+ parser: string | RegExp | P,
204
+ closer: string | RegExp | Parser<Parser.Node<P>, Parser.Input<P>>,
205
+ optional?: boolean,
206
+ backtracks?: readonly number[],
207
+ ): P;
208
+ export function close<N>(
209
+ parser: string | RegExp | Parser<N>,
210
+ closer: string | RegExp | Parser<N, Input>,
211
+ optional?: boolean,
212
+ backtracks: readonly number[] = [],
213
+ ): Parser<N> {
214
+ return surround('', parser as Parser<N>, closer, optional, backtracks);
215
+ }
216
+
217
+ const commandsize = 2;
218
+ export function isBacktrack(
219
+ input: Input,
220
+ backtrack: number,
221
+ position: number = input.position,
222
+ length: number = 1,
223
+ ): boolean {
224
+ assert(1 & backtrack);
225
+ assert(backtrack >>> commandsize);
226
+ assert(0 < length && length < 3);
227
+ const { backtracks, offset } = input;
228
+ for (let i = 0; i < length; ++i) {
229
+ if (backtracks[position + i + offset] & backtrack >>> commandsize) return true;
230
+ }
231
+ return false;
232
+ }
233
+ export function setBacktrack(
234
+ input: Input,
235
+ backtrack: number,
236
+ position: number,
237
+ length: number = 1,
238
+ ): void {
239
+ // バックトラックの可能性がなく記録不要の場合もあるが判別が面倒なので省略
240
+ assert(2 & backtrack);
241
+ assert(backtrack >>> commandsize);
242
+ assert(0 < length && length < 3);
243
+ const { backtracks, offset } = input;
244
+ for (let i = 0; i < length; ++i) {
245
+ backtracks[position + i + offset] |= backtrack >>> commandsize;
246
+ }
247
+ }
248
+
249
+ function wrap<T>(pattern: string | RegExp): Parser<T> {
250
+ const test = tester(pattern, true);
251
+ return (input, output) => test(input, output) ? output.context : Result.fail;
252
+ }
253
+
254
+ function reduce(backtracks: readonly number[]): readonly [number, number, number] {
255
+ let len = 1;
256
+ let rbs = 0;
257
+ let wbs = 0;
258
+ for (const backtrack of backtracks) {
259
+ if (backtrack >>> commandsize === 0) {
260
+ len = backtrack;
261
+ assert(len > 0);
262
+ continue;
263
+ }
264
+ assert(backtrack >>> commandsize);
265
+ if (1 & backtrack) {
266
+ rbs |= backtrack;
267
+ }
268
+ if (2 & backtrack) {
269
+ wbs |= backtrack;
270
+ }
271
+ }
272
+ return [len, rbs, wbs];
273
+ }
package/src/combinator.ts CHANGED
@@ -1,24 +1,28 @@
1
- export * from './combinator/data/parser/union';
2
- export * from './combinator/data/parser/inits';
3
- export * from './combinator/data/parser/tails';
4
- export * from './combinator/data/parser/sequence';
5
- export * from './combinator/data/parser/subsequence';
6
- export * from './combinator/data/parser/some';
7
- export * from './combinator/data/parser/context';
8
- export * from './combinator/control/constraint/block';
9
- export * from './combinator/control/constraint/line';
10
- export * from './combinator/control/constraint/contract';
11
- export * from './combinator/control/manipulation/fence';
12
- export * from './combinator/control/manipulation/indent';
13
- export * from './combinator/control/manipulation/scope';
14
- export * from './combinator/control/manipulation/clear';
15
- export * from './combinator/control/manipulation/surround';
16
- export * from './combinator/control/manipulation/match';
17
- export * from './combinator/control/manipulation/convert';
18
- export * from './combinator/control/manipulation/duplicate';
19
- export * from './combinator/control/manipulation/reverse';
20
- export * from './combinator/control/manipulation/fallback';
21
- export * from './combinator/control/manipulation/recovery';
22
- export * from './combinator/control/manipulation/lazy';
23
- export * from './combinator/control/monad/fmap';
24
- export * from './combinator/control/monad/bind';
1
+ export * from './combinator/control/union';
2
+ export * from './combinator/control/inits';
3
+ export * from './combinator/control/tails';
4
+ export * from './combinator/control/sequence';
5
+ export * from './combinator/control/subsequence';
6
+ export * from './combinator/control/some';
7
+ export * from './combinator/control/state';
8
+ export * from './combinator/effect/state';
9
+ export * from './combinator/effect/clock';
10
+ export * from './combinator/effect/recursion';
11
+ export * from './combinator/effect/precedence';
12
+ export * from './combinator/effect/scope';
13
+ export * from './combinator/effect/backtrack';
14
+ export * from './combinator/process/block';
15
+ export * from './combinator/process/line';
16
+ export * from './combinator/process/contract';
17
+ export * from './combinator/process/fence';
18
+ export * from './combinator/process/indent';
19
+ export * from './combinator/process/scope';
20
+ export * from './combinator/process/clear';
21
+ export * from './combinator/process/surround';
22
+ export * from './combinator/process/match';
23
+ export * from './combinator/process/duplicate';
24
+ export * from './combinator/process/reverse';
25
+ export * from './combinator/process/fallback';
26
+ export * from './combinator/process/lazy';
27
+ export * from './combinator/process/fmap';
28
+ export * from './combinator/process/bind';
package/src/debug.test.ts CHANGED
@@ -1,12 +1,15 @@
1
- import { Parser, Input } from './combinator/data/parser';
1
+ import { Parser, Input, Output, run } from './combinator/parser';
2
2
  import { html, define } from 'typed-dom/dom';
3
3
  import { querySelectorWith, querySelectorAllWith } from 'typed-dom/query';
4
4
 
5
- export function inspect(parser: Parser<DocumentFragment | HTMLElement | string>, input: Input, until: number | string = Infinity): [string[], string] | undefined {
6
- const context = input;
7
- const result = parser(input);
8
- return result && [
9
- result.foldl<string[]>((acc, { value: node }) => {
5
+ export function inspect(parser: Parser<DocumentFragment | HTMLElement | string>, input: Input, until: number | string = Infinity): [...string[][], string] | undefined {
6
+ const output = new Output<DocumentFragment | HTMLElement | string>();
7
+ for (const _ of run(parser, input, output));
8
+ assert.deepStrictEqual(output.data, [output.data[0]]);
9
+ assert(output.state || output.peek().length === 0);
10
+ assert(!output.error);
11
+ return !output.state ? undefined : [
12
+ ...output.data.map(nodes => nodes.foldl<string[]>((acc, { value: node }) => {
10
13
  assert(node);
11
14
  if (typeof node === 'string') return acc.push(node), acc;
12
15
  if (node instanceof DocumentFragment) return acc.push(html('div', [node]).innerHTML), acc;
@@ -35,8 +38,8 @@ export function inspect(parser: Parser<DocumentFragment | HTMLElement | string>,
35
38
  assert(el.innerHTML.startsWith(node.outerHTML.slice(0, until)));
36
39
  }
37
40
  return acc.push(normalize(node.outerHTML.slice(0, until))), acc;
38
- }, []),
39
- context.source.slice(context.position),
41
+ }, [])),
42
+ input.source.slice(input.position),
40
43
  ];
41
44
  }
42
45
 
@@ -1,6 +1,5 @@
1
1
  import { autolink } from './autolink';
2
- import { input } from '../combinator/data/parser';
3
- import { Context } from './context';
2
+ import { input } from './context';
4
3
  import { inspect } from '../debug.test';
5
4
 
6
5
  describe('Unit: parser/autolink', () => {
@@ -8,22 +7,22 @@ describe('Unit: parser/autolink', () => {
8
7
  const parser = autolink;
9
8
 
10
9
  it('basic', () => {
11
- assert.deepStrictEqual(inspect(parser, input('http://host)', new Context())), [['<a class="url" href="http://host)" target="_blank">http://host)</a>'], '']);
12
- assert.deepStrictEqual(inspect(parser, input('http://host\\', new Context())), [['<a class="url" href="http://host\\" target="_blank">http://host\\</a>'], '']);
13
- assert.deepStrictEqual(inspect(parser, input('!http://host)', new Context())), [['!', '<a class="url" href="http://host)" target="_blank">http://host)</a>'], '']);
14
- assert.deepStrictEqual(inspect(parser, input('!http://host\\', new Context())), [['!', '<a class="url" href="http://host\\" target="_blank">http://host\\</a>'], '']);
15
- assert.deepStrictEqual(inspect(parser, input('#a', new Context())), [['<a class="hashtag" href="/hashtags/a">#a</a>'], '']);
16
- assert.deepStrictEqual(inspect(parser, input('@a#b', new Context())), [['<a class="channel" href="/@a?ch=b">@a#b</a>'], '']);
17
- assert.deepStrictEqual(inspect(parser, input('\\\n', new Context())), [['\\', '<br>'], '']);
18
- assert.deepStrictEqual(inspect(parser, input('a#b', new Context())), [['a', '#b'], '']);
19
- assert.deepStrictEqual(inspect(parser, input('0a#b', new Context())), [['0a', '#b'], '']);
20
- assert.deepStrictEqual(inspect(parser, input('あ#b', new Context())), [['あ', '#b'], '']);
21
- assert.deepStrictEqual(inspect(parser, input('あい#b', new Context())), [['あい', '#b'], '']);
22
- assert.deepStrictEqual(inspect(parser, input('0aあ#b', new Context())), [['0a', 'あ', '#b'], '']);
23
- assert.deepStrictEqual(inspect(parser, input('0aあい#b', new Context())), [['0a', 'あい', '#b'], '']);
24
- assert.deepStrictEqual(inspect(parser, input('a\n#b', new Context())), [['a', '<br>', '<a class="hashtag" href="/hashtags/b">#b</a>'], '']);
25
- assert.deepStrictEqual(inspect(parser, input('a\\\n#b', new Context())), [['a', '\\', '<br>', '<a class="hashtag" href="/hashtags/b">#b</a>'], '']);
26
- assert.deepStrictEqual(inspect(parser, input('0a>>b', new Context())), [['0a', '>', '>b'], '']);
10
+ assert.deepStrictEqual(inspect(parser, input('http://host)')), [['<a class="url" href="http://host)" target="_blank">http://host)</a>'], '']);
11
+ assert.deepStrictEqual(inspect(parser, input('http://host\\')), [['<a class="url" href="http://host\\" target="_blank">http://host\\</a>'], '']);
12
+ assert.deepStrictEqual(inspect(parser, input('!http://host)')), [['!', '<a class="url" href="http://host)" target="_blank">http://host)</a>'], '']);
13
+ assert.deepStrictEqual(inspect(parser, input('!http://host\\')), [['!', '<a class="url" href="http://host\\" target="_blank">http://host\\</a>'], '']);
14
+ assert.deepStrictEqual(inspect(parser, input('#a')), [['<a class="hashtag" href="/hashtags/a">#a</a>'], '']);
15
+ assert.deepStrictEqual(inspect(parser, input('@a#b')), [['<a class="channel" href="/@a?ch=b">@a#b</a>'], '']);
16
+ assert.deepStrictEqual(inspect(parser, input('\\\n')), [['\\', '<br>'], '']);
17
+ assert.deepStrictEqual(inspect(parser, input('a#b')), [['a', '#b'], '']);
18
+ assert.deepStrictEqual(inspect(parser, input('0a#b')), [['0a', '#b'], '']);
19
+ assert.deepStrictEqual(inspect(parser, input('あ#b')), [['あ', '#b'], '']);
20
+ assert.deepStrictEqual(inspect(parser, input('あい#b')), [['あい', '#b'], '']);
21
+ assert.deepStrictEqual(inspect(parser, input('0aあ#b')), [['0a', 'あ', '#b'], '']);
22
+ assert.deepStrictEqual(inspect(parser, input('0aあい#b')), [['0a', 'あい', '#b'], '']);
23
+ assert.deepStrictEqual(inspect(parser, input('a\n#b')), [['a', '<br>', '<a class="hashtag" href="/hashtags/b">#b</a>'], '']);
24
+ assert.deepStrictEqual(inspect(parser, input('a\\\n#b')), [['a', '\\', '<br>', '<a class="hashtag" href="/hashtags/b">#b</a>'], '']);
25
+ assert.deepStrictEqual(inspect(parser, input('0a>>b')), [['0a', '>', '>b'], '']);
27
26
  });
28
27
 
29
28
  });