parseman 0.46.0 → 0.47.1

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 (316) hide show
  1. package/README.md +8 -3
  2. package/dist/analysis/choice-cost-render.d.ts +12 -4
  3. package/dist/analysis/choice-cost-render.d.ts.map +1 -1
  4. package/dist/analysis/choice-cost.d.ts +2 -2
  5. package/dist/analysis/choice-cost.d.ts.map +1 -1
  6. package/dist/analysis/commitment.d.ts +131 -0
  7. package/dist/analysis/commitment.d.ts.map +1 -0
  8. package/dist/analysis/diagnostics.cjs +10339 -7787
  9. package/dist/analysis/diagnostics.cjs.map +3 -4
  10. package/dist/analysis/diagnostics.d.ts +2 -2
  11. package/dist/analysis/diagnostics.js +10339 -7787
  12. package/dist/analysis/diagnostics.js.map +3 -4
  13. package/dist/analysis/duplication.d.ts.map +1 -1
  14. package/dist/analysis/fix.d.ts +3 -3
  15. package/dist/analysis/fix.d.ts.map +1 -1
  16. package/dist/analysis/format-number.d.ts +23 -0
  17. package/dist/analysis/format-number.d.ts.map +1 -0
  18. package/dist/analysis/gating.d.ts +39 -2
  19. package/dist/analysis/gating.d.ts.map +1 -1
  20. package/dist/analysis/rebuild.d.ts.map +1 -1
  21. package/dist/analysis/terminal.d.ts +5 -2
  22. package/dist/analysis/terminal.d.ts.map +1 -1
  23. package/dist/cli/index.cjs +10924 -8506
  24. package/dist/cli/index.cjs.map +3 -4
  25. package/dist/cli/index.js +10924 -8506
  26. package/dist/cli/index.js.map +3 -4
  27. package/dist/combinators/adjacency.d.ts +100 -0
  28. package/dist/combinators/adjacency.d.ts.map +1 -0
  29. package/dist/combinators/choice.d.ts.map +1 -1
  30. package/dist/combinators/completions.d.ts.map +1 -1
  31. package/dist/combinators/dispatch.d.ts +8 -1
  32. package/dist/combinators/dispatch.d.ts.map +1 -1
  33. package/dist/combinators/expect.d.ts.map +1 -1
  34. package/dist/combinators/first-set.d.ts.map +1 -1
  35. package/dist/combinators/gate.d.ts.map +1 -1
  36. package/dist/combinators/grammar.d.ts +22 -0
  37. package/dist/combinators/grammar.d.ts.map +1 -1
  38. package/dist/combinators/literal.d.ts +8 -0
  39. package/dist/combinators/literal.d.ts.map +1 -1
  40. package/dist/combinators/map.d.ts +0 -1
  41. package/dist/combinators/map.d.ts.map +1 -1
  42. package/dist/combinators/node.d.ts +13 -3
  43. package/dist/combinators/node.d.ts.map +1 -1
  44. package/dist/combinators/parser.d.ts +1 -1
  45. package/dist/combinators/parser.d.ts.map +1 -1
  46. package/dist/combinators/ref.d.ts.map +1 -1
  47. package/dist/combinators/regex.d.ts +1 -16
  48. package/dist/combinators/regex.d.ts.map +1 -1
  49. package/dist/combinators/repeat.d.ts +30 -2
  50. package/dist/combinators/repeat.d.ts.map +1 -1
  51. package/dist/combinators/scanTo.d.ts +71 -1
  52. package/dist/combinators/scanTo.d.ts.map +1 -1
  53. package/dist/combinators/sequence.d.ts.map +1 -1
  54. package/dist/combinators/token.d.ts.map +1 -1
  55. package/dist/combinators/trivia-boundary.d.ts +26 -0
  56. package/dist/combinators/trivia-boundary.d.ts.map +1 -0
  57. package/dist/combinators/trivia-skip.d.ts +45 -1
  58. package/dist/combinators/trivia-skip.d.ts.map +1 -1
  59. package/dist/combinators/withCtx.d.ts.map +1 -1
  60. package/dist/compiler/compile-linkable-table.d.ts +104 -0
  61. package/dist/compiler/compile-linkable-table.d.ts.map +1 -0
  62. package/dist/compiler/degradation.d.ts +0 -2
  63. package/dist/compiler/degradation.d.ts.map +1 -1
  64. package/dist/compiler/fields.d.ts +13 -0
  65. package/dist/compiler/fields.d.ts.map +1 -1
  66. package/dist/compiler/grammar-coverage-ids.d.ts +5 -5
  67. package/dist/compiler/grammar-coverage-ids.d.ts.map +1 -1
  68. package/dist/compiler/ir-serialize.d.ts.map +1 -1
  69. package/dist/compiler/linker.d.ts +47 -53
  70. package/dist/compiler/linker.d.ts.map +1 -1
  71. package/dist/compiler/token-alphabet.d.ts +81 -0
  72. package/dist/compiler/token-alphabet.d.ts.map +1 -0
  73. package/dist/compiler/token-dispatch.d.ts +95 -0
  74. package/dist/compiler/token-dispatch.d.ts.map +1 -0
  75. package/dist/compiler/token-scanner.d.ts +53 -0
  76. package/dist/compiler/token-scanner.d.ts.map +1 -0
  77. package/dist/compiler/value-usage.d.ts.map +1 -1
  78. package/dist/coverage.d.ts +1 -1
  79. package/dist/coverage.d.ts.map +1 -1
  80. package/dist/cst/capture-buffer.d.ts +35 -0
  81. package/dist/cst/capture-buffer.d.ts.map +1 -1
  82. package/dist/cst/reflection-attach.d.ts +8 -0
  83. package/dist/cst/reflection-attach.d.ts.map +1 -0
  84. package/dist/cst/reflection-symbols.d.ts +10 -0
  85. package/dist/cst/reflection-symbols.d.ts.map +1 -0
  86. package/dist/cst/reflection.d.ts +2 -4
  87. package/dist/cst/reflection.d.ts.map +1 -1
  88. package/dist/cst/root-trivia-scope.d.ts +26 -0
  89. package/dist/cst/root-trivia-scope.d.ts.map +1 -0
  90. package/dist/cst/trivia-charscan.d.ts +18 -0
  91. package/dist/cst/trivia-charscan.d.ts.map +1 -0
  92. package/dist/cst/trivia-kinds.d.ts +19 -0
  93. package/dist/cst/trivia-kinds.d.ts.map +1 -1
  94. package/dist/functional/doc.d.ts.map +1 -1
  95. package/dist/functional/run-tabled.d.ts +12 -0
  96. package/dist/functional/run-tabled.d.ts.map +1 -0
  97. package/dist/functional/run.d.ts +25 -42
  98. package/dist/functional/run.d.ts.map +1 -1
  99. package/dist/grammar-metadata.d.ts +3 -0
  100. package/dist/grammar-metadata.d.ts.map +1 -0
  101. package/dist/index.cjs +9290 -7181
  102. package/dist/index.cjs.map +3 -4
  103. package/dist/index.d.ts +18 -9
  104. package/dist/index.d.ts.map +1 -1
  105. package/dist/index.js +9300 -7191
  106. package/dist/index.js.map +3 -4
  107. package/dist/language-service/index.cjs +313 -121
  108. package/dist/language-service/index.cjs.map +3 -4
  109. package/dist/language-service/index.js +313 -121
  110. package/dist/language-service/index.js.map +3 -4
  111. package/dist/oracle/index.cjs.map +0 -1
  112. package/dist/oracle/index.js.map +0 -1
  113. package/dist/parse-context.d.ts +49 -0
  114. package/dist/parse-context.d.ts.map +1 -0
  115. package/dist/plugin/evaluator.d.ts +40 -1
  116. package/dist/plugin/evaluator.d.ts.map +1 -1
  117. package/dist/plugin/index.cjs +11175 -9534
  118. package/dist/plugin/index.cjs.map +3 -4
  119. package/dist/plugin/index.d.ts.map +1 -1
  120. package/dist/plugin/index.js +11175 -9534
  121. package/dist/plugin/index.js.map +3 -4
  122. package/dist/regex/classes.d.ts +28 -0
  123. package/dist/regex/classes.d.ts.map +1 -1
  124. package/dist/regex/first-set.d.ts +14 -0
  125. package/dist/regex/first-set.d.ts.map +1 -1
  126. package/dist/run/index.cjs +136 -51
  127. package/dist/run/index.cjs.map +2 -3
  128. package/dist/run/index.js +136 -51
  129. package/dist/run/index.js.map +2 -3
  130. package/dist/spec/index.cjs +2934 -1611
  131. package/dist/spec/index.cjs.map +3 -4
  132. package/dist/spec/index.js +2934 -1611
  133. package/dist/spec/index.js.map +3 -4
  134. package/dist/spec/model.d.ts +8 -0
  135. package/dist/spec/model.d.ts.map +1 -1
  136. package/dist/table/assemble.d.ts +307 -0
  137. package/dist/table/assemble.d.ts.map +1 -0
  138. package/dist/table/cell.d.ts +42 -0
  139. package/dist/table/cell.d.ts.map +1 -0
  140. package/dist/table/child-slots.d.ts +30 -0
  141. package/dist/table/child-slots.d.ts.map +1 -0
  142. package/dist/table/compile-rule-map.d.ts +136 -0
  143. package/dist/table/compile-rule-map.d.ts.map +1 -0
  144. package/dist/table/compile.d.ts +56 -0
  145. package/dist/table/compile.d.ts.map +1 -0
  146. package/dist/table/duplication-hook.d.ts +32 -0
  147. package/dist/table/duplication-hook.d.ts.map +1 -0
  148. package/dist/table/emit-assembly.d.ts +139 -0
  149. package/dist/table/emit-assembly.d.ts.map +1 -0
  150. package/dist/table/emit.d.ts +113 -0
  151. package/dist/table/emit.d.ts.map +1 -0
  152. package/dist/table/encode-baseline.d.ts +16 -0
  153. package/dist/table/encode-baseline.d.ts.map +1 -0
  154. package/dist/table/encode.d.ts +71 -0
  155. package/dist/table/encode.d.ts.map +1 -0
  156. package/dist/table/exec-baseline.d.ts +9 -0
  157. package/dist/table/exec-baseline.d.ts.map +1 -0
  158. package/dist/table/exec.d.ts +72 -0
  159. package/dist/table/exec.d.ts.map +1 -0
  160. package/dist/table/fold.d.ts +25 -0
  161. package/dist/table/fold.d.ts.map +1 -0
  162. package/dist/table/index.cjs +11463 -0
  163. package/dist/table/index.cjs.map +6 -0
  164. package/dist/table/index.d.ts +100 -0
  165. package/dist/table/index.d.ts.map +1 -0
  166. package/dist/table/index.js +11440 -0
  167. package/dist/table/index.js.map +6 -0
  168. package/dist/table/inspect.d.ts +17 -0
  169. package/dist/table/inspect.d.ts.map +1 -0
  170. package/dist/table/ops.d.ts +453 -0
  171. package/dist/table/ops.d.ts.map +1 -0
  172. package/dist/table/program.d.ts +496 -0
  173. package/dist/table/program.d.ts.map +1 -0
  174. package/dist/table/run-support.d.ts +38 -0
  175. package/dist/table/run-support.d.ts.map +1 -0
  176. package/dist/{compiler/scannable-run.d.ts → table/scan-shapes.d.ts} +14 -13
  177. package/dist/table/scan-shapes.d.ts.map +1 -0
  178. package/dist/table/site-labels.d.ts +58 -0
  179. package/dist/table/site-labels.d.ts.map +1 -0
  180. package/dist/table/stamp.d.ts +70 -0
  181. package/dist/table/stamp.d.ts.map +1 -0
  182. package/dist/types.d.ts +80 -24
  183. package/dist/types.d.ts.map +1 -1
  184. package/dist/version.d.ts +1 -1
  185. package/package.json +26 -2
  186. package/src/analysis/choice-cost-gate.ts +359 -0
  187. package/src/analysis/choice-cost-render.ts +293 -0
  188. package/src/analysis/choice-cost.ts +1008 -0
  189. package/src/analysis/commitment.ts +557 -0
  190. package/src/analysis/corpus.ts +130 -0
  191. package/src/analysis/diagnose-render.ts +625 -0
  192. package/src/analysis/diagnose.ts +317 -0
  193. package/src/analysis/diagnostics.ts +33 -0
  194. package/src/analysis/duplication.ts +1955 -0
  195. package/src/analysis/fix-render.ts +217 -0
  196. package/src/analysis/fix.ts +669 -0
  197. package/src/analysis/format-number.ts +31 -0
  198. package/src/analysis/gating.ts +792 -0
  199. package/src/analysis/grammar.ts +72 -0
  200. package/src/analysis/rebuild.ts +287 -0
  201. package/src/analysis/terminal.ts +336 -0
  202. package/src/cli/index.ts +413 -0
  203. package/src/combinators/adjacency.ts +171 -0
  204. package/src/combinators/attempt.ts +41 -0
  205. package/src/combinators/case-fold.ts +90 -0
  206. package/src/combinators/choice.ts +466 -0
  207. package/src/combinators/completions.ts +64 -0
  208. package/src/combinators/dispatch.ts +358 -0
  209. package/src/combinators/expect.ts +159 -0
  210. package/src/combinators/first-set.ts +348 -0
  211. package/src/combinators/gate.ts +50 -0
  212. package/src/combinators/grammar.ts +272 -0
  213. package/src/combinators/keywords.ts +145 -0
  214. package/src/combinators/literal.ts +102 -0
  215. package/src/combinators/map.ts +106 -0
  216. package/src/combinators/node.ts +345 -0
  217. package/src/combinators/not.ts +55 -0
  218. package/src/combinators/parser.ts +266 -0
  219. package/src/combinators/peek.ts +63 -0
  220. package/src/combinators/probe.ts +25 -0
  221. package/src/combinators/ref.ts +109 -0
  222. package/src/combinators/regex.ts +170 -0
  223. package/src/combinators/repeat.ts +561 -0
  224. package/src/combinators/scanTo.ts +393 -0
  225. package/src/combinators/sequence.ts +237 -0
  226. package/src/combinators/token.ts +120 -0
  227. package/src/combinators/trivia-boundary.ts +48 -0
  228. package/src/combinators/trivia-skip.ts +852 -0
  229. package/src/combinators/withCtx.ts +45 -0
  230. package/src/compiler/build-arity.ts +330 -0
  231. package/src/compiler/compile-linkable-table.ts +209 -0
  232. package/src/compiler/degradation.ts +266 -0
  233. package/src/compiler/fields.ts +191 -0
  234. package/src/compiler/grammar-coverage-ids.ts +129 -0
  235. package/src/compiler/ir-serialize.ts +615 -0
  236. package/src/compiler/linker.ts +954 -0
  237. package/src/compiler/token-alphabet.ts +209 -0
  238. package/src/compiler/token-dispatch.ts +353 -0
  239. package/src/compiler/token-scanner.ts +318 -0
  240. package/src/compiler/value-usage.ts +147 -0
  241. package/src/coverage.ts +383 -0
  242. package/src/cst/capture-buffer.ts +297 -0
  243. package/src/cst/host-mode.ts +69 -0
  244. package/src/cst/reflection-attach.ts +11 -0
  245. package/src/cst/reflection-symbols.ts +9 -0
  246. package/src/cst/reflection.ts +149 -0
  247. package/src/cst/relative-spans.ts +207 -0
  248. package/src/cst/root-trivia-scope.ts +30 -0
  249. package/src/cst/trivia-charscan.ts +312 -0
  250. package/src/cst/trivia-entries.ts +285 -0
  251. package/src/cst/trivia-index.ts +131 -0
  252. package/src/cst/trivia-kinds.ts +358 -0
  253. package/src/cst/types.ts +76 -0
  254. package/src/cst/walk.ts +101 -0
  255. package/src/functional/doc.ts +959 -0
  256. package/src/functional/run-tabled.ts +153 -0
  257. package/src/functional/run.ts +558 -0
  258. package/src/grammar-metadata.ts +2 -0
  259. package/src/index.ts +189 -0
  260. package/src/language-service/index.ts +221 -0
  261. package/src/line-index.ts +131 -0
  262. package/src/oracle/digest.ts +620 -0
  263. package/src/oracle/index.ts +55 -0
  264. package/src/parse-context.ts +91 -0
  265. package/src/plugin/direct-builder-static.ts +102 -0
  266. package/src/plugin/evaluator.ts +1505 -0
  267. package/src/plugin/free-identifiers.ts +338 -0
  268. package/src/plugin/index.ts +2281 -0
  269. package/src/plugin/reducer-resolver.ts +606 -0
  270. package/src/recovery/scan.ts +155 -0
  271. package/src/regex/classes.ts +183 -0
  272. package/src/regex/first-set.ts +316 -0
  273. package/src/run/index.ts +31 -0
  274. package/src/spec/ebnf.ts +128 -0
  275. package/src/spec/index.ts +54 -0
  276. package/src/spec/model.ts +636 -0
  277. package/src/spec/railroad-lib.ts +559 -0
  278. package/src/spec/railroad.ts +256 -0
  279. package/src/table/assemble.ts +3006 -0
  280. package/src/table/cell.ts +42 -0
  281. package/src/table/child-slots.ts +113 -0
  282. package/src/table/compile-rule-map.ts +336 -0
  283. package/src/table/compile.ts +244 -0
  284. package/src/table/duplication-hook.ts +89 -0
  285. package/src/table/emit-assembly.ts +1777 -0
  286. package/src/table/emit.ts +429 -0
  287. package/src/table/encode-baseline.ts +340 -0
  288. package/src/table/encode.ts +1569 -0
  289. package/src/table/exec-baseline.ts +537 -0
  290. package/src/table/exec.ts +1680 -0
  291. package/src/table/fold.ts +38 -0
  292. package/src/table/index.ts +108 -0
  293. package/src/table/inspect.ts +52 -0
  294. package/src/table/ops.ts +467 -0
  295. package/src/table/program.ts +876 -0
  296. package/src/table/run-support.ts +66 -0
  297. package/src/table/scan-shapes.ts +1578 -0
  298. package/src/table/site-labels.ts +228 -0
  299. package/src/table/stamp.ts +229 -0
  300. package/src/types.ts +563 -0
  301. package/src/version.ts +16 -0
  302. package/dist/compiler/codegen.d.ts +0 -334
  303. package/dist/compiler/codegen.d.ts.map +0 -1
  304. package/dist/compiler/inline-build.d.ts +0 -11
  305. package/dist/compiler/inline-build.d.ts.map +0 -1
  306. package/dist/compiler/inline-callback.d.ts +0 -21
  307. package/dist/compiler/inline-callback.d.ts.map +0 -1
  308. package/dist/compiler/line-index.d.ts +0 -2
  309. package/dist/compiler/line-index.d.ts.map +0 -1
  310. package/dist/compiler/module-hoist.d.ts +0 -106
  311. package/dist/compiler/module-hoist.d.ts.map +0 -1
  312. package/dist/compiler/scannable-run.d.ts.map +0 -1
  313. package/dist/compiler/scannable-terminal.d.ts +0 -23
  314. package/dist/compiler/scannable-terminal.d.ts.map +0 -1
  315. package/dist/compiler/trivia-fast-path.d.ts +0 -38
  316. package/dist/compiler/trivia-fast-path.d.ts.map +0 -1
