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.
Files changed (231) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/design.md +0 -6
  3. package/dist/index.js +2749 -1979
  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 +26 -0
  15. package/src/combinator/control/sequence.test.ts +38 -0
  16. package/src/combinator/control/sequence.ts +17 -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 +42 -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 +18 -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 +293 -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 +271 -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 +56 -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 +8 -9
  63. package/src/parser/block/extension/aside.ts +76 -47
  64. package/src/parser/block/extension/example.test.ts +18 -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 +62 -63
  71. package/src/parser/block/extension/figure.ts +23 -21
  72. package/src/parser/block/extension/message.test.ts +13 -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 +70 -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 +31 -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 +35 -32
  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 +13 -14
  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 +6 -7
  159. package/src/parser/inline/media.test.ts +92 -93
  160. package/src/parser/inline/media.ts +31 -37
  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 +60 -49
  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 +13 -16
  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 +20 -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 +19 -20
  186. package/src/parser/source/unescapable.test.ts +23 -24
  187. package/src/parser/source/unescapable.ts +16 -16
  188. package/src/parser/util.ts +1 -1
  189. package/src/parser/visibility.ts +35 -14
  190. package/src/processor/figure.test.ts +20 -20
  191. package/src/processor/figure.ts +18 -10
  192. package/src/processor/note.test.ts +13 -13
  193. package/src/processor/note.ts +4 -2
  194. package/src/renderer/render/media/pdf.ts +2 -2
  195. package/src/renderer/render/media/twitter.ts +2 -2
  196. package/src/renderer/render/media.test.ts +12 -12
  197. package/src/renderer/render.test.ts +11 -11
  198. package/src/util/info.test.ts +2 -2
  199. package/src/util/quote.test.ts +3 -3
  200. package/src/util/quote.ts +6 -5
  201. package/src/util/toc.test.ts +12 -12
  202. package/src/combinator/control/constraint/block.test.ts +0 -20
  203. package/src/combinator/control/constraint/block.ts +0 -28
  204. package/src/combinator/control/constraint/contract.ts +0 -27
  205. package/src/combinator/control/constraint/line.test.ts +0 -21
  206. package/src/combinator/control/constraint/line.ts +0 -42
  207. package/src/combinator/control/manipulation/clear.ts +0 -5
  208. package/src/combinator/control/manipulation/convert.ts +0 -22
  209. package/src/combinator/control/manipulation/duplicate.ts +0 -7
  210. package/src/combinator/control/manipulation/fence.ts +0 -54
  211. package/src/combinator/control/manipulation/indent.test.ts +0 -31
  212. package/src/combinator/control/manipulation/indent.ts +0 -39
  213. package/src/combinator/control/manipulation/lazy.ts +0 -8
  214. package/src/combinator/control/manipulation/match.ts +0 -27
  215. package/src/combinator/control/manipulation/recovery.ts +0 -18
  216. package/src/combinator/control/manipulation/reverse.ts +0 -8
  217. package/src/combinator/control/manipulation/scope.ts +0 -61
  218. package/src/combinator/control/manipulation/surround.ts +0 -223
  219. package/src/combinator/control/monad/bind.ts +0 -26
  220. package/src/combinator/control/monad/fmap.ts +0 -10
  221. package/src/combinator/data/parser/context.ts +0 -96
  222. package/src/combinator/data/parser/inits.ts +0 -20
  223. package/src/combinator/data/parser/sequence.test.ts +0 -33
  224. package/src/combinator/data/parser/sequence.ts +0 -20
  225. package/src/combinator/data/parser/some.test.ts +0 -37
  226. package/src/combinator/data/parser/subsequence.test.ts +0 -41
  227. package/src/combinator/data/parser/subsequence.ts +0 -13
  228. package/src/combinator/data/parser/tails.ts +0 -8
  229. package/src/combinator/data/parser/union.test.ts +0 -33
  230. package/src/combinator/data/parser/union.ts +0 -18
  231. package/src/combinator/data/parser.ts +0 -144
@@ -1,11 +1,11 @@
1
1
  import { quote } from './quote';
