yaml 2.0.0-2 → 2.0.0-6

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 (347) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +46 -26
  3. package/browser/dist/compose/compose-collection.js +59 -0
  4. package/browser/dist/compose/compose-doc.js +39 -0
  5. package/browser/dist/compose/compose-node.js +72 -0
  6. package/browser/dist/compose/compose-scalar.js +74 -0
  7. package/browser/dist/compose/composer.js +218 -0
  8. package/browser/dist/compose/resolve-block-map.js +93 -0
  9. package/browser/dist/compose/resolve-block-scalar.js +185 -0
  10. package/browser/dist/compose/resolve-block-seq.js +40 -0
  11. package/browser/dist/compose/resolve-end.js +37 -0
  12. package/browser/dist/compose/resolve-flow-collection.js +189 -0
  13. package/browser/dist/compose/resolve-flow-scalar.js +211 -0
  14. package/browser/dist/compose/resolve-props.js +126 -0
  15. package/browser/dist/compose/util-contains-newline.js +34 -0
  16. package/browser/dist/compose/util-empty-scalar-position.js +27 -0
  17. package/browser/dist/compose/util-map-includes.js +17 -0
  18. package/browser/dist/doc/Document.js +251 -348
  19. package/browser/dist/doc/anchors.js +72 -0
  20. package/browser/dist/doc/applyReviver.js +41 -28
  21. package/browser/dist/doc/createNode.js +71 -55
  22. package/browser/dist/doc/directives.js +161 -0
  23. package/browser/dist/errors.js +50 -72
  24. package/browser/dist/index.js +17 -73
  25. package/browser/dist/log.js +11 -7
  26. package/browser/dist/nodes/Alias.js +94 -0
  27. package/browser/dist/nodes/Collection.js +133 -0
  28. package/browser/dist/nodes/Node.js +41 -0
  29. package/browser/dist/nodes/Pair.js +28 -0
  30. package/browser/dist/nodes/Scalar.js +23 -0
  31. package/browser/dist/nodes/YAMLMap.js +116 -0
  32. package/browser/dist/nodes/YAMLSeq.js +105 -0
  33. package/browser/dist/nodes/addPairToJSMap.js +103 -0
  34. package/browser/dist/nodes/toJS.js +35 -0
  35. package/browser/dist/options.js +14 -95
  36. package/browser/dist/parse/cst-scalar.js +219 -0
  37. package/browser/dist/parse/cst-stringify.js +61 -0
  38. package/browser/dist/parse/cst-visit.js +97 -0
  39. package/browser/dist/parse/cst.js +98 -0
  40. package/browser/dist/parse/lexer.js +656 -0
  41. package/browser/dist/parse/line-counter.js +39 -0
  42. package/browser/dist/parse/parser.js +876 -0
  43. package/browser/dist/public-api.js +101 -0
  44. package/browser/dist/schema/Schema.js +23 -0
  45. package/browser/dist/schema/common/map.js +42 -0
  46. package/browser/dist/schema/common/null.js +13 -0
  47. package/browser/dist/schema/common/seq.js +33 -0
  48. package/browser/dist/schema/common/string.js +14 -0
  49. package/browser/dist/schema/core/bool.js +19 -0
  50. package/browser/dist/schema/core/float.js +40 -0
  51. package/browser/dist/schema/core/int.js +38 -0
  52. package/browser/dist/schema/core/schema.js +23 -0
  53. package/browser/dist/schema/json/schema.js +62 -0
  54. package/browser/dist/schema/tags.js +79 -0
  55. package/browser/dist/schema/yaml-1.1/binary.js +66 -0
  56. package/browser/dist/schema/yaml-1.1/bool.js +26 -0
  57. package/browser/dist/schema/yaml-1.1/float.js +43 -0
  58. package/browser/dist/schema/yaml-1.1/int.js +71 -0
  59. package/browser/dist/schema/yaml-1.1/omap.js +73 -0
  60. package/browser/dist/schema/yaml-1.1/pairs.js +77 -0
  61. package/browser/dist/schema/yaml-1.1/schema.js +37 -0
  62. package/browser/dist/schema/yaml-1.1/set.js +87 -0
  63. package/browser/dist/schema/yaml-1.1/timestamp.js +101 -0
  64. package/browser/dist/stringify/foldFlowLines.js +121 -139
  65. package/browser/dist/stringify/stringify.js +87 -72
  66. package/browser/dist/stringify/stringifyCollection.js +122 -0
  67. package/browser/dist/stringify/stringifyComment.js +14 -0
  68. package/browser/dist/stringify/stringifyDocument.js +64 -0
  69. package/browser/dist/stringify/stringifyNumber.js +20 -23
  70. package/browser/dist/stringify/stringifyPair.js +111 -0
  71. package/browser/dist/stringify/stringifyString.js +295 -322
  72. package/browser/dist/util.js +4 -4
  73. package/browser/dist/visit.js +146 -0
  74. package/browser/index.js +4 -1
  75. package/dist/compose/compose-collection.d.ts +5 -0
  76. package/dist/compose/compose-collection.js +61 -0
  77. package/dist/compose/compose-doc.d.ts +6 -0
  78. package/dist/compose/compose-doc.js +41 -0
  79. package/dist/compose/compose-node.d.ts +25 -0
  80. package/dist/compose/compose-node.js +75 -0
  81. package/dist/compose/compose-scalar.d.ts +5 -0
  82. package/dist/compose/compose-scalar.js +76 -0
  83. package/dist/compose/composer.d.ts +62 -0
  84. package/dist/compose/composer.js +222 -0
  85. package/dist/compose/resolve-block-map.d.ts +6 -0
  86. package/dist/compose/resolve-block-map.js +95 -0
  87. package/dist/compose/resolve-block-scalar.d.ts +10 -0
  88. package/dist/compose/resolve-block-scalar.js +187 -0
  89. package/dist/compose/resolve-block-seq.d.ts +5 -0
  90. package/dist/compose/resolve-block-seq.js +42 -0
  91. package/dist/compose/resolve-end.d.ts +6 -0
  92. package/dist/compose/resolve-end.js +39 -0
  93. package/dist/compose/resolve-flow-collection.d.ts +6 -0
  94. package/dist/compose/resolve-flow-collection.js +191 -0
  95. package/dist/compose/resolve-flow-scalar.d.ts +10 -0
  96. package/dist/compose/resolve-flow-scalar.js +213 -0
  97. package/dist/compose/resolve-props.d.ts +21 -0
  98. package/dist/compose/resolve-props.js +128 -0
  99. package/dist/compose/util-contains-newline.d.ts +2 -0
  100. package/dist/compose/util-contains-newline.js +36 -0
  101. package/dist/compose/util-empty-scalar-position.d.ts +2 -0
  102. package/dist/compose/util-empty-scalar-position.js +29 -0
  103. package/dist/compose/util-map-includes.d.ts +4 -0
  104. package/dist/compose/util-map-includes.js +19 -0
  105. package/dist/doc/Document.d.ts +130 -0
  106. package/dist/doc/Document.js +250 -347
  107. package/dist/doc/anchors.d.ts +24 -0
  108. package/dist/doc/anchors.js +77 -0
  109. package/dist/doc/applyReviver.d.ts +9 -0
  110. package/dist/doc/applyReviver.js +41 -28
  111. package/dist/doc/createNode.d.ts +16 -0
  112. package/dist/doc/createNode.js +71 -55
  113. package/dist/doc/directives.d.ts +46 -0
  114. package/dist/doc/directives.js +163 -0
  115. package/dist/errors.d.ts +21 -0
  116. package/dist/errors.js +51 -74
  117. package/dist/index.d.ts +19 -0
  118. package/dist/index.js +40 -71
  119. package/dist/log.d.ts +3 -0
  120. package/dist/log.js +11 -6
  121. package/dist/nodes/Alias.d.ts +24 -0
  122. package/dist/nodes/Alias.js +96 -0
  123. package/dist/nodes/Collection.d.ts +67 -0
  124. package/dist/nodes/Collection.js +137 -0
  125. package/dist/nodes/Node.d.ts +48 -0
  126. package/dist/nodes/Node.js +59 -0
  127. package/dist/nodes/Pair.d.ts +16 -0
  128. package/dist/nodes/Pair.js +31 -0
  129. package/dist/nodes/Scalar.d.ts +40 -0
  130. package/dist/nodes/Scalar.js +26 -0
  131. package/dist/nodes/YAMLMap.d.ts +39 -0
  132. package/dist/nodes/YAMLMap.js +119 -0
  133. package/dist/nodes/YAMLSeq.d.ts +53 -0
  134. package/dist/nodes/YAMLSeq.js +107 -0
  135. package/dist/nodes/addPairToJSMap.d.ts +3 -0
  136. package/dist/nodes/addPairToJSMap.js +105 -0
  137. package/dist/nodes/toJS.d.ts +30 -0
  138. package/dist/nodes/toJS.js +37 -0
  139. package/dist/options.d.ts +278 -0
  140. package/dist/options.js +13 -96
  141. package/dist/parse/cst-scalar.d.ts +58 -0
  142. package/dist/parse/cst-scalar.js +223 -0
  143. package/dist/parse/cst-stringify.d.ts +8 -0
  144. package/dist/parse/cst-stringify.js +63 -0
  145. package/dist/parse/cst-visit.d.ts +39 -0
  146. package/dist/parse/cst-visit.js +99 -0
  147. package/dist/parse/cst.d.ts +106 -0
  148. package/dist/parse/cst.js +112 -0
  149. package/dist/parse/lexer.d.ts +85 -0
  150. package/dist/parse/lexer.js +658 -0
  151. package/dist/parse/line-counter.d.ts +22 -0
  152. package/dist/parse/line-counter.js +41 -0
  153. package/dist/parse/parser.d.ts +84 -0
  154. package/dist/parse/parser.js +880 -0
  155. package/dist/public-api.d.ts +43 -0
  156. package/dist/public-api.js +106 -0
  157. package/dist/schema/Schema.d.ts +16 -0
  158. package/dist/schema/Schema.js +25 -0
  159. package/dist/schema/common/map.d.ts +2 -0
  160. package/dist/schema/common/map.js +44 -0
  161. package/dist/schema/common/null.d.ts +4 -0
  162. package/dist/schema/common/null.js +15 -0
  163. package/dist/schema/common/seq.d.ts +2 -0
  164. package/dist/schema/common/seq.js +35 -0
  165. package/dist/schema/common/string.d.ts +2 -0
  166. package/dist/schema/common/string.js +16 -0
  167. package/dist/schema/core/bool.d.ts +4 -0
  168. package/dist/schema/core/bool.js +21 -0
  169. package/dist/schema/core/float.d.ts +4 -0
  170. package/dist/schema/core/float.js +44 -0
  171. package/dist/schema/core/int.d.ts +4 -0
  172. package/dist/schema/core/int.js +42 -0
  173. package/dist/schema/core/schema.d.ts +1 -0
  174. package/dist/schema/core/schema.js +25 -0
  175. package/dist/schema/json/schema.d.ts +2 -0
  176. package/dist/schema/json/schema.js +64 -0
  177. package/dist/schema/tags.d.ts +41 -0
  178. package/dist/schema/tags.js +82 -0
  179. package/dist/schema/types.d.ts +82 -0
  180. package/dist/schema/yaml-1.1/binary.d.ts +2 -0
  181. package/dist/schema/yaml-1.1/binary.js +68 -0
  182. package/dist/schema/yaml-1.1/bool.d.ts +7 -0
  183. package/dist/schema/yaml-1.1/bool.js +29 -0
  184. package/dist/schema/yaml-1.1/float.d.ts +4 -0
  185. package/dist/schema/yaml-1.1/float.js +47 -0
  186. package/dist/schema/yaml-1.1/int.d.ts +5 -0
  187. package/dist/schema/yaml-1.1/int.js +76 -0
  188. package/dist/schema/yaml-1.1/omap.d.ts +21 -0
  189. package/dist/schema/yaml-1.1/omap.js +76 -0
  190. package/dist/schema/yaml-1.1/pairs.d.ts +10 -0
  191. package/dist/schema/yaml-1.1/pairs.js +81 -0
  192. package/dist/schema/yaml-1.1/schema.d.ts +1 -0
  193. package/dist/schema/yaml-1.1/schema.js +39 -0
  194. package/dist/schema/yaml-1.1/set.d.ts +22 -0
  195. package/dist/schema/yaml-1.1/set.js +90 -0
  196. package/dist/schema/yaml-1.1/timestamp.d.ts +6 -0
  197. package/dist/schema/yaml-1.1/timestamp.js +105 -0
  198. package/dist/stringify/foldFlowLines.d.ts +34 -0
  199. package/dist/stringify/foldFlowLines.js +121 -139
  200. package/dist/stringify/stringify.d.ts +18 -0
  201. package/dist/stringify/stringify.js +87 -71
  202. package/dist/stringify/stringifyCollection.d.ts +21 -0
  203. package/dist/stringify/stringifyCollection.js +124 -0
  204. package/dist/stringify/stringifyComment.d.ts +2 -0
  205. package/dist/stringify/stringifyComment.js +17 -0
  206. package/dist/stringify/stringifyDocument.d.ts +3 -0
  207. package/dist/stringify/stringifyDocument.js +66 -0
  208. package/dist/stringify/stringifyNumber.d.ts +2 -0
  209. package/dist/stringify/stringifyNumber.js +20 -23
  210. package/dist/stringify/stringifyPair.d.ts +3 -0
  211. package/dist/stringify/stringifyPair.js +113 -0
  212. package/dist/stringify/stringifyString.d.ts +3 -0
  213. package/dist/stringify/stringifyString.js +295 -322
  214. package/dist/test-events.d.ts +4 -0
  215. package/dist/test-events.js +124 -148
  216. package/dist/util.d.ts +6 -0
  217. package/dist/util.js +9 -12
  218. package/dist/visit.d.ts +63 -0
  219. package/dist/visit.js +148 -0
  220. package/package.json +31 -39
  221. package/util.js +2 -14
  222. package/browser/dist/_virtual/_rollupPluginBabelHelpers.js +0 -16
  223. package/browser/dist/ast/Alias.js +0 -95
  224. package/browser/dist/ast/Collection.js +0 -192
  225. package/browser/dist/ast/Merge.js +0 -71
  226. package/browser/dist/ast/Node.js +0 -3
  227. package/browser/dist/ast/Pair.js +0 -200
  228. package/browser/dist/ast/Scalar.js +0 -21
  229. package/browser/dist/ast/YAMLMap.js +0 -91
  230. package/browser/dist/ast/YAMLSeq.js +0 -67
  231. package/browser/dist/ast/toJS.js +0 -29
  232. package/browser/dist/ast/visit.js +0 -83
  233. package/browser/dist/constants.js +0 -40
  234. package/browser/dist/cst/Alias.js +0 -26
  235. package/browser/dist/cst/BlankLine.js +0 -34
  236. package/browser/dist/cst/BlockValue.js +0 -240
  237. package/browser/dist/cst/Collection.js +0 -259
  238. package/browser/dist/cst/CollectionItem.js +0 -123
  239. package/browser/dist/cst/Comment.js +0 -27
  240. package/browser/dist/cst/Directive.js +0 -53
  241. package/browser/dist/cst/Document.js +0 -262
  242. package/browser/dist/cst/FlowCollection.js +0 -201
  243. package/browser/dist/cst/Node.js +0 -355
  244. package/browser/dist/cst/ParseContext.js +0 -234
  245. package/browser/dist/cst/PlainValue.js +0 -171
  246. package/browser/dist/cst/QuoteDouble.js +0 -224
  247. package/browser/dist/cst/QuoteSingle.js +0 -100
  248. package/browser/dist/cst/Range.js +0 -56
  249. package/browser/dist/cst/parse.js +0 -46
  250. package/browser/dist/cst/source-utils.js +0 -169
  251. package/browser/dist/doc/Anchors.js +0 -115
  252. package/browser/dist/doc/Schema.js +0 -34
  253. package/browser/dist/doc/getSchemaTags.js +0 -33
  254. package/browser/dist/doc/listTagNames.js +0 -27
  255. package/browser/dist/doc/parseContents.js +0 -60
  256. package/browser/dist/doc/parseDirectives.js +0 -107
  257. package/browser/dist/resolve/collection-utils.js +0 -86
  258. package/browser/dist/resolve/resolveMap.js +0 -315
  259. package/browser/dist/resolve/resolveNode.js +0 -173
  260. package/browser/dist/resolve/resolveScalar.js +0 -20
  261. package/browser/dist/resolve/resolveSeq.js +0 -170
  262. package/browser/dist/resolve/resolveTag.js +0 -107
  263. package/browser/dist/resolve/resolveTagName.js +0 -87
  264. package/browser/dist/stringify/addComment.js +0 -10
  265. package/browser/dist/stringify/stringifyTag.js +0 -28
  266. package/browser/dist/tags/core.js +0 -128
  267. package/browser/dist/tags/failsafe/index.js +0 -7
  268. package/browser/dist/tags/failsafe/map.js +0 -37
  269. package/browser/dist/tags/failsafe/seq.js +0 -34
  270. package/browser/dist/tags/failsafe/string.js +0 -20
  271. package/browser/dist/tags/index.js +0 -39
  272. package/browser/dist/tags/json.js +0 -62
  273. package/browser/dist/tags/options.js +0 -31
  274. package/browser/dist/tags/yaml-1.1/binary.js +0 -83
  275. package/browser/dist/tags/yaml-1.1/index.js +0 -174
  276. package/browser/dist/tags/yaml-1.1/omap.js +0 -86
  277. package/browser/dist/tags/yaml-1.1/pairs.js +0 -62
  278. package/browser/dist/tags/yaml-1.1/set.js +0 -78
  279. package/browser/dist/tags/yaml-1.1/timestamp.js +0 -101
  280. package/browser/dist/types.js +0 -10
  281. package/cst.d.ts +0 -185
  282. package/dist/_virtual/_rollupPluginBabelHelpers.js +0 -18
  283. package/dist/ast/Alias.js +0 -97
  284. package/dist/ast/Collection.js +0 -196
  285. package/dist/ast/Merge.js +0 -74
  286. package/dist/ast/Node.js +0 -5
  287. package/dist/ast/Pair.js +0 -203
  288. package/dist/ast/Scalar.js +0 -24
  289. package/dist/ast/YAMLMap.js +0 -94
  290. package/dist/ast/YAMLSeq.js +0 -69
  291. package/dist/ast/toJS.js +0 -31
  292. package/dist/ast/visit.js +0 -85
  293. package/dist/constants.js +0 -46
  294. package/dist/cst/Alias.js +0 -28
  295. package/dist/cst/BlankLine.js +0 -36
  296. package/dist/cst/BlockValue.js +0 -243
  297. package/dist/cst/Collection.js +0 -262
  298. package/dist/cst/CollectionItem.js +0 -125
  299. package/dist/cst/Comment.js +0 -29
  300. package/dist/cst/Directive.js +0 -55
  301. package/dist/cst/Document.js +0 -264
  302. package/dist/cst/FlowCollection.js +0 -203
  303. package/dist/cst/Node.js +0 -357
  304. package/dist/cst/ParseContext.js +0 -236
  305. package/dist/cst/PlainValue.js +0 -173
  306. package/dist/cst/QuoteDouble.js +0 -226
  307. package/dist/cst/QuoteSingle.js +0 -102
  308. package/dist/cst/Range.js +0 -58
  309. package/dist/cst/parse.js +0 -48
  310. package/dist/cst/source-utils.js +0 -173
  311. package/dist/doc/Anchors.js +0 -117
  312. package/dist/doc/Schema.js +0 -36
  313. package/dist/doc/getSchemaTags.js +0 -35
  314. package/dist/doc/listTagNames.js +0 -29
  315. package/dist/doc/parseContents.js +0 -62
  316. package/dist/doc/parseDirectives.js +0 -109
  317. package/dist/resolve/collection-utils.js +0 -91
  318. package/dist/resolve/resolveMap.js +0 -317
  319. package/dist/resolve/resolveNode.js +0 -175
  320. package/dist/resolve/resolveScalar.js +0 -22
  321. package/dist/resolve/resolveSeq.js +0 -172
  322. package/dist/resolve/resolveTag.js +0 -109
  323. package/dist/resolve/resolveTagName.js +0 -89
  324. package/dist/stringify/addComment.js +0 -13
  325. package/dist/stringify/stringifyTag.js +0 -30
  326. package/dist/tags/core.js +0 -138
  327. package/dist/tags/failsafe/index.js +0 -9
  328. package/dist/tags/failsafe/map.js +0 -39
  329. package/dist/tags/failsafe/seq.js +0 -36
  330. package/dist/tags/failsafe/string.js +0 -22
  331. package/dist/tags/index.js +0 -42
  332. package/dist/tags/json.js +0 -64
  333. package/dist/tags/options.js +0 -37
  334. package/dist/tags/yaml-1.1/binary.js +0 -85
  335. package/dist/tags/yaml-1.1/index.js +0 -176
  336. package/dist/tags/yaml-1.1/omap.js +0 -89
  337. package/dist/tags/yaml-1.1/pairs.js +0 -66
  338. package/dist/tags/yaml-1.1/set.js +0 -81
  339. package/dist/tags/yaml-1.1/timestamp.js +0 -105
  340. package/dist/types.js +0 -29
  341. package/index.d.ts +0 -519
  342. package/index.js +0 -1
  343. package/types.d.ts +0 -379
  344. package/types.js +0 -17
  345. package/types.mjs +0 -17
  346. package/util.d.ts +0 -76
  347. package/util.mjs +0 -15
