@fengwk/kchart 0.0.0-stage → 0.1.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 (201) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +21 -0
  3. package/NOTICE.md +11 -0
  4. package/README.md +68 -2
  5. package/README.zh-CN.md +69 -0
  6. package/dist/LICENSE +21 -0
  7. package/dist/NOTICE.md +11 -0
  8. package/dist/assets/ICONS-LICENSE.txt +53 -0
  9. package/dist/assets/maps/LICENSE.md +75 -0
  10. package/dist/assets/maps/METADATA.md +9 -0
  11. package/dist/assets/maps/REGIONS.md +28 -0
  12. package/dist/assets/maps/us-states.geojson +1 -0
  13. package/dist/assets/maps/world.geojson +1 -0
  14. package/dist/esm/_virtual/_kchart-families.js +48 -0
  15. package/dist/esm/_virtual/_rolldown/runtime.js +12 -0
  16. package/dist/esm/assets/icons.js +11 -0
  17. package/dist/esm/assets/maps/us-states.js +3 -0
  18. package/dist/esm/assets/maps/world.js +3 -0
  19. package/dist/esm/assets/themes/claude.js +69 -0
  20. package/dist/esm/assets/themes/cloudmusic.js +62 -0
  21. package/dist/esm/assets/themes/kanagawa.js +67 -0
  22. package/dist/esm/assets/themes/obsidian.js +67 -0
  23. package/dist/esm/assets/themes/paper.js +61 -0
  24. package/dist/esm/assets/themes/terminal.js +60 -0
  25. package/dist/esm/assets/themes/tokyo.js +62 -0
  26. package/dist/esm/chart/basic.js +1313 -0
  27. package/dist/esm/chart/catalog.js +476 -0
  28. package/dist/esm/chart/composition.js +844 -0
  29. package/dist/esm/chart/context.js +189 -0
  30. package/dist/esm/chart/distribution.js +442 -0
  31. package/dist/esm/chart/fields.js +67 -0
  32. package/dist/esm/chart/maps.js +563 -0
  33. package/dist/esm/chart/mindmap.js +629 -0
  34. package/dist/esm/chart/multivariate.js +1229 -0
  35. package/dist/esm/chart/registry.js +108 -0
  36. package/dist/esm/chart/relations.js +949 -0
  37. package/dist/esm/chart/spec.js +1415 -0
  38. package/dist/esm/chart/stat-compare.js +302 -0
  39. package/dist/esm/chart/stat-trend.js +181 -0
  40. package/dist/esm/chart/statistics.js +1007 -0
  41. package/dist/esm/chart/unit-variants.js +645 -0
  42. package/dist/esm/chart/variants.js +552 -0
  43. package/dist/esm/core/color.js +197 -0
  44. package/dist/esm/core/context.js +256 -0
  45. package/dist/esm/core/diagnostics.js +23 -0
  46. package/dist/esm/core/format.js +167 -0
  47. package/dist/esm/core/geometry.js +130 -0
  48. package/dist/esm/core/icon-data.js +5 -0
  49. package/dist/esm/core/icons.js +173 -0
  50. package/dist/esm/core/scale.js +112 -0
  51. package/dist/esm/core/scene.js +234 -0
  52. package/dist/esm/core/svg.js +89 -0
  53. package/dist/esm/core/text.js +184 -0
  54. package/dist/esm/core/theme.js +318 -0
  55. package/dist/esm/diagram/agentflow.js +596 -0
  56. package/dist/esm/diagram/architecture.js +729 -0
  57. package/dist/esm/diagram/block.js +988 -0
  58. package/dist/esm/diagram/blocks.js +687 -0
  59. package/dist/esm/diagram/c4.js +1002 -0
  60. package/dist/esm/diagram/class.js +1158 -0
  61. package/dist/esm/diagram/cynefin.js +749 -0
  62. package/dist/esm/diagram/emphasis.js +7 -0
  63. package/dist/esm/diagram/er.js +657 -0
  64. package/dist/esm/diagram/eventmodeling.js +948 -0
  65. package/dist/esm/diagram/gantt-date.js +323 -0
  66. package/dist/esm/diagram/gantt.js +661 -0
  67. package/dist/esm/diagram/gitgraph.js +472 -0
  68. package/dist/esm/diagram/graph-layout.js +4046 -0
  69. package/dist/esm/diagram/graph-parse.js +1366 -0
  70. package/dist/esm/diagram/graph-route.js +490 -0
  71. package/dist/esm/diagram/graph.js +553 -0
  72. package/dist/esm/diagram/ishikawa.js +496 -0
  73. package/dist/esm/diagram/journey.js +512 -0
  74. package/dist/esm/diagram/kanban.js +280 -0
  75. package/dist/esm/diagram/label.js +56 -0
  76. package/dist/esm/diagram/layers.js +385 -0
  77. package/dist/esm/diagram/map-label.js +44 -0
  78. package/dist/esm/diagram/mindmap.js +277 -0
  79. package/dist/esm/diagram/numeric-composition.js +78 -0
  80. package/dist/esm/diagram/numeric-model.js +86 -0
  81. package/dist/esm/diagram/numeric-parse.js +205 -0
  82. package/dist/esm/diagram/numeric-radar.js +355 -0
  83. package/dist/esm/diagram/numeric-sankey.js +201 -0
  84. package/dist/esm/diagram/numeric-xy.js +422 -0
  85. package/dist/esm/diagram/packet.js +175 -0
  86. package/dist/esm/diagram/presentation.js +252 -0
  87. package/dist/esm/diagram/quadrant.js +297 -0
  88. package/dist/esm/diagram/railroad-parse.js +465 -0
  89. package/dist/esm/diagram/railroad.js +294 -0
  90. package/dist/esm/diagram/registry.js +296 -0
  91. package/dist/esm/diagram/relations.js +966 -0
  92. package/dist/esm/diagram/requirement.js +341 -0
  93. package/dist/esm/diagram/sequence.js +1228 -0
  94. package/dist/esm/diagram/shapes.js +366 -0
  95. package/dist/esm/diagram/source.js +538 -0
  96. package/dist/esm/diagram/support.js +153 -0
  97. package/dist/esm/diagram/swimlane-mermaid.js +140 -0
  98. package/dist/esm/diagram/swimlane.js +1186 -0
  99. package/dist/esm/diagram/time.js +748 -0
  100. package/dist/esm/diagram/timeline.js +376 -0
  101. package/dist/esm/diagram/tree-family-icon.js +22 -0
  102. package/dist/esm/diagram/treeview.js +293 -0
  103. package/dist/esm/diagram/usecase.js +852 -0
  104. package/dist/esm/diagram/venn.js +1038 -0
  105. package/dist/esm/diagram/wardley.js +838 -0
  106. package/dist/esm/index.d.ts +133 -0
  107. package/dist/esm/index.js +92 -0
  108. package/dist/esm/interaction.js +561 -0
  109. package/dist/esm/mermaid.d.ts +28 -0
  110. package/dist/esm/mermaid.js +70 -0
  111. package/dist/esm/node.d.ts +68 -0
  112. package/dist/esm/node.js +373 -0
  113. package/dist/esm/package.json +1 -0
  114. package/dist/esm/page-api.js +65 -0
  115. package/dist/esm/runtime.js +352 -0
  116. package/dist/esm/src/families.js +209 -0
  117. package/dist/page-api.js +1 -0
  118. package/dist/units/chart/basic.js +1 -0
  119. package/dist/units/chart/catalog.js +1 -0
  120. package/dist/units/chart/composition.js +1 -0
  121. package/dist/units/chart/context.js +1 -0
  122. package/dist/units/chart/distribution.js +1 -0
  123. package/dist/units/chart/fields.js +1 -0
  124. package/dist/units/chart/maps.js +1 -0
  125. package/dist/units/chart/mindmap.js +1 -0
  126. package/dist/units/chart/multivariate.js +1 -0
  127. package/dist/units/chart/registry.js +1 -0
  128. package/dist/units/chart/relations.js +1 -0
  129. package/dist/units/chart/spec.js +3 -0
  130. package/dist/units/chart/stat-compare.js +1 -0
  131. package/dist/units/chart/stat-trend.js +1 -0
  132. package/dist/units/chart/statistics.js +1 -0
  133. package/dist/units/chart/unit-variants.js +1 -0
  134. package/dist/units/chart/variants.js +1 -0
  135. package/dist/units/core/color.js +1 -0
  136. package/dist/units/core/context.js +1 -0
  137. package/dist/units/core/diagnostics.js +1 -0
  138. package/dist/units/core/format.js +1 -0
  139. package/dist/units/core/geometry.js +1 -0
  140. package/dist/units/core/icon-data.js +1 -0
  141. package/dist/units/core/icons.js +1 -0
  142. package/dist/units/core/scale.js +1 -0
  143. package/dist/units/core/scene.js +1 -0
  144. package/dist/units/core/svg.js +1 -0
  145. package/dist/units/core/text.js +2 -0
  146. package/dist/units/core/theme.js +1 -0
  147. package/dist/units/diagram/agentflow.js +12 -0
  148. package/dist/units/diagram/architecture.js +4 -0
  149. package/dist/units/diagram/block.js +2 -0
  150. package/dist/units/diagram/blocks.js +2 -0
  151. package/dist/units/diagram/c4.js +6 -0
  152. package/dist/units/diagram/class.js +5 -0
  153. package/dist/units/diagram/cynefin.js +4 -0
  154. package/dist/units/diagram/emphasis.js +1 -0
  155. package/dist/units/diagram/er.js +9 -0
  156. package/dist/units/diagram/eventmodeling.js +5 -0
  157. package/dist/units/diagram/gantt-date.js +1 -0
  158. package/dist/units/diagram/gantt.js +3 -0
  159. package/dist/units/diagram/gitgraph.js +2 -0
  160. package/dist/units/diagram/graph-layout.js +4 -0
  161. package/dist/units/diagram/graph-parse.js +9 -0
  162. package/dist/units/diagram/graph-route.js +1 -0
  163. package/dist/units/diagram/graph.js +1 -0
  164. package/dist/units/diagram/ishikawa.js +2 -0
  165. package/dist/units/diagram/journey.js +5 -0
  166. package/dist/units/diagram/kanban.js +2 -0
  167. package/dist/units/diagram/label.js +4 -0
  168. package/dist/units/diagram/layers.js +1 -0
  169. package/dist/units/diagram/map-label.js +1 -0
  170. package/dist/units/diagram/mindmap.js +2 -0
  171. package/dist/units/diagram/numeric-composition.js +1 -0
  172. package/dist/units/diagram/numeric-model.js +1 -0
  173. package/dist/units/diagram/numeric-parse.js +2 -0
  174. package/dist/units/diagram/numeric-radar.js +3 -0
  175. package/dist/units/diagram/numeric-sankey.js +2 -0
  176. package/dist/units/diagram/numeric-xy.js +2 -0
  177. package/dist/units/diagram/packet.js +2 -0
  178. package/dist/units/diagram/presentation.js +2 -0
  179. package/dist/units/diagram/quadrant.js +2 -0
  180. package/dist/units/diagram/railroad-parse.js +4 -0
  181. package/dist/units/diagram/railroad.js +1 -0
  182. package/dist/units/diagram/registry.js +2 -0
  183. package/dist/units/diagram/relations.js +3 -0
  184. package/dist/units/diagram/requirement.js +3 -0
  185. package/dist/units/diagram/sequence.js +2 -0
  186. package/dist/units/diagram/shapes.js +60 -0
  187. package/dist/units/diagram/source.js +20 -0
  188. package/dist/units/diagram/support.js +3 -0
  189. package/dist/units/diagram/swimlane-mermaid.js +3 -0
  190. package/dist/units/diagram/swimlane.js +2 -0
  191. package/dist/units/diagram/time.js +3 -0
  192. package/dist/units/diagram/timeline.js +2 -0
  193. package/dist/units/diagram/tree-family-icon.js +1 -0
  194. package/dist/units/diagram/treeview.js +2 -0
  195. package/dist/units/diagram/usecase.js +3 -0
  196. package/dist/units/diagram/venn.js +2 -0
  197. package/dist/units/diagram/wardley.js +4 -0
  198. package/dist/units/interaction.js +1 -0
  199. package/dist/units/runtime.js +1 -0
  200. package/dist/units.json +712 -0
  201. package/package.json +30 -4
