yaml 2.0.0-4 → 2.0.0-8

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 (220) hide show
  1. package/README.md +5 -5
  2. package/browser/dist/compose/compose-collection.js +12 -9
  3. package/browser/dist/compose/compose-doc.js +23 -6
  4. package/browser/dist/compose/compose-node.js +27 -17
  5. package/browser/dist/compose/compose-scalar.js +21 -23
  6. package/browser/dist/compose/composer.js +108 -78
  7. package/browser/dist/compose/resolve-block-map.js +41 -29
  8. package/browser/dist/compose/resolve-block-scalar.js +14 -17
  9. package/browser/dist/compose/resolve-block-seq.js +21 -17
  10. package/browser/dist/compose/resolve-end.js +7 -8
  11. package/browser/dist/compose/resolve-flow-collection.js +158 -199
  12. package/browser/dist/compose/resolve-flow-scalar.js +62 -26
  13. package/browser/dist/compose/resolve-props.js +72 -37
  14. package/browser/dist/compose/util-contains-newline.js +15 -12
  15. package/browser/dist/compose/util-map-includes.js +17 -0
  16. package/browser/dist/doc/Document.js +74 -99
  17. package/browser/dist/doc/anchors.js +72 -0
  18. package/browser/dist/doc/createNode.js +35 -19
  19. package/browser/dist/doc/directives.js +5 -0
  20. package/browser/dist/errors.js +16 -12
  21. package/browser/dist/index.js +3 -3
  22. package/browser/dist/nodes/Alias.js +51 -28
  23. package/browser/dist/nodes/Collection.js +23 -92
  24. package/browser/dist/nodes/Node.js +9 -1
  25. package/browser/dist/nodes/Pair.js +16 -231
  26. package/browser/dist/nodes/YAMLMap.js +8 -4
  27. package/browser/dist/nodes/YAMLSeq.js +7 -4
  28. package/browser/dist/nodes/addPairToJSMap.js +103 -0
  29. package/browser/dist/nodes/toJS.js +12 -10
  30. package/browser/dist/options.js +1 -2
  31. package/browser/dist/parse/cst-scalar.js +219 -0
  32. package/browser/dist/parse/cst-stringify.js +61 -0
  33. package/browser/dist/parse/cst-visit.js +97 -0
  34. package/{dist/parse/tokens.js → browser/dist/parse/cst.js} +12 -7
  35. package/browser/dist/parse/lexer.js +149 -125
  36. package/browser/dist/parse/parser.js +272 -196
  37. package/browser/dist/public-api.js +11 -14
  38. package/browser/dist/schema/Schema.js +28 -0
  39. package/browser/dist/{tags/failsafe → schema/common}/map.js +0 -0
  40. package/browser/dist/schema/common/null.js +13 -0
  41. package/browser/dist/{tags/failsafe → schema/common}/seq.js +1 -1
  42. package/browser/dist/{tags/failsafe → schema/common}/string.js +0 -0
  43. package/browser/dist/schema/core/bool.js +19 -0
  44. package/browser/dist/schema/core/float.js +40 -0
  45. package/browser/dist/schema/core/int.js +38 -0
  46. package/browser/dist/schema/core/schema.js +23 -0
  47. package/browser/dist/{tags/json.js → schema/json/schema.js} +5 -6
  48. package/browser/dist/schema/tags.js +79 -0
  49. package/browser/dist/{tags → schema}/yaml-1.1/binary.js +0 -0
  50. package/browser/dist/schema/yaml-1.1/bool.js +26 -0
  51. package/browser/dist/schema/yaml-1.1/float.js +43 -0
  52. package/browser/dist/schema/yaml-1.1/int.js +71 -0
  53. package/browser/dist/{tags → schema}/yaml-1.1/omap.js +0 -0
  54. package/browser/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
  55. package/browser/dist/schema/yaml-1.1/schema.js +37 -0
  56. package/browser/dist/{tags → schema}/yaml-1.1/set.js +0 -0
  57. package/browser/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
  58. package/browser/dist/stringify/stringify.js +8 -6
  59. package/browser/dist/stringify/stringifyCollection.js +122 -0
  60. package/browser/dist/stringify/stringifyComment.js +14 -0
  61. package/browser/dist/stringify/stringifyDocument.js +64 -0
  62. package/browser/dist/stringify/stringifyPair.js +111 -0
  63. package/browser/dist/stringify/stringifyString.js +53 -52
  64. package/browser/dist/visit.js +20 -1
  65. package/dist/compose/compose-collection.d.ts +4 -4
  66. package/dist/compose/compose-collection.js +12 -9
  67. package/dist/compose/compose-doc.d.ts +4 -3
  68. package/dist/compose/compose-doc.js +23 -6
  69. package/dist/compose/compose-node.d.ts +15 -7
  70. package/dist/compose/compose-node.js +27 -17
  71. package/dist/compose/compose-scalar.d.ts +4 -3
  72. package/dist/compose/compose-scalar.js +20 -22
  73. package/dist/compose/composer.d.ts +28 -23
  74. package/dist/compose/composer.js +109 -79
  75. package/dist/compose/resolve-block-map.d.ts +5 -4
  76. package/dist/compose/resolve-block-map.js +41 -29
  77. package/dist/compose/resolve-block-scalar.d.ts +5 -3
  78. package/dist/compose/resolve-block-scalar.js +14 -17
  79. package/dist/compose/resolve-block-seq.d.ts +4 -4
  80. package/dist/compose/resolve-block-seq.js +21 -17
  81. package/dist/compose/resolve-end.d.ts +3 -2
  82. package/dist/compose/resolve-end.js +7 -8
  83. package/dist/compose/resolve-flow-collection.d.ts +4 -5
  84. package/dist/compose/resolve-flow-collection.js +157 -198
  85. package/dist/compose/resolve-flow-scalar.d.ts +5 -3
  86. package/dist/compose/resolve-flow-scalar.js +62 -26
  87. package/dist/compose/resolve-props.d.ts +16 -10
  88. package/dist/compose/resolve-props.js +72 -37
  89. package/dist/compose/util-contains-newline.d.ts +1 -1
  90. package/dist/compose/util-contains-newline.js +15 -12
  91. package/dist/compose/util-empty-scalar-position.d.ts +1 -1
  92. package/dist/compose/util-map-includes.d.ts +4 -0
  93. package/dist/compose/util-map-includes.js +19 -0
  94. package/dist/doc/Document.d.ts +35 -15
  95. package/dist/doc/Document.js +72 -97
  96. package/dist/doc/anchors.d.ts +24 -0
  97. package/dist/doc/anchors.js +77 -0
  98. package/dist/doc/createNode.d.ts +10 -11
  99. package/dist/doc/createNode.js +34 -18
  100. package/dist/doc/directives.d.ts +1 -0
  101. package/dist/doc/directives.js +5 -0
  102. package/dist/errors.d.ts +11 -8
  103. package/dist/errors.js +16 -12
  104. package/dist/index.d.ts +5 -3
  105. package/dist/index.js +3 -3
  106. package/dist/nodes/Alias.d.ts +19 -9
  107. package/dist/nodes/Alias.js +51 -28
  108. package/dist/nodes/Collection.d.ts +9 -17
  109. package/dist/nodes/Collection.js +22 -91
  110. package/dist/nodes/Node.d.ts +10 -4
  111. package/dist/nodes/Node.js +9 -0
  112. package/dist/nodes/Pair.d.ts +10 -13
  113. package/dist/nodes/Pair.js +15 -230
  114. package/dist/nodes/Scalar.d.ts +6 -3
  115. package/dist/nodes/YAMLMap.d.ts +3 -3
  116. package/dist/nodes/YAMLMap.js +8 -4
  117. package/dist/nodes/YAMLSeq.d.ts +3 -3
  118. package/dist/nodes/YAMLSeq.js +7 -4
  119. package/dist/nodes/addPairToJSMap.d.ts +3 -0
  120. package/dist/nodes/addPairToJSMap.js +105 -0
  121. package/dist/nodes/toJS.d.ts +4 -5
  122. package/dist/nodes/toJS.js +12 -10
  123. package/dist/options.d.ts +41 -25
  124. package/dist/options.js +1 -2
  125. package/dist/parse/cst-scalar.d.ts +58 -0
  126. package/dist/parse/cst-scalar.js +223 -0
  127. package/dist/parse/cst-stringify.d.ts +8 -0
  128. package/dist/parse/cst-stringify.js +63 -0
  129. package/dist/parse/cst-visit.d.ts +39 -0
  130. package/dist/parse/cst-visit.js +99 -0
  131. package/dist/parse/{tokens.d.ts → cst.d.ts} +23 -7
  132. package/{browser/dist/parse/tokens.js → dist/parse/cst.js} +27 -1
  133. package/dist/parse/lexer.d.ts +7 -14
  134. package/dist/parse/lexer.js +150 -126
  135. package/dist/parse/parser.d.ts +31 -24
  136. package/dist/parse/parser.js +274 -198
  137. package/dist/public-api.d.ts +7 -7
  138. package/dist/public-api.js +11 -14
  139. package/dist/{doc → schema}/Schema.d.ts +6 -3
  140. package/dist/schema/Schema.js +30 -0
  141. package/dist/{tags/failsafe → schema/common}/map.d.ts +0 -0
  142. package/dist/{tags/failsafe → schema/common}/map.js +0 -0
  143. package/dist/schema/common/null.d.ts +4 -0
  144. package/dist/schema/common/null.js +15 -0
  145. package/dist/{tags/failsafe → schema/common}/seq.d.ts +0 -0
  146. package/dist/{tags/failsafe → schema/common}/seq.js +1 -1
  147. package/dist/{tags/failsafe → schema/common}/string.d.ts +0 -0
  148. package/dist/{tags/failsafe → schema/common}/string.js +0 -0
  149. package/dist/schema/core/bool.d.ts +4 -0
  150. package/dist/schema/core/bool.js +21 -0
  151. package/dist/schema/core/float.d.ts +4 -0
  152. package/dist/schema/core/float.js +44 -0
  153. package/dist/schema/core/int.d.ts +4 -0
  154. package/dist/schema/core/int.js +42 -0
  155. package/dist/schema/core/schema.d.ts +1 -0
  156. package/dist/schema/core/schema.js +25 -0
  157. package/dist/schema/json/schema.d.ts +2 -0
  158. package/dist/{tags/json.js → schema/json/schema.js} +5 -6
  159. package/dist/schema/tags.d.ts +41 -0
  160. package/dist/schema/tags.js +82 -0
  161. package/dist/{tags → schema}/types.d.ts +3 -7
  162. package/dist/{tags → schema}/yaml-1.1/binary.d.ts +0 -0
  163. package/dist/{tags → schema}/yaml-1.1/binary.js +0 -0
  164. package/dist/schema/yaml-1.1/bool.d.ts +7 -0
  165. package/dist/schema/yaml-1.1/bool.js +29 -0
  166. package/dist/schema/yaml-1.1/float.d.ts +4 -0
  167. package/dist/schema/yaml-1.1/float.js +47 -0
  168. package/dist/schema/yaml-1.1/int.d.ts +5 -0
  169. package/dist/schema/yaml-1.1/int.js +76 -0
  170. package/dist/{tags → schema}/yaml-1.1/omap.d.ts +0 -0
  171. package/dist/{tags → schema}/yaml-1.1/omap.js +0 -0
  172. package/dist/{tags → schema}/yaml-1.1/pairs.d.ts +3 -2
  173. package/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
  174. package/dist/schema/yaml-1.1/schema.d.ts +1 -0
  175. package/dist/schema/yaml-1.1/schema.js +39 -0
  176. package/dist/{tags → schema}/yaml-1.1/set.d.ts +1 -1
  177. package/dist/{tags → schema}/yaml-1.1/set.js +0 -0
  178. package/dist/{tags → schema}/yaml-1.1/timestamp.d.ts +0 -0
  179. package/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
  180. package/dist/stringify/stringify.d.ts +1 -2
  181. package/dist/stringify/stringify.js +8 -6
  182. package/dist/stringify/stringifyCollection.d.ts +21 -0
  183. package/dist/stringify/stringifyCollection.js +124 -0
  184. package/dist/stringify/stringifyComment.d.ts +2 -0
  185. package/dist/stringify/stringifyComment.js +17 -0
  186. package/dist/stringify/stringifyDocument.d.ts +3 -0
  187. package/dist/stringify/stringifyDocument.js +66 -0
  188. package/dist/stringify/stringifyPair.d.ts +3 -0
  189. package/dist/stringify/stringifyPair.js +113 -0
  190. package/dist/stringify/stringifyString.js +53 -52
  191. package/dist/test-events.d.ts +1 -2
  192. package/dist/test-events.js +25 -12
  193. package/dist/util.d.ts +1 -1
  194. package/dist/visit.d.ts +10 -1
  195. package/dist/visit.js +20 -1
  196. package/package.json +9 -8
  197. package/browser/dist/doc/Anchors.js +0 -102
  198. package/browser/dist/doc/Schema.js +0 -27
  199. package/browser/dist/doc/getSchemaTags.js +0 -30
  200. package/browser/dist/stringify/addComment.js +0 -17
  201. package/browser/dist/tags/core.js +0 -109
  202. package/browser/dist/tags/failsafe/index.js +0 -7
  203. package/browser/dist/tags/index.js +0 -34
  204. package/browser/dist/tags/yaml-1.1/index.js +0 -152
  205. package/dist/doc/Anchors.d.ts +0 -36
  206. package/dist/doc/Anchors.js +0 -104
  207. package/dist/doc/Schema.js +0 -29
  208. package/dist/doc/getSchemaTags.d.ts +0 -3
  209. package/dist/doc/getSchemaTags.js +0 -32
  210. package/dist/stringify/addComment.d.ts +0 -2
  211. package/dist/stringify/addComment.js +0 -20
  212. package/dist/tags/core.d.ts +0 -14
  213. package/dist/tags/core.js +0 -119
  214. package/dist/tags/failsafe/index.d.ts +0 -1
  215. package/dist/tags/failsafe/index.js +0 -9
  216. package/dist/tags/index.d.ts +0 -31
  217. package/dist/tags/index.js +0 -37
  218. package/dist/tags/json.d.ts +0 -2
  219. package/dist/tags/yaml-1.1/index.d.ts +0 -2
  220. package/dist/tags/yaml-1.1/index.js +0 -154