2
- import { parse } from '../api';
2
+ import { run, parse } from '../api';
3
3
 
4
4
  describe('Unit: util/quote', () => {
5
5
  describe('quote', () => {
6
6
  it('basic', () => {
7
7
  const range = document.createRange();
8
- const el = parse('>>1\n> a\n>>?\n> 2\n>>4 `b` ${c}$\n [e](f) ').firstElementChild!;
8
+ const el = run(parse('>>1\n> a\n>>?\n> 2\n>>4 `b` ${c}$\n [e](f) ')).firstElementChild!;
9
9
  range.setStart(el.firstChild!.firstChild!, 0);
10
10
  range.setEnd(el.lastChild!.lastChild!.lastChild!, 1);
11
11
  assert.deepStrictEqual(quote('3', range), `>>>1\n>> a\n>>>?\n>> 2\n>>3\n> >>4 \`b\` \${c}$\n> e`);
@@ -13,7 +13,7 @@ describe('Unit: util/quote', () => {
13
13
 
14
14
  it('adjustment', () => {
15
15
  const range = document.createRange();
16
- const el = parse('>>>1\n>> a').firstElementChild!;
16
+ const el = run(parse('>>>1\n>> a')).firstElementChild!;
17
17
  range.setEnd(el.lastChild!.lastChild!, 4);
18
18
  range.setStart(el.children[0].firstChild!, 0);
19
19
  assert(quote('2', range) === '>>>>1\n>>> a\n>>2');
package/src/util/quote.ts CHANGED
@@ -1,11 +1,12 @@
1
- import { input } from '../combinator/data/parser';
2
- import { Context } from '../parser/context';
1
+ import { Input } from '../parser/context';
2
+ import { Output, run } from '../combinator/parser';
3
3
  import { cite } from '../parser/block/reply/cite';
4
4
 
5
5
  export function quote(anchor: string, range: Range): string {
6
- const context = input('', new Context());
7
- cite(input(`>>${anchor}`, context));
8
- if (context.position !== context.source.length) throw new Error(`Invalid anchor: ${anchor}`);
6
+ const input = new Input({ source: `>>${anchor}` });
7
+ const output = new Output<HTMLElement>();
8
+ for (const _ of run(cite, input, output));
9
+ if (input.position !== input.source.length) throw new Error(`Invalid anchor: ${anchor}`);
9
10
  fit(range);
10
11
  const node = trim(range.cloneContents());
11
12
  if (!node.firstChild) return '';
@@ -1,18 +1,18 @@
1
1
  import { toc } from './toc';
2
- import { parse } from '../api';
2
+ import { run, parse } from '../api';
3
3
  import { html } from 'typed-dom/dom';
4
4
 
5
5
  describe('Unit: util/toc', () => {
6
6
  describe('toc', () => {
7
7
  it('empty', () => {
8
8
  assert.strictEqual(
9
- toc(parse('')).outerHTML,
9
+ toc(run(parse(''))).outerHTML,
10
10
  html('ul').outerHTML);
11
11
  });
12
12
 
13
13
  it('1', () => {
14
14
  assert.strictEqual(
15
- toc(parse('# 1')).outerHTML,
15
+ toc(run(parse('# 1'))).outerHTML,
16
16
  html('ul', [
17
17
  html('li', [html('a', { href: '#index::1', 'data-index': '1' }, '1')]),
18
18
  ]).outerHTML);
@@ -20,7 +20,7 @@ describe('Unit: util/toc', () => {
20
20
 
21
21
  it('1a', () => {
22
22
  assert.strictEqual(
23
- toc(parse('# 1\n\na')).outerHTML,
23
+ toc(run(parse('# 1\n\na'))).outerHTML,
24
24
  html('ul', [
25
25
  html('li', [html('a', { href: '#index::1', 'data-index': '1' }, '1')]),
26
26
  ]).outerHTML);
@@ -28,7 +28,7 @@ describe('Unit: util/toc', () => {
28
28
 
29
29
  it('11', () => {
30
30
  assert.strictEqual(
31
- toc(parse('# 1\n\n# 2')).outerHTML,
31
+ toc(run(parse('# 1\n\n# 2'))).outerHTML,
32
32
  html('ul', [
33
33
  html('li', [html('a', { href: '#index::1', 'data-index': '1' }, '1')]),
34
34
  html('li', [html('a', { href: '#index::2', 'data-index': '2' }, '2')]),
@@ -37,7 +37,7 @@ describe('Unit: util/toc', () => {
37
37
 
38
38
  it('12', () => {
39
39
  assert.strictEqual(
40
- toc(parse('# 1\n\n## 2')).outerHTML,
40
+ toc(run(parse('# 1\n\n## 2'))).outerHTML,
41
41
  html('ul', [
42
42
  html('li', [html('a', { href: '#index::1', 'data-index': '1' }, '1'),
43
43
  html('ul', [
@@ -49,7 +49,7 @@ describe('Unit: util/toc', () => {
49
49
 
50
50
  it('121', () => {
51
51
  assert.strictEqual(
52
- toc(parse('# 1\n\n## 2\n\n# 3')).outerHTML,
52
+ toc(run(parse('# 1\n\n## 2\n\n# 3'))).outerHTML,
53
53
  html('ul', [
54
54
  html('li', [html('a', { href: '#index::1', 'data-index': '1' }, '1'),
55
55
  html('ul', [
@@ -62,7 +62,7 @@ describe('Unit: util/toc', () => {
62
62
 
63
63
  it('1232', () => {
64
64
  assert.strictEqual(
65
- toc(parse('# 1\n\n## 2\n\n### 3\n\n## 4')).outerHTML,
65
+ toc(run(parse('# 1\n\n## 2\n\n### 3\n\n## 4'))).outerHTML,
66
66
  html('ul', [
67
67
  html('li', [html('a', { href: '#index::1', 'data-index': '1' }, '1'),
68
68
  html('ul', [
@@ -79,7 +79,7 @@ describe('Unit: util/toc', () => {
79
79
 
80
80
  it('1231', () => {
81
81
  assert.strictEqual(
82
- toc(parse('# 1\n\n## 2\n\n### 3\n\n# 4')).outerHTML,
82
+ toc(run(parse('# 1\n\n## 2\n\n### 3\n\n# 4'))).outerHTML,
83
83
  html('ul', [
84
84
  html('li', [html('a', { href: '#index::1', 'data-index': '1' }, '1'),
85
85
  html('ul', [
@@ -96,7 +96,7 @@ describe('Unit: util/toc', () => {
96
96
 
97
97
  it('132', () => {
98
98
  assert.strictEqual(
99
- toc(parse('# 1\n\n### 2\n\n## 3')).outerHTML,
99
+ toc(run(parse('# 1\n\n### 2\n\n## 3'))).outerHTML,
100
100
  html('ul', [
101
101
  html('li', [html('a', { href: '#index::1', 'data-index': '1' }, '1'),
102
102
  html('ul', [
@@ -109,7 +109,7 @@ describe('Unit: util/toc', () => {
109
109
 
110
110
  it('212', () => {
111
111
  assert.strictEqual(
112
- toc(parse('## 1\n\n# 2\n\n## 3')).outerHTML,
112
+ toc(run(parse('## 1\n\n# 2\n\n## 3'))).outerHTML,
113
113
  html('ul', [
114
114
  html('li', [html('a', { href: '#index::1', 'data-index': '1' }, '1')]),
115
115
  html('li', [html('a', { href: '#index::2', 'data-index': '2' }, '2'),
@@ -122,7 +122,7 @@ describe('Unit: util/toc', () => {
122
122
 
123
123
  it('A12A2', () => {
124
124
  assert.strictEqual(
125
- toc(parse('~~~aside\n# 1\n~~~\n\n# 2\n\n## 3\n\n~~~aside\n## 4\n~~~\n\n## 5')).outerHTML,
125
+ toc(run(parse('~~~aside\n# 1\n~~~\n\n# 2\n\n## 3\n\n~~~aside\n## 4\n~~~\n\n## 5'))).outerHTML,
126
126
  html('ul', [
127
127
  html('li', [html('a', { href: '#index::1' }, '1')]),
128
128
  html('li', [html('a', { href: '#index::2', 'data-index': '1' }, '2'),
@@ -1,20 +0,0 @@
1
- import { block } from './block';
2
- import { List, Node, Context, input } from '../../data/parser';
3
- import { inspect } from '../../../debug.test';
4
-
5
- describe('Unit: combinator/block', () => {
6
- describe('block', () => {
7
- it('invalid', () => {
8
- assert.throws(() => block(_ => new List<Node<string>>())(input(' \n', new Context())));
9
- });
10
-
11
- it('valid', () => {
12
- assert.deepStrictEqual(inspect(block(context => { context.position = context.source.length; return new List<Node<string>>(); }), input('\n', new Context())), [[], '']);
13
- assert.deepStrictEqual(inspect(block(context => { context.position = context.source.length; return new List<Node<string>>(); }), input(' \n', new Context())), [[], '']);
14
- assert.deepStrictEqual(inspect(block(context => { context.position = context.source.length; return new List<Node<string>>(); }), input('\n\n', new Context())), [[], '']);
15
- assert.deepStrictEqual(inspect(block(context => { context.position = context.source.length - 1; return new List<Node<string>>(); }), input('\n\n', new Context())), [[], '\n']);
16
- });
17
-
18
- });
19
-
20
- });
@@ -1,28 +0,0 @@
1
- import { Parser, List, Segment } from '../../data/parser';
2
- import { isEmptyline } from './line';
3
-
4
- export function block<P extends Parser>(parser: P, separation?: boolean, segment?: number): P;
5
- export function block<N>(parser: Parser<N>, separation = true, segment = 0): Parser<N> {
6
- assert(parser);
7
- return input => {
8
- const context = input;
9
- const { source, position } = context;
10
- if (position === source.length) return;
11
- if (segment !== 0 && context.segment & Segment.write) {
12
- if (context.segment !== (segment | Segment.write)) return;
13
- context.position = source.length;
14
- return new List();
15
- }
16
- const result = parser(input);
17
- if (result === undefined ||
18
- separation && !isEmptyline(source, context.position)) {
19
- context.position = position;
20
- return;
21
- }
22
- assert(context.position === source.length || source[context.position - 1] === '\n');
23
- if (segment !== 0 && context.segment & Segment.write ^ Segment.write) {
24
- context.segment = segment;
25
- }
26
- return result;
27
- };
28
- }
@@ -1,27 +0,0 @@
1
- import { Parser, Input, List, Node, Context } from '../../data/parser';
2
- import { tester } from '../../data/delimiter';
3
- import { bind } from '../monad/bind';
4
-
5
- //export function contract<P extends Parser>(patterns: string | RegExp | (string | RegExp)[], parser: P, cond: (nodes: readonly Data<P>[], rest: string) => boolean): P;
6
- //export function contract<N>(patterns: string | RegExp | (string | RegExp)[], parser: Parser<N>, cond: (nodes: readonly N[], rest: string) => boolean): Parser<N> {
7
- // return verify(validate(patterns, parser), cond);
8
- //}
9
-
10
- export function validate<P extends Parser>(pattern: string | RegExp, parser: P): P;
11
- export function validate<P extends Parser>(cond: ((input: Input<Parser.Context<P>>) => boolean), parser: P): P;
12
- export function validate<N>(pattern: string | RegExp | ((input: Input<Context>) => boolean), parser: Parser<N>): Parser<N> {
13
- if (typeof pattern === 'function') return guard(pattern, parser);
14
- const test = tester(pattern, false);
15
- return input => test(input) && parser(input);
16
- }
17
-
18
- function guard<P extends Parser>(f: (input: Input<Parser.Context<P>>) => boolean, parser: P): P;
19
- function guard<N>(f: (input: Input<Context>) => boolean, parser: Parser<N>): Parser<N> {
20
- return input => f(input) ? parser(input) : undefined;
21
- }
22
-
23
- export function verify<P extends Parser>(parser: P, cond: (nodes: List<Node<Parser.Node<P>>>, context: Parser.Context<P>) => boolean): P;
24
- export function verify<N>(parser: Parser<N>, cond: (nodes: List<Node<N>>, context: Context) => boolean): Parser<N> {
25
- assert(parser);
26
- return bind(parser, (nodes, context) => cond(nodes, context) ? nodes : undefined)
27
- }
@@ -1,21 +0,0 @@
1
- import { List, Node, Context, input } from '../../data/parser';
2
- import { line } from './line';
3
- import { inspect } from '../../../debug.test';
4
-
5
- describe('Unit: combinator/line', () => {
6
- describe('line', () => {
7
- it('invalid', () => {
8
- assert.deepStrictEqual(inspect(line(_ => new List<Node<string>>()), input('', new Context())), undefined);
9
- });
10
-
11
- it('valid', () => {
12
- assert.deepStrictEqual(inspect(line(context => { context.position = context.source.length; return new List<Node<string>>(); }), input(' ', new Context())), [[], '']);
13
- assert.deepStrictEqual(inspect(line(context => { context.position = context.source.length; return new List<Node<string>>(); }), input('\n', new Context())), [[], '']);
14
- assert.deepStrictEqual(inspect(line(context => { context.position = context.source.length; return new List<Node<string>>(); }), input('\n\n', new Context())), [[], '\n']);
15
- assert.deepStrictEqual(inspect(line(context => { context.position = context.source.length; return new List<Node<string>>(); }), input(' \n', new Context())), [[], '']);
16
- assert.deepStrictEqual(inspect(line(context => { context.position = context.source.length - 1; return new List<Node<string>>(); }), input(' \n', new Context())), [[], '']);
17
- });
18
-
19
- });
20
-
21
- });
@@ -1,42 +0,0 @@
1
- import { Parser, input } from '../../data/parser';
2
-
3
- export function line<P extends Parser>(parser: P): P;
4
- export function line<N>(parser: Parser<N>): Parser<N> {
5
- assert(parser);
6
- return context => {
7
- const { source, position } = context;
8
- if (position === source.length) return;
9
- const line = firstline(source, position);
10
- context.offset += position;
11
- const result = parser(input(line, context));
12
- context.source = source;
13
- context.position = result
14
- ? context.position === 0
15
- ? position + line.length
16
- : position + context.position
17
- : position;
18
- context.offset -= position;
19
- if (result === undefined ||
20
- context.position < position + line.length && !isEmptyline(source, context.position)) {
21
- context.position = position;
22
- return;
23
- }
24
- context.position = position + line.length;
25
- return result;
26
- };
27
- }
28
-
29
- export function firstline(source: string, position: number): string {
30
- const i = source.indexOf('\n', position);
31
- return i === -1
32
- ? source.slice(position)
33
- : source.slice(position, i + 1);
34
- }
35
-
36
- const emptyline = /[^\S\r\n]*(?:$|\r?\n)/y;
37
- export function isEmptyline(source: string, position: number): boolean {
38
- emptyline.lastIndex = position;
39
- return source.length === position
40
- || source[position] === '\n'
41
- || emptyline.test(source);
42
- }
@@ -1,5 +0,0 @@
1
- import { Parser, List, Context } from '../../data/parser';
2
-
3
- export function clear<D extends Parser<unknown, C>[], C extends Context>(parser: Parser<unknown, C, D>): Parser<never, C, D> {
4
- return input => parser(input) && new List();
5
- }
@@ -1,22 +0,0 @@
1
- import { Parser, List, Context, subinput } from '../../data/parser';
2
-
3
- export function convert<P extends Parser>(conv: (source: string, context: Parser.Context<P>) => string, parser: P): P;
4
- export function convert<N>(conv: (source: string, context: Context) => string, parser: Parser<N>): Parser<N> {
5
- assert(parser);
6
- return input => {
7
- const context = input;
8
- const { source, position, offset } = context;
9
- if (position === source.length) return;
10
- const src = conv(source.slice(position), context);
11
- assert(context.position === position);
12
- if (src === '') {
13
- context.position = source.length;
14
- return new List();
15
- }
16
- const result = parser(subinput(src, context));
17
- context.position = result ? context.source.length : position;
18
- assert(context.source === source);
19
- assert(context.offset === offset);
20
- return result;
21
- };
22
- }
@@ -1,7 +0,0 @@
1
- import { Parser, List, Node, Context } from '../../data/parser';
2
- import { fmap } from '../monad/fmap';
3
-
4
- export function dup<N, C extends Context, D extends Parser<unknown, C>[]>(parser: Parser<N, C, D>): Parser<List<Node<N>>, C, D>;
5
- export function dup<N>(parser: Parser<N>): Parser<List<Node<N>>> {
6
- return fmap(parser, nodes => new List([new Node(nodes)]));
7
- }
@@ -1,54 +0,0 @@
1
- import { Parser, List, Node, Context, failsafe } from '../../data/parser';
2
- import { spend } from '../../../combinator';
3
- import { firstline, isEmptyline } from '../constraint/line';
4
- import { push } from 'spica/array';
5
-
6
- export function fence<C extends Context, D extends Parser<unknown, C>[]>(opener: RegExp, limit: number, separation = true): Parser<string, C, D> {
7
- assert(!opener.flags.match(/[gm]/) && opener.sticky && !opener.source.startsWith('^'));
8
- return failsafe(input => {
9
- const context = input;
10
- const { source, position } = context;
11
- if (position === source.length) return;
12
- opener.lastIndex = position;
13
- const matches = opener.exec(source);
14
- if (!matches) return;
15
- assert(matches[0] === firstline(source, position));
16
- spend(context, matches[0].length);
17
- const delim = matches[1];
18
- assert(delim && delim === delim.trim());
19
- if (matches[0].includes(delim, delim.length)) return;
20
- context.position += matches[0].length;
21
- // Prevent annoying parsing in editing.
22
- const secondline = firstline(source, context.position);
23
- if (isEmptyline(secondline, 0) && firstline(source, context.position + secondline.length).trimEnd() !== delim) return;
24
- let block = '';
25
- let closer = '';
26
- let overflow = '';
27
- for (let count = 1; ; ++count) {
28
- if (context.position === source.length) break;
29
- const line = firstline(source, context.position);
30
- if ((closer || count > limit + 1) && isEmptyline(line, 0)) break;
31
- if(closer) {
32
- overflow += line;
33
- }
34
- if (!closer && count <= limit + 1 && line.slice(0, delim.length) === delim && line.trimEnd() === delim) {
35
- closer = line;
36
- if (isEmptyline(source, context.position + line.length)) {
37
- context.position += line.length;
38
- break;
39
- }
40
- if (!separation) {
41
- context.position += line.length;
42
- break;
43
- }
44
- assert(!overflow);
45
- overflow = line;
46
- }
47
- if (!overflow) {
48
- block += line;
49
- }
50
- context.position += line.length;
51
- }
52
- return new List(push([block, overflow, closer], matches).map(str => new Node(str)));
53
- });
54
- }
@@ -1,31 +0,0 @@
1
- import { indent } from './indent';
2
- import { List, Node, Context, input } from '../../data/parser';
3
- import { inspect } from '../../../debug.test';
4
-
5
- describe('Unit: combinator/indent', () => {
6
- describe('indent', () => {
7
- it('valid', () => {
8
- const parser = indent(context => { context.position = context.source.length; return new List([new Node(context.source)]); });
9
- assert.deepStrictEqual(inspect(parser, input('', new Context())), undefined);
10
- assert.deepStrictEqual(inspect(parser, input(' ', new Context())), undefined);
11
- assert.deepStrictEqual(inspect(parser, input(' ', new Context())), undefined);
12
- assert.deepStrictEqual(inspect(parser, input('a ', new Context())), undefined);
13
- assert.deepStrictEqual(inspect(parser, input(' a', new Context())), [['a'], '']);
14
- assert.deepStrictEqual(inspect(parser, input(' a ', new Context())), [['a '], '']);
15
- assert.deepStrictEqual(inspect(parser, input(' a\n', new Context())), [['a'], '']);
16
- assert.deepStrictEqual(inspect(parser, input(' a \n', new Context())), [['a '], '']);
17
- assert.deepStrictEqual(inspect(parser, input(' a', new Context())), [['a'], '']);
18
- assert.deepStrictEqual(inspect(parser, input(' a', new Context())), [['a'], '']);
19
- assert.deepStrictEqual(inspect(parser, input(' a', new Context())), [['a'], '']);
20
- assert.deepStrictEqual(inspect(parser, input(' a', new Context())), [[' a'], '']);
21
- assert.deepStrictEqual(inspect(parser, input(' a\n a', new Context())), [['a\na'], '']);
22
- assert.deepStrictEqual(inspect(parser, input(' a\n a', new Context())), [['a\n a'], '']);
23
- assert.deepStrictEqual(inspect(parser, input(' a\n a', new Context())), [['a\n a'], '']);
24
- assert.deepStrictEqual(inspect(parser, input(' a\n a', new Context())), [['a'], ' a']);
25
- assert.deepStrictEqual(inspect(parser, input(' \ta', new Context())), [['\ta'], '']);
26
- assert.deepStrictEqual(inspect(parser, input('\ta', new Context())), [['a'], '']);
27
- });
28
-
29
- });
30
-
31
- });
@@ -1,39 +0,0 @@
1
- import { Parser, List, Node, subinput, failsafe } from '../../data/parser';
2
- import { some } from '../../data/parser/some';
3
- import { block } from '../constraint/block';
4
- import { line } from '../constraint/line';
5
- import { bind } from '../monad/bind';
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<N>(opener: RegExp | Parser<N>, parser: Parser<N> | boolean = false, separation = false): Parser<N> {
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<N>);
20
- return failsafe(bind(block(match(
21
- opener,
22
- memoize(
23
- ([indent]) =>
24
- some(line(open(indent, context => {
25
- const { source, position } = context;
26
- context.position = source.length;
27
- return new List([new Node(source.slice(position))]);
28
- }))),
29
- ([indent]) => indent.length * 2 + -(indent[0] === ' '), [], 2 ** 4 - 1)), separation),
30
- (lines, context) =>
31
- parser(subinput(trimBlockEnd(lines.foldl((acc, node) => acc + node.value, '')), context))));
32
- }
33
-
34
- function trimBlockEnd(block: string): string {
35
- return block === ''
36
- || block.at(-1) !== '\n'
37
- ? block
38
- : block.slice(0, -1);
39
- }
@@ -1,8 +0,0 @@
1
- import { Parser } from '../../data/parser';
2
-
3
- export function lazy<P extends Parser>(builder: () => P): P;
4
- export function lazy<N>(builder: () => Parser<N>): Parser<N> {
5
- let parser: Parser<N>;
6
- return input =>
7
- (parser ??= builder())(input);
8
- }
@@ -1,27 +0,0 @@
1
- import { Parser } from '../../data/parser';
2
- import { spend } from '../../../combinator';
3
-
4
- export function match<P extends Parser>(pattern: RegExp, f: (matched: RegExpMatchArray) => P): P;
5
- export function match<N>(pattern: RegExp, f: (matched: RegExpMatchArray) => Parser<N>): Parser<N> {
6
- assert(!pattern.flags.match(/[gm]/) && pattern.sticky && !pattern.source.startsWith('^'));
7
- const count = typeof pattern === 'object'
8
- ? /[^^\\*+][*+]|{\d+,}/.test(pattern.source)
9
- : false;
10
- return input => {
11
- const context = input;
12
- const { source, position } = context;
13
- if (position === source.length) return;
14
- pattern.lastIndex = position;
15
- const params = pattern.exec(source);
16
- if (!params) return;
17
- assert(source.startsWith(params[0], position));
18
- count && spend(context, params[0].length);
19
- const result = f(params)(input);
20
- context.position += result
21
- ? context.position === position
22
- ? params[0].length
23
- : 0
24
- : context.position - position;
25
- return result;
26
- };
27
- }
@@ -1,18 +0,0 @@
1
- import { Parser, Input, Result, Context } from '../../data/parser';
2
-
3
- export function recover<P extends Parser>(parser: P, catcher: (input: Input<Parser.Context<P>>, reason: unknown) => Result<Parser.Node<P>>): P;
4
- export function recover<N>(parser: Parser<N>, catcher: (input: Input<Context>, reason: unknown) => Result<N>): Parser<N> {
5
- return input => {
6
- const context = input;
7
- const { source, position } = context;
8
- try {
9
- return parser(input);
10
- }
11
- catch (reason) {
12
- assert(reason instanceof Error && reason.name === 'AssertionError' && !+console.error(reason) || 1);
13
- context.source = source;
14
- context.position = position;
15
- return catcher(input, reason);
16
- }
17
- };
18
- }
@@ -1,8 +0,0 @@
1
- import { Parser, List, Node } from '../../data/parser';
2
- import { fmap } from '../monad/fmap';
3
-
4
- export function reverse<P extends Parser>(parser: P): P;
5
- export function reverse<N>(parser: Parser<N>): Parser<N> {
6
- return fmap(parser, nodes => nodes.foldr((node, acc) => acc.push(nodes.delete(node)) && acc, new List<Node<N>>()));
7
- }
8
-
@@ -1,61 +0,0 @@
1
- import { Parser, input, failsafe } from '../../data/parser';
2
- import { matcher } from '../../data/delimiter';
3
-
4
- export function focus<P extends Parser>(scope: string | RegExp, parser: P, slice?: boolean): P;
5
- export function focus<N>(scope: string | RegExp, parser: Parser<N>, slice = true): Parser<N> {
6
- assert(parser);
7
- const match = matcher(scope, false);
8
- return failsafe(context => {
9
- const { SID, source, position } = context;
10
- if (position === source.length) return;
11
- const src = match(context)?.head?.value ?? '';
12
- assert(source.startsWith(src, position));
13
- if (src === '') return;
14
- const range = context.range = src.length;
15
- if (!slice) {
16
- const result = parser(context);
17
- context.position += result && context.position === position ? range : 0;
18
- return result;
19
- }
20
- context.offset += position;
21
- const result = parser(input(src, context));
22
- context.source = source;
23
- context.SID = SID;
24
- context.position += position;
25
- context.position += result && context.position === position ? src.length : 0;
26
- context.offset -= position;
27
- return result;
28
- });
29
- }
30
-
31
- //export function rewrite<N, C extends Ctx, D extends Parser<unknown, C>[]>(scope: Parser<unknown, C, D>, parser: Parser<N, C, never>): Parser<N, C, D>;
32
- export function rewrite<P extends Parser>(scope: Parser<unknown, Parser.Context<P>>, parser: P, slice?: boolean): P;
33
- export function rewrite<N>(scope: Parser, parser: Parser<N>, slice = true): Parser<N> {
34
- assert(scope);
35
- assert(parser);
36
- return failsafe(context => {
37
- const { SID, source, position } = context;
38
- if (position === source.length) return;
39
- const res1 = scope(context);
40
- assert(context.position > position || !res1);
41
- if (res1 === undefined || context.position < position) return;
42
- const range = context.range = context.position - position;
43
- if (!slice) {
44
- context.position = position;
45
- const res2 = parser(context);
46
- context.position += res2 && context.position === position ? range : 0;
47
- return res2;
48
- }
49
- const src = source.slice(position, context.position);
50
- assert(src !== '');
51
- assert(source.startsWith(src, position));
52
- context.offset += position;
53
- const res2 = parser(input(src, context));
54
- context.SID = SID;
55
- context.source = source;
56
- context.position += position;
57
- context.position += res2 && context.position === position ? src.length : 0;
58
- context.offset -= position;
59
- return res2;
60
- });
61
- }