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