yaml 2.8.2 → 3.0.0-0

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 (238) hide show
  1. package/README.md +65 -85
  2. package/{bin.mjs → bin.js} +1 -1
  3. package/dist/chunk-BYypO7fO.js +18 -0
  4. package/dist/cli.d.ts +10 -7
  5. package/dist/cli.js +181 -0
  6. package/dist/directives-CG6n4xSM.js +2856 -0
  7. package/dist/index.d.ts +335 -25
  8. package/dist/index.js +4 -49
  9. package/dist/line-counter-TFl552k_.js +3098 -0
  10. package/dist/public-api-BE74JyVm.js +79 -0
  11. package/dist/stringifyString-S_ITVLTO.d.ts +1306 -0
  12. package/dist/test-events.d.ts +6 -3
  13. package/dist/test-events.js +98 -124
  14. package/dist/util.d.ts +2 -16
  15. package/dist/util.js +2 -27
  16. package/package.json +20 -44
  17. package/browser/dist/compose/compose-collection.js +0 -88
  18. package/browser/dist/compose/compose-doc.js +0 -43
  19. package/browser/dist/compose/compose-node.js +0 -102
  20. package/browser/dist/compose/compose-scalar.js +0 -86
  21. package/browser/dist/compose/composer.js +0 -217
  22. package/browser/dist/compose/resolve-block-map.js +0 -115
  23. package/browser/dist/compose/resolve-block-scalar.js +0 -198
  24. package/browser/dist/compose/resolve-block-seq.js +0 -49
  25. package/browser/dist/compose/resolve-end.js +0 -37
  26. package/browser/dist/compose/resolve-flow-collection.js +0 -207
  27. package/browser/dist/compose/resolve-flow-scalar.js +0 -223
  28. package/browser/dist/compose/resolve-props.js +0 -146
  29. package/browser/dist/compose/util-contains-newline.js +0 -34
  30. package/browser/dist/compose/util-empty-scalar-position.js +0 -26
  31. package/browser/dist/compose/util-flow-indent-check.js +0 -15
  32. package/browser/dist/compose/util-map-includes.js +0 -13
  33. package/browser/dist/doc/Document.js +0 -335
  34. package/browser/dist/doc/anchors.js +0 -71
  35. package/browser/dist/doc/applyReviver.js +0 -55
  36. package/browser/dist/doc/createNode.js +0 -88
  37. package/browser/dist/doc/directives.js +0 -176
  38. package/browser/dist/errors.js +0 -57
  39. package/browser/dist/index.js +0 -17
  40. package/browser/dist/log.js +0 -11
  41. package/browser/dist/nodes/Alias.js +0 -114
  42. package/browser/dist/nodes/Collection.js +0 -147
  43. package/browser/dist/nodes/Node.js +0 -38
  44. package/browser/dist/nodes/Pair.js +0 -36
  45. package/browser/dist/nodes/Scalar.js +0 -24
  46. package/browser/dist/nodes/YAMLMap.js +0 -144
  47. package/browser/dist/nodes/YAMLSeq.js +0 -113
  48. package/browser/dist/nodes/addPairToJSMap.js +0 -63
  49. package/browser/dist/nodes/identity.js +0 -36
  50. package/browser/dist/nodes/toJS.js +0 -37
  51. package/browser/dist/parse/cst-scalar.js +0 -214
  52. package/browser/dist/parse/cst-stringify.js +0 -61
  53. package/browser/dist/parse/cst-visit.js +0 -97
  54. package/browser/dist/parse/cst.js +0 -98
  55. package/browser/dist/parse/lexer.js +0 -717
  56. package/browser/dist/parse/line-counter.js +0 -39
  57. package/browser/dist/parse/parser.js +0 -967
  58. package/browser/dist/public-api.js +0 -102
  59. package/browser/dist/schema/Schema.js +0 -37
  60. package/browser/dist/schema/common/map.js +0 -17
  61. package/browser/dist/schema/common/null.js +0 -15
  62. package/browser/dist/schema/common/seq.js +0 -17
  63. package/browser/dist/schema/common/string.js +0 -14
  64. package/browser/dist/schema/core/bool.js +0 -19
  65. package/browser/dist/schema/core/float.js +0 -43
  66. package/browser/dist/schema/core/int.js +0 -38
  67. package/browser/dist/schema/core/schema.js +0 -23
  68. package/browser/dist/schema/json/schema.js +0 -62
  69. package/browser/dist/schema/tags.js +0 -96
  70. package/browser/dist/schema/yaml-1.1/binary.js +0 -58
  71. package/browser/dist/schema/yaml-1.1/bool.js +0 -26
  72. package/browser/dist/schema/yaml-1.1/float.js +0 -46
  73. package/browser/dist/schema/yaml-1.1/int.js +0 -71
  74. package/browser/dist/schema/yaml-1.1/merge.js +0 -64
  75. package/browser/dist/schema/yaml-1.1/omap.js +0 -74
  76. package/browser/dist/schema/yaml-1.1/pairs.js +0 -78
  77. package/browser/dist/schema/yaml-1.1/schema.js +0 -39
  78. package/browser/dist/schema/yaml-1.1/set.js +0 -93
  79. package/browser/dist/schema/yaml-1.1/timestamp.js +0 -101
  80. package/browser/dist/stringify/foldFlowLines.js +0 -146
  81. package/browser/dist/stringify/stringify.js +0 -128
  82. package/browser/dist/stringify/stringifyCollection.js +0 -143
  83. package/browser/dist/stringify/stringifyComment.js +0 -20
  84. package/browser/dist/stringify/stringifyDocument.js +0 -85
  85. package/browser/dist/stringify/stringifyNumber.js +0 -24
  86. package/browser/dist/stringify/stringifyPair.js +0 -150
  87. package/browser/dist/stringify/stringifyString.js +0 -336
  88. package/browser/dist/util.js +0 -11
  89. package/browser/dist/visit.js +0 -233
  90. package/browser/index.js +0 -5
  91. package/browser/package.json +0 -3
  92. package/dist/cli.mjs +0 -201
  93. package/dist/compose/compose-collection.d.ts +0 -11
  94. package/dist/compose/compose-collection.js +0 -90
  95. package/dist/compose/compose-doc.d.ts +0 -7
  96. package/dist/compose/compose-doc.js +0 -45
  97. package/dist/compose/compose-node.d.ts +0 -29
  98. package/dist/compose/compose-node.js +0 -105
  99. package/dist/compose/compose-scalar.d.ts +0 -5
  100. package/dist/compose/compose-scalar.js +0 -88
  101. package/dist/compose/composer.d.ts +0 -63
  102. package/dist/compose/composer.js +0 -222
  103. package/dist/compose/resolve-block-map.d.ts +0 -6
  104. package/dist/compose/resolve-block-map.js +0 -117
  105. package/dist/compose/resolve-block-scalar.d.ts +0 -11
  106. package/dist/compose/resolve-block-scalar.js +0 -200
  107. package/dist/compose/resolve-block-seq.d.ts +0 -6
  108. package/dist/compose/resolve-block-seq.js +0 -51
  109. package/dist/compose/resolve-end.d.ts +0 -6
  110. package/dist/compose/resolve-end.js +0 -39
  111. package/dist/compose/resolve-flow-collection.d.ts +0 -7
  112. package/dist/compose/resolve-flow-collection.js +0 -209
  113. package/dist/compose/resolve-flow-scalar.d.ts +0 -10
  114. package/dist/compose/resolve-flow-scalar.js +0 -225
  115. package/dist/compose/resolve-props.d.ts +0 -23
  116. package/dist/compose/resolve-props.js +0 -148
  117. package/dist/compose/util-contains-newline.d.ts +0 -2
  118. package/dist/compose/util-contains-newline.js +0 -36
  119. package/dist/compose/util-empty-scalar-position.d.ts +0 -2
  120. package/dist/compose/util-empty-scalar-position.js +0 -28
  121. package/dist/compose/util-flow-indent-check.d.ts +0 -3
  122. package/dist/compose/util-flow-indent-check.js +0 -17
  123. package/dist/compose/util-map-includes.d.ts +0 -4
  124. package/dist/compose/util-map-includes.js +0 -15
  125. package/dist/doc/Document.d.ts +0 -141
  126. package/dist/doc/Document.js +0 -337
  127. package/dist/doc/anchors.d.ts +0 -24
  128. package/dist/doc/anchors.js +0 -76
  129. package/dist/doc/applyReviver.d.ts +0 -9
  130. package/dist/doc/applyReviver.js +0 -57
  131. package/dist/doc/createNode.d.ts +0 -17
  132. package/dist/doc/createNode.js +0 -90
  133. package/dist/doc/directives.d.ts +0 -49
  134. package/dist/doc/directives.js +0 -178
  135. package/dist/errors.d.ts +0 -21
  136. package/dist/errors.js +0 -62
  137. package/dist/log.d.ts +0 -3
  138. package/dist/log.js +0 -19
  139. package/dist/nodes/Alias.d.ts +0 -29
  140. package/dist/nodes/Alias.js +0 -116
  141. package/dist/nodes/Collection.d.ts +0 -73
  142. package/dist/nodes/Collection.js +0 -151
  143. package/dist/nodes/Node.d.ts +0 -53
  144. package/dist/nodes/Node.js +0 -40
  145. package/dist/nodes/Pair.d.ts +0 -22
  146. package/dist/nodes/Pair.js +0 -39
  147. package/dist/nodes/Scalar.d.ts +0 -43
  148. package/dist/nodes/Scalar.js +0 -27
  149. package/dist/nodes/YAMLMap.d.ts +0 -53
  150. package/dist/nodes/YAMLMap.js +0 -147
  151. package/dist/nodes/YAMLSeq.d.ts +0 -60
  152. package/dist/nodes/YAMLSeq.js +0 -115
  153. package/dist/nodes/addPairToJSMap.d.ts +0 -4
  154. package/dist/nodes/addPairToJSMap.js +0 -65
  155. package/dist/nodes/identity.d.ts +0 -23
  156. package/dist/nodes/identity.js +0 -53
  157. package/dist/nodes/toJS.d.ts +0 -29
  158. package/dist/nodes/toJS.js +0 -39
  159. package/dist/options.d.ts +0 -344
  160. package/dist/parse/cst-scalar.d.ts +0 -64
  161. package/dist/parse/cst-scalar.js +0 -218
  162. package/dist/parse/cst-stringify.d.ts +0 -8
  163. package/dist/parse/cst-stringify.js +0 -63
  164. package/dist/parse/cst-visit.d.ts +0 -39
  165. package/dist/parse/cst-visit.js +0 -99
  166. package/dist/parse/cst.d.ts +0 -109
  167. package/dist/parse/cst.js +0 -112
  168. package/dist/parse/lexer.d.ts +0 -87
  169. package/dist/parse/lexer.js +0 -719
  170. package/dist/parse/line-counter.d.ts +0 -22
  171. package/dist/parse/line-counter.js +0 -41
  172. package/dist/parse/parser.d.ts +0 -84
  173. package/dist/parse/parser.js +0 -972
  174. package/dist/public-api.d.ts +0 -44
  175. package/dist/public-api.js +0 -107
  176. package/dist/schema/Schema.d.ts +0 -17
  177. package/dist/schema/Schema.js +0 -39
  178. package/dist/schema/common/map.d.ts +0 -2
  179. package/dist/schema/common/map.js +0 -19
  180. package/dist/schema/common/null.d.ts +0 -4
  181. package/dist/schema/common/null.js +0 -17
  182. package/dist/schema/common/seq.d.ts +0 -2
  183. package/dist/schema/common/seq.js +0 -19
  184. package/dist/schema/common/string.d.ts +0 -2
  185. package/dist/schema/common/string.js +0 -16
  186. package/dist/schema/core/bool.d.ts +0 -4
  187. package/dist/schema/core/bool.js +0 -21
  188. package/dist/schema/core/float.d.ts +0 -4
  189. package/dist/schema/core/float.js +0 -47
  190. package/dist/schema/core/int.d.ts +0 -4
  191. package/dist/schema/core/int.js +0 -42
  192. package/dist/schema/core/schema.d.ts +0 -1
  193. package/dist/schema/core/schema.js +0 -25
  194. package/dist/schema/json/schema.d.ts +0 -2
  195. package/dist/schema/json/schema.js +0 -64
  196. package/dist/schema/json-schema.d.ts +0 -69
  197. package/dist/schema/tags.d.ts +0 -48
  198. package/dist/schema/tags.js +0 -99
  199. package/dist/schema/types.d.ts +0 -92
  200. package/dist/schema/yaml-1.1/binary.d.ts +0 -2
  201. package/dist/schema/yaml-1.1/binary.js +0 -70
  202. package/dist/schema/yaml-1.1/bool.d.ts +0 -7
  203. package/dist/schema/yaml-1.1/bool.js +0 -29
  204. package/dist/schema/yaml-1.1/float.d.ts +0 -4
  205. package/dist/schema/yaml-1.1/float.js +0 -50
  206. package/dist/schema/yaml-1.1/int.d.ts +0 -5
  207. package/dist/schema/yaml-1.1/int.js +0 -76
  208. package/dist/schema/yaml-1.1/merge.d.ts +0 -9
  209. package/dist/schema/yaml-1.1/merge.js +0 -68
  210. package/dist/schema/yaml-1.1/omap.d.ts +0 -22
  211. package/dist/schema/yaml-1.1/omap.js +0 -77
  212. package/dist/schema/yaml-1.1/pairs.d.ts +0 -10
  213. package/dist/schema/yaml-1.1/pairs.js +0 -82
  214. package/dist/schema/yaml-1.1/schema.d.ts +0 -1
  215. package/dist/schema/yaml-1.1/schema.js +0 -41
  216. package/dist/schema/yaml-1.1/set.d.ts +0 -28
  217. package/dist/schema/yaml-1.1/set.js +0 -96
  218. package/dist/schema/yaml-1.1/timestamp.d.ts +0 -6
  219. package/dist/schema/yaml-1.1/timestamp.js +0 -105
  220. package/dist/stringify/foldFlowLines.d.ts +0 -34
  221. package/dist/stringify/foldFlowLines.js +0 -151
  222. package/dist/stringify/stringify.d.ts +0 -21
  223. package/dist/stringify/stringify.js +0 -131
  224. package/dist/stringify/stringifyCollection.d.ts +0 -17
  225. package/dist/stringify/stringifyCollection.js +0 -145
  226. package/dist/stringify/stringifyComment.d.ts +0 -10
  227. package/dist/stringify/stringifyComment.js +0 -24
  228. package/dist/stringify/stringifyDocument.d.ts +0 -4
  229. package/dist/stringify/stringifyDocument.js +0 -87
  230. package/dist/stringify/stringifyNumber.d.ts +0 -2
  231. package/dist/stringify/stringifyNumber.js +0 -26
  232. package/dist/stringify/stringifyPair.d.ts +0 -3
  233. package/dist/stringify/stringifyPair.js +0 -152
  234. package/dist/stringify/stringifyString.d.ts +0 -9
  235. package/dist/stringify/stringifyString.js +0 -338
  236. package/dist/visit.d.ts +0 -102
  237. package/dist/visit.js +0 -236
  238. package/util.js +0 -2
