yaml 2.0.0-4 → 2.0.0-5

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 (210) 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 +20 -17
  5. package/browser/dist/compose/compose-scalar.js +20 -23
  6. package/browser/dist/compose/composer.js +105 -76
  7. package/browser/dist/compose/resolve-block-map.js +38 -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 +6 -7
  11. package/browser/dist/compose/resolve-flow-collection.js +153 -199
  12. package/browser/dist/compose/resolve-flow-scalar.js +49 -25
  13. package/browser/dist/compose/resolve-props.js +47 -35
  14. package/browser/dist/compose/util-contains-newline.js +15 -12
  15. package/browser/dist/doc/Document.js +50 -99
  16. package/browser/dist/doc/anchors.js +72 -0
  17. package/browser/dist/doc/createNode.js +27 -17
  18. package/browser/dist/errors.js +16 -12
  19. package/browser/dist/index.js +3 -3
  20. package/browser/dist/nodes/Alias.js +51 -28
  21. package/browser/dist/nodes/Collection.js +5 -83
  22. package/browser/dist/nodes/Node.js +2 -1
  23. package/browser/dist/nodes/Pair.js +9 -232
  24. package/browser/dist/nodes/YAMLMap.js +8 -4
  25. package/browser/dist/nodes/YAMLSeq.js +7 -4
  26. package/browser/dist/nodes/addPairToJSMap.js +103 -0
  27. package/browser/dist/nodes/toJS.js +12 -10
  28. package/browser/dist/options.js +0 -2
  29. package/browser/dist/parse/cst-scalar.js +219 -0
  30. package/browser/dist/parse/cst-stringify.js +61 -0
  31. package/browser/dist/parse/cst-visit.js +97 -0
  32. package/{dist/parse/tokens.js → browser/dist/parse/cst.js} +12 -7
  33. package/browser/dist/parse/lexer.js +99 -105
  34. package/browser/dist/parse/parser.js +219 -144
  35. package/browser/dist/public-api.js +11 -14
  36. package/browser/dist/schema/Schema.js +23 -0
  37. package/browser/dist/{tags/failsafe → schema/common}/map.js +0 -0
  38. package/browser/dist/schema/common/null.js +13 -0
  39. package/browser/dist/{tags/failsafe → schema/common}/seq.js +1 -1
  40. package/browser/dist/{tags/failsafe → schema/common}/string.js +0 -0
  41. package/browser/dist/schema/core/bool.js +19 -0
  42. package/browser/dist/schema/core/float.js +40 -0
  43. package/browser/dist/schema/core/int.js +38 -0
  44. package/browser/dist/schema/core/schema.js +23 -0
  45. package/browser/dist/{tags/json.js → schema/json/schema.js} +5 -6
  46. package/browser/dist/schema/tags.js +79 -0
  47. package/browser/dist/{tags → schema}/yaml-1.1/binary.js +0 -0
  48. package/browser/dist/schema/yaml-1.1/bool.js +26 -0
  49. package/browser/dist/schema/yaml-1.1/float.js +43 -0
  50. package/browser/dist/schema/yaml-1.1/int.js +71 -0
  51. package/browser/dist/{tags → schema}/yaml-1.1/omap.js +0 -0
  52. package/browser/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
  53. package/browser/dist/schema/yaml-1.1/schema.js +37 -0
  54. package/browser/dist/{tags → schema}/yaml-1.1/set.js +0 -0
  55. package/browser/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
  56. package/browser/dist/stringify/addComment.js +1 -7
  57. package/browser/dist/stringify/stringify.js +8 -6
  58. package/browser/dist/stringify/stringifyCollection.js +108 -0
  59. package/browser/dist/stringify/stringifyDocument.js +61 -0
  60. package/browser/dist/stringify/stringifyPair.js +113 -0
  61. package/browser/dist/stringify/stringifyString.js +53 -52
  62. package/browser/dist/visit.js +20 -1
  63. package/dist/compose/compose-collection.d.ts +4 -4
  64. package/dist/compose/compose-collection.js +12 -9
  65. package/dist/compose/compose-doc.d.ts +4 -3
  66. package/dist/compose/compose-doc.js +23 -6
  67. package/dist/compose/compose-node.d.ts +15 -7
  68. package/dist/compose/compose-node.js +20 -17
  69. package/dist/compose/compose-scalar.d.ts +4 -3
  70. package/dist/compose/compose-scalar.js +19 -22
  71. package/dist/compose/composer.d.ts +28 -23
  72. package/dist/compose/composer.js +106 -77
  73. package/dist/compose/resolve-block-map.d.ts +4 -4
  74. package/dist/compose/resolve-block-map.js +38 -29
  75. package/dist/compose/resolve-block-scalar.d.ts +5 -3
  76. package/dist/compose/resolve-block-scalar.js +14 -17
  77. package/dist/compose/resolve-block-seq.d.ts +4 -4
  78. package/dist/compose/resolve-block-seq.js +21 -17
  79. package/dist/compose/resolve-end.d.ts +3 -2
  80. package/dist/compose/resolve-end.js +6 -7
  81. package/dist/compose/resolve-flow-collection.d.ts +4 -5
  82. package/dist/compose/resolve-flow-collection.js +152 -198
  83. package/dist/compose/resolve-flow-scalar.d.ts +5 -3
  84. package/dist/compose/resolve-flow-scalar.js +49 -25
  85. package/dist/compose/resolve-props.d.ts +17 -10
  86. package/dist/compose/resolve-props.js +47 -35
  87. package/dist/compose/util-contains-newline.d.ts +1 -1
  88. package/dist/compose/util-contains-newline.js +15 -12
  89. package/dist/compose/util-empty-scalar-position.d.ts +1 -1
  90. package/dist/doc/Document.d.ts +27 -15
  91. package/dist/doc/Document.js +48 -97
  92. package/dist/doc/anchors.d.ts +24 -0
  93. package/dist/doc/anchors.js +77 -0
  94. package/dist/doc/createNode.d.ts +8 -10
  95. package/dist/doc/createNode.js +26 -16
  96. package/dist/errors.d.ts +11 -8
  97. package/dist/errors.js +16 -12
  98. package/dist/index.d.ts +5 -3
  99. package/dist/index.js +3 -3
  100. package/dist/nodes/Alias.d.ts +19 -9
  101. package/dist/nodes/Alias.js +51 -28
  102. package/dist/nodes/Collection.d.ts +3 -17
  103. package/dist/nodes/Collection.js +4 -82
  104. package/dist/nodes/Node.d.ts +6 -4
  105. package/dist/nodes/Node.js +2 -0
  106. package/dist/nodes/Pair.d.ts +6 -11
  107. package/dist/nodes/Pair.js +8 -231
  108. package/dist/nodes/Scalar.d.ts +6 -3
  109. package/dist/nodes/YAMLMap.d.ts +3 -3
  110. package/dist/nodes/YAMLMap.js +8 -4
  111. package/dist/nodes/YAMLSeq.d.ts +3 -3
  112. package/dist/nodes/YAMLSeq.js +7 -4
  113. package/dist/nodes/addPairToJSMap.d.ts +3 -0
  114. package/dist/nodes/addPairToJSMap.js +105 -0
  115. package/dist/nodes/toJS.d.ts +4 -5
  116. package/dist/nodes/toJS.js +12 -10
  117. package/dist/options.d.ts +20 -26
  118. package/dist/options.js +0 -2
  119. package/dist/parse/cst-scalar.d.ts +58 -0
  120. package/dist/parse/cst-scalar.js +223 -0
  121. package/dist/parse/cst-stringify.d.ts +8 -0
  122. package/dist/parse/cst-stringify.js +63 -0
  123. package/dist/parse/cst-visit.d.ts +39 -0
  124. package/dist/parse/cst-visit.js +99 -0
  125. package/dist/parse/{tokens.d.ts → cst.d.ts} +23 -7
  126. package/{browser/dist/parse/tokens.js → dist/parse/cst.js} +27 -1
  127. package/dist/parse/lexer.d.ts +4 -13
  128. package/dist/parse/lexer.js +100 -106
  129. package/dist/parse/parser.d.ts +30 -24
  130. package/dist/parse/parser.js +221 -146
  131. package/dist/public-api.d.ts +7 -7
  132. package/dist/public-api.js +11 -14
  133. package/dist/{doc → schema}/Schema.d.ts +5 -3
  134. package/dist/schema/Schema.js +25 -0
  135. package/dist/{tags/failsafe → schema/common}/map.d.ts +0 -0
  136. package/dist/{tags/failsafe → schema/common}/map.js +0 -0
  137. package/dist/schema/common/null.d.ts +4 -0
  138. package/dist/schema/common/null.js +15 -0
  139. package/dist/{tags/failsafe → schema/common}/seq.d.ts +0 -0
  140. package/dist/{tags/failsafe → schema/common}/seq.js +1 -1
  141. package/dist/{tags/failsafe → schema/common}/string.d.ts +0 -0
  142. package/dist/{tags/failsafe → schema/common}/string.js +0 -0
  143. package/dist/schema/core/bool.d.ts +4 -0
  144. package/dist/schema/core/bool.js +21 -0
  145. package/dist/schema/core/float.d.ts +4 -0
  146. package/dist/schema/core/float.js +44 -0
  147. package/dist/schema/core/int.d.ts +4 -0
  148. package/dist/schema/core/int.js +42 -0
  149. package/dist/schema/core/schema.d.ts +1 -0
  150. package/dist/schema/core/schema.js +25 -0
  151. package/dist/schema/json/schema.d.ts +2 -0
  152. package/dist/{tags/json.js → schema/json/schema.js} +5 -6
  153. package/dist/schema/tags.d.ts +41 -0
  154. package/dist/schema/tags.js +82 -0
  155. package/dist/{tags → schema}/types.d.ts +3 -7
  156. package/dist/{tags → schema}/yaml-1.1/binary.d.ts +0 -0
  157. package/dist/{tags → schema}/yaml-1.1/binary.js +0 -0
  158. package/dist/schema/yaml-1.1/bool.d.ts +7 -0
  159. package/dist/schema/yaml-1.1/bool.js +29 -0
  160. package/dist/schema/yaml-1.1/float.d.ts +4 -0
  161. package/dist/schema/yaml-1.1/float.js +47 -0
  162. package/dist/schema/yaml-1.1/int.d.ts +5 -0
  163. package/dist/schema/yaml-1.1/int.js +76 -0
  164. package/dist/{tags → schema}/yaml-1.1/omap.d.ts +0 -0
  165. package/dist/{tags → schema}/yaml-1.1/omap.js +0 -0
  166. package/dist/{tags → schema}/yaml-1.1/pairs.d.ts +3 -2
  167. package/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
  168. package/dist/schema/yaml-1.1/schema.d.ts +1 -0
  169. package/dist/schema/yaml-1.1/schema.js +39 -0
  170. package/dist/{tags → schema}/yaml-1.1/set.d.ts +1 -1
  171. package/dist/{tags → schema}/yaml-1.1/set.js +0 -0
  172. package/dist/{tags → schema}/yaml-1.1/timestamp.d.ts +0 -0
  173. package/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
  174. package/dist/stringify/addComment.js +0 -7
  175. package/dist/stringify/stringify.d.ts +1 -2
  176. package/dist/stringify/stringify.js +8 -6
  177. package/dist/stringify/stringifyCollection.d.ts +21 -0
  178. package/dist/stringify/stringifyCollection.js +110 -0
  179. package/dist/stringify/stringifyDocument.d.ts +3 -0
  180. package/dist/stringify/stringifyDocument.js +63 -0
  181. package/dist/stringify/stringifyPair.d.ts +3 -0
  182. package/dist/stringify/stringifyPair.js +115 -0
  183. package/dist/stringify/stringifyString.js +53 -52
  184. package/dist/test-events.d.ts +1 -2
  185. package/dist/test-events.js +25 -12
  186. package/dist/util.d.ts +1 -1
  187. package/dist/visit.d.ts +10 -1
  188. package/dist/visit.js +20 -1
  189. package/package.json +4 -3
  190. package/browser/dist/doc/Anchors.js +0 -102
  191. package/browser/dist/doc/Schema.js +0 -27
  192. package/browser/dist/doc/getSchemaTags.js +0 -30
  193. package/browser/dist/tags/core.js +0 -109
  194. package/browser/dist/tags/failsafe/index.js +0 -7
  195. package/browser/dist/tags/index.js +0 -34
  196. package/browser/dist/tags/yaml-1.1/index.js +0 -152
  197. package/dist/doc/Anchors.d.ts +0 -36
  198. package/dist/doc/Anchors.js +0 -104
  199. package/dist/doc/Schema.js +0 -29
  200. package/dist/doc/getSchemaTags.d.ts +0 -3
  201. package/dist/doc/getSchemaTags.js +0 -32
  202. package/dist/tags/core.d.ts +0 -14
  203. package/dist/tags/core.js +0 -119
  204. package/dist/tags/failsafe/index.d.ts +0 -1
  205. package/dist/tags/failsafe/index.js +0 -9
  206. package/dist/tags/index.d.ts +0 -31
  207. package/dist/tags/index.js +0 -37
  208. package/dist/tags/json.d.ts +0 -2
  209. package/dist/tags/yaml-1.1/index.d.ts +0 -2
  210. package/dist/tags/yaml-1.1/index.js +0 -154
