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
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var tokens = require('./tokens.js');
3
+ var cst = require('./cst.js');
4
4
 
5
5
  /*
6
6
  START -> stream
@@ -100,13 +100,7 @@ const isNotIdentifierChar = (ch) => !ch || invalidIdentifierChars.includes(ch);
100
100
  * - `\u{FEFF}` (Byte order mark): Emitted separately outside documents
101
101
  */
102
102
  class Lexer {
103
- /**
104
- * Define/initialise a YAML lexer. `push` will be called separately with each
105
- * token when `lex()` is passed an input string.
106
- *
107
- * @public
108
- */
109
- constructor(push) {
103
+ constructor() {
110
104
  /**
111
105
  * Flag indicating whether the end of the current buffer marks the end of
112
106
  * all input
@@ -144,22 +138,20 @@ class Lexer {
144
138
  this.next = null;
145
139
  /** A pointer to `buffer`; the current position of the lexer. */
146
140
  this.pos = 0;
147
- this.push = push;
148
141
  }
149
142
  /**
150
- * Read YAML tokens from the `source` string, calling the callback
151
- * defined in the constructor for each one. If `incomplete`, a part
152
- * of the last line may be left as a buffer for the next call.
143
+ * Generate YAML tokens from the `source` string. If `incomplete`,
144
+ * a part of the last line may be left as a buffer for the next call.
153
145
  *
154
- * @public
146
+ * @returns A generator of lexical tokens
155
147
  */
156
- lex(source, incomplete) {
148
+ *lex(source, incomplete = false) {
157
149
  if (source)
158
150
  this.buffer = this.buffer ? this.buffer + source : source;
159
151
  this.atEnd = !incomplete;
160
152
  let next = this.next || 'stream';
161
153
  while (next && (incomplete || this.hasChars(1)))
162
- next = this.parseNext(next);
154
+ next = yield* this.parseNext(next);
163
155
  }
164
156
  atLineEnd() {
165
157
  let i = this.pos;
@@ -217,32 +209,32 @@ class Lexer {
217
209
  peek(n) {
218
210
  return this.buffer.substr(this.pos, n);
219
211
  }
220
- parseNext(next) {
212
+ *parseNext(next) {
221
213
  switch (next) {
222
214
  case 'stream':
223
- return this.parseStream();
215
+ return yield* this.parseStream();
224
216
  case 'line-start':
225
- return this.parseLineStart();
217
+ return yield* this.parseLineStart();
226
218
  case 'block-start':
227
- return this.parseBlockStart();
219
+ return yield* this.parseBlockStart();
228
220
  case 'doc':
229
- return this.parseDocument();
221
+ return yield* this.parseDocument();
230
222
  case 'flow':
231
- return this.parseFlowCollection();
223
+ return yield* this.parseFlowCollection();
232
224
  case 'quoted-scalar':
233
- return this.parseQuotedScalar();
225
+ return yield* this.parseQuotedScalar();
234
226
  case 'block-scalar':
235
- return this.parseBlockScalar();
227
+ return yield* this.parseBlockScalar();
236
228
  case 'plain-scalar':
237
- return this.parsePlainScalar();
229
+ return yield* this.parsePlainScalar();
238
230
  }
239
231
  }
240
- parseStream() {
232
+ *parseStream() {
241
233
  let line = this.getLine();
242
234
  if (line === null)
243
235
  return this.setNext('stream');
244
- if (line[0] === tokens.BOM) {
245
- this.pushCount(1);
236
+ if (line[0] === cst.BOM) {
237
+ yield* this.pushCount(1);
246
238
  line = line.substring(1);
247
239
  }
248
240
  if (line[0] === '%') {
@@ -260,21 +252,21 @@ class Lexer {
260
252
  else
261
253
  break;
262
254
  }
263
- const n = this.pushCount(dirEnd) + this.pushSpaces(true);
264
- this.pushCount(line.length - n); // possible comment
255
+ const n = (yield* this.pushCount(dirEnd)) + (yield* this.pushSpaces(true));
256
+ yield* this.pushCount(line.length - n); // possible comment
265
257
  this.pushNewline();
266
258
  return 'stream';
267
259
  }
268
260
  if (this.atLineEnd()) {
269
- const sp = this.pushSpaces(true);
270
- this.pushCount(line.length - sp);
271
- this.pushNewline();
261
+ const sp = yield* this.pushSpaces(true);
262
+ yield* this.pushCount(line.length - sp);
263
+ yield* this.pushNewline();
272
264
  return 'stream';
273
265
  }
274
- this.push(tokens.DOCUMENT);
275
- return this.parseLineStart();
266
+ yield cst.DOCUMENT;
267
+ return yield* this.parseLineStart();
276
268
  }
277
- parseLineStart() {
269
+ *parseLineStart() {
278
270
  const ch = this.charAt(0);
279
271
  if (!ch && !this.atEnd)
280
272
  return this.setNext('line-start');
@@ -283,80 +275,80 @@ class Lexer {
283
275
  return this.setNext('line-start');
284
276
  const s = this.peek(3);
285
277
  if (s === '---' && isEmpty(this.charAt(3))) {
286
- this.pushCount(3);
278
+ yield* this.pushCount(3);
287
279
  this.indentValue = 0;
288
280
  this.indentNext = 0;
289
281
  return 'doc';
290
282
  }
291
283
  else if (s === '...' && isEmpty(this.charAt(3))) {
292
- this.pushCount(3);
284
+ yield* this.pushCount(3);
293
285
  return 'stream';
294
286
  }
295
287
  }
296
- this.indentValue = this.pushSpaces(false);
288
+ this.indentValue = yield* this.pushSpaces(false);
297
289
  if (this.indentNext > this.indentValue && !isEmpty(this.charAt(1)))
298
290
  this.indentNext = this.indentValue;
299
- return this.parseBlockStart();
291
+ return yield* this.parseBlockStart();
300
292
  }
301
- parseBlockStart() {
293
+ *parseBlockStart() {
302
294
  const [ch0, ch1] = this.peek(2);
303
295
  if (!ch1 && !this.atEnd)
304
296
  return this.setNext('block-start');
305
297
  if ((ch0 === '-' || ch0 === '?' || ch0 === ':') && isEmpty(ch1)) {
306
- const n = this.pushCount(1) + this.pushSpaces(true);
298
+ const n = (yield* this.pushCount(1)) + (yield* this.pushSpaces(true));
307
299
  this.indentNext = this.indentValue + 1;
308
300
  this.indentValue += n;
309
- return this.parseBlockStart();
301
+ return yield* this.parseBlockStart();
310
302
  }
311
303
  return 'doc';
312
304
  }
313
- parseDocument() {
314
- this.pushSpaces(true);
305
+ *parseDocument() {
306
+ yield* this.pushSpaces(true);
315
307
  const line = this.getLine();
316
308
  if (line === null)
317
309
  return this.setNext('doc');
318
- let n = this.pushIndicators();
310
+ let n = yield* this.pushIndicators();
319
311
  switch (line[n]) {
320
312
  case '#':
321
- this.pushCount(line.length - n);
313
+ yield* this.pushCount(line.length - n);
322
314
  // fallthrough
323
315
  case undefined:
324
- this.pushNewline();
325
- return this.parseLineStart();
316
+ yield* this.pushNewline();
317
+ return yield* this.parseLineStart();
326
318
  case '{':
327
319
  case '[':
328
- this.pushCount(1);
320
+ yield* this.pushCount(1);
329
321
  this.flowKey = false;
330
322
  this.flowLevel = 1;
331
323
  return 'flow';
332
324
  case '}':
333
325
  case ']':
334
326
  // this is an error
335
- this.pushCount(1);
327
+ yield* this.pushCount(1);
336
328
  return 'doc';
337
329
  case '*':
338
- this.pushUntil(isNotIdentifierChar);
330
+ yield* this.pushUntil(isNotIdentifierChar);
339
331
  return 'doc';
340
332
  case '"':
341
333
  case "'":
342
- return this.parseQuotedScalar();
334
+ return yield* this.parseQuotedScalar();
343
335
  case '|':
344
336
  case '>':
345
- n += this.parseBlockScalarHeader();
346
- n += this.pushSpaces(true);
347
- this.pushCount(line.length - n);
348
- this.pushNewline();
349
- return this.parseBlockScalar();
337
+ n += yield* this.parseBlockScalarHeader();
338
+ n += yield* this.pushSpaces(true);
339
+ yield* this.pushCount(line.length - n);
340
+ yield* this.pushNewline();
341
+ return yield* this.parseBlockScalar();
350
342
  default:
351
- return this.parsePlainScalar();
343
+ return yield* this.parsePlainScalar();
352
344
  }
353
345
  }
354
- parseFlowCollection() {
346
+ *parseFlowCollection() {
355
347
  let nl, sp;
356
348
  let indent = -1;
357
349
  do {
358
- nl = this.pushNewline();
359
- sp = this.pushSpaces(true);
350
+ nl = yield* this.pushNewline();
351
+ sp = yield* this.pushSpaces(true);
360
352
  if (nl > 0)
361
353
  this.indentValue = indent = sp;
362
354
  } while (nl + sp > 0);
@@ -376,54 +368,54 @@ class Lexer {
376
368
  if (!atFlowEndMarker) {
377
369
  // this is an error
378
370
  this.flowLevel = 0;
379
- this.push(tokens.FLOW_END);
380
- return this.parseLineStart();
371
+ yield cst.FLOW_END;
372
+ return yield* this.parseLineStart();
381
373
  }
382
374
  }
383
375
  let n = 0;
384
376
  while (line[n] === ',')
385
- n += this.pushCount(1) + this.pushSpaces(true);
386
- n += this.pushIndicators();
377
+ n += (yield* this.pushCount(1)) + (yield* this.pushSpaces(true));
378
+ n += yield* this.pushIndicators();
387
379
  switch (line[n]) {
388
380
  case undefined:
389
381
  return 'flow';
390
382
  case '#':
391
- this.pushCount(line.length - n);
383
+ yield* this.pushCount(line.length - n);
392
384
  return 'flow';
393
385
  case '{':
394
386
  case '[':
395
- this.pushCount(1);
387
+ yield* this.pushCount(1);
396
388
  this.flowKey = false;
397
389
  this.flowLevel += 1;
398
390
  return 'flow';
399
391
  case '}':
400
392
  case ']':
401
- this.pushCount(1);
393
+ yield* this.pushCount(1);
402
394
  this.flowKey = true;
403
395
  this.flowLevel -= 1;
404
396
  return this.flowLevel ? 'flow' : 'doc';
405
397
  case '*':
406
- this.pushUntil(isNotIdentifierChar);
398
+ yield* this.pushUntil(isNotIdentifierChar);
407
399
  return 'flow';
408
400
  case '"':
409
401
  case "'":
410
402
  this.flowKey = true;
411
- return this.parseQuotedScalar();
403
+ return yield* this.parseQuotedScalar();
412
404
  case ':': {
413
405
  const next = this.charAt(1);
414
406
  if (this.flowKey || isEmpty(next) || next === ',') {
415
- this.pushCount(1);
416
- this.pushSpaces(true);
407
+ yield* this.pushCount(1);
408
+ yield* this.pushSpaces(true);
417
409
  return 'flow';
418
410
  }
419
411
  }
420
412
  // fallthrough
421
413
  default:
422
414
  this.flowKey = false;
423
- return this.parsePlainScalar();
415
+ return yield* this.parsePlainScalar();
424
416
  }
425
417
  }
426
- parseQuotedScalar() {
418
+ *parseQuotedScalar() {
427
419
  const quote = this.charAt(0);
428
420
  let end = this.buffer.indexOf(quote, this.pos + 1);
429
421
  if (quote === "'") {
@@ -459,10 +451,10 @@ class Lexer {
459
451
  return this.setNext('quoted-scalar');
460
452
  end = this.buffer.length;
461
453
  }
462
- this.pushToIndex(end + 1, false);
454
+ yield* this.pushToIndex(end + 1, false);
463
455
  return this.flowLevel ? 'flow' : 'doc';
464
456
  }
465
- parseBlockScalarHeader() {
457
+ *parseBlockScalarHeader() {
466
458
  this.blockScalarIndent = -1;
467
459
  this.blockScalarKeep = false;
468
460
  let i = this.pos;
@@ -475,9 +467,9 @@ class Lexer {
475
467
  else if (ch !== '-')
476
468
  break;
477
469
  }
478
- return this.pushUntil(ch => isEmpty(ch) || ch === '#');
470
+ return yield* this.pushUntil(ch => isEmpty(ch) || ch === '#');
479
471
  }
480
- parseBlockScalar() {
472
+ *parseBlockScalar() {
481
473
  let nl = this.pos - 1; // may be -1 if this.pos === 0
482
474
  let indent = 0;
483
475
  let ch;
@@ -534,11 +526,11 @@ class Lexer {
534
526
  break;
535
527
  } while (true);
536
528
  }
537
- this.push(tokens.SCALAR);
538
- this.pushToIndex(nl + 1, true);
539
- return this.parseLineStart();
529
+ yield cst.SCALAR;
530
+ yield* this.pushToIndex(nl + 1, true);
531
+ return yield* this.parseLineStart();
540
532
  }
541
- parsePlainScalar() {
533
+ *parsePlainScalar() {
542
534
  const inFlow = this.flowLevel > 0;
543
535
  let end = this.pos - 1;
544
536
  let i = this.pos - 1;
@@ -577,69 +569,71 @@ class Lexer {
577
569
  }
578
570
  if (!ch && !this.atEnd)
579
571
  return this.setNext('plain-scalar');
580
- this.push(tokens.SCALAR);
581
- this.pushToIndex(end + 1, true);
572
+ yield cst.SCALAR;
573
+ yield* this.pushToIndex(end + 1, true);
582
574
  return inFlow ? 'flow' : 'doc';
583
575
  }
584
- pushCount(n) {
576
+ *pushCount(n) {
585
577
  if (n > 0) {
586
- this.push(this.buffer.substr(this.pos, n));
578
+ yield this.buffer.substr(this.pos, n);
587
579
  this.pos += n;
588
580
  return n;
589
581
  }
590
582
  return 0;
591
583
  }
592
- pushToIndex(i, allowEmpty) {
584
+ *pushToIndex(i, allowEmpty) {
593
585
  const s = this.buffer.slice(this.pos, i);
594
586
  if (s) {
595
- this.push(s);
587
+ yield s;
596
588
  this.pos += s.length;
597
589
  return s.length;
598
590
  }
599
591
  else if (allowEmpty)
600
- this.push('');
592
+ yield '';
601
593
  return 0;
602
594
  }
603
- pushIndicators() {
595
+ *pushIndicators() {
604
596
  switch (this.charAt(0)) {
605
597
  case '!':
606
598
  if (this.charAt(1) === '<')
607
- return (this.pushVerbatimTag() +
608
- this.pushSpaces(true) +
609
- this.pushIndicators());
599
+ return ((yield* this.pushVerbatimTag()) +
600
+ (yield* this.pushSpaces(true)) +
601
+ (yield* this.pushIndicators()));
610
602
  // fallthrough
611
603
  case '&':
612
- return (this.pushUntil(isNotIdentifierChar) +
613
- this.pushSpaces(true) +
614
- this.pushIndicators());
604
+ return ((yield* this.pushUntil(isNotIdentifierChar)) +
605
+ (yield* this.pushSpaces(true)) +
606
+ (yield* this.pushIndicators()));
615
607
  case ':':
616
608
  case '?': // this is an error outside flow collections
617
609
  case '-': // this is an error
618
610
  if (isEmpty(this.charAt(1))) {
619
611
  if (this.flowLevel === 0)
620
612
  this.indentNext = this.indentValue + 1;
621
- return (this.pushCount(1) + this.pushSpaces(true) + this.pushIndicators());
613
+ return ((yield* this.pushCount(1)) +
614
+ (yield* this.pushSpaces(true)) +
615
+ (yield* this.pushIndicators()));
622
616
  }
623
617
  }
624
618
  return 0;
625
619
  }
626
- pushVerbatimTag() {
620
+ *pushVerbatimTag() {
627
621
  let i = this.pos + 2;
628
622
  let ch = this.buffer[i];
629
623
  while (!isEmpty(ch) && ch !== '>')
630
624
  ch = this.buffer[++i];
631
- return this.pushToIndex(ch === '>' ? i + 1 : i, false);
625
+ return yield* this.pushToIndex(ch === '>' ? i + 1 : i, false);
632
626
  }
633
- pushNewline() {
627
+ *pushNewline() {
634
628
  const ch = this.buffer[this.pos];
635
629
  if (ch === '\n')
636
- return this.pushCount(1);
630
+ return yield* this.pushCount(1);
637
631
  else if (ch === '\r' && this.charAt(1) === '\n')
638
- return this.pushCount(2);
632
+ return yield* this.pushCount(2);
639
633
  else
640
634
  return 0;
641
635
  }
642
- pushSpaces(allowTabs) {
636
+ *pushSpaces(allowTabs) {
643
637
  let i = this.pos - 1;
644
638
  let ch;
645
639
  do {
@@ -647,17 +641,17 @@ class Lexer {
647
641
  } while (ch === ' ' || (allowTabs && ch === '\t'));
648
642
  const n = i - this.pos;
649
643
  if (n > 0) {
650
- this.push(this.buffer.substr(this.pos, n));
644
+ yield this.buffer.substr(this.pos, n);
651
645
  this.pos = i;
652
646
  }
653
647
  return n;
654
648
  }
655
- pushUntil(test) {
649
+ *pushUntil(test) {
656
650
  let i = this.pos;
657
651
  let ch = this.buffer[i];
658
652
  while (!test(ch))
659
653
  ch = this.buffer[++i];
660
- return this.pushToIndex(i, false);
654
+ return yield* this.pushToIndex(i, false);
661
655
  }
662
656
  }
663
657
 
@@ -1,23 +1,32 @@
1
- import { Token } from './tokens.js';
1
+ import { Token } from './cst.js';
2
2
  /**
3
3
  * A YAML concrete syntax tree (CST) parser
4
4
  *
5
- * While the `parse()` method provides an API for parsing a source string
6
- * directly, the parser may also be used with a user-provided lexer:
7
- *
8
5
  * ```ts
9
- * const cst: Token[] = []
10
- * const parser = new Parser(tok => cst.push(tok))
11
6
  * const src: string = ...
7
+ * for (const token of new Parser().parse(src)) {
8
+ * // token: Token
9
+ * }
10
+ * ```
12
11
  *
13
- * // The following would be equivalent to `parser.parse(src, false)`
14
- * const lexer = new Lexer(parser.next)
15
- * lexer.lex(src, false)
16
- * parser.end()
12
+ * To use the parser with a user-provided lexer:
13
+ *
14
+ * ```ts
15
+ * function* parse(source: string, lexer: Lexer) {
16
+ * const parser = new Parser()
17
+ * for (const lexeme of lexer.lex(source))
18
+ * yield* parser.next(lexeme)
19
+ * yield* parser.end()
20
+ * }
21
+ *
22
+ * const src: string = ...
23
+ * const lexer = new Lexer()
24
+ * for (const token of parse(src, lexer)) {
25
+ * // token: Token
26
+ * }
17
27
  * ```
18
28
  */
19
29
  export declare class Parser {
20
- private push;
21
30
  private onNewLine?;
22
31
  /** If true, space and sequence indicators count as indentation */
23
32
  private atNewLine;
@@ -36,29 +45,26 @@ export declare class Parser {
36
45
  /** The type of the current token, set in parse() */
37
46
  private type;
38
47
  /**
39
- * @param push - Called separately with each parsed token
40
48
  * @param onNewLine - If defined, called separately with the start position of
41
49
  * each new line (in `parse()`, including the start of input).
42
- * @public
43
50
  */
44
- constructor(push: (token: Token) => void, onNewLine?: (offset: number) => void);
51
+ constructor(onNewLine?: (offset: number) => void);
45
52
  /**
46
- * Parse `source` as a YAML stream, calling `push` with each directive,
47
- * document and other structure as it is completely parsed. If `incomplete`,
48
- * a part of the last line may be left as a buffer for the next call.
53
+ * Parse `source` as a YAML stream.
54
+ * If `incomplete`, a part of the last line may be left as a buffer for the next call.
55
+ *
56
+ * Errors are not thrown, but yielded as `{ type: 'error', message }` tokens.
49
57
  *
50
- * Errors are not thrown, but pushed out as `{ type: 'error', message }` tokens.
51
- * @public
58
+ * @returns A generator of tokens representing each directive, document, and other structure.
52
59
  */
53
- parse(source: string, incomplete?: boolean): void;
60
+ parse(source: string, incomplete?: boolean): Generator<Token, void, unknown>;
54
61
  /**
55
- * Advance the parser by the `source` of one lexical token. Bound to the
56
- * Parser instance, so may be used directly as a callback function.
62
+ * Advance the parser by the `source` of one lexical token.
57
63
  */
58
- next: (source: string) => void;
64
+ next(source: string): Generator<Token, void, unknown>;
59
65
  private lexer;
60
66
  /** Call at end of input to push out any remaining constructions */
61
- end(): void;
67
+ end(): Generator<Token, void, unknown>;
62
68
  private get sourceToken();
63
69
  private step;
64
70
  private peek;