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
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ var cst = require('./cst.js');
3
4
  var lexer = require('./lexer.js');
4
- var tokens = require('./tokens.js');
5
5
 
6
6
  function includesToken(list, type) {
7
7
  for (let i = 0; i < list.length; ++i)
@@ -22,38 +22,6 @@ function includesNonEmpty(list) {
22
22
  }
23
23
  return false;
24
24
  }
25
- function atFirstEmptyLineAfterComments(start) {
26
- let hasComment = false;
27
- for (let i = 0; i < start.length; ++i) {
28
- switch (start[i].type) {
29
- case 'space':
30
- break;
31
- case 'comment':
32
- hasComment = true;
33
- break;
34
- case 'newline':
35
- if (!hasComment)
36
- return false;
37
- break;
38
- default:
39
- return false;
40
- }
41
- }
42
- if (hasComment) {
43
- for (let i = start.length - 1; i >= 0; --i) {
44
- switch (start[i].type) {
45
- /* istanbul ignore next */
46
- case 'space':
47
- break;
48
- case 'newline':
49
- return true;
50
- default:
51
- return false;
52
- }
53
- }
54
- }
55
- return false;
56
- }
57
25
  function isFlowToken(token) {
58
26
  switch (token === null || token === void 0 ? void 0 : token.type) {
59
27
  case 'alias':
@@ -89,6 +57,7 @@ function getFirstKeyStartProps(prev) {
89
57
  let i = prev.length;
90
58
  loop: while (--i >= 0) {
91
59
  switch (prev[i].type) {
60
+ case 'doc-start':
92
61
  case 'explicit-key-ind':
93
62
  case 'map-value-ind':
94
63
  case 'seq-item-ind':
@@ -101,31 +70,62 @@ function getFirstKeyStartProps(prev) {
101
70
  }
102
71
  return prev.splice(i, prev.length);
103
72
  }
73
+ function fixFlowSeqItems(fc) {
74
+ if (fc.start.type === 'flow-seq-start') {
75
+ for (const it of fc.items) {
76
+ if (it.sep &&
77
+ !it.value &&
78
+ !includesToken(it.start, 'explicit-key-ind') &&
79
+ !includesToken(it.sep, 'map-value-ind')) {
80
+ if (it.key)
81
+ it.value = it.key;
82
+ delete it.key;
83
+ if (isFlowToken(it.value)) {
84
+ if (it.value.end)
85
+ Array.prototype.push.apply(it.value.end, it.sep);
86
+ else
87
+ it.value.end = it.sep;
88
+ }
89
+ else
90
+ Array.prototype.push.apply(it.start, it.sep);
91
+ delete it.sep;
92
+ }
93
+ }
94
+ }
95
+ }
104
96
  /**
105
97
  * A YAML concrete syntax tree (CST) parser
106
98
  *
107
- * While the `parse()` method provides an API for parsing a source string
108
- * directly, the parser may also be used with a user-provided lexer:
109
- *
110
99
  * ```ts
111
- * const cst: Token[] = []
112
- * const parser = new Parser(tok => cst.push(tok))
113
100
  * const src: string = ...
101
+ * for (const token of new Parser().parse(src)) {
102
+ * // token: Token
103
+ * }
104
+ * ```
105
+ *
106
+ * To use the parser with a user-provided lexer:
107
+ *
108
+ * ```ts
109
+ * function* parse(source: string, lexer: Lexer) {
110
+ * const parser = new Parser()
111
+ * for (const lexeme of lexer.lex(source))
112
+ * yield* parser.next(lexeme)
113
+ * yield* parser.end()
114
+ * }
114
115
  *
115
- * // The following would be equivalent to `parser.parse(src, false)`
116
- * const lexer = new Lexer(parser.next)
117
- * lexer.lex(src, false)
118
- * parser.end()
116
+ * const src: string = ...
117
+ * const lexer = new Lexer()
118
+ * for (const token of parse(src, lexer)) {
119
+ * // token: Token
120
+ * }
119
121
  * ```
120
122
  */
121
123
  class Parser {
122
124
  /**
123
- * @param push - Called separately with each parsed token
124
125
  * @param onNewLine - If defined, called separately with the start position of
125
126
  * each new line (in `parse()`, including the start of input).
126
- * @public
127
127
  */
128
- constructor(push, onNewLine) {
128
+ constructor(onNewLine) {
129
129
  /** If true, space and sequence indicators count as indentation */
130
130
  this.atNewLine = true;
131
131
  /** If true, next token is a scalar value */
@@ -142,96 +142,97 @@ class Parser {
142
142
  this.source = '';
143
143
  /** The type of the current token, set in parse() */
144
144
  this.type = '';
145
- /**
146
- * Advance the parser by the `source` of one lexical token. Bound to the
147
- * Parser instance, so may be used directly as a callback function.
148
- */
149
- this.next = (source) => {
150
- this.source = source;
151
- if (process.env.LOG_TOKENS)
152
- console.log('|', tokens.prettyToken(source));
153
- if (this.atScalar) {
154
- this.atScalar = false;
155
- this.step();
156
- this.offset += source.length;
157
- return;
158
- }
159
- const type = tokens.tokenType(source);
160
- if (!type) {
161
- const message = `Not a YAML token: ${source}`;
162
- this.pop({ type: 'error', offset: this.offset, message, source });
163
- this.offset += source.length;
164
- }
165
- else if (type === 'scalar') {
166
- this.atNewLine = false;
167
- this.atScalar = true;
168
- this.type = 'scalar';
169
- }
170
- else {
171
- this.type = type;
172
- this.step();
173
- switch (type) {
174
- case 'newline':
175
- this.atNewLine = true;
176
- this.indent = 0;
177
- if (this.onNewLine)
178
- this.onNewLine(this.offset + source.length);
179
- break;
180
- case 'space':
181
- if (this.atNewLine && source[0] === ' ')
182
- this.indent += source.length;
183
- break;
184
- case 'explicit-key-ind':
185
- case 'map-value-ind':
186
- case 'seq-item-ind':
187
- if (this.atNewLine)
188
- this.indent += source.length;
189
- break;
190
- case 'doc-mode':
191
- return;
192
- default:
193
- this.atNewLine = false;
194
- }
195
- this.offset += source.length;
196
- }
197
- };
198
145
  // Must be defined after `next()`
199
- this.lexer = new lexer.Lexer(this.next);
200
- this.push = push;
146
+ this.lexer = new lexer.Lexer();
201
147
  this.onNewLine = onNewLine;
202
148
  }
203
149
  /**
204
- * Parse `source` as a YAML stream, calling `push` with each directive,
205
- * document and other structure as it is completely parsed. If `incomplete`,
206
- * a part of the last line may be left as a buffer for the next call.
150
+ * Parse `source` as a YAML stream.
151
+ * If `incomplete`, a part of the last line may be left as a buffer for the next call.
207
152
  *
208
- * Errors are not thrown, but pushed out as `{ type: 'error', message }` tokens.
209
- * @public
153
+ * Errors are not thrown, but yielded as `{ type: 'error', message }` tokens.
154
+ *
155
+ * @returns A generator of tokens representing each directive, document, and other structure.
210
156
  */
211
- parse(source, incomplete = false) {
157
+ *parse(source, incomplete = false) {
212
158
  if (this.onNewLine && this.offset === 0)
213
159
  this.onNewLine(0);
214
- this.lexer.lex(source, incomplete);
160
+ for (const lexeme of this.lexer.lex(source, incomplete))
161
+ yield* this.next(lexeme);
215
162
  if (!incomplete)
216
- this.end();
163
+ yield* this.end();
164
+ }
165
+ /**
166
+ * Advance the parser by the `source` of one lexical token.
167
+ */
168
+ *next(source) {
169
+ this.source = source;
170
+ if (process.env.LOG_TOKENS)
171
+ console.log('|', cst.prettyToken(source));
172
+ if (this.atScalar) {
173
+ this.atScalar = false;
174
+ yield* this.step();
175
+ this.offset += source.length;
176
+ return;
177
+ }
178
+ const type = cst.tokenType(source);
179
+ if (!type) {
180
+ const message = `Not a YAML token: ${source}`;
181
+ yield* this.pop({ type: 'error', offset: this.offset, message, source });
182
+ this.offset += source.length;
183
+ }
184
+ else if (type === 'scalar') {
185
+ this.atNewLine = false;
186
+ this.atScalar = true;
187
+ this.type = 'scalar';
188
+ }
189
+ else {
190
+ this.type = type;
191
+ yield* this.step();
192
+ switch (type) {
193
+ case 'newline':
194
+ this.atNewLine = true;
195
+ this.indent = 0;
196
+ if (this.onNewLine)
197
+ this.onNewLine(this.offset + source.length);
198
+ break;
199
+ case 'space':
200
+ if (this.atNewLine && source[0] === ' ')
201
+ this.indent += source.length;
202
+ break;
203
+ case 'explicit-key-ind':
204
+ case 'map-value-ind':
205
+ case 'seq-item-ind':
206
+ if (this.atNewLine)
207
+ this.indent += source.length;
208
+ break;
209
+ case 'doc-mode':
210
+ return;
211
+ default:
212
+ this.atNewLine = false;
213
+ }
214
+ this.offset += source.length;
215
+ }
217
216
  }
218
217
  /** Call at end of input to push out any remaining constructions */
219
- end() {
218
+ *end() {
220
219
  while (this.stack.length > 0)
221
- this.pop();
220
+ yield* this.pop();
222
221
  }
223
222
  get sourceToken() {
224
- return {
223
+ const st = {
225
224
  type: this.type,
225
+ offset: this.offset,
226
226
  indent: this.indent,
227
227
  source: this.source
228
228
  };
229
+ return st;
229
230
  }
230
- step() {
231
+ *step() {
231
232
  const top = this.peek(1);
232
233
  if (this.type === 'doc-end' && (!top || top.type !== 'doc-end')) {
233
234
  while (this.stack.length > 0)
234
- this.pop();
235
+ yield* this.pop();
235
236
  this.stack.push({
236
237
  type: 'doc-end',
237
238
  offset: this.offset,
@@ -240,47 +241,49 @@ class Parser {
240
241
  return;
241
242
  }
242
243
  if (!top)
243
- return this.stream();
244
+ return yield* this.stream();
244
245
  switch (top.type) {
245
246
  case 'document':
246
- return this.document(top);
247
+ return yield* this.document(top);
247
248
  case 'alias':
248
249
  case 'scalar':
249
250
  case 'single-quoted-scalar':
250
251
  case 'double-quoted-scalar':
251
- return this.scalar(top);
252
+ return yield* this.scalar(top);
252
253
  case 'block-scalar':
253
- return this.blockScalar(top);
254
+ return yield* this.blockScalar(top);
254
255
  case 'block-map':
255
- return this.blockMap(top);
256
+ return yield* this.blockMap(top);
256
257
  case 'block-seq':
257
- return this.blockSequence(top);
258
+ return yield* this.blockSequence(top);
258
259
  case 'flow-collection':
259
- return this.flowCollection(top);
260
+ return yield* this.flowCollection(top);
260
261
  case 'doc-end':
261
- return this.documentEnd(top);
262
+ return yield* this.documentEnd(top);
262
263
  }
263
264
  /* istanbul ignore next should not happen */
264
- this.pop();
265
+ yield* this.pop();
265
266
  }
266
267
  peek(n) {
267
268
  return this.stack[this.stack.length - n];
268
269
  }
269
- pop(error) {
270
+ *pop(error) {
270
271
  const token = error || this.stack.pop();
271
272
  /* istanbul ignore if should not happen */
272
273
  if (!token) {
273
274
  const message = 'Tried to pop an empty stack';
274
- this.push({ type: 'error', source: '', message });
275
+ yield { type: 'error', offset: this.offset, source: '', message };
275
276
  }
276
277
  else if (this.stack.length === 0) {
277
- this.push(token);
278
+ yield token;
278
279
  }
279
280
  else {
280
281
  const top = this.peek(1);
281
282
  // For these, parent indent is needed instead of own
282
283
  if (token.type === 'block-scalar' || token.type === 'flow-collection')
283
284
  token.indent = 'indent' in top ? top.indent : -1;
285
+ if (token.type === 'flow-collection')
286
+ fixFlowSeqItems(token);
284
287
  switch (top.type) {
285
288
  case 'document':
286
289
  top.value = token;
@@ -313,13 +316,20 @@ class Parser {
313
316
  it.value = token;
314
317
  break;
315
318
  }
316
- case 'flow-collection':
317
- top.items.push(token);
318
- break;
319
+ case 'flow-collection': {
320
+ const it = top.items[top.items.length - 1];
321
+ if (!it || it.value)
322
+ top.items.push({ start: [], key: token, sep: [] });
323
+ else if (it.sep)
324
+ it.value = token;
325
+ else
326
+ Object.assign(it, { key: token, sep: [] });
327
+ return;
328
+ }
319
329
  /* istanbul ignore next should not happen */
320
330
  default:
321
- this.pop();
322
- this.pop(token);
331
+ yield* this.pop();
332
+ yield* this.pop(token);
323
333
  }
324
334
  if ((top.type === 'document' ||
325
335
  top.type === 'block-map' ||
@@ -342,16 +352,16 @@ class Parser {
342
352
  }
343
353
  }
344
354
  }
345
- stream() {
355
+ *stream() {
346
356
  switch (this.type) {
347
357
  case 'directive-line':
348
- this.push({ type: 'directive', source: this.source });
358
+ yield { type: 'directive', offset: this.offset, source: this.source };
349
359
  return;
350
360
  case 'byte-order-mark':
351
361
  case 'space':
352
362
  case 'comment':
353
363
  case 'newline':
354
- this.push(this.sourceToken);
364
+ yield this.sourceToken;
355
365
  return;
356
366
  case 'doc-mode':
357
367
  case 'doc-start': {
@@ -366,21 +376,21 @@ class Parser {
366
376
  return;
367
377
  }
368
378
  }
369
- this.push({
379
+ yield {
370
380
  type: 'error',
371
381
  offset: this.offset,
372
382
  message: `Unexpected ${this.type} token in YAML stream`,
373
383
  source: this.source
374
- });
384
+ };
375
385
  }
376
- document(doc) {
386
+ *document(doc) {
377
387
  if (doc.value)
378
- return this.lineEnd(doc);
388
+ return yield* this.lineEnd(doc);
379
389
  switch (this.type) {
380
390
  case 'doc-start': {
381
391
  if (includesNonEmpty(doc.start)) {
382
- this.pop();
383
- this.step();
392
+ yield* this.pop();
393
+ yield* this.step();
384
394
  }
385
395
  else
386
396
  doc.start.push(this.sourceToken);
@@ -398,15 +408,15 @@ class Parser {
398
408
  if (bv)
399
409
  this.stack.push(bv);
400
410
  else {
401
- this.push({
411
+ yield {
402
412
  type: 'error',
403
413
  offset: this.offset,
404
414
  message: `Unexpected ${this.type} token in YAML document`,
405
415
  source: this.source
406
- });
416
+ };
407
417
  }
408
418
  }
409
- scalar(scalar) {
419
+ *scalar(scalar) {
410
420
  if (this.type === 'map-value-ind') {
411
421
  const prev = getPrevProps(this.peek(2));
412
422
  const start = getFirstKeyStartProps(prev);
@@ -428,9 +438,9 @@ class Parser {
428
438
  this.stack[this.stack.length - 1] = map;
429
439
  }
430
440
  else
431
- this.lineEnd(scalar);
441
+ yield* this.lineEnd(scalar);
432
442
  }
433
- blockScalar(scalar) {
443
+ *blockScalar(scalar) {
434
444
  switch (this.type) {
435
445
  case 'space':
436
446
  case 'comment':
@@ -449,39 +459,53 @@ class Parser {
449
459
  nl = this.source.indexOf('\n', nl) + 1;
450
460
  }
451
461
  }
452
- this.pop();
462
+ yield* this.pop();
453
463
  break;
454
464
  /* istanbul ignore next should not happen */
455
465
  default:
456
- this.pop();
457
- this.step();
466
+ yield* this.pop();
467
+ yield* this.step();
458
468
  }
459
469
  }
460
- blockMap(map) {
470
+ *blockMap(map) {
461
471
  var _a;
462
472
  const it = map.items[map.items.length - 1];
463
473
  // it.sep is true-ish if pair already has key or : separator
464
474
  switch (this.type) {
465
475
  case 'newline':
466
476
  this.onKeyLine = false;
467
- if (!it.sep && atFirstEmptyLineAfterComments(it.start)) {
468
- const prev = map.items[map.items.length - 2];
469
- const end = (_a = prev === null || prev === void 0 ? void 0 : prev.value) === null || _a === void 0 ? void 0 : _a.end;
470
- if (Array.isArray(end)) {
471
- Array.prototype.push.apply(end, it.start);
472
- it.start = [this.sourceToken];
473
- return;
474
- }
477
+ if (it.value) {
478
+ const end = 'end' in it.value ? it.value.end : undefined;
479
+ const last = Array.isArray(end) ? end[end.length - 1] : undefined;
480
+ if ((last === null || last === void 0 ? void 0 : last.type) === 'comment')
481
+ end === null || end === void 0 ? void 0 : end.push(this.sourceToken);
482
+ else
483
+ map.items.push({ start: [this.sourceToken] });
475
484
  }
476
- // fallthrough
485
+ else if (it.sep)
486
+ it.sep.push(this.sourceToken);
487
+ else
488
+ it.start.push(this.sourceToken);
489
+ return;
477
490
  case 'space':
478
491
  case 'comment':
479
492
  if (it.value)
480
493
  map.items.push({ start: [this.sourceToken] });
481
494
  else if (it.sep)
482
495
  it.sep.push(this.sourceToken);
483
- else
496
+ else {
497
+ if (this.atIndentedComment(it.start, map.indent)) {
498
+ const prev = map.items[map.items.length - 2];
499
+ const end = (_a = prev === null || prev === void 0 ? void 0 : prev.value) === null || _a === void 0 ? void 0 : _a.end;
500
+ if (Array.isArray(end)) {
501
+ Array.prototype.push.apply(end, it.start);
502
+ end.push(this.sourceToken);
503
+ map.items.pop();
504
+ return;
505
+ }
506
+ }
484
507
  it.start.push(this.sourceToken);
508
+ }
485
509
  return;
486
510
  }
487
511
  if (this.indent >= map.indent) {
@@ -578,30 +602,42 @@ class Parser {
578
602
  }
579
603
  }
580
604
  }
581
- this.pop();
582
- this.step();
605
+ yield* this.pop();
606
+ yield* this.step();
583
607
  }
584
- blockSequence(seq) {
608
+ *blockSequence(seq) {
585
609
  var _a;
586
610
  const it = seq.items[seq.items.length - 1];
587
611
  switch (this.type) {
588
612
  case 'newline':
589
- if (!it.value && atFirstEmptyLineAfterComments(it.start)) {
590
- const prev = seq.items[seq.items.length - 2];
591
- const end = (_a = prev === null || prev === void 0 ? void 0 : prev.value) === null || _a === void 0 ? void 0 : _a.end;
592
- if (Array.isArray(end)) {
593
- Array.prototype.push.apply(end, it.start);
594
- it.start = [this.sourceToken];
595
- return;
596
- }
613
+ if (it.value) {
614
+ const end = 'end' in it.value ? it.value.end : undefined;
615
+ const last = Array.isArray(end) ? end[end.length - 1] : undefined;
616
+ if ((last === null || last === void 0 ? void 0 : last.type) === 'comment')
617
+ end === null || end === void 0 ? void 0 : end.push(this.sourceToken);
618
+ else
619
+ seq.items.push({ start: [this.sourceToken] });
597
620
  }
598
- // fallthrough
621
+ else
622
+ it.start.push(this.sourceToken);
623
+ return;
599
624
  case 'space':
600
625
  case 'comment':
601
626
  if (it.value)
602
627
  seq.items.push({ start: [this.sourceToken] });
603
- else
628
+ else {
629
+ if (this.atIndentedComment(it.start, seq.indent)) {
630
+ const prev = seq.items[seq.items.length - 2];
631
+ const end = (_a = prev === null || prev === void 0 ? void 0 : prev.value) === null || _a === void 0 ? void 0 : _a.end;
632
+ if (Array.isArray(end)) {
633
+ Array.prototype.push.apply(end, it.start);
634
+ end.push(this.sourceToken);
635
+ seq.items.pop();
636
+ return;
637
+ }
638
+ }
604
639
  it.start.push(this.sourceToken);
640
+ }
605
641
  return;
606
642
  case 'anchor':
607
643
  case 'tag':
@@ -620,38 +656,65 @@ class Parser {
620
656
  }
621
657
  if (this.indent > seq.indent) {
622
658
  const bv = this.startBlockValue(seq);
623
- if (bv)
624
- return this.stack.push(bv);
659
+ if (bv) {
660
+ this.stack.push(bv);
661
+ return;
662
+ }
625
663
  }
626
- this.pop();
627
- this.step();
664
+ yield* this.pop();
665
+ yield* this.step();
628
666
  }
629
- flowCollection(fc) {
667
+ *flowCollection(fc) {
668
+ const it = fc.items[fc.items.length - 1];
630
669
  if (this.type === 'flow-error-end') {
631
670
  let top;
632
671
  do {
633
- this.pop();
672
+ yield* this.pop();
634
673
  top = this.peek(1);
635
674
  } while (top && top.type === 'flow-collection');
636
675
  }
637
676
  else if (fc.end.length === 0) {
638
677
  switch (this.type) {
639
- case 'space':
640
- case 'comment':
641
- case 'newline':
642
678
  case 'comma':
643
679
  case 'explicit-key-ind':
680
+ if (!it || it.sep)
681
+ fc.items.push({ start: [this.sourceToken] });
682
+ else
683
+ it.start.push(this.sourceToken);
684
+ return;
644
685
  case 'map-value-ind':
686
+ if (!it || it.value)
687
+ fc.items.push({ start: [], key: null, sep: [this.sourceToken] });
688
+ else if (it.sep)
689
+ it.sep.push(this.sourceToken);
690
+ else
691
+ Object.assign(it, { key: null, sep: [this.sourceToken] });
692
+ return;
693
+ case 'space':
694
+ case 'comment':
695
+ case 'newline':
645
696
  case 'anchor':
646
697
  case 'tag':
647
- fc.items.push(this.sourceToken);
698
+ if (!it || it.value)
699
+ fc.items.push({ start: [this.sourceToken] });
700
+ else if (it.sep)
701
+ it.sep.push(this.sourceToken);
702
+ else
703
+ it.start.push(this.sourceToken);
648
704
  return;
649
705
  case 'alias':
650
706
  case 'scalar':
651
707
  case 'single-quoted-scalar':
652
- case 'double-quoted-scalar':
653
- fc.items.push(this.flowScalar(this.type));
708
+ case 'double-quoted-scalar': {
709
+ const fs = this.flowScalar(this.type);
710
+ if (!it || it.value)
711
+ fc.items.push({ start: [], key: fs, sep: [] });
712
+ else if (it.sep)
713
+ this.stack.push(fs);
714
+ else
715
+ Object.assign(it, { key: fs, sep: [] });
654
716
  return;
717
+ }
655
718
  case 'flow-map-end':
656
719
  case 'flow-seq-end':
657
720
  fc.end.push(this.sourceToken);
@@ -660,10 +723,10 @@ class Parser {
660
723
  const bv = this.startBlockValue(fc);
661
724
  /* istanbul ignore else should not happen */
662
725
  if (bv)
663
- return this.stack.push(bv);
726
+ this.stack.push(bv);
664
727
  else {
665
- this.pop();
666
- this.step();
728
+ yield* this.pop();
729
+ yield* this.step();
667
730
  }
668
731
  }
669
732
  else {
@@ -672,13 +735,14 @@ class Parser {
672
735
  (this.type === 'map-value-ind' ||
673
736
  (this.type === 'newline' &&
674
737
  !parent.items[parent.items.length - 1].sep))) {
675
- this.pop();
676
- this.step();
738
+ yield* this.pop();
739
+ yield* this.step();
677
740
  }
678
741
  else if (this.type === 'map-value-ind' &&
679
742
  parent.type !== 'flow-collection') {
680
743
  const prev = getPrevProps(parent);
681
744
  const start = getFirstKeyStartProps(prev);
745
+ fixFlowSeqItems(fc);
682
746
  const sep = fc.end.splice(1, fc.end.length);
683
747
  sep.push(this.sourceToken);
684
748
  const map = {
@@ -691,7 +755,7 @@ class Parser {
691
755
  this.stack[this.stack.length - 1] = map;
692
756
  }
693
757
  else {
694
- this.lineEnd(fc);
758
+ yield* this.lineEnd(fc);
695
759
  }
696
760
  }
697
761
  }
@@ -722,7 +786,8 @@ class Parser {
722
786
  type: 'block-scalar',
723
787
  offset: this.offset,
724
788
  indent: this.indent,
725
- props: [this.sourceToken]
789
+ props: [this.sourceToken],
790
+ source: ''
726
791
  };
727
792
  case 'flow-map-start':
728
793
  case 'flow-seq-start':
@@ -741,14 +806,18 @@ class Parser {
741
806
  indent: this.indent,
742
807
  items: [{ start: [this.sourceToken] }]
743
808
  };
744
- case 'explicit-key-ind':
809
+ case 'explicit-key-ind': {
745
810
  this.onKeyLine = true;
811
+ const prev = getPrevProps(parent);
812
+ const start = getFirstKeyStartProps(prev);
813
+ start.push(this.sourceToken);
746
814
  return {
747
815
  type: 'block-map',
748
816
  offset: this.offset,
749
817
  indent: this.indent,
750
- items: [{ start: [this.sourceToken] }]
818
+ items: [{ start }]
751
819
  };
820
+ }
752
821
  case 'map-value-ind': {
753
822
  this.onKeyLine = true;
754
823
  const prev = getPrevProps(parent);
@@ -763,17 +832,24 @@ class Parser {
763
832
  }
764
833
  return null;
765
834
  }
766
- documentEnd(docEnd) {
835
+ atIndentedComment(start, indent) {
836
+ if (this.type !== 'comment')
837
+ return false;
838
+ if (this.indent <= indent)
839
+ return false;
840
+ return start.every(st => st.type === 'newline' || st.type === 'space');
841
+ }
842
+ *documentEnd(docEnd) {
767
843
  if (this.type !== 'doc-mode') {
768
844
  if (docEnd.end)
769
845
  docEnd.end.push(this.sourceToken);
770
846
  else
771
847
  docEnd.end = [this.sourceToken];
772
848
  if (this.type === 'newline')
773
- this.pop();
849
+ yield* this.pop();
774
850
  }
775
851
  }
776
- lineEnd(token) {
852
+ *lineEnd(token) {
777
853
  switch (this.type) {
778
854
  case 'comma':
779
855
  case 'doc-start':
@@ -781,8 +857,8 @@ class Parser {
781
857
  case 'flow-seq-end':
782
858
  case 'flow-map-end':
783
859
  case 'map-value-ind':
784
- this.pop();
785
- this.step();
860
+ yield* this.pop();
861
+ yield* this.step();
786
862
  break;
787
863
  case 'newline':
788
864
  this.onKeyLine = false;
@@ -796,7 +872,7 @@ class Parser {
796
872
  else
797
873
  token.end = [this.sourceToken];
798
874
  if (this.type === 'newline')
799
- this.pop();
875
+ yield* this.pop();
800
876
  }
801
877
  }
802
878
  }