yaml 2.0.0-2 → 2.0.0-6

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 (347) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +46 -26
  3. package/browser/dist/compose/compose-collection.js +59 -0
  4. package/browser/dist/compose/compose-doc.js +39 -0
  5. package/browser/dist/compose/compose-node.js +72 -0
  6. package/browser/dist/compose/compose-scalar.js +74 -0
  7. package/browser/dist/compose/composer.js +218 -0
  8. package/browser/dist/compose/resolve-block-map.js +93 -0
  9. package/browser/dist/compose/resolve-block-scalar.js +185 -0
  10. package/browser/dist/compose/resolve-block-seq.js +40 -0
  11. package/browser/dist/compose/resolve-end.js +37 -0
  12. package/browser/dist/compose/resolve-flow-collection.js +189 -0
  13. package/browser/dist/compose/resolve-flow-scalar.js +211 -0
  14. package/browser/dist/compose/resolve-props.js +126 -0
  15. package/browser/dist/compose/util-contains-newline.js +34 -0
  16. package/browser/dist/compose/util-empty-scalar-position.js +27 -0
  17. package/browser/dist/compose/util-map-includes.js +17 -0
  18. package/browser/dist/doc/Document.js +251 -348
  19. package/browser/dist/doc/anchors.js +72 -0
  20. package/browser/dist/doc/applyReviver.js +41 -28
  21. package/browser/dist/doc/createNode.js +71 -55
  22. package/browser/dist/doc/directives.js +161 -0
  23. package/browser/dist/errors.js +50 -72
  24. package/browser/dist/index.js +17 -73
  25. package/browser/dist/log.js +11 -7
  26. package/browser/dist/nodes/Alias.js +94 -0
  27. package/browser/dist/nodes/Collection.js +133 -0
  28. package/browser/dist/nodes/Node.js +41 -0
  29. package/browser/dist/nodes/Pair.js +28 -0
  30. package/browser/dist/nodes/Scalar.js +23 -0
  31. package/browser/dist/nodes/YAMLMap.js +116 -0
  32. package/browser/dist/nodes/YAMLSeq.js +105 -0
  33. package/browser/dist/nodes/addPairToJSMap.js +103 -0
  34. package/browser/dist/nodes/toJS.js +35 -0
  35. package/browser/dist/options.js +14 -95
  36. package/browser/dist/parse/cst-scalar.js +219 -0
  37. package/browser/dist/parse/cst-stringify.js +61 -0
  38. package/browser/dist/parse/cst-visit.js +97 -0
  39. package/browser/dist/parse/cst.js +98 -0
  40. package/browser/dist/parse/lexer.js +656 -0
  41. package/browser/dist/parse/line-counter.js +39 -0
  42. package/browser/dist/parse/parser.js +876 -0
  43. package/browser/dist/public-api.js +101 -0
  44. package/browser/dist/schema/Schema.js +23 -0
  45. package/browser/dist/schema/common/map.js +42 -0
  46. package/browser/dist/schema/common/null.js +13 -0
  47. package/browser/dist/schema/common/seq.js +33 -0
  48. package/browser/dist/schema/common/string.js +14 -0
  49. package/browser/dist/schema/core/bool.js +19 -0
  50. package/browser/dist/schema/core/float.js +40 -0
  51. package/browser/dist/schema/core/int.js +38 -0
  52. package/browser/dist/schema/core/schema.js +23 -0
  53. package/browser/dist/schema/json/schema.js +62 -0
  54. package/browser/dist/schema/tags.js +79 -0
  55. package/browser/dist/schema/yaml-1.1/binary.js +66 -0
  56. package/browser/dist/schema/yaml-1.1/bool.js +26 -0
  57. package/browser/dist/schema/yaml-1.1/float.js +43 -0
  58. package/browser/dist/schema/yaml-1.1/int.js +71 -0
  59. package/browser/dist/schema/yaml-1.1/omap.js +73 -0
  60. package/browser/dist/schema/yaml-1.1/pairs.js +77 -0
  61. package/browser/dist/schema/yaml-1.1/schema.js +37 -0
  62. package/browser/dist/schema/yaml-1.1/set.js +87 -0
  63. package/browser/dist/schema/yaml-1.1/timestamp.js +101 -0
  64. package/browser/dist/stringify/foldFlowLines.js +121 -139
  65. package/browser/dist/stringify/stringify.js +87 -72
  66. package/browser/dist/stringify/stringifyCollection.js +122 -0
  67. package/browser/dist/stringify/stringifyComment.js +14 -0
  68. package/browser/dist/stringify/stringifyDocument.js +64 -0
  69. package/browser/dist/stringify/stringifyNumber.js +20 -23
  70. package/browser/dist/stringify/stringifyPair.js +111 -0
  71. package/browser/dist/stringify/stringifyString.js +295 -322
  72. package/browser/dist/util.js +4 -4
  73. package/browser/dist/visit.js +146 -0
  74. package/browser/index.js +4 -1
  75. package/dist/compose/compose-collection.d.ts +5 -0
  76. package/dist/compose/compose-collection.js +61 -0
  77. package/dist/compose/compose-doc.d.ts +6 -0
  78. package/dist/compose/compose-doc.js +41 -0
  79. package/dist/compose/compose-node.d.ts +25 -0
  80. package/dist/compose/compose-node.js +75 -0
  81. package/dist/compose/compose-scalar.d.ts +5 -0
  82. package/dist/compose/compose-scalar.js +76 -0
  83. package/dist/compose/composer.d.ts +62 -0
  84. package/dist/compose/composer.js +222 -0
  85. package/dist/compose/resolve-block-map.d.ts +6 -0
  86. package/dist/compose/resolve-block-map.js +95 -0
  87. package/dist/compose/resolve-block-scalar.d.ts +10 -0
  88. package/dist/compose/resolve-block-scalar.js +187 -0
  89. package/dist/compose/resolve-block-seq.d.ts +5 -0
  90. package/dist/compose/resolve-block-seq.js +42 -0
  91. package/dist/compose/resolve-end.d.ts +6 -0
  92. package/dist/compose/resolve-end.js +39 -0
  93. package/dist/compose/resolve-flow-collection.d.ts +6 -0
  94. package/dist/compose/resolve-flow-collection.js +191 -0
  95. package/dist/compose/resolve-flow-scalar.d.ts +10 -0
  96. package/dist/compose/resolve-flow-scalar.js +213 -0
  97. package/dist/compose/resolve-props.d.ts +21 -0
  98. package/dist/compose/resolve-props.js +128 -0
  99. package/dist/compose/util-contains-newline.d.ts +2 -0
  100. package/dist/compose/util-contains-newline.js +36 -0
  101. package/dist/compose/util-empty-scalar-position.d.ts +2 -0
  102. package/dist/compose/util-empty-scalar-position.js +29 -0
  103. package/dist/compose/util-map-includes.d.ts +4 -0
  104. package/dist/compose/util-map-includes.js +19 -0
  105. package/dist/doc/Document.d.ts +130 -0
  106. package/dist/doc/Document.js +250 -347
  107. package/dist/doc/anchors.d.ts +24 -0
  108. package/dist/doc/anchors.js +77 -0
  109. package/dist/doc/applyReviver.d.ts +9 -0
  110. package/dist/doc/applyReviver.js +41 -28
  111. package/dist/doc/createNode.d.ts +16 -0
  112. package/dist/doc/createNode.js +71 -55
  113. package/dist/doc/directives.d.ts +46 -0
  114. package/dist/doc/directives.js +163 -0
  115. package/dist/errors.d.ts +21 -0
  116. package/dist/errors.js +51 -74
  117. package/dist/index.d.ts +19 -0
  118. package/dist/index.js +40 -71
  119. package/dist/log.d.ts +3 -0
  120. package/dist/log.js +11 -6
  121. package/dist/nodes/Alias.d.ts +24 -0
  122. package/dist/nodes/Alias.js +96 -0
  123. package/dist/nodes/Collection.d.ts +67 -0
  124. package/dist/nodes/Collection.js +137 -0
  125. package/dist/nodes/Node.d.ts +48 -0
  126. package/dist/nodes/Node.js +59 -0
  127. package/dist/nodes/Pair.d.ts +16 -0
  128. package/dist/nodes/Pair.js +31 -0
  129. package/dist/nodes/Scalar.d.ts +40 -0
  130. package/dist/nodes/Scalar.js +26 -0
  131. package/dist/nodes/YAMLMap.d.ts +39 -0
  132. package/dist/nodes/YAMLMap.js +119 -0
  133. package/dist/nodes/YAMLSeq.d.ts +53 -0
  134. package/dist/nodes/YAMLSeq.js +107 -0
  135. package/dist/nodes/addPairToJSMap.d.ts +3 -0
  136. package/dist/nodes/addPairToJSMap.js +105 -0
  137. package/dist/nodes/toJS.d.ts +30 -0
  138. package/dist/nodes/toJS.js +37 -0
  139. package/dist/options.d.ts +278 -0
  140. package/dist/options.js +13 -96
  141. package/dist/parse/cst-scalar.d.ts +58 -0
  142. package/dist/parse/cst-scalar.js +223 -0
  143. package/dist/parse/cst-stringify.d.ts +8 -0
  144. package/dist/parse/cst-stringify.js +63 -0
  145. package/dist/parse/cst-visit.d.ts +39 -0
  146. package/dist/parse/cst-visit.js +99 -0
  147. package/dist/parse/cst.d.ts +106 -0
  148. package/dist/parse/cst.js +112 -0
  149. package/dist/parse/lexer.d.ts +85 -0
  150. package/dist/parse/lexer.js +658 -0
  151. package/dist/parse/line-counter.d.ts +22 -0
  152. package/dist/parse/line-counter.js +41 -0
  153. package/dist/parse/parser.d.ts +84 -0
  154. package/dist/parse/parser.js +880 -0
  155. package/dist/public-api.d.ts +43 -0
  156. package/dist/public-api.js +106 -0
  157. package/dist/schema/Schema.d.ts +16 -0
  158. package/dist/schema/Schema.js +25 -0
  159. package/dist/schema/common/map.d.ts +2 -0
  160. package/dist/schema/common/map.js +44 -0
  161. package/dist/schema/common/null.d.ts +4 -0
  162. package/dist/schema/common/null.js +15 -0
  163. package/dist/schema/common/seq.d.ts +2 -0
  164. package/dist/schema/common/seq.js +35 -0
  165. package/dist/schema/common/string.d.ts +2 -0
  166. package/dist/schema/common/string.js +16 -0
  167. package/dist/schema/core/bool.d.ts +4 -0
  168. package/dist/schema/core/bool.js +21 -0
  169. package/dist/schema/core/float.d.ts +4 -0
  170. package/dist/schema/core/float.js +44 -0
  171. package/dist/schema/core/int.d.ts +4 -0
  172. package/dist/schema/core/int.js +42 -0
  173. package/dist/schema/core/schema.d.ts +1 -0
  174. package/dist/schema/core/schema.js +25 -0
  175. package/dist/schema/json/schema.d.ts +2 -0
  176. package/dist/schema/json/schema.js +64 -0
  177. package/dist/schema/tags.d.ts +41 -0
  178. package/dist/schema/tags.js +82 -0
  179. package/dist/schema/types.d.ts +82 -0
  180. package/dist/schema/yaml-1.1/binary.d.ts +2 -0
  181. package/dist/schema/yaml-1.1/binary.js +68 -0
  182. package/dist/schema/yaml-1.1/bool.d.ts +7 -0
  183. package/dist/schema/yaml-1.1/bool.js +29 -0
  184. package/dist/schema/yaml-1.1/float.d.ts +4 -0
  185. package/dist/schema/yaml-1.1/float.js +47 -0
  186. package/dist/schema/yaml-1.1/int.d.ts +5 -0
  187. package/dist/schema/yaml-1.1/int.js +76 -0
  188. package/dist/schema/yaml-1.1/omap.d.ts +21 -0
  189. package/dist/schema/yaml-1.1/omap.js +76 -0
  190. package/dist/schema/yaml-1.1/pairs.d.ts +10 -0
  191. package/dist/schema/yaml-1.1/pairs.js +81 -0
  192. package/dist/schema/yaml-1.1/schema.d.ts +1 -0
  193. package/dist/schema/yaml-1.1/schema.js +39 -0
  194. package/dist/schema/yaml-1.1/set.d.ts +22 -0
  195. package/dist/schema/yaml-1.1/set.js +90 -0
  196. package/dist/schema/yaml-1.1/timestamp.d.ts +6 -0
  197. package/dist/schema/yaml-1.1/timestamp.js +105 -0
  198. package/dist/stringify/foldFlowLines.d.ts +34 -0
  199. package/dist/stringify/foldFlowLines.js +121 -139
  200. package/dist/stringify/stringify.d.ts +18 -0
  201. package/dist/stringify/stringify.js +87 -71
  202. package/dist/stringify/stringifyCollection.d.ts +21 -0
  203. package/dist/stringify/stringifyCollection.js +124 -0
  204. package/dist/stringify/stringifyComment.d.ts +2 -0
  205. package/dist/stringify/stringifyComment.js +17 -0
  206. package/dist/stringify/stringifyDocument.d.ts +3 -0
  207. package/dist/stringify/stringifyDocument.js +66 -0
  208. package/dist/stringify/stringifyNumber.d.ts +2 -0
  209. package/dist/stringify/stringifyNumber.js +20 -23
  210. package/dist/stringify/stringifyPair.d.ts +3 -0
  211. package/dist/stringify/stringifyPair.js +113 -0
  212. package/dist/stringify/stringifyString.d.ts +3 -0
  213. package/dist/stringify/stringifyString.js +295 -322
  214. package/dist/test-events.d.ts +4 -0
  215. package/dist/test-events.js +124 -148
  216. package/dist/util.d.ts +6 -0
  217. package/dist/util.js +9 -12
  218. package/dist/visit.d.ts +63 -0
  219. package/dist/visit.js +148 -0
  220. package/package.json +31 -39
  221. package/util.js +2 -14
  222. package/browser/dist/_virtual/_rollupPluginBabelHelpers.js +0 -16
  223. package/browser/dist/ast/Alias.js +0 -95
  224. package/browser/dist/ast/Collection.js +0 -192
  225. package/browser/dist/ast/Merge.js +0 -71
  226. package/browser/dist/ast/Node.js +0 -3
  227. package/browser/dist/ast/Pair.js +0 -200
  228. package/browser/dist/ast/Scalar.js +0 -21
  229. package/browser/dist/ast/YAMLMap.js +0 -91
  230. package/browser/dist/ast/YAMLSeq.js +0 -67
  231. package/browser/dist/ast/toJS.js +0 -29
  232. package/browser/dist/ast/visit.js +0 -83
  233. package/browser/dist/constants.js +0 -40
  234. package/browser/dist/cst/Alias.js +0 -26
  235. package/browser/dist/cst/BlankLine.js +0 -34
  236. package/browser/dist/cst/BlockValue.js +0 -240
  237. package/browser/dist/cst/Collection.js +0 -259
  238. package/browser/dist/cst/CollectionItem.js +0 -123
  239. package/browser/dist/cst/Comment.js +0 -27
  240. package/browser/dist/cst/Directive.js +0 -53
  241. package/browser/dist/cst/Document.js +0 -262
  242. package/browser/dist/cst/FlowCollection.js +0 -201
  243. package/browser/dist/cst/Node.js +0 -355
  244. package/browser/dist/cst/ParseContext.js +0 -234
  245. package/browser/dist/cst/PlainValue.js +0 -171
  246. package/browser/dist/cst/QuoteDouble.js +0 -224
  247. package/browser/dist/cst/QuoteSingle.js +0 -100
  248. package/browser/dist/cst/Range.js +0 -56
  249. package/browser/dist/cst/parse.js +0 -46
  250. package/browser/dist/cst/source-utils.js +0 -169
  251. package/browser/dist/doc/Anchors.js +0 -115
  252. package/browser/dist/doc/Schema.js +0 -34
  253. package/browser/dist/doc/getSchemaTags.js +0 -33
  254. package/browser/dist/doc/listTagNames.js +0 -27
  255. package/browser/dist/doc/parseContents.js +0 -60
  256. package/browser/dist/doc/parseDirectives.js +0 -107
  257. package/browser/dist/resolve/collection-utils.js +0 -86
  258. package/browser/dist/resolve/resolveMap.js +0 -315
  259. package/browser/dist/resolve/resolveNode.js +0 -173
  260. package/browser/dist/resolve/resolveScalar.js +0 -20
  261. package/browser/dist/resolve/resolveSeq.js +0 -170
  262. package/browser/dist/resolve/resolveTag.js +0 -107
  263. package/browser/dist/resolve/resolveTagName.js +0 -87
  264. package/browser/dist/stringify/addComment.js +0 -10
  265. package/browser/dist/stringify/stringifyTag.js +0 -28
  266. package/browser/dist/tags/core.js +0 -128
  267. package/browser/dist/tags/failsafe/index.js +0 -7
  268. package/browser/dist/tags/failsafe/map.js +0 -37
  269. package/browser/dist/tags/failsafe/seq.js +0 -34
  270. package/browser/dist/tags/failsafe/string.js +0 -20
  271. package/browser/dist/tags/index.js +0 -39
  272. package/browser/dist/tags/json.js +0 -62
  273. package/browser/dist/tags/options.js +0 -31
  274. package/browser/dist/tags/yaml-1.1/binary.js +0 -83
  275. package/browser/dist/tags/yaml-1.1/index.js +0 -174
  276. package/browser/dist/tags/yaml-1.1/omap.js +0 -86
  277. package/browser/dist/tags/yaml-1.1/pairs.js +0 -62
  278. package/browser/dist/tags/yaml-1.1/set.js +0 -78
  279. package/browser/dist/tags/yaml-1.1/timestamp.js +0 -101
  280. package/browser/dist/types.js +0 -10
  281. package/cst.d.ts +0 -185
  282. package/dist/_virtual/_rollupPluginBabelHelpers.js +0 -18
  283. package/dist/ast/Alias.js +0 -97
  284. package/dist/ast/Collection.js +0 -196
  285. package/dist/ast/Merge.js +0 -74
  286. package/dist/ast/Node.js +0 -5
  287. package/dist/ast/Pair.js +0 -203
  288. package/dist/ast/Scalar.js +0 -24
  289. package/dist/ast/YAMLMap.js +0 -94
  290. package/dist/ast/YAMLSeq.js +0 -69
  291. package/dist/ast/toJS.js +0 -31
  292. package/dist/ast/visit.js +0 -85
  293. package/dist/constants.js +0 -46
  294. package/dist/cst/Alias.js +0 -28
  295. package/dist/cst/BlankLine.js +0 -36
  296. package/dist/cst/BlockValue.js +0 -243
  297. package/dist/cst/Collection.js +0 -262
  298. package/dist/cst/CollectionItem.js +0 -125
  299. package/dist/cst/Comment.js +0 -29
  300. package/dist/cst/Directive.js +0 -55
  301. package/dist/cst/Document.js +0 -264
  302. package/dist/cst/FlowCollection.js +0 -203
  303. package/dist/cst/Node.js +0 -357
  304. package/dist/cst/ParseContext.js +0 -236
  305. package/dist/cst/PlainValue.js +0 -173
  306. package/dist/cst/QuoteDouble.js +0 -226
  307. package/dist/cst/QuoteSingle.js +0 -102
  308. package/dist/cst/Range.js +0 -58
  309. package/dist/cst/parse.js +0 -48
  310. package/dist/cst/source-utils.js +0 -173
  311. package/dist/doc/Anchors.js +0 -117
  312. package/dist/doc/Schema.js +0 -36
  313. package/dist/doc/getSchemaTags.js +0 -35
  314. package/dist/doc/listTagNames.js +0 -29
  315. package/dist/doc/parseContents.js +0 -62
  316. package/dist/doc/parseDirectives.js +0 -109
  317. package/dist/resolve/collection-utils.js +0 -91
  318. package/dist/resolve/resolveMap.js +0 -317
  319. package/dist/resolve/resolveNode.js +0 -175
  320. package/dist/resolve/resolveScalar.js +0 -22
  321. package/dist/resolve/resolveSeq.js +0 -172
  322. package/dist/resolve/resolveTag.js +0 -109
  323. package/dist/resolve/resolveTagName.js +0 -89
  324. package/dist/stringify/addComment.js +0 -13
  325. package/dist/stringify/stringifyTag.js +0 -30
  326. package/dist/tags/core.js +0 -138
  327. package/dist/tags/failsafe/index.js +0 -9
  328. package/dist/tags/failsafe/map.js +0 -39
  329. package/dist/tags/failsafe/seq.js +0 -36
  330. package/dist/tags/failsafe/string.js +0 -22
  331. package/dist/tags/index.js +0 -42
  332. package/dist/tags/json.js +0 -64
  333. package/dist/tags/options.js +0 -37
  334. package/dist/tags/yaml-1.1/binary.js +0 -85
  335. package/dist/tags/yaml-1.1/index.js +0 -176
  336. package/dist/tags/yaml-1.1/omap.js +0 -89
  337. package/dist/tags/yaml-1.1/pairs.js +0 -66
  338. package/dist/tags/yaml-1.1/set.js +0 -81
  339. package/dist/tags/yaml-1.1/timestamp.js +0 -105
  340. package/dist/types.js +0 -29
  341. package/index.d.ts +0 -519
  342. package/index.js +0 -1
  343. package/types.d.ts +0 -379
  344. package/types.js +0 -17
  345. package/types.mjs +0 -17
  346. package/util.d.ts +0 -76
  347. package/util.mjs +0 -15