@@ -1,967 +0,0 @@
1
- import { tokenType } from './cst.js';
2
- import { Lexer } from './lexer.js';
3
-
4
- function includesToken(list, type) {
5
- for (let i = 0; i < list.length; ++i)
6
- if (list[i].type === type)
7
- return true;
8
- return false;
9
- }
10
- function findNonEmptyIndex(list) {
11
- for (let i = 0; i < list.length; ++i) {
12
- switch (list[i].type) {
13
- case 'space':
14
- case 'comment':
15
- case 'newline':
16
- break;
17
- default:
18
- return i;
19
- }
20
- }
21
- return -1;
22
- }
23
- function isFlowToken(token) {
24
- switch (token?.type) {
25
- case 'alias':
26
- case 'scalar':
27
- case 'single-quoted-scalar':
28
- case 'double-quoted-scalar':
29
- case 'flow-collection':
30
- return true;
31
- default:
32
- return false;
33
- }
34
- }
35
- function getPrevProps(parent) {
36
- switch (parent.type) {
37
- case 'document':
38
- return parent.start;
39
- case 'block-map': {
40
- const it = parent.items[parent.items.length - 1];
41
- return it.sep ?? it.start;
42
- }
43
- case 'block-seq':
44
- return parent.items[parent.items.length - 1].start;
45
- /* istanbul ignore next should not happen */
46
- default:
47
- return [];
48
- }
49
- }
50
- /** Note: May modify input array */
51
- function getFirstKeyStartProps(prev) {
52
- if (prev.length === 0)
53
- return [];
54
- let i = prev.length;
55
- loop: while (--i >= 0) {
56
- switch (prev[i].type) {
57
- case 'doc-start':
58
- case 'explicit-key-ind':
59
- case 'map-value-ind':
60
- case 'seq-item-ind':
61
- case 'newline':
62
- break loop;
63
- }
64
- }
65
- while (prev[++i]?.type === 'space') {
66
- /* loop */
67
- }
68
- return prev.splice(i, prev.length);
69
- }
70
- function fixFlowSeqItems(fc) {
71
- if (fc.start.type === 'flow-seq-start') {
72
- for (const it of fc.items) {
73
- if (it.sep &&
74
- !it.value &&
75
- !includesToken(it.start, 'explicit-key-ind') &&
76
- !includesToken(it.sep, 'map-value-ind')) {
77
- if (it.key)
78
- it.value = it.key;
79
- delete it.key;
80
- if (isFlowToken(it.value)) {
81
- if (it.value.end)
82
- Array.prototype.push.apply(it.value.end, it.sep);
83
- else
84
- it.value.end = it.sep;
85
- }
86
- else
87
- Array.prototype.push.apply(it.start, it.sep);
88
- delete it.sep;
89
- }
90
- }
91
- }
92
- }
93
- /**
94
- * A YAML concrete syntax tree (CST) parser
95
- *
96
- * ```ts
97
- * const src: string = ...
98
- * for (const token of new Parser().parse(src)) {
99
- * // token: Token
100
- * }
101
- * ```
102
- *
103
- * To use the parser with a user-provided lexer:
104
- *
105
- * ```ts
106
- * function* parse(source: string, lexer: Lexer) {
107
- * const parser = new Parser()
108
- * for (const lexeme of lexer.lex(source))
109
- * yield* parser.next(lexeme)
110
- * yield* parser.end()
111
- * }
112
- *
113
- * const src: string = ...
114
- * const lexer = new Lexer()
115
- * for (const token of parse(src, lexer)) {
116
- * // token: Token
117
- * }
118
- * ```
119
- */
120
- class Parser {
121
- /**
122
- * @param onNewLine - If defined, called separately with the start position of
123
- * each new line (in `parse()`, including the start of input).
124
- */
125
- constructor(onNewLine) {
126
- /** If true, space and sequence indicators count as indentation */
127
- this.atNewLine = true;
128
- /** If true, next token is a scalar value */
129
- this.atScalar = false;
130
- /** Current indentation level */
131
- this.indent = 0;
132
- /** Current offset since the start of parsing */
133
- this.offset = 0;
134
- /** On the same line with a block map key */
135
- this.onKeyLine = false;
136
- /** Top indicates the node that's currently being built */
137
- this.stack = [];
138
- /** The source of the current token, set in parse() */
139
- this.source = '';
140
- /** The type of the current token, set in parse() */
141
- this.type = '';
142
- // Must be defined after `next()`
143
- this.lexer = new Lexer();
144
- this.onNewLine = onNewLine;
145
- }
146
- /**
147
- * Parse `source` as a YAML stream.
148
- * If `incomplete`, a part of the last line may be left as a buffer for the next call.
149
- *
150
- * Errors are not thrown, but yielded as `{ type: 'error', message }` tokens.
151
- *
152
- * @returns A generator of tokens representing each directive, document, and other structure.
153
- */
154
- *parse(source, incomplete = false) {
155
- if (this.onNewLine && this.offset === 0)
156
- this.onNewLine(0);
157
- for (const lexeme of this.lexer.lex(source, incomplete))
158
- yield* this.next(lexeme);
159
- if (!incomplete)
160
- yield* this.end();
161
- }
162
- /**
163
- * Advance the parser by the `source` of one lexical token.
164
- */
165
- *next(source) {
166
- this.source = source;
167
- if (this.atScalar) {
168
- this.atScalar = false;
169
- yield* this.step();
170
- this.offset += source.length;
171
- return;
172
- }
173
- const type = tokenType(source);
174
- if (!type) {
175
- const message = `Not a YAML token: ${source}`;
176
- yield* this.pop({ type: 'error', offset: this.offset, message, source });
177
- this.offset += source.length;
178
- }
179
- else if (type === 'scalar') {
180
- this.atNewLine = false;
181
- this.atScalar = true;
182
- this.type = 'scalar';
183
- }
184
- else {
185
- this.type = type;
186
- yield* this.step();
187
- switch (type) {
188
- case 'newline':
189
- this.atNewLine = true;
190
- this.indent = 0;
191
- if (this.onNewLine)
192
- this.onNewLine(this.offset + source.length);
193
- break;
194
- case 'space':
195
- if (this.atNewLine && source[0] === ' ')
196
- this.indent += source.length;
197
- break;
198
- case 'explicit-key-ind':
199
- case 'map-value-ind':
200
- case 'seq-item-ind':
201
- if (this.atNewLine)
202
- this.indent += source.length;
203
- break;
204
- case 'doc-mode':
205
- case 'flow-error-end':
206
- return;
207
- default:
208
- this.atNewLine = false;
209
- }
210
- this.offset += source.length;
211
- }
212
- }
213
- /** Call at end of input to push out any remaining constructions */
214
- *end() {
215
- while (this.stack.length > 0)
216
- yield* this.pop();
217
- }
218
- get sourceToken() {
219
- const st = {
220
- type: this.type,
221
- offset: this.offset,
222
- indent: this.indent,
223
- source: this.source
224
- };
225
- return st;
226
- }
227
- *step() {
228
- const top = this.peek(1);
229
- if (this.type === 'doc-end' && top?.type !== 'doc-end') {
230
- while (this.stack.length > 0)
231
- yield* this.pop();
232
- this.stack.push({
233
- type: 'doc-end',
234
- offset: this.offset,
235
- source: this.source
236
- });
237
- return;
238
- }
239
- if (!top)
240
- return yield* this.stream();
241
- switch (top.type) {
242
- case 'document':
243
- return yield* this.document(top);
244
- case 'alias':
245
- case 'scalar':
246
- case 'single-quoted-scalar':
247
- case 'double-quoted-scalar':
248
- return yield* this.scalar(top);
249
- case 'block-scalar':
250
- return yield* this.blockScalar(top);
251
- case 'block-map':
252
- return yield* this.blockMap(top);
253
- case 'block-seq':
254
- return yield* this.blockSequence(top);
255
- case 'flow-collection':
256
- return yield* this.flowCollection(top);
257
- case 'doc-end':
258
- return yield* this.documentEnd(top);
259
- }
260
- /* istanbul ignore next should not happen */
261
- yield* this.pop();
262
- }
263
- peek(n) {
264
- return this.stack[this.stack.length - n];
265
- }
266
- *pop(error) {
267
- const token = error ?? this.stack.pop();
268
- /* istanbul ignore if should not happen */
269
- if (!token) {
270
- const message = 'Tried to pop an empty stack';
271
- yield { type: 'error', offset: this.offset, source: '', message };
272
- }
273
- else if (this.stack.length === 0) {
274
- yield token;
275
- }
276
- else {
277
- const top = this.peek(1);
278
- if (token.type === 'block-scalar') {
279
- // Block scalars use their parent rather than header indent
280
- token.indent = 'indent' in top ? top.indent : 0;
281
- }
282
- else if (token.type === 'flow-collection' && top.type === 'document') {
283
- // Ignore all indent for top-level flow collections
284
- token.indent = 0;
285
- }
286
- if (token.type === 'flow-collection')
287
- fixFlowSeqItems(token);
288
- switch (top.type) {
289
- case 'document':
290
- top.value = token;
291
- break;
292
- case 'block-scalar':
293
- top.props.push(token); // error
294
- break;
295
- case 'block-map': {
296
- const it = top.items[top.items.length - 1];
297
- if (it.value) {
298
- top.items.push({ start: [], key: token, sep: [] });
299
- this.onKeyLine = true;
300
- return;
301
- }
302
- else if (it.sep) {
303
- it.value = token;
304
- }
305
- else {
306
- Object.assign(it, { key: token, sep: [] });
307
- this.onKeyLine = !it.explicitKey;
308
- return;
309
- }
310
- break;
311
- }
312
- case 'block-seq': {
313
- const it = top.items[top.items.length - 1];
314
- if (it.value)
315
- top.items.push({ start: [], value: token });
316
- else
317
- it.value = token;
318
- break;
319
- }
320
- case 'flow-collection': {
321
- const it = top.items[top.items.length - 1];
322
- if (!it || it.value)
323
- top.items.push({ start: [], key: token, sep: [] });
324
- else if (it.sep)
325
- it.value = token;
326
- else
327
- Object.assign(it, { key: token, sep: [] });
328
- return;
329
- }
330
- /* istanbul ignore next should not happen */
331
- default:
332
- yield* this.pop();
333
- yield* this.pop(token);
334
- }
335
- if ((top.type === 'document' ||
336
- top.type === 'block-map' ||
337
- top.type === 'block-seq') &&
338
- (token.type === 'block-map' || token.type === 'block-seq')) {
339
- const last = token.items[token.items.length - 1];
340
- if (last &&
341
- !last.sep &&
342
- !last.value &&
343
- last.start.length > 0 &&
344
- findNonEmptyIndex(last.start) === -1 &&
345
- (token.indent === 0 ||
346
- last.start.every(st => st.type !== 'comment' || st.indent < token.indent))) {
347
- if (top.type === 'document')
348
- top.end = last.start;
349
- else
350
- top.items.push({ start: last.start });
351
- token.items.splice(-1, 1);
352
- }
353
- }
354
- }
355
- }
356
- *stream() {
357
- switch (this.type) {
358
- case 'directive-line':
359
- yield { type: 'directive', offset: this.offset, source: this.source };
360
- return;
361
- case 'byte-order-mark':
362
- case 'space':
363
- case 'comment':
364
- case 'newline':
365
- yield this.sourceToken;
366
- return;
367
- case 'doc-mode':
368
- case 'doc-start': {
369
- const doc = {
370
- type: 'document',
371
- offset: this.offset,
372
- start: []
373
- };
374
- if (this.type === 'doc-start')
375
- doc.start.push(this.sourceToken);
376
- this.stack.push(doc);
377
- return;
378
- }
379
- }
380
- yield {
381
- type: 'error',
382
- offset: this.offset,
383
- message: `Unexpected ${this.type} token in YAML stream`,
384
- source: this.source
385
- };
386
- }
387
- *document(doc) {
388
- if (doc.value)
389
- return yield* this.lineEnd(doc);
390
- switch (this.type) {
391
- case 'doc-start': {
392
- if (findNonEmptyIndex(doc.start) !== -1) {
393
- yield* this.pop();
394
- yield* this.step();
395
- }
396
- else
397
- doc.start.push(this.sourceToken);
398
- return;
399
- }
400
- case 'anchor':
401
- case 'tag':
402
- case 'space':
403
- case 'comment':
404
- case 'newline':
405
- doc.start.push(this.sourceToken);
406
- return;
407
- }
408
- const bv = this.startBlockValue(doc);
409
- if (bv)
410
- this.stack.push(bv);
411
- else {
412
- yield {
413
- type: 'error',
414
- offset: this.offset,
415
- message: `Unexpected ${this.type} token in YAML document`,
416
- source: this.source
417
- };
418
- }
419
- }
420
- *scalar(scalar) {
421
- if (this.type === 'map-value-ind') {
422
- const prev = getPrevProps(this.peek(2));
423
- const start = getFirstKeyStartProps(prev);
424
- let sep;
425
- if (scalar.end) {
426
- sep = scalar.end;
427
- sep.push(this.sourceToken);
428
- delete scalar.end;
429
- }
430
- else
431
- sep = [this.sourceToken];
432
- const map = {
433
- type: 'block-map',
434
- offset: scalar.offset,
435
- indent: scalar.indent,
436
- items: [{ start, key: scalar, sep }]
437
- };
438
- this.onKeyLine = true;
439
- this.stack[this.stack.length - 1] = map;
440
- }
441
- else
442
- yield* this.lineEnd(scalar);
443
- }
444
- *blockScalar(scalar) {
445
- switch (this.type) {
446
- case 'space':
447
- case 'comment':
448
- case 'newline':
449
- scalar.props.push(this.sourceToken);
450
- return;
451
- case 'scalar':
452
- scalar.source = this.source;
453
- // block-scalar source includes trailing newline
454
- this.atNewLine = true;
455
- this.indent = 0;
456
- if (this.onNewLine) {
457
- let nl = this.source.indexOf('\n') + 1;
458
- while (nl !== 0) {
459
- this.onNewLine(this.offset + nl);
460
- nl = this.source.indexOf('\n', nl) + 1;
461
- }
462
- }
463
- yield* this.pop();
464
- break;
465
- /* istanbul ignore next should not happen */
466
- default:
467
- yield* this.pop();
468
- yield* this.step();
469
- }
470
- }
471
- *blockMap(map) {
472
- const it = map.items[map.items.length - 1];
473
- // it.sep is true-ish if pair already has key or : separator
474
- switch (this.type) {
475
- case 'newline':
476
- this.onKeyLine = false;
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?.type === 'comment')
481
- end?.push(this.sourceToken);
482
- else
483
- map.items.push({ start: [this.sourceToken] });
484
- }
485
- else if (it.sep) {
486
- it.sep.push(this.sourceToken);
487
- }
488
- else {
489
- it.start.push(this.sourceToken);
490
- }
491
- return;
492
- case 'space':
493
- case 'comment':
494
- if (it.value) {
495
- map.items.push({ start: [this.sourceToken] });
496
- }
497
- else if (it.sep) {
498
- it.sep.push(this.sourceToken);
499
- }
500
- else {
501
- if (this.atIndentedComment(it.start, map.indent)) {
502
- const prev = map.items[map.items.length - 2];
503
- const end = prev?.value?.end;
504
- if (Array.isArray(end)) {
505
- Array.prototype.push.apply(end, it.start);
506
- end.push(this.sourceToken);
507
- map.items.pop();
508
- return;
509
- }
510
- }
511
- it.start.push(this.sourceToken);
512
- }
513
- return;
514
- }
515
- if (this.indent >= map.indent) {
516
- const atMapIndent = !this.onKeyLine && this.indent === map.indent;
517
- const atNextItem = atMapIndent &&
518
- (it.sep || it.explicitKey) &&
519
- this.type !== 'seq-item-ind';
520
- // For empty nodes, assign newline-separated not indented empty tokens to following node
521
- let start = [];
522
- if (atNextItem && it.sep && !it.value) {
523
- const nl = [];
524
- for (let i = 0; i < it.sep.length; ++i) {
525
- const st = it.sep[i];
526
- switch (st.type) {
527
- case 'newline':
528
- nl.push(i);
529
- break;
530
- case 'space':
531
- break;
532
- case 'comment':
533
- if (st.indent > map.indent)
534
- nl.length = 0;
535
- break;
536
- default:
537
- nl.length = 0;
538
- }
539
- }
540
- if (nl.length >= 2)
541
- start = it.sep.splice(nl[1]);
542
- }
543
- switch (this.type) {
544
- case 'anchor':
545
- case 'tag':
546
- if (atNextItem || it.value) {
547
- start.push(this.sourceToken);
548
- map.items.push({ start });
549
- this.onKeyLine = true;
550
- }
551
- else if (it.sep) {
552
- it.sep.push(this.sourceToken);
553
- }
554
- else {
555
- it.start.push(this.sourceToken);
556
- }
557
- return;
558
- case 'explicit-key-ind':
559
- if (!it.sep && !it.explicitKey) {
560
- it.start.push(this.sourceToken);
561
- it.explicitKey = true;
562
- }
563
- else if (atNextItem || it.value) {
564
- start.push(this.sourceToken);
565
- map.items.push({ start, explicitKey: true });
566
- }
567
- else {
568
- this.stack.push({
569
- type: 'block-map',
570
- offset: this.offset,
571
- indent: this.indent,
572
- items: [{ start: [this.sourceToken], explicitKey: true }]
573
- });
574
- }
575
- this.onKeyLine = true;
576
- return;
577
- case 'map-value-ind':
578
- if (it.explicitKey) {
579
- if (!it.sep) {
580
- if (includesToken(it.start, 'newline')) {
581
- Object.assign(it, { key: null, sep: [this.sourceToken] });
582
- }
583
- else {
584
- const start = getFirstKeyStartProps(it.start);
585
- this.stack.push({
586
- type: 'block-map',
587
- offset: this.offset,
588
- indent: this.indent,
589
- items: [{ start, key: null, sep: [this.sourceToken] }]
590
- });
591
- }
592
- }
593
- else if (it.value) {
594
- map.items.push({ start: [], key: null, sep: [this.sourceToken] });
595
- }
596
- else if (includesToken(it.sep, 'map-value-ind')) {
597
- this.stack.push({
598
- type: 'block-map',
599
- offset: this.offset,
600
- indent: this.indent,
601
- items: [{ start, key: null, sep: [this.sourceToken] }]
602
- });
603
- }
604
- else if (isFlowToken(it.key) &&
605
- !includesToken(it.sep, 'newline')) {
606
- const start = getFirstKeyStartProps(it.start);
607
- const key = it.key;
608
- const sep = it.sep;
609
- sep.push(this.sourceToken);
610
- // @ts-expect-error type guard is wrong here
611
- delete it.key;
612
- // @ts-expect-error type guard is wrong here
613
- delete it.sep;
614
- this.stack.push({
615
- type: 'block-map',
616
- offset: this.offset,
617
- indent: this.indent,
618
- items: [{ start, key, sep }]
619
- });
620
- }
621
- else if (start.length > 0) {
622
- // Not actually at next item
623
- it.sep = it.sep.concat(start, this.sourceToken);
624
- }
625
- else {
626
- it.sep.push(this.sourceToken);
627
- }
628
- }
629
- else {
630
- if (!it.sep) {
631
- Object.assign(it, { key: null, sep: [this.sourceToken] });
632
- }
633
- else if (it.value || atNextItem) {
634
- map.items.push({ start, key: null, sep: [this.sourceToken] });
635
- }
636
- else if (includesToken(it.sep, 'map-value-ind')) {
637
- this.stack.push({
638
- type: 'block-map',
639
- offset: this.offset,
640
- indent: this.indent,
641
- items: [{ start: [], key: null, sep: [this.sourceToken] }]
642
- });
643
- }
644
- else {
645
- it.sep.push(this.sourceToken);
646
- }
647
- }
648
- this.onKeyLine = true;
649
- return;
650
- case 'alias':
651
- case 'scalar':
652
- case 'single-quoted-scalar':
653
- case 'double-quoted-scalar': {
654
- const fs = this.flowScalar(this.type);
655
- if (atNextItem || it.value) {
656
- map.items.push({ start, key: fs, sep: [] });
657
- this.onKeyLine = true;
658
- }
659
- else if (it.sep) {
660
- this.stack.push(fs);
661
- }
662
- else {
663
- Object.assign(it, { key: fs, sep: [] });
664
- this.onKeyLine = true;
665
- }
666
- return;
667
- }
668
- default: {
669
- const bv = this.startBlockValue(map);
670
- if (bv) {
671
- if (bv.type === 'block-seq') {
672
- if (!it.explicitKey &&
673
- it.sep &&
674
- !includesToken(it.sep, 'newline')) {
675
- yield* this.pop({
676
- type: 'error',
677
- offset: this.offset,
678
- message: 'Unexpected block-seq-ind on same line with key',
679
- source: this.source
680
- });
681
- return;
682
- }
683
- }
684
- else if (atMapIndent) {
685
- map.items.push({ start });
686
- }
687
- this.stack.push(bv);
688
- return;
689
- }
690
- }
691
- }
692
- }
693
- yield* this.pop();
694
- yield* this.step();
695
- }
696
- *blockSequence(seq) {
697
- const it = seq.items[seq.items.length - 1];
698
- switch (this.type) {
699
- case 'newline':
700
- if (it.value) {
701
- const end = 'end' in it.value ? it.value.end : undefined;
702
- const last = Array.isArray(end) ? end[end.length - 1] : undefined;
703
- if (last?.type === 'comment')
704
- end?.push(this.sourceToken);
705
- else
706
- seq.items.push({ start: [this.sourceToken] });
707
- }
708
- else
709
- it.start.push(this.sourceToken);
710
- return;
711
- case 'space':
712
- case 'comment':
713
- if (it.value)
714
- seq.items.push({ start: [this.sourceToken] });
715
- else {
716
- if (this.atIndentedComment(it.start, seq.indent)) {
717
- const prev = seq.items[seq.items.length - 2];
718
- const end = prev?.value?.end;
719
- if (Array.isArray(end)) {
720
- Array.prototype.push.apply(end, it.start);
721
- end.push(this.sourceToken);
722
- seq.items.pop();
723
- return;
724
- }
725
- }
726
- it.start.push(this.sourceToken);
727
- }
728
- return;
729
- case 'anchor':
730
- case 'tag':
731
- if (it.value || this.indent <= seq.indent)
732
- break;
733
- it.start.push(this.sourceToken);
734
- return;
735
- case 'seq-item-ind':
736
- if (this.indent !== seq.indent)
737
- break;
738
- if (it.value || includesToken(it.start, 'seq-item-ind'))
739
- seq.items.push({ start: [this.sourceToken] });
740
- else
741
- it.start.push(this.sourceToken);
742
- return;
743
- }
744
- if (this.indent > seq.indent) {
745
- const bv = this.startBlockValue(seq);
746
- if (bv) {
747
- this.stack.push(bv);
748
- return;
749
- }
750
- }
751
- yield* this.pop();
752
- yield* this.step();
753
- }
754
- *flowCollection(fc) {
755
- const it = fc.items[fc.items.length - 1];
756
- if (this.type === 'flow-error-end') {
757
- let top;
758
- do {
759
- yield* this.pop();
760
- top = this.peek(1);
761
- } while (top?.type === 'flow-collection');
762
- }
763
- else if (fc.end.length === 0) {
764
- switch (this.type) {
765
- case 'comma':
766
- case 'explicit-key-ind':
767
- if (!it || it.sep)
768
- fc.items.push({ start: [this.sourceToken] });
769
- else
770
- it.start.push(this.sourceToken);
771
- return;
772
- case 'map-value-ind':
773
- if (!it || it.value)
774
- fc.items.push({ start: [], key: null, sep: [this.sourceToken] });
775
- else if (it.sep)
776
- it.sep.push(this.sourceToken);
777
- else
778
- Object.assign(it, { key: null, sep: [this.sourceToken] });
779
- return;
780
- case 'space':
781
- case 'comment':
782
- case 'newline':
783
- case 'anchor':
784
- case 'tag':
785
- if (!it || it.value)
786
- fc.items.push({ start: [this.sourceToken] });
787
- else if (it.sep)
788
- it.sep.push(this.sourceToken);
789
- else
790
- it.start.push(this.sourceToken);
791
- return;
792
- case 'alias':
793
- case 'scalar':
794
- case 'single-quoted-scalar':
795
- case 'double-quoted-scalar': {
796
- const fs = this.flowScalar(this.type);
797
- if (!it || it.value)
798
- fc.items.push({ start: [], key: fs, sep: [] });
799
- else if (it.sep)
800
- this.stack.push(fs);
801
- else
802
- Object.assign(it, { key: fs, sep: [] });
803
- return;
804
- }
805
- case 'flow-map-end':
806
- case 'flow-seq-end':
807
- fc.end.push(this.sourceToken);
808
- return;
809
- }
810
- const bv = this.startBlockValue(fc);
811
- /* istanbul ignore else should not happen */
812
- if (bv)
813
- this.stack.push(bv);
814
- else {
815
- yield* this.pop();
816
- yield* this.step();
817
- }
818
- }
819
- else {
820
- const parent = this.peek(2);
821
- if (parent.type === 'block-map' &&
822
- ((this.type === 'map-value-ind' && parent.indent === fc.indent) ||
823
- (this.type === 'newline' &&
824
- !parent.items[parent.items.length - 1].sep))) {
825
- yield* this.pop();
826
- yield* this.step();
827
- }
828
- else if (this.type === 'map-value-ind' &&
829
- parent.type !== 'flow-collection') {
830
- const prev = getPrevProps(parent);
831
- const start = getFirstKeyStartProps(prev);
832
- fixFlowSeqItems(fc);
833
- const sep = fc.end.splice(1, fc.end.length);
834
- sep.push(this.sourceToken);
835
- const map = {
836
- type: 'block-map',
837
- offset: fc.offset,
838
- indent: fc.indent,
839
- items: [{ start, key: fc, sep }]
840
- };
841
- this.onKeyLine = true;
842
- this.stack[this.stack.length - 1] = map;
843
- }
844
- else {
845
- yield* this.lineEnd(fc);
846
- }
847
- }
848
- }
849
- flowScalar(type) {
850
- if (this.onNewLine) {
851
- let nl = this.source.indexOf('\n') + 1;
852
- while (nl !== 0) {
853
- this.onNewLine(this.offset + nl);
854
- nl = this.source.indexOf('\n', nl) + 1;
855
- }
856
- }
857
- return {
858
- type,
859
- offset: this.offset,
860
- indent: this.indent,
861
- source: this.source
862
- };
863
- }
864
- startBlockValue(parent) {
865
- switch (this.type) {
866
- case 'alias':
867
- case 'scalar':
868
- case 'single-quoted-scalar':
869
- case 'double-quoted-scalar':
870
- return this.flowScalar(this.type);
871
- case 'block-scalar-header':
872
- return {
873
- type: 'block-scalar',
874
- offset: this.offset,
875
- indent: this.indent,
876
- props: [this.sourceToken],
877
- source: ''
878
- };
879
- case 'flow-map-start':
880
- case 'flow-seq-start':
881
- return {
882
- type: 'flow-collection',
883
- offset: this.offset,
884
- indent: this.indent,
885
- start: this.sourceToken,
886
- items: [],
887
- end: []
888
- };
889
- case 'seq-item-ind':
890
- return {
891
- type: 'block-seq',
892
- offset: this.offset,
893
- indent: this.indent,
894
- items: [{ start: [this.sourceToken] }]
895
- };
896
- case 'explicit-key-ind': {
897
- this.onKeyLine = true;
898
- const prev = getPrevProps(parent);
899
- const start = getFirstKeyStartProps(prev);
900
- start.push(this.sourceToken);
901
- return {
902
- type: 'block-map',
903
- offset: this.offset,
904
- indent: this.indent,
905
- items: [{ start, explicitKey: true }]
906
- };
907
- }
908
- case 'map-value-ind': {
909
- this.onKeyLine = true;
910
- const prev = getPrevProps(parent);
911
- const start = getFirstKeyStartProps(prev);
912
- return {
913
- type: 'block-map',
914
- offset: this.offset,
915
- indent: this.indent,
916
- items: [{ start, key: null, sep: [this.sourceToken] }]
917
- };
918
- }
919
- }
920
- return null;
921
- }
922
- atIndentedComment(start, indent) {
923
- if (this.type !== 'comment')
924
- return false;
925
- if (this.indent <= indent)
926
- return false;
927
- return start.every(st => st.type === 'newline' || st.type === 'space');
928
- }
929
- *documentEnd(docEnd) {
930
- if (this.type !== 'doc-mode') {
931
- if (docEnd.end)
932
- docEnd.end.push(this.sourceToken);
933
- else
934
- docEnd.end = [this.sourceToken];
935
- if (this.type === 'newline')
936
- yield* this.pop();
937
- }
938
- }
939
- *lineEnd(token) {
940
- switch (this.type) {
941
- case 'comma':
942
- case 'doc-start':
943
- case 'doc-end':
944
- case 'flow-seq-end':
945
- case 'flow-map-end':
946
- case 'map-value-ind':
947
- yield* this.pop();
948
- yield* this.step();
949
- break;
950
- case 'newline':
951
- this.onKeyLine = false;
952
- // fallthrough
953
- case 'space':
954
- case 'comment':
955
- default:
956
- // all other values are errors
957
- if (token.end)
958
- token.end.push(this.sourceToken);
959
- else
960
- token.end = [this.sourceToken];
961
- if (this.type === 'newline')
962
- yield* this.pop();
963
- }
964
- }
965
- }
966
-
967
- export { Parser };