@tinta-lang/syntax 0.0.0-stage → 0.1.16

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 (245) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +16 -2
  3. package/THIRD_PARTY_NOTICES.txt +13651 -0
  4. package/dist/directive.d.ts +40 -0
  5. package/dist/editing.d.ts +46 -0
  6. package/dist/enrichment.d.ts +5 -0
  7. package/dist/expression.d.ts +26 -0
  8. package/dist/image-destinations.d.ts +3 -0
  9. package/dist/index.d.ts +8 -0
  10. package/dist/index.js +10118 -0
  11. package/dist/parser.d.ts +40 -0
  12. package/dist/registry.d.ts +144 -0
  13. package/dist/source.d.ts +28 -0
  14. package/package.json +289 -3
  15. package/parser-provenance.json +1419 -0
  16. package/patches/README.md +5 -0
  17. package/patches/micromark.patch +84 -0
  18. package/vendor/@noble/hashes/LICENSE +21 -0
  19. package/vendor/@noble/hashes/_md.js +209 -0
  20. package/vendor/@noble/hashes/_u64.js +77 -0
  21. package/vendor/@noble/hashes/package.json +91 -0
  22. package/vendor/@noble/hashes/sha2.js +446 -0
  23. package/vendor/@noble/hashes/utils.js +687 -0
  24. package/vendor/character-entities/index.js +2132 -0
  25. package/vendor/character-entities/license +22 -0
  26. package/vendor/character-entities/package.json +78 -0
  27. package/vendor/decode-named-character-reference/index.js +19 -0
  28. package/vendor/decode-named-character-reference/license +22 -0
  29. package/vendor/decode-named-character-reference/package.json +90 -0
  30. package/vendor/devlop/lib/default.js +9 -0
  31. package/vendor/devlop/license +22 -0
  32. package/vendor/devlop/package.json +80 -0
  33. package/vendor/longest-streak/index.js +36 -0
  34. package/vendor/longest-streak/license +22 -0
  35. package/vendor/longest-streak/package.json +73 -0
  36. package/vendor/markdown-table/index.js +393 -0
  37. package/vendor/markdown-table/license +22 -0
  38. package/vendor/markdown-table/package.json +77 -0
  39. package/vendor/mdast-util-from-markdown/index.js +2 -0
  40. package/vendor/mdast-util-from-markdown/lib/index.js +1270 -0
  41. package/vendor/mdast-util-from-markdown/license +22 -0
  42. package/vendor/mdast-util-from-markdown/package.json +98 -0
  43. package/vendor/mdast-util-gfm-strikethrough/index.js +5 -0
  44. package/vendor/mdast-util-gfm-strikethrough/lib/index.js +133 -0
  45. package/vendor/mdast-util-gfm-strikethrough/license +22 -0
  46. package/vendor/mdast-util-gfm-strikethrough/package.json +84 -0
  47. package/vendor/mdast-util-gfm-table/index.js +2 -0
  48. package/vendor/mdast-util-gfm-table/lib/index.js +300 -0
  49. package/vendor/mdast-util-gfm-table/license +22 -0
  50. package/vendor/mdast-util-gfm-table/package.json +101 -0
  51. package/vendor/mdast-util-gfm-task-list-item/index.js +4 -0
  52. package/vendor/mdast-util-gfm-task-list-item/lib/index.js +141 -0
  53. package/vendor/mdast-util-gfm-task-list-item/license +22 -0
  54. package/vendor/mdast-util-gfm-task-list-item/package.json +90 -0
  55. package/vendor/mdast-util-phrasing/index.js +1 -0
  56. package/vendor/mdast-util-phrasing/lib/index.js +45 -0
  57. package/vendor/mdast-util-phrasing/license +23 -0
  58. package/vendor/mdast-util-phrasing/package.json +78 -0
  59. package/vendor/mdast-util-to-markdown/index.js +3 -0
  60. package/vendor/mdast-util-to-markdown/lib/configure.js +98 -0
  61. package/vendor/mdast-util-to-markdown/lib/handle/blockquote.js +40 -0
  62. package/vendor/mdast-util-to-markdown/lib/handle/break.js +41 -0
  63. package/vendor/mdast-util-to-markdown/lib/handle/code.js +86 -0
  64. package/vendor/mdast-util-to-markdown/lib/handle/definition.js +83 -0
  65. package/vendor/mdast-util-to-markdown/lib/handle/emphasis.js +67 -0
  66. package/vendor/mdast-util-to-markdown/lib/handle/heading.js +84 -0
  67. package/vendor/mdast-util-to-markdown/lib/handle/html.js +27 -0
  68. package/vendor/mdast-util-to-markdown/lib/handle/image-reference.js +78 -0
  69. package/vendor/mdast-util-to-markdown/lib/handle/image.js +92 -0
  70. package/vendor/mdast-util-to-markdown/lib/handle/index.js +45 -0
  71. package/vendor/mdast-util-to-markdown/lib/handle/inline-code.js +86 -0
  72. package/vendor/mdast-util-to-markdown/lib/handle/link-reference.js +78 -0
  73. package/vendor/mdast-util-to-markdown/lib/handle/link.js +129 -0
  74. package/vendor/mdast-util-to-markdown/lib/handle/list-item.js +73 -0
  75. package/vendor/mdast-util-to-markdown/lib/handle/list.js +107 -0
  76. package/vendor/mdast-util-to-markdown/lib/handle/paragraph.js +27 -0
  77. package/vendor/mdast-util-to-markdown/lib/handle/root.js +34 -0
  78. package/vendor/mdast-util-to-markdown/lib/handle/strong.js +67 -0
  79. package/vendor/mdast-util-to-markdown/lib/handle/text.js +30 -0
  80. package/vendor/mdast-util-to-markdown/lib/handle/thematic-break.js +27 -0
  81. package/vendor/mdast-util-to-markdown/lib/index.js +213 -0
  82. package/vendor/mdast-util-to-markdown/lib/join.js +56 -0
  83. package/vendor/mdast-util-to-markdown/lib/unsafe.js +160 -0
  84. package/vendor/mdast-util-to-markdown/lib/util/association.js +33 -0
  85. package/vendor/mdast-util-to-markdown/lib/util/character.js +32 -0
  86. package/vendor/mdast-util-to-markdown/lib/util/check-bullet-ordered.js +25 -0
  87. package/vendor/mdast-util-to-markdown/lib/util/check-bullet-other.js +42 -0
  88. package/vendor/mdast-util-to-markdown/lib/util/check-bullet.js +25 -0
  89. package/vendor/mdast-util-to-markdown/lib/util/check-emphasis.js +25 -0
  90. package/vendor/mdast-util-to-markdown/lib/util/check-fence.js +25 -0
  91. package/vendor/mdast-util-to-markdown/lib/util/check-list-item-indent.js +25 -0
  92. package/vendor/mdast-util-to-markdown/lib/util/check-quote.js +25 -0
  93. package/vendor/mdast-util-to-markdown/lib/util/check-rule-repetition.js +25 -0
  94. package/vendor/mdast-util-to-markdown/lib/util/check-rule.js +25 -0
  95. package/vendor/mdast-util-to-markdown/lib/util/check-strong.js +25 -0
  96. package/vendor/mdast-util-to-markdown/lib/util/compile-pattern.js +30 -0
  97. package/vendor/mdast-util-to-markdown/lib/util/container-flow.js +93 -0
  98. package/vendor/mdast-util-to-markdown/lib/util/container-phrasing.js +704 -0
  99. package/vendor/mdast-util-to-markdown/lib/util/encode-character-reference.js +11 -0
  100. package/vendor/mdast-util-to-markdown/lib/util/encode-info.js +82 -0
  101. package/vendor/mdast-util-to-markdown/lib/util/format-code-as-indented.js +27 -0
  102. package/vendor/mdast-util-to-markdown/lib/util/format-heading-as-setext.js +39 -0
  103. package/vendor/mdast-util-to-markdown/lib/util/format-link-as-autolink.js +39 -0
  104. package/vendor/mdast-util-to-markdown/lib/util/html-kind.js +63 -0
  105. package/vendor/mdast-util-to-markdown/lib/util/indent-lines.js +40 -0
  106. package/vendor/mdast-util-to-markdown/lib/util/pattern-in-scope.js +52 -0
  107. package/vendor/mdast-util-to-markdown/lib/util/safe.js +263 -0
  108. package/vendor/mdast-util-to-markdown/lib/util/track.js +55 -0
  109. package/vendor/mdast-util-to-markdown/license +22 -0
  110. package/vendor/mdast-util-to-markdown/package.json +88 -0
  111. package/vendor/mdast-util-to-string/index.js +5 -0
  112. package/vendor/mdast-util-to-string/lib/index.js +108 -0
  113. package/vendor/mdast-util-to-string/license +22 -0
  114. package/vendor/mdast-util-to-string/package.json +78 -0
  115. package/vendor/micromark/index.js +60 -0
  116. package/vendor/micromark/lib/compile.js +1068 -0
  117. package/vendor/micromark/lib/constructs.js +85 -0
  118. package/vendor/micromark/lib/create-tokenizer.js +615 -0
  119. package/vendor/micromark/lib/initialize/content.js +79 -0
  120. package/vendor/micromark/lib/initialize/document.js +370 -0
  121. package/vendor/micromark/lib/initialize/flow.js +58 -0
  122. package/vendor/micromark/lib/initialize/text.js +216 -0
  123. package/vendor/micromark/lib/parse.js +56 -0
  124. package/vendor/micromark/lib/postprocess.js +18 -0
  125. package/vendor/micromark/lib/preprocess.js +112 -0
  126. package/vendor/micromark/license +22 -0
  127. package/vendor/micromark/package.json +87 -0
  128. package/vendor/micromark-core-commonmark/index.js +22 -0
  129. package/vendor/micromark-core-commonmark/lib/attention.js +231 -0
  130. package/vendor/micromark-core-commonmark/lib/autolink.js +233 -0
  131. package/vendor/micromark-core-commonmark/lib/blank-line.js +61 -0
  132. package/vendor/micromark-core-commonmark/lib/block-quote.js +143 -0
  133. package/vendor/micromark-core-commonmark/lib/character-escape.js +64 -0
  134. package/vendor/micromark-core-commonmark/lib/character-reference.js +149 -0
  135. package/vendor/micromark-core-commonmark/lib/code-fenced.js +422 -0
  136. package/vendor/micromark-core-commonmark/lib/code-indented.js +159 -0
  137. package/vendor/micromark-core-commonmark/lib/code-text.js +225 -0
  138. package/vendor/micromark-core-commonmark/lib/content.js +163 -0
  139. package/vendor/micromark-core-commonmark/lib/definition.js +254 -0
  140. package/vendor/micromark-core-commonmark/lib/hard-break-escape.js +60 -0
  141. package/vendor/micromark-core-commonmark/lib/heading-atx.js +191 -0
  142. package/vendor/micromark-core-commonmark/lib/html-flow.js +827 -0
  143. package/vendor/micromark-core-commonmark/lib/html-text.js +678 -0
  144. package/vendor/micromark-core-commonmark/lib/label-end.js +561 -0
  145. package/vendor/micromark-core-commonmark/lib/label-start-image.js +110 -0
  146. package/vendor/micromark-core-commonmark/lib/label-start-link.js +63 -0
  147. package/vendor/micromark-core-commonmark/lib/line-ending.js +33 -0
  148. package/vendor/micromark-core-commonmark/lib/list.js +213 -0
  149. package/vendor/micromark-core-commonmark/lib/partial-non-lazy-continuation.js +61 -0
  150. package/vendor/micromark-core-commonmark/lib/setext-underline.js +187 -0
  151. package/vendor/micromark-core-commonmark/lib/thematic-break.js +102 -0
  152. package/vendor/micromark-core-commonmark/license +22 -0
  153. package/vendor/micromark-core-commonmark/package.json +62 -0
  154. package/vendor/micromark-extension-gfm-strikethrough/index.js +3 -0
  155. package/vendor/micromark-extension-gfm-strikethrough/lib/html.js +26 -0
  156. package/vendor/micromark-extension-gfm-strikethrough/lib/syntax.js +142 -0
  157. package/vendor/micromark-extension-gfm-strikethrough/license +22 -0
  158. package/vendor/micromark-extension-gfm-strikethrough/package.json +129 -0
  159. package/vendor/micromark-extension-gfm-table/index.js +2 -0
  160. package/vendor/micromark-extension-gfm-table/lib/edit-map.js +222 -0
  161. package/vendor/micromark-extension-gfm-table/lib/html.js +135 -0
  162. package/vendor/micromark-extension-gfm-table/lib/infer.js +52 -0
  163. package/vendor/micromark-extension-gfm-table/lib/syntax.js +846 -0
  164. package/vendor/micromark-extension-gfm-table/license +22 -0
  165. package/vendor/micromark-extension-gfm-table/package.json +86 -0
  166. package/vendor/micromark-extension-gfm-task-list-item/index.js +2 -0
  167. package/vendor/micromark-extension-gfm-task-list-item/lib/html.js +29 -0
  168. package/vendor/micromark-extension-gfm-task-list-item/lib/syntax.js +158 -0
  169. package/vendor/micromark-extension-gfm-task-list-item/license +22 -0
  170. package/vendor/micromark-extension-gfm-task-list-item/package.json +131 -0
  171. package/vendor/micromark-factory-destination/index.js +206 -0
  172. package/vendor/micromark-factory-destination/license +22 -0
  173. package/vendor/micromark-factory-destination/package.json +57 -0
  174. package/vendor/micromark-factory-label/index.js +148 -0
  175. package/vendor/micromark-factory-label/license +22 -0
  176. package/vendor/micromark-factory-label/package.json +60 -0
  177. package/vendor/micromark-factory-space/index.js +116 -0
  178. package/vendor/micromark-factory-space/license +22 -0
  179. package/vendor/micromark-factory-space/package.json +46 -0
  180. package/vendor/micromark-factory-title/index.js +158 -0
  181. package/vendor/micromark-factory-title/license +22 -0
  182. package/vendor/micromark-factory-title/package.json +58 -0
  183. package/vendor/micromark-factory-whitespace/index.js +44 -0
  184. package/vendor/micromark-factory-whitespace/license +22 -0
  185. package/vendor/micromark-factory-whitespace/package.json +57 -0
  186. package/vendor/micromark-util-character/index.js +246 -0
  187. package/vendor/micromark-util-character/license +22 -0
  188. package/vendor/micromark-util-character/package.json +57 -0
  189. package/vendor/micromark-util-chunked/index.js +81 -0
  190. package/vendor/micromark-util-chunked/license +22 -0
  191. package/vendor/micromark-util-chunked/package.json +57 -0
  192. package/vendor/micromark-util-classify-character/index.js +27 -0
  193. package/vendor/micromark-util-classify-character/license +22 -0
  194. package/vendor/micromark-util-classify-character/package.json +59 -0
  195. package/vendor/micromark-util-combine-extensions/index.js +143 -0
  196. package/vendor/micromark-util-combine-extensions/license +22 -0
  197. package/vendor/micromark-util-combine-extensions/package.json +52 -0
  198. package/vendor/micromark-util-decode-numeric-character-reference/index.js +32 -0
  199. package/vendor/micromark-util-decode-numeric-character-reference/license +22 -0
  200. package/vendor/micromark-util-decode-numeric-character-reference/package.json +59 -0
  201. package/vendor/micromark-util-decode-string/index.js +45 -0
  202. package/vendor/micromark-util-decode-string/license +22 -0
  203. package/vendor/micromark-util-decode-string/package.json +63 -0
  204. package/vendor/micromark-util-edit-map/index.js +225 -0
  205. package/vendor/micromark-util-edit-map/license +22 -0
  206. package/vendor/micromark-util-edit-map/package.json +48 -0
  207. package/vendor/micromark-util-encode/index.js +33 -0
  208. package/vendor/micromark-util-encode/license +22 -0
  209. package/vendor/micromark-util-encode/package.json +47 -0
  210. package/vendor/micromark-util-html-tag-name/index.js +93 -0
  211. package/vendor/micromark-util-html-tag-name/license +22 -0
  212. package/vendor/micromark-util-html-tag-name/package.json +47 -0
  213. package/vendor/micromark-util-normalize-identifier/index.js +33 -0
  214. package/vendor/micromark-util-normalize-identifier/license +22 -0
  215. package/vendor/micromark-util-normalize-identifier/package.json +58 -0
  216. package/vendor/micromark-util-resolve-all/index.js +32 -0
  217. package/vendor/micromark-util-resolve-all/license +22 -0
  218. package/vendor/micromark-util-resolve-all/package.json +48 -0
  219. package/vendor/micromark-util-sanitize-uri/index.js +107 -0
  220. package/vendor/micromark-util-sanitize-uri/license +22 -0
  221. package/vendor/micromark-util-sanitize-uri/package.json +59 -0
  222. package/vendor/micromark-util-subtokenize/index.js +228 -0
  223. package/vendor/micromark-util-subtokenize/lib/splice-buffer.js +251 -0
  224. package/vendor/micromark-util-subtokenize/license +22 -0
  225. package/vendor/micromark-util-subtokenize/package.json +60 -0
  226. package/vendor/unist-util-is/index.js +7 -0
  227. package/vendor/unist-util-is/lib/index.js +296 -0
  228. package/vendor/unist-util-is/license +22 -0
  229. package/vendor/unist-util-is/package.json +103 -0
  230. package/vendor/unist-util-stringify-position/index.js +1 -0
  231. package/vendor/unist-util-stringify-position/lib/index.js +84 -0
  232. package/vendor/unist-util-stringify-position/license +22 -0
  233. package/vendor/unist-util-stringify-position/package.json +80 -0
  234. package/vendor/unist-util-visit/index.js +2 -0
  235. package/vendor/unist-util-visit/lib/index.js +312 -0
  236. package/vendor/unist-util-visit/license +22 -0
  237. package/vendor/unist-util-visit/package.json +108 -0
  238. package/vendor/unist-util-visit-parents/index.js +2 -0
  239. package/vendor/unist-util-visit-parents/lib/color.js +7 -0
  240. package/vendor/unist-util-visit-parents/lib/index.js +399 -0
  241. package/vendor/unist-util-visit-parents/license +22 -0
  242. package/vendor/unist-util-visit-parents/package.json +106 -0
  243. package/vendor/zwitch/index.js +118 -0
  244. package/vendor/zwitch/license +22 -0
  245. package/vendor/zwitch/package.json +72 -0
