@chikx/compiler 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 (398) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +40 -2
  3. package/dist/emitter/index.d.ts +96 -0
  4. package/dist/emitter/index.d.ts.map +1 -0
  5. package/dist/emitter/index.js +415 -0
  6. package/dist/emitter/index.js.map +1 -0
  7. package/dist/formatter/index.d.ts +53 -0
  8. package/dist/formatter/index.d.ts.map +1 -0
  9. package/dist/formatter/index.js +464 -0
  10. package/dist/formatter/index.js.map +1 -0
  11. package/dist/host/node.d.ts +34 -0
  12. package/dist/host/node.d.ts.map +1 -0
  13. package/dist/host/node.js +80 -0
  14. package/dist/host/node.js.map +1 -0
  15. package/dist/index.d.ts +93 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +792 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/ir/factories.d.ts +35 -0
  20. package/dist/ir/factories.d.ts.map +1 -0
  21. package/dist/ir/factories.js +47 -0
  22. package/dist/ir/factories.js.map +1 -0
  23. package/dist/ir/index.d.ts +13 -0
  24. package/dist/ir/index.d.ts.map +1 -0
  25. package/dist/ir/index.js +2 -0
  26. package/dist/ir/index.js.map +1 -0
  27. package/dist/ir/types.d.ts +139 -0
  28. package/dist/ir/types.d.ts.map +1 -0
  29. package/dist/ir/types.js +2 -0
  30. package/dist/ir/types.js.map +1 -0
  31. package/dist/lexer/at-keywords.d.ts +27 -0
  32. package/dist/lexer/at-keywords.d.ts.map +1 -0
  33. package/dist/lexer/at-keywords.js +35 -0
  34. package/dist/lexer/at-keywords.js.map +1 -0
  35. package/dist/lexer/characters.d.ts +38 -0
  36. package/dist/lexer/characters.d.ts.map +1 -0
  37. package/dist/lexer/characters.js +74 -0
  38. package/dist/lexer/characters.js.map +1 -0
  39. package/dist/lexer/comments.d.ts +22 -0
  40. package/dist/lexer/comments.d.ts.map +1 -0
  41. package/dist/lexer/comments.js +20 -0
  42. package/dist/lexer/comments.js.map +1 -0
  43. package/dist/lexer/diagnostics.d.ts +24 -0
  44. package/dist/lexer/diagnostics.d.ts.map +1 -0
  45. package/dist/lexer/diagnostics.js +29 -0
  46. package/dist/lexer/diagnostics.js.map +1 -0
  47. package/dist/lexer/dollar-refs.d.ts +13 -0
  48. package/dist/lexer/dollar-refs.d.ts.map +1 -0
  49. package/dist/lexer/dollar-refs.js +22 -0
  50. package/dist/lexer/dollar-refs.js.map +1 -0
  51. package/dist/lexer/index.d.ts +32 -0
  52. package/dist/lexer/index.d.ts.map +1 -0
  53. package/dist/lexer/index.js +26 -0
  54. package/dist/lexer/index.js.map +1 -0
  55. package/dist/lexer/names.d.ts +23 -0
  56. package/dist/lexer/names.d.ts.map +1 -0
  57. package/dist/lexer/names.js +48 -0
  58. package/dist/lexer/names.js.map +1 -0
  59. package/dist/lexer/numbers.d.ts +22 -0
  60. package/dist/lexer/numbers.d.ts.map +1 -0
  61. package/dist/lexer/numbers.js +60 -0
  62. package/dist/lexer/numbers.js.map +1 -0
  63. package/dist/lexer/scanner.d.ts +30 -0
  64. package/dist/lexer/scanner.d.ts.map +1 -0
  65. package/dist/lexer/scanner.js +158 -0
  66. package/dist/lexer/scanner.js.map +1 -0
  67. package/dist/lexer/strings.d.ts +25 -0
  68. package/dist/lexer/strings.d.ts.map +1 -0
  69. package/dist/lexer/strings.js +41 -0
  70. package/dist/lexer/strings.js.map +1 -0
  71. package/dist/lexer/urls.d.ts +28 -0
  72. package/dist/lexer/urls.d.ts.map +1 -0
  73. package/dist/lexer/urls.js +54 -0
  74. package/dist/lexer/urls.js.map +1 -0
  75. package/dist/lowering/expand-groups.d.ts +61 -0
  76. package/dist/lowering/expand-groups.d.ts.map +1 -0
  77. package/dist/lowering/expand-groups.js +117 -0
  78. package/dist/lowering/expand-groups.js.map +1 -0
  79. package/dist/lowering/group.d.ts +30 -0
  80. package/dist/lowering/group.d.ts.map +1 -0
  81. package/dist/lowering/group.js +109 -0
  82. package/dist/lowering/group.js.map +1 -0
  83. package/dist/lowering/index.d.ts +26 -0
  84. package/dist/lowering/index.d.ts.map +1 -0
  85. package/dist/lowering/index.js +23 -0
  86. package/dist/lowering/index.js.map +1 -0
  87. package/dist/lowering/lower.d.ts +21 -0
  88. package/dist/lowering/lower.d.ts.map +1 -0
  89. package/dist/lowering/lower.js +296 -0
  90. package/dist/lowering/lower.js.map +1 -0
  91. package/dist/lowering/media.d.ts +63 -0
  92. package/dist/lowering/media.d.ts.map +1 -0
  93. package/dist/lowering/media.js +237 -0
  94. package/dist/lowering/media.js.map +1 -0
  95. package/dist/lowering/merge.d.ts +35 -0
  96. package/dist/lowering/merge.d.ts.map +1 -0
  97. package/dist/lowering/merge.js +146 -0
  98. package/dist/lowering/merge.js.map +1 -0
  99. package/dist/lowering/property-families.d.ts +50 -0
  100. package/dist/lowering/property-families.d.ts.map +1 -0
  101. package/dist/lowering/property-families.js +175 -0
  102. package/dist/lowering/property-families.js.map +1 -0
  103. package/dist/lowering/spans.d.ts +36 -0
  104. package/dist/lowering/spans.d.ts.map +1 -0
  105. package/dist/lowering/spans.js +100 -0
  106. package/dist/lowering/spans.js.map +1 -0
  107. package/dist/naming/index.d.ts +37 -0
  108. package/dist/naming/index.d.ts.map +1 -0
  109. package/dist/naming/index.js +34 -0
  110. package/dist/naming/index.js.map +1 -0
  111. package/dist/naming/mapgen.d.ts +70 -0
  112. package/dist/naming/mapgen.d.ts.map +1 -0
  113. package/dist/naming/mapgen.js +153 -0
  114. package/dist/naming/mapgen.js.map +1 -0
  115. package/dist/naming/merge.d.ts +62 -0
  116. package/dist/naming/merge.d.ts.map +1 -0
  117. package/dist/naming/merge.js +77 -0
  118. package/dist/naming/merge.js.map +1 -0
  119. package/dist/naming/rewrite.d.ts +14 -0
  120. package/dist/naming/rewrite.d.ts.map +1 -0
  121. package/dist/naming/rewrite.js +191 -0
  122. package/dist/naming/rewrite.js.map +1 -0
  123. package/dist/naming/scan.d.ts +80 -0
  124. package/dist/naming/scan.d.ts.map +1 -0
  125. package/dist/naming/scan.js +193 -0
  126. package/dist/naming/scan.js.map +1 -0
  127. package/dist/naming/table.d.ts +68 -0
  128. package/dist/naming/table.d.ts.map +1 -0
  129. package/dist/naming/table.js +132 -0
  130. package/dist/naming/table.js.map +1 -0
  131. package/dist/naming/types.d.ts +94 -0
  132. package/dist/naming/types.d.ts.map +1 -0
  133. package/dist/naming/types.js +2 -0
  134. package/dist/naming/types.js.map +1 -0
  135. package/dist/output/import-path.d.ts +24 -0
  136. package/dist/output/import-path.d.ts.map +1 -0
  137. package/dist/output/import-path.js +30 -0
  138. package/dist/output/import-path.js.map +1 -0
  139. package/dist/output/index.d.ts +40 -0
  140. package/dist/output/index.d.ts.map +1 -0
  141. package/dist/output/index.js +138 -0
  142. package/dist/output/index.js.map +1 -0
  143. package/dist/output/manifest.d.ts +17 -0
  144. package/dist/output/manifest.d.ts.map +1 -0
  145. package/dist/output/manifest.js +22 -0
  146. package/dist/output/manifest.js.map +1 -0
  147. package/dist/output/paths.d.ts +54 -0
  148. package/dist/output/paths.d.ts.map +1 -0
  149. package/dist/output/paths.js +183 -0
  150. package/dist/output/paths.js.map +1 -0
  151. package/dist/output/resolve.d.ts +100 -0
  152. package/dist/output/resolve.d.ts.map +1 -0
  153. package/dist/output/resolve.js +313 -0
  154. package/dist/output/resolve.js.map +1 -0
  155. package/dist/output/validate.d.ts +82 -0
  156. package/dist/output/validate.d.ts.map +1 -0
  157. package/dist/output/validate.js +128 -0
  158. package/dist/output/validate.js.map +1 -0
  159. package/dist/parser/cursor.d.ts +55 -0
  160. package/dist/parser/cursor.d.ts.map +1 -0
  161. package/dist/parser/cursor.js +120 -0
  162. package/dist/parser/cursor.js.map +1 -0
  163. package/dist/parser/declaration.d.ts +21 -0
  164. package/dist/parser/declaration.d.ts.map +1 -0
  165. package/dist/parser/declaration.js +201 -0
  166. package/dist/parser/declaration.js.map +1 -0
  167. package/dist/parser/diagnostics.d.ts +52 -0
  168. package/dist/parser/diagnostics.d.ts.map +1 -0
  169. package/dist/parser/diagnostics.js +55 -0
  170. package/dist/parser/diagnostics.js.map +1 -0
  171. package/dist/parser/group.d.ts +26 -0
  172. package/dist/parser/group.d.ts.map +1 -0
  173. package/dist/parser/group.js +219 -0
  174. package/dist/parser/group.js.map +1 -0
  175. package/dist/parser/index.d.ts +30 -0
  176. package/dist/parser/index.d.ts.map +1 -0
  177. package/dist/parser/index.js +48 -0
  178. package/dist/parser/index.js.map +1 -0
  179. package/dist/parser/modifier.d.ts +25 -0
  180. package/dist/parser/modifier.d.ts.map +1 -0
  181. package/dist/parser/modifier.js +41 -0
  182. package/dist/parser/modifier.js.map +1 -0
  183. package/dist/parser/recovery.d.ts +23 -0
  184. package/dist/parser/recovery.d.ts.map +1 -0
  185. package/dist/parser/recovery.js +80 -0
  186. package/dist/parser/recovery.js.map +1 -0
  187. package/dist/parser/rule.d.ts +39 -0
  188. package/dist/parser/rule.d.ts.map +1 -0
  189. package/dist/parser/rule.js +337 -0
  190. package/dist/parser/rule.js.map +1 -0
  191. package/dist/parser/statement.d.ts +21 -0
  192. package/dist/parser/statement.d.ts.map +1 -0
  193. package/dist/parser/statement.js +48 -0
  194. package/dist/parser/statement.js.map +1 -0
  195. package/dist/parser/statements.d.ts +33 -0
  196. package/dist/parser/statements.d.ts.map +1 -0
  197. package/dist/parser/statements.js +605 -0
  198. package/dist/parser/statements.js.map +1 -0
  199. package/dist/parser/value.d.ts +57 -0
  200. package/dist/parser/value.d.ts.map +1 -0
  201. package/dist/parser/value.js +226 -0
  202. package/dist/parser/value.js.map +1 -0
  203. package/dist/resolver/graph.d.ts +20 -0
  204. package/dist/resolver/graph.d.ts.map +1 -0
  205. package/dist/resolver/graph.js +382 -0
  206. package/dist/resolver/graph.js.map +1 -0
  207. package/dist/resolver/groups.d.ts +70 -0
  208. package/dist/resolver/groups.d.ts.map +1 -0
  209. package/dist/resolver/groups.js +143 -0
  210. package/dist/resolver/groups.js.map +1 -0
  211. package/dist/resolver/index.d.ts +46 -0
  212. package/dist/resolver/index.d.ts.map +1 -0
  213. package/dist/resolver/index.js +46 -0
  214. package/dist/resolver/index.js.map +1 -0
  215. package/dist/resolver/keyframes.d.ts +56 -0
  216. package/dist/resolver/keyframes.d.ts.map +1 -0
  217. package/dist/resolver/keyframes.js +102 -0
  218. package/dist/resolver/keyframes.js.map +1 -0
  219. package/dist/resolver/paths.d.ts +37 -0
  220. package/dist/resolver/paths.d.ts.map +1 -0
  221. package/dist/resolver/paths.js +135 -0
  222. package/dist/resolver/paths.js.map +1 -0
  223. package/dist/resolver/rules.d.ts +34 -0
  224. package/dist/resolver/rules.d.ts.map +1 -0
  225. package/dist/resolver/rules.js +52 -0
  226. package/dist/resolver/rules.js.map +1 -0
  227. package/dist/resolver/scopes.d.ts +44 -0
  228. package/dist/resolver/scopes.d.ts.map +1 -0
  229. package/dist/resolver/scopes.js +213 -0
  230. package/dist/resolver/scopes.js.map +1 -0
  231. package/dist/resolver/types.d.ts +238 -0
  232. package/dist/resolver/types.d.ts.map +1 -0
  233. package/dist/resolver/types.js +11 -0
  234. package/dist/resolver/types.js.map +1 -0
  235. package/dist/selectors/flatten.d.ts +95 -0
  236. package/dist/selectors/flatten.d.ts.map +1 -0
  237. package/dist/selectors/flatten.js +384 -0
  238. package/dist/selectors/flatten.js.map +1 -0
  239. package/dist/selectors/index.d.ts +15 -0
  240. package/dist/selectors/index.d.ts.map +1 -0
  241. package/dist/selectors/index.js +14 -0
  242. package/dist/selectors/index.js.map +1 -0
  243. package/dist/selectors/scan.d.ts +54 -0
  244. package/dist/selectors/scan.d.ts.map +1 -0
  245. package/dist/selectors/scan.js +191 -0
  246. package/dist/selectors/scan.js.map +1 -0
  247. package/dist/selectors/specificity.d.ts +28 -0
  248. package/dist/selectors/specificity.d.ts.map +1 -0
  249. package/dist/selectors/specificity.js +297 -0
  250. package/dist/selectors/specificity.js.map +1 -0
  251. package/dist/setup-list.d.ts +41 -0
  252. package/dist/setup-list.d.ts.map +1 -0
  253. package/dist/setup-list.js +122 -0
  254. package/dist/setup-list.js.map +1 -0
  255. package/dist/sourcemaps/index.d.ts +14 -0
  256. package/dist/sourcemaps/index.d.ts.map +1 -0
  257. package/dist/sourcemaps/index.js +13 -0
  258. package/dist/sourcemaps/index.js.map +1 -0
  259. package/dist/sourcemaps/mappings.d.ts +77 -0
  260. package/dist/sourcemaps/mappings.d.ts.map +1 -0
  261. package/dist/sourcemaps/mappings.js +124 -0
  262. package/dist/sourcemaps/mappings.js.map +1 -0
  263. package/dist/sourcemaps/vlq.d.ts +12 -0
  264. package/dist/sourcemaps/vlq.d.ts.map +1 -0
  265. package/dist/sourcemaps/vlq.js +29 -0
  266. package/dist/sourcemaps/vlq.js.map +1 -0
  267. package/dist/types/ast.d.ts +291 -0
  268. package/dist/types/ast.d.ts.map +1 -0
  269. package/dist/types/ast.js +2 -0
  270. package/dist/types/ast.js.map +1 -0
  271. package/dist/types/diagnostics.d.ts +55 -0
  272. package/dist/types/diagnostics.d.ts.map +1 -0
  273. package/dist/types/diagnostics.js +61 -0
  274. package/dist/types/diagnostics.js.map +1 -0
  275. package/dist/types/options.d.ts +274 -0
  276. package/dist/types/options.d.ts.map +1 -0
  277. package/dist/types/options.js +11 -0
  278. package/dist/types/options.js.map +1 -0
  279. package/dist/types/result.d.ts +112 -0
  280. package/dist/types/result.d.ts.map +1 -0
  281. package/dist/types/result.js +2 -0
  282. package/dist/types/result.js.map +1 -0
  283. package/dist/types/span.d.ts +19 -0
  284. package/dist/types/span.d.ts.map +1 -0
  285. package/dist/types/span.js +2 -0
  286. package/dist/types/span.js.map +1 -0
  287. package/dist/types/token.d.ts +68 -0
  288. package/dist/types/token.d.ts.map +1 -0
  289. package/dist/types/token.js +2 -0
  290. package/dist/types/token.js.map +1 -0
  291. package/dist/variables/ast.d.ts +56 -0
  292. package/dist/variables/ast.d.ts.map +1 -0
  293. package/dist/variables/ast.js +167 -0
  294. package/dist/variables/ast.js.map +1 -0
  295. package/dist/variables/expand.d.ts +109 -0
  296. package/dist/variables/expand.d.ts.map +1 -0
  297. package/dist/variables/expand.js +213 -0
  298. package/dist/variables/expand.js.map +1 -0
  299. package/dist/variables/index.d.ts +36 -0
  300. package/dist/variables/index.d.ts.map +1 -0
  301. package/dist/variables/index.js +33 -0
  302. package/dist/variables/index.js.map +1 -0
  303. package/dist/variables/naming.d.ts +39 -0
  304. package/dist/variables/naming.d.ts.map +1 -0
  305. package/dist/variables/naming.js +14 -0
  306. package/dist/variables/naming.js.map +1 -0
  307. package/dist/variables/runtime.d.ts +81 -0
  308. package/dist/variables/runtime.d.ts.map +1 -0
  309. package/dist/variables/runtime.js +228 -0
  310. package/dist/variables/runtime.js.map +1 -0
  311. package/dist/variables/substitute.d.ts +65 -0
  312. package/dist/variables/substitute.d.ts.map +1 -0
  313. package/dist/variables/substitute.js +73 -0
  314. package/dist/variables/substitute.js.map +1 -0
  315. package/dist/variables/walk.d.ts +37 -0
  316. package/dist/variables/walk.d.ts.map +1 -0
  317. package/dist/variables/walk.js +113 -0
  318. package/dist/variables/walk.js.map +1 -0
  319. package/package.json +35 -4
  320. package/src/emitter/index.ts +658 -0
  321. package/src/formatter/index.ts +544 -0
  322. package/src/host/node.ts +85 -0
  323. package/src/index.ts +1021 -0
  324. package/src/ir/factories.ts +85 -0
  325. package/src/ir/index.ts +28 -0
  326. package/src/ir/types.ts +145 -0
  327. package/src/lexer/at-keywords.ts +35 -0
  328. package/src/lexer/characters.ts +82 -0
  329. package/src/lexer/comments.ts +27 -0
  330. package/src/lexer/diagnostics.ts +42 -0
  331. package/src/lexer/dollar-refs.ts +23 -0
  332. package/src/lexer/index.ts +37 -0
  333. package/src/lexer/names.ts +49 -0
  334. package/src/lexer/numbers.ts +59 -0
  335. package/src/lexer/scanner.ts +199 -0
  336. package/src/lexer/strings.ts +52 -0
  337. package/src/lexer/urls.ts +63 -0
  338. package/src/lowering/expand-groups.ts +206 -0
  339. package/src/lowering/group.ts +119 -0
  340. package/src/lowering/index.ts +43 -0
  341. package/src/lowering/lower.ts +441 -0
  342. package/src/lowering/media.ts +314 -0
  343. package/src/lowering/merge.ts +163 -0
  344. package/src/lowering/property-families.ts +194 -0
  345. package/src/lowering/spans.ts +118 -0
  346. package/src/naming/index.ts +44 -0
  347. package/src/naming/mapgen.ts +252 -0
  348. package/src/naming/merge.ts +114 -0
  349. package/src/naming/rewrite.ts +333 -0
  350. package/src/naming/scan.ts +267 -0
  351. package/src/naming/table.ts +153 -0
  352. package/src/naming/types.ts +100 -0
  353. package/src/output/import-path.ts +30 -0
  354. package/src/output/index.ts +213 -0
  355. package/src/output/manifest.ts +28 -0
  356. package/src/output/paths.ts +200 -0
  357. package/src/output/resolve.ts +401 -0
  358. package/src/output/validate.ts +251 -0
  359. package/src/parser/cursor.ts +138 -0
  360. package/src/parser/declaration.ts +247 -0
  361. package/src/parser/diagnostics.ts +75 -0
  362. package/src/parser/group.ts +303 -0
  363. package/src/parser/index.ts +50 -0
  364. package/src/parser/modifier.ts +60 -0
  365. package/src/parser/recovery.ts +88 -0
  366. package/src/parser/rule.ts +370 -0
  367. package/src/parser/statement.ts +47 -0
  368. package/src/parser/statements.ts +667 -0
  369. package/src/parser/value.ts +265 -0
  370. package/src/resolver/graph.ts +467 -0
  371. package/src/resolver/groups.ts +177 -0
  372. package/src/resolver/index.ts +68 -0
  373. package/src/resolver/keyframes.ts +131 -0
  374. package/src/resolver/paths.ts +142 -0
  375. package/src/resolver/rules.ts +54 -0
  376. package/src/resolver/scopes.ts +342 -0
  377. package/src/resolver/types.ts +259 -0
  378. package/src/selectors/flatten.ts +448 -0
  379. package/src/selectors/index.ts +14 -0
  380. package/src/selectors/scan.ts +227 -0
  381. package/src/selectors/specificity.ts +319 -0
  382. package/src/setup-list.ts +172 -0
  383. package/src/sourcemaps/index.ts +25 -0
  384. package/src/sourcemaps/mappings.ts +190 -0
  385. package/src/sourcemaps/vlq.ts +29 -0
  386. package/src/types/ast.ts +318 -0
  387. package/src/types/diagnostics.ts +124 -0
  388. package/src/types/options.ts +286 -0
  389. package/src/types/result.ts +121 -0
  390. package/src/types/span.ts +18 -0
  391. package/src/types/token.ts +77 -0
  392. package/src/variables/ast.ts +182 -0
  393. package/src/variables/expand.ts +319 -0
  394. package/src/variables/index.ts +38 -0
  395. package/src/variables/naming.ts +46 -0
  396. package/src/variables/runtime.ts +336 -0
  397. package/src/variables/substitute.ts +148 -0
  398. package/src/variables/walk.ts +147 -0
