@graphty/graph-io 0.0.0 → 0.2.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 (339) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +250 -28
  3. package/dist/chunks/children-CL3Cy0ez.js +238 -0
  4. package/dist/chunks/children-CL3Cy0ez.js.map +1 -0
  5. package/dist/chunks/escape-DyI8JofU.js +938 -0
  6. package/dist/chunks/escape-DyI8JofU.js.map +1 -0
  7. package/dist/chunks/importer-CQnJuWJw.js +2987 -0
  8. package/dist/chunks/importer-CQnJuWJw.js.map +1 -0
  9. package/dist/chunks/importer-CpCpfbxr.js +2015 -0
  10. package/dist/chunks/importer-CpCpfbxr.js.map +1 -0
  11. package/dist/chunks/importer-DbnGYr3_.js +2342 -0
  12. package/dist/chunks/importer-DbnGYr3_.js.map +1 -0
  13. package/dist/chunks/importer-GozH8DkN.js +3050 -0
  14. package/dist/chunks/importer-GozH8DkN.js.map +1 -0
  15. package/dist/chunks/records-CGpxszm1.js +605 -0
  16. package/dist/chunks/records-CGpxszm1.js.map +1 -0
  17. package/dist/chunks/text-CajMdVFy.js +189 -0
  18. package/dist/chunks/text-CajMdVFy.js.map +1 -0
  19. package/dist/chunks/writer-DxSKC7TL.js +2842 -0
  20. package/dist/chunks/writer-DxSKC7TL.js.map +1 -0
  21. package/dist/csv.d.ts +1 -0
  22. package/dist/csv.js +1702 -0
  23. package/dist/csv.js.map +1 -0
  24. package/dist/dot.d.ts +1 -0
  25. package/dist/dot.js +8 -0
  26. package/dist/dot.js.map +1 -0
  27. package/dist/gexf.d.ts +1 -0
  28. package/dist/gexf.js +3466 -0
  29. package/dist/gexf.js.map +1 -0
  30. package/dist/gml.d.ts +1 -0
  31. package/dist/gml.js +2647 -0
  32. package/dist/gml.js.map +1 -0
  33. package/dist/graph-io.d.ts +1 -0
  34. package/dist/graph-io.js +790 -0
  35. package/dist/graph-io.js.map +1 -0
  36. package/dist/graphml.d.ts +1 -0
  37. package/dist/graphml.js +8 -0
  38. package/dist/graphml.js.map +1 -0
  39. package/dist/json.d.ts +1 -0
  40. package/dist/json.js +11 -0
  41. package/dist/json.js.map +1 -0
  42. package/dist/neo4j.d.ts +1 -0
  43. package/dist/neo4j.js +2046 -0
  44. package/dist/neo4j.js.map +1 -0
  45. package/dist/pajek.d.ts +1 -0
  46. package/dist/pajek.js +8 -0
  47. package/dist/pajek.js.map +1 -0
  48. package/dist/src/children.d.ts +134 -0
  49. package/dist/src/children.d.ts.map +1 -0
  50. package/dist/src/children.js +274 -0
  51. package/dist/src/children.js.map +1 -0
  52. package/dist/src/common/attributes.d.ts +229 -0
  53. package/dist/src/common/attributes.d.ts.map +1 -0
  54. package/dist/src/common/attributes.js +368 -0
  55. package/dist/src/common/attributes.js.map +1 -0
  56. package/dist/src/common/codes.d.ts +105 -0
  57. package/dist/src/common/codes.d.ts.map +1 -0
  58. package/dist/src/common/codes.js +107 -0
  59. package/dist/src/common/codes.js.map +1 -0
  60. package/dist/src/common/declared-types.d.ts +84 -0
  61. package/dist/src/common/declared-types.d.ts.map +1 -0
  62. package/dist/src/common/declared-types.js +326 -0
  63. package/dist/src/common/declared-types.js.map +1 -0
  64. package/dist/src/common/direction.d.ts +206 -0
  65. package/dist/src/common/direction.d.ts.map +1 -0
  66. package/dist/src/common/direction.js +370 -0
  67. package/dist/src/common/direction.js.map +1 -0
  68. package/dist/src/common/escape.d.ts +92 -0
  69. package/dist/src/common/escape.d.ts.map +1 -0
  70. package/dist/src/common/escape.js +212 -0
  71. package/dist/src/common/escape.js.map +1 -0
  72. package/dist/src/common/export.d.ts +249 -0
  73. package/dist/src/common/export.d.ts.map +1 -0
  74. package/dist/src/common/export.js +594 -0
  75. package/dist/src/common/export.js.map +1 -0
  76. package/dist/src/common/format.d.ts +59 -0
  77. package/dist/src/common/format.d.ts.map +1 -0
  78. package/dist/src/common/format.js +106 -0
  79. package/dist/src/common/format.js.map +1 -0
  80. package/dist/src/common/ids.d.ts +83 -0
  81. package/dist/src/common/ids.d.ts.map +1 -0
  82. package/dist/src/common/ids.js +158 -0
  83. package/dist/src/common/ids.js.map +1 -0
  84. package/dist/src/common/input.d.ts +100 -0
  85. package/dist/src/common/input.d.ts.map +1 -0
  86. package/dist/src/common/input.js +335 -0
  87. package/dist/src/common/input.js.map +1 -0
  88. package/dist/src/common/lists.d.ts +34 -0
  89. package/dist/src/common/lists.d.ts.map +1 -0
  90. package/dist/src/common/lists.js +185 -0
  91. package/dist/src/common/lists.js.map +1 -0
  92. package/dist/src/common/options.d.ts +108 -0
  93. package/dist/src/common/options.d.ts.map +1 -0
  94. package/dist/src/common/options.js +265 -0
  95. package/dist/src/common/options.js.map +1 -0
  96. package/dist/src/common/report.d.ts +187 -0
  97. package/dist/src/common/report.d.ts.map +1 -0
  98. package/dist/src/common/report.js +274 -0
  99. package/dist/src/common/report.js.map +1 -0
  100. package/dist/src/common/temporal.d.ts +71 -0
  101. package/dist/src/common/temporal.d.ts.map +1 -0
  102. package/dist/src/common/temporal.js +266 -0
  103. package/dist/src/common/temporal.js.map +1 -0
  104. package/dist/src/common/text.d.ts +104 -0
  105. package/dist/src/common/text.d.ts.map +1 -0
  106. package/dist/src/common/text.js +255 -0
  107. package/dist/src/common/text.js.map +1 -0
  108. package/dist/src/common/weights.d.ts +77 -0
  109. package/dist/src/common/weights.d.ts.map +1 -0
  110. package/dist/src/common/weights.js +156 -0
  111. package/dist/src/common/weights.js.map +1 -0
  112. package/dist/src/common/writer.d.ts +51 -0
  113. package/dist/src/common/writer.d.ts.map +1 -0
  114. package/dist/src/common/writer.js +108 -0
  115. package/dist/src/common/writer.js.map +1 -0
  116. package/dist/src/common/xml.d.ts +245 -0
  117. package/dist/src/common/xml.d.ts.map +1 -0
  118. package/dist/src/common/xml.js +942 -0
  119. package/dist/src/common/xml.js.map +1 -0
  120. package/dist/src/formats/csv/exporter.d.ts +70 -0
  121. package/dist/src/formats/csv/exporter.d.ts.map +1 -0
  122. package/dist/src/formats/csv/exporter.js +682 -0
  123. package/dist/src/formats/csv/exporter.js.map +1 -0
  124. package/dist/src/formats/csv/header.d.ts +66 -0
  125. package/dist/src/formats/csv/header.d.ts.map +1 -0
  126. package/dist/src/formats/csv/header.js +152 -0
  127. package/dist/src/formats/csv/header.js.map +1 -0
  128. package/dist/src/formats/csv/importer.d.ts +82 -0
  129. package/dist/src/formats/csv/importer.d.ts.map +1 -0
  130. package/dist/src/formats/csv/importer.js +849 -0
  131. package/dist/src/formats/csv/importer.js.map +1 -0
  132. package/dist/src/formats/csv/index.d.ts +60 -0
  133. package/dist/src/formats/csv/index.d.ts.map +1 -0
  134. package/dist/src/formats/csv/index.js +63 -0
  135. package/dist/src/formats/csv/index.js.map +1 -0
  136. package/dist/src/formats/csv/records.d.ts +188 -0
  137. package/dist/src/formats/csv/records.d.ts.map +1 -0
  138. package/dist/src/formats/csv/records.js +702 -0
  139. package/dist/src/formats/csv/records.js.map +1 -0
  140. package/dist/src/formats/csv/values.d.ts +105 -0
  141. package/dist/src/formats/csv/values.d.ts.map +1 -0
  142. package/dist/src/formats/csv/values.js +192 -0
  143. package/dist/src/formats/csv/values.js.map +1 -0
  144. package/dist/src/formats/dot/exporter.d.ts +52 -0
  145. package/dist/src/formats/dot/exporter.d.ts.map +1 -0
  146. package/dist/src/formats/dot/exporter.js +836 -0
  147. package/dist/src/formats/dot/exporter.js.map +1 -0
  148. package/dist/src/formats/dot/importer.d.ts +102 -0
  149. package/dist/src/formats/dot/importer.d.ts.map +1 -0
  150. package/dist/src/formats/dot/importer.js +1291 -0
  151. package/dist/src/formats/dot/importer.js.map +1 -0
  152. package/dist/src/formats/dot/index.d.ts +7 -0
  153. package/dist/src/formats/dot/index.d.ts.map +1 -0
  154. package/dist/src/formats/dot/index.js +7 -0
  155. package/dist/src/formats/dot/index.js.map +1 -0
  156. package/dist/src/formats/dot/names.d.ts +29 -0
  157. package/dist/src/formats/dot/names.d.ts.map +1 -0
  158. package/dist/src/formats/dot/names.js +28 -0
  159. package/dist/src/formats/dot/names.js.map +1 -0
  160. package/dist/src/formats/dot/tokenizer.d.ts +114 -0
  161. package/dist/src/formats/dot/tokenizer.d.ts.map +1 -0
  162. package/dist/src/formats/dot/tokenizer.js +341 -0
  163. package/dist/src/formats/dot/tokenizer.js.map +1 -0
  164. package/dist/src/formats/gexf/exporter.d.ts +56 -0
  165. package/dist/src/formats/gexf/exporter.d.ts.map +1 -0
  166. package/dist/src/formats/gexf/exporter.js +1395 -0
  167. package/dist/src/formats/gexf/exporter.js.map +1 -0
  168. package/dist/src/formats/gexf/importer.d.ts +73 -0
  169. package/dist/src/formats/gexf/importer.d.ts.map +1 -0
  170. package/dist/src/formats/gexf/importer.js +1880 -0
  171. package/dist/src/formats/gexf/importer.js.map +1 -0
  172. package/dist/src/formats/gexf/index.d.ts +96 -0
  173. package/dist/src/formats/gexf/index.d.ts.map +1 -0
  174. package/dist/src/formats/gexf/index.js +97 -0
  175. package/dist/src/formats/gexf/index.js.map +1 -0
  176. package/dist/src/formats/gexf/schema.d.ts +135 -0
  177. package/dist/src/formats/gexf/schema.d.ts.map +1 -0
  178. package/dist/src/formats/gexf/schema.js +323 -0
  179. package/dist/src/formats/gexf/schema.js.map +1 -0
  180. package/dist/src/formats/gml/exporter.d.ts +69 -0
  181. package/dist/src/formats/gml/exporter.d.ts.map +1 -0
  182. package/dist/src/formats/gml/exporter.js +1093 -0
  183. package/dist/src/formats/gml/exporter.js.map +1 -0
  184. package/dist/src/formats/gml/importer.d.ts +66 -0
  185. package/dist/src/formats/gml/importer.d.ts.map +1 -0
  186. package/dist/src/formats/gml/importer.js +1331 -0
  187. package/dist/src/formats/gml/importer.js.map +1 -0
  188. package/dist/src/formats/gml/index.d.ts +85 -0
  189. package/dist/src/formats/gml/index.d.ts.map +1 -0
  190. package/dist/src/formats/gml/index.js +88 -0
  191. package/dist/src/formats/gml/index.js.map +1 -0
  192. package/dist/src/formats/gml/syntax.d.ts +186 -0
  193. package/dist/src/formats/gml/syntax.d.ts.map +1 -0
  194. package/dist/src/formats/gml/syntax.js +467 -0
  195. package/dist/src/formats/gml/syntax.js.map +1 -0
  196. package/dist/src/formats/graphml/constants.d.ts +169 -0
  197. package/dist/src/formats/graphml/constants.d.ts.map +1 -0
  198. package/dist/src/formats/graphml/constants.js +165 -0
  199. package/dist/src/formats/graphml/constants.js.map +1 -0
  200. package/dist/src/formats/graphml/exporter.d.ts +34 -0
  201. package/dist/src/formats/graphml/exporter.d.ts.map +1 -0
  202. package/dist/src/formats/graphml/exporter.js +1176 -0
  203. package/dist/src/formats/graphml/exporter.js.map +1 -0
  204. package/dist/src/formats/graphml/importer.d.ts +31 -0
  205. package/dist/src/formats/graphml/importer.d.ts.map +1 -0
  206. package/dist/src/formats/graphml/importer.js +1607 -0
  207. package/dist/src/formats/graphml/importer.js.map +1 -0
  208. package/dist/src/formats/graphml/index.d.ts +8 -0
  209. package/dist/src/formats/graphml/index.d.ts.map +1 -0
  210. package/dist/src/formats/graphml/index.js +8 -0
  211. package/dist/src/formats/graphml/index.js.map +1 -0
  212. package/dist/src/formats/graphml/tree.d.ts +72 -0
  213. package/dist/src/formats/graphml/tree.d.ts.map +1 -0
  214. package/dist/src/formats/graphml/tree.js +290 -0
  215. package/dist/src/formats/graphml/tree.js.map +1 -0
  216. package/dist/src/formats/json/dialect.d.ts +125 -0
  217. package/dist/src/formats/json/dialect.d.ts.map +1 -0
  218. package/dist/src/formats/json/dialect.js +262 -0
  219. package/dist/src/formats/json/dialect.js.map +1 -0
  220. package/dist/src/formats/json/exporter.d.ts +89 -0
  221. package/dist/src/formats/json/exporter.d.ts.map +1 -0
  222. package/dist/src/formats/json/exporter.js +1358 -0
  223. package/dist/src/formats/json/exporter.js.map +1 -0
  224. package/dist/src/formats/json/importer.d.ts +108 -0
  225. package/dist/src/formats/json/importer.d.ts.map +1 -0
  226. package/dist/src/formats/json/importer.js +1838 -0
  227. package/dist/src/formats/json/importer.js.map +1 -0
  228. package/dist/src/formats/json/index.d.ts +8 -0
  229. package/dist/src/formats/json/index.d.ts.map +1 -0
  230. package/dist/src/formats/json/index.js +8 -0
  231. package/dist/src/formats/json/index.js.map +1 -0
  232. package/dist/src/formats/neo4j/exporter.d.ts +68 -0
  233. package/dist/src/formats/neo4j/exporter.d.ts.map +1 -0
  234. package/dist/src/formats/neo4j/exporter.js +1055 -0
  235. package/dist/src/formats/neo4j/exporter.js.map +1 -0
  236. package/dist/src/formats/neo4j/header.d.ts +52 -0
  237. package/dist/src/formats/neo4j/header.d.ts.map +1 -0
  238. package/dist/src/formats/neo4j/header.js +131 -0
  239. package/dist/src/formats/neo4j/header.js.map +1 -0
  240. package/dist/src/formats/neo4j/importer.d.ts +73 -0
  241. package/dist/src/formats/neo4j/importer.d.ts.map +1 -0
  242. package/dist/src/formats/neo4j/importer.js +932 -0
  243. package/dist/src/formats/neo4j/importer.js.map +1 -0
  244. package/dist/src/formats/neo4j/index.d.ts +79 -0
  245. package/dist/src/formats/neo4j/index.d.ts.map +1 -0
  246. package/dist/src/formats/neo4j/index.js +83 -0
  247. package/dist/src/formats/neo4j/index.js.map +1 -0
  248. package/dist/src/formats/pajek/exporter.d.ts +58 -0
  249. package/dist/src/formats/pajek/exporter.d.ts.map +1 -0
  250. package/dist/src/formats/pajek/exporter.js +825 -0
  251. package/dist/src/formats/pajek/exporter.js.map +1 -0
  252. package/dist/src/formats/pajek/importer.d.ts +88 -0
  253. package/dist/src/formats/pajek/importer.d.ts.map +1 -0
  254. package/dist/src/formats/pajek/importer.js +1047 -0
  255. package/dist/src/formats/pajek/importer.js.map +1 -0
  256. package/dist/src/formats/pajek/index.d.ts +7 -0
  257. package/dist/src/formats/pajek/index.d.ts.map +1 -0
  258. package/dist/src/formats/pajek/index.js +7 -0
  259. package/dist/src/formats/pajek/index.js.map +1 -0
  260. package/dist/src/formats/pajek/syntax.d.ts +112 -0
  261. package/dist/src/formats/pajek/syntax.d.ts.map +1 -0
  262. package/dist/src/formats/pajek/syntax.js +269 -0
  263. package/dist/src/formats/pajek/syntax.js.map +1 -0
  264. package/dist/src/index.d.ts +35 -0
  265. package/dist/src/index.d.ts.map +1 -0
  266. package/dist/src/index.js +39 -0
  267. package/dist/src/index.js.map +1 -0
  268. package/dist/src/registry.d.ts +207 -0
  269. package/dist/src/registry.d.ts.map +1 -0
  270. package/dist/src/registry.js +481 -0
  271. package/dist/src/registry.js.map +1 -0
  272. package/dist/src/sniff.d.ts +104 -0
  273. package/dist/src/sniff.d.ts.map +1 -0
  274. package/dist/src/sniff.js +357 -0
  275. package/dist/src/sniff.js.map +1 -0
  276. package/dist/src/types.d.ts +238 -0
  277. package/dist/src/types.d.ts.map +1 -0
  278. package/dist/src/types.js +29 -0
  279. package/dist/src/types.js.map +1 -0
  280. package/dist/tsconfig.build.tsbuildinfo +1 -0
  281. package/package.json +122 -7
  282. package/src/children.ts +335 -0
  283. package/src/common/attributes.ts +520 -0
  284. package/src/common/codes.ts +153 -0
  285. package/src/common/declared-types.ts +374 -0
  286. package/src/common/direction.ts +518 -0
  287. package/src/common/escape.ts +231 -0
  288. package/src/common/export.ts +817 -0
  289. package/src/common/format.ts +111 -0
  290. package/src/common/ids.ts +176 -0
  291. package/src/common/input.ts +378 -0
  292. package/src/common/lists.ts +196 -0
  293. package/src/common/options.ts +377 -0
  294. package/src/common/report.ts +352 -0
  295. package/src/common/temporal.ts +302 -0
  296. package/src/common/text.ts +294 -0
  297. package/src/common/weights.ts +202 -0
  298. package/src/common/writer.ts +123 -0
  299. package/src/common/xml.ts +1053 -0
  300. package/src/formats/csv/exporter.ts +894 -0
  301. package/src/formats/csv/header.ts +172 -0
  302. package/src/formats/csv/importer.ts +1104 -0
  303. package/src/formats/csv/index.ts +88 -0
  304. package/src/formats/csv/records.ts +813 -0
  305. package/src/formats/csv/values.ts +224 -0
  306. package/src/formats/dot/exporter.ts +1014 -0
  307. package/src/formats/dot/importer.ts +1549 -0
  308. package/src/formats/dot/index.ts +7 -0
  309. package/src/formats/dot/names.ts +40 -0
  310. package/src/formats/dot/tokenizer.ts +384 -0
  311. package/src/formats/gexf/exporter.ts +1696 -0
  312. package/src/formats/gexf/importer.ts +2333 -0
  313. package/src/formats/gexf/index.ts +142 -0
  314. package/src/formats/gexf/schema.ts +361 -0
  315. package/src/formats/gml/exporter.ts +1404 -0
  316. package/src/formats/gml/importer.ts +1591 -0
  317. package/src/formats/gml/index.ts +128 -0
  318. package/src/formats/gml/syntax.ts +545 -0
  319. package/src/formats/graphml/constants.ts +225 -0
  320. package/src/formats/graphml/exporter.ts +1458 -0
  321. package/src/formats/graphml/importer.ts +2027 -0
  322. package/src/formats/graphml/index.ts +8 -0
  323. package/src/formats/graphml/tree.ts +318 -0
  324. package/src/formats/json/dialect.ts +317 -0
  325. package/src/formats/json/exporter.ts +1616 -0
  326. package/src/formats/json/importer.ts +2271 -0
  327. package/src/formats/json/index.ts +8 -0
  328. package/src/formats/neo4j/exporter.ts +1287 -0
  329. package/src/formats/neo4j/header.ts +156 -0
  330. package/src/formats/neo4j/importer.ts +1220 -0
  331. package/src/formats/neo4j/index.ts +116 -0
  332. package/src/formats/pajek/exporter.ts +1000 -0
  333. package/src/formats/pajek/importer.ts +1311 -0
  334. package/src/formats/pajek/index.ts +7 -0
  335. package/src/formats/pajek/syntax.ts +307 -0
  336. package/src/index.ts +244 -0
  337. package/src/registry.ts +617 -0
  338. package/src/sniff.ts +397 -0
  339. package/src/types.ts +262 -0
