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.
- package/README.md +5 -5
- package/browser/dist/compose/compose-collection.js +12 -9
- package/browser/dist/compose/compose-doc.js +23 -6
- package/browser/dist/compose/compose-node.js +20 -17
- package/browser/dist/compose/compose-scalar.js +20 -23
- package/browser/dist/compose/composer.js +105 -76
- package/browser/dist/compose/resolve-block-map.js +38 -29
- package/browser/dist/compose/resolve-block-scalar.js +14 -17
- package/browser/dist/compose/resolve-block-seq.js +21 -17
- package/browser/dist/compose/resolve-end.js +6 -7
- package/browser/dist/compose/resolve-flow-collection.js +153 -199
- package/browser/dist/compose/resolve-flow-scalar.js +49 -25
- package/browser/dist/compose/resolve-props.js +47 -35
- package/browser/dist/compose/util-contains-newline.js +15 -12
- package/browser/dist/doc/Document.js +50 -99
- package/browser/dist/doc/anchors.js +72 -0
- package/browser/dist/doc/createNode.js +27 -17
- package/browser/dist/errors.js +16 -12
- package/browser/dist/index.js +3 -3
- package/browser/dist/nodes/Alias.js +51 -28
- package/browser/dist/nodes/Collection.js +5 -83
- package/browser/dist/nodes/Node.js +2 -1
- package/browser/dist/nodes/Pair.js +9 -232
- package/browser/dist/nodes/YAMLMap.js +8 -4
- package/browser/dist/nodes/YAMLSeq.js +7 -4
- package/browser/dist/nodes/addPairToJSMap.js +103 -0
- package/browser/dist/nodes/toJS.js +12 -10
- package/browser/dist/options.js +0 -2
- package/browser/dist/parse/cst-scalar.js +219 -0
- package/browser/dist/parse/cst-stringify.js +61 -0
- package/browser/dist/parse/cst-visit.js +97 -0
- package/{dist/parse/tokens.js → browser/dist/parse/cst.js} +12 -7
- package/browser/dist/parse/lexer.js +99 -105
- package/browser/dist/parse/parser.js +219 -144
- package/browser/dist/public-api.js +11 -14
- package/browser/dist/schema/Schema.js +23 -0
- package/browser/dist/{tags/failsafe → schema/common}/map.js +0 -0
- package/browser/dist/schema/common/null.js +13 -0
- package/browser/dist/{tags/failsafe → schema/common}/seq.js +1 -1
- package/browser/dist/{tags/failsafe → schema/common}/string.js +0 -0
- package/browser/dist/schema/core/bool.js +19 -0
- package/browser/dist/schema/core/float.js +40 -0
- package/browser/dist/schema/core/int.js +38 -0
- package/browser/dist/schema/core/schema.js +23 -0
- package/browser/dist/{tags/json.js → schema/json/schema.js} +5 -6
- package/browser/dist/schema/tags.js +79 -0
- package/browser/dist/{tags → schema}/yaml-1.1/binary.js +0 -0
- package/browser/dist/schema/yaml-1.1/bool.js +26 -0
- package/browser/dist/schema/yaml-1.1/float.js +43 -0
- package/browser/dist/schema/yaml-1.1/int.js +71 -0
- package/browser/dist/{tags → schema}/yaml-1.1/omap.js +0 -0
- package/browser/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
- package/browser/dist/schema/yaml-1.1/schema.js +37 -0
- package/browser/dist/{tags → schema}/yaml-1.1/set.js +0 -0
- package/browser/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
- package/browser/dist/stringify/addComment.js +1 -7
- package/browser/dist/stringify/stringify.js +8 -6
- package/browser/dist/stringify/stringifyCollection.js +108 -0
- package/browser/dist/stringify/stringifyDocument.js +61 -0
- package/browser/dist/stringify/stringifyPair.js +113 -0
- package/browser/dist/stringify/stringifyString.js +53 -52
- package/browser/dist/visit.js +20 -1
- package/dist/compose/compose-collection.d.ts +4 -4
- package/dist/compose/compose-collection.js +12 -9
- package/dist/compose/compose-doc.d.ts +4 -3
- package/dist/compose/compose-doc.js +23 -6
- package/dist/compose/compose-node.d.ts +15 -7
- package/dist/compose/compose-node.js +20 -17
- package/dist/compose/compose-scalar.d.ts +4 -3
- package/dist/compose/compose-scalar.js +19 -22
- package/dist/compose/composer.d.ts +28 -23
- package/dist/compose/composer.js +106 -77
- package/dist/compose/resolve-block-map.d.ts +4 -4
- package/dist/compose/resolve-block-map.js +38 -29
- package/dist/compose/resolve-block-scalar.d.ts +5 -3
- package/dist/compose/resolve-block-scalar.js +14 -17
- package/dist/compose/resolve-block-seq.d.ts +4 -4
- package/dist/compose/resolve-block-seq.js +21 -17
- package/dist/compose/resolve-end.d.ts +3 -2
- package/dist/compose/resolve-end.js +6 -7
- package/dist/compose/resolve-flow-collection.d.ts +4 -5
- package/dist/compose/resolve-flow-collection.js +152 -198
- package/dist/compose/resolve-flow-scalar.d.ts +5 -3
- package/dist/compose/resolve-flow-scalar.js +49 -25
- package/dist/compose/resolve-props.d.ts +17 -10
- package/dist/compose/resolve-props.js +47 -35
- package/dist/compose/util-contains-newline.d.ts +1 -1
- package/dist/compose/util-contains-newline.js +15 -12
- package/dist/compose/util-empty-scalar-position.d.ts +1 -1
- package/dist/doc/Document.d.ts +27 -15
- package/dist/doc/Document.js +48 -97
- package/dist/doc/anchors.d.ts +24 -0
- package/dist/doc/anchors.js +77 -0
- package/dist/doc/createNode.d.ts +8 -10
- package/dist/doc/createNode.js +26 -16
- package/dist/errors.d.ts +11 -8
- package/dist/errors.js +16 -12
- package/dist/index.d.ts +5 -3
- package/dist/index.js +3 -3
- package/dist/nodes/Alias.d.ts +19 -9
- package/dist/nodes/Alias.js +51 -28
- package/dist/nodes/Collection.d.ts +3 -17
- package/dist/nodes/Collection.js +4 -82
- package/dist/nodes/Node.d.ts +6 -4
- package/dist/nodes/Node.js +2 -0
- package/dist/nodes/Pair.d.ts +6 -11
- package/dist/nodes/Pair.js +8 -231
- package/dist/nodes/Scalar.d.ts +6 -3
- package/dist/nodes/YAMLMap.d.ts +3 -3
- package/dist/nodes/YAMLMap.js +8 -4
- package/dist/nodes/YAMLSeq.d.ts +3 -3
- package/dist/nodes/YAMLSeq.js +7 -4
- package/dist/nodes/addPairToJSMap.d.ts +3 -0
- package/dist/nodes/addPairToJSMap.js +105 -0
- package/dist/nodes/toJS.d.ts +4 -5
- package/dist/nodes/toJS.js +12 -10
- package/dist/options.d.ts +20 -26
- package/dist/options.js +0 -2
- package/dist/parse/cst-scalar.d.ts +58 -0
- package/dist/parse/cst-scalar.js +223 -0
- package/dist/parse/cst-stringify.d.ts +8 -0
- package/dist/parse/cst-stringify.js +63 -0
- package/dist/parse/cst-visit.d.ts +39 -0
- package/dist/parse/cst-visit.js +99 -0
- package/dist/parse/{tokens.d.ts → cst.d.ts} +23 -7
- package/{browser/dist/parse/tokens.js → dist/parse/cst.js} +27 -1
- package/dist/parse/lexer.d.ts +4 -13
- package/dist/parse/lexer.js +100 -106
- package/dist/parse/parser.d.ts +30 -24
- package/dist/parse/parser.js +221 -146
- package/dist/public-api.d.ts +7 -7
- package/dist/public-api.js +11 -14
- package/dist/{doc → schema}/Schema.d.ts +5 -3
- package/dist/schema/Schema.js +25 -0
- package/dist/{tags/failsafe → schema/common}/map.d.ts +0 -0
- package/dist/{tags/failsafe → schema/common}/map.js +0 -0
- package/dist/schema/common/null.d.ts +4 -0
- package/dist/schema/common/null.js +15 -0
- package/dist/{tags/failsafe → schema/common}/seq.d.ts +0 -0
- package/dist/{tags/failsafe → schema/common}/seq.js +1 -1
- package/dist/{tags/failsafe → schema/common}/string.d.ts +0 -0
- package/dist/{tags/failsafe → schema/common}/string.js +0 -0
- package/dist/schema/core/bool.d.ts +4 -0
- package/dist/schema/core/bool.js +21 -0
- package/dist/schema/core/float.d.ts +4 -0
- package/dist/schema/core/float.js +44 -0
- package/dist/schema/core/int.d.ts +4 -0
- package/dist/schema/core/int.js +42 -0
- package/dist/schema/core/schema.d.ts +1 -0
- package/dist/schema/core/schema.js +25 -0
- package/dist/schema/json/schema.d.ts +2 -0
- package/dist/{tags/json.js → schema/json/schema.js} +5 -6
- package/dist/schema/tags.d.ts +41 -0
- package/dist/schema/tags.js +82 -0
- package/dist/{tags → schema}/types.d.ts +3 -7
- package/dist/{tags → schema}/yaml-1.1/binary.d.ts +0 -0
- package/dist/{tags → schema}/yaml-1.1/binary.js +0 -0
- package/dist/schema/yaml-1.1/bool.d.ts +7 -0
- package/dist/schema/yaml-1.1/bool.js +29 -0
- package/dist/schema/yaml-1.1/float.d.ts +4 -0
- package/dist/schema/yaml-1.1/float.js +47 -0
- package/dist/schema/yaml-1.1/int.d.ts +5 -0
- package/dist/schema/yaml-1.1/int.js +76 -0
- package/dist/{tags → schema}/yaml-1.1/omap.d.ts +0 -0
- package/dist/{tags → schema}/yaml-1.1/omap.js +0 -0
- package/dist/{tags → schema}/yaml-1.1/pairs.d.ts +3 -2
- package/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
- package/dist/schema/yaml-1.1/schema.d.ts +1 -0
- package/dist/schema/yaml-1.1/schema.js +39 -0
- package/dist/{tags → schema}/yaml-1.1/set.d.ts +1 -1
- package/dist/{tags → schema}/yaml-1.1/set.js +0 -0
- package/dist/{tags → schema}/yaml-1.1/timestamp.d.ts +0 -0
- package/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
- package/dist/stringify/addComment.js +0 -7
- package/dist/stringify/stringify.d.ts +1 -2
- package/dist/stringify/stringify.js +8 -6
- package/dist/stringify/stringifyCollection.d.ts +21 -0
- package/dist/stringify/stringifyCollection.js +110 -0
- package/dist/stringify/stringifyDocument.d.ts +3 -0
- package/dist/stringify/stringifyDocument.js +63 -0
- package/dist/stringify/stringifyPair.d.ts +3 -0
- package/dist/stringify/stringifyPair.js +115 -0
- package/dist/stringify/stringifyString.js +53 -52
- package/dist/test-events.d.ts +1 -2
- package/dist/test-events.js +25 -12
- package/dist/util.d.ts +1 -1
- package/dist/visit.d.ts +10 -1
- package/dist/visit.js +20 -1
- package/package.json +4 -3
- package/browser/dist/doc/Anchors.js +0 -102
- package/browser/dist/doc/Schema.js +0 -27
- package/browser/dist/doc/getSchemaTags.js +0 -30
- package/browser/dist/tags/core.js +0 -109
- package/browser/dist/tags/failsafe/index.js +0 -7
- package/browser/dist/tags/index.js +0 -34
- package/browser/dist/tags/yaml-1.1/index.js +0 -152
- package/dist/doc/Anchors.d.ts +0 -36
- package/dist/doc/Anchors.js +0 -104
- package/dist/doc/Schema.js +0 -29
- package/dist/doc/getSchemaTags.d.ts +0 -3
- package/dist/doc/getSchemaTags.js +0 -32
- package/dist/tags/core.d.ts +0 -14
- package/dist/tags/core.js +0 -119
- package/dist/tags/failsafe/index.d.ts +0 -1
- package/dist/tags/failsafe/index.js +0 -9
- package/dist/tags/index.d.ts +0 -31
- package/dist/tags/index.js +0 -37
- package/dist/tags/json.d.ts +0 -2
- package/dist/tags/yaml-1.1/index.d.ts +0 -2
- package/dist/tags/yaml-1.1/index.js +0 -154
package/dist/parse/parser.js
CHANGED
|
@@ -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)
|
|
@@ -89,6 +89,7 @@ function getFirstKeyStartProps(prev) {
|
|
|
89
89
|
let i = prev.length;
|
|
90
90
|
loop: while (--i >= 0) {
|
|
91
91
|
switch (prev[i].type) {
|
|
92
|
+
case 'doc-start':
|
|
92
93
|
case 'explicit-key-ind':
|
|
93
94
|
case 'map-value-ind':
|
|
94
95
|
case 'seq-item-ind':
|
|
@@ -101,31 +102,62 @@ function getFirstKeyStartProps(prev) {
|
|
|
101
102
|
}
|
|
102
103
|
return prev.splice(i, prev.length);
|
|
103
104
|
}
|
|
105
|
+
function fixFlowSeqItems(fc) {
|
|
106
|
+
if (fc.start.type === 'flow-seq-start') {
|
|
107
|
+
for (const it of fc.items) {
|
|
108
|
+
if (it.sep &&
|
|
109
|
+
!it.value &&
|
|
110
|
+
!includesToken(it.start, 'explicit-key-ind') &&
|
|
111
|
+
!includesToken(it.sep, 'map-value-ind')) {
|
|
112
|
+
if (it.key)
|
|
113
|
+
it.value = it.key;
|
|
114
|
+
delete it.key;
|
|
115
|
+
if (isFlowToken(it.value)) {
|
|
116
|
+
if (it.value.end)
|
|
117
|
+
Array.prototype.push.apply(it.value.end, it.sep);
|
|
118
|
+
else
|
|
119
|
+
it.value.end = it.sep;
|
|
120
|
+
}
|
|
121
|
+
else
|
|
122
|
+
Array.prototype.push.apply(it.start, it.sep);
|
|
123
|
+
delete it.sep;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
104
128
|
/**
|
|
105
129
|
* A YAML concrete syntax tree (CST) parser
|
|
106
130
|
*
|
|
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
131
|
* ```ts
|
|
111
|
-
* const cst: Token[] = []
|
|
112
|
-
* const parser = new Parser(tok => cst.push(tok))
|
|
113
132
|
* const src: string = ...
|
|
133
|
+
* for (const token of new Parser().parse(src)) {
|
|
134
|
+
* // token: Token
|
|
135
|
+
* }
|
|
136
|
+
* ```
|
|
137
|
+
*
|
|
138
|
+
* To use the parser with a user-provided lexer:
|
|
114
139
|
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
140
|
+
* ```ts
|
|
141
|
+
* function* parse(source: string, lexer: Lexer) {
|
|
142
|
+
* const parser = new Parser()
|
|
143
|
+
* for (const lexeme of lexer.lex(source))
|
|
144
|
+
* yield* parser.next(lexeme)
|
|
145
|
+
* yield* parser.end()
|
|
146
|
+
* }
|
|
147
|
+
*
|
|
148
|
+
* const src: string = ...
|
|
149
|
+
* const lexer = new Lexer()
|
|
150
|
+
* for (const token of parse(src, lexer)) {
|
|
151
|
+
* // token: Token
|
|
152
|
+
* }
|
|
119
153
|
* ```
|
|
120
154
|
*/
|
|
121
155
|
class Parser {
|
|
122
156
|
/**
|
|
123
|
-
* @param push - Called separately with each parsed token
|
|
124
157
|
* @param onNewLine - If defined, called separately with the start position of
|
|
125
158
|
* each new line (in `parse()`, including the start of input).
|
|
126
|
-
* @public
|
|
127
159
|
*/
|
|
128
|
-
constructor(
|
|
160
|
+
constructor(onNewLine) {
|
|
129
161
|
/** If true, space and sequence indicators count as indentation */
|
|
130
162
|
this.atNewLine = true;
|
|
131
163
|
/** If true, next token is a scalar value */
|
|
@@ -142,96 +174,97 @@ class Parser {
|
|
|
142
174
|
this.source = '';
|
|
143
175
|
/** The type of the current token, set in parse() */
|
|
144
176
|
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
177
|
// Must be defined after `next()`
|
|
199
|
-
this.lexer = new lexer.Lexer(
|
|
200
|
-
this.push = push;
|
|
178
|
+
this.lexer = new lexer.Lexer();
|
|
201
179
|
this.onNewLine = onNewLine;
|
|
202
180
|
}
|
|
203
181
|
/**
|
|
204
|
-
* Parse `source` as a YAML stream
|
|
205
|
-
*
|
|
206
|
-
* a part of the last line may be left as a buffer for the next call.
|
|
182
|
+
* Parse `source` as a YAML stream.
|
|
183
|
+
* If `incomplete`, a part of the last line may be left as a buffer for the next call.
|
|
207
184
|
*
|
|
208
|
-
* Errors are not thrown, but
|
|
209
|
-
*
|
|
185
|
+
* Errors are not thrown, but yielded as `{ type: 'error', message }` tokens.
|
|
186
|
+
*
|
|
187
|
+
* @returns A generator of tokens representing each directive, document, and other structure.
|
|
210
188
|
*/
|
|
211
|
-
parse(source, incomplete = false) {
|
|
189
|
+
*parse(source, incomplete = false) {
|
|
212
190
|
if (this.onNewLine && this.offset === 0)
|
|
213
191
|
this.onNewLine(0);
|
|
214
|
-
this.lexer.lex(source, incomplete)
|
|
192
|
+
for (const lexeme of this.lexer.lex(source, incomplete))
|
|
193
|
+
yield* this.next(lexeme);
|
|
215
194
|
if (!incomplete)
|
|
216
|
-
this.end();
|
|
195
|
+
yield* this.end();
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Advance the parser by the `source` of one lexical token.
|
|
199
|
+
*/
|
|
200
|
+
*next(source) {
|
|
201
|
+
this.source = source;
|
|
202
|
+
if (process.env.LOG_TOKENS)
|
|
203
|
+
console.log('|', cst.prettyToken(source));
|
|
204
|
+
if (this.atScalar) {
|
|
205
|
+
this.atScalar = false;
|
|
206
|
+
yield* this.step();
|
|
207
|
+
this.offset += source.length;
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
const type = cst.tokenType(source);
|
|
211
|
+
if (!type) {
|
|
212
|
+
const message = `Not a YAML token: ${source}`;
|
|
213
|
+
yield* this.pop({ type: 'error', offset: this.offset, message, source });
|
|
214
|
+
this.offset += source.length;
|
|
215
|
+
}
|
|
216
|
+
else if (type === 'scalar') {
|
|
217
|
+
this.atNewLine = false;
|
|
218
|
+
this.atScalar = true;
|
|
219
|
+
this.type = 'scalar';
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
this.type = type;
|
|
223
|
+
yield* this.step();
|
|
224
|
+
switch (type) {
|
|
225
|
+
case 'newline':
|
|
226
|
+
this.atNewLine = true;
|
|
227
|
+
this.indent = 0;
|
|
228
|
+
if (this.onNewLine)
|
|
229
|
+
this.onNewLine(this.offset + source.length);
|
|
230
|
+
break;
|
|
231
|
+
case 'space':
|
|
232
|
+
if (this.atNewLine && source[0] === ' ')
|
|
233
|
+
this.indent += source.length;
|
|
234
|
+
break;
|
|
235
|
+
case 'explicit-key-ind':
|
|
236
|
+
case 'map-value-ind':
|
|
237
|
+
case 'seq-item-ind':
|
|
238
|
+
if (this.atNewLine)
|
|
239
|
+
this.indent += source.length;
|
|
240
|
+
break;
|
|
241
|
+
case 'doc-mode':
|
|
242
|
+
return;
|
|
243
|
+
default:
|
|
244
|
+
this.atNewLine = false;
|
|
245
|
+
}
|
|
246
|
+
this.offset += source.length;
|
|
247
|
+
}
|
|
217
248
|
}
|
|
218
249
|
/** Call at end of input to push out any remaining constructions */
|
|
219
|
-
end() {
|
|
250
|
+
*end() {
|
|
220
251
|
while (this.stack.length > 0)
|
|
221
|
-
this.pop();
|
|
252
|
+
yield* this.pop();
|
|
222
253
|
}
|
|
223
254
|
get sourceToken() {
|
|
224
|
-
|
|
255
|
+
const st = {
|
|
225
256
|
type: this.type,
|
|
257
|
+
offset: this.offset,
|
|
226
258
|
indent: this.indent,
|
|
227
259
|
source: this.source
|
|
228
260
|
};
|
|
261
|
+
return st;
|
|
229
262
|
}
|
|
230
|
-
step() {
|
|
263
|
+
*step() {
|
|
231
264
|
const top = this.peek(1);
|
|
232
265
|
if (this.type === 'doc-end' && (!top || top.type !== 'doc-end')) {
|
|
233
266
|
while (this.stack.length > 0)
|
|
234
|
-
this.pop();
|
|
267
|
+
yield* this.pop();
|
|
235
268
|
this.stack.push({
|
|
236
269
|
type: 'doc-end',
|
|
237
270
|
offset: this.offset,
|
|
@@ -240,47 +273,49 @@ class Parser {
|
|
|
240
273
|
return;
|
|
241
274
|
}
|
|
242
275
|
if (!top)
|
|
243
|
-
return this.stream();
|
|
276
|
+
return yield* this.stream();
|
|
244
277
|
switch (top.type) {
|
|
245
278
|
case 'document':
|
|
246
|
-
return this.document(top);
|
|
279
|
+
return yield* this.document(top);
|
|
247
280
|
case 'alias':
|
|
248
281
|
case 'scalar':
|
|
249
282
|
case 'single-quoted-scalar':
|
|
250
283
|
case 'double-quoted-scalar':
|
|
251
|
-
return this.scalar(top);
|
|
284
|
+
return yield* this.scalar(top);
|
|
252
285
|
case 'block-scalar':
|
|
253
|
-
return this.blockScalar(top);
|
|
286
|
+
return yield* this.blockScalar(top);
|
|
254
287
|
case 'block-map':
|
|
255
|
-
return this.blockMap(top);
|
|
288
|
+
return yield* this.blockMap(top);
|
|
256
289
|
case 'block-seq':
|
|
257
|
-
return this.blockSequence(top);
|
|
290
|
+
return yield* this.blockSequence(top);
|
|
258
291
|
case 'flow-collection':
|
|
259
|
-
return this.flowCollection(top);
|
|
292
|
+
return yield* this.flowCollection(top);
|
|
260
293
|
case 'doc-end':
|
|
261
|
-
return this.documentEnd(top);
|
|
294
|
+
return yield* this.documentEnd(top);
|
|
262
295
|
}
|
|
263
296
|
/* istanbul ignore next should not happen */
|
|
264
|
-
this.pop();
|
|
297
|
+
yield* this.pop();
|
|
265
298
|
}
|
|
266
299
|
peek(n) {
|
|
267
300
|
return this.stack[this.stack.length - n];
|
|
268
301
|
}
|
|
269
|
-
pop(error) {
|
|
302
|
+
*pop(error) {
|
|
270
303
|
const token = error || this.stack.pop();
|
|
271
304
|
/* istanbul ignore if should not happen */
|
|
272
305
|
if (!token) {
|
|
273
306
|
const message = 'Tried to pop an empty stack';
|
|
274
|
-
|
|
307
|
+
yield { type: 'error', offset: this.offset, source: '', message };
|
|
275
308
|
}
|
|
276
309
|
else if (this.stack.length === 0) {
|
|
277
|
-
|
|
310
|
+
yield token;
|
|
278
311
|
}
|
|
279
312
|
else {
|
|
280
313
|
const top = this.peek(1);
|
|
281
314
|
// For these, parent indent is needed instead of own
|
|
282
315
|
if (token.type === 'block-scalar' || token.type === 'flow-collection')
|
|
283
316
|
token.indent = 'indent' in top ? top.indent : -1;
|
|
317
|
+
if (token.type === 'flow-collection')
|
|
318
|
+
fixFlowSeqItems(token);
|
|
284
319
|
switch (top.type) {
|
|
285
320
|
case 'document':
|
|
286
321
|
top.value = token;
|
|
@@ -313,13 +348,20 @@ class Parser {
|
|
|
313
348
|
it.value = token;
|
|
314
349
|
break;
|
|
315
350
|
}
|
|
316
|
-
case 'flow-collection':
|
|
317
|
-
top.items.
|
|
318
|
-
|
|
351
|
+
case 'flow-collection': {
|
|
352
|
+
const it = top.items[top.items.length - 1];
|
|
353
|
+
if (!it || it.value)
|
|
354
|
+
top.items.push({ start: [], key: token, sep: [] });
|
|
355
|
+
else if (it.sep)
|
|
356
|
+
it.value = token;
|
|
357
|
+
else
|
|
358
|
+
Object.assign(it, { key: token, sep: [] });
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
319
361
|
/* istanbul ignore next should not happen */
|
|
320
362
|
default:
|
|
321
|
-
this.pop();
|
|
322
|
-
this.pop(token);
|
|
363
|
+
yield* this.pop();
|
|
364
|
+
yield* this.pop(token);
|
|
323
365
|
}
|
|
324
366
|
if ((top.type === 'document' ||
|
|
325
367
|
top.type === 'block-map' ||
|
|
@@ -342,16 +384,16 @@ class Parser {
|
|
|
342
384
|
}
|
|
343
385
|
}
|
|
344
386
|
}
|
|
345
|
-
stream() {
|
|
387
|
+
*stream() {
|
|
346
388
|
switch (this.type) {
|
|
347
389
|
case 'directive-line':
|
|
348
|
-
|
|
390
|
+
yield { type: 'directive', offset: this.offset, source: this.source };
|
|
349
391
|
return;
|
|
350
392
|
case 'byte-order-mark':
|
|
351
393
|
case 'space':
|
|
352
394
|
case 'comment':
|
|
353
395
|
case 'newline':
|
|
354
|
-
this.
|
|
396
|
+
yield this.sourceToken;
|
|
355
397
|
return;
|
|
356
398
|
case 'doc-mode':
|
|
357
399
|
case 'doc-start': {
|
|
@@ -366,21 +408,21 @@ class Parser {
|
|
|
366
408
|
return;
|
|
367
409
|
}
|
|
368
410
|
}
|
|
369
|
-
|
|
411
|
+
yield {
|
|
370
412
|
type: 'error',
|
|
371
413
|
offset: this.offset,
|
|
372
414
|
message: `Unexpected ${this.type} token in YAML stream`,
|
|
373
415
|
source: this.source
|
|
374
|
-
}
|
|
416
|
+
};
|
|
375
417
|
}
|
|
376
|
-
document(doc) {
|
|
418
|
+
*document(doc) {
|
|
377
419
|
if (doc.value)
|
|
378
|
-
return this.lineEnd(doc);
|
|
420
|
+
return yield* this.lineEnd(doc);
|
|
379
421
|
switch (this.type) {
|
|
380
422
|
case 'doc-start': {
|
|
381
423
|
if (includesNonEmpty(doc.start)) {
|
|
382
|
-
this.pop();
|
|
383
|
-
this.step();
|
|
424
|
+
yield* this.pop();
|
|
425
|
+
yield* this.step();
|
|
384
426
|
}
|
|
385
427
|
else
|
|
386
428
|
doc.start.push(this.sourceToken);
|
|
@@ -398,15 +440,15 @@ class Parser {
|
|
|
398
440
|
if (bv)
|
|
399
441
|
this.stack.push(bv);
|
|
400
442
|
else {
|
|
401
|
-
|
|
443
|
+
yield {
|
|
402
444
|
type: 'error',
|
|
403
445
|
offset: this.offset,
|
|
404
446
|
message: `Unexpected ${this.type} token in YAML document`,
|
|
405
447
|
source: this.source
|
|
406
|
-
}
|
|
448
|
+
};
|
|
407
449
|
}
|
|
408
450
|
}
|
|
409
|
-
scalar(scalar) {
|
|
451
|
+
*scalar(scalar) {
|
|
410
452
|
if (this.type === 'map-value-ind') {
|
|
411
453
|
const prev = getPrevProps(this.peek(2));
|
|
412
454
|
const start = getFirstKeyStartProps(prev);
|
|
@@ -428,9 +470,9 @@ class Parser {
|
|
|
428
470
|
this.stack[this.stack.length - 1] = map;
|
|
429
471
|
}
|
|
430
472
|
else
|
|
431
|
-
this.lineEnd(scalar);
|
|
473
|
+
yield* this.lineEnd(scalar);
|
|
432
474
|
}
|
|
433
|
-
blockScalar(scalar) {
|
|
475
|
+
*blockScalar(scalar) {
|
|
434
476
|
switch (this.type) {
|
|
435
477
|
case 'space':
|
|
436
478
|
case 'comment':
|
|
@@ -449,15 +491,15 @@ class Parser {
|
|
|
449
491
|
nl = this.source.indexOf('\n', nl) + 1;
|
|
450
492
|
}
|
|
451
493
|
}
|
|
452
|
-
this.pop();
|
|
494
|
+
yield* this.pop();
|
|
453
495
|
break;
|
|
454
496
|
/* istanbul ignore next should not happen */
|
|
455
497
|
default:
|
|
456
|
-
this.pop();
|
|
457
|
-
this.step();
|
|
498
|
+
yield* this.pop();
|
|
499
|
+
yield* this.step();
|
|
458
500
|
}
|
|
459
501
|
}
|
|
460
|
-
blockMap(map) {
|
|
502
|
+
*blockMap(map) {
|
|
461
503
|
var _a;
|
|
462
504
|
const it = map.items[map.items.length - 1];
|
|
463
505
|
// it.sep is true-ish if pair already has key or : separator
|
|
@@ -578,10 +620,10 @@ class Parser {
|
|
|
578
620
|
}
|
|
579
621
|
}
|
|
580
622
|
}
|
|
581
|
-
this.pop();
|
|
582
|
-
this.step();
|
|
623
|
+
yield* this.pop();
|
|
624
|
+
yield* this.step();
|
|
583
625
|
}
|
|
584
|
-
blockSequence(seq) {
|
|
626
|
+
*blockSequence(seq) {
|
|
585
627
|
var _a;
|
|
586
628
|
const it = seq.items[seq.items.length - 1];
|
|
587
629
|
switch (this.type) {
|
|
@@ -620,38 +662,65 @@ class Parser {
|
|
|
620
662
|
}
|
|
621
663
|
if (this.indent > seq.indent) {
|
|
622
664
|
const bv = this.startBlockValue(seq);
|
|
623
|
-
if (bv)
|
|
624
|
-
|
|
665
|
+
if (bv) {
|
|
666
|
+
this.stack.push(bv);
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
625
669
|
}
|
|
626
|
-
this.pop();
|
|
627
|
-
this.step();
|
|
670
|
+
yield* this.pop();
|
|
671
|
+
yield* this.step();
|
|
628
672
|
}
|
|
629
|
-
flowCollection(fc) {
|
|
673
|
+
*flowCollection(fc) {
|
|
674
|
+
const it = fc.items[fc.items.length - 1];
|
|
630
675
|
if (this.type === 'flow-error-end') {
|
|
631
676
|
let top;
|
|
632
677
|
do {
|
|
633
|
-
this.pop();
|
|
678
|
+
yield* this.pop();
|
|
634
679
|
top = this.peek(1);
|
|
635
680
|
} while (top && top.type === 'flow-collection');
|
|
636
681
|
}
|
|
637
682
|
else if (fc.end.length === 0) {
|
|
638
683
|
switch (this.type) {
|
|
639
|
-
case 'space':
|
|
640
|
-
case 'comment':
|
|
641
|
-
case 'newline':
|
|
642
684
|
case 'comma':
|
|
643
685
|
case 'explicit-key-ind':
|
|
686
|
+
if (!it || it.sep)
|
|
687
|
+
fc.items.push({ start: [this.sourceToken] });
|
|
688
|
+
else
|
|
689
|
+
it.start.push(this.sourceToken);
|
|
690
|
+
return;
|
|
644
691
|
case 'map-value-ind':
|
|
692
|
+
if (!it || it.value)
|
|
693
|
+
fc.items.push({ start: [], key: null, sep: [this.sourceToken] });
|
|
694
|
+
else if (it.sep)
|
|
695
|
+
it.sep.push(this.sourceToken);
|
|
696
|
+
else
|
|
697
|
+
Object.assign(it, { key: null, sep: [this.sourceToken] });
|
|
698
|
+
return;
|
|
699
|
+
case 'space':
|
|
700
|
+
case 'comment':
|
|
701
|
+
case 'newline':
|
|
645
702
|
case 'anchor':
|
|
646
703
|
case 'tag':
|
|
647
|
-
|
|
704
|
+
if (!it || it.value)
|
|
705
|
+
fc.items.push({ start: [this.sourceToken] });
|
|
706
|
+
else if (it.sep)
|
|
707
|
+
it.sep.push(this.sourceToken);
|
|
708
|
+
else
|
|
709
|
+
it.start.push(this.sourceToken);
|
|
648
710
|
return;
|
|
649
711
|
case 'alias':
|
|
650
712
|
case 'scalar':
|
|
651
713
|
case 'single-quoted-scalar':
|
|
652
|
-
case 'double-quoted-scalar':
|
|
653
|
-
|
|
714
|
+
case 'double-quoted-scalar': {
|
|
715
|
+
const fs = this.flowScalar(this.type);
|
|
716
|
+
if (!it || it.value)
|
|
717
|
+
fc.items.push({ start: [], key: fs, sep: [] });
|
|
718
|
+
else if (it.sep)
|
|
719
|
+
this.stack.push(fs);
|
|
720
|
+
else
|
|
721
|
+
Object.assign(it, { key: fs, sep: [] });
|
|
654
722
|
return;
|
|
723
|
+
}
|
|
655
724
|
case 'flow-map-end':
|
|
656
725
|
case 'flow-seq-end':
|
|
657
726
|
fc.end.push(this.sourceToken);
|
|
@@ -660,10 +729,10 @@ class Parser {
|
|
|
660
729
|
const bv = this.startBlockValue(fc);
|
|
661
730
|
/* istanbul ignore else should not happen */
|
|
662
731
|
if (bv)
|
|
663
|
-
|
|
732
|
+
this.stack.push(bv);
|
|
664
733
|
else {
|
|
665
|
-
this.pop();
|
|
666
|
-
this.step();
|
|
734
|
+
yield* this.pop();
|
|
735
|
+
yield* this.step();
|
|
667
736
|
}
|
|
668
737
|
}
|
|
669
738
|
else {
|
|
@@ -672,13 +741,14 @@ class Parser {
|
|
|
672
741
|
(this.type === 'map-value-ind' ||
|
|
673
742
|
(this.type === 'newline' &&
|
|
674
743
|
!parent.items[parent.items.length - 1].sep))) {
|
|
675
|
-
this.pop();
|
|
676
|
-
this.step();
|
|
744
|
+
yield* this.pop();
|
|
745
|
+
yield* this.step();
|
|
677
746
|
}
|
|
678
747
|
else if (this.type === 'map-value-ind' &&
|
|
679
748
|
parent.type !== 'flow-collection') {
|
|
680
749
|
const prev = getPrevProps(parent);
|
|
681
750
|
const start = getFirstKeyStartProps(prev);
|
|
751
|
+
fixFlowSeqItems(fc);
|
|
682
752
|
const sep = fc.end.splice(1, fc.end.length);
|
|
683
753
|
sep.push(this.sourceToken);
|
|
684
754
|
const map = {
|
|
@@ -691,7 +761,7 @@ class Parser {
|
|
|
691
761
|
this.stack[this.stack.length - 1] = map;
|
|
692
762
|
}
|
|
693
763
|
else {
|
|
694
|
-
this.lineEnd(fc);
|
|
764
|
+
yield* this.lineEnd(fc);
|
|
695
765
|
}
|
|
696
766
|
}
|
|
697
767
|
}
|
|
@@ -722,7 +792,8 @@ class Parser {
|
|
|
722
792
|
type: 'block-scalar',
|
|
723
793
|
offset: this.offset,
|
|
724
794
|
indent: this.indent,
|
|
725
|
-
props: [this.sourceToken]
|
|
795
|
+
props: [this.sourceToken],
|
|
796
|
+
source: ''
|
|
726
797
|
};
|
|
727
798
|
case 'flow-map-start':
|
|
728
799
|
case 'flow-seq-start':
|
|
@@ -741,14 +812,18 @@ class Parser {
|
|
|
741
812
|
indent: this.indent,
|
|
742
813
|
items: [{ start: [this.sourceToken] }]
|
|
743
814
|
};
|
|
744
|
-
case 'explicit-key-ind':
|
|
815
|
+
case 'explicit-key-ind': {
|
|
745
816
|
this.onKeyLine = true;
|
|
817
|
+
const prev = getPrevProps(parent);
|
|
818
|
+
const start = getFirstKeyStartProps(prev);
|
|
819
|
+
start.push(this.sourceToken);
|
|
746
820
|
return {
|
|
747
821
|
type: 'block-map',
|
|
748
822
|
offset: this.offset,
|
|
749
823
|
indent: this.indent,
|
|
750
|
-
items: [{ start
|
|
824
|
+
items: [{ start }]
|
|
751
825
|
};
|
|
826
|
+
}
|
|
752
827
|
case 'map-value-ind': {
|
|
753
828
|
this.onKeyLine = true;
|
|
754
829
|
const prev = getPrevProps(parent);
|
|
@@ -763,17 +838,17 @@ class Parser {
|
|
|
763
838
|
}
|
|
764
839
|
return null;
|
|
765
840
|
}
|
|
766
|
-
documentEnd(docEnd) {
|
|
841
|
+
*documentEnd(docEnd) {
|
|
767
842
|
if (this.type !== 'doc-mode') {
|
|
768
843
|
if (docEnd.end)
|
|
769
844
|
docEnd.end.push(this.sourceToken);
|
|
770
845
|
else
|
|
771
846
|
docEnd.end = [this.sourceToken];
|
|
772
847
|
if (this.type === 'newline')
|
|
773
|
-
this.pop();
|
|
848
|
+
yield* this.pop();
|
|
774
849
|
}
|
|
775
850
|
}
|
|
776
|
-
lineEnd(token) {
|
|
851
|
+
*lineEnd(token) {
|
|
777
852
|
switch (this.type) {
|
|
778
853
|
case 'comma':
|
|
779
854
|
case 'doc-start':
|
|
@@ -781,8 +856,8 @@ class Parser {
|
|
|
781
856
|
case 'flow-seq-end':
|
|
782
857
|
case 'flow-map-end':
|
|
783
858
|
case 'map-value-ind':
|
|
784
|
-
this.pop();
|
|
785
|
-
this.step();
|
|
859
|
+
yield* this.pop();
|
|
860
|
+
yield* this.step();
|
|
786
861
|
break;
|
|
787
862
|
case 'newline':
|
|
788
863
|
this.onKeyLine = false;
|
|
@@ -796,7 +871,7 @@ class Parser {
|
|
|
796
871
|
else
|
|
797
872
|
token.end = [this.sourceToken];
|
|
798
873
|
if (this.type === 'newline')
|
|
799
|
-
this.pop();
|
|
874
|
+
yield* this.pop();
|
|
800
875
|
}
|
|
801
876
|
}
|
|
802
877
|
}
|
package/dist/public-api.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Composer } from './compose/composer.js';
|
|
|
2
2
|
import type { Reviver } from './doc/applyReviver.js';
|
|
3
3
|
import { Document, Replacer } from './doc/Document.js';
|
|
4
4
|
import type { ParsedNode } from './nodes/Node.js';
|
|
5
|
-
import type {
|
|
5
|
+
import type { CreateNodeOptions, DocumentOptions, ParseOptions, SchemaOptions, ToJSOptions, ToStringOptions } from './options.js';
|
|
6
6
|
export interface EmptyStream extends Array<Document.Parsed>, ReturnType<Composer['streamInfo']> {
|
|
7
7
|
empty: true;
|
|
8
8
|
}
|
|
@@ -15,9 +15,9 @@ export interface EmptyStream extends Array<Document.Parsed>, ReturnType<Composer
|
|
|
15
15
|
* EmptyStream and contain additional stream information. In
|
|
16
16
|
* TypeScript, you should use `'empty' in docs` as a type guard for it.
|
|
17
17
|
*/
|
|
18
|
-
export declare function parseAllDocuments<T extends ParsedNode = ParsedNode>(source: string, options?:
|
|
18
|
+
export declare function parseAllDocuments<T extends ParsedNode = ParsedNode>(source: string, options?: ParseOptions & DocumentOptions & SchemaOptions): Document.Parsed<T>[] | EmptyStream;
|
|
19
19
|
/** Parse an input string into a single YAML.Document */
|
|
20
|
-
export declare function parseDocument<T extends ParsedNode = ParsedNode>(source: string, options?:
|
|
20
|
+
export declare function parseDocument<T extends ParsedNode = ParsedNode>(source: string, options?: ParseOptions & DocumentOptions & SchemaOptions): Document.Parsed<T>;
|
|
21
21
|
/**
|
|
22
22
|
* Parse an input string into JavaScript.
|
|
23
23
|
*
|
|
@@ -31,13 +31,13 @@ export declare function parseDocument<T extends ParsedNode = ParsedNode>(source:
|
|
|
31
31
|
* document, so Maps become objects, Sequences arrays, and scalars result in
|
|
32
32
|
* nulls, booleans, numbers and strings.
|
|
33
33
|
*/
|
|
34
|
-
export declare function parse(src: string, options?:
|
|
35
|
-
export declare function parse(src: string, reviver: Reviver, options?:
|
|
34
|
+
export declare function parse(src: string, options?: ParseOptions & DocumentOptions & SchemaOptions & ToJSOptions): any;
|
|
35
|
+
export declare function parse(src: string, reviver: Reviver, options?: ParseOptions & DocumentOptions & SchemaOptions & ToJSOptions): any;
|
|
36
36
|
/**
|
|
37
37
|
* Stringify a value as a YAML document.
|
|
38
38
|
*
|
|
39
39
|
* @param replacer - A replacer array or function, as in `JSON.stringify()`
|
|
40
40
|
* @returns Will always include `\n` as the last character, as is expected of YAML documents.
|
|
41
41
|
*/
|
|
42
|
-
export declare function stringify(value: any, options?:
|
|
43
|
-
export declare function stringify(value: any, replacer?: Replacer | null, options?: string | number | (
|
|
42
|
+
export declare function stringify(value: any, options?: DocumentOptions & SchemaOptions & ParseOptions & CreateNodeOptions & ToStringOptions): string;
|
|
43
|
+
export declare function stringify(value: any, replacer?: Replacer | null, options?: string | number | (DocumentOptions & SchemaOptions & ParseOptions & CreateNodeOptions & ToStringOptions)): string;
|