@@ -0,0 +1,559 @@
1
+ /**
2
+ * Vendored copy of tabatkins railroad-diagrams (v1.0.0), license CC0-1.0.
3
+ * https://github.com/tabatkins/railroad-diagrams
4
+ *
5
+ * Public domain (CC0) — embedded verbatim as strings so the generated
6
+ * railroad-diagram HTML page is fully self-contained (no CDN, no runtime dep).
7
+ * Do not edit by hand; regenerate from the npm package if bumping versions.
8
+ */
9
+ /* eslint-disable */
10
+ export const RAILROAD_JS: string = `/*
11
+ Railroad Diagrams
12
+ by Tab Atkins Jr. (and others)
13
+ http://xanthir.com
14
+ http://twitter.com/tabatkins
15
+ http://github.com/tabatkins/railroad-diagrams
16
+
17
+ This document and all associated files in the github project are licensed under CC0: http://creativecommons.org/publicdomain/zero/1.0/
18
+ This means you can reuse, remix, or otherwise appropriate this project for your own use WITHOUT RESTRICTION.
19
+ (The actual legal meaning can be found at the above link.)
20
+ Don't ask me for permission to use any part of this project, JUST USE IT.
21
+ I would appreciate attribution, but that is not required by the license.
22
+ */
23
+
24
+ /*
25
+ This file uses a module pattern to avoid leaking names into the global scope.
26
+ The only accidental leakage is the name "temp".
27
+ The exported names can be found at the bottom of this file;
28
+ simply change the names in the array of strings to change what they are called in your application.
29
+
30
+ As well, several configuration constants are passed into the module function at the bottom of this file.
31
+ At runtime, these constants can be found on the Diagram class.
32
+ */
33
+
34
+ (function(options) {
35
+ function subclassOf(baseClass, superClass) {
36
+ baseClass.prototype = Object.create(superClass.prototype);
37
+ baseClass.prototype.$super = superClass.prototype;
38
+ }
39
+
40
+ function unnull(/* children */) {
41
+ return [].slice.call(arguments).reduce(function(sofar, x) { return sofar !== undefined ? sofar : x; });
42
+ }
43
+
44
+ function determineGaps(outer, inner) {
45
+ var diff = outer - inner;
46
+ switch(Diagram.INTERNAL_ALIGNMENT) {
47
+ case 'left': return [0, diff]; break;
48
+ case 'right': return [diff, 0]; break;
49
+ case 'center':
50
+ default: return [diff/2, diff/2]; break;
51
+ }
52
+ }
53
+
54
+ function wrapString(value) {
55
+ return ((typeof value) == 'string') ? new Terminal(value) : value;
56
+ }
57
+
58
+
59
+ function SVG(name, attrs, text) {
60
+ attrs = attrs || {};
61
+ text = text || '';
62
+ var el = document.createElementNS("http://www.w3.org/2000/svg",name);
63
+ for(var attr in attrs) {
64
+ el.setAttribute(attr, attrs[attr]);
65
+ }
66
+ el.textContent = text;
67
+ return el;
68
+ }
69
+
70
+ function FakeSVG(tagName, attrs, text){
71
+ if(!(this instanceof FakeSVG)) return new FakeSVG(tagName, attrs, text);
72
+ if(text) this.children = text;
73
+ else this.children = [];
74
+ this.tagName = tagName;
75
+ this.attrs = unnull(attrs, {});
76
+ return this;
77
+ };
78
+ FakeSVG.prototype.format = function(x, y, width) {
79
+ // Virtual
80
+ };
81
+ FakeSVG.prototype.addTo = function(parent) {
82
+ if(parent instanceof FakeSVG) {
83
+ parent.children.push(this);
84
+ return this;
85
+ } else {
86
+ var svg = this.toSVG();
87
+ parent.appendChild(svg);
88
+ return svg;
89
+ }
90
+ };
91
+ FakeSVG.prototype.escapeString = function(string) {
92
+ // Escape markdown and HTML special characters
93
+ return string.replace(/[*_\\\`\\[\\]<&]/g, function(charString) {
94
+ return '&#' + charString.charCodeAt(0) + ';';
95
+ });
96
+ };
97
+ FakeSVG.prototype.toSVG = function() {
98
+ var el = SVG(this.tagName, this.attrs);
99
+ if(typeof this.children == 'string') {
100
+ el.innerHTML = FakeSVG.prototype.escapeString(this.children);
101
+ } else {
102
+ this.children.forEach(function(e) {
103
+ el.appendChild(e.toSVG());
104
+ });
105
+ }
106
+ return el;
107
+ };
108
+ FakeSVG.prototype.toString = function() {
109
+ var str = '<' + this.tagName;
110
+ var group = this.tagName == "g" || this.tagName == "svg";
111
+ for(var attr in this.attrs) {
112
+ str += ' ' + attr + '="' + (this.attrs[attr]+'').replace(/&/g, '&amp;').replace(/"/g, '&quot;') + '"';
113
+ }
114
+ str += '>';
115
+ if(group) str += "\\n";
116
+ if(typeof this.children == 'string') {
117
+ str += FakeSVG.prototype.escapeString(this.children);
118
+ } else {
119
+ this.children.forEach(function(e) {
120
+ str += e;
121
+ });
122
+ }
123
+ str += '</' + this.tagName + '>\\n';
124
+ return str;
125
+ }
126
+
127
+ function Path(x,y) {
128
+ if(!(this instanceof Path)) return new Path(x,y);
129
+ FakeSVG.call(this, 'path');
130
+ this.attrs.d = "M"+x+' '+y;
131
+ }
132
+ subclassOf(Path, FakeSVG);
133
+ Path.prototype.m = function(x,y) {
134
+ this.attrs.d += 'm'+x+' '+y;
135
+ return this;
136
+ }
137
+ Path.prototype.h = function(val) {
138
+ this.attrs.d += 'h'+val;
139
+ return this;
140
+ }
141
+ Path.prototype.right = Path.prototype.h;
142
+ Path.prototype.left = function(val) { return this.h(-val); }
143
+ Path.prototype.v = function(val) {
144
+ this.attrs.d += 'v'+val;
145
+ return this;
146
+ }
147
+ Path.prototype.down = Path.prototype.v;
148
+ Path.prototype.up = function(val) { return this.v(-val); }
149
+ Path.prototype.arc = function(sweep){
150
+ var x = Diagram.ARC_RADIUS;
151
+ var y = Diagram.ARC_RADIUS;
152
+ if(sweep[0] == 'e' || sweep[1] == 'w') {
153
+ x *= -1;
154
+ }
155
+ if(sweep[0] == 's' || sweep[1] == 'n') {
156
+ y *= -1;
157
+ }
158
+ if(sweep == 'ne' || sweep == 'es' || sweep == 'sw' || sweep == 'wn') {
159
+ var cw = 1;
160
+ } else {
161
+ var cw = 0;
162
+ }
163
+ this.attrs.d += "a"+Diagram.ARC_RADIUS+" "+Diagram.ARC_RADIUS+" 0 0 "+cw+' '+x+' '+y;
164
+ return this;
165
+ }
166
+ Path.prototype.format = function() {
167
+ // All paths in this library start/end horizontally.
168
+ // The extra .5 ensures a minor overlap, so there's no seams in bad rasterizers.
169
+ this.attrs.d += 'h.5';
170
+ return this;
171
+ }
172
+
173
+ function Diagram(items) {
174
+ if(!(this instanceof Diagram)) return new Diagram([].slice.call(arguments));
175
+ FakeSVG.call(this, 'svg', {class: Diagram.DIAGRAM_CLASS});
176
+ this.items = items.map(wrapString);
177
+ this.items.unshift(new Start);
178
+ this.items.push(new End);
179
+ this.width = this.items.reduce(function(sofar, el) { return sofar + el.width + (el.needsSpace?20:0)}, 0)+1;
180
+ this.up = Math.max.apply(null, this.items.map(function (x) { return x.up; }));
181
+ this.down = Math.max.apply(null, this.items.map(function (x) { return x.down; }));
182
+ this.formatted = false;
183
+ }
184
+ subclassOf(Diagram, FakeSVG);
185
+ for(var option in options) {
186
+ Diagram[option] = options[option];
187
+ }
188
+ Diagram.prototype.format = function(paddingt, paddingr, paddingb, paddingl) {
189
+ paddingt = unnull(paddingt, 20);
190
+ paddingr = unnull(paddingr, paddingt, 20);
191
+ paddingb = unnull(paddingb, paddingt, 20);
192
+ paddingl = unnull(paddingl, paddingr, 20);
193
+ var x = paddingl;
194
+ var y = paddingt;
195
+ y += this.up;
196
+ var g = FakeSVG('g', Diagram.STROKE_ODD_PIXEL_LENGTH ? {transform:'translate(.5 .5)'} : {});
197
+ for(var i = 0; i < this.items.length; i++) {
198
+ var item = this.items[i];
199
+ if(item.needsSpace) {
200
+ Path(x,y).h(10).addTo(g);
201
+ x += 10;
202
+ }
203
+ item.format(x, y, item.width).addTo(g);
204
+ x += item.width;
205
+ if(item.needsSpace) {
206
+ Path(x,y).h(10).addTo(g);
207
+ x += 10;
208
+ }
209
+ }
210
+ this.attrs.width = this.width + paddingl + paddingr;
211
+ this.attrs.height = this.up + this.down + paddingt + paddingb;
212
+ this.attrs.viewBox = "0 0 " + this.attrs.width + " " + this.attrs.height;
213
+ g.addTo(this);
214
+ this.formatted = true;
215
+ return this;
216
+ }
217
+ Diagram.prototype.addTo = function(parent) {
218
+ var scriptTag = document.getElementsByTagName('script');
219
+ scriptTag = scriptTag[scriptTag.length - 1];
220
+ var parentTag = scriptTag.parentNode;
221
+ parent = parent || parentTag;
222
+ return this.$super.addTo.call(this, parent);
223
+ }
224
+ Diagram.prototype.toSVG = function() {
225
+ if (!this.formatted) {
226
+ this.format();
227
+ }
228
+ return this.$super.toSVG.call(this);
229
+ }
230
+ Diagram.prototype.toString = function() {
231
+ if (!this.formatted) {
232
+ this.format();
233
+ }
234
+ return this.$super.toString.call(this);
235
+ }
236
+
237
+ function ComplexDiagram() {
238
+ var diagram = new Diagram([].slice.call(arguments));
239
+ var items = diagram.items;
240
+ items.shift();
241
+ items.pop();
242
+ items.unshift(new Start(false));
243
+ items.push(new End(false));
244
+ diagram.items = items;
245
+ return diagram;
246
+ }
247
+
248
+ function Sequence(items) {
249
+ if(!(this instanceof Sequence)) return new Sequence([].slice.call(arguments));
250
+ FakeSVG.call(this, 'g');
251
+ this.items = items.map(wrapString);
252
+ this.width = this.items.reduce(function(sofar, el) { return sofar + el.width + (el.needsSpace?20:0)}, 0);
253
+ this.up = this.items.reduce(function(sofar,el) { return Math.max(sofar, el.up)}, 0);
254
+ this.down = this.items.reduce(function(sofar,el) { return Math.max(sofar, el.down)}, 0);
255
+ }
256
+ subclassOf(Sequence, FakeSVG);
257
+ Sequence.prototype.format = function(x,y,width) {
258
+ // Hook up the two sides if this is narrower than its stated width.
259
+ var gaps = determineGaps(width, this.width);
260
+ Path(x,y).h(gaps[0]).addTo(this);
261
+ Path(x+gaps[0]+this.width,y).h(gaps[1]).addTo(this);
262
+ x += gaps[0];
263
+
264
+ for(var i = 0; i < this.items.length; i++) {
265
+ var item = this.items[i];
266
+ if(item.needsSpace) {
267
+ Path(x,y).h(10).addTo(this);
268
+ x += 10;
269
+ }
270
+ item.format(x, y, item.width).addTo(this);
271
+ x += item.width;
272
+ if(item.needsSpace) {
273
+ Path(x,y).h(10).addTo(this);
274
+ x += 10;
275
+ }
276
+ }
277
+ return this;
278
+ }
279
+
280
+ function Choice(normal, items) {
281
+ if(!(this instanceof Choice)) return new Choice(normal, [].slice.call(arguments,1));
282
+ FakeSVG.call(this, 'g');
283
+ if( typeof normal !== "number" || normal !== Math.floor(normal) ) {
284
+ throw new TypeError("The first argument of Choice() must be an integer.");
285
+ } else if(normal < 0 || normal >= items.length) {
286
+ throw new RangeError("The first argument of Choice() must be an index for one of the items.");
287
+ } else {
288
+ this.normal = normal;
289
+ }
290
+ this.items = items.map(wrapString);
291
+ this.width = this.items.reduce(function(sofar, el){return Math.max(sofar, el.width)},0) + Diagram.ARC_RADIUS*4;
292
+ this.up = this.down = 0;
293
+ for(var i = 0; i < this.items.length; i++) {
294
+ var item = this.items[i];
295
+ if(i < normal) { this.up += Math.max(Diagram.ARC_RADIUS,item.up + item.down + Diagram.VERTICAL_SEPARATION); }
296
+ if(i == normal) { this.up += Math.max(Diagram.ARC_RADIUS, item.up); this.down += Math.max(Diagram.ARC_RADIUS, item.down); }
297
+ if(i > normal) { this.down += Math.max(Diagram.ARC_RADIUS,Diagram.VERTICAL_SEPARATION + item.up + item.down); }
298
+ }
299
+ }
300
+ subclassOf(Choice, FakeSVG);
301
+ Choice.prototype.format = function(x,y,width) {
302
+ // Hook up the two sides if this is narrower than its stated width.
303
+ var gaps = determineGaps(width, this.width);
304
+ Path(x,y).h(gaps[0]).addTo(this);
305
+ Path(x+gaps[0]+this.width,y).h(gaps[1]).addTo(this);
306
+ x += gaps[0];
307
+
308
+ var last = this.items.length -1;
309
+ var innerWidth = this.width - Diagram.ARC_RADIUS*4;
310
+
311
+ // Do the elements that curve above
312
+ for(var i = this.normal - 1; i >= 0; i--) {
313
+ var item = this.items[i];
314
+ if( i == this.normal - 1 ) {
315
+ var distanceFromY = Math.max(Diagram.ARC_RADIUS*2, this.items[i+1].up + Diagram.VERTICAL_SEPARATION + item.down);
316
+ }
317
+ Path(x,y).arc('se').up(distanceFromY - Diagram.ARC_RADIUS*2).arc('wn').addTo(this);
318
+ item.format(x+Diagram.ARC_RADIUS*2,y - distanceFromY,innerWidth).addTo(this);
319
+ Path(x+Diagram.ARC_RADIUS*2+innerWidth, y-distanceFromY).arc('ne').down(distanceFromY - Diagram.ARC_RADIUS*2).arc('ws').addTo(this);
320
+ distanceFromY += Math.max(Diagram.ARC_RADIUS, item.up + Diagram.VERTICAL_SEPARATION + (i == 0 ? 0 : this.items[i-1].down));
321
+ }
322
+
323
+ // Do the straight-line path.
324
+ Path(x,y).right(Diagram.ARC_RADIUS*2).addTo(this);
325
+ this.items[this.normal].format(x+Diagram.ARC_RADIUS*2, y, innerWidth).addTo(this);
326
+ Path(x+Diagram.ARC_RADIUS*2+innerWidth, y).right(Diagram.ARC_RADIUS*2).addTo(this);
327
+
328
+ // Do the elements that curve below
329
+ for(var i = this.normal+1; i <= last; i++) {
330
+ var item = this.items[i];
331
+ if( i == this.normal + 1 ) {
332
+ var distanceFromY = Math.max(Diagram.ARC_RADIUS*2, this.items[i-1].down + Diagram.VERTICAL_SEPARATION + item.up);
333
+ }
334
+ Path(x,y).arc('ne').down(distanceFromY - Diagram.ARC_RADIUS*2).arc('ws').addTo(this);
335
+ item.format(x+Diagram.ARC_RADIUS*2, y+distanceFromY, innerWidth).addTo(this);
336
+ Path(x+Diagram.ARC_RADIUS*2+innerWidth, y+distanceFromY).arc('se').up(distanceFromY - Diagram.ARC_RADIUS*2).arc('wn').addTo(this);
337
+ distanceFromY += Math.max(Diagram.ARC_RADIUS, item.down + Diagram.VERTICAL_SEPARATION + (i == last ? 0 : this.items[i+1].up));
338
+ }
339
+
340
+ return this;
341
+ }
342
+
343
+ function Optional(item, skip) {
344
+ if( skip === undefined )
345
+ return Choice(1, Skip(), item);
346
+ else if ( skip === "skip" )
347
+ return Choice(0, Skip(), item);
348
+ else
349
+ throw "Unknown value for Optional()'s 'skip' argument.";
350
+ }
351
+
352
+ function OneOrMore(item, rep) {
353
+ if(!(this instanceof OneOrMore)) return new OneOrMore(item, rep);
354
+ FakeSVG.call(this, 'g');
355
+ rep = rep || (new Skip);
356
+ this.item = wrapString(item);
357
+ this.rep = wrapString(rep);
358
+ this.width = Math.max(this.item.width, this.rep.width) + Diagram.ARC_RADIUS*2;
359
+ this.up = this.item.up;
360
+ this.down = Math.max(Diagram.ARC_RADIUS*2, this.item.down + Diagram.VERTICAL_SEPARATION + this.rep.up + this.rep.down);
361
+ }
362
+ subclassOf(OneOrMore, FakeSVG);
363
+ OneOrMore.prototype.needsSpace = true;
364
+ OneOrMore.prototype.format = function(x,y,width) {
365
+ // Hook up the two sides if this is narrower than its stated width.
366
+ var gaps = determineGaps(width, this.width);
367
+ Path(x,y).h(gaps[0]).addTo(this);
368
+ Path(x+gaps[0]+this.width,y).h(gaps[1]).addTo(this);
369
+ x += gaps[0];
370
+
371
+ // Draw item
372
+ Path(x,y).right(Diagram.ARC_RADIUS).addTo(this);
373
+ this.item.format(x+Diagram.ARC_RADIUS,y,this.width-Diagram.ARC_RADIUS*2).addTo(this);
374
+ Path(x+this.width-Diagram.ARC_RADIUS,y).right(Diagram.ARC_RADIUS).addTo(this);
375
+
376
+ // Draw repeat arc
377
+ var distanceFromY = Math.max(Diagram.ARC_RADIUS*2, this.item.down+Diagram.VERTICAL_SEPARATION+this.rep.up);
378
+ Path(x+Diagram.ARC_RADIUS,y).arc('nw').down(distanceFromY-Diagram.ARC_RADIUS*2).arc('ws').addTo(this);
379
+ this.rep.format(x+Diagram.ARC_RADIUS, y+distanceFromY, this.width - Diagram.ARC_RADIUS*2).addTo(this);
380
+ Path(x+this.width-Diagram.ARC_RADIUS, y+distanceFromY).arc('se').up(distanceFromY-Diagram.ARC_RADIUS*2).arc('en').addTo(this);
381
+
382
+ return this;
383
+ }
384
+
385
+ function ZeroOrMore(item, rep, skip) {
386
+ return Optional(OneOrMore(item, rep), skip);
387
+ }
388
+
389
+ function Start(simpleType) {
390
+ if(!(this instanceof Start)) return new Start();
391
+ FakeSVG.call(this, 'path');
392
+ this.width = 20;
393
+ this.up = 10;
394
+ this.down = 10;
395
+ this.simpleType = simpleType;
396
+ }
397
+ subclassOf(Start, FakeSVG);
398
+ Start.prototype.format = function(x,y) {
399
+ if (this.simpleType === false) {
400
+ this.attrs.d = 'M '+x+' '+(y-10)+' v 20 m 0 -10 h 20.5';
401
+ } else {
402
+ this.attrs.d = 'M '+x+' '+(y-10)+' v 20 m 10 -20 v 20 m -10 -10 h 20.5';
403
+ }
404
+ return this;
405
+ }
406
+
407
+ function End(simpleType) {
408
+ if(!(this instanceof End)) return new End();
409
+ FakeSVG.call(this, 'path');
410
+ this.width = 20;
411
+ this.up = 10;
412
+ this.down = 10;
413
+ this.simpleType = simpleType;
414
+ }
415
+ subclassOf(End, FakeSVG);
416
+ End.prototype.format = function(x,y) {
417
+ if (this.simpleType === false) {
418
+ this.attrs.d = 'M '+x+' '+y+' h 20 m 0 -10 v 20';
419
+ } else {
420
+ this.attrs.d = 'M '+x+' '+y+' h 20 m -10 -10 v 20 m 10 -20 v 20';
421
+ }
422
+ return this;
423
+ }
424
+
425
+ function Terminal(text) {
426
+ if(!(this instanceof Terminal)) return new Terminal(text);
427
+ FakeSVG.call(this, 'g');
428
+ this.text = text;
429
+ this.width = text.length * 8 + 20; /* Assume that each char is .5em, and that the em is 16px */
430
+ this.up = 11;
431
+ this.down = 11;
432
+ }
433
+ subclassOf(Terminal, FakeSVG);
434
+ Terminal.prototype.needsSpace = true;
435
+ Terminal.prototype.format = function(x, y, width) {
436
+ // Hook up the two sides if this is narrower than its stated width.
437
+ var gaps = determineGaps(width, this.width);
438
+ Path(x,y).h(gaps[0]).addTo(this);
439
+ Path(x+gaps[0]+this.width,y).h(gaps[1]).addTo(this);
440
+ x += gaps[0];
441
+
442
+ FakeSVG('rect', {x:x, y:y-11, width:this.width, height:this.up+this.down, rx:10, ry:10}).addTo(this);
443
+ FakeSVG('text', {x:x+this.width/2, y:y+4}, this.text).addTo(this);
444
+ return this;
445
+ }
446
+
447
+ function NonTerminal(text) {
448
+ if(!(this instanceof NonTerminal)) return new NonTerminal(text);
449
+ FakeSVG.call(this, 'g');
450
+ this.text = text;
451
+ this.width = text.length * 8 + 20;
452
+ this.up = 11;
453
+ this.down = 11;
454
+ }
455
+ subclassOf(NonTerminal, FakeSVG);
456
+ NonTerminal.prototype.needsSpace = true;
457
+ NonTerminal.prototype.format = function(x, y, width) {
458
+ // Hook up the two sides if this is narrower than its stated width.
459
+ var gaps = determineGaps(width, this.width);
460
+ Path(x,y).h(gaps[0]).addTo(this);
461
+ Path(x+gaps[0]+this.width,y).h(gaps[1]).addTo(this);
462
+ x += gaps[0];
463
+
464
+ FakeSVG('rect', {x:x, y:y-11, width:this.width, height:this.up+this.down}).addTo(this);
465
+ FakeSVG('text', {x:x+this.width/2, y:y+4}, this.text).addTo(this);
466
+ return this;
467
+ }
468
+
469
+ function Comment(text) {
470
+ if(!(this instanceof Comment)) return new Comment(text);
471
+ FakeSVG.call(this, 'g');
472
+ this.text = text;
473
+ this.width = text.length * 7 + 10;
474
+ this.up = 11;
475
+ this.down = 11;
476
+ }
477
+ subclassOf(Comment, FakeSVG);
478
+ Comment.prototype.needsSpace = true;
479
+ Comment.prototype.format = function(x, y, width) {
480
+ // Hook up the two sides if this is narrower than its stated width.
481
+ var gaps = determineGaps(width, this.width);
482
+ Path(x,y).h(gaps[0]).addTo(this);
483
+ Path(x+gaps[0]+this.width,y).h(gaps[1]).addTo(this);
484
+ x += gaps[0];
485
+
486
+ FakeSVG('text', {x:x+this.width/2, y:y+5, class:'comment'}, this.text).addTo(this);
487
+ return this;
488
+ }
489
+
490
+ function Skip() {
491
+ if(!(this instanceof Skip)) return new Skip();
492
+ FakeSVG.call(this, 'g');
493
+ this.width = 0;
494
+ this.up = 0;
495
+ this.down = 0;
496
+ }
497
+ subclassOf(Skip, FakeSVG);
498
+ Skip.prototype.format = function(x, y, width) {
499
+ Path(x,y).right(width).addTo(this);
500
+ return this;
501
+ }
502
+
503
+ var root;
504
+ if (typeof define === 'function' && define.amd) {
505
+ // AMD. Register as an anonymous module.
506
+ root = {};
507
+ define([], function() {
508
+ return root;
509
+ });
510
+ } else if (typeof exports === 'object') {
511
+ // CommonJS for node
512
+ root = exports;
513
+ } else {
514
+ // Browser globals (root is window)
515
+ root = this;
516
+ }
517
+
518
+ var temp = [Diagram, ComplexDiagram, Sequence, Choice, Optional, OneOrMore, ZeroOrMore, Terminal, NonTerminal, Comment, Skip];
519
+ /*
520
+ These are the names that the internal classes are exported as.
521
+ If you would like different names, adjust them here.
522
+ */
523
+ ['Diagram', 'ComplexDiagram', 'Sequence', 'Choice', 'Optional', 'OneOrMore', 'ZeroOrMore', 'Terminal', 'NonTerminal', 'Comment', 'Skip']
524
+ .forEach(function(e,i) { root[e] = temp[i]; });
525
+ }).call(this,
526
+ {
527
+ VERTICAL_SEPARATION: 8,
528
+ ARC_RADIUS: 10,
529
+ DIAGRAM_CLASS: 'railroad-diagram',
530
+ STROKE_ODD_PIXEL_LENGTH: true,
531
+ INTERNAL_ALIGNMENT: 'center',
532
+ }
533
+ );
534
+ `
535
+
536
+ export const RAILROAD_CSS: string = `svg.railroad-diagram {
537
+ background-color: hsl(30,20%,95%);
538
+ }
539
+ svg.railroad-diagram path {
540
+ stroke-width: 3;
541
+ stroke: black;
542
+ fill: rgba(0,0,0,0);
543
+ }
544
+ svg.railroad-diagram text {
545
+ font: bold 14px monospace;
546
+ text-anchor: middle;
547
+ }
548
+ svg.railroad-diagram text.label {
549
+ text-anchor: start;
550
+ }
551
+ svg.railroad-diagram text.comment {
552
+ font: italic 12px monospace;
553
+ }
554
+ svg.railroad-diagram rect {
555
+ stroke-width: 3;
556
+ stroke: black;
557
+ fill: hsl(120,100%,90%);
558
+ }
559
+ `