@@ -0,0 +1,63 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Stringify a CST document, token, or collection item
5
+ *
6
+ * Fair warning: This applies no validation whatsoever, and
7
+ * simply concatenates the sources in their logical order.
8
+ */
9
+ const stringify = (cst) => 'type' in cst ? stringifyToken(cst) : stringifyItem(cst);
10
+ function stringifyToken(token) {
11
+ switch (token.type) {
12
+ case 'block-scalar': {
13
+ let res = '';
14
+ for (const tok of token.props)
15
+ res += stringifyToken(tok);
16
+ return res + token.source;
17
+ }
18
+ case 'block-map':
19
+ case 'block-seq': {
20
+ let res = '';
21
+ for (const item of token.items)
22
+ res += stringifyItem(item);
23
+ return res;
24
+ }
25
+ case 'flow-collection': {
26
+ let res = token.start.source;
27
+ for (const item of token.items)
28
+ res += stringifyItem(item);
29
+ for (const st of token.end)
30
+ res += st.source;
31
+ return res;
32
+ }
33
+ case 'document': {
34
+ let res = stringifyItem(token);
35
+ if (token.end)
36
+ for (const st of token.end)
37
+ res += st.source;
38
+ return res;
39
+ }
40
+ default: {
41
+ let res = token.source;
42
+ if ('end' in token && token.end)
43
+ for (const st of token.end)
44
+ res += st.source;
45
+ return res;
46
+ }
47
+ }
48
+ }
49
+ function stringifyItem({ start, key, sep, value }) {
50
+ let res = '';
51
+ for (const st of start)
52
+ res += st.source;
53
+ if (key)
54
+ res += stringifyToken(key);
55
+ if (sep)
56
+ for (const st of sep)
57
+ res += st.source;
58
+ if (value)
59
+ res += stringifyToken(value);
60
+ return res;
61
+ }
62
+
63
+ exports.stringify = stringify;
@@ -0,0 +1,39 @@
1
+ import type { CollectionItem, Document } from './cst.js';
2
+ export declare type VisitPath = readonly ['key' | 'value', number][];
3
+ export declare type Visitor = (item: CollectionItem, path: VisitPath) => number | symbol | Visitor | void;
4
+ /**
5
+ * Apply a visitor to a CST document or item.
6
+ *
7
+ * Walks through the tree (depth-first) starting from the root, calling a
8
+ * `visitor` function with two arguments when entering each item:
9
+ * - `item`: The current item, which included the following members:
10
+ * - `start: SourceToken[]` – Source tokens before the key or value,
11
+ * possibly including its anchor or tag.
12
+ * - `key?: Token | null` – Set for pair values. May then be `null`, if
13
+ * the key before the `:` separator is empty.
14
+ * - `sep?: SourceToken[]` – Source tokens between the key and the value,
15
+ * which should include the `:` map value indicator if `value` is set.
16
+ * - `value?: Token` – The value of a sequence item, or of a map pair.
17
+ * - `path`: The steps from the root to the current node, as an array of
18
+ * `['key' | 'value', number]` tuples.
19
+ *
20
+ * The return value of the visitor may be used to control the traversal:
21
+ * - `undefined` (default): Do nothing and continue
22
+ * - `visit.SKIP`: Do not visit the children of this token, continue with
23
+ * next sibling
24
+ * - `visit.BREAK`: Terminate traversal completely
25
+ * - `visit.REMOVE`: Remove the current item, then continue with the next one
26
+ * - `number`: Set the index of the next step. This is useful especially if
27
+ * the index of the current token has changed.
28
+ * - `function`: Define the next visitor for this item. After the original
29
+ * visitor is called on item entry, next visitors are called after handling
30
+ * a non-empty `key` and when exiting the item.
31
+ */
32
+ export declare function visit(cst: Document | CollectionItem, visitor: Visitor): void;
33
+ export declare namespace visit {
34
+ var BREAK: symbol;
35
+ var SKIP: symbol;
36
+ var REMOVE: symbol;
37
+ var itemAtPath: (cst: Document | CollectionItem, path: VisitPath) => CollectionItem | undefined;
38
+ var parentCollection: (cst: Document | CollectionItem, path: VisitPath) => import("./cst.js").BlockMap | import("./cst.js").BlockSequence | import("./cst.js").FlowCollection;
39
+ }
@@ -0,0 +1,99 @@
1
+ 'use strict';
2
+
3
+ const BREAK = Symbol('break visit');
4
+ const SKIP = Symbol('skip children');
5
+ const REMOVE = Symbol('remove item');
6
+ /**
7
+ * Apply a visitor to a CST document or item.
8
+ *
9
+ * Walks through the tree (depth-first) starting from the root, calling a
10
+ * `visitor` function with two arguments when entering each item:
11
+ * - `item`: The current item, which included the following members:
12
+ * - `start: SourceToken[]` – Source tokens before the key or value,
13
+ * possibly including its anchor or tag.
14
+ * - `key?: Token | null` – Set for pair values. May then be `null`, if
15
+ * the key before the `:` separator is empty.
16
+ * - `sep?: SourceToken[]` – Source tokens between the key and the value,
17
+ * which should include the `:` map value indicator if `value` is set.
18
+ * - `value?: Token` – The value of a sequence item, or of a map pair.
19
+ * - `path`: The steps from the root to the current node, as an array of
20
+ * `['key' | 'value', number]` tuples.
21
+ *
22
+ * The return value of the visitor may be used to control the traversal:
23
+ * - `undefined` (default): Do nothing and continue
24
+ * - `visit.SKIP`: Do not visit the children of this token, continue with
25
+ * next sibling
26
+ * - `visit.BREAK`: Terminate traversal completely
27
+ * - `visit.REMOVE`: Remove the current item, then continue with the next one
28
+ * - `number`: Set the index of the next step. This is useful especially if
29
+ * the index of the current token has changed.
30
+ * - `function`: Define the next visitor for this item. After the original
31
+ * visitor is called on item entry, next visitors are called after handling
32
+ * a non-empty `key` and when exiting the item.
33
+ */
34
+ function visit(cst, visitor) {
35
+ if ('type' in cst && cst.type === 'document')
36
+ cst = { start: cst.start, value: cst.value };
37
+ _visit(Object.freeze([]), cst, visitor);
38
+ }
39
+ // Without the `as symbol` casts, TS declares these in the `visit`
40
+ // namespace using `var`, but then complains about that because
41
+ // `unique symbol` must be `const`.
42
+ /** Terminate visit traversal completely */
43
+ visit.BREAK = BREAK;
44
+ /** Do not visit the children of the current item */
45
+ visit.SKIP = SKIP;
46
+ /** Remove the current item */
47
+ visit.REMOVE = REMOVE;
48
+ /** Find the item at `path` from `cst` as the root */
49
+ visit.itemAtPath = (cst, path) => {
50
+ let item = cst;
51
+ for (const [field, index] of path) {
52
+ const tok = item && item[field];
53
+ if (tok && 'items' in tok) {
54
+ item = tok.items[index];
55
+ }
56
+ else
57
+ return undefined;
58
+ }
59
+ return item;
60
+ };
61
+ /**
62
+ * Get the immediate parent collection of the item at `path` from `cst` as the root.
63
+ *
64
+ * Throws an error if the collection is not found, which should never happen if the item itself exists.
65
+ */
66
+ visit.parentCollection = (cst, path) => {
67
+ const parent = visit.itemAtPath(cst, path.slice(0, -1));
68
+ const field = path[path.length - 1][0];
69
+ const coll = parent && parent[field];
70
+ if (coll && 'items' in coll)
71
+ return coll;
72
+ throw new Error('Parent collection not found');
73
+ };
74
+ function _visit(path, item, visitor) {
75
+ let ctrl = visitor(item, path);
76
+ if (typeof ctrl === 'symbol')
77
+ return ctrl;
78
+ for (const field of ['key', 'value']) {
79
+ const token = item[field];
80
+ if (token && 'items' in token) {
81
+ for (let i = 0; i < token.items.length; ++i) {
82
+ const ci = _visit(Object.freeze(path.concat([[field, i]])), token.items[i], visitor);
83
+ if (typeof ci === 'number')
84
+ i = ci - 1;
85
+ else if (ci === BREAK)
86
+ return BREAK;
87
+ else if (ci === REMOVE) {
88
+ token.items.splice(i, 1);
89
+ i -= 1;
90
+ }
91
+ }
92
+ if (typeof ctrl === 'function' && field === 'key')
93
+ ctrl = ctrl(item, path);
94
+ }
95
+ }
96
+ return typeof ctrl === 'function' ? ctrl(item, path) : ctrl;
97
+ }
98
+
99
+ exports.visit = visit;
@@ -0,0 +1,106 @@
1
+ export { createScalarToken, resolveAsScalar, setScalarValue } from './cst-scalar.js';
2
+ export { stringify } from './cst-stringify.js';
3
+ export { visit, Visitor, VisitPath } from './cst-visit.js';
4
+ export interface SourceToken {
5
+ type: 'byte-order-mark' | 'doc-mode' | 'doc-start' | 'space' | 'comment' | 'newline' | 'directive-line' | 'anchor' | 'tag' | 'seq-item-ind' | 'explicit-key-ind' | 'map-value-ind' | 'flow-map-start' | 'flow-map-end' | 'flow-seq-start' | 'flow-seq-end' | 'flow-error-end' | 'comma' | 'block-scalar-header';
6
+ offset: number;
7
+ indent: number;
8
+ source: string;
9
+ }
10
+ export interface ErrorToken {
11
+ type: 'error';
12
+ offset: number;
13
+ source: string;
14
+ message: string;
15
+ }
16
+ export interface Directive {
17
+ type: 'directive';
18
+ offset: number;
19
+ source: string;
20
+ }
21
+ export interface Document {
22
+ type: 'document';
23
+ offset: number;
24
+ start: SourceToken[];
25
+ value?: Token;
26
+ end?: SourceToken[];
27
+ }
28
+ export interface DocumentEnd {
29
+ type: 'doc-end';
30
+ offset: number;
31
+ source: string;
32
+ end?: SourceToken[];
33
+ }
34
+ export interface FlowScalar {
35
+ type: 'alias' | 'scalar' | 'single-quoted-scalar' | 'double-quoted-scalar';
36
+ offset: number;
37
+ indent: number;
38
+ source: string;
39
+ end?: SourceToken[];
40
+ }
41
+ export interface BlockScalar {
42
+ type: 'block-scalar';
43
+ offset: number;
44
+ indent: number;
45
+ props: Token[];
46
+ source: string;
47
+ }
48
+ export interface BlockMap {
49
+ type: 'block-map';
50
+ offset: number;
51
+ indent: number;
52
+ items: Array<{
53
+ start: SourceToken[];
54
+ key?: never;
55
+ sep?: never;
56
+ value?: never;
57
+ } | {
58
+ start: SourceToken[];
59
+ key: Token | null;
60
+ sep: SourceToken[];
61
+ value?: Token;
62
+ }>;
63
+ }
64
+ export interface BlockSequence {
65
+ type: 'block-seq';
66
+ offset: number;
67
+ indent: number;
68
+ items: Array<{
69
+ start: SourceToken[];
70
+ key?: never;
71
+ sep?: never;
72
+ value?: Token;
73
+ }>;
74
+ }
75
+ export declare type CollectionItem = {
76
+ start: SourceToken[];
77
+ key?: Token | null;
78
+ sep?: SourceToken[];
79
+ value?: Token;
80
+ };
81
+ export interface FlowCollection {
82
+ type: 'flow-collection';
83
+ offset: number;
84
+ indent: number;
85
+ start: SourceToken;
86
+ items: CollectionItem[];
87
+ end: SourceToken[];
88
+ }
89
+ export declare type Token = SourceToken | ErrorToken | Directive | Document | DocumentEnd | FlowScalar | BlockScalar | BlockMap | BlockSequence | FlowCollection;
90
+ export declare type TokenType = SourceToken['type'] | DocumentEnd['type'] | FlowScalar['type'];
91
+ /** The byte order mark */
92
+ export declare const BOM = "\uFEFF";
93
+ /** Start of doc-mode */
94
+ export declare const DOCUMENT = "\u0002";
95
+ /** Unexpected end of flow-mode */
96
+ export declare const FLOW_END = "\u0018";
97
+ /** Next token is a scalar value */
98
+ export declare const SCALAR = "\u001F";
99
+ /** @returns `true` if `token` is a flow or block collection */
100
+ export declare const isCollection: (token: Token | null | undefined) => token is BlockMap | BlockSequence | FlowCollection;
101
+ /** @returns `true` if `token` is a flow or block scalar; not an alias */
102
+ export declare const isScalar: (token: Token | null | undefined) => token is BlockScalar | FlowScalar;
103
+ /** Get a printable representation of a lexer token */
104
+ export declare function prettyToken(token: string): string;
105
+ /** Identify the type of a lexer token. May return `null` for unknown tokens. */
106
+ export declare function tokenType(source: string): TokenType | null;
@@ -0,0 +1,112 @@
1
+ 'use strict';
2
+
3
+ var cstScalar = require('./cst-scalar.js');
4
+ var cstStringify = require('./cst-stringify.js');
5
+ var cstVisit = require('./cst-visit.js');
6
+
7
+ /** The byte order mark */
8
+ const BOM = '\u{FEFF}';
9
+ /** Start of doc-mode */
10
+ const DOCUMENT = '\x02'; // C0: Start of Text
11
+ /** Unexpected end of flow-mode */
12
+ const FLOW_END = '\x18'; // C0: Cancel
13
+ /** Next token is a scalar value */
14
+ const SCALAR = '\x1f'; // C0: Unit Separator
15
+ /** @returns `true` if `token` is a flow or block collection */
16
+ const isCollection = (token) => !!token && 'items' in token;
17
+ /** @returns `true` if `token` is a flow or block scalar; not an alias */
18
+ const isScalar = (token) => !!token &&
19
+ (token.type === 'scalar' ||
20
+ token.type === 'single-quoted-scalar' ||
21
+ token.type === 'double-quoted-scalar' ||
22
+ token.type === 'block-scalar');
23
+ /* istanbul ignore next */
24
+ /** Get a printable representation of a lexer token */
25
+ function prettyToken(token) {
26
+ switch (token) {
27
+ case BOM:
28
+ return '<BOM>';
29
+ case DOCUMENT:
30
+ return '<DOC>';
31
+ case FLOW_END:
32
+ return '<FLOW_END>';
33
+ case SCALAR:
34
+ return '<SCALAR>';
35
+ default:
36
+ return JSON.stringify(token);
37
+ }
38
+ }
39
+ /** Identify the type of a lexer token. May return `null` for unknown tokens. */
40
+ function tokenType(source) {
41
+ switch (source) {
42
+ case BOM:
43
+ return 'byte-order-mark';
44
+ case DOCUMENT:
45
+ return 'doc-mode';
46
+ case FLOW_END:
47
+ return 'flow-error-end';
48
+ case SCALAR:
49
+ return 'scalar';
50
+ case '---':
51
+ return 'doc-start';
52
+ case '...':
53
+ return 'doc-end';
54
+ case '':
55
+ case '\n':
56
+ case '\r\n':
57
+ return 'newline';
58
+ case '-':
59
+ return 'seq-item-ind';
60
+ case '?':
61
+ return 'explicit-key-ind';
62
+ case ':':
63
+ return 'map-value-ind';
64
+ case '{':
65
+ return 'flow-map-start';
66
+ case '}':
67
+ return 'flow-map-end';
68
+ case '[':
69
+ return 'flow-seq-start';
70
+ case ']':
71
+ return 'flow-seq-end';
72
+ case ',':
73
+ return 'comma';
74
+ }
75
+ switch (source[0]) {
76
+ case ' ':
77
+ case '\t':
78
+ return 'space';
79
+ case '#':
80
+ return 'comment';
81
+ case '%':
82
+ return 'directive-line';
83
+ case '*':
84
+ return 'alias';
85
+ case '&':
86
+ return 'anchor';
87
+ case '!':
88
+ return 'tag';
89
+ case "'":
90
+ return 'single-quoted-scalar';
91
+ case '"':
92
+ return 'double-quoted-scalar';
93
+ case '|':
94
+ case '>':
95
+ return 'block-scalar-header';
96
+ }
97
+ return null;
98
+ }
99
+
100
+ exports.createScalarToken = cstScalar.createScalarToken;
101
+ exports.resolveAsScalar = cstScalar.resolveAsScalar;
102
+ exports.setScalarValue = cstScalar.setScalarValue;
103
+ exports.stringify = cstStringify.stringify;
104
+ exports.visit = cstVisit.visit;
105
+ exports.BOM = BOM;
106
+ exports.DOCUMENT = DOCUMENT;
107
+ exports.FLOW_END = FLOW_END;
108
+ exports.SCALAR = SCALAR;
109
+ exports.isCollection = isCollection;
110
+ exports.isScalar = isScalar;
111
+ exports.prettyToken = prettyToken;
112
+ exports.tokenType = tokenType;
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Splits an input string into lexical tokens, i.e. smaller strings that are
3
+ * easily identifiable by `tokens.tokenType()`.
4
+ *
5
+ * Lexing starts always in a "stream" context. Incomplete input may be buffered
6
+ * until a complete token can be emitted.
7
+ *
8
+ * In addition to slices of the original input, the following control characters
9
+ * may also be emitted:
10
+ *
11
+ * - `\x02` (Start of Text): A document starts with the next token
12
+ * - `\x18` (Cancel): Unexpected end of flow-mode (indicates an error)
13
+ * - `\x1f` (Unit Separator): Next token is a scalar value
14
+ * - `\u{FEFF}` (Byte order mark): Emitted separately outside documents
15
+ */
16
+ export declare class Lexer {
17
+ /**
18
+ * Flag indicating whether the end of the current buffer marks the end of
19
+ * all input
20
+ */
21
+ private atEnd;
22
+ /**
23
+ * Explicit indent set in block scalar header, as an offset from the current
24
+ * minimum indent, so e.g. set to 1 from a header `|2+`. Set to -1 if not
25
+ * explicitly set.
26
+ */
27
+ private blockScalarIndent;
28
+ /**
29
+ * Block scalars that include a + (keep) chomping indicator in their header
30
+ * include trailing empty lines, which are otherwise excluded from the
31
+ * scalar's contents.
32
+ */
33
+ private blockScalarKeep;
34
+ /** Current input */
35
+ private buffer;
36
+ /**
37
+ * Flag noting whether the map value indicator : can immediately follow this
38
+ * node within a flow context.
39
+ */
40
+ private flowKey;
41
+ /** Count of surrounding flow collection levels. */
42
+ private flowLevel;
43
+ /**
44
+ * Minimum level of indentation required for next lines to be parsed as a
45
+ * part of the current scalar value.
46
+ */
47
+ private indentNext;
48
+ /** Indentation level of the current line. */
49
+ private indentValue;
50
+ /** Stores the state of the lexer if reaching the end of incpomplete input */
51
+ private next;
52
+ /** A pointer to `buffer`; the current position of the lexer. */
53
+ private pos;
54
+ /**
55
+ * Generate YAML tokens from the `source` string. If `incomplete`,
56
+ * a part of the last line may be left as a buffer for the next call.
57
+ *
58
+ * @returns A generator of lexical tokens
59
+ */
60
+ lex(source: string, incomplete?: boolean): Generator<string, void, unknown>;
61
+ private atLineEnd;
62
+ private charAt;
63
+ private continueScalar;
64
+ private getLine;
65
+ private hasChars;
66
+ private setNext;
67
+ private peek;
68
+ private parseNext;
69
+ private parseStream;
70
+ private parseLineStart;
71
+ private parseBlockStart;
72
+ private parseDocument;
73
+ private parseFlowCollection;
74
+ private parseQuotedScalar;
75
+ private parseBlockScalarHeader;
76
+ private parseBlockScalar;
77
+ private parsePlainScalar;
78
+ private pushCount;
79
+ private pushToIndex;
80
+ private pushIndicators;
81
+ private pushVerbatimTag;
82
+ private pushNewline;
83
+ private pushSpaces;
84
+ private pushUntil;
85
+ }