yaml 2.0.0-3 → 2.0.0-7

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