@@ -0,0 +1,658 @@
1
+ 'use strict';
2
+
3
+ var cst = require('./cst.js');
4
+
5
+ /*
6
+ START -> stream
7
+
8
+ stream
9
+ directive -> line-end -> stream
10
+ indent + line-end -> stream
11
+ [else] -> line-start
12
+
13
+ line-end
14
+ comment -> line-end
15
+ newline -> .
16
+ input-end -> END
17
+
18
+ line-start
19
+ doc-start -> doc
20
+ doc-end -> stream
21
+ [else] -> indent -> block-start
22
+
23
+ block-start
24
+ seq-item-start -> block-start
25
+ explicit-key-start -> block-start
26
+ map-value-start -> block-start
27
+ [else] -> doc
28
+
29
+ doc
30
+ line-end -> line-start
31
+ spaces -> doc
32
+ anchor -> doc
33
+ tag -> doc
34
+ flow-start -> flow -> doc
35
+ flow-end -> error -> doc
36
+ seq-item-start -> error -> doc
37
+ explicit-key-start -> error -> doc
38
+ map-value-start -> doc
39
+ alias -> doc
40
+ quote-start -> quoted-scalar -> doc
41
+ block-scalar-header -> line-end -> block-scalar(min) -> line-start
42
+ [else] -> plain-scalar(false, min) -> doc
43
+
44
+ flow
45
+ line-end -> flow
46
+ spaces -> flow
47
+ anchor -> flow
48
+ tag -> flow
49
+ flow-start -> flow -> flow
50
+ flow-end -> .
51
+ seq-item-start -> error -> flow
52
+ explicit-key-start -> flow
53
+ map-value-start -> flow
54
+ alias -> flow
55
+ quote-start -> quoted-scalar -> flow
56
+ comma -> flow
57
+ [else] -> plain-scalar(true, 0) -> flow
58
+
59
+ quoted-scalar
60
+ quote-end -> .
61
+ [else] -> quoted-scalar
62
+
63
+ block-scalar(min)
64
+ newline + peek(indent < min) -> .
65
+ [else] -> block-scalar(min)
66
+
67
+ plain-scalar(is-flow, min)
68
+ scalar-end(is-flow) -> .
69
+ peek(newline + (indent < min)) -> .
70
+ [else] -> plain-scalar(min)
71
+ */
72
+ function isEmpty(ch) {
73
+ switch (ch) {
74
+ case undefined:
75
+ case ' ':
76
+ case '\n':
77
+ case '\r':
78
+ case '\t':
79
+ return true;
80
+ default:
81
+ return false;
82
+ }
83
+ }
84
+ const invalidFlowScalarChars = [',', '[', ']', '{', '}'];
85
+ const invalidIdentifierChars = [' ', ',', '[', ']', '{', '}', '\n', '\r', '\t'];
86
+ const isNotIdentifierChar = (ch) => !ch || invalidIdentifierChars.includes(ch);
87
+ /**
88
+ * Splits an input string into lexical tokens, i.e. smaller strings that are
89
+ * easily identifiable by `tokens.tokenType()`.
90
+ *
91
+ * Lexing starts always in a "stream" context. Incomplete input may be buffered
92
+ * until a complete token can be emitted.
93
+ *
94
+ * In addition to slices of the original input, the following control characters
95
+ * may also be emitted:
96
+ *
97
+ * - `\x02` (Start of Text): A document starts with the next token
98
+ * - `\x18` (Cancel): Unexpected end of flow-mode (indicates an error)
99
+ * - `\x1f` (Unit Separator): Next token is a scalar value
100
+ * - `\u{FEFF}` (Byte order mark): Emitted separately outside documents
101
+ */
102
+ class Lexer {
103
+ constructor() {
104
+ /**
105
+ * Flag indicating whether the end of the current buffer marks the end of
106
+ * all input
107
+ */
108
+ this.atEnd = false;
109
+ /**
110
+ * Explicit indent set in block scalar header, as an offset from the current
111
+ * minimum indent, so e.g. set to 1 from a header `|2+`. Set to -1 if not
112
+ * explicitly set.
113
+ */
114
+ this.blockScalarIndent = -1;
115
+ /**
116
+ * Block scalars that include a + (keep) chomping indicator in their header
117
+ * include trailing empty lines, which are otherwise excluded from the
118
+ * scalar's contents.
119
+ */
120
+ this.blockScalarKeep = false;
121
+ /** Current input */
122
+ this.buffer = '';
123
+ /**
124
+ * Flag noting whether the map value indicator : can immediately follow this
125
+ * node within a flow context.
126
+ */
127
+ this.flowKey = false;
128
+ /** Count of surrounding flow collection levels. */
129
+ this.flowLevel = 0;
130
+ /**
131
+ * Minimum level of indentation required for next lines to be parsed as a
132
+ * part of the current scalar value.
133
+ */
134
+ this.indentNext = 0;
135
+ /** Indentation level of the current line. */
136
+ this.indentValue = 0;
137
+ /** Stores the state of the lexer if reaching the end of incpomplete input */
138
+ this.next = null;
139
+ /** A pointer to `buffer`; the current position of the lexer. */
140
+ this.pos = 0;
141
+ }
142
+ /**
143
+ * Generate YAML tokens from the `source` string. If `incomplete`,
144
+ * a part of the last line may be left as a buffer for the next call.
145
+ *
146
+ * @returns A generator of lexical tokens
147
+ */
148
+ *lex(source, incomplete = false) {
149
+ if (source)
150
+ this.buffer = this.buffer ? this.buffer + source : source;
151
+ this.atEnd = !incomplete;
152
+ let next = this.next || 'stream';
153
+ while (next && (incomplete || this.hasChars(1)))
154
+ next = yield* this.parseNext(next);
155
+ }
156
+ atLineEnd() {
157
+ let i = this.pos;
158
+ let ch = this.buffer[i];
159
+ while (ch === ' ' || ch === '\t')
160
+ ch = this.buffer[++i];
161
+ if (!ch || ch === '#' || ch === '\n')
162
+ return true;
163
+ if (ch === '\r')
164
+ return this.buffer[i + 1] === '\n';
165
+ return false;
166
+ }
167
+ charAt(n) {
168
+ return this.buffer[this.pos + n];
169
+ }
170
+ continueScalar(offset) {
171
+ let ch = this.buffer[offset];
172
+ if (this.indentNext > 0) {
173
+ let indent = 0;
174
+ while (ch === ' ')
175
+ ch = this.buffer[++indent + offset];
176
+ if (ch === '\r') {
177
+ const next = this.buffer[indent + offset + 1];
178
+ if (next === '\n' || (!next && !this.atEnd))
179
+ return offset + indent + 1;
180
+ }
181
+ return ch === '\n' || indent >= this.indentNext || (!ch && !this.atEnd)
182
+ ? offset + indent
183
+ : -1;
184
+ }
185
+ if (ch === '-' || ch === '.') {
186
+ const dt = this.buffer.substr(offset, 3);
187
+ if ((dt === '---' || dt === '...') && isEmpty(this.buffer[offset + 3]))
188
+ return -1;
189
+ }
190
+ return offset;
191
+ }
192
+ getLine() {
193
+ let end = this.buffer.indexOf('\n', this.pos);
194
+ if (end === -1)
195
+ return this.atEnd ? this.buffer.substring(this.pos) : null;
196
+ if (this.buffer[end - 1] === '\r')
197
+ end -= 1;
198
+ return this.buffer.substring(this.pos, end);
199
+ }
200
+ hasChars(n) {
201
+ return this.pos + n <= this.buffer.length;
202
+ }
203
+ setNext(state) {
204
+ this.buffer = this.buffer.substring(this.pos);
205
+ this.pos = 0;
206
+ this.next = state;
207
+ return null;
208
+ }
209
+ peek(n) {
210
+ return this.buffer.substr(this.pos, n);
211
+ }
212
+ *parseNext(next) {
213
+ switch (next) {
214
+ case 'stream':
215
+ return yield* this.parseStream();
216
+ case 'line-start':
217
+ return yield* this.parseLineStart();
218
+ case 'block-start':
219
+ return yield* this.parseBlockStart();
220
+ case 'doc':
221
+ return yield* this.parseDocument();
222
+ case 'flow':
223
+ return yield* this.parseFlowCollection();
224
+ case 'quoted-scalar':
225
+ return yield* this.parseQuotedScalar();
226
+ case 'block-scalar':
227
+ return yield* this.parseBlockScalar();
228
+ case 'plain-scalar':
229
+ return yield* this.parsePlainScalar();
230
+ }
231
+ }
232
+ *parseStream() {
233
+ let line = this.getLine();
234
+ if (line === null)
235
+ return this.setNext('stream');
236
+ if (line[0] === cst.BOM) {
237
+ yield* this.pushCount(1);
238
+ line = line.substring(1);
239
+ }
240
+ if (line[0] === '%') {
241
+ let dirEnd = line.length;
242
+ const cs = line.indexOf('#');
243
+ if (cs !== -1) {
244
+ const ch = line[cs - 1];
245
+ if (ch === ' ' || ch === '\t')
246
+ dirEnd = cs - 1;
247
+ }
248
+ while (true) {
249
+ const ch = line[dirEnd - 1];
250
+ if (ch === ' ' || ch === '\t')
251
+ dirEnd -= 1;
252
+ else
253
+ break;
254
+ }
255
+ const n = (yield* this.pushCount(dirEnd)) + (yield* this.pushSpaces(true));
256
+ yield* this.pushCount(line.length - n); // possible comment
257
+ this.pushNewline();
258
+ return 'stream';
259
+ }
260
+ if (this.atLineEnd()) {
261
+ const sp = yield* this.pushSpaces(true);
262
+ yield* this.pushCount(line.length - sp);
263
+ yield* this.pushNewline();
264
+ return 'stream';
265
+ }
266
+ yield cst.DOCUMENT;
267
+ return yield* this.parseLineStart();
268
+ }
269
+ *parseLineStart() {
270
+ const ch = this.charAt(0);
271
+ if (!ch && !this.atEnd)
272
+ return this.setNext('line-start');
273
+ if (ch === '-' || ch === '.') {
274
+ if (!this.atEnd && !this.hasChars(4))
275
+ return this.setNext('line-start');
276
+ const s = this.peek(3);
277
+ if (s === '---' && isEmpty(this.charAt(3))) {
278
+ yield* this.pushCount(3);
279
+ this.indentValue = 0;
280
+ this.indentNext = 0;
281
+ return 'doc';
282
+ }
283
+ else if (s === '...' && isEmpty(this.charAt(3))) {
284
+ yield* this.pushCount(3);
285
+ return 'stream';
286
+ }
287
+ }
288
+ this.indentValue = yield* this.pushSpaces(false);
289
+ if (this.indentNext > this.indentValue && !isEmpty(this.charAt(1)))
290
+ this.indentNext = this.indentValue;
291
+ return yield* this.parseBlockStart();
292
+ }
293
+ *parseBlockStart() {
294
+ const [ch0, ch1] = this.peek(2);
295
+ if (!ch1 && !this.atEnd)
296
+ return this.setNext('block-start');
297
+ if ((ch0 === '-' || ch0 === '?' || ch0 === ':') && isEmpty(ch1)) {
298
+ const n = (yield* this.pushCount(1)) + (yield* this.pushSpaces(true));
299
+ this.indentNext = this.indentValue + 1;
300
+ this.indentValue += n;
301
+ return yield* this.parseBlockStart();
302
+ }
303
+ return 'doc';
304
+ }
305
+ *parseDocument() {
306
+ yield* this.pushSpaces(true);
307
+ const line = this.getLine();
308
+ if (line === null)
309
+ return this.setNext('doc');
310
+ let n = yield* this.pushIndicators();
311
+ switch (line[n]) {
312
+ case '#':
313
+ yield* this.pushCount(line.length - n);
314
+ // fallthrough
315
+ case undefined:
316
+ yield* this.pushNewline();
317
+ return yield* this.parseLineStart();
318
+ case '{':
319
+ case '[':
320
+ yield* this.pushCount(1);
321
+ this.flowKey = false;
322
+ this.flowLevel = 1;
323
+ return 'flow';
324
+ case '}':
325
+ case ']':
326
+ // this is an error
327
+ yield* this.pushCount(1);
328
+ return 'doc';
329
+ case '*':
330
+ yield* this.pushUntil(isNotIdentifierChar);
331
+ return 'doc';
332
+ case '"':
333
+ case "'":
334
+ return yield* this.parseQuotedScalar();
335
+ case '|':
336
+ case '>':
337
+ n += yield* this.parseBlockScalarHeader();
338
+ n += yield* this.pushSpaces(true);
339
+ yield* this.pushCount(line.length - n);
340
+ yield* this.pushNewline();
341
+ return yield* this.parseBlockScalar();
342
+ default:
343
+ return yield* this.parsePlainScalar();
344
+ }
345
+ }
346
+ *parseFlowCollection() {
347
+ let nl, sp;
348
+ let indent = -1;
349
+ do {
350
+ nl = yield* this.pushNewline();
351
+ sp = yield* this.pushSpaces(true);
352
+ if (nl > 0)
353
+ this.indentValue = indent = sp;
354
+ } while (nl + sp > 0);
355
+ const line = this.getLine();
356
+ if (line === null)
357
+ return this.setNext('flow');
358
+ if ((indent !== -1 && indent < this.indentNext) ||
359
+ (indent === 0 &&
360
+ (line.startsWith('---') || line.startsWith('...')) &&
361
+ isEmpty(line[3]))) {
362
+ // Allowing for the terminal ] or } at the same (rather than greater)
363
+ // indent level as the initial [ or { is technically invalid, but
364
+ // failing here would be surprising to users.
365
+ const atFlowEndMarker = indent === this.indentNext - 1 &&
366
+ this.flowLevel === 1 &&
367
+ (line[0] === ']' || line[0] === '}');
368
+ if (!atFlowEndMarker) {
369
+ // this is an error
370
+ this.flowLevel = 0;
371
+ yield cst.FLOW_END;
372
+ return yield* this.parseLineStart();
373
+ }
374
+ }
375
+ let n = 0;
376
+ while (line[n] === ',')
377
+ n += (yield* this.pushCount(1)) + (yield* this.pushSpaces(true));
378
+ n += yield* this.pushIndicators();
379
+ switch (line[n]) {
380
+ case undefined:
381
+ return 'flow';
382
+ case '#':
383
+ yield* this.pushCount(line.length - n);
384
+ return 'flow';
385
+ case '{':
386
+ case '[':
387
+ yield* this.pushCount(1);
388
+ this.flowKey = false;
389
+ this.flowLevel += 1;
390
+ return 'flow';
391
+ case '}':
392
+ case ']':
393
+ yield* this.pushCount(1);
394
+ this.flowKey = true;
395
+ this.flowLevel -= 1;
396
+ return this.flowLevel ? 'flow' : 'doc';
397
+ case '*':
398
+ yield* this.pushUntil(isNotIdentifierChar);
399
+ return 'flow';
400
+ case '"':
401
+ case "'":
402
+ this.flowKey = true;
403
+ return yield* this.parseQuotedScalar();
404
+ case ':': {
405
+ const next = this.charAt(1);
406
+ if (this.flowKey || isEmpty(next) || next === ',') {
407
+ yield* this.pushCount(1);
408
+ yield* this.pushSpaces(true);
409
+ return 'flow';
410
+ }
411
+ }
412
+ // fallthrough
413
+ default:
414
+ this.flowKey = false;
415
+ return yield* this.parsePlainScalar();
416
+ }
417
+ }
418
+ *parseQuotedScalar() {
419
+ const quote = this.charAt(0);
420
+ let end = this.buffer.indexOf(quote, this.pos + 1);
421
+ if (quote === "'") {
422
+ while (end !== -1 && this.buffer[end + 1] === "'")
423
+ end = this.buffer.indexOf("'", end + 2);
424
+ }
425
+ else {
426
+ // double-quote
427
+ while (end !== -1) {
428
+ let n = 0;
429
+ while (this.buffer[end - 1 - n] === '\\')
430
+ n += 1;
431
+ if (n % 2 === 0)
432
+ break;
433
+ end = this.buffer.indexOf('"', end + 1);
434
+ }
435
+ }
436
+ let nl = this.buffer.indexOf('\n', this.pos);
437
+ if (nl !== -1 && nl < end) {
438
+ while (nl !== -1 && nl < end) {
439
+ const cs = this.continueScalar(nl + 1);
440
+ if (cs === -1)
441
+ break;
442
+ nl = this.buffer.indexOf('\n', cs);
443
+ }
444
+ if (nl !== -1 && nl < end) {
445
+ // this is an error caused by an unexpected unindent
446
+ end = nl - 1;
447
+ }
448
+ }
449
+ if (end === -1) {
450
+ if (!this.atEnd)
451
+ return this.setNext('quoted-scalar');
452
+ end = this.buffer.length;
453
+ }
454
+ yield* this.pushToIndex(end + 1, false);
455
+ return this.flowLevel ? 'flow' : 'doc';
456
+ }
457
+ *parseBlockScalarHeader() {
458
+ this.blockScalarIndent = -1;
459
+ this.blockScalarKeep = false;
460
+ let i = this.pos;
461
+ while (true) {
462
+ const ch = this.buffer[++i];
463
+ if (ch === '+')
464
+ this.blockScalarKeep = true;
465
+ else if (ch > '0' && ch <= '9')
466
+ this.blockScalarIndent = Number(ch) - 1;
467
+ else if (ch !== '-')
468
+ break;
469
+ }
470
+ return yield* this.pushUntil(ch => isEmpty(ch) || ch === '#');
471
+ }
472
+ *parseBlockScalar() {
473
+ let nl = this.pos - 1; // may be -1 if this.pos === 0
474
+ let indent = 0;
475
+ let ch;
476
+ loop: for (let i = this.pos; (ch = this.buffer[i]); ++i) {
477
+ switch (ch) {
478
+ case ' ':
479
+ indent += 1;
480
+ break;
481
+ case '\n':
482
+ nl = i;
483
+ indent = 0;
484
+ break;
485
+ case '\r': {
486
+ const next = this.buffer[i + 1];
487
+ if (!next && !this.atEnd)
488
+ return this.setNext('block-scalar');
489
+ if (next === '\n')
490
+ break;
491
+ } // fallthrough
492
+ default:
493
+ break loop;
494
+ }
495
+ }
496
+ if (!ch && !this.atEnd)
497
+ return this.setNext('block-scalar');
498
+ if (indent >= this.indentNext) {
499
+ if (this.blockScalarIndent === -1)
500
+ this.indentNext = indent;
501
+ else
502
+ this.indentNext += this.blockScalarIndent;
503
+ do {
504
+ const cs = this.continueScalar(nl + 1);
505
+ if (cs === -1)
506
+ break;
507
+ nl = this.buffer.indexOf('\n', cs);
508
+ } while (nl !== -1);
509
+ if (nl === -1) {
510
+ if (!this.atEnd)
511
+ return this.setNext('block-scalar');
512
+ nl = this.buffer.length;
513
+ }
514
+ }
515
+ if (!this.blockScalarKeep) {
516
+ do {
517
+ let i = nl - 1;
518
+ let ch = this.buffer[i];
519
+ if (ch === '\r')
520
+ ch = this.buffer[--i];
521
+ while (ch === ' ' || ch === '\t')
522
+ ch = this.buffer[--i];
523
+ if (ch === '\n' && i >= this.pos)
524
+ nl = i;
525
+ else
526
+ break;
527
+ } while (true);
528
+ }
529
+ yield cst.SCALAR;
530
+ yield* this.pushToIndex(nl + 1, true);
531
+ return yield* this.parseLineStart();
532
+ }
533
+ *parsePlainScalar() {
534
+ const inFlow = this.flowLevel > 0;
535
+ let end = this.pos - 1;
536
+ let i = this.pos - 1;
537
+ let ch;
538
+ while ((ch = this.buffer[++i])) {
539
+ if (ch === ':') {
540
+ const next = this.buffer[i + 1];
541
+ if (isEmpty(next) || (inFlow && next === ','))
542
+ break;
543
+ end = i;
544
+ }
545
+ else if (isEmpty(ch)) {
546
+ const next = this.buffer[i + 1];
547
+ if (next === '#' || (inFlow && invalidFlowScalarChars.includes(next)))
548
+ break;
549
+ if (ch === '\r') {
550
+ if (next === '\n') {
551
+ i += 1;
552
+ ch = '\n';
553
+ }
554
+ else
555
+ end = i;
556
+ }
557
+ if (ch === '\n') {
558
+ const cs = this.continueScalar(i + 1);
559
+ if (cs === -1)
560
+ break;
561
+ i = Math.max(i, cs - 2); // to advance, but still account for ' #'
562
+ }
563
+ }
564
+ else {
565
+ if (inFlow && invalidFlowScalarChars.includes(ch))
566
+ break;
567
+ end = i;
568
+ }
569
+ }
570
+ if (!ch && !this.atEnd)
571
+ return this.setNext('plain-scalar');
572
+ yield cst.SCALAR;
573
+ yield* this.pushToIndex(end + 1, true);
574
+ return inFlow ? 'flow' : 'doc';
575
+ }
576
+ *pushCount(n) {
577
+ if (n > 0) {
578
+ yield this.buffer.substr(this.pos, n);
579
+ this.pos += n;
580
+ return n;
581
+ }
582
+ return 0;
583
+ }
584
+ *pushToIndex(i, allowEmpty) {
585
+ const s = this.buffer.slice(this.pos, i);
586
+ if (s) {
587
+ yield s;
588
+ this.pos += s.length;
589
+ return s.length;
590
+ }
591
+ else if (allowEmpty)
592
+ yield '';
593
+ return 0;
594
+ }
595
+ *pushIndicators() {
596
+ switch (this.charAt(0)) {
597
+ case '!':
598
+ if (this.charAt(1) === '<')
599
+ return ((yield* this.pushVerbatimTag()) +
600
+ (yield* this.pushSpaces(true)) +
601
+ (yield* this.pushIndicators()));
602
+ // fallthrough
603
+ case '&':
604
+ return ((yield* this.pushUntil(isNotIdentifierChar)) +
605
+ (yield* this.pushSpaces(true)) +
606
+ (yield* this.pushIndicators()));
607
+ case ':':
608
+ case '?': // this is an error outside flow collections
609
+ case '-': // this is an error
610
+ if (isEmpty(this.charAt(1))) {
611
+ if (this.flowLevel === 0)
612
+ this.indentNext = this.indentValue + 1;
613
+ return ((yield* this.pushCount(1)) +
614
+ (yield* this.pushSpaces(true)) +
615
+ (yield* this.pushIndicators()));
616
+ }
617
+ }
618
+ return 0;
619
+ }
620
+ *pushVerbatimTag() {
621
+ let i = this.pos + 2;
622
+ let ch = this.buffer[i];
623
+ while (!isEmpty(ch) && ch !== '>')
624
+ ch = this.buffer[++i];
625
+ return yield* this.pushToIndex(ch === '>' ? i + 1 : i, false);
626
+ }
627
+ *pushNewline() {
628
+ const ch = this.buffer[this.pos];
629
+ if (ch === '\n')
630
+ return yield* this.pushCount(1);
631
+ else if (ch === '\r' && this.charAt(1) === '\n')
632
+ return yield* this.pushCount(2);
633
+ else
634
+ return 0;
635
+ }
636
+ *pushSpaces(allowTabs) {
637
+ let i = this.pos - 1;
638
+ let ch;
639
+ do {
640
+ ch = this.buffer[++i];
641
+ } while (ch === ' ' || (allowTabs && ch === '\t'));
642
+ const n = i - this.pos;
643
+ if (n > 0) {
644
+ yield this.buffer.substr(this.pos, n);
645
+ this.pos = i;
646
+ }
647
+ return n;
648
+ }
649
+ *pushUntil(test) {
650
+ let i = this.pos;
651
+ let ch = this.buffer[i];
652
+ while (!test(ch))
653
+ ch = this.buffer[++i];
654
+ return yield* this.pushToIndex(i, false);
655
+ }
656
+ }
657
+
658
+ exports.Lexer = Lexer;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Tracks newlines during parsing in order to provide an efficient API for
3
+ * determining the one-indexed `{ line, col }` position for any offset
4
+ * within the input.
5
+ */
6
+ export declare class LineCounter {
7
+ lineStarts: number[];
8
+ /**
9
+ * Should be called in ascending order. Otherwise, call
10
+ * `lineCounter.lineStarts.sort()` before calling `linePos()`.
11
+ */
12
+ addNewLine: (offset: number) => number;
13
+ /**
14
+ * Performs a binary search and returns the 1-indexed { line, col }
15
+ * position of `offset`. If `line === 0`, `addNewLine` has never been
16
+ * called or `offset` is before the first known newline.
17
+ */
18
+ linePos: (offset: number) => {
19
+ line: number;
20
+ col: number;
21
+ };
22
+ }