@@ -0,0 +1,1291 @@
1
+ /**
2
+ * The DOT / Graphviz importer (design sections 8.4, 4.1, 5.1; research note 07 section 2.4). A
3
+ * recursive-descent parser over the DOT grammar pushes nodes and edges into the sink as they are
4
+ * mentioned, in first-mention order:
5
+ *
6
+ * - `graph` / `digraph` set the direction through the DirectionResolver (rule 1 of 8.4); `strict`
7
+ * merges parallel edges as cgraph does and is recorded in `meta.extra.dot.strict`; the graph name
8
+ * becomes `meta.name`.
9
+ * - node, edge and graph attribute statements (`node [..]`, `edge [..]`, `graph [..]`, `ID = ID`)
10
+ * are stateful, scoped defaults applied to the elements created after them in the same or a
11
+ * nested subgraph; every element receives its effective attributes as plain cells, so the
12
+ * exporter writes them back explicitly.
13
+ * - edge chains `a -> b -> c` and subgraph endpoints `{a b} -> c` expand to one edge per pair, in
14
+ * cgraph order; a port on an edge endpoint is kept in the `graphty.sourcePort` /
15
+ * `graphty.targetPort` edge columns (roles sourcePort / targetPort) and never part of the id; a
16
+ * port on a node statement has no meaning and is dropped with a warning.
17
+ * - a subgraph named `cluster*` (or carrying `cluster=true`) becomes a container NODE whose id is
18
+ * the cluster name (design section 5.10: containment is the `parent` role, never an adjacency
19
+ * edge); members get `graphty.parent` = the container's index, the container's own attributes are
20
+ * its node cells, and `graphty.cluster` = true marks it. A plain node and a cluster of the same
21
+ * name (fdp's cluster edges) merge into that one node with a warning. Other subgraphs are
22
+ * transparent grouping; their attributes are reported as dropped.
23
+ * - attribute values are ID strings inferred per column by the sink under the 5.1 grammar, except
24
+ * `label` (text, role label), `pos` on a node (the position role column, f32 x3, design section
25
+ * 5.2), `weight` (THE weight, `weightFrom`) and `key` (cgraph's edge identity, the edge id role).
26
+ * - ids are coerced with the common rule (`canonical` by default: `1` and `"1"` are the same node,
27
+ * as the DOT grammar says).
28
+ *
29
+ * The whole text is read first (design section 8.4 allows it for DOT). A grammar violation is
30
+ * fatal, as it is for Graphviz itself: the import aborts with ImportError (code E_DOT_SYNTAX)
31
+ * carrying the partial report. Errors the sink raises for one element are recorded and the element
32
+ * is skipped (section 8.6).
33
+ */
34
+ import { GraphFormatError, INVALID_INDEX, } from "@graphty/graph-format";
35
+ import { declareResolved } from "../../common/attributes.js";
36
+ import { COLUMN_RENAMED_CODE, DIRECTION_FORCED_CODE, DIRECTION_REFUSED_CODE, EMPTY_INPUT_CODE, ID_MERGED_CODE, INVALID_UTF8_CODE, MIXED_DIRECTION_CODE, MULTIPLE_GRAPHS_CODE, OPTION_IGNORED_CODE, ROLE_TAKEN_CODE, SINK_OPTION_CODE, SYNTAX_CODE, } from "../../common/codes.js";
37
+ import { DirectionResolver } from "../../common/direction.js";
38
+ import { IdCoercer } from "../../common/ids.js";
39
+ import { readText, throwIfAborted } from "../../common/input.js";
40
+ import { reportSinkOptions, reportUnusedOptions, resolveImportOptions, } from "../../common/options.js";
41
+ import { ImportReportBuilder } from "../../common/report.js";
42
+ import { parseTextCell, TextCellWriter, WIDENING_UNSUPPORTED_CODE } from "../../common/text.js";
43
+ import { parseWeightText } from "../../common/weights.js";
44
+ import { CLUSTER_COLUMN, DOT_FORMAT, DOT_ORIGIN, KEY_ATTRIBUTE, LABEL_ATTRIBUTE, PARENT_COLUMN, PIN_ATTRIBUTE, POS_ATTRIBUTE, SOURCE_PORT_COLUMN, TARGET_PORT_COLUMN, } from "./names.js";
45
+ import { DotSyntaxError, DotTokenizer } from "./tokenizer.js";
46
+ /**
47
+ * The issue codes the DOT importer records (design section 8.6), by name: the codes shared with
48
+ * the other importers (src/common/codes.ts) and the DOT-specific ones. A key is the code without
49
+ * its severity and format prefixes.
50
+ */
51
+ export const DOT_ISSUE = Object.freeze({
52
+ /** A grammar violation; fatal. */
53
+ SYNTAX: SYNTAX_CODE,
54
+ /** The input holds no graph at all (empty or only comments); fatal. */
55
+ EMPTY_INPUT: EMPTY_INPUT_CODE,
56
+ /** The input holds invalid UTF-8 (fatal). */
57
+ INVALID_UTF8: INVALID_UTF8_CODE,
58
+ /** Subgraphs or braces nested deeper than the parser's limit; fatal. */
59
+ NESTING: "E_DOT_NESTING",
60
+ /** An edge operator contradicting the graph keyword (warning under "operator" / "header"). */
61
+ EDGE_OPERATOR: "W_DOT_EDGE_OPERATOR",
62
+ /** A second graph in the same input; only the first is read. */
63
+ MULTIPLE_GRAPHS: MULTIPLE_GRAPHS_CODE,
64
+ /** A badly delimited numeral (`1e3`) split into two tokens, as Graphviz does with a warning. */
65
+ NUMERAL_AMBIGUITY: "W_DOT_NUMERAL_AMBIGUITY",
66
+ /** Attributes of a subgraph that is not a cluster (rank=same and the like) cannot be represented. */
67
+ SUBGRAPH_ATTRIBUTES_DROPPED: "W_DOT_SUBGRAPH_ATTRIBUTES_DROPPED",
68
+ /** A port on a node statement has no meaning and was dropped. */
69
+ NODE_PORT_DROPPED: "W_DOT_NODE_PORT_DROPPED",
70
+ /** A plain node and a cluster share a name and were merged into one container node. */
71
+ CLUSTER_NODE_MERGED: "W_DOT_CLUSTER_NODE_MERGED",
72
+ /** A node mentioned in two unrelated clusters keeps the first. */
73
+ CLUSTER_CONFLICT: "W_DOT_CLUSTER_CONFLICT",
74
+ /** A node `pos` that is not a point; the value was dropped. */
75
+ BAD_POS: "W_DOT_BAD_POS",
76
+ /** A parallel edge merged into an earlier one under `strict`. */
77
+ STRICT_MERGED: "W_DOT_STRICT_MERGED",
78
+ /** An edge merged into an earlier one with the same endpoints and `key`. */
79
+ KEY_MERGED: "W_DOT_KEY_MERGED",
80
+ /** A role (label, id, position, ...) was already taken in the caller's sink; the column was declared without it. */
81
+ ROLE_TAKEN: ROLE_TAKEN_CODE,
82
+ /** A column of another shape exists in the caller's sink under a name the importer declares; renamed `<name>#<id>`. */
83
+ COLUMN_RENAMED: COLUMN_RENAMED_CODE,
84
+ /** A common option the format has no use for was given a non-default value. */
85
+ OPTION_IGNORED: OPTION_IGNORED_CODE,
86
+ /** Two distinct id texts merged under ids: "number". */
87
+ ID_MERGED: ID_MERGED_CODE,
88
+ /** A builder-policy option the sink does not honour. */
89
+ SINK_OPTION: SINK_OPTION_CODE,
90
+ /** The sink refused the file's direction. */
91
+ DIRECTION_REFUSED: DIRECTION_REFUSED_CODE,
92
+ /** Edges forced to the policy's direction. */
93
+ DIRECTION_FORCED: DIRECTION_FORCED_CODE,
94
+ /** A mixed file under onMixedDirection "error" (fatal). */
95
+ MIXED_DIRECTION: MIXED_DIRECTION_CODE,
96
+ /** A text column the sink could not widen to the dtype its cells imply. */
97
+ WIDENING_UNSUPPORTED: WIDENING_UNSUPPORTED_CODE,
98
+ });
99
+ /** The common options the DOT importer reads (the rest is reported by reportUnusedOptions). */
100
+ const USED_OPTIONS = new Set([
101
+ "ids",
102
+ "addMissingNodes",
103
+ "duplicateEdges",
104
+ "selfLoops",
105
+ "onMixedDirection",
106
+ "weightFrom",
107
+ "weightDtype",
108
+ "errorLimit",
109
+ "signal",
110
+ "onProgress",
111
+ ]);
112
+ /** The deepest nesting of subgraphs and braces the parser accepts (each level is one stack frame). */
113
+ const MAX_NESTING = 1024;
114
+ const EXTENSIONS = Object.freeze([".dot", ".gv"]);
115
+ const MIME_TYPES = Object.freeze(["text/vnd.graphviz"]);
116
+ const CLUSTER_PREFIX = "cluster";
117
+ const CLUSTER_ATTRIBUTE = "cluster";
118
+ const STATEMENTS_PER_ABORT_CHECK = 64;
119
+ const MAX_ANCESTOR_WALK = 4096;
120
+ const DOT_HEADER = /^\s*(strict\s+)?(di)?graph\b/i;
121
+ const TRUE_TEXTS = new Set(["true", "yes", "1"]);
122
+ const POINT_TEXT = /^\s*([-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?)\s*,\s*([-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?)(?:\s*,\s*([-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?))?\s*(!?)\s*$/;
123
+ /**
124
+ * The importer plugin for DOT / Graphviz text (design section 12.4).
125
+ */
126
+ export const dotImporter = Object.freeze({
127
+ format: DOT_FORMAT,
128
+ extensions: EXTENSIONS,
129
+ mimeTypes: MIME_TYPES,
130
+ /**
131
+ * Confidence that the head of an input is DOT: the `[strict] graph | digraph` header after
132
+ * optional comments.
133
+ * @param head - the first bytes of the input
134
+ * @returns 0.95 for a header followed by `{`, 0.8 for a header alone, 0 otherwise
135
+ */
136
+ sniff(head) {
137
+ const text = stripLeadingComments(new TextDecoder("utf-8").decode(head));
138
+ const match = DOT_HEADER.exec(text);
139
+ if (match === null) {
140
+ return 0;
141
+ }
142
+ return text.includes("{") ? 0.95 : 0.8;
143
+ },
144
+ /**
145
+ * Read a DOT document into the sink.
146
+ * @param input - the text, bytes or stream
147
+ * @param sink - the sink to push into
148
+ * @param options - format-specific and common options
149
+ * @returns the import report; ImportError (E_IMPORT) on a syntax error or beyond the error limit
150
+ */
151
+ async import(input, sink, options) {
152
+ const resolved = resolveImportOptions(options, {
153
+ ids: "canonical",
154
+ defaultDirected: true,
155
+ weightFrom: "weight",
156
+ });
157
+ const mismatch = mismatchOption(options?.mismatchedEdgeOperator);
158
+ const report = new ImportReportBuilder(DOT_FORMAT, resolved.errorLimit);
159
+ reportUnusedOptions(options, report, USED_OPTIONS);
160
+ reportSinkOptions(sink, options, report);
161
+ const text = await readText(input, report, resolved);
162
+ const parser = new DotParser(text, sink, report, resolved, mismatch);
163
+ try {
164
+ parser.parse();
165
+ }
166
+ catch (err) {
167
+ if (err instanceof DotSyntaxError) {
168
+ report.fail(SYNTAX_CODE, err.message, { line: err.line }, { line: err.line });
169
+ }
170
+ throw err;
171
+ }
172
+ // an abort raised during the last few statements (after the last periodic check) still rejects
173
+ throwIfAborted(resolved.signal);
174
+ return report.finish();
175
+ },
176
+ });
177
+ /**
178
+ * Resolve the mismatchedEdgeOperator option.
179
+ * @param value - the caller's value
180
+ * @returns the value or the default; E_UNSUPPORTED for anything else
181
+ */
182
+ function mismatchOption(value) {
183
+ if (value === undefined) {
184
+ return "operator";
185
+ }
186
+ if (value === "operator" || value === "header" || value === "error") {
187
+ return value;
188
+ }
189
+ throw new GraphFormatError("E_UNSUPPORTED", `option mismatchedEdgeOperator: ${JSON.stringify(value)} is not one of "operator", "header", "error"`, { option: "mismatchedEdgeOperator", found: value, supported: ["operator", "header", "error"] });
190
+ }
191
+ /**
192
+ * Remove leading whitespace and comments from a head sample so the header regex sees the keyword.
193
+ * @param text - the decoded head
194
+ * @returns the text from the first non-comment character
195
+ */
196
+ function stripLeadingComments(text) {
197
+ let rest = text;
198
+ for (;;) {
199
+ const trimmed = rest.replace(/^\s+/, "");
200
+ if (trimmed.startsWith("//") || trimmed.startsWith("#")) {
201
+ const nl = trimmed.search(/[\r\n]/);
202
+ if (nl < 0) {
203
+ return "";
204
+ }
205
+ rest = trimmed.slice(nl);
206
+ continue;
207
+ }
208
+ if (trimmed.startsWith("/*")) {
209
+ const end = trimmed.indexOf("*/");
210
+ if (end < 0) {
211
+ return "";
212
+ }
213
+ rest = trimmed.slice(end + 2);
214
+ continue;
215
+ }
216
+ return trimmed;
217
+ }
218
+ }
219
+ /**
220
+ * Whether a bare token is a given keyword (keywords are case-insensitive; a quoted id never is).
221
+ * @param token - the token
222
+ * @param keyword - the lower-case keyword
223
+ * @returns true for a match
224
+ */
225
+ function isKeyword(token, keyword) {
226
+ return token.kind === "id" && !token.quoted && !token.html && token.text.toLowerCase() === keyword;
227
+ }
228
+ /**
229
+ * Whether a token is a given punctuation.
230
+ * @param token - the token
231
+ * @param text - the punctuation
232
+ * @returns true for a match
233
+ */
234
+ function isPunct(token, text) {
235
+ return token.kind === "punct" && token.text === text;
236
+ }
237
+ /**
238
+ * Whether a token is an edge operator.
239
+ * @param token - the token
240
+ * @returns true for `->` or `--`
241
+ */
242
+ function isEdgeOp(token) {
243
+ return token.kind === "punct" && (token.text === "->" || token.text === "--");
244
+ }
245
+ /**
246
+ * A short description of a token for syntax error messages.
247
+ * @param token - the token
248
+ * @returns `end of input`, or the token text in quotes
249
+ */
250
+ function describeToken(token) {
251
+ if (token.kind === "eof") {
252
+ return "end of input";
253
+ }
254
+ return JSON.stringify(token.text.length > 40 ? `${token.text.slice(0, 40)}...` : token.text);
255
+ }
256
+ /**
257
+ * Whether a subgraph name marks a cluster.
258
+ * @param name - the name, or null
259
+ * @returns true when it starts with `cluster`
260
+ */
261
+ function isClusterName(name) {
262
+ return name !== null && name.startsWith(CLUSTER_PREFIX);
263
+ }
264
+ /**
265
+ * The parser and pusher for one import call.
266
+ */
267
+ class DotParser {
268
+ /**
269
+ * Create a parser over one document.
270
+ * @param text - the DOT text
271
+ * @param sink - the sink
272
+ * @param report - the report
273
+ * @param options - the resolved common options
274
+ * @param mismatch - the resolved mismatchedEdgeOperator option
275
+ */
276
+ constructor(text, sink, report, options, mismatch) {
277
+ this.directed = true;
278
+ this.strict = false;
279
+ this.statements = 0;
280
+ /** The id of every node mentioned so far, by index (the sink has no idOf). */
281
+ this.idOf = new Map();
282
+ /** Container nodes this import created or adopted. */
283
+ this.containers = new Set();
284
+ /** The parent assigned to a node by this import. */
285
+ this.parentOf = new Map();
286
+ /** Nodes already warned about a cluster conflict. */
287
+ this.conflictWarned = new Set();
288
+ /** Under `strict`: the edge index of every (source, target) pair pushed. */
289
+ this.strictEdges = new Map();
290
+ /** The edge index of every (source, target, key) triple pushed. */
291
+ this.keyedEdges = new Map();
292
+ /** Columns of the caller's sink adopted under a name the importer declares with another shape; values are inferred for them. */
293
+ this.nodeLabelHandle = INVALID_INDEX;
294
+ this.edgeLabelHandle = INVALID_INDEX;
295
+ this.positionHandle = INVALID_INDEX;
296
+ this.clusterHandle = INVALID_INDEX;
297
+ this.parentHandle = INVALID_INDEX;
298
+ this.keyHandle = INVALID_INDEX;
299
+ this.sourcePortHandle = INVALID_INDEX;
300
+ this.targetPortHandle = INVALID_INDEX;
301
+ /** The current subgraph nesting depth. */
302
+ this.depth = 0;
303
+ /** The inferred attribute columns by name, per domain (the 5.1 text grammar per column). */
304
+ this.nodeWriters = new Map();
305
+ this.edgeWriters = new Map();
306
+ this.lexer = new DotTokenizer(text, (numeral, line) => {
307
+ report.warning("validation-error", DOT_ISSUE.NUMERAL_AMBIGUITY, `badly delimited number ${JSON.stringify(numeral)} splits into two tokens (Graphviz warns the same)`, { line, element: numeral });
308
+ });
309
+ this.sink = sink;
310
+ this.report = report;
311
+ this.options = options;
312
+ this.mismatch = mismatch;
313
+ this.ids = new IdCoercer(options.ids);
314
+ this.resolver = new DirectionResolver(sink, report, options.onMixedDirection);
315
+ }
316
+ /**
317
+ * Parse the whole document: `[strict] (graph | digraph) [ID] { stmt_list }`.
318
+ */
319
+ parse() {
320
+ const { lexer } = this;
321
+ let token = lexer.next();
322
+ if (token.kind === "eof") {
323
+ this.report.fail(EMPTY_INPUT_CODE, "the input holds no graph (empty or only comments)", {
324
+ line: token.line,
325
+ });
326
+ }
327
+ if (isKeyword(token, "strict")) {
328
+ this.strict = true;
329
+ token = lexer.next();
330
+ }
331
+ if (isKeyword(token, "digraph")) {
332
+ this.directed = true;
333
+ }
334
+ else if (isKeyword(token, "graph")) {
335
+ this.directed = false;
336
+ }
337
+ else {
338
+ throw new DotSyntaxError(`expected "graph" or "digraph", found ${describeToken(token)}`, token.line);
339
+ }
340
+ const headerLine = token.line;
341
+ token = lexer.next();
342
+ let name = null;
343
+ if (token.kind === "id") {
344
+ name = token.text;
345
+ token = lexer.next();
346
+ }
347
+ if (!isPunct(token, "{")) {
348
+ throw new DotSyntaxError(`expected "{" after the graph header, found ${describeToken(token)}`, token.line);
349
+ }
350
+ this.resolver.setHeader(this.directed, { line: headerLine });
351
+ this.sink.setMeta({
352
+ name,
353
+ sourceFormat: DOT_FORMAT,
354
+ ...(this.strict ? { extra: { dot: { strict: true } } } : {}),
355
+ });
356
+ const root = this.newScope(null, headerLine, true, null);
357
+ this.statementList(root);
358
+ const trailing = lexer.next();
359
+ if (trailing.kind !== "eof") {
360
+ this.report.warning("unsupported", MULTIPLE_GRAPHS_CODE, `content after the closing brace of the graph (${describeToken(trailing)}) was not read; one graph per input`, { line: trailing.line });
361
+ }
362
+ }
363
+ /**
364
+ * Create a scope.
365
+ * @param parent - the enclosing scope, or null for the root
366
+ * @param line - the line the scope opens on
367
+ * @param root - whether this is the root graph
368
+ * @param name - the subgraph name, or null
369
+ * @returns the scope
370
+ */
371
+ newScope(parent, line, root, name) {
372
+ return {
373
+ root,
374
+ name,
375
+ line,
376
+ nodeDefaults: new Map(parent?.nodeDefaults),
377
+ edgeDefaults: new Map(parent?.edgeDefaults),
378
+ members: [],
379
+ memberSet: new Set(),
380
+ containers: [],
381
+ attributes: new Map(),
382
+ cluster: false,
383
+ container: INVALID_INDEX,
384
+ };
385
+ }
386
+ /**
387
+ * Parse `stmt_list }` for a scope whose `{` was consumed.
388
+ * @param scope - the scope
389
+ */
390
+ statementList(scope) {
391
+ const { lexer } = this;
392
+ for (;;) {
393
+ const token = lexer.peek();
394
+ if (isPunct(token, "}")) {
395
+ lexer.next();
396
+ return;
397
+ }
398
+ if (token.kind === "eof") {
399
+ throw new DotSyntaxError(scope.root
400
+ ? 'unexpected end of input: missing "}" closing the graph'
401
+ : `unexpected end of input: missing "}" closing the subgraph opened on line ${scope.line}`, token.line);
402
+ }
403
+ if (isPunct(token, ";")) {
404
+ lexer.next();
405
+ continue;
406
+ }
407
+ this.statement(scope);
408
+ if (++this.statements % STATEMENTS_PER_ABORT_CHECK === 0) {
409
+ throwIfAborted(this.options.signal);
410
+ }
411
+ }
412
+ }
413
+ /**
414
+ * Parse one statement of a scope.
415
+ * @param scope - the scope
416
+ */
417
+ statement(scope) {
418
+ const { lexer } = this;
419
+ const token = lexer.peek();
420
+ if (token.kind === "punct") {
421
+ if (token.text === "{") {
422
+ const group = this.subgraph(scope);
423
+ this.maybeEdgeStatement(scope, group, token.line);
424
+ return;
425
+ }
426
+ throw new DotSyntaxError(`unexpected ${describeToken(token)} at the start of a statement`, token.line);
427
+ }
428
+ if (!token.quoted && !token.html) {
429
+ const keyword = token.text.toLowerCase();
430
+ if (keyword === "node" || keyword === "edge" || keyword === "graph") {
431
+ lexer.next();
432
+ this.attributeStatement(scope, keyword, token.line);
433
+ return;
434
+ }
435
+ if (keyword === "subgraph") {
436
+ const group = this.subgraph(scope);
437
+ this.maybeEdgeStatement(scope, group, token.line);
438
+ return;
439
+ }
440
+ if (keyword === "digraph" || keyword === "strict") {
441
+ throw new DotSyntaxError(`unexpected keyword ${describeToken(token)} inside a graph`, token.line);
442
+ }
443
+ }
444
+ // ID '=' ID, a node statement, or an edge statement starting with a node
445
+ const id = this.identifier();
446
+ if (isPunct(lexer.peek(), "=")) {
447
+ lexer.next();
448
+ const value = this.identifier();
449
+ this.scopeAttribute(scope, { name: id.text, value: value.text, line: id.line });
450
+ return;
451
+ }
452
+ const port = this.port();
453
+ if (isEdgeOp(lexer.peek())) {
454
+ const endpoint = this.mentionEndpoint(scope, id, port);
455
+ this.edgeStatement(scope, endpoint === null ? [] : [endpoint], id.line);
456
+ return;
457
+ }
458
+ this.nodeStatement(scope, id, port);
459
+ }
460
+ /**
461
+ * Read an ID, concatenating quoted strings joined by `+`.
462
+ * @returns the id token (the concatenation keeps the first token's line and quoted flag)
463
+ */
464
+ identifier() {
465
+ const { lexer } = this;
466
+ const token = lexer.next();
467
+ if (token.kind !== "id") {
468
+ throw new DotSyntaxError(`expected an identifier, found ${describeToken(token)}`, token.line);
469
+ }
470
+ if (!token.quoted || !isPunct(lexer.peek(), "+")) {
471
+ return token;
472
+ }
473
+ let { text } = token;
474
+ while (isPunct(lexer.peek(), "+")) {
475
+ const plus = lexer.next();
476
+ const more = lexer.next();
477
+ if (more.kind !== "id" || !more.quoted) {
478
+ throw new DotSyntaxError(`expected a quoted string after "+", found ${describeToken(more)}`, more.kind === "eof" ? plus.line : more.line);
479
+ }
480
+ text += more.text;
481
+ }
482
+ return { kind: "id", text, quoted: true, html: false, line: token.line };
483
+ }
484
+ /**
485
+ * Read an optional port after a node id: `: ID [ : compass ]` or `: compass`.
486
+ * @returns the port text (`f0`, `f0:n`, `n`), or null when there is none
487
+ */
488
+ port() {
489
+ const { lexer } = this;
490
+ if (!isPunct(lexer.peek(), ":")) {
491
+ return null;
492
+ }
493
+ lexer.next();
494
+ let { text } = this.identifier();
495
+ if (isPunct(lexer.peek(), ":")) {
496
+ lexer.next();
497
+ text += `:${this.identifier().text}`;
498
+ }
499
+ return text;
500
+ }
501
+ /**
502
+ * Parse `[subgraph [ID]] { stmt_list }` and return its member nodes for use as an edge endpoint.
503
+ * @param parent - the enclosing scope
504
+ * @returns the endpoints (every node mentioned in the subgraph, in first-mention order)
505
+ */
506
+ subgraph(parent) {
507
+ const { lexer } = this;
508
+ let token = lexer.next();
509
+ let name = null;
510
+ if (isKeyword(token, "subgraph")) {
511
+ token = lexer.next();
512
+ if (token.kind === "id") {
513
+ name = token.text;
514
+ token = lexer.next();
515
+ }
516
+ }
517
+ if (!isPunct(token, "{")) {
518
+ throw new DotSyntaxError(`expected "{" to open a subgraph, found ${describeToken(token)}`, token.line);
519
+ }
520
+ if (++this.depth > MAX_NESTING) {
521
+ this.report.fail(DOT_ISSUE.NESTING, `subgraphs nested deeper than ${MAX_NESTING} levels (the parser recurses per level)`, { line: token.line });
522
+ }
523
+ const scope = this.newScope(parent, token.line, false, name);
524
+ if (isClusterName(name)) {
525
+ scope.cluster = true;
526
+ this.ensureContainer(scope);
527
+ }
528
+ this.statementList(scope);
529
+ this.depth--;
530
+ this.closeScope(scope, parent);
531
+ return scope.members.map((index) => ({ id: this.idOf.get(index) ?? index, index, port: null }));
532
+ }
533
+ /**
534
+ * Finish a subgraph: apply its attributes to the container node when it is a cluster (or report
535
+ * them dropped), assign parents to its members, and propagate members and containers upward.
536
+ * @param scope - the closed scope
537
+ * @param parent - the enclosing scope
538
+ */
539
+ closeScope(scope, parent) {
540
+ const { container } = scope;
541
+ if (container !== INVALID_INDEX) {
542
+ for (const attribute of scope.attributes.values()) {
543
+ if (attribute.name !== CLUSTER_ATTRIBUTE) {
544
+ this.setNodeAttribute(container, attribute.name, attribute.value, attribute.line);
545
+ }
546
+ }
547
+ for (const nested of scope.containers) {
548
+ if (nested !== container && !this.parentOf.has(nested)) {
549
+ this.setParent(nested, container);
550
+ }
551
+ }
552
+ for (const member of scope.members) {
553
+ if (member === container) {
554
+ continue;
555
+ }
556
+ const existing = this.parentOf.get(member);
557
+ if (existing === undefined) {
558
+ this.setParent(member, container);
559
+ }
560
+ else if (existing !== container && !this.isAncestor(container, existing)) {
561
+ this.clusterConflict(member, existing, container, scope.line);
562
+ }
563
+ }
564
+ parent.containers.push(container);
565
+ }
566
+ else {
567
+ if (scope.attributes.size > 0) {
568
+ const names = [...scope.attributes.keys()].join(", ");
569
+ this.report.warning("unsupported", DOT_ISSUE.SUBGRAPH_ATTRIBUTES_DROPPED, `subgraph ${scope.name === null ? "(anonymous)" : JSON.stringify(scope.name)} is not a cluster; its attributes (${names}) cannot be represented and were dropped`, { line: scope.line, element: scope.name });
570
+ }
571
+ for (const nested of scope.containers) {
572
+ parent.containers.push(nested);
573
+ }
574
+ }
575
+ if (!parent.root) {
576
+ for (const member of scope.members) {
577
+ if (!parent.memberSet.has(member)) {
578
+ parent.memberSet.add(member);
579
+ parent.members.push(member);
580
+ }
581
+ }
582
+ }
583
+ }
584
+ /**
585
+ * Whether `candidate` is an ancestor of `node` through the parents assigned so far.
586
+ * @param candidate - the possible ancestor
587
+ * @param node - the node whose chain is walked
588
+ * @returns true when candidate is reached
589
+ */
590
+ isAncestor(candidate, node) {
591
+ let current = node;
592
+ for (let steps = 0; current !== undefined && steps < MAX_ANCESTOR_WALK; steps++) {
593
+ if (current === candidate) {
594
+ return true;
595
+ }
596
+ current = this.parentOf.get(current);
597
+ }
598
+ return false;
599
+ }
600
+ /**
601
+ * Record a node mentioned in two unrelated clusters (once per node).
602
+ * @param member - the node
603
+ * @param existing - its parent
604
+ * @param container - the cluster it was also mentioned in
605
+ * @param line - the line of the losing cluster
606
+ */
607
+ clusterConflict(member, existing, container, line) {
608
+ if (this.conflictWarned.has(member)) {
609
+ return;
610
+ }
611
+ this.conflictWarned.add(member);
612
+ const id = this.idOf.get(member) ?? member;
613
+ this.report.warning("coercion", DOT_ISSUE.CLUSTER_CONFLICT, `node ${JSON.stringify(id)} is in cluster ${JSON.stringify(this.idOf.get(existing) ?? existing)} and in cluster ${JSON.stringify(this.idOf.get(container) ?? container)}; the first is kept`, { line, element: String(id) });
614
+ }
615
+ /**
616
+ * Assign a parent.
617
+ * @param node - the node index
618
+ * @param container - the container index
619
+ */
620
+ setParent(node, container) {
621
+ if (node === container) {
622
+ return;
623
+ }
624
+ try {
625
+ this.sink.setNodeValue(this.parentColumn(), node, container);
626
+ this.parentOf.set(node, container);
627
+ }
628
+ catch (err) {
629
+ this.report.recordError(err, { element: String(this.idOf.get(node) ?? node) });
630
+ }
631
+ }
632
+ /**
633
+ * Create (or adopt) the container node of a cluster scope.
634
+ * @param scope - the cluster scope
635
+ */
636
+ ensureContainer(scope) {
637
+ if (scope.container !== INVALID_INDEX || scope.name === null) {
638
+ return;
639
+ }
640
+ const id = this.coerceId(scope.name, scope.line);
641
+ if (id === null) {
642
+ return;
643
+ }
644
+ let index = this.sink.indexOf(id);
645
+ try {
646
+ if (index === INVALID_INDEX) {
647
+ index = this.sink.addNode(id);
648
+ this.report.counts.nodes++;
649
+ }
650
+ else if (!this.containers.has(index)) {
651
+ this.report.warning("coercion", DOT_ISSUE.CLUSTER_NODE_MERGED, `cluster ${JSON.stringify(scope.name)} and the node of the same name were merged into one container node`, { line: scope.line, element: scope.name });
652
+ }
653
+ this.idOf.set(index, id);
654
+ if (!this.containers.has(index)) {
655
+ this.containers.add(index);
656
+ this.sink.setNodeValue(this.clusterColumn(), index, true);
657
+ }
658
+ scope.container = index;
659
+ }
660
+ catch (err) {
661
+ this.report.recordError(err, { line: scope.line, element: scope.name });
662
+ if (index === INVALID_INDEX) {
663
+ this.report.counts.skippedNodes++;
664
+ }
665
+ }
666
+ }
667
+ /**
668
+ * Apply an `ID = ID` or `graph [..]` attribute: the graph table at the root, a buffered
669
+ * subgraph attribute otherwise (`cluster=true` turns the subgraph into a cluster).
670
+ * @param scope - the scope
671
+ * @param attribute - the attribute
672
+ */
673
+ scopeAttribute(scope, attribute) {
674
+ if (scope.root) {
675
+ this.setGraphAttribute(attribute);
676
+ return;
677
+ }
678
+ scope.attributes.set(attribute.name, attribute);
679
+ if (attribute.name === CLUSTER_ATTRIBUTE && TRUE_TEXTS.has(attribute.value.trim().toLowerCase())) {
680
+ scope.cluster = true;
681
+ this.ensureContainer(scope);
682
+ }
683
+ }
684
+ /**
685
+ * Parse `node|edge|graph attr_list` after the keyword.
686
+ * @param scope - the scope
687
+ * @param keyword - which defaults are set
688
+ * @param line - the keyword's line
689
+ */
690
+ attributeStatement(scope, keyword, line) {
691
+ if (!isPunct(this.lexer.peek(), "[")) {
692
+ const found = this.lexer.peek();
693
+ throw new DotSyntaxError(`expected "[" after "${keyword}", found ${describeToken(found)}`, found.line);
694
+ }
695
+ const attributes = this.attributeList();
696
+ switch (keyword) {
697
+ case "node":
698
+ for (const a of attributes) {
699
+ scope.nodeDefaults.set(a.name, a.value);
700
+ }
701
+ break;
702
+ case "edge":
703
+ for (const a of attributes) {
704
+ scope.edgeDefaults.set(a.name, a.value);
705
+ }
706
+ break;
707
+ case "graph":
708
+ for (const a of attributes) {
709
+ this.scopeAttribute(scope, a);
710
+ }
711
+ break;
712
+ default:
713
+ throw new DotSyntaxError(`unknown attribute statement "${keyword}"`, line);
714
+ }
715
+ }
716
+ /**
717
+ * Parse one or more `[ a_list ]` groups.
718
+ * @returns the attributes in order (a repeated name keeps its last value at application time)
719
+ */
720
+ attributeList() {
721
+ const { lexer } = this;
722
+ const out = [];
723
+ while (isPunct(lexer.peek(), "[")) {
724
+ lexer.next();
725
+ for (;;) {
726
+ const token = lexer.peek();
727
+ if (isPunct(token, "]")) {
728
+ lexer.next();
729
+ break;
730
+ }
731
+ if (isPunct(token, ";") || isPunct(token, ",")) {
732
+ lexer.next();
733
+ continue;
734
+ }
735
+ const name = this.identifier();
736
+ const eq = lexer.next();
737
+ if (!isPunct(eq, "=")) {
738
+ throw new DotSyntaxError(`expected "=" after attribute name ${JSON.stringify(name.text)}, found ${describeToken(eq)}`, eq.line);
739
+ }
740
+ const value = this.identifier();
741
+ out.push({ name: name.text, value: value.text, line: name.line });
742
+ }
743
+ }
744
+ return out;
745
+ }
746
+ /**
747
+ * Parse a node statement after its id and port: an optional attribute list, then apply.
748
+ * @param scope - the scope
749
+ * @param id - the id token
750
+ * @param port - the port, if any (dropped with a warning)
751
+ */
752
+ nodeStatement(scope, id, port) {
753
+ const attributes = this.attributeList();
754
+ const endpoint = this.mentionEndpoint(scope, id, null);
755
+ if (endpoint === null) {
756
+ return;
757
+ }
758
+ if (port !== null) {
759
+ this.report.warning("unsupported", DOT_ISSUE.NODE_PORT_DROPPED, `port ${JSON.stringify(port)} on the node statement of ${JSON.stringify(id.text)} has no meaning and was dropped`, { line: id.line, element: id.text });
760
+ }
761
+ for (const attribute of attributes) {
762
+ this.setNodeAttribute(endpoint.index, attribute.name, attribute.value, attribute.line);
763
+ }
764
+ }
765
+ /**
766
+ * After a subgraph statement: continue as an edge statement when an edge operator follows.
767
+ * @param scope - the scope
768
+ * @param group - the subgraph's members
769
+ * @param line - the statement's line
770
+ */
771
+ maybeEdgeStatement(scope, group, line) {
772
+ if (isEdgeOp(this.lexer.peek())) {
773
+ this.edgeStatement(scope, group, line);
774
+ }
775
+ }
776
+ /**
777
+ * Parse `edgeRHS [attr_list]` after the first endpoint group and push the edges.
778
+ * @param scope - the scope
779
+ * @param first - the first group (empty when its node could not be created)
780
+ * @param line - the statement's line
781
+ */
782
+ edgeStatement(scope, first, line) {
783
+ const { lexer } = this;
784
+ const groups = [first];
785
+ const kinds = [];
786
+ while (isEdgeOp(lexer.peek())) {
787
+ const op = lexer.next();
788
+ kinds.push(this.edgeKind(op));
789
+ const next = lexer.peek();
790
+ if (isPunct(next, "{") || isKeyword(next, "subgraph")) {
791
+ groups.push(this.subgraph(scope));
792
+ }
793
+ else {
794
+ const id = this.identifier();
795
+ const port = this.port();
796
+ const endpoint = this.mentionEndpoint(scope, id, port);
797
+ groups.push(endpoint === null ? [] : [endpoint]);
798
+ }
799
+ }
800
+ const attributes = this.attributeList();
801
+ for (let k = 0; k < kinds.length; k++) {
802
+ for (const source of groups[k]) {
803
+ for (const target of groups[k + 1]) {
804
+ this.pushEdge(scope, source, target, kinds[k], attributes, line);
805
+ }
806
+ }
807
+ }
808
+ }
809
+ /**
810
+ * The direction of an edge from its operator, checked against the graph keyword.
811
+ * @param op - the operator token
812
+ * @returns the edge kind
813
+ */
814
+ edgeKind(op) {
815
+ const operatorDirected = op.text === "->";
816
+ if (operatorDirected === this.directed) {
817
+ return operatorDirected ? "directed" : "undirected";
818
+ }
819
+ const message = `edge operator "${op.text}" in a ${this.directed ? "digraph" : "graph"}`;
820
+ switch (this.mismatch) {
821
+ case "error":
822
+ throw new DotSyntaxError(`${message} (mismatchedEdgeOperator: "error")`, op.line);
823
+ case "header":
824
+ this.report.warnOnce("coercion", DOT_ISSUE.EDGE_OPERATOR, `${message}; read with the graph's direction (mismatchedEdgeOperator: "header")`, { line: op.line });
825
+ return this.directed ? "directed" : "undirected";
826
+ case "operator":
827
+ this.report.warnOnce("coercion", DOT_ISSUE.EDGE_OPERATOR, `${message}; read with the operator's direction and resolved per onMixedDirection`, { line: op.line });
828
+ return operatorDirected ? "directed" : "undirected";
829
+ default: {
830
+ const name = this.mismatch;
831
+ throw new GraphFormatError("E_UNSUPPORTED", `unknown mismatchedEdgeOperator ${name}`, { found: name });
832
+ }
833
+ }
834
+ }
835
+ /**
836
+ * Coerce an id text, recording a merge under ids: "number".
837
+ * @param text - the id text
838
+ * @param line - the line, for issues
839
+ * @returns the id, or null when the text was rejected (recorded, node skipped)
840
+ */
841
+ coerceId(text, line) {
842
+ try {
843
+ const id = this.ids.text(text);
844
+ const merge = this.ids.lastMerge;
845
+ if (merge !== null) {
846
+ this.report.warning("coercion", DOT_ISSUE.ID_MERGED, `id text ${JSON.stringify(merge.text)} merged with ${JSON.stringify(merge.previousText)} as ${merge.id} under ids: "number"`, { line, element: text });
847
+ }
848
+ return id;
849
+ }
850
+ catch (err) {
851
+ this.report.recordError(err, { line, element: text });
852
+ this.report.counts.skippedNodes++;
853
+ return null;
854
+ }
855
+ }
856
+ /**
857
+ * Mention a node: create it on first mention (applying the scope's node defaults), record it as
858
+ * a member of the scope, and return it as an endpoint.
859
+ * @param scope - the scope
860
+ * @param id - the id token
861
+ * @param port - the endpoint's port, or null
862
+ * @returns the endpoint, or null when the node could not be created (recorded)
863
+ */
864
+ mentionEndpoint(scope, id, port) {
865
+ const nodeId = this.coerceId(id.text, id.line);
866
+ if (nodeId === null) {
867
+ return null;
868
+ }
869
+ const { sink } = this;
870
+ let index = sink.indexOf(nodeId);
871
+ if (index === INVALID_INDEX) {
872
+ try {
873
+ index = sink.addNode(nodeId);
874
+ }
875
+ catch (err) {
876
+ this.report.recordError(err, { line: id.line, element: id.text });
877
+ this.report.counts.skippedNodes++;
878
+ return null;
879
+ }
880
+ this.report.counts.nodes++;
881
+ this.idOf.set(index, nodeId);
882
+ for (const [name, value] of scope.nodeDefaults) {
883
+ this.setNodeAttribute(index, name, value, id.line);
884
+ }
885
+ }
886
+ else if (!this.idOf.has(index)) {
887
+ this.idOf.set(index, nodeId);
888
+ }
889
+ if (!scope.root && !scope.memberSet.has(index)) {
890
+ scope.memberSet.add(index);
891
+ scope.members.push(index);
892
+ }
893
+ return { id: nodeId, index, port };
894
+ }
895
+ /**
896
+ * Push one edge with its effective attributes (scope defaults overridden by the statement's).
897
+ * @param scope - the scope
898
+ * @param source - the source endpoint
899
+ * @param target - the target endpoint
900
+ * @param kind - the edge's direction
901
+ * @param attributes - the statement's attributes
902
+ * @param line - the statement's line
903
+ */
904
+ pushEdge(scope, source, target, kind, attributes, line) {
905
+ const effective = new Map(scope.edgeDefaults);
906
+ for (const a of attributes) {
907
+ effective.set(a.name, a.value);
908
+ }
909
+ const element = `${String(source.id)} ${kind === "directed" ? "->" : "--"} ${String(target.id)}`;
910
+ const where = { line, element };
911
+ const { weightFrom } = this.options;
912
+ let weight;
913
+ const weightText = weightFrom === null ? undefined : effective.get(weightFrom);
914
+ if (weightText !== undefined) {
915
+ try {
916
+ weight = parseWeightText(weightText);
917
+ }
918
+ catch (err) {
919
+ this.report.recordError(err, where);
920
+ this.report.counts.skippedEdges++;
921
+ return;
922
+ }
923
+ }
924
+ const key = effective.get(KEY_ATTRIBUTE);
925
+ const dedupeKey = this.dedupeKey(source.index, target.index, kind, key);
926
+ if (dedupeKey !== null) {
927
+ const existing = (this.strict ? this.strictEdges : this.keyedEdges).get(dedupeKey);
928
+ if (existing !== undefined) {
929
+ this.mergeEdge(existing, weight, effective, where, key);
930
+ this.setPorts(existing, source, target, where);
931
+ return;
932
+ }
933
+ }
934
+ const { sink } = this;
935
+ const before = sink.edgeCount;
936
+ let e;
937
+ try {
938
+ e = this.resolver.addEdge(source.id, target.id, kind, weight, where);
939
+ }
940
+ catch (err) {
941
+ this.report.recordError(err, where);
942
+ this.report.counts.skippedEdges++;
943
+ return;
944
+ }
945
+ this.report.counts.edges += sink.edgeCount - before;
946
+ if (dedupeKey !== null) {
947
+ (this.strict ? this.strictEdges : this.keyedEdges).set(dedupeKey, e);
948
+ }
949
+ for (const [name, value] of effective) {
950
+ if (name !== weightFrom) {
951
+ this.setEdgeAttribute(e, name, value, line, element);
952
+ }
953
+ }
954
+ this.setPorts(e, source, target, where);
955
+ }
956
+ /**
957
+ * Write the endpoints' ports of an edge, when they have any.
958
+ * @param e - the edge index
959
+ * @param source - the source endpoint
960
+ * @param target - the target endpoint
961
+ * @param where - the line and element
962
+ */
963
+ setPorts(e, source, target, where) {
964
+ if (source.port !== null) {
965
+ this.setPort(e, "source", source.port, where);
966
+ }
967
+ if (target.port !== null) {
968
+ this.setPort(e, "target", target.port, where);
969
+ }
970
+ }
971
+ /**
972
+ * The key under which an edge is merged with an earlier one: every (source, target) pair under
973
+ * `strict` (unordered for an undirected edge), else the (source, target, key) triple of an edge
974
+ * carrying a `key` attribute (cgraph's edge identity).
975
+ * @param source - the source index
976
+ * @param target - the target index
977
+ * @param kind - the edge's direction
978
+ * @param key - the `key` attribute, or undefined
979
+ * @returns the dedupe key, or null when the edge is never merged
980
+ */
981
+ dedupeKey(source, target, kind, key) {
982
+ const ordered = kind === "undirected" && target < source ? `${target}>${source}` : `${source}>${target}`;
983
+ if (this.strict) {
984
+ return ordered;
985
+ }
986
+ return key === undefined ? null : `${ordered}#${key}`;
987
+ }
988
+ /**
989
+ * Merge a repeated edge into the earlier one: its attributes overwrite, an explicit weight too.
990
+ * @param e - the existing edge index
991
+ * @param weight - the repeated edge's weight, or undefined
992
+ * @param effective - the repeated edge's effective attributes
993
+ * @param where - the line and element
994
+ * @param key - the `key` attribute when the merge is by key
995
+ */
996
+ mergeEdge(e, weight, effective, where, key) {
997
+ const byKey = !this.strict && key !== undefined;
998
+ this.report.warning("merged", byKey ? DOT_ISSUE.KEY_MERGED : DOT_ISSUE.STRICT_MERGED, byKey
999
+ ? `edge ${where.element} with key ${JSON.stringify(key)} repeats an earlier edge; attributes merged`
1000
+ : `parallel edge ${where.element} merged into the earlier one (strict graph)`, where);
1001
+ try {
1002
+ if (weight !== undefined) {
1003
+ this.sink.setEdgeWeight(e, weight);
1004
+ }
1005
+ }
1006
+ catch (err) {
1007
+ this.report.recordError(err, where);
1008
+ }
1009
+ for (const [name, value] of effective) {
1010
+ if (name !== this.options.weightFrom) {
1011
+ this.setEdgeAttribute(e, name, value, where.line, where.element);
1012
+ }
1013
+ }
1014
+ }
1015
+ /**
1016
+ * Write one node attribute: `label` to the label column, `pos` to the position column,
1017
+ * anything else as an inferred cell.
1018
+ * @param index - the node index
1019
+ * @param name - the attribute name
1020
+ * @param value - the value text
1021
+ * @param line - the line, for issues
1022
+ */
1023
+ setNodeAttribute(index, name, value, line) {
1024
+ const element = String(this.idOf.get(index) ?? index);
1025
+ try {
1026
+ if (name === LABEL_ATTRIBUTE) {
1027
+ this.sink.setNodeValue(this.nodeLabel(), index, value);
1028
+ }
1029
+ else if (name === POS_ATTRIBUTE) {
1030
+ this.setPosition(index, value, line, element);
1031
+ }
1032
+ else {
1033
+ this.textWriter("node", name).write(index, value);
1034
+ }
1035
+ }
1036
+ catch (err) {
1037
+ this.report.recordError(err, { line, element });
1038
+ }
1039
+ }
1040
+ /**
1041
+ * Write one edge attribute: `label` to the label column, `key` to the edge id column, anything
1042
+ * else as an inferred cell.
1043
+ * @param e - the edge index
1044
+ * @param name - the attribute name
1045
+ * @param value - the value text
1046
+ * @param line - the line, for issues
1047
+ * @param element - the edge description, for issues
1048
+ */
1049
+ setEdgeAttribute(e, name, value, line, element) {
1050
+ try {
1051
+ if (name === LABEL_ATTRIBUTE) {
1052
+ this.setEdgeText(this.edgeLabel(), e, value);
1053
+ }
1054
+ else if (name === KEY_ATTRIBUTE) {
1055
+ this.setEdgeText(this.keyColumn(), e, value);
1056
+ }
1057
+ else {
1058
+ this.textWriter("edge", name).write(e, value);
1059
+ }
1060
+ }
1061
+ catch (err) {
1062
+ this.report.recordError(err, { line, element });
1063
+ }
1064
+ }
1065
+ /**
1066
+ * Write a graph attribute (the root's `ID = ID` and `graph [..]`).
1067
+ * @param attribute - the attribute
1068
+ */
1069
+ setGraphAttribute(attribute) {
1070
+ try {
1071
+ if (attribute.name === LABEL_ATTRIBUTE) {
1072
+ this.sink.setGraphValue(attribute.name, attribute.value, { dtype: "string", origin: DOT_ORIGIN });
1073
+ }
1074
+ else {
1075
+ this.sink.setGraphValue(attribute.name, parseTextCell(attribute.value), { origin: DOT_ORIGIN });
1076
+ }
1077
+ }
1078
+ catch (err) {
1079
+ this.report.recordError(err, { line: attribute.line, element: attribute.name });
1080
+ }
1081
+ }
1082
+ /**
1083
+ * Write a node's `pos`: `x,y[,z][!]` into the position column, the `!` as `pin` = true.
1084
+ * @param index - the node index
1085
+ * @param text - the pos text
1086
+ * @param line - the line
1087
+ * @param element - the node id text
1088
+ */
1089
+ setPosition(index, text, line, element) {
1090
+ const match = POINT_TEXT.exec(text);
1091
+ if (match === null) {
1092
+ this.report.warning("validation-error", DOT_ISSUE.BAD_POS, `pos ${JSON.stringify(text)} is not a point "x,y[,z][!]"; dropped`, { line, element });
1093
+ return;
1094
+ }
1095
+ const x = Number(match[1]);
1096
+ const y = Number(match[2]);
1097
+ const z = match[3] === undefined ? 0 : Number(match[3]);
1098
+ const dims = match[3] === undefined ? 2 : 3;
1099
+ this.sink.setNodeValue(this.positionColumn(dims), index, [x, y, z]);
1100
+ if (match[4] === "!") {
1101
+ this.sink.setNodeValue(PIN_ATTRIBUTE, index, true);
1102
+ }
1103
+ }
1104
+ /**
1105
+ * Write an endpoint's port into the source / target port column.
1106
+ * @param e - the edge index
1107
+ * @param side - which endpoint
1108
+ * @param port - the port text
1109
+ * @param where - the line and element
1110
+ */
1111
+ setPort(e, side, port, where) {
1112
+ try {
1113
+ this.setEdgeText(side === "source" ? this.sourcePortColumn() : this.targetPortColumn(), e, port);
1114
+ }
1115
+ catch (err) {
1116
+ this.report.recordError(err, where);
1117
+ }
1118
+ }
1119
+ /**
1120
+ * Write a text cell into one of the importer's edge text columns.
1121
+ * @param handle - the column
1122
+ * @param e - the edge index
1123
+ * @param text - the text
1124
+ */
1125
+ setEdgeText(handle, e, text) {
1126
+ this.sink.setEdgeValue(handle, e, text);
1127
+ }
1128
+ // ============================================================ lazily declared columns
1129
+ /**
1130
+ * The node label column (string, role label).
1131
+ * @returns the handle
1132
+ */
1133
+ nodeLabel() {
1134
+ if (this.nodeLabelHandle === INVALID_INDEX) {
1135
+ this.nodeLabelHandle = this.declare("node", {
1136
+ name: LABEL_ATTRIBUTE,
1137
+ dtype: "string",
1138
+ nullable: true,
1139
+ role: "label",
1140
+ origin: DOT_ORIGIN,
1141
+ });
1142
+ }
1143
+ return this.nodeLabelHandle;
1144
+ }
1145
+ /**
1146
+ * The edge label column (string, role label).
1147
+ * @returns the handle
1148
+ */
1149
+ edgeLabel() {
1150
+ if (this.edgeLabelHandle === INVALID_INDEX) {
1151
+ this.edgeLabelHandle = this.declare("edge", {
1152
+ name: LABEL_ATTRIBUTE,
1153
+ dtype: "string",
1154
+ nullable: true,
1155
+ role: "label",
1156
+ origin: DOT_ORIGIN,
1157
+ });
1158
+ }
1159
+ return this.edgeLabelHandle;
1160
+ }
1161
+ /**
1162
+ * The position column (f32 x3, role position, design section 5.2), declared on the first `pos`.
1163
+ * @param dims - the dimensions of the first value, recorded in extra.sourceDims
1164
+ * @returns the handle
1165
+ */
1166
+ positionColumn(dims) {
1167
+ if (this.positionHandle === INVALID_INDEX) {
1168
+ this.positionHandle = this.declare("node", {
1169
+ name: POS_ATTRIBUTE,
1170
+ dtype: "f32",
1171
+ components: 3,
1172
+ nullable: true,
1173
+ mutable: true,
1174
+ role: "position",
1175
+ origin: { ...DOT_ORIGIN, type: "point" },
1176
+ extra: { sourceDims: dims, units: "file" },
1177
+ });
1178
+ }
1179
+ return this.positionHandle;
1180
+ }
1181
+ /**
1182
+ * The cluster marker column (bool).
1183
+ * @returns the handle
1184
+ */
1185
+ clusterColumn() {
1186
+ if (this.clusterHandle === INVALID_INDEX) {
1187
+ this.clusterHandle = this.declare("node", {
1188
+ name: CLUSTER_COLUMN,
1189
+ dtype: "bool",
1190
+ nullable: true,
1191
+ origin: DOT_ORIGIN,
1192
+ });
1193
+ }
1194
+ return this.clusterHandle;
1195
+ }
1196
+ /**
1197
+ * The parent column (u32, role parent, refersTo node).
1198
+ * @returns the handle
1199
+ */
1200
+ parentColumn() {
1201
+ if (this.parentHandle === INVALID_INDEX) {
1202
+ this.parentHandle = this.declare("node", {
1203
+ name: PARENT_COLUMN,
1204
+ dtype: "u32",
1205
+ nullable: true,
1206
+ role: "parent",
1207
+ refersTo: "node",
1208
+ origin: DOT_ORIGIN,
1209
+ });
1210
+ }
1211
+ return this.parentHandle;
1212
+ }
1213
+ /**
1214
+ * The edge key column (string, role id): cgraph's edge identity within a source / target pair.
1215
+ * @returns the handle
1216
+ */
1217
+ keyColumn() {
1218
+ if (this.keyHandle === INVALID_INDEX) {
1219
+ this.keyHandle = this.declare("edge", {
1220
+ name: KEY_ATTRIBUTE,
1221
+ dtype: "string",
1222
+ nullable: true,
1223
+ role: "id",
1224
+ origin: { ...DOT_ORIGIN, id: KEY_ATTRIBUTE },
1225
+ });
1226
+ }
1227
+ return this.keyHandle;
1228
+ }
1229
+ /**
1230
+ * The source port column (string, role sourcePort).
1231
+ * @returns the handle
1232
+ */
1233
+ sourcePortColumn() {
1234
+ if (this.sourcePortHandle === INVALID_INDEX) {
1235
+ this.sourcePortHandle = this.declare("edge", {
1236
+ name: SOURCE_PORT_COLUMN,
1237
+ dtype: "string",
1238
+ nullable: true,
1239
+ role: "sourcePort",
1240
+ origin: DOT_ORIGIN,
1241
+ });
1242
+ }
1243
+ return this.sourcePortHandle;
1244
+ }
1245
+ /**
1246
+ * The target port column (string, role targetPort).
1247
+ * @returns the handle
1248
+ */
1249
+ targetPortColumn() {
1250
+ if (this.targetPortHandle === INVALID_INDEX) {
1251
+ this.targetPortHandle = this.declare("edge", {
1252
+ name: TARGET_PORT_COLUMN,
1253
+ dtype: "string",
1254
+ nullable: true,
1255
+ role: "targetPort",
1256
+ origin: DOT_ORIGIN,
1257
+ });
1258
+ }
1259
+ return this.targetPortHandle;
1260
+ }
1261
+ /**
1262
+ * The inferred-column writer of an attribute name in a domain (design section 5.1: the column's
1263
+ * dtype follows the text grammar per column; a caller's column of the name receives parsed
1264
+ * values through the sink's own inference).
1265
+ * @param domain - node or edge
1266
+ * @param name - the attribute name
1267
+ * @returns the writer
1268
+ */
1269
+ textWriter(domain, name) {
1270
+ const writers = domain === "node" ? this.nodeWriters : this.edgeWriters;
1271
+ let writer = writers.get(name);
1272
+ if (writer === undefined) {
1273
+ writer = new TextCellWriter(name, domain, this.sink, this.report);
1274
+ writers.set(name, writer);
1275
+ }
1276
+ return writer;
1277
+ }
1278
+ /**
1279
+ * Declare one of the importer's columns on the sink through the shared design section 5.6
1280
+ * rule: a caller's sink that already holds the role gets the column without it (reported), one
1281
+ * that holds the name with another shape gets it renamed `<name>#<id>` (reported).
1282
+ * @param domain - node or edge
1283
+ * @param decl - the declaration
1284
+ * @returns the handle
1285
+ */
1286
+ declare(domain, decl) {
1287
+ const withId = decl.origin?.id === undefined ? { ...decl, origin: { ...decl.origin, id: decl.name } } : decl;
1288
+ return declareResolved(this.sink, domain, withId, this.report, { element: decl.name }).handle;
1289
+ }
1290
+ }
1291
+ //# sourceMappingURL=importer.js.map