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,222 @@
1
+ 'use strict';
2
+
3
+ var directives = require('../doc/directives.js');
4
+ var Document = require('../doc/Document.js');
5
+ var errors = require('../errors.js');
6
+ var Node = require('../nodes/Node.js');
7
+ var options = require('../options.js');
8
+ var composeDoc = require('./compose-doc.js');
9
+ var resolveEnd = require('./resolve-end.js');
10
+
11
+ function getErrorPos(src) {
12
+ if (typeof src === 'number')
13
+ return [src, src + 1];
14
+ if (Array.isArray(src))
15
+ return src.length === 2 ? src : [src[0], src[1]];
16
+ const { offset, source } = src;
17
+ return [offset, offset + (typeof source === 'string' ? source.length : 1)];
18
+ }
19
+ function parsePrelude(prelude) {
20
+ let comment = '';
21
+ let atComment = false;
22
+ let afterEmptyLine = false;
23
+ for (let i = 0; i < prelude.length; ++i) {
24
+ const source = prelude[i];
25
+ switch (source[0]) {
26
+ case '#':
27
+ comment +=
28
+ (comment === '' ? '' : afterEmptyLine ? '\n\n' : '\n') +
29
+ (source.substring(1) || ' ');
30
+ atComment = true;
31
+ afterEmptyLine = false;
32
+ break;
33
+ case '%':
34
+ if (prelude[i + 1][0] !== '#')
35
+ i += 1;
36
+ atComment = false;
37
+ break;
38
+ default:
39
+ // This may be wrong after doc-end, but in that case it doesn't matter
40
+ if (!atComment)
41
+ afterEmptyLine = true;
42
+ atComment = false;
43
+ }
44
+ }
45
+ return { comment, afterEmptyLine };
46
+ }
47
+ /**
48
+ * Compose a stream of CST nodes into a stream of YAML Documents.
49
+ *
50
+ * ```ts
51
+ * import { Composer, Parser } from 'yaml'
52
+ *
53
+ * const src: string = ...
54
+ * const tokens = new Parser().parse(src)
55
+ * const docs = new Composer().compose(tokens)
56
+ * ```
57
+ */
58
+ class Composer {
59
+ constructor(options$1 = {}) {
60
+ this.doc = null;
61
+ this.atDirectives = false;
62
+ this.prelude = [];
63
+ this.errors = [];
64
+ this.warnings = [];
65
+ this.onError = (source, code, message, warning) => {
66
+ const pos = getErrorPos(source);
67
+ if (warning)
68
+ this.warnings.push(new errors.YAMLWarning(pos, code, message));
69
+ else
70
+ this.errors.push(new errors.YAMLParseError(pos, code, message));
71
+ };
72
+ this.directives = new directives.Directives({
73
+ version: options$1.version || options.defaultOptions.version
74
+ });
75
+ this.options = options$1;
76
+ }
77
+ decorate(doc, afterDoc) {
78
+ const { comment, afterEmptyLine } = parsePrelude(this.prelude);
79
+ //console.log({ dc: doc.comment, prelude, comment })
80
+ if (comment) {
81
+ const dc = doc.contents;
82
+ if (afterDoc) {
83
+ doc.comment = doc.comment ? `${doc.comment}\n${comment}` : comment;
84
+ }
85
+ else if (afterEmptyLine || doc.directives.marker || !dc) {
86
+ doc.commentBefore = comment;
87
+ }
88
+ else if (Node.isCollection(dc) && !dc.flow && dc.items.length > 0) {
89
+ let it = dc.items[0];
90
+ if (Node.isPair(it))
91
+ it = it.key;
92
+ const cb = it.commentBefore;
93
+ it.commentBefore = cb ? `${comment}\n${cb}` : comment;
94
+ }
95
+ else {
96
+ const cb = dc.commentBefore;
97
+ dc.commentBefore = cb ? `${comment}\n${cb}` : comment;
98
+ }
99
+ }
100
+ if (afterDoc) {
101
+ Array.prototype.push.apply(doc.errors, this.errors);
102
+ Array.prototype.push.apply(doc.warnings, this.warnings);
103
+ }
104
+ else {
105
+ doc.errors = this.errors;
106
+ doc.warnings = this.warnings;
107
+ }
108
+ this.prelude = [];
109
+ this.errors = [];
110
+ this.warnings = [];
111
+ }
112
+ /**
113
+ * Current stream status information.
114
+ *
115
+ * Mostly useful at the end of input for an empty stream.
116
+ */
117
+ streamInfo() {
118
+ return {
119
+ comment: parsePrelude(this.prelude).comment,
120
+ directives: this.directives,
121
+ errors: this.errors,
122
+ warnings: this.warnings
123
+ };
124
+ }
125
+ /**
126
+ * Compose tokens into documents.
127
+ *
128
+ * @param forceDoc - If the stream contains no document, still emit a final document including any comments and directives that would be applied to a subsequent document.
129
+ * @param endOffset - Should be set if `forceDoc` is also set, to set the document range end and to indicate errors correctly.
130
+ */
131
+ *compose(tokens, forceDoc = false, endOffset = -1) {
132
+ for (const token of tokens)
133
+ yield* this.next(token);
134
+ yield* this.end(forceDoc, endOffset);
135
+ }
136
+ /** Advance the composer by one CST token. */
137
+ *next(token) {
138
+ if (process.env.LOG_STREAM)
139
+ console.dir(token, { depth: null });
140
+ switch (token.type) {
141
+ case 'directive':
142
+ this.directives.add(token.source, (offset, message, warning) => {
143
+ const pos = getErrorPos(token);
144
+ pos[0] += offset;
145
+ this.onError(pos, 'BAD_DIRECTIVE', message, warning);
146
+ });
147
+ this.prelude.push(token.source);
148
+ this.atDirectives = true;
149
+ break;
150
+ case 'document': {
151
+ const doc = composeDoc.composeDoc(this.options, this.directives, token, this.onError);
152
+ if (this.atDirectives && !doc.directives.marker)
153
+ this.onError(token, 'MISSING_CHAR', 'Missing directives-end indicator line');
154
+ this.decorate(doc, false);
155
+ if (this.doc)
156
+ yield this.doc;
157
+ this.doc = doc;
158
+ this.atDirectives = false;
159
+ break;
160
+ }
161
+ case 'byte-order-mark':
162
+ case 'space':
163
+ break;
164
+ case 'comment':
165
+ case 'newline':
166
+ this.prelude.push(token.source);
167
+ break;
168
+ case 'error': {
169
+ const msg = token.source
170
+ ? `${token.message}: ${JSON.stringify(token.source)}`
171
+ : token.message;
172
+ const error = new errors.YAMLParseError(getErrorPos(token), 'UNEXPECTED_TOKEN', msg);
173
+ if (this.atDirectives || !this.doc)
174
+ this.errors.push(error);
175
+ else
176
+ this.doc.errors.push(error);
177
+ break;
178
+ }
179
+ case 'doc-end': {
180
+ if (!this.doc) {
181
+ const msg = 'Unexpected doc-end without preceding document';
182
+ this.errors.push(new errors.YAMLParseError(getErrorPos(token), 'UNEXPECTED_TOKEN', msg));
183
+ break;
184
+ }
185
+ const end = resolveEnd.resolveEnd(token.end, token.offset + token.source.length, this.doc.options.strict, this.onError);
186
+ this.decorate(this.doc, true);
187
+ if (end.comment) {
188
+ const dc = this.doc.comment;
189
+ this.doc.comment = dc ? `${dc}\n${end.comment}` : end.comment;
190
+ }
191
+ this.doc.range[2] = end.offset;
192
+ break;
193
+ }
194
+ default:
195
+ this.errors.push(new errors.YAMLParseError(getErrorPos(token), 'UNEXPECTED_TOKEN', `Unsupported token ${token.type}`));
196
+ }
197
+ }
198
+ /**
199
+ * Call at end of input to yield any remaining document.
200
+ *
201
+ * @param forceDoc - If the stream contains no document, still emit a final document including any comments and directives that would be applied to a subsequent document.
202
+ * @param endOffset - Should be set if `forceDoc` is also set, to set the document range end and to indicate errors correctly.
203
+ */
204
+ *end(forceDoc = false, endOffset = -1) {
205
+ if (this.doc) {
206
+ this.decorate(this.doc, true);
207
+ yield this.doc;
208
+ this.doc = null;
209
+ }
210
+ else if (forceDoc) {
211
+ const opts = Object.assign({ directives: this.directives }, this.options);
212
+ const doc = new Document.Document(undefined, opts);
213
+ if (this.atDirectives)
214
+ this.onError(endOffset, 'MISSING_CHAR', 'Missing directives-end indicator line');
215
+ doc.range = [0, endOffset, endOffset];
216
+ this.decorate(doc, false);
217
+ yield doc;
218
+ }
219
+ }
220
+ }
221
+
222
+ exports.Composer = Composer;
@@ -0,0 +1,6 @@
1
+ import type { ParsedNode } from '../nodes/Node.js';
2
+ import { YAMLMap } from '../nodes/YAMLMap.js';
3
+ import type { BlockMap } from '../parse/cst.js';
4
+ import type { ComposeContext, ComposeNode } from './compose-node.js';
5
+ import type { ComposeErrorHandler } from './composer.js';
6
+ export declare function resolveBlockMap({ composeNode, composeEmptyNode }: ComposeNode, ctx: ComposeContext, bm: BlockMap, onError: ComposeErrorHandler): YAMLMap.Parsed<ParsedNode, ParsedNode | null>;
@@ -0,0 +1,95 @@
1
+ 'use strict';
2
+
3
+ var Pair = require('../nodes/Pair.js');
4
+ var YAMLMap = require('../nodes/YAMLMap.js');
5
+ var resolveProps = require('./resolve-props.js');
6
+ var utilContainsNewline = require('./util-contains-newline.js');
7
+ var utilMapIncludes = require('./util-map-includes.js');
8
+
9
+ const startColMsg = 'All mapping items must start at the same column';
10
+ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
11
+ var _a;
12
+ const map = new YAMLMap.YAMLMap(ctx.schema);
13
+ let offset = bm.offset;
14
+ for (const { start, key, sep, value } of bm.items) {
15
+ // key properties
16
+ const keyProps = resolveProps.resolveProps(start, {
17
+ indicator: 'explicit-key-ind',
18
+ next: key || (sep === null || sep === void 0 ? void 0 : sep[0]),
19
+ offset,
20
+ onError,
21
+ startOnNewline: true
22
+ });
23
+ const implicitKey = !keyProps.found;
24
+ if (implicitKey) {
25
+ if (key) {
26
+ if (key.type === 'block-seq')
27
+ onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'A block sequence may not be used as an implicit map key');
28
+ else if ('indent' in key && key.indent !== bm.indent)
29
+ onError(offset, 'BAD_INDENT', startColMsg);
30
+ }
31
+ if (!keyProps.anchor && !keyProps.tag && !sep) {
32
+ // TODO: assert being at last item?
33
+ if (keyProps.comment) {
34
+ if (map.comment)
35
+ map.comment += '\n' + keyProps.comment;
36
+ else
37
+ map.comment = keyProps.comment;
38
+ }
39
+ continue;
40
+ }
41
+ }
42
+ else if (((_a = keyProps.found) === null || _a === void 0 ? void 0 : _a.indent) !== bm.indent)
43
+ onError(offset, 'BAD_INDENT', startColMsg);
44
+ if (implicitKey && utilContainsNewline.containsNewline(key))
45
+ onError(key, // checked by containsNewline()
46
+ 'MULTILINE_IMPLICIT_KEY', 'Implicit keys need to be on a single line');
47
+ // key value
48
+ const keyStart = keyProps.end;
49
+ const keyNode = key
50
+ ? composeNode(ctx, key, keyProps, onError)
51
+ : composeEmptyNode(ctx, keyStart, start, null, keyProps, onError);
52
+ if (utilMapIncludes.mapIncludes(ctx, map.items, keyNode))
53
+ onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
54
+ // value properties
55
+ const valueProps = resolveProps.resolveProps(sep || [], {
56
+ indicator: 'map-value-ind',
57
+ next: value,
58
+ offset: keyNode.range[2],
59
+ onError,
60
+ startOnNewline: !key || key.type === 'block-scalar'
61
+ });
62
+ offset = valueProps.end;
63
+ if (valueProps.found) {
64
+ if (implicitKey) {
65
+ if ((value === null || value === void 0 ? void 0 : value.type) === 'block-map' && !valueProps.hasNewline)
66
+ onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'Nested mappings are not allowed in compact mappings');
67
+ if (ctx.options.strict &&
68
+ keyProps.start < valueProps.found.offset - 1024)
69
+ onError(keyNode.range, 'KEY_OVER_1024_CHARS', 'The : indicator must be at most 1024 chars after the start of an implicit block mapping key');
70
+ }
71
+ // value value
72
+ const valueNode = value
73
+ ? composeNode(ctx, value, valueProps, onError)
74
+ : composeEmptyNode(ctx, offset, sep, null, valueProps, onError);
75
+ offset = valueNode.range[2];
76
+ map.items.push(new Pair.Pair(keyNode, valueNode));
77
+ }
78
+ else {
79
+ // key with no value
80
+ if (implicitKey)
81
+ onError(keyNode.range, 'MISSING_CHAR', 'Implicit map keys need to be followed by map values');
82
+ if (valueProps.comment) {
83
+ if (keyNode.comment)
84
+ keyNode.comment += '\n' + valueProps.comment;
85
+ else
86
+ keyNode.comment = valueProps.comment;
87
+ }
88
+ map.items.push(new Pair.Pair(keyNode));
89
+ }
90
+ }
91
+ map.range = [bm.offset, offset, offset];
92
+ return map;
93
+ }
94
+
95
+ exports.resolveBlockMap = resolveBlockMap;
@@ -0,0 +1,10 @@
1
+ import { Range } from '../nodes/Node.js';
2
+ import { Scalar } from '../nodes/Scalar.js';
3
+ import type { BlockScalar } from '../parse/cst.js';
4
+ import type { ComposeErrorHandler } from './composer.js';
5
+ export declare function resolveBlockScalar(scalar: BlockScalar, strict: boolean, onError: ComposeErrorHandler): {
6
+ value: string;
7
+ type: Scalar.BLOCK_FOLDED | Scalar.BLOCK_LITERAL | null;
8
+ comment: string;
9
+ range: Range;
10
+ };
@@ -0,0 +1,187 @@
1
+ 'use strict';
2
+
3
+ var Scalar = require('../nodes/Scalar.js');
4
+
5
+ function resolveBlockScalar(scalar, strict, onError) {
6
+ const start = scalar.offset;
7
+ const header = parseBlockScalarHeader(scalar, strict, onError);
8
+ if (!header)
9
+ return { value: '', type: null, comment: '', range: [start, start, start] };
10
+ const type = header.mode === '>' ? Scalar.Scalar.BLOCK_FOLDED : Scalar.Scalar.BLOCK_LITERAL;
11
+ const lines = scalar.source ? splitLines(scalar.source) : [];
12
+ // determine the end of content & start of chomping
13
+ let chompStart = lines.length;
14
+ for (let i = lines.length - 1; i >= 0; --i) {
15
+ const content = lines[i][1];
16
+ if (content === '' || content === '\r')
17
+ chompStart = i;
18
+ else
19
+ break;
20
+ }
21
+ // shortcut for empty contents
22
+ if (!scalar.source || chompStart === 0) {
23
+ const value = header.chomp === '+' ? lines.map(line => line[0]).join('\n') : '';
24
+ let end = start + header.length;
25
+ if (scalar.source)
26
+ end += scalar.source.length;
27
+ return { value, type, comment: header.comment, range: [start, end, end] };
28
+ }
29
+ // find the indentation level to trim from start
30
+ let trimIndent = scalar.indent + header.indent;
31
+ let offset = scalar.offset + header.length;
32
+ let contentStart = 0;
33
+ for (let i = 0; i < chompStart; ++i) {
34
+ const [indent, content] = lines[i];
35
+ if (content === '' || content === '\r') {
36
+ if (header.indent === 0 && indent.length > trimIndent)
37
+ trimIndent = indent.length;
38
+ }
39
+ else {
40
+ if (indent.length < trimIndent) {
41
+ const message = 'Block scalars with more-indented leading empty lines must use an explicit indentation indicator';
42
+ onError(offset + indent.length, 'MISSING_CHAR', message);
43
+ }
44
+ if (header.indent === 0)
45
+ trimIndent = indent.length;
46
+ contentStart = i;
47
+ break;
48
+ }
49
+ offset += indent.length + content.length + 1;
50
+ }
51
+ let value = '';
52
+ let sep = '';
53
+ let prevMoreIndented = false;
54
+ // leading whitespace is kept intact
55
+ for (let i = 0; i < contentStart; ++i)
56
+ value += lines[i][0].slice(trimIndent) + '\n';
57
+ for (let i = contentStart; i < chompStart; ++i) {
58
+ let [indent, content] = lines[i];
59
+ offset += indent.length + content.length + 1;
60
+ const crlf = content[content.length - 1] === '\r';
61
+ if (crlf)
62
+ content = content.slice(0, -1);
63
+ /* istanbul ignore if already caught in lexer */
64
+ if (content && indent.length < trimIndent) {
65
+ const src = header.indent
66
+ ? 'explicit indentation indicator'
67
+ : 'first line';
68
+ const message = `Block scalar lines must not be less indented than their ${src}`;
69
+ onError(offset - content.length - (crlf ? 2 : 1), 'BAD_INDENT', message);
70
+ indent = '';
71
+ }
72
+ if (type === Scalar.Scalar.BLOCK_LITERAL) {
73
+ value += sep + indent.slice(trimIndent) + content;
74
+ sep = '\n';
75
+ }
76
+ else if (indent.length > trimIndent || content[0] === '\t') {
77
+ // more-indented content within a folded block
78
+ if (sep === ' ')
79
+ sep = '\n';
80
+ else if (!prevMoreIndented && sep === '\n')
81
+ sep = '\n\n';
82
+ value += sep + indent.slice(trimIndent) + content;
83
+ sep = '\n';
84
+ prevMoreIndented = true;
85
+ }
86
+ else if (content === '') {
87
+ // empty line
88
+ if (sep === '\n')
89
+ value += '\n';
90
+ else
91
+ sep = '\n';
92
+ }
93
+ else {
94
+ value += sep + content;
95
+ sep = ' ';
96
+ prevMoreIndented = false;
97
+ }
98
+ }
99
+ switch (header.chomp) {
100
+ case '-':
101
+ break;
102
+ case '+':
103
+ for (let i = chompStart; i < lines.length; ++i)
104
+ value += '\n' + lines[i][0].slice(trimIndent);
105
+ if (value[value.length - 1] !== '\n')
106
+ value += '\n';
107
+ break;
108
+ default:
109
+ value += '\n';
110
+ }
111
+ const end = start + header.length + scalar.source.length;
112
+ return { value, type, comment: header.comment, range: [start, end, end] };
113
+ }
114
+ function parseBlockScalarHeader({ offset, props }, strict, onError) {
115
+ /* istanbul ignore if should not happen */
116
+ if (props[0].type !== 'block-scalar-header') {
117
+ onError(props[0], 'IMPOSSIBLE', 'Block scalar header not found');
118
+ return null;
119
+ }
120
+ const { source } = props[0];
121
+ const mode = source[0];
122
+ let indent = 0;
123
+ let chomp = '';
124
+ let error = -1;
125
+ for (let i = 1; i < source.length; ++i) {
126
+ const ch = source[i];
127
+ if (!chomp && (ch === '-' || ch === '+'))
128
+ chomp = ch;
129
+ else {
130
+ const n = Number(ch);
131
+ if (!indent && n)
132
+ indent = n;
133
+ else if (error === -1)
134
+ error = offset + i;
135
+ }
136
+ }
137
+ if (error !== -1)
138
+ onError(error, 'UNEXPECTED_TOKEN', `Block scalar header includes extra characters: ${source}`);
139
+ let hasSpace = false;
140
+ let comment = '';
141
+ let length = source.length;
142
+ for (let i = 1; i < props.length; ++i) {
143
+ const token = props[i];
144
+ switch (token.type) {
145
+ case 'space':
146
+ hasSpace = true;
147
+ // fallthrough
148
+ case 'newline':
149
+ length += token.source.length;
150
+ break;
151
+ case 'comment':
152
+ if (strict && !hasSpace) {
153
+ const message = 'Comments must be separated from other tokens by white space characters';
154
+ onError(token, 'MISSING_CHAR', message);
155
+ }
156
+ length += token.source.length;
157
+ comment = token.source.substring(1);
158
+ break;
159
+ case 'error':
160
+ onError(token, 'UNEXPECTED_TOKEN', token.message);
161
+ length += token.source.length;
162
+ break;
163
+ /* istanbul ignore next should not happen */
164
+ default: {
165
+ const message = `Unexpected token in block scalar header: ${token.type}`;
166
+ onError(token, 'UNEXPECTED_TOKEN', message);
167
+ const ts = token.source;
168
+ if (ts && typeof ts === 'string')
169
+ length += ts.length;
170
+ }
171
+ }
172
+ }
173
+ return { mode, indent, chomp, comment, length };
174
+ }
175
+ /** @returns Array of lines split up as `[indent, content]` */
176
+ function splitLines(source) {
177
+ const split = source.split(/\n( *)/);
178
+ const first = split[0];
179
+ const m = first.match(/^( *)/);
180
+ const line0 = m && m[1] ? [m[1], first.slice(m[1].length)] : ['', first];
181
+ const lines = [line0];
182
+ for (let i = 1; i < split.length; i += 2)
183
+ lines.push([split[i], split[i + 1]]);
184
+ return lines;
185
+ }
186
+
187
+ exports.resolveBlockScalar = resolveBlockScalar;
@@ -0,0 +1,5 @@
1
+ import { YAMLSeq } from '../nodes/YAMLSeq.js';
2
+ import type { BlockSequence } from '../parse/cst.js';
3
+ import type { ComposeContext, ComposeNode } from './compose-node.js';
4
+ import type { ComposeErrorHandler } from './composer.js';
5
+ export declare function resolveBlockSeq({ composeNode, composeEmptyNode }: ComposeNode, ctx: ComposeContext, bs: BlockSequence, onError: ComposeErrorHandler): YAMLSeq.Parsed<import("../index.js").ParsedNode>;
@@ -0,0 +1,42 @@
1
+ 'use strict';
2
+
3
+ var YAMLSeq = require('../nodes/YAMLSeq.js');
4
+ var resolveProps = require('./resolve-props.js');
5
+
6
+ function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError) {
7
+ const seq = new YAMLSeq.YAMLSeq(ctx.schema);
8
+ let offset = bs.offset;
9
+ for (const { start, value } of bs.items) {
10
+ const props = resolveProps.resolveProps(start, {
11
+ indicator: 'seq-item-ind',
12
+ next: value,
13
+ offset,
14
+ onError,
15
+ startOnNewline: true
16
+ });
17
+ offset = props.end;
18
+ if (!props.found) {
19
+ if (props.anchor || props.tag || value) {
20
+ if (value && value.type === 'block-seq')
21
+ onError(offset, 'BAD_INDENT', 'All sequence items must start at the same column');
22
+ else
23
+ onError(offset, 'MISSING_CHAR', 'Sequence item without - indicator');
24
+ }
25
+ else {
26
+ // TODO: assert being at last item?
27
+ if (props.comment)
28
+ seq.comment = props.comment;
29
+ continue;
30
+ }
31
+ }
32
+ const node = value
33
+ ? composeNode(ctx, value, props, onError)
34
+ : composeEmptyNode(ctx, offset, start, null, props, onError);
35
+ offset = node.range[2];
36
+ seq.items.push(node);
37
+ }
38
+ seq.range = [bs.offset, offset, offset];
39
+ return seq;
40
+ }
41
+
42
+ exports.resolveBlockSeq = resolveBlockSeq;
@@ -0,0 +1,6 @@
1
+ import type { SourceToken } from '../parse/cst.js';
2
+ import type { ComposeErrorHandler } from './composer.js';
3
+ export declare function resolveEnd(end: SourceToken[] | undefined, offset: number, reqSpace: boolean, onError: ComposeErrorHandler): {
4
+ comment: string;
5
+ offset: number;
6
+ };
@@ -0,0 +1,39 @@
1
+ 'use strict';
2
+
3
+ function resolveEnd(end, offset, reqSpace, onError) {
4
+ let comment = '';
5
+ if (end) {
6
+ let hasSpace = false;
7
+ let sep = '';
8
+ for (const token of end) {
9
+ const { source, type } = token;
10
+ switch (type) {
11
+ case 'space':
12
+ hasSpace = true;
13
+ break;
14
+ case 'comment': {
15
+ if (reqSpace && !hasSpace)
16
+ onError(token, 'MISSING_CHAR', 'Comments must be separated from other tokens by white space characters');
17
+ const cb = source.substring(1) || ' ';
18
+ if (!comment)
19
+ comment = cb;
20
+ else
21
+ comment += sep + cb;
22
+ sep = '';
23
+ break;
24
+ }
25
+ case 'newline':
26
+ if (comment)
27
+ sep += source;
28
+ hasSpace = true;
29
+ break;
30
+ default:
31
+ onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${type} at node end`);
32
+ }
33
+ offset += source.length;
34
+ }
35
+ }
36
+ return { comment, offset };
37
+ }
38
+
39
+ exports.resolveEnd = resolveEnd;
@@ -0,0 +1,6 @@
1
+ import { YAMLMap } from '../nodes/YAMLMap.js';
2
+ import { YAMLSeq } from '../nodes/YAMLSeq.js';
3
+ import type { FlowCollection } from '../parse/cst.js';
4
+ import type { ComposeContext, ComposeNode } from './compose-node.js';
5
+ import type { ComposeErrorHandler } from './composer.js';
6
+ export declare function resolveFlowCollection({ composeNode, composeEmptyNode }: ComposeNode, ctx: ComposeContext, fc: FlowCollection, onError: ComposeErrorHandler): YAMLMap.Parsed<import("../nodes/Node.js").ParsedNode, import("../nodes/Node.js").ParsedNode | null> | YAMLSeq.Parsed<import("../nodes/Node.js").ParsedNode>;