@@ -5,44 +5,41 @@ 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
+ onError(tagToken || token, 'TAG_RESOLVE_FAILED', error.message);
25
25
  scalar = new Scalar.Scalar(value);
26
26
  }
27
- scalar.range = [offset, offset + length];
27
+ scalar.range = range;
28
28
  scalar.source = value;
29
29
  if (type)
30
30
  scalar.type = type;
31
31
  if (tagName)
32
32
  scalar.tag = tagName;
33
- if (tag === null || tag === void 0 ? void 0 : tag.format)
33
+ if (tag.format)
34
34
  scalar.format = tag.format;
35
35
  if (comment)
36
36
  scalar.comment = comment;
37
- if (anchor)
38
- doc.anchors.setAnchor(scalar, anchor);
39
37
  return scalar;
40
38
  }
41
- const defaultScalarTag = (schema) => schema.tags.find(tag => !tag.collection && tag.tag === 'tag:yaml.org,2002:str');
42
- function findScalarTagByName(schema, value, tagName, onError) {
39
+ function findScalarTagByName(schema, value, tagName, tagToken, onError) {
43
40
  var _a;
44
41
  if (tagName === '!')
45
- return defaultScalarTag(schema); // non-specific tag
42
+ return schema[Node.SCALAR]; // non-specific tag
46
43
  const matchWithTest = [];
47
44
  for (const tag of schema.tags) {
48
45
  if (!tag.collection && tag.tag === tagName) {
@@ -62,8 +59,8 @@ function findScalarTagByName(schema, value, tagName, onError) {
62
59
  schema.tags.push(Object.assign({}, kt, { default: false, test: undefined }));
63
60
  return kt;
64
61
  }
65
- onError(0, `Unresolved tag: ${tagName}`, tagName !== 'tag:yaml.org,2002:str');
66
- return defaultScalarTag(schema);
62
+ onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, tagName !== 'tag:yaml.org,2002:str');
63
+ return schema[Node.SCALAR];
67
64
  }
68
65
  function findScalarTagByTest(schema, value, apply) {
69
66
  var _a;
@@ -73,7 +70,7 @@ function findScalarTagByTest(schema, value, apply) {
73
70
  return tag;
74
71
  }
75
72
  }
76
- return defaultScalarTag(schema);
73
+ return schema[Node.SCALAR];
77
74
  }
78
75
 
79
76
  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,6 +8,14 @@ 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) {
12
20
  let comment = '';
13
21
  let atComment = false;
@@ -40,90 +48,30 @@ function parsePrelude(prelude) {
40
48
  * Compose a stream of CST nodes into a stream of YAML Documents.
41
49
  *
42
50
  * ```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()
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)
49
56
  * ```
50
57
  */
51
58
  class Composer {
52
- constructor(onDocument, options$1 = {}) {
59
+ constructor(options$1 = {}) {
53
60
  this.doc = null;
54
61
  this.atDirectives = false;
55
62
  this.prelude = [];
56
63
  this.errors = [];
57
64
  this.warnings = [];
58
- this.onError = (offset, message, warning) => {
65
+ this.onError = (source, code, message, warning) => {
66
+ const pos = getErrorPos(source);
59
67
  if (warning)
60
- this.warnings.push(new errors.YAMLWarning(offset, message));
68
+ this.warnings.push(new errors.YAMLWarning(pos, code, message));
61
69
  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
- }
70
+ this.errors.push(new errors.YAMLParseError(pos, code, message));
122
71
  };
123
72
  this.directives = new directives.Directives({
124
- version: (options$1 === null || options$1 === void 0 ? void 0 : options$1.version) || options.defaultOptions.version
73
+ version: options$1.version || options.defaultOptions.version
125
74
  });
126
- this.onDocument = onDocument;
127
75
  this.options = options$1;
128
76
  }
129
77
  decorate(doc, afterDoc) {
@@ -138,7 +86,9 @@ class Composer {
138
86
  doc.commentBefore = comment;
139
87
  }
140
88
  else if (Node.isCollection(dc) && !dc.flow && dc.items.length > 0) {
141
- const it = dc.items[0];
89
+ let it = dc.items[0];
90
+ if (Node.isPair(it))
91
+ it = it.key;
142
92
  const cb = it.commentBefore;
143
93
  it.commentBefore = cb ? `${comment}\n${cb}` : comment;
144
94
  }
@@ -172,20 +122,99 @@ class Composer {
172
122
  warnings: this.warnings
173
123
  };
174
124
  }
175
- end(forceDoc = false, offset = -1) {
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) {
176
205
  if (this.doc) {
177
206
  this.decorate(this.doc, true);
178
- this.onDocument(this.doc);
207
+ yield this.doc;
179
208
  this.doc = null;
180
209
  }
181
210
  else if (forceDoc) {
182
211
  const opts = Object.assign({ directives: this.directives }, this.options);
183
212
  const doc = new Document.Document(undefined, opts);
184
213
  if (this.atDirectives)
185
- this.onError(offset, 'Missing directives-end indicator line');
186
- doc.range = [0, offset];
214
+ this.onError(endOffset, 'MISSING_CHAR', 'Missing directives-end indicator line');
215
+ doc.range = [0, endOffset, endOffset];
187
216
  this.decorate(doc, false);
188
- this.onDocument(doc);
217
+ yield doc;
189
218
  }
190
219
  }
191
220
  }
@@ -1,5 +1,5 @@
1
- import type { Document } from '../doc/Document.js';
2
1
  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>;
2
+ import type { BlockMap } from '../parse/cst.js';
3
+ import type { ComposeContext, ComposeNode } from './compose-node.js';
4
+ import type { ComposeErrorHandler } from './composer.js';
5
+ export declare function resolveBlockMap({ composeNode, composeEmptyNode }: ComposeNode, ctx: ComposeContext, bm: BlockMap, onError: ComposeErrorHandler): YAMLMap.Parsed<import("../index.js").ParsedNode, import("../index.js").ParsedNode | null>;
@@ -6,24 +6,28 @@ var resolveProps = require('./resolve-props.js');
6
6
  var utilContainsNewline = require('./util-contains-newline.js');
7
7
 
8
8
  const startColMsg = 'All mapping items must start at the same column';
9
- function resolveBlockMap({ composeNode, composeEmptyNode }, doc, { indent, items, offset }, anchor, onError) {
9
+ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
10
10
  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) {
11
+ const map = new YAMLMap.YAMLMap(ctx.schema);
12
+ let offset = bm.offset;
13
+ for (const { start, key, sep, value } of bm.items) {
16
14
  // key properties
17
- const keyProps = resolveProps.resolveProps(doc, start, true, 'explicit-key-ind', offset, onError);
15
+ const keyProps = resolveProps.resolveProps(start, {
16
+ ctx,
17
+ indicator: 'explicit-key-ind',
18
+ offset,
19
+ onError,
20
+ startOnNewline: true
21
+ });
18
22
  const implicitKey = !keyProps.found;
19
23
  if (implicitKey) {
20
24
  if (key) {
21
25
  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);
26
+ onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'A block sequence may not be used as an implicit map key');
27
+ else if ('indent' in key && key.indent !== bm.indent)
28
+ onError(offset, 'BAD_INDENT', startColMsg);
25
29
  }
26
- if (!keyProps.anchor && !keyProps.tagName && !sep) {
30
+ if (!keyProps.anchor && !keyProps.tag && !sep) {
27
31
  // TODO: assert being at last item?
28
32
  if (keyProps.comment) {
29
33
  if (map.comment)
@@ -34,39 +38,44 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, doc, { indent, items
34
38
  continue;
35
39
  }
36
40
  }
37
- else if (((_a = keyProps.found) === null || _a === void 0 ? void 0 : _a.indent) !== indent)
38
- onError(offset, startColMsg);
39
- offset += keyProps.length;
41
+ else if (((_a = keyProps.found) === null || _a === void 0 ? void 0 : _a.indent) !== bm.indent)
42
+ onError(offset, 'BAD_INDENT', startColMsg);
40
43
  if (implicitKey && utilContainsNewline.containsNewline(key))
41
- onError(offset, 'Implicit keys need to be on a single line');
44
+ onError(key, // checked by containsNewline()
45
+ 'MULTILINE_IMPLICIT_KEY', 'Implicit keys need to be on a single line');
42
46
  // key value
43
- const keyStart = offset;
47
+ const keyStart = keyProps.end;
44
48
  const keyNode = key
45
- ? composeNode(doc, key, keyProps, onError)
46
- : composeEmptyNode(doc, offset, start, null, keyProps, onError);
47
- offset = keyNode.range[1];
49
+ ? composeNode(ctx, key, keyProps, onError)
50
+ : composeEmptyNode(ctx, keyStart, start, null, keyProps, onError);
48
51
  // value properties
49
- const valueProps = resolveProps.resolveProps(doc, sep || [], !key || key.type === 'block-scalar', 'map-value-ind', offset, onError);
50
- offset += valueProps.length;
52
+ const valueProps = resolveProps.resolveProps(sep || [], {
53
+ ctx,
54
+ indicator: 'map-value-ind',
55
+ offset: keyNode.range[2],
56
+ onError,
57
+ startOnNewline: !key || key.type === 'block-scalar'
58
+ });
59
+ offset = valueProps.end;
51
60
  if (valueProps.found) {
52
61
  if (implicitKey) {
53
62
  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 &&
63
+ onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'Nested mappings are not allowed in compact mappings');
64
+ if (ctx.options.strict &&
56
65
  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');
66
+ 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
67
  }
59
68
  // value value
60
69
  const valueNode = value
61
- ? composeNode(doc, value, valueProps, onError)
62
- : composeEmptyNode(doc, offset, sep, null, valueProps, onError);
63
- offset = valueNode.range[1];
70
+ ? composeNode(ctx, value, valueProps, onError)
71
+ : composeEmptyNode(ctx, offset, sep, null, valueProps, onError);
72
+ offset = valueNode.range[2];
64
73
  map.items.push(new Pair.Pair(keyNode, valueNode));
65
74
  }
66
75
  else {
67
76
  // key with no value
68
77
  if (implicitKey)
69
- onError(keyStart, 'Implicit map keys need to be followed by map values');
78
+ onError(keyNode.range, 'MISSING_CHAR', 'Implicit map keys need to be followed by map values');
70
79
  if (valueProps.comment) {
71
80
  if (keyNode.comment)
72
81
  keyNode.comment += '\n' + valueProps.comment;
@@ -76,7 +85,7 @@ function resolveBlockMap({ composeNode, composeEmptyNode }, doc, { indent, items
76
85
  map.items.push(new Pair.Pair(keyNode));
77
86
  }
78
87
  }
79
- map.range = [start, offset];
88
+ map.range = [bm.offset, offset, offset];
80
89
  return map;
81
90
  }
82
91
 
@@ -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
  };
@@ -3,9 +3,10 @@
3
3
  var Scalar = require('../nodes/Scalar.js');
4
4
 
5
5
  function resolveBlockScalar(scalar, strict, onError) {
6
+ const start = scalar.offset;
6
7
  const header = parseBlockScalarHeader(scalar, strict, onError);
7
8
  if (!header)
8
- return { value: '', type: null, comment: '', length: 0 };
9
+ return { value: '', type: null, comment: '', range: [start, start, start] };
9
10
  const type = header.mode === '>' ? Scalar.Scalar.BLOCK_FOLDED : Scalar.Scalar.BLOCK_LITERAL;
10
11
  const lines = scalar.source ? splitLines(scalar.source) : [];
11
12
  // determine the end of content & start of chomping
@@ -20,10 +21,10 @@ function resolveBlockScalar(scalar, strict, onError) {
20
21
  // shortcut for empty contents
21
22
  if (!scalar.source || chompStart === 0) {
22
23
  const value = header.chomp === '+' ? lines.map(line => line[0]).join('\n') : '';
23
- let length = header.length;
24
+ let end = start + header.length;
24
25
  if (scalar.source)
25
- length += scalar.source.length;
26
- return { value, type, comment: header.comment, length };
26
+ end += scalar.source.length;
27
+ return { value, type, comment: header.comment, range: [start, end, end] };
27
28
  }
28
29
  // find the indentation level to trim from start
29
30
  let trimIndent = scalar.indent + header.indent;
@@ -38,7 +39,7 @@ function resolveBlockScalar(scalar, strict, onError) {
38
39
  else {
39
40
  if (indent.length < trimIndent) {
40
41
  const message = 'Block scalars with more-indented leading empty lines must use an explicit indentation indicator';
41
- onError(offset + indent.length, message);
42
+ onError(offset + indent.length, 'MISSING_CHAR', message);
42
43
  }
43
44
  if (header.indent === 0)
44
45
  trimIndent = indent.length;
@@ -65,7 +66,7 @@ function resolveBlockScalar(scalar, strict, onError) {
65
66
  ? 'explicit indentation indicator'
66
67
  : 'first line';
67
68
  const message = `Block scalar lines must not be less indented than their ${src}`;
68
- onError(offset - content.length - (crlf ? 2 : 1), message);
69
+ onError(offset - content.length - (crlf ? 2 : 1), 'BAD_INDENT', message);
69
70
  indent = '';
70
71
  }
71
72
  if (type === Scalar.Scalar.BLOCK_LITERAL) {
@@ -107,17 +108,13 @@ function resolveBlockScalar(scalar, strict, onError) {
107
108
  default:
108
109
  value += '\n';
109
110
  }
110
- return {
111
- value,
112
- type,
113
- comment: header.comment,
114
- length: header.length + scalar.source.length
115
- };
111
+ const end = start + header.length + scalar.source.length;
112
+ return { value, type, comment: header.comment, range: [start, end, end] };
116
113
  }
117
114
  function parseBlockScalarHeader({ offset, props }, strict, onError) {
118
115
  /* istanbul ignore if should not happen */
119
116
  if (props[0].type !== 'block-scalar-header') {
120
- onError(offset, 'Block scalar header not found');
117
+ onError(props[0], 'IMPOSSIBLE', 'Block scalar header not found');
121
118
  return null;
122
119
  }
123
120
  const { source } = props[0];
@@ -138,7 +135,7 @@ function parseBlockScalarHeader({ offset, props }, strict, onError) {
138
135
  }
139
136
  }
140
137
  if (error !== -1)
141
- onError(error, `Block scalar header includes extra characters: ${source}`);
138
+ onError(error, 'UNEXPECTED_TOKEN', `Block scalar header includes extra characters: ${source}`);
142
139
  let hasSpace = false;
143
140
  let comment = '';
144
141
  let length = source.length;
@@ -154,19 +151,19 @@ function parseBlockScalarHeader({ offset, props }, strict, onError) {
154
151
  case 'comment':
155
152
  if (strict && !hasSpace) {
156
153
  const message = 'Comments must be separated from other tokens by white space characters';
157
- onError(offset + length, message);
154
+ onError(token, 'COMMENT_SPACE', message);
158
155
  }
159
156
  length += token.source.length;
160
157
  comment = token.source.substring(1);
161
158
  break;
162
159
  case 'error':
163
- onError(offset + length, token.message);
160
+ onError(token, 'UNEXPECTED_TOKEN', token.message);
164
161
  length += token.source.length;
165
162
  break;
166
163
  /* istanbul ignore next should not happen */
167
164
  default: {
168
165
  const message = `Unexpected token in block scalar header: ${token.type}`;
169
- onError(offset + length, message);
166
+ onError(token, 'UNEXPECTED_TOKEN', message);
170
167
  const ts = token.source;
171
168
  if (ts && typeof ts === 'string')
172
169
  length += ts.length;
@@ -1,5 +1,5 @@
1
- import type { Document } from '../doc/Document.js';
2
1
  import { YAMLSeq } from '../nodes/YAMLSeq.js';
3
- import type { BlockSequence } from '../parse/tokens.js';
4
- import type { ComposeNode } from './compose-node.js';
5
- export declare function resolveBlockSeq({ composeNode, composeEmptyNode }: ComposeNode, doc: Document.Parsed, { items, offset }: BlockSequence, anchor: string | null, onError: (offset: number, message: string, warning?: boolean) => void): YAMLSeq.Parsed<import("../index.js").ParsedNode>;
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>;