@@ -0,0 +1,465 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { resolveIconRef } from "../core/icons.js";
3
+ import { normalizeLabel } from "./label.js";
4
+ import { presentation } from "./presentation.js";
5
+ import { error, warning } from "./support.js";
6
+ // src/diagram/railroad-parse.js
7
+ var railroad_parse_exports = /* @__PURE__ */ __exportAll({
8
+ childrenOf: () => childrenOf,
9
+ parseAbnf: () => parseAbnf,
10
+ parseEbnf: () => parseEbnf,
11
+ parseIr: () => parseIr,
12
+ parsePeg: () => parsePeg
13
+ });
14
+ var terminal = (value) => ({
15
+ type: "terminal",
16
+ value
17
+ });
18
+ var reference = (name, token) => ({
19
+ type: "nonterminal",
20
+ name,
21
+ line: token.line,
22
+ column: token.column
23
+ });
24
+ var special = (text) => ({
25
+ type: "special",
26
+ text
27
+ });
28
+ var optional = (element) => ({
29
+ type: "optional",
30
+ element
31
+ });
32
+ var repetition = (element, min, max = Infinity) => ({
33
+ type: "repetition",
34
+ element,
35
+ min,
36
+ max
37
+ });
38
+ var sequence = (elements) => elements.length === 1 ? elements[0] : {
39
+ type: "sequence",
40
+ elements
41
+ };
42
+ var choice = (alternatives) => alternatives.length === 1 ? alternatives[0] : {
43
+ type: "choice",
44
+ alternatives
45
+ };
46
+ var childrenOf = (node) => node.elements || node.alternatives || (node.element ? [node.element] : []);
47
+ var unescape = (text) => text.replace(/\\(.)/g, (_, ch) => ({
48
+ n: "\n",
49
+ r: "\r",
50
+ t: " "
51
+ })[ch] ?? ch);
52
+ var CAP = 2e3;
53
+ var DEPTH = 24;
54
+ var RailroadSyntax = class extends Error {
55
+ constructor(diagnostic) {
56
+ super(diagnostic.message);
57
+ this.diagnostic = diagnostic;
58
+ }
59
+ };
60
+ function reader(body, info, dialect, look, assets) {
61
+ const abnf = dialect === "abnf";
62
+ const patterns = [
63
+ ["hidden", /\s+|%%[^\n]*/y],
64
+ ...dialect === "ir" || dialect === "ebnf" ? [["hidden", /\/\*[\s\S]*?\*\//y]] : [],
65
+ ...dialect === "ebnf" ? [["hidden", /\(\*[\s\S]*?\*\)/y], ["special", /\?(?=[^?;]*[^?\s;][^?;]*\?)[^?;]*\?/y]] : [],
66
+ ...dialect === "peg" ? [["hidden", /#[^\n]*/y]] : [],
67
+ ["title", /title[\t ]+(?:"(?:[^"\\\n]|\\.)*"|'(?:[^'\\\n]|\\.)*'|[^\n]*?)(?=[\t ]*(?:%%|\n|$))/y],
68
+ ["accessibility", /accTitle[\t ]*:[^\n]*|accDescr(?:[\t ]*:[^\n]*|\s*\{[^}]*\})/y],
69
+ ["string", abnf ? /"[^"]*"/y : /"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'/y],
70
+ ...abnf ? [["numeric", /%[xXdDbB][0-9A-Fa-f]+(?:-[0-9A-Fa-f]+|\.[0-9A-Fa-f]+)*/y], ["repeat", /[0-9]*\*[0-9]*|[0-9]+/y]] : [],
71
+ ["id", abnf ? /[A-Za-z][A-Za-z0-9-]*/y : /[A-Za-z_][\w-]*/y],
72
+ ["icon", /::icon\(\s*[\w:-]+\s*\)/y],
73
+ ["classes", /:::[\w-]+(?:,[\w-]+)*/y],
74
+ ["punctuation", /::=|<-|[\s\S]/y]
75
+ ];
76
+ let offset = 0, line = info.firstLine ?? 2, column = 1, token;
77
+ function advance(raw) {
78
+ offset += raw.length;
79
+ const rows = raw.split("\n");
80
+ line += rows.length - 1;
81
+ column = rows.length === 1 ? column + raw.length : rows.at(-1).length + 1;
82
+ }
83
+ function peek() {
84
+ while (!token) {
85
+ if (offset === body.length) return {
86
+ kind: "end",
87
+ value: "",
88
+ line,
89
+ column
90
+ };
91
+ for (const [kind, pattern] of patterns) {
92
+ pattern.lastIndex = offset;
93
+ const raw = pattern.exec(body)?.[0];
94
+ if (!raw) continue;
95
+ const value = kind === "string" ? abnf ? raw.slice(1, -1) : unescape(raw.slice(1, -1)) : raw;
96
+ if (kind !== "hidden") token = {
97
+ kind: kind === "punctuation" ? raw : kind,
98
+ value,
99
+ line,
100
+ column
101
+ };
102
+ advance(raw);
103
+ break;
104
+ }
105
+ }
106
+ return token;
107
+ }
108
+ function fail(code, zh, en, fixZh, fixEn, at = peek()) {
109
+ throw new RailroadSyntax({
110
+ ...error(info.lang, at.line, zh, en, fixZh, fixEn, at.column),
111
+ code: `railroad.${code}`
112
+ });
113
+ }
114
+ function take(kind) {
115
+ const next = peek();
116
+ if (next.kind !== kind) return null;
117
+ token = void 0;
118
+ return next;
119
+ }
120
+ function expect(kind, code = "unexpected-token") {
121
+ const next = take(kind);
122
+ if (next) return next;
123
+ if (["\"", "'"].includes(peek().kind) && !(abnf && peek().kind === "'")) fail("unterminated-string", "字符串未闭合", "Unterminated string", "添加对应的结束引号", "Add the matching closing quote");
124
+ fail(code, `应为「${kind}」`, `Expected ${kind}`, `在此处使用「${kind}」`, `Use ${kind} here`);
125
+ }
126
+ function checkDepth(depth) {
127
+ if (depth > DEPTH) fail("depth-limit", "语法轨道嵌套过深", "Railroad nesting is too deep", "将嵌套部分提取为规则", "Extract nested expressions into rules");
128
+ }
129
+ function commentTail() {
130
+ if (!/^[\t ]*[A-Za-z][A-Za-z0-9-]*[\t ]*=/.test(body.slice(offset))) advance(/^[^\n]*/.exec(body.slice(offset))[0]);
131
+ }
132
+ function presentationLine() {
133
+ const next = peek();
134
+ if (next.kind !== "id" || !/^(class|classdef|style|linkstyle|click|href|link|callback)$/i.test(next.value)) return false;
135
+ const rest = /^[^\n]*/.exec(body.slice(offset))[0];
136
+ if (/^\s*(?:=|::=|<-|:::|::icon\()/.test(rest) || !look.accept(next.value + rest, next.line)) return false;
137
+ token = void 0;
138
+ advance(rest);
139
+ return true;
140
+ }
141
+ function asset(issue) {
142
+ if (!assets.has(issue.message)) assets.set(issue.message, issue);
143
+ }
144
+ function iconOf(ref, at) {
145
+ const icon = resolveIconRef(ref);
146
+ if (!icon) asset(warning("IGNORED_ASSET", info.lang, at.line, `未知图标 ${ref}`, `Unknown icon ${ref}`, "改用内置图标", "Use a supported icon", at.column));
147
+ return icon;
148
+ }
149
+ function label(node, raw, at) {
150
+ if (!/[<&#$`]|(?:fa[bsrlk]?|logos|mdi|tabler|simple-icons):|::icon\(/i.test(raw)) return node;
151
+ let explicit;
152
+ const source = raw.replace(/::icon\(\s*([\w:-]+)\s*\)|(?<![\w:-])((?:logos|mdi|tabler|simple-icons):[\w-]+)/gi, (_, call, token) => {
153
+ const icon = iconOf(call ?? token, at);
154
+ explicit ??= icon;
155
+ return "";
156
+ });
157
+ const normalized = normalizeLabel(source, {
158
+ lang: info.lang,
159
+ line: at.line
160
+ });
161
+ normalized.diagnostics.forEach(asset);
162
+ if (normalized.text !== raw.trim()) node.label = normalized.text;
163
+ const icon = normalized.icon ?? explicit;
164
+ if (icon) node.icon = icon;
165
+ return node;
166
+ }
167
+ function decorate(node, id, at) {
168
+ let suffix;
169
+ while (suffix = take("classes") || take("icon")) if (suffix.kind === "classes") look.addClasses(id, suffix.value.slice(3).split(","), suffix.line);
170
+ else node.icon = iconOf(suffix.value.slice(7, -1).trim(), at) ?? node.icon;
171
+ return node;
172
+ }
173
+ return {
174
+ peek,
175
+ take,
176
+ expect,
177
+ fail,
178
+ checkDepth,
179
+ commentTail,
180
+ presentationLine,
181
+ label,
182
+ decorate
183
+ };
184
+ }
185
+ function expression(r, dialect, hints) {
186
+ const ebnf = dialect === "ebnf", abnf = dialect === "abnf", peg = dialect === "peg";
187
+ const groups = ebnf ? [
188
+ "(",
189
+ "[",
190
+ "{"
191
+ ] : abnf ? ["(", "["] : ["("];
192
+ const factorKinds = [
193
+ "id",
194
+ "string",
195
+ ...groups,
196
+ ...ebnf ? ["special"] : abnf ? ["numeric", "repeat"] : [
197
+ "&",
198
+ "!",
199
+ "."
200
+ ]
201
+ ];
202
+ const choiceOp = ebnf ? "|" : "/";
203
+ function primary(depth) {
204
+ const token = r.peek();
205
+ if (r.take("string") || abnf && r.take("numeric")) return r.label(terminal(token.value), token.value, token);
206
+ if (r.take("id")) return r.decorate(reference(token.value, token), token.value, token);
207
+ if (ebnf && r.take("special")) {
208
+ const text = token.value.slice(1, -1).trim();
209
+ return r.label(special(text), text, token);
210
+ }
211
+ if (peg && r.take(".")) return special(".");
212
+ if (groups.includes(token.kind)) {
213
+ r.take(token.kind);
214
+ const node = alternatives(depth + 1);
215
+ r.expect({
216
+ "(": ")",
217
+ "[": "]",
218
+ "{": "}"
219
+ }[token.kind]);
220
+ return token.kind === "[" ? optional(node) : token.kind === "{" ? repetition(node, 0) : node;
221
+ }
222
+ mismatch();
223
+ r.expect("string");
224
+ }
225
+ function mismatch() {
226
+ const kind = r.peek().kind;
227
+ if (abnf && [
228
+ "|",
229
+ "+",
230
+ "?",
231
+ "'",
232
+ "{"
233
+ ].includes(kind)) r.fail("abnf-unsupported", "此写法不属于 ABNF", "This syntax is not ABNF", "使用 /、双引号和前置重复次数", "Use /, double quotes and a repeat prefix");
234
+ if (peg && [
235
+ "[",
236
+ "{",
237
+ "-",
238
+ "|"
239
+ ].includes(kind)) r.fail("peg-unsupported", "此写法不属于 PEG", "This syntax is not PEG", "使用 /、圆括号和后置 ?、*、+", "Use /, parentheses and postfix ?, *, +");
240
+ }
241
+ function factor(depth) {
242
+ const start = r.peek();
243
+ const count = abnf && r.take("repeat");
244
+ const predicate = peg && (r.take("&") || r.take("!"));
245
+ let node = primary(depth);
246
+ if (count) {
247
+ const [lo, hi] = count.value.split("*");
248
+ const min = Number(lo), max = hi === void 0 ? min : hi === "" ? Infinity : Number(hi);
249
+ if (!Number.isSafeInteger(min) || max !== Infinity && !Number.isSafeInteger(max) || min > max) r.fail("abnf-bad-repeat", "无效的 ABNF 重复次数", "Invalid ABNF repetition bounds", "使用递增的整数范围", "Use an ascending integer range", count);
250
+ node = min === 0 && max === 1 ? optional(node) : repetition(node, min, max);
251
+ }
252
+ if (ebnf || peg) {
253
+ let postfixes = 0;
254
+ do {
255
+ if (r.take("?")) node = optional(node);
256
+ else if (r.take("*")) node = repetition(node, 0);
257
+ else if (r.take("+")) node = repetition(node, 1);
258
+ else if (ebnf && r.take("-")) node = sequence([
259
+ node,
260
+ {
261
+ ...terminal("-"),
262
+ exception: true
263
+ },
264
+ primary(depth)
265
+ ]);
266
+ else break;
267
+ r.checkDepth(depth + ++postfixes);
268
+ } while (ebnf);
269
+ }
270
+ if (predicate) {
271
+ const label = node.type === "terminal" ? JSON.stringify(node.value) : node.name ?? node.text;
272
+ if (label === void 0) hints.push({
273
+ type: "predicate",
274
+ ...start
275
+ });
276
+ return special(predicate.value + (label ?? "(...)"));
277
+ }
278
+ return node;
279
+ }
280
+ function alternatives(depth) {
281
+ r.checkDepth(depth);
282
+ const lanes = [];
283
+ do {
284
+ const elements = [factor(depth)];
285
+ while (ebnf && r.take(",") || factorKinds.includes(r.peek().kind)) elements.push(factor(depth));
286
+ mismatch();
287
+ lanes.push(sequence(elements));
288
+ } while (r.take(choiceOp));
289
+ return choice(lanes);
290
+ }
291
+ return alternatives(1);
292
+ }
293
+ var constructors = new Map(Object.entries({
294
+ terminal,
295
+ nonterminal: reference,
296
+ special,
297
+ optional,
298
+ sequence,
299
+ choice,
300
+ zeroOrMore: (node) => repetition(node, 0),
301
+ oneOrMore: (node) => repetition(node, 1)
302
+ }));
303
+ function irExpression(r, depth = 1) {
304
+ r.checkDepth(depth);
305
+ const name = r.expect("id");
306
+ const build = constructors.get(name.value);
307
+ if (!build) r.fail("ir-unknown-constructor", `未知构造器「${name.value}」`, `Unknown constructor ${name.value}`, `使用 ${[...constructors.keys()].join("、")}`, `Use ${[...constructors.keys()].join(", ")}`, name);
308
+ r.expect("(");
309
+ if (r.peek().kind === ")") r.fail("ir-arity", "构造器缺少参数", "Constructor has no arguments", "添加构造器参数", "Add a constructor argument");
310
+ let node;
311
+ if ([
312
+ "terminal",
313
+ "nonterminal",
314
+ "special"
315
+ ].includes(name.value)) {
316
+ const token = r.expect("string");
317
+ node = r.label(build(token.value, token), token.value, token);
318
+ } else if (["sequence", "choice"].includes(name.value)) {
319
+ const args = [];
320
+ do
321
+ args.push(irExpression(r, depth + 1));
322
+ while (r.take(","));
323
+ node = build(args);
324
+ } else node = build(irExpression(r, depth + 1));
325
+ r.expect(")", "ir-arity");
326
+ return node.type === "nonterminal" ? r.decorate(node, node.name, name) : node;
327
+ }
328
+ var ABNF_CORE = /* @__PURE__ */ new Set([
329
+ "alpha",
330
+ "bit",
331
+ "char",
332
+ "cr",
333
+ "crlf",
334
+ "ctl",
335
+ "digit",
336
+ "dquote",
337
+ "hexdig",
338
+ "htab",
339
+ "lf",
340
+ "lwsp",
341
+ "octet",
342
+ "sp",
343
+ "vchar",
344
+ "wsp"
345
+ ]);
346
+ function parseRules(body, info, dialect) {
347
+ if (info.params) {
348
+ body = " ".repeat(info.header.lastIndexOf(info.params)) + info.params + "\n" + body;
349
+ info = {
350
+ ...info,
351
+ firstLine: info.headerLine
352
+ };
353
+ }
354
+ const rules = [], errors = [], hints = [];
355
+ let title, accTitle, accDescr, count = 0;
356
+ const look = presentation(info), assets = /* @__PURE__ */ new Map();
357
+ const r = reader(body, info, dialect, look, assets);
358
+ const abnf = dialect === "abnf";
359
+ const key = (name) => abnf ? name.toLowerCase() : name;
360
+ try {
361
+ if (body.length > 2e5) r.fail("source-limit", "语法文本过长", "Railroad source is too large", "拆分语法图", "Split the grammar into diagrams");
362
+ while (r.peek().kind !== "end") {
363
+ if (r.presentationLine()) continue;
364
+ if (abnf && r.take(";")) {
365
+ r.commentTail();
366
+ continue;
367
+ }
368
+ const heading = r.take("title");
369
+ if (heading) {
370
+ const raw = heading.value.slice(5).trim();
371
+ const text = /^["']/.test(raw) && raw.at(-1) === raw[0] ? unescape(raw.slice(1, -1)) : raw;
372
+ title = r.label({}, text, heading).label ?? text;
373
+ continue;
374
+ }
375
+ const accessibility = r.take("accessibility");
376
+ if (accessibility) {
377
+ const raw = accessibility.value;
378
+ if (raw.startsWith("accTitle")) accTitle = raw.slice(raw.indexOf(":") + 1).trim();
379
+ else if (/^accDescr\s*\{/.test(raw)) accDescr = raw.slice(raw.indexOf("{") + 1, -1).trim();
380
+ else accDescr = raw.slice(raw.indexOf(":") + 1).trim();
381
+ continue;
382
+ }
383
+ const name = r.expect("id");
384
+ const rule = r.decorate({
385
+ name: name.value,
386
+ line: name.line,
387
+ column: name.column
388
+ }, name.value, name);
389
+ if (dialect === "peg") r.expect("<-", "peg-bad-operator");
390
+ else if (!(dialect === "ebnf" && r.take("::="))) r.expect("=");
391
+ const definition = dialect === "ir" ? irExpression(r) : expression(r, dialect, hints);
392
+ r.expect(";", "rule-expected");
393
+ if (abnf) r.commentTail();
394
+ const stack = [[definition, 1]];
395
+ while (stack.length) {
396
+ const [node, depth] = stack.pop();
397
+ if (++count > CAP) r.fail("node-limit", "语法图节点过多", "Too many railroad nodes", "拆分语法图", "Split the grammar into diagrams", name);
398
+ if (depth > DEPTH) r.fail("depth-limit", "语法轨道嵌套过深", "Railroad nesting is too deep", "将嵌套部分提取为规则", "Extract nested expressions into rules", name);
399
+ for (const child of childrenOf(node)) stack.push([child, depth + 1]);
400
+ }
401
+ rules.push({
402
+ ...rule,
403
+ definition
404
+ });
405
+ }
406
+ } catch (cause) {
407
+ if (!(cause instanceof RailroadSyntax)) throw cause;
408
+ return {
409
+ model: null,
410
+ errors: [cause.diagnostic]
411
+ };
412
+ }
413
+ const names = /* @__PURE__ */ new Set(), missing = /* @__PURE__ */ new Set(), duplicates = /* @__PURE__ */ new Set();
414
+ const hint = (at, zh, en, fixZh, fixEn) => errors.push(warning("DIAGRAM_HINT", info.lang, at.line, zh, en, fixZh, fixEn, at.column));
415
+ for (const rule of rules) {
416
+ const name = key(rule.name);
417
+ if (names.has(name) && !duplicates.has(name)) {
418
+ duplicates.add(name);
419
+ hint(rule, `规则「${rule.name}」重复`, `Duplicate rule ${rule.name}`, "合并重复规则", "Combine duplicate rules");
420
+ }
421
+ names.add(name);
422
+ }
423
+ const decorate = (subject, id) => {
424
+ const status = look.statusOf(id), link = look.linkOf(id);
425
+ if (status) subject.status = status;
426
+ if (link) subject.link = link;
427
+ };
428
+ for (const rule of rules) {
429
+ decorate(rule, rule.name);
430
+ const stack = [rule.definition];
431
+ while (stack.length) {
432
+ const node = stack.pop();
433
+ if (node.type === "nonterminal") decorate(node, node.name);
434
+ if (node.type === "nonterminal" && !names.has(key(node.name)) && !(abnf && ABNF_CORE.has(key(node.name)))) {
435
+ node.undefined = true;
436
+ if (!missing.has(key(node.name))) {
437
+ missing.add(key(node.name));
438
+ hint(node, `未定义规则「${node.name}」`, `Undefined rule ${node.name}`, "定义该规则或检查拼写", "Define the rule or check its spelling");
439
+ }
440
+ }
441
+ stack.push(...childrenOf(node).slice().reverse());
442
+ }
443
+ }
444
+ if (hints.length) hint(hints[0], "复合前瞻显示为 (…)", "Compound lookahead is shown as (...)", "用命名规则显示前瞻目标", "Use a named rule to show the lookahead target");
445
+ return {
446
+ model: {
447
+ rules,
448
+ title,
449
+ accTitle,
450
+ accDescr,
451
+ count,
452
+ edgeStatusOf: look.edgeStatusOf
453
+ },
454
+ errors: [
455
+ ...errors,
456
+ ...assets.values(),
457
+ ...look.diagnostics()
458
+ ]
459
+ };
460
+ }
461
+ var parseEbnf = (body, info) => parseRules(body, info, "ebnf");
462
+ var parseAbnf = (body, info) => parseRules(body, info, "abnf");
463
+ var parsePeg = (body, info) => parseRules(body, info, "peg");
464
+ var parseIr = (body, info) => parseRules(body, info, "ir");
465
+ export { childrenOf, parseAbnf, parseEbnf, parseIr, parsePeg, railroad_parse_exports };