@@ -7,30 +7,36 @@ var resolveEnd = require('./resolve-end.js');
7
7
  var utilEmptyScalarPosition = require('./util-empty-scalar-position.js');
8
8
 
9
9
  const CN = { composeNode, composeEmptyNode };
10
- function composeNode(doc, token, props, onError) {
11
- const { spaceBefore, comment, anchor, tagName } = props;
10
+ function composeNode(ctx, token, props, onError) {
11
+ const { spaceBefore, comment, anchor, tag } = props;
12
12
  let node;
13
13
  switch (token.type) {
14
14
  case 'alias':
15
- node = composeAlias(doc, token, onError);
16
- if (anchor || tagName)
17
- onError(token.offset, 'An alias node must not specify any properties');
15
+ node = composeAlias(ctx, token, onError);
16
+ if (anchor || tag)
17
+ onError(token, 'ALIAS_PROPS', 'An alias node must not specify any properties');
18
18
  break;
19
19
  case 'scalar':
20
20
  case 'single-quoted-scalar':
21
21
  case 'double-quoted-scalar':
22
22
  case 'block-scalar':
23
- node = composeScalar.composeScalar(doc, token, anchor, tagName, onError);
23
+ node = composeScalar.composeScalar(ctx, token, tag, onError);
24
+ if (anchor)
25
+ node.anchor = anchor.source.substring(1);
24
26
  break;
25
27
  case 'block-map':
26
28
  case 'block-seq':
27
29
  case 'flow-collection':
28
- node = composeCollection.composeCollection(CN, doc, token, anchor, tagName, onError);
30
+ node = composeCollection.composeCollection(CN, ctx, token, tag, onError);
31
+ if (anchor)
32
+ node.anchor = anchor.source.substring(1);
29
33
  break;
30
34
  default:
31
35
  console.log(token);
32
36
  throw new Error(`Unsupporten token type: ${token.type}`);
33
37
  }
38
+ if (anchor && node.anchor === '')
39
+ onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string');
34
40
  if (spaceBefore)
35
41
  node.spaceBefore = true;
36
42
  if (comment) {
@@ -41,28 +47,32 @@ function composeNode(doc, token, props, onError) {
41
47
  }
42
48
  return node;
43
49
  }
44
- function composeEmptyNode(doc, offset, before, pos, { spaceBefore, comment, anchor, tagName }, onError) {
50
+ function composeEmptyNode(ctx, offset, before, pos, { spaceBefore, comment, anchor, tag }, onError) {
45
51
  const token = {
46
52
  type: 'scalar',
47
53
  offset: utilEmptyScalarPosition.emptyScalarPosition(offset, before, pos),
48
54
  indent: -1,
49
55
  source: ''
50
56
  };
51
- const node = composeScalar.composeScalar(doc, token, anchor, tagName, onError);
57
+ const node = composeScalar.composeScalar(ctx, token, tag, onError);
58
+ if (anchor) {
59
+ node.anchor = anchor.source.substring(1);
60
+ if (node.anchor === '')
61
+ onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string');
62
+ }
52
63
  if (spaceBefore)
53
64
  node.spaceBefore = true;
54
65
  if (comment)
55
66
  node.comment = comment;
56
67
  return node;
57
68
  }
58
- function composeAlias(doc, { offset, source, end }, onError) {
59
- const name = source.substring(1);
60
- const src = doc.anchors.getNode(name);
61
- if (!src)
62
- onError(offset, `Aliased anchor not found: ${name}`);
63
- const alias = new Alias.Alias(src);
64
- const re = resolveEnd.resolveEnd(end, offset + source.length, doc.options.strict, onError);
65
- alias.range = [offset, re.offset];
69
+ function composeAlias({ options }, { offset, source, end }, onError) {
70
+ const alias = new Alias.Alias(source.substring(1));
71
+ if (alias.source === '')
72
+ onError(offset, 'BAD_ALIAS', 'Alias cannot be an empty string');
73
+ const valueEnd = offset + source.length;
74
+ const re = resolveEnd.resolveEnd(end, valueEnd, options.strict, onError);
75
+ alias.range = [offset, valueEnd, re.offset];
66
76
  if (re.comment)
67
77
  alias.comment = re.comment;
68
78
  return alias;
@@ -1,4 +1,5 @@
1
- import type { Document } from '../doc/Document.js';
2
1
  import { Scalar } from '../nodes/Scalar.js';
3
- import type { BlockScalar, FlowScalar } from '../parse/tokens.js';
4
- export declare function composeScalar(doc: Document.Parsed, token: FlowScalar | BlockScalar, anchor: string | null, tagName: string | null, onError: (offset: number, message: string) => void): Scalar.Parsed;
2
+ import type { BlockScalar, FlowScalar, SourceToken } from '../parse/cst.js';
3
+ import type { ComposeContext } from './compose-node.js';
4
+ import type { ComposeErrorHandler } from './composer.js';
5
+ export declare function composeScalar(ctx: ComposeContext, token: FlowScalar | BlockScalar, tagToken: SourceToken | null, onError: ComposeErrorHandler): Scalar.Parsed;
@@ -5,44 +5,42 @@ var Scalar = require('../nodes/Scalar.js');
5
5
  var resolveBlockScalar = require('./resolve-block-scalar.js');
6
6
  var resolveFlowScalar = require('./resolve-flow-scalar.js');
7
7
 
8
- function composeScalar(doc, token, anchor, tagName, onError) {
9
- const { offset } = token;
10
- const { value, type, comment, length } = token.type === 'block-scalar'
11
- ? resolveBlockScalar.resolveBlockScalar(token, doc.options.strict, onError)
12
- : resolveFlowScalar.resolveFlowScalar(token, doc.options.strict, onError);
13
- const tag = tagName
14
- ? findScalarTagByName(doc.schema, value, tagName, onError)
15
- : findScalarTagByTest(doc.schema, value, token.type === 'scalar');
8
+ function composeScalar(ctx, token, tagToken, onError) {
9
+ const { value, type, comment, range } = token.type === 'block-scalar'
10
+ ? resolveBlockScalar.resolveBlockScalar(token, ctx.options.strict, onError)
11
+ : resolveFlowScalar.resolveFlowScalar(token, ctx.options.strict, onError);
12
+ const tagName = tagToken
13
+ ? ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg))
14
+ : null;
15
+ const tag = tagToken && tagName
16
+ ? findScalarTagByName(ctx.schema, value, tagName, tagToken, onError)
17
+ : findScalarTagByTest(ctx.schema, value, token.type === 'scalar');
16
18
  let scalar;
17
19
  try {
18
- const res = tag
19
- ? tag.resolve(value, msg => onError(offset, msg), doc.options)
20
- : value;
20
+ const res = tag.resolve(value, msg => onError(tagToken || token, 'TAG_RESOLVE_FAILED', msg), ctx.options);
21
21
  scalar = Node.isScalar(res) ? res : new Scalar.Scalar(res);
22
22
  }
23
23
  catch (error) {
24
- onError(offset, error.message);
24
+ const msg = error instanceof Error ? error.message : String(error);
25
+ onError(tagToken || token, 'TAG_RESOLVE_FAILED', msg);
25
26
  scalar = new Scalar.Scalar(value);
26
27
  }
27
- scalar.range = [offset, offset + length];
28
+ scalar.range = range;
28
29
  scalar.source = value;
29
30
  if (type)
30
31
  scalar.type = type;
31
32
  if (tagName)
32
33
  scalar.tag = tagName;
33
- if (tag === null || tag === void 0 ? void 0 : tag.format)
34
+ if (tag.format)
34
35
  scalar.format = tag.format;
35
36
  if (comment)
36
37
  scalar.comment = comment;
37
- if (anchor)
38
- doc.anchors.setAnchor(scalar, anchor);
39
38
  return scalar;
40
39
  }
41
- const defaultScalarTag = (schema) => schema.tags.find(tag => !tag.collection && tag.tag === 'tag:yaml.org,2002:str');
42
- function findScalarTagByName(schema, value, tagName, onError) {
40
+ function findScalarTagByName(schema, value, tagName, tagToken, onError) {
43
41
  var _a;
44
42
  if (tagName === '!')
45
- return defaultScalarTag(schema); // non-specific tag
43
+ return schema[Node.SCALAR]; // non-specific tag
46
44
  const matchWithTest = [];
47
45
  for (const tag of schema.tags) {
48
46
  if (!tag.collection && tag.tag === tagName) {
@@ -62,8 +60,8 @@ function findScalarTagByName(schema, value, tagName, onError) {
62
60
  schema.tags.push(Object.assign({}, kt, { default: false, test: undefined }));
63
61
  return kt;
64
62
  }
65
- onError(0, `Unresolved tag: ${tagName}`, tagName !== 'tag:yaml.org,2002:str');
66
- return defaultScalarTag(schema);
63
+ onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, tagName !== 'tag:yaml.org,2002:str');
64
+ return schema[Node.SCALAR];
67
65
  }
68
66
  function findScalarTagByTest(schema, value, apply) {
69
67
  var _a;
@@ -73,7 +71,7 @@ function findScalarTagByTest(schema, value, apply) {
73
71
  return tag;
74
72
  }
75
73
  }
76
- return defaultScalarTag(schema);
74
+ return schema[Node.SCALAR];
77
75
  }
78
76
 
79
77
  exports.composeScalar = composeScalar;
@@ -1,29 +1,34 @@
1
1
  import { Directives } from '../doc/directives.js';
2
- import { YAMLParseError, YAMLWarning } from '../errors.js';
3
- import { Options } from '../options.js';
4
- import type { Token } from '../parse/tokens.js';
2
+ import { Document } from '../doc/Document.js';
3
+ import { ErrorCode, YAMLParseError, YAMLWarning } from '../errors.js';
4
+ import { Range } from '../nodes/Node.js';
5
+ import { DocumentOptions, ParseOptions, SchemaOptions } from '../options.js';
6
+ import type { Token } from '../parse/cst.js';
7
+ declare type ErrorSource = number | [number, number] | Range | {
8
+ offset: number;
9
+ source?: string;
10
+ };
11
+ export declare type ComposeErrorHandler = (source: ErrorSource, code: ErrorCode, message: string, warning?: boolean) => void;
5
12
  /**
6
13
  * Compose a stream of CST nodes into a stream of YAML Documents.
7
14
  *
8
15
  * ```ts
9
- * const options: Options = { ... }
10
- * const docs: Document.Parsed[] = []
11
- * const composer = new Composer(doc => docs.push(doc), options)
12
- * const parser = new Parser(composer.next)
13
- * parser.parse(source)
14
- * composer.end()
16
+ * import { Composer, Parser } from 'yaml'
17
+ *
18
+ * const src: string = ...
19
+ * const tokens = new Parser().parse(src)
20
+ * const docs = new Composer().compose(tokens)
15
21
  * ```
16
22
  */
17
23
  export declare class Composer {
18
24
  private directives;
19
25
  private doc;
20
- private onDocument;
21
26
  private options;
22
27
  private atDirectives;
23
28
  private prelude;
24
29
  private errors;
25
30
  private warnings;
26
- constructor(onDocument: Composer['onDocument'], options?: Options);
31
+ constructor(options?: ParseOptions & DocumentOptions & SchemaOptions);
27
32
  private onError;
28
33
  private decorate;
29
34
  /**
@@ -38,20 +43,20 @@ export declare class Composer {
38
43
  warnings: YAMLWarning[];
39
44
  };
40
45
  /**
41
- * Advance the composed by one CST token. Bound to the Composer
42
- * instance, so may be used directly as a callback function.
46
+ * Compose tokens into documents.
47
+ *
48
+ * @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.
49
+ * @param endOffset - Should be set if `forceDoc` is also set, to set the document range end and to indicate errors correctly.
43
50
  */
44
- next: (token: Token) => void;
45
- /** Call at end of input to push out any remaining document. */
46
- end(): void;
51
+ compose(tokens: Iterable<Token>, forceDoc?: boolean, endOffset?: number): Generator<Document.Parsed<import("../nodes/Node.js").ParsedNode>, void, unknown>;
52
+ /** Advance the composer by one CST token. */
53
+ next(token: Token): Generator<Document.Parsed<import("../nodes/Node.js").ParsedNode>, void, unknown>;
47
54
  /**
48
- * Call at end of input to push out any remaining document.
55
+ * Call at end of input to yield any remaining document.
49
56
  *
50
- * @param forceDoc - If the stream contains no document, still emit a final
51
- * document including any comments and directives that would be applied
52
- * to a subsequent document.
53
- * @param offset - Should be set if `forceDoc` is also set, to set the
54
- * document range end and to indicate errors correctly.
57
+ * @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.
58
+ * @param endOffset - Should be set if `forceDoc` is also set, to set the document range end and to indicate errors correctly.
55
59
  */
56
- end(forceDoc: true, offset: number): void;
60
+ end(forceDoc?: boolean, endOffset?: number): Generator<Document.Parsed<import("../nodes/Node.js").ParsedNode>, void, unknown>;
57
61
  }
62
+ export {};
@@ -8,7 +8,16 @@ var options = require('../options.js');
8
8
  var composeDoc = require('./compose-doc.js');
9
9
  var resolveEnd = require('./resolve-end.js');
10
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
+ }
11
19
  function parsePrelude(prelude) {
20
+ var _a;
12
21
  let comment = '';
13
22
  let atComment = false;
14
23
  let afterEmptyLine = false;
@@ -18,12 +27,12 @@ function parsePrelude(prelude) {
18
27
  case '#':
19
28
  comment +=
20
29
  (comment === '' ? '' : afterEmptyLine ? '\n\n' : '\n') +
21
- source.substring(1);
30
+ (source.substring(1) || ' ');
22
31
  atComment = true;
23
32
  afterEmptyLine = false;
24
33
  break;
25
34
  case '%':
26
- if (prelude[i + 1][0] !== '#')
35
+ if (((_a = prelude[i + 1]) === null || _a === void 0 ? void 0 : _a[0]) !== '#')
27
36
  i += 1;
28
37
  atComment = false;
29
38
  break;
@@ -40,90 +49,30 @@ function parsePrelude(prelude) {
40
49
  * Compose a stream of CST nodes into a stream of YAML Documents.
41
50
  *
42
51
  * ```ts
43
- * const options: Options = { ... }
44
- * const docs: Document.Parsed[] = []
45
- * const composer = new Composer(doc => docs.push(doc), options)
46
- * const parser = new Parser(composer.next)
47
- * parser.parse(source)
48
- * composer.end()
52
+ * import { Composer, Parser } from 'yaml'
53
+ *
54
+ * const src: string = ...
55
+ * const tokens = new Parser().parse(src)
56
+ * const docs = new Composer().compose(tokens)
49
57
  * ```
50
58
  */
51
59
  class Composer {
52
- constructor(onDocument, options$1 = {}) {
60
+ constructor(options$1 = {}) {
53
61
  this.doc = null;
54
62
  this.atDirectives = false;
55
63
  this.prelude = [];
56
64
  this.errors = [];
57
65
  this.warnings = [];
58
- this.onError = (offset, message, warning) => {
66
+ this.onError = (source, code, message, warning) => {
67
+ const pos = getErrorPos(source);
59
68
  if (warning)
60
- this.warnings.push(new errors.YAMLWarning(offset, message));
69
+ this.warnings.push(new errors.YAMLWarning(pos, code, message));
61
70
  else
62
- this.errors.push(new errors.YAMLParseError(offset, message));
63
- };
64
- /**
65
- * Advance the composed by one CST token. Bound to the Composer
66
- * instance, so may be used directly as a callback function.
67
- */
68
- this.next = (token) => {
69
- if (process.env.LOG_STREAM)
70
- console.dir(token, { depth: null });
71
- switch (token.type) {
72
- case 'directive':
73
- this.directives.add(token.source, this.onError);
74
- this.prelude.push(token.source);
75
- this.atDirectives = true;
76
- break;
77
- case 'document': {
78
- const doc = composeDoc.composeDoc(this.options, this.directives, token, this.onError);
79
- this.decorate(doc, false);
80
- if (this.doc)
81
- this.onDocument(this.doc);
82
- this.doc = doc;
83
- this.atDirectives = false;
84
- break;
85
- }
86
- case 'byte-order-mark':
87
- case 'space':
88
- break;
89
- case 'comment':
90
- case 'newline':
91
- this.prelude.push(token.source);
92
- break;
93
- case 'error': {
94
- const msg = token.source
95
- ? `${token.message}: ${JSON.stringify(token.source)}`
96
- : token.message;
97
- const error = new errors.YAMLParseError(-1, msg);
98
- if (this.atDirectives || !this.doc)
99
- this.errors.push(error);
100
- else
101
- this.doc.errors.push(error);
102
- break;
103
- }
104
- case 'doc-end': {
105
- if (!this.doc) {
106
- const msg = 'Unexpected doc-end without preceding document';
107
- this.errors.push(new errors.YAMLParseError(token.offset, msg));
108
- break;
109
- }
110
- const end = resolveEnd.resolveEnd(token.end, token.offset + token.source.length, this.doc.options.strict, this.onError);
111
- this.decorate(this.doc, true);
112
- if (end.comment) {
113
- const dc = this.doc.comment;
114
- this.doc.comment = dc ? `${dc}\n${end.comment}` : end.comment;
115
- }
116
- this.doc.range[1] = end.offset;
117
- break;
118
- }
119
- default:
120
- this.errors.push(new errors.YAMLParseError(-1, `Unsupported token ${token.type}`));
121
- }
71
+ this.errors.push(new errors.YAMLParseError(pos, code, message));
122
72
  };
123
73
  this.directives = new directives.Directives({
124
- version: (options$1 === null || options$1 === void 0 ? void 0 : options$1.version) || options.defaultOptions.version
74
+ version: options$1.version || options.defaultOptions.version
125
75
  });
126
- this.onDocument = onDocument;
127
76
  this.options = options$1;
128
77
  }
129
78
  decorate(doc, afterDoc) {
@@ -138,7 +87,9 @@ class Composer {
138
87
  doc.commentBefore = comment;
139
88
  }
140
89
  else if (Node.isCollection(dc) && !dc.flow && dc.items.length > 0) {
141
- const it = dc.items[0];
90
+ let it = dc.items[0];
91
+ if (Node.isPair(it))
92
+ it = it.key;
142
93
  const cb = it.commentBefore;
143
94
  it.commentBefore = cb ? `${comment}\n${cb}` : comment;
144
95
  }
@@ -172,20 +123,99 @@ class Composer {
172
123
  warnings: this.warnings
173
124
  };
174
125
  }
175
- end(forceDoc = false, offset = -1) {
126
+ /**
127
+ * Compose tokens into documents.
128
+ *
129
+ * @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.
130
+ * @param endOffset - Should be set if `forceDoc` is also set, to set the document range end and to indicate errors correctly.
131
+ */
132
+ *compose(tokens, forceDoc = false, endOffset = -1) {
133
+ for (const token of tokens)
134
+ yield* this.next(token);
135
+ yield* this.end(forceDoc, endOffset);
136
+ }
137
+ /** Advance the composer by one CST token. */
138
+ *next(token) {
139
+ if (process.env.LOG_STREAM)
140
+ console.dir(token, { depth: null });
141
+ switch (token.type) {
142
+ case 'directive':
143
+ this.directives.add(token.source, (offset, message, warning) => {
144
+ const pos = getErrorPos(token);
145
+ pos[0] += offset;
146
+ this.onError(pos, 'BAD_DIRECTIVE', message, warning);
147
+ });
148
+ this.prelude.push(token.source);
149
+ this.atDirectives = true;
150
+ break;
151
+ case 'document': {
152
+ const doc = composeDoc.composeDoc(this.options, this.directives, token, this.onError);
153
+ if (this.atDirectives && !doc.directives.marker)
154
+ this.onError(token, 'MISSING_CHAR', 'Missing directives-end indicator line');
155
+ this.decorate(doc, false);
156
+ if (this.doc)
157
+ yield this.doc;
158
+ this.doc = doc;
159
+ this.atDirectives = false;
160
+ break;
161
+ }
162
+ case 'byte-order-mark':
163
+ case 'space':
164
+ break;
165
+ case 'comment':
166
+ case 'newline':
167
+ this.prelude.push(token.source);
168
+ break;
169
+ case 'error': {
170
+ const msg = token.source
171
+ ? `${token.message}: ${JSON.stringify(token.source)}`
172
+ : token.message;
173
+ const error = new errors.YAMLParseError(getErrorPos(token), 'UNEXPECTED_TOKEN', msg);
174
+ if (this.atDirectives || !this.doc)
175
+ this.errors.push(error);
176
+ else
177
+ this.doc.errors.push(error);
178
+ break;
179
+ }
180
+ case 'doc-end': {
181
+ if (!this.doc) {
182
+ const msg = 'Unexpected doc-end without preceding document';
183
+ this.errors.push(new errors.YAMLParseError(getErrorPos(token), 'UNEXPECTED_TOKEN', msg));
184
+ break;
185
+ }
186
+ const end = resolveEnd.resolveEnd(token.end, token.offset + token.source.length, this.doc.options.strict, this.onError);
187
+ this.decorate(this.doc, true);
188
+ if (end.comment) {
189
+ const dc = this.doc.comment;
190
+ this.doc.comment = dc ? `${dc}\n${end.comment}` : end.comment;
191
+ }
192
+ this.doc.range[2] = end.offset;
193
+ break;
194
+ }
195
+ default:
196
+ this.errors.push(new errors.YAMLParseError(getErrorPos(token), 'UNEXPECTED_TOKEN', `Unsupported token ${token.type}`));
197
+ }
198
+ }
199
+ /**
200
+ * Call at end of input to yield any remaining document.
201
+ *
202
+ * @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.
203
+ * @param endOffset - Should be set if `forceDoc` is also set, to set the document range end and to indicate errors correctly.
204
+ */
205
+ *end(forceDoc = false, endOffset = -1) {
176
206
  if (this.doc) {
177
207
  this.decorate(this.doc, true);
178
- this.onDocument(this.doc);
208
+ yield this.doc;
179
209
  this.doc = null;
180
210
  }
181
211
  else if (forceDoc) {
182
212
  const opts = Object.assign({ directives: this.directives }, this.options);
183
213
  const doc = new Document.Document(undefined, opts);
184
214
  if (this.atDirectives)
185
- this.onError(offset, 'Missing directives-end indicator line');
186
- doc.range = [0, offset];
215
+ this.onError(endOffset, 'MISSING_CHAR', 'Missing directives-end indicator line');
216
+ doc.range = [0, endOffset, endOffset];
187
217
  this.decorate(doc, false);
188
- this.onDocument(doc);
218
+ yield doc;
189
219
  }
190
220
  }
191
221
  }
@@ -1,5 +1,6 @@
1
- import type { Document } from '../doc/Document.js';
1
+ import type { ParsedNode } from '../nodes/Node.js';
2
2
  import { YAMLMap } from '../nodes/YAMLMap.js';
3
- import type { BlockMap } from '../parse/tokens.js';
4
- import type { ComposeNode } from './compose-node.js';
5
- export declare function resolveBlockMap({ composeNode, composeEmptyNode }: ComposeNode, doc: Document.Parsed, { indent, items, offset }: BlockMap, anchor: string | null, onError: (offset: number, message: string, warning?: boolean) => void): YAMLMap.Parsed<import("../index.js").ParsedNode, import("../index.js").ParsedNode | null>;
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>;
@@ -4,26 +4,31 @@ var Pair = require('../nodes/Pair.js');
4
4
  var YAMLMap = require('../nodes/YAMLMap.js');
5
5
  var resolveProps = require('./resolve-props.js');
6
6
  var utilContainsNewline = require('./util-contains-newline.js');
7
+ var utilMapIncludes = require('./util-map-includes.js');
7
8
 
8
9
  const startColMsg = 'All mapping items must start at the same column';
9
- function resolveBlockMap({ composeNode, composeEmptyNode }, doc, { indent, items, offset }, anchor, onError) {
10
+ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
10
11
  var _a;
11
- const start = offset;
12
- const map = new YAMLMap.YAMLMap(doc.schema);
13
- if (anchor)
14
- doc.anchors.setAnchor(map, anchor);
15
- for (const { start, key, sep, value } of items) {
12
+ const map = new YAMLMap.YAMLMap(ctx.schema);
13
+ let offset = bm.offset;
14
+ for (const { start, key, sep, value } of bm.items) {
16
15
  // key properties
17
- const keyProps = resolveProps.resolveProps(doc, start, true, 'explicit-key-ind', offset, onError);
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
+ });
18
23
  const implicitKey = !keyProps.found;
19
24
  if (implicitKey) {
20
25
  if (key) {
21
26
  if (key.type === 'block-seq')
22
- onError(offset, 'A block sequence may not be used as an implicit map key');
23
- else if ('indent' in key && key.indent !== indent)
24
- onError(offset, startColMsg);
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);
25
30
  }
26
- if (!keyProps.anchor && !keyProps.tagName && !sep) {
31
+ if (!keyProps.anchor && !keyProps.tag && !sep) {
27
32
  // TODO: assert being at last item?
28
33
  if (keyProps.comment) {
29
34
  if (map.comment)
@@ -34,39 +39,46 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, doc, { indent, items
34
39
  continue;
35
40
  }
36
41
  }
37
- else if (((_a = keyProps.found) === null || _a === void 0 ? void 0 : _a.indent) !== indent)
38
- onError(offset, startColMsg);
39
- offset += keyProps.length;
42
+ else if (((_a = keyProps.found) === null || _a === void 0 ? void 0 : _a.indent) !== bm.indent)
43
+ onError(offset, 'BAD_INDENT', startColMsg);
40
44
  if (implicitKey && utilContainsNewline.containsNewline(key))
41
- onError(offset, 'Implicit keys need to be on a single line');
45
+ onError(key, // checked by containsNewline()
46
+ 'MULTILINE_IMPLICIT_KEY', 'Implicit keys need to be on a single line');
42
47
  // key value
43
- const keyStart = offset;
48
+ const keyStart = keyProps.end;
44
49
  const keyNode = key
45
- ? composeNode(doc, key, keyProps, onError)
46
- : composeEmptyNode(doc, offset, start, null, keyProps, onError);
47
- offset = keyNode.range[1];
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');
48
54
  // value properties
49
- const valueProps = resolveProps.resolveProps(doc, sep || [], !key || key.type === 'block-scalar', 'map-value-ind', offset, onError);
50
- offset += valueProps.length;
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;
51
63
  if (valueProps.found) {
52
64
  if (implicitKey) {
53
65
  if ((value === null || value === void 0 ? void 0 : value.type) === 'block-map' && !valueProps.hasNewline)
54
- onError(offset, 'Nested mappings are not allowed in compact mappings');
55
- if (doc.options.strict &&
66
+ onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'Nested mappings are not allowed in compact mappings');
67
+ if (ctx.options.strict &&
56
68
  keyProps.start < valueProps.found.offset - 1024)
57
- onError(offset, 'The : indicator must be at most 1024 chars after the start of an implicit block mapping key');
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');
58
70
  }
59
71
  // value value
60
72
  const valueNode = value
61
- ? composeNode(doc, value, valueProps, onError)
62
- : composeEmptyNode(doc, offset, sep, null, valueProps, onError);
63
- offset = valueNode.range[1];
73
+ ? composeNode(ctx, value, valueProps, onError)
74
+ : composeEmptyNode(ctx, offset, sep, null, valueProps, onError);
75
+ offset = valueNode.range[2];
64
76
  map.items.push(new Pair.Pair(keyNode, valueNode));
65
77
  }
66
78
  else {
67
79
  // key with no value
68
80
  if (implicitKey)
69
- onError(keyStart, 'Implicit map keys need to be followed by map values');
81
+ onError(keyNode.range, 'MISSING_CHAR', 'Implicit map keys need to be followed by map values');
70
82
  if (valueProps.comment) {
71
83
  if (keyNode.comment)
72
84
  keyNode.comment += '\n' + valueProps.comment;
@@ -76,7 +88,7 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, doc, { indent, items
76
88
  map.items.push(new Pair.Pair(keyNode));
77
89
  }
78
90
  }
79
- map.range = [start, offset];
91
+ map.range = [bm.offset, offset, offset];
80
92
  return map;
81
93
  }
82
94
 
@@ -1,8 +1,10 @@
1
+ import { Range } from '../nodes/Node.js';
1
2
  import { Scalar } from '../nodes/Scalar.js';
2
- import type { BlockScalar } from '../parse/tokens.js';
3
- export declare function resolveBlockScalar(scalar: BlockScalar, strict: boolean, onError: (offset: number, message: string) => void): {
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): {
4
6
  value: string;
5
7
  type: Scalar.BLOCK_FOLDED | Scalar.BLOCK_LITERAL | null;
6
8
  comment: string;
7
- length: number;
9
+ range: Range;
8
10
  };