@@ -0,0 +1,286 @@
1
+ /**
2
+ * Options and host contracts for parsing, compiling and planning outputs.
3
+ *
4
+ * All paths crossing this API are workspace-relative or absolute POSIX paths
5
+ * with forward slashes; hosts convert platform separators once at their
6
+ * boundary. The compiler core never reads the filesystem: project operations
7
+ * go through an injected {@link ModuleHost}, and single-source operations are
8
+ * pure functions.
9
+ */
10
+
11
+ /** Options for `parse`. */
12
+ export interface ParseOptions {
13
+ /**
14
+ * Workspace-relative POSIX path recorded as `Span.file` on every node and
15
+ * diagnostic produced for this source. The source text itself is supplied
16
+ * directly to `parse` and is never read from disk.
17
+ */
18
+ readonly file: string;
19
+ }
20
+
21
+ /** Class naming strategies available in v0.1 (ADR-0010). */
22
+ export type NamingStrategy = "none" | "prefix";
23
+
24
+ /** Class naming configuration. */
25
+ export interface NamingOptions {
26
+ /** `none` keeps authored class names; `prefix` prepends the effective prefix. */
27
+ readonly strategy: NamingStrategy;
28
+ /**
29
+ * Prefix used by the `prefix` strategy, for example `ui` producing `ui-card`.
30
+ * An empty string leaves names unchanged. Omitted means the file-level
31
+ * `prefix` option or setup key applies.
32
+ */
33
+ readonly prefix?: string;
34
+ }
35
+
36
+ /**
37
+ * Media grouping modes accepted by v0.1.
38
+ *
39
+ * `safe` (the public default) merges only adjacent compatible rules and media
40
+ * contexts without changing declaration order, specificity, layer or scope;
41
+ * `none` emits rules as authored and performs no merging. `merge` is an opt-in
42
+ * optimization (ADR-0024): it keeps the safe adjacent pass and additionally
43
+ * moves same-selector/same-condition conditional runs across declarations whose
44
+ * property families are provably disjoint (`all` and `var()` values are
45
+ * barriers). It never merges unconditional runs, different selectors,
46
+ * different conditions or different native contexts. Future modes such as
47
+ * ordered-media grouping remain deliberately absent and must
48
+ * be rejected as invalid configuration values.
49
+ */
50
+ export type MediaGroupingMode = "safe" | "none" | "merge";
51
+
52
+ /**
53
+ * Output settings shared by file-level setup, project configuration and CLI
54
+ * overrides (SPECIFICATION.md §9).
55
+ *
56
+ * Defaults: `cssDir`/`mapDir` = the source directory; `cssName` =
57
+ * `<stem>.css`; `mapName` = `<stem>.map.js`; `emitCss` and `emitMap` = `true`;
58
+ * `mapTypes` = `false`; `sourceMap` = `false`; `minify` = `true`;
59
+ * `importCss` = `false`. `minify` defaults on (ADR-0022) and compacts generated
60
+ * structural whitespace while preserving authored CSS tokens and declaration
61
+ * order; `minify: false` / `--no-minify` opts out.
62
+ * `mapTypes` is opt-in only: the JS class map is the normal artifact and the
63
+ * `.d.ts` beside it is generated only when requested (CSSX-A52). `importCss`
64
+ * is opt-in only: the map carries no CSS import unless a bundler-driven
65
+ * consumer asks for one (ADR-0021).
66
+ */
67
+ export interface OutputOptions {
68
+ /**
69
+ * Directory for the CSS artifact. A file-level directory resolves relative
70
+ * to the source file (never the working directory); a project-level
71
+ * directory resolves relative to the workspace root. A file-level directory
72
+ * is exact, without mirrored subfolders.
73
+ */
74
+ readonly cssDir?: string;
75
+ /**
76
+ * Optional CSS basename, for example `button.css`. Basenames only: no
77
+ * separators or traversal, and the `.css` extension is required.
78
+ */
79
+ readonly cssName?: string;
80
+ /** Directory for the JS class map, resolved like {@link OutputOptions.cssDir}. */
81
+ readonly mapDir?: string;
82
+ /**
83
+ * Optional JS map basename, for example `classes.js`. The opt-in declaration
84
+ * file becomes `classes.d.ts`.
85
+ */
86
+ readonly mapName?: string;
87
+ /** Write the stylesheet. Default `true`. */
88
+ readonly emitCss?: boolean;
89
+ /** Write the JS class map. Default `true`. */
90
+ readonly emitMap?: boolean;
91
+ /**
92
+ * Write a `.d.ts` beside the JS map with one literal `export declare const`
93
+ * per class. Default `false` (opt-in, CSSX-A52); automatically suppressed with
94
+ * a documented warning when map emission is on but types were requested
95
+ * without a map (SPECIFICATION.md §8).
96
+ */
97
+ readonly mapTypes?: boolean;
98
+ /**
99
+ * Deprecated alias for {@link OutputOptions.mapTypes}, kept for existing
100
+ * configurations. `map-types` wins when both are authored in one layer.
101
+ */
102
+ readonly emitTypes?: boolean;
103
+ /** Write a CSS debugging source map. Default `false`; never written when CSS is disabled. */
104
+ readonly sourceMap?: boolean;
105
+ /**
106
+ * Compact CSS output. Default `true` (ADR-0022). Compacts generated structural
107
+ * whitespace only and preserves authored CSS tokens, quoted strings, `calc()`
108
+ * spacing and declaration order. Set `false` (or pass `--no-minify`) for
109
+ * readable output.
110
+ */
111
+ readonly minify?: boolean;
112
+ /**
113
+ * Whether a style-merged module's class bindings join the importing build's
114
+ * class map and declarations. Default `true` (the ADR-0014 one-table rule).
115
+ * A module opts out with `@setup { map-inherit: false; }` (ADR-0020): its
116
+ * rules still merge into the importing stylesheet, but its bindings stay out
117
+ * of the importer's `map.js`, `.d.ts` and `CompileResult.classes`. The
118
+ * module's own entry map is never affected.
119
+ */
120
+ readonly mapInherit?: boolean;
121
+ /**
122
+ * Opt-in CSS side-effect import in the generated class map. Default `false`,
123
+ * so the map stays importable by plain Node without a loader (CSSX-A52).
124
+ * With `@setup { import-css: true; }` (ADR-0021) the map starts with one
125
+ * `import "<relative stylesheet>";` naming the emitted CSS artifact, which
126
+ * bundlers that carry CSS through imports (Vite, Rspack, Next-style loaders)
127
+ * then process. Omitted when the build emits no stylesheet; `.d.ts` output
128
+ * never carries the import.
129
+ */
130
+ readonly importCss?: boolean;
131
+ }
132
+
133
+ /** Options for compiling one source string with `compile`. */
134
+ export interface CompileOptions extends ParseOptions, OutputOptions {
135
+ /** Effective file-level class prefix; `naming.prefix` wins when both are set. */
136
+ readonly prefix?: string;
137
+ /** Class naming strategy and prefix. Defaults to strategy `prefix` with an empty prefix. */
138
+ readonly naming?: NamingOptions;
139
+ /**
140
+ * Project-level media alias overrides merged after built-in defaults and
141
+ * before file-level `@media-types`, which remain the source-level override
142
+ * layer (ADR-0006).
143
+ */
144
+ readonly media?: Readonly<Record<string, string>>;
145
+ /**
146
+ * Enable strict validation, including rejection of unknown configuration
147
+ * keys. Default `true` (ADR-0023); pass `false` to keep the warning path.
148
+ */
149
+ readonly strict?: boolean;
150
+ /** Media grouping mode. Default `safe`; `merge` is the opt-in optimizer (ADR-0024). */
151
+ readonly mediaGrouping?: MediaGroupingMode;
152
+ /**
153
+ * Absolute POSIX workspace root used for containment checks. When omitted,
154
+ * `compile` stays filesystem-free and resolves paths relative to `file`.
155
+ */
156
+ readonly workspaceRoot?: string;
157
+ }
158
+
159
+ /** Compiler behaviour settings as they appear in `chikx.config.json` (`compiler`). */
160
+ export interface CompilerConfig {
161
+ /**
162
+ * Reject unknown configuration keys and other lenient recoveries. Default
163
+ * `true` (ADR-0023); `false` keeps the warning path.
164
+ */
165
+ readonly strict?: boolean;
166
+ /** Media grouping mode. Default `safe`. */
167
+ readonly mediaGrouping?: MediaGroupingMode;
168
+ }
169
+
170
+ /**
171
+ * Declarative project configuration read from `chikx.config.json`
172
+ * (SPECIFICATION.md §10).
173
+ *
174
+ * Keys are camelCase in JSON; source `@setup` keys stay kebab-case. The
175
+ * document schema is frozen in P5, so these structural fields mirror §10
176
+ * without adding validation policy.
177
+ */
178
+ export interface ProjectConfig {
179
+ /** Glob patterns for project entries. */
180
+ readonly include?: readonly string[];
181
+ /** Glob patterns excluded from project discovery. */
182
+ readonly exclude?: readonly string[];
183
+ /** Project-level output settings, resolved relative to the workspace root. */
184
+ readonly output?: OutputOptions;
185
+ /** Project-level class naming configuration. */
186
+ readonly classes?: NamingOptions;
187
+ /** Project-level media alias overrides. */
188
+ readonly media?: Readonly<Record<string, string>>;
189
+ /** Compiler behaviour settings. */
190
+ readonly compiler?: CompilerConfig;
191
+ }
192
+
193
+ /** Options for compiling several entries through an injected host. */
194
+ export interface CompileProjectOptions {
195
+ /**
196
+ * Absolute POSIX workspace root override. Defaults to the nearest ancestor
197
+ * with `chikx.config.json`, else `package.json`, as reported by the host.
198
+ */
199
+ readonly projectRoot?: string;
200
+ /** Explicit configuration file path, relative to `projectRoot`. */
201
+ readonly configFile?: string;
202
+ /** Project-level output settings overriding the configuration file. */
203
+ readonly output?: OutputOptions;
204
+ /** Project-level naming overriding the configuration file. */
205
+ readonly naming?: NamingOptions;
206
+ /** Project-level media aliases overriding the configuration file. */
207
+ readonly media?: Readonly<Record<string, string>>;
208
+ /** Strict validation override. */
209
+ readonly strict?: boolean;
210
+ /** Media grouping override. */
211
+ readonly mediaGrouping?: MediaGroupingMode;
212
+ }
213
+
214
+ /**
215
+ * Effective output settings after merging built-in defaults, project
216
+ * configuration, file `@setup` and explicit CLI overrides.
217
+ *
218
+ * Every setting has a value, but directory fields are still authored strings:
219
+ * `planOutputs` resolves them against the documented base (source file for
220
+ * file-level settings, workspace root for project-level settings) and
221
+ * validates the result before any write. The authored `mapTypes` alias is not
222
+ * carried here: it resolves into {@link ResolvedSetup.emitTypes}, the single
223
+ * flag the planner reads.
224
+ */
225
+ export interface ResolvedSetup
226
+ extends Required<Omit<OutputOptions, "mapTypes" | "mapInherit" | "importCss">> {
227
+ /** Effective class prefix after naming precedence is applied. */
228
+ readonly prefix: string;
229
+ /**
230
+ * Effective module map-inheritance flag (ADR-0020). Defaults to `true` in
231
+ * `defaultSetupFor`; optional so hosts that only consume destination planning
232
+ * can omit it, and the compile pipeline treats an absent value as `true`.
233
+ */
234
+ readonly mapInherit?: boolean;
235
+ /**
236
+ * Effective class-map CSS import flag (ADR-0021). Defaults to `false` in
237
+ * `defaultSetupFor`; optional for the same host-compatibility reason as
238
+ * {@link ResolvedSetup.mapInherit}, and an absent value means `false`.
239
+ */
240
+ readonly importCss?: boolean;
241
+ /**
242
+ * File-level `@setup { css-dir: "a", "b"; }` list ([ADR-0019]).
243
+ *
244
+ * Present only when a file authored more than one CSS destination. Every
245
+ * entry resolves exactly relative to the source file, like the single
246
+ * {@link OutputOptions.cssDir}, and replaces any project/CLI single directory
247
+ * for the CSS artifact. `cssDir` keeps the first entry for consumers that
248
+ * still read the single value. Project configuration and CLI overrides cannot
249
+ * author a list in v0.1.
250
+ */
251
+ readonly cssDirs?: readonly string[];
252
+ }
253
+
254
+ /** Explicit CLI overrides; partial by design and never flatten multiple outputs silently. */
255
+ export type CliOutputOverrides = OutputOptions & {
256
+ /** CLI-level class prefix override. */
257
+ readonly prefix?: string;
258
+ };
259
+
260
+ /**
261
+ * Filesystem and policy operations the compiler needs from its embedding
262
+ * environment.
263
+ *
264
+ * Every method is asynchronous where it may hit the disk, and paths are
265
+ * absolute POSIX paths unless stated otherwise. Implementations own caching,
266
+ * case sensitivity and symlink policy; the compiler never imports `fs`.
267
+ */
268
+ export interface ModuleHost {
269
+ /** Read a UTF-8 source file. Rejects when the file is missing or unreadable. */
270
+ readFile(path: string): Promise<string>;
271
+ /** Test whether a path exists and is readable as a file. */
272
+ fileExists(path: string): Promise<boolean>;
273
+ /**
274
+ * Return the canonical real path used as module cache key, resolving
275
+ * symlinks and platform case rules.
276
+ */
277
+ canonicalize(path: string): string;
278
+ /**
279
+ * Resolve an import specifier `to` relative to the importing file `from`,
280
+ * returning an absolute POSIX path. Resolution is lexical here; callers use
281
+ * {@link ModuleHost.canonicalize} to detect symlink escapes.
282
+ */
283
+ resolvePath(from: string, to: string): string;
284
+ /** Absolute POSIX path of the workspace root used for containment checks. */
285
+ workspaceRoot(): string;
286
+ }
@@ -0,0 +1,121 @@
1
+ import type { Stylesheet } from "./ast.js";
2
+ import type { Diagnostic } from "./diagnostics.js";
3
+ import type { CliOutputOverrides, ProjectConfig, ResolvedSetup } from "./options.js";
4
+
5
+ /**
6
+ * Map from authored class name to emitted class name, for example
7
+ * `{ "button-primary": "ui-button-primary" }`.
8
+ *
9
+ * Keys are the exact authored class tokens (safe quoted keys in generated JS),
10
+ * so `__proto__` and other hazardous names must be represented with a
11
+ * null-prototype object or computed keys. The same key set backs camelCase
12
+ * named exports and the optional default-export aliases; an alias is added only
13
+ * when it is unambiguous, and a collision such as `foo-bar` with `fooBar` is a
14
+ * CHIKX4001 error rather than a silent omission (ADR-0010). Global classes are
15
+ * never present. Iteration order is deterministic.
16
+ */
17
+ export type ClassMap = Readonly<Record<string, string>>;
18
+
19
+ /** Result of compiling one source string. */
20
+ export interface CompileResult {
21
+ /** Generated stylesheet when CSS emission is enabled. */
22
+ readonly css?: string;
23
+ /** Generated JS class map when map emission is enabled. */
24
+ readonly js?: string;
25
+ /** Generated TypeScript declarations when type emission is enabled. */
26
+ readonly types?: string;
27
+ /** Standard v3 CSS source map text when requested and CSS is emitted. */
28
+ readonly sourceMap?: string;
29
+ /** Class name table with null-prototype safety semantics. */
30
+ readonly classes: ClassMap;
31
+ /** All diagnostics, errors and warnings, in source order. */
32
+ readonly diagnostics: readonly Diagnostic[];
33
+ /** Workspace-relative POSIX paths of every module the source depends on. */
34
+ readonly dependencies: readonly string[];
35
+ }
36
+
37
+ /** Result of a tolerant parse. */
38
+ export interface ParseResult {
39
+ /** Partial AST recovered at safe statement boundaries; never `undefined`. */
40
+ readonly ast: Stylesheet;
41
+ /** Syntax and lexical diagnostics in source order. */
42
+ readonly diagnostics: readonly Diagnostic[];
43
+ }
44
+
45
+ /** Kinds of artifact the output planner can plan. */
46
+ export type OutputKind = "css" | "js" | "types" | "cssSourceMap";
47
+
48
+ /** One planned artifact; the planner never writes it. */
49
+ export interface PlannedOutput {
50
+ /** Artifact kind, which determines the writer and manifest handling. */
51
+ readonly kind: OutputKind;
52
+ /** Absolute, POSIX-normalized destination path. */
53
+ readonly absolutePath: string;
54
+ /** Workspace-relative POSIX destination used for manifests and reporting. */
55
+ readonly workspacePath: string;
56
+ /** Workspace-relative POSIX path of the source that produced the artifact. */
57
+ readonly sourceFile: string;
58
+ }
59
+
60
+ /**
61
+ * One compiler-owned artifact recorded in the build manifest.
62
+ *
63
+ * Manifests let `clean` remove only files the compiler created and left
64
+ * untouched, and let rebuilds drop obsolete owned outputs after a successful
65
+ * build. Entries are deterministic and never carry timestamps.
66
+ */
67
+ export interface ManifestEntry {
68
+ /** Artifact kind of the tracked file. */
69
+ readonly kind: OutputKind;
70
+ /** Workspace-relative POSIX path of the tracked file. */
71
+ readonly workspacePath: string;
72
+ /** Workspace-relative POSIX path of the source that produced it. */
73
+ readonly sourceFile: string;
74
+ /** Content hash recorded after a successful write, when available. */
75
+ readonly contentHash?: string;
76
+ }
77
+
78
+ /**
79
+ * Ordered, validated set of artifacts for one entry plus its manifest records.
80
+ *
81
+ * `outputs` follows a stable kind order (CSS, JS map, declarations, CSS source
82
+ * map) so builds are deterministic; `manifest` mirrors the same artifacts in
83
+ * the same order.
84
+ */
85
+ export interface OutputPlan {
86
+ /** Planned artifacts in deterministic order. */
87
+ readonly outputs: readonly PlannedOutput[];
88
+ /** Manifest records for the planned artifacts, in the same order. */
89
+ readonly manifest: readonly ManifestEntry[];
90
+ }
91
+
92
+ /** Inputs to the pure output planner. */
93
+ export interface PlanOutputsInput {
94
+ /** Workspace-relative POSIX path of the entry source. */
95
+ readonly sourceFile: string;
96
+ /** Effective output settings after defaults, config, `@setup` and CLI precedence. */
97
+ readonly setup: ResolvedSetup;
98
+ /** Project configuration contributing project-level directories and output flags. */
99
+ readonly projectConfig?: ProjectConfig;
100
+ /** Explicit CLI overrides applied last. */
101
+ readonly cliOverrides?: CliOutputOverrides;
102
+ /** Absolute POSIX workspace root used for containment checks. */
103
+ readonly workspaceRoot: string;
104
+ }
105
+
106
+ /** Result of planning outputs: the plan plus any collision or safety diagnostics. */
107
+ export interface PlanOutputsResult {
108
+ /** Valid plan containing only artifacts that passed every CHIKX5001 check. */
109
+ readonly plan: OutputPlan;
110
+ /** Output-planning diagnostics; any error means the plan must not be written. */
111
+ readonly diagnostics: readonly Diagnostic[];
112
+ }
113
+
114
+ /**
115
+ * Signature of the pure output planner exposed as `planOutputs`.
116
+ *
117
+ * It resolves every destination, validates collisions, overlap, workspace and
118
+ * symlink escapes, and returns diagnostics instead of writing or throwing for
119
+ * expected planning failures.
120
+ */
121
+ export type PlanOutputs = (input: PlanOutputsInput) => PlanOutputsResult;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * A half-open range of offsets into one source file.
3
+ *
4
+ * `start` is inclusive and `end` is exclusive, so a zero-width span has
5
+ * `start === end`. Offsets are UTF-16 code-unit indices into the authored
6
+ * source text. Line and column numbers are deliberately **not** stored: every
7
+ * consumer derives them from `start`/`end` with the same offset-to-position
8
+ * rule, which keeps diagnostics, source maps and editor ranges consistent
9
+ * across stages.
10
+ */
11
+ export interface Span {
12
+ /** Inclusive offset of the first covered character. */
13
+ readonly start: number;
14
+ /** Exclusive offset just past the last covered character. */
15
+ readonly end: number;
16
+ /** Workspace-relative, POSIX-normalized path (forward slashes) of the source file. */
17
+ readonly file: string;
18
+ }
@@ -0,0 +1,77 @@
1
+ /**
2
+ * The lexical vocabulary shared by the tokenizer, parser and AST.
3
+ *
4
+ * A token always covers one contiguous slice of the original source and never
5
+ * normalizes escaped text. `Token.value` is the exact source text for the
6
+ * token, with the single documented exception of `function`, whose value
7
+ * excludes the opening parenthesis so parentheses stay individually balanced.
8
+ */
9
+ export type TokenKind =
10
+ /** Identifier or keyword, including escaped characters (`\31 23`, `status\:active`). */
11
+ | "ident"
12
+ /** Numeric token such as `12`, `0.5` or `10px`, kept as authored. */
13
+ | "number"
14
+ /** Quoted string including its delimiters; embedded `@`, `$`, `{`, `}` and `;` stay inert. */
15
+ | "string"
16
+ /** A complete `url(...)` construct including parentheses, quotes and any `@` or `$` inside. */
17
+ | "url"
18
+ /** A CSS function name; the immediately following `(` is emitted as a separate `lparen`. */
19
+ | "function"
20
+ /** Any other single character without a dedicated kind. */
21
+ | "delim"
22
+ | "colon"
23
+ | "semicolon"
24
+ | "comma"
25
+ | "lbrace"
26
+ | "rbrace"
27
+ | "lparen"
28
+ | "rparen"
29
+ | "lbracket"
30
+ | "rbracket"
31
+ /** A `#` name or hash value such as `#2563eb`. */
32
+ | "hash"
33
+ /**
34
+ * An `@` name such as `@media` or `@setup`, including the `@`. Directive and
35
+ * native at-rule names both arrive here because alias tables are
36
+ * configuration-dependent; the parser promotes recognized condition names to
37
+ * `modifier`.
38
+ */
39
+ | "at-keyword"
40
+ /** A compile-time variable reference, either `$name` or `$name.member`, including the `$`. */
41
+ | "dollar-ref"
42
+ /**
43
+ * A recognized Chikx condition modifier such as `@md` or `@xl+dark`, with the
44
+ * `@` and any `+` joins preserved. The tokenizer never emits this kind on its
45
+ * own: the parser reclassifies `at-keyword` to `modifier` only at top-level
46
+ * value depth (inline variants) or in block-condition position. It must not
47
+ * occur anywhere else, so an `@` inside strings, comments, URLs, functions or
48
+ * without preceding whitespace never becomes one.
49
+ */
50
+ | "modifier"
51
+ /** Insignificant whitespace, emitted so token streams can reproduce the source exactly. */
52
+ | "whitespace"
53
+ /** A block comment emitted for fidelity and skipped where insignificant. */
54
+ | "comment"
55
+ /** Zero-width end-of-input marker with an empty value. */
56
+ | "eof";
57
+
58
+ /**
59
+ * One lexical token pointing into the original source.
60
+ *
61
+ * Tokens carry offsets only; derive line and column from `start`/`end` when
62
+ * reporting. Escapes are never rewritten, so a stream that keeps whitespace and
63
+ * comment tokens reproduces the authored source byte for byte.
64
+ */
65
+ export interface Token {
66
+ readonly kind: TokenKind;
67
+ /**
68
+ * Exact source slice covered by `[start, end)` for every kind except
69
+ * `function`, whose value is the function name without its `(`. `eof` has an
70
+ * empty value.
71
+ */
72
+ readonly value: string;
73
+ /** Inclusive source offset of the token. */
74
+ readonly start: number;
75
+ /** Exclusive source offset of the token. */
76
+ readonly end: number;
77
+ }
@@ -0,0 +1,182 @@
1
+ import type { BlockItem, Declaration, Stylesheet, TokenDeclaration } from "../types/ast.js";
2
+ import type { Span } from "../types/span.js";
3
+ import type { Token } from "../types/token.js";
4
+ import { splitReference } from "./expand.js";
5
+ import type { TokenReference } from "./expand.js";
6
+
7
+ /**
8
+ * AST lookups used by substitution and runtime emission.
9
+ *
10
+ * The AST keeps absolute token offsets for values that contain `$` references
11
+ * (ADR-0005), which is exactly the provenance needed to report a reference at
12
+ * its authored position after lowering has replaced the AST with the IR.
13
+ */
14
+
15
+ /** One authored `$` reference found in the AST, with its absolute span. */
16
+ export interface AstReference {
17
+ /** Parsed name and optional namespace. */
18
+ readonly reference: TokenReference;
19
+ /** Exact authored span of the reference token. */
20
+ readonly span: Span;
21
+ }
22
+
23
+ /**
24
+ * Map each declaration segment start to its parsed value tokens.
25
+ *
26
+ * Lowering tiles a declaration span into one segment per value (A05), and the
27
+ * IR keeps that segment origin in `DeclarationIr.span`, so an IR declaration
28
+ * can recover the authored tokens by its `span.start`. The value is `undefined`
29
+ * when the authored segment contains no `$` reference (`tokens` is optional by
30
+ * contract), which is also the case where substitution has nothing to do.
31
+ */
32
+ export function segmentValueTokens(ast: Stylesheet | undefined): Map<number, readonly Token[]> {
33
+ const segments = new Map<number, readonly Token[]>();
34
+ if (ast === undefined) return segments;
35
+
36
+ const addDeclaration = (declaration: Declaration): void => {
37
+ if (declaration.tokens !== undefined) segments.set(declaration.span.start, declaration.tokens);
38
+ for (const variant of declaration.variants) {
39
+ if (variant.tokens !== undefined) segments.set(variant.span.start, variant.tokens);
40
+ }
41
+ };
42
+
43
+ const visitBody = (items: readonly BlockItem[]): void => {
44
+ for (const item of items) {
45
+ switch (item.kind) {
46
+ case "declaration":
47
+ addDeclaration(item);
48
+ break;
49
+ case "rule":
50
+ visitBody(item.body);
51
+ break;
52
+ case "conditional-block":
53
+ visitBody(item.body);
54
+ break;
55
+ case "native-at-rule":
56
+ if (item.block !== undefined) visitBody(item.block);
57
+ break;
58
+ case "group-use-site":
59
+ // Use-site overrides are ordinary declarations of the consuming
60
+ // module even before expansion replaces the node (ADR-0017).
61
+ for (const declaration of item.declarations) addDeclaration(declaration);
62
+ break;
63
+ }
64
+ }
65
+ };
66
+
67
+ for (const item of ast.items) {
68
+ if (item.kind === "rule") visitBody(item.body);
69
+ else if (item.kind === "native-at-rule" && item.block !== undefined) visitBody(item.block);
70
+ }
71
+
72
+ return segments;
73
+ }
74
+
75
+ /**
76
+ * Collect the segment starts authored inside top-level `@group.<name>`
77
+ * definitions (ADR-0017).
78
+ *
79
+ * Definitions emit no IR, so a {@link DeclarationIr} whose origin segment is in
80
+ * this set can only have reached the IR through use-site expansion. Substitution
81
+ * uses that to enforce owner decision (b): group defaults are compile-time only,
82
+ * so a `@runtime` reference there is CHIKX2001 and the segment is omitted
83
+ * instead of rewritten to `var(--...)` with the consuming build's prefix.
84
+ *
85
+ * Both the base segment and every inline-variant segment are included, matching
86
+ * the IR's per-segment spans.
87
+ */
88
+ export function groupDefaultSegments(ast: Stylesheet | undefined): Set<number> {
89
+ const starts = new Set<number>();
90
+ if (ast === undefined) return starts;
91
+ for (const item of ast.items) {
92
+ if (item.kind !== "group-definition") continue;
93
+ for (const declaration of item.declarations) {
94
+ starts.add(declaration.span.start);
95
+ for (const variant of declaration.variants) starts.add(variant.span.start);
96
+ }
97
+ }
98
+ return starts;
99
+ }
100
+
101
+ /**
102
+ * Collect authored `$` references from one module's AST.
103
+ *
104
+ * `includeRules` adds references from stylesheet rule values (used for the entry
105
+ * module, whose declarations are compiled); without it only `@variables` and
106
+ * `@runtime` values are visited, which is what imported token-only modules can
107
+ * contribute to the entry's artifact (SPECIFICATION.md §6.1: imports bring in
108
+ * tokens only, not rules). Rule values include the declarations of `@group.`
109
+ * use-site blocks, because this module's build emits them (ADR-0017).
110
+ */
111
+ export function astReferences(
112
+ ast: Stylesheet,
113
+ options: { readonly includeRules: boolean },
114
+ ): AstReference[] {
115
+ const references: AstReference[] = [];
116
+
117
+ const collect = (tokens: readonly Token[] | undefined, file: string): void => {
118
+ if (tokens === undefined) return;
119
+ for (const token of tokens) {
120
+ if (token.kind !== "dollar-ref") continue;
121
+ const reference = splitReference(token.value);
122
+ if (reference === null) continue;
123
+ references.push({ reference, span: { start: token.start, end: token.end, file } });
124
+ }
125
+ };
126
+
127
+ const collectDeclaration = (declaration: Declaration, file: string): void => {
128
+ collect(declaration.tokens, file);
129
+ for (const variant of declaration.variants) collect(variant.tokens, file);
130
+ };
131
+
132
+ const collectTokenDeclaration = (token: TokenDeclaration, file: string): void => {
133
+ collect(token.tokens, file);
134
+ };
135
+
136
+ const visitBody = (items: readonly BlockItem[], file: string): void => {
137
+ for (const item of items) {
138
+ switch (item.kind) {
139
+ case "declaration":
140
+ collectDeclaration(item, file);
141
+ break;
142
+ case "rule":
143
+ visitBody(item.body, file);
144
+ break;
145
+ case "conditional-block":
146
+ visitBody(item.body, file);
147
+ break;
148
+ case "native-at-rule":
149
+ if (item.block !== undefined) visitBody(item.block, file);
150
+ break;
151
+ case "group-use-site":
152
+ // Use-site overrides are emitted in this module's build, so a
153
+ // `@runtime` reference authored there must register its custom
154
+ // property exactly like a plain declaration (ADR-0017 item 6).
155
+ for (const declaration of item.declarations) collectDeclaration(declaration, file);
156
+ break;
157
+ }
158
+ }
159
+ };
160
+
161
+ const file = ast.file;
162
+ for (const item of ast.items) {
163
+ switch (item.kind) {
164
+ case "variables":
165
+ for (const token of item.tokens) collectTokenDeclaration(token, file);
166
+ break;
167
+ case "runtime":
168
+ for (const declaration of item.declarations) collectDeclaration(declaration, file);
169
+ break;
170
+ case "rule":
171
+ if (options.includeRules) visitBody(item.body, file);
172
+ break;
173
+ case "native-at-rule":
174
+ if (options.includeRules && item.block !== undefined) visitBody(item.block, file);
175
+ break;
176
+ default:
177
+ break;
178
+ }
179
+ }
180
+
181
+ return references;
182
+ }