@@ -0,0 +1,85 @@
1
+ /**
2
+ * @import {Extension} from 'micromark-util-types'
3
+ */
4
+
5
+ import { attention, autolink, blockQuote, characterEscape, characterReference, codeFenced, codeIndented, codeText, definition, hardBreakEscape, headingAtx, htmlFlow, htmlText, labelEnd, labelStartImage, labelStartLink, lineEnding, list, setextUnderline, thematicBreak } from 'micromark-core-commonmark';
6
+ import { resolver as resolveText } from './initialize/text.js';
7
+
8
+ /** @satisfies {Extension['document']} */
9
+ export const document = {
10
+ [42]: list,
11
+ [43]: list,
12
+ [45]: list,
13
+ [48]: list,
14
+ [49]: list,
15
+ [50]: list,
16
+ [51]: list,
17
+ [52]: list,
18
+ [53]: list,
19
+ [54]: list,
20
+ [55]: list,
21
+ [56]: list,
22
+ [57]: list,
23
+ [62]: blockQuote
24
+ };
25
+
26
+ /** @satisfies {Extension['contentInitial']} */
27
+ export const contentInitial = {
28
+ [91]: definition
29
+ };
30
+
31
+ /** @satisfies {Extension['flowInitial']} */
32
+ export const flowInitial = {
33
+ [-2]: codeIndented,
34
+ [-1]: codeIndented,
35
+ [32]: codeIndented
36
+ };
37
+
38
+ /** @satisfies {Extension['flow']} */
39
+ export const flow = {
40
+ [35]: headingAtx,
41
+ [42]: thematicBreak,
42
+ [45]: [setextUnderline, thematicBreak],
43
+ [60]: htmlFlow,
44
+ [61]: setextUnderline,
45
+ [95]: thematicBreak,
46
+ [96]: codeFenced,
47
+ [126]: codeFenced
48
+ };
49
+
50
+ /** @satisfies {Extension['string']} */
51
+ export const string = {
52
+ [38]: characterReference,
53
+ [92]: characterEscape
54
+ };
55
+
56
+ /** @satisfies {Extension['text']} */
57
+ export const text = {
58
+ [-5]: lineEnding,
59
+ [-4]: lineEnding,
60
+ [-3]: lineEnding,
61
+ [33]: labelStartImage,
62
+ [38]: characterReference,
63
+ [42]: attention,
64
+ [60]: [autolink, htmlText],
65
+ [91]: labelStartLink,
66
+ [92]: [hardBreakEscape, characterEscape],
67
+ [93]: labelEnd,
68
+ [95]: attention,
69
+ [96]: codeText
70
+ };
71
+
72
+ /** @satisfies {Extension['insideSpan']} */
73
+ export const insideSpan = {
74
+ null: [attention, resolveText]
75
+ };
76
+
77
+ /** @satisfies {Extension['attentionMarkers']} */
78
+ export const attentionMarkers = {
79
+ null: [42, 95]
80
+ };
81
+
82
+ /** @satisfies {Extension['disable']} */
83
+ export const disable = {
84
+ null: []
85
+ };
@@ -0,0 +1,615 @@
1
+ /**
2
+ * @import {
3
+ * Chunk,
4
+ * Code,
5
+ * ConstructRecord,
6
+ * Construct,
7
+ * Effects,
8
+ * InitialConstruct,
9
+ * ParseContext,
10
+ * Point,
11
+ * State,
12
+ * TokenizeContext,
13
+ * Token
14
+ * } from 'micromark-util-types'
15
+ */
16
+
17
+ /**
18
+ * @callback Restore
19
+ * Restore the state.
20
+ * @returns {undefined}
21
+ * Nothing.
22
+ *
23
+ * @typedef Info
24
+ * Info.
25
+ * @property {Restore} restore
26
+ * Restore.
27
+ * @property {number} from
28
+ * From.
29
+ *
30
+ * @callback ReturnHandle
31
+ * Handle a successful run.
32
+ * @param {Construct} construct
33
+ * Construct.
34
+ * @param {Info} info
35
+ * Info.
36
+ * @returns {undefined}
37
+ * Nothing.
38
+ */
39
+
40
+ import { markdownLineEnding } from 'micromark-util-character';
41
+ import { push, splice } from 'micromark-util-chunked';
42
+ import { resolveAll } from 'micromark-util-resolve-all';
43
+ /**
44
+ * Create a tokenizer.
45
+ * Tokenizers deal with one type of data (e.g., containers, flow, text).
46
+ * The parser is the object dealing with it all.
47
+ * `initialize` works like other constructs, except that only its `tokenize`
48
+ * function is used, in which case it doesn’t receive an `ok` or `nok`.
49
+ * `from` can be given to set the point before the first character, although
50
+ * when further lines are indented, they must be set with `defineSkip`.
51
+ *
52
+ * @param {ParseContext} parser
53
+ * Parser.
54
+ * @param {InitialConstruct} initialize
55
+ * Construct.
56
+ * @param {Omit<Point, '_bufferIndex' | '_index'> | undefined} [from]
57
+ * Point (optional).
58
+ * @returns {TokenizeContext}
59
+ * Context.
60
+ */
61
+ export function createTokenizer(parser, initialize, from, observeTokenizer) {
62
+ /** @type {Point} */
63
+ let point = {
64
+ _bufferIndex: -1,
65
+ _index: 0,
66
+ line: from && from.line || 1,
67
+ column: from && from.column || 1,
68
+ offset: from && from.offset || 0
69
+ };
70
+ /** @type {Record<string, number>} */
71
+ const columnStart = {};
72
+ /** @type {Array<Construct>} */
73
+ const resolveAllConstructs = [];
74
+ /** @type {Array<Chunk>} */
75
+ let chunks = [];
76
+ /** @type {Array<Token>} */
77
+ let stack = [];
78
+ /** @type {boolean | undefined} */
79
+ let consumed = true;
80
+
81
+ /**
82
+ * Tools used for tokenizing.
83
+ *
84
+ * @type {Effects}
85
+ */
86
+ const effects = {
87
+ attempt: constructFactory(onsuccessfulconstruct),
88
+ check: constructFactory(onsuccessfulcheck),
89
+ consume,
90
+ enter,
91
+ exit,
92
+ interrupt: constructFactory(onsuccessfulcheck, {
93
+ interrupt: true
94
+ })
95
+ };
96
+
97
+ /**
98
+ * State and tools for resolving and serializing.
99
+ *
100
+ * @type {TokenizeContext}
101
+ */
102
+ const context = {
103
+ code: null,
104
+ containerState: {},
105
+ defineSkip,
106
+ events: [],
107
+ now,
108
+ parser,
109
+ previous: null,
110
+ sliceSerialize,
111
+ sliceStream,
112
+ write
113
+ };
114
+
115
+ /**
116
+ * The state function.
117
+ *
118
+ * @type {State | undefined}
119
+ */
120
+ let state = initialize.tokenize.call(context, effects);
121
+
122
+ /**
123
+ * Track which character we expect to be consumed, to catch bugs.
124
+ *
125
+ * @type {Code}
126
+ */
127
+ let expectedCode;
128
+ if (initialize.resolveAll) {
129
+ resolveAllConstructs.push(initialize);
130
+ }
131
+ return context;
132
+
133
+ /** @type {TokenizeContext['write']} */
134
+ function write(slice) {
135
+ chunks = push(chunks, slice);
136
+ main();
137
+
138
+ // Exit if we’re not done, resolve might change stuff.
139
+ if (chunks[chunks.length - 1] !== null) {
140
+ return [];
141
+ }
142
+ addResult(initialize, 0);
143
+
144
+ // Otherwise, resolve, and exit.
145
+ context.events = resolveAll(resolveAllConstructs, context.events, context);
146
+ return context.events;
147
+ }
148
+
149
+ //
150
+ // Tools.
151
+ //
152
+
153
+ /** @type {TokenizeContext['sliceSerialize']} */
154
+ function sliceSerialize(token, expandTabs) {
155
+ return serializeChunks(sliceStream(token), expandTabs);
156
+ }
157
+
158
+ /** @type {TokenizeContext['sliceStream']} */
159
+ function sliceStream(token) {
160
+ return sliceChunks(chunks, token);
161
+ }
162
+
163
+ /** @type {TokenizeContext['now']} */
164
+ function now() {
165
+ // This is a hot path, so we clone manually instead of `Object.assign({}, point)`
166
+ const {
167
+ _bufferIndex,
168
+ _index,
169
+ line,
170
+ column,
171
+ offset
172
+ } = point;
173
+ return {
174
+ _bufferIndex,
175
+ _index,
176
+ line,
177
+ column,
178
+ offset
179
+ };
180
+ }
181
+
182
+ /** @type {TokenizeContext['defineSkip']} */
183
+ function defineSkip(value) {
184
+ columnStart[value.line] = value.column;
185
+ accountForPotentialSkip();
186
+ }
187
+
188
+ //
189
+ // State management.
190
+ //
191
+
192
+ /**
193
+ * Main loop (note that `_index` and `_bufferIndex` in `point` are modified by
194
+ * `consume`).
195
+ * Here is where we walk through the chunks, which either include strings of
196
+ * several characters, or numerical character codes.
197
+ * The reason to do this in a loop instead of a call is so the stack can
198
+ * drain.
199
+ *
200
+ * @returns {undefined}
201
+ * Nothing.
202
+ */
203
+ function main() {
204
+ while (point._index < chunks.length) {
205
+ const chunk = chunks[point._index];
206
+
207
+ // If we’re in a buffer chunk, loop through it.
208
+ if (typeof chunk === 'string') {
209
+ const chunkIndex = point._index;
210
+ if (point._bufferIndex < 0) {
211
+ point._bufferIndex = 0;
212
+ }
213
+ while (point._index === chunkIndex && point._bufferIndex < chunk.length) {
214
+ go(chunk.charCodeAt(point._bufferIndex));
215
+ }
216
+ } else {
217
+ go(chunk);
218
+ }
219
+ }
220
+ }
221
+
222
+ /**
223
+ * Deal with one code.
224
+ *
225
+ * @param {Code} code
226
+ * Code.
227
+ * @returns {undefined}
228
+ * Nothing.
229
+ */
230
+ function go(code) {
231
+ observeTokenizer?.('transition', point.offset)
232
+ consumed = undefined;
233
+ expectedCode = code;
234
+ state = state(code);
235
+ }
236
+
237
+ /** @type {Effects['consume']} */
238
+ function consume(code) {
239
+ if (markdownLineEnding(code)) {
240
+ point.line++;
241
+ point.column = 1;
242
+ point.offset += code === -3 ? 2 : 1;
243
+ accountForPotentialSkip();
244
+ } else if (code !== -1) {
245
+ point.column++;
246
+ point.offset++;
247
+ }
248
+
249
+ // Not in a string chunk.
250
+ if (point._bufferIndex < 0) {
251
+ point._index++;
252
+ } else {
253
+ point._bufferIndex++;
254
+
255
+ // At end of string chunk.
256
+ if (point._bufferIndex ===
257
+ // Points w/ non-negative `_bufferIndex` reference
258
+ // strings.
259
+ /** @type {string} */
260
+ chunks[point._index].length) {
261
+ point._bufferIndex = -1;
262
+ point._index++;
263
+ }
264
+ }
265
+
266
+ // Expose the previous character.
267
+ context.previous = code;
268
+
269
+ // Mark as consumed.
270
+ consumed = true;
271
+ }
272
+
273
+ /** @type {Effects['enter']} */
274
+ function enter(type, fields) {
275
+ observeTokenizer?.('enter', point.offset, type, stack)
276
+ /** @type {Token} */
277
+ // @ts-expect-error Patch instead of assign required fields to help GC.
278
+ const token = fields || {};
279
+ token.type = type;
280
+ token.start = now();
281
+ context.events.push(['enter', token, context]);
282
+ stack.push(token);
283
+ return token;
284
+ }
285
+
286
+ /** @type {Effects['exit']} */
287
+ function exit(type) {
288
+ const token = stack.pop();
289
+ token.end = now();
290
+ context.events.push(['exit', token, context]);
291
+ return token;
292
+ }
293
+
294
+ /**
295
+ * Use results.
296
+ *
297
+ * @type {ReturnHandle}
298
+ */
299
+ function onsuccessfulconstruct(construct, info) {
300
+ addResult(construct, info.from);
301
+ }
302
+
303
+ /**
304
+ * Discard results.
305
+ *
306
+ * @type {ReturnHandle}
307
+ */
308
+ function onsuccessfulcheck(_, info) {
309
+ info.restore();
310
+ }
311
+
312
+ /**
313
+ * Factory to attempt/check/interrupt.
314
+ *
315
+ * @param {ReturnHandle} onreturn
316
+ * Callback.
317
+ * @param {{interrupt?: boolean | undefined} | undefined} [fields]
318
+ * Fields.
319
+ */
320
+ function constructFactory(onreturn, fields) {
321
+ return hook;
322
+
323
+ /**
324
+ * Handle either an object mapping codes to constructs, a list of
325
+ * constructs, or a single construct.
326
+ *
327
+ * @param {Array<Construct> | ConstructRecord | Construct} constructs
328
+ * Constructs.
329
+ * @param {State} returnState
330
+ * State.
331
+ * @param {State | undefined} [bogusState]
332
+ * State.
333
+ * @returns {State}
334
+ * State.
335
+ */
336
+ function hook(constructs, returnState, bogusState) {
337
+ /** @type {ReadonlyArray<Construct>} */
338
+ let listOfConstructs;
339
+ /** @type {number} */
340
+ let constructIndex;
341
+ /** @type {Construct} */
342
+ let currentConstruct;
343
+ /** @type {Info} */
344
+ let info;
345
+ return Array.isArray(constructs) ? /* c8 ignore next 1 */
346
+ handleListOfConstructs(constructs) : 'tokenize' in constructs ?
347
+ // Looks like a construct.
348
+ handleListOfConstructs([(/** @type {Construct} */constructs)]) : handleMapOfConstructs(constructs);
349
+
350
+ /**
351
+ * Handle a list of construct.
352
+ *
353
+ * @param {ConstructRecord} map
354
+ * Constructs.
355
+ * @returns {State}
356
+ * State.
357
+ */
358
+ function handleMapOfConstructs(map) {
359
+ return start;
360
+
361
+ /** @type {State} */
362
+ function start(code) {
363
+ const left = code !== null && map[code];
364
+ const all = code !== null && map.null;
365
+ const list = [
366
+ // To do: add more extension tests.
367
+ /* c8 ignore next 2 */
368
+ ...(Array.isArray(left) ? left : left ? [left] : []), ...(Array.isArray(all) ? all : all ? [all] : [])];
369
+ return handleListOfConstructs(list)(code);
370
+ }
371
+ }
372
+
373
+ /**
374
+ * Handle a list of construct.
375
+ *
376
+ * @param {ReadonlyArray<Construct>} list
377
+ * Constructs.
378
+ * @returns {State}
379
+ * State.
380
+ */
381
+ function handleListOfConstructs(list) {
382
+ listOfConstructs = list;
383
+ constructIndex = 0;
384
+ if (list.length === 0) {
385
+ return bogusState;
386
+ }
387
+ return handleConstruct(list[constructIndex]);
388
+ }
389
+
390
+ /**
391
+ * Handle a single construct.
392
+ *
393
+ * @param {Construct} construct
394
+ * Construct.
395
+ * @returns {State}
396
+ * State.
397
+ */
398
+ function handleConstruct(construct) {
399
+ return start;
400
+
401
+ /** @type {State} */
402
+ function start(code) {
403
+ // To do: not needed to store if there is no bogus state, probably?
404
+ // Currently doesn’t work because `inspect` in document does a check
405
+ // w/o a bogus, which doesn’t make sense. But it does seem to help perf
406
+ // by not storing.
407
+ info = store();
408
+ currentConstruct = construct;
409
+ if (!construct.partial) {
410
+ context.currentConstruct = construct;
411
+ }
412
+
413
+ // Always populated by defaults.
414
+
415
+ if (construct.name && context.parser.constructs.disable.null.includes(construct.name)) {
416
+ return nok(code);
417
+ }
418
+ return construct.tokenize.call(
419
+ // If we do have fields, create an object w/ `context` as its
420
+ // prototype.
421
+ // This allows a “live binding”, which is needed for `interrupt`.
422
+ fields ? Object.assign(Object.create(context), fields) : context, effects, ok, nok)(code);
423
+ }
424
+ }
425
+
426
+ /** @type {State} */
427
+ function ok(code) {
428
+ consumed = true;
429
+ onreturn(currentConstruct, info);
430
+ return returnState;
431
+ }
432
+
433
+ /** @type {State} */
434
+ function nok(code) {
435
+ consumed = true;
436
+ info.restore();
437
+ if (++constructIndex < listOfConstructs.length) {
438
+ return handleConstruct(listOfConstructs[constructIndex]);
439
+ }
440
+ return bogusState;
441
+ }
442
+ }
443
+ }
444
+
445
+ /**
446
+ * @param {Construct} construct
447
+ * Construct.
448
+ * @param {number} from
449
+ * From.
450
+ * @returns {undefined}
451
+ * Nothing.
452
+ */
453
+ function addResult(construct, from) {
454
+ if (construct.resolveAll && !resolveAllConstructs.includes(construct)) {
455
+ resolveAllConstructs.push(construct);
456
+ }
457
+ if (construct.resolve) {
458
+ splice(context.events, from, context.events.length - from, construct.resolve(context.events.slice(from), context));
459
+ }
460
+ if (construct.resolveTo) {
461
+ context.events = construct.resolveTo(context.events, context);
462
+ }
463
+ }
464
+
465
+ /**
466
+ * Store state.
467
+ *
468
+ * @returns {Info}
469
+ * Info.
470
+ */
471
+ function store() {
472
+ const startPoint = now();
473
+ const startPrevious = context.previous;
474
+ const startCurrentConstruct = context.currentConstruct;
475
+ const startEventsIndex = context.events.length;
476
+ const startStack = Array.from(stack);
477
+ return {
478
+ from: startEventsIndex,
479
+ restore
480
+ };
481
+
482
+ /**
483
+ * Restore state.
484
+ *
485
+ * @returns {undefined}
486
+ * Nothing.
487
+ */
488
+ function restore() {
489
+ point = startPoint;
490
+ context.previous = startPrevious;
491
+ context.currentConstruct = startCurrentConstruct;
492
+ context.events.length = startEventsIndex;
493
+ stack = startStack;
494
+ accountForPotentialSkip();
495
+ }
496
+ }
497
+
498
+ /**
499
+ * Move the current point a bit forward in the line when it’s on a column
500
+ * skip.
501
+ *
502
+ * @returns {undefined}
503
+ * Nothing.
504
+ */
505
+ function accountForPotentialSkip() {
506
+ if (point.line in columnStart && point.column < 2) {
507
+ point.column = columnStart[point.line];
508
+ point.offset += columnStart[point.line] - 1;
509
+ }
510
+ }
511
+ }
512
+
513
+ /**
514
+ * Get the chunks from a slice of chunks in the range of a token.
515
+ *
516
+ * @param {ReadonlyArray<Chunk>} chunks
517
+ * Chunks.
518
+ * @param {Pick<Token, 'end' | 'start'>} token
519
+ * Token.
520
+ * @returns {Array<Chunk>}
521
+ * Chunks.
522
+ */
523
+ function sliceChunks(chunks, token) {
524
+ const startIndex = token.start._index;
525
+ const startBufferIndex = token.start._bufferIndex;
526
+ const endIndex = token.end._index;
527
+ const endBufferIndex = token.end._bufferIndex;
528
+ /** @type {Array<Chunk>} */
529
+ let view;
530
+ if (startIndex === endIndex) {
531
+ // @ts-expect-error `_bufferIndex` is used on string chunks.
532
+ view = [chunks[startIndex].slice(startBufferIndex, endBufferIndex)];
533
+ } else {
534
+ view = chunks.slice(startIndex, endIndex);
535
+ if (startBufferIndex > -1) {
536
+ const head = view[0];
537
+ if (typeof head === 'string') {
538
+ view[0] = head.slice(startBufferIndex);
539
+ /* c8 ignore next 4 -- used to be used, no longer */
540
+ } else {
541
+ view.shift();
542
+ }
543
+ }
544
+ if (endBufferIndex > 0) {
545
+ // @ts-expect-error `_bufferIndex` is used on string chunks.
546
+ view.push(chunks[endIndex].slice(0, endBufferIndex));
547
+ }
548
+ }
549
+ return view;
550
+ }
551
+
552
+ /**
553
+ * Get the string value of a slice of chunks.
554
+ *
555
+ * @param {ReadonlyArray<Chunk>} chunks
556
+ * Chunks.
557
+ * @param {boolean | undefined} [expandTabs=false]
558
+ * Whether to expand tabs (default: `false`).
559
+ * @returns {string}
560
+ * Result.
561
+ */
562
+ function serializeChunks(chunks, expandTabs) {
563
+ let index = -1;
564
+ /** @type {Array<string>} */
565
+ const result = [];
566
+ /** @type {boolean | undefined} */
567
+ let atTab;
568
+ while (++index < chunks.length) {
569
+ const chunk = chunks[index];
570
+ /** @type {string} */
571
+ let value;
572
+ if (typeof chunk === 'string') {
573
+ value = chunk;
574
+ } else {
575
+ switch (chunk) {
576
+ case -5:
577
+ {
578
+ value = "\r";
579
+ break;
580
+ }
581
+ case -4:
582
+ {
583
+ value = "\n";
584
+ break;
585
+ }
586
+ case -3:
587
+ {
588
+ value = "\r" + "\n";
589
+ break;
590
+ }
591
+ case -2:
592
+ {
593
+ value = expandTabs ? " " : "\t";
594
+ break;
595
+ }
596
+ case -1:
597
+ {
598
+ if (!expandTabs && atTab) {
599
+ continue;
600
+ }
601
+ value = " ";
602
+ break;
603
+ }
604
+ default:
605
+ {
606
+ // Currently only replacement character.
607
+ value = String.fromCharCode(chunk);
608
+ }
609
+ }
610
+ }
611
+ atTab = chunk === -2;
612
+ result.push(value);
613
+ }
614
+ return result.join('');
615
+ }