@moondraft/import 0.0.0-stage → 0.1.0

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 (323) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +34 -0
  3. package/README.md +36 -2
  4. package/SOURCE.md +169 -0
  5. package/dist/adapter/convert.d.ts +122 -0
  6. package/dist/adapter/convert.js +2302 -0
  7. package/dist/adapter/figma.d.ts +9 -0
  8. package/dist/adapter/figma.js +30 -0
  9. package/dist/adapter/file-components.d.ts +14 -0
  10. package/dist/adapter/file-components.js +262 -0
  11. package/dist/adapter/file-resources.d.ts +29 -0
  12. package/dist/adapter/file-resources.js +562 -0
  13. package/dist/adapter/index.d.ts +6 -0
  14. package/dist/adapter/index.js +9 -0
  15. package/dist/adapter/link.d.ts +45 -0
  16. package/dist/adapter/link.js +688 -0
  17. package/dist/adapter/payload.d.ts +31 -0
  18. package/dist/adapter/payload.js +78 -0
  19. package/dist/adapter/shapes.d.ts +31 -0
  20. package/dist/adapter/shapes.js +75 -0
  21. package/dist/adapter/variables.d.ts +44 -0
  22. package/dist/adapter/variables.js +103 -0
  23. package/dist/figma-file.d.ts +38 -0
  24. package/dist/figma-file.js +97 -0
  25. package/dist/figma-worker.d.ts +11 -0
  26. package/dist/figma-worker.js +12 -0
  27. package/dist/figma.d.ts +26 -0
  28. package/dist/figma.js +96 -0
  29. package/dist/index.d.ts +28 -0
  30. package/dist/index.js +49 -0
  31. package/dist/report.d.ts +15 -0
  32. package/dist/report.js +69 -0
  33. package/dist/svg.d.ts +33 -0
  34. package/dist/svg.js +44 -0
  35. package/dist/syntax.d.ts +8 -0
  36. package/dist/syntax.js +19 -0
  37. package/dist/taco/figma-import/clipboard.d.ts +25 -0
  38. package/dist/taco/figma-import/clipboard.js +87 -0
  39. package/dist/taco/figma-import/clipboardSize.d.ts +31 -0
  40. package/dist/taco/figma-import/clipboardSize.js +107 -0
  41. package/dist/taco/figma-import/container/compression.d.ts +9 -0
  42. package/dist/taco/figma-import/container/compression.js +174 -0
  43. package/dist/taco/figma-import/container/error.d.ts +15 -0
  44. package/dist/taco/figma-import/container/error.js +19 -0
  45. package/dist/taco/figma-import/container/kiwiContainer.d.ts +38 -0
  46. package/dist/taco/figma-import/container/kiwiContainer.js +61 -0
  47. package/dist/taco/figma-import/container/limits.d.ts +22 -0
  48. package/dist/taco/figma-import/container/limits.js +22 -0
  49. package/dist/taco/figma-import/container/zip.d.ts +14 -0
  50. package/dist/taco/figma-import/container/zip.js +154 -0
  51. package/dist/taco/figma-import/figFile.d.ts +34 -0
  52. package/dist/taco/figma-import/figFile.js +91 -0
  53. package/dist/taco/figma-import/figFileDocument.d.ts +25 -0
  54. package/dist/taco/figma-import/figFileDocument.js +100 -0
  55. package/dist/taco/figma-import/import/blobs.d.ts +10 -0
  56. package/dist/taco/figma-import/import/blobs.js +23 -0
  57. package/dist/taco/figma-import/import/carry.d.ts +11 -0
  58. package/dist/taco/figma-import/import/carry.js +33 -0
  59. package/dist/taco/figma-import/import/componentProps.d.ts +84 -0
  60. package/dist/taco/figma-import/import/componentProps.js +313 -0
  61. package/dist/taco/figma-import/import/componentSets.d.ts +83 -0
  62. package/dist/taco/figma-import/import/componentSets.js +647 -0
  63. package/dist/taco/figma-import/import/converters/effects.d.ts +31 -0
  64. package/dist/taco/figma-import/import/converters/effects.js +127 -0
  65. package/dist/taco/figma-import/import/converters/ellipse.d.ts +7 -0
  66. package/dist/taco/figma-import/import/converters/ellipse.js +54 -0
  67. package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
  68. package/dist/taco/figma-import/import/converters/frame.js +55 -0
  69. package/dist/taco/figma-import/import/converters/gradient.d.ts +72 -0
  70. package/dist/taco/figma-import/import/converters/gradient.js +106 -0
  71. package/dist/taco/figma-import/import/converters/group.d.ts +21 -0
  72. package/dist/taco/figma-import/import/converters/group.js +39 -0
  73. package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
  74. package/dist/taco/figma-import/import/converters/image.js +83 -0
  75. package/dist/taco/figma-import/import/converters/layout.d.ts +34 -0
  76. package/dist/taco/figma-import/import/converters/layout.js +168 -0
  77. package/dist/taco/figma-import/import/converters/line.d.ts +11 -0
  78. package/dist/taco/figma-import/import/converters/line.js +40 -0
  79. package/dist/taco/figma-import/import/converters/rectangle.d.ts +3 -0
  80. package/dist/taco/figma-import/import/converters/rectangle.js +11 -0
  81. package/dist/taco/figma-import/import/converters/section.d.ts +9 -0
  82. package/dist/taco/figma-import/import/converters/section.js +35 -0
  83. package/dist/taco/figma-import/import/converters/shared.d.ts +68 -0
  84. package/dist/taco/figma-import/import/converters/shared.js +362 -0
  85. package/dist/taco/figma-import/import/converters/text.d.ts +41 -0
  86. package/dist/taco/figma-import/import/converters/text.js +576 -0
  87. package/dist/taco/figma-import/import/converters/vector.d.ts +84 -0
  88. package/dist/taco/figma-import/import/converters/vector.js +468 -0
  89. package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +98 -0
  90. package/dist/taco/figma-import/import/converters/vectorNetwork.js +385 -0
  91. package/dist/taco/figma-import/import/fields.d.ts +15 -0
  92. package/dist/taco/figma-import/import/fields.js +29 -0
  93. package/dist/taco/figma-import/import/instances.d.ts +163 -0
  94. package/dist/taco/figma-import/import/instances.js +1323 -0
  95. package/dist/taco/figma-import/import/nestingCycles.d.ts +86 -0
  96. package/dist/taco/figma-import/import/nestingCycles.js +488 -0
  97. package/dist/taco/figma-import/import/slots.d.ts +56 -0
  98. package/dist/taco/figma-import/import/slots.js +96 -0
  99. package/dist/taco/figma-import/import/styles.d.ts +46 -0
  100. package/dist/taco/figma-import/import/styles.js +42 -0
  101. package/dist/taco/figma-import/import/tree.d.ts +66 -0
  102. package/dist/taco/figma-import/import/tree.js +194 -0
  103. package/dist/taco/figma-import/import/types.d.ts +232 -0
  104. package/dist/taco/figma-import/import/types.js +33 -0
  105. package/dist/taco/figma-import/import/variables.d.ts +136 -0
  106. package/dist/taco/figma-import/import/variables.js +762 -0
  107. package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
  108. package/dist/taco/figma-import/import/variantTrees.js +480 -0
  109. package/dist/taco/figma-import/import/walk.d.ts +159 -0
  110. package/dist/taco/figma-import/import/walk.js +1297 -0
  111. package/dist/taco/figma-import/index.d.ts +22 -0
  112. package/dist/taco/figma-import/index.js +13 -0
  113. package/dist/taco/figma-import/kiwi/byteReader.d.ts +37 -0
  114. package/dist/taco/figma-import/kiwi/byteReader.js +151 -0
  115. package/dist/taco/figma-import/kiwi/error.d.ts +14 -0
  116. package/dist/taco/figma-import/kiwi/error.js +29 -0
  117. package/dist/taco/figma-import/kiwi/limits.d.ts +18 -0
  118. package/dist/taco/figma-import/kiwi/limits.js +18 -0
  119. package/dist/taco/figma-import/kiwi/message.d.ts +10 -0
  120. package/dist/taco/figma-import/kiwi/message.js +145 -0
  121. package/dist/taco/figma-import/kiwi/schema.d.ts +42 -0
  122. package/dist/taco/figma-import/kiwi/schema.js +206 -0
  123. package/dist/taco/figma-import/kiwi/types.d.ts +46 -0
  124. package/dist/taco/figma-import/kiwi/types.js +18 -0
  125. package/dist/taco/model/agent-hidden-fields.d.ts +20 -0
  126. package/dist/taco/model/agent-hidden-fields.js +29 -0
  127. package/dist/taco/model/asset-users.d.ts +23 -0
  128. package/dist/taco/model/asset-users.js +128 -0
  129. package/dist/taco/model/auto-layout-lines.d.ts +32 -0
  130. package/dist/taco/model/auto-layout-lines.js +70 -0
  131. package/dist/taco/model/codec.d.ts +444 -0
  132. package/dist/taco/model/codec.js +5635 -0
  133. package/dist/taco/model/color.d.ts +11 -0
  134. package/dist/taco/model/color.js +61 -0
  135. package/dist/taco/model/comment-operations.d.ts +102 -0
  136. package/dist/taco/model/comment-operations.js +227 -0
  137. package/dist/taco/model/component-evaluation.d.ts +94 -0
  138. package/dist/taco/model/component-evaluation.js +548 -0
  139. package/dist/taco/model/component-expressions.d.ts +242 -0
  140. package/dist/taco/model/component-expressions.js +1082 -0
  141. package/dist/taco/model/component-properties.d.ts +111 -0
  142. package/dist/taco/model/component-properties.js +339 -0
  143. package/dist/taco/model/component-samples.d.ts +83 -0
  144. package/dist/taco/model/component-samples.js +290 -0
  145. package/dist/taco/model/component-validity.d.ts +85 -0
  146. package/dist/taco/model/component-validity.js +397 -0
  147. package/dist/taco/model/components.d.ts +155 -0
  148. package/dist/taco/model/components.js +505 -0
  149. package/dist/taco/model/connector-ends.d.ts +20 -0
  150. package/dist/taco/model/connector-ends.js +188 -0
  151. package/dist/taco/model/connector-geometry.d.ts +74 -0
  152. package/dist/taco/model/connector-geometry.js +293 -0
  153. package/dist/taco/model/corner-radii.d.ts +26 -0
  154. package/dist/taco/model/corner-radii.js +47 -0
  155. package/dist/taco/model/derivations.d.ts +81 -0
  156. package/dist/taco/model/derivations.js +151 -0
  157. package/dist/taco/model/export-settings.d.ts +53 -0
  158. package/dist/taco/model/export-settings.js +156 -0
  159. package/dist/taco/model/expressions/ast.d.ts +153 -0
  160. package/dist/taco/model/expressions/ast.js +6 -0
  161. package/dist/taco/model/expressions/cache.d.ts +23 -0
  162. package/dist/taco/model/expressions/cache.js +43 -0
  163. package/dist/taco/model/expressions/check.d.ts +72 -0
  164. package/dist/taco/model/expressions/check.js +517 -0
  165. package/dist/taco/model/expressions/completion.d.ts +61 -0
  166. package/dist/taco/model/expressions/completion.js +421 -0
  167. package/dist/taco/model/expressions/evaluate.d.ts +32 -0
  168. package/dist/taco/model/expressions/evaluate.js +519 -0
  169. package/dist/taco/model/expressions/forms.d.ts +84 -0
  170. package/dist/taco/model/expressions/forms.js +200 -0
  171. package/dist/taco/model/expressions/index.d.ts +14 -0
  172. package/dist/taco/model/expressions/index.js +17 -0
  173. package/dist/taco/model/expressions/lexer.d.ts +42 -0
  174. package/dist/taco/model/expressions/lexer.js +213 -0
  175. package/dist/taco/model/expressions/library.d.ts +33 -0
  176. package/dist/taco/model/expressions/library.js +117 -0
  177. package/dist/taco/model/expressions/names.d.ts +43 -0
  178. package/dist/taco/model/expressions/names.js +99 -0
  179. package/dist/taco/model/expressions/parser.d.ts +15 -0
  180. package/dist/taco/model/expressions/parser.js +438 -0
  181. package/dist/taco/model/expressions/rename.d.ts +34 -0
  182. package/dist/taco/model/expressions/rename.js +46 -0
  183. package/dist/taco/model/expressions/source.d.ts +15 -0
  184. package/dist/taco/model/expressions/source.js +161 -0
  185. package/dist/taco/model/expressions/types.d.ts +119 -0
  186. package/dist/taco/model/expressions/types.js +102 -0
  187. package/dist/taco/model/expressions/values.d.ts +97 -0
  188. package/dist/taco/model/expressions/values.js +160 -0
  189. package/dist/taco/model/geometry-limits.d.ts +16 -0
  190. package/dist/taco/model/geometry-limits.js +67 -0
  191. package/dist/taco/model/geometry.d.ts +47 -0
  192. package/dist/taco/model/geometry.js +193 -0
  193. package/dist/taco/model/group-bounds.d.ts +38 -0
  194. package/dist/taco/model/group-bounds.js +144 -0
  195. package/dist/taco/model/id-table.d.ts +37 -0
  196. package/dist/taco/model/id-table.js +80 -0
  197. package/dist/taco/model/index.d.ts +25 -0
  198. package/dist/taco/model/index.js +27 -0
  199. package/dist/taco/model/instance-resolver.d.ts +372 -0
  200. package/dist/taco/model/instance-resolver.js +1748 -0
  201. package/dist/taco/model/layer-syntax.d.ts +340 -0
  202. package/dist/taco/model/layer-syntax.js +3506 -0
  203. package/dist/taco/model/layout/constraints.d.ts +84 -0
  204. package/dist/taco/model/layout/constraints.js +445 -0
  205. package/dist/taco/model/layout/frame-layout.d.ts +46 -0
  206. package/dist/taco/model/layout/frame-layout.js +618 -0
  207. package/dist/taco/model/layout/node-sizing.d.ts +163 -0
  208. package/dist/taco/model/layout/node-sizing.js +486 -0
  209. package/dist/taco/model/legacy-connector-caps.d.ts +15 -0
  210. package/dist/taco/model/legacy-connector-caps.js +70 -0
  211. package/dist/taco/model/linear-call.d.ts +48 -0
  212. package/dist/taco/model/linear-call.js +58 -0
  213. package/dist/taco/model/new-nodes.d.ts +145 -0
  214. package/dist/taco/model/new-nodes.js +277 -0
  215. package/dist/taco/model/node-builder.d.ts +449 -0
  216. package/dist/taco/model/node-builder.js +2294 -0
  217. package/dist/taco/model/node-table-watch.d.ts +36 -0
  218. package/dist/taco/model/node-table-watch.js +63 -0
  219. package/dist/taco/model/node-transform.d.ts +39 -0
  220. package/dist/taco/model/node-transform.js +79 -0
  221. package/dist/taco/model/node-users-index.d.ts +10 -0
  222. package/dist/taco/model/node-users-index.js +82 -0
  223. package/dist/taco/model/operation-references.d.ts +80 -0
  224. package/dist/taco/model/operation-references.js +399 -0
  225. package/dist/taco/model/operations.d.ts +610 -0
  226. package/dist/taco/model/operations.js +2882 -0
  227. package/dist/taco/model/overlay-table.d.ts +71 -0
  228. package/dist/taco/model/overlay-table.js +171 -0
  229. package/dist/taco/model/override-remap.d.ts +69 -0
  230. package/dist/taco/model/override-remap.js +230 -0
  231. package/dist/taco/model/parent-table.d.ts +53 -0
  232. package/dist/taco/model/parent-table.js +193 -0
  233. package/dist/taco/model/problem-collector.d.ts +27 -0
  234. package/dist/taco/model/problem-collector.js +101 -0
  235. package/dist/taco/model/sha256.d.ts +10 -0
  236. package/dist/taco/model/sha256.js +74 -0
  237. package/dist/taco/model/shape-geometry.d.ts +96 -0
  238. package/dist/taco/model/shape-geometry.js +368 -0
  239. package/dist/taco/model/shared-node-table.d.ts +97 -0
  240. package/dist/taco/model/shared-node-table.js +546 -0
  241. package/dist/taco/model/slots.d.ts +113 -0
  242. package/dist/taco/model/slots.js +281 -0
  243. package/dist/taco/model/stroke-settings.d.ts +37 -0
  244. package/dist/taco/model/stroke-settings.js +90 -0
  245. package/dist/taco/model/svg-path-import.d.ts +89 -0
  246. package/dist/taco/model/svg-path-import.js +219 -0
  247. package/dist/taco/model/text-measurement.d.ts +142 -0
  248. package/dist/taco/model/text-measurement.js +846 -0
  249. package/dist/taco/model/text-reads.d.ts +66 -0
  250. package/dist/taco/model/text-reads.js +128 -0
  251. package/dist/taco/model/text-sizer.d.ts +358 -0
  252. package/dist/taco/model/text-sizer.js +996 -0
  253. package/dist/taco/model/theme-users.d.ts +59 -0
  254. package/dist/taco/model/theme-users.js +312 -0
  255. package/dist/taco/model/thumbnail.d.ts +15 -0
  256. package/dist/taco/model/thumbnail.js +34 -0
  257. package/dist/taco/model/types.d.ts +2347 -0
  258. package/dist/taco/model/types.js +366 -0
  259. package/dist/taco/model/unknown-property.d.ts +5 -0
  260. package/dist/taco/model/unknown-property.js +7 -0
  261. package/dist/taco/model/validated-documents.d.ts +22 -0
  262. package/dist/taco/model/validated-documents.js +22 -0
  263. package/dist/taco/model/variable-modes.d.ts +61 -0
  264. package/dist/taco/model/variable-modes.js +438 -0
  265. package/dist/taco/model/variable-references.d.ts +54 -0
  266. package/dist/taco/model/variable-references.js +183 -0
  267. package/dist/taco/model/variables.d.ts +24 -0
  268. package/dist/taco/model/variables.js +151 -0
  269. package/dist/taco/model/vector-network.d.ts +34 -0
  270. package/dist/taco/model/vector-network.js +411 -0
  271. package/dist/taco/svg-import/import-svg.d.ts +36 -0
  272. package/dist/taco/svg-import/import-svg.js +729 -0
  273. package/dist/taco/svg-import/index.d.ts +3 -0
  274. package/dist/taco/svg-import/index.js +3 -0
  275. package/dist/taco/svg-import/limits/limits.d.ts +79 -0
  276. package/dist/taco/svg-import/limits/limits.js +160 -0
  277. package/dist/taco/svg-import/paint/gradients.d.ts +64 -0
  278. package/dist/taco/svg-import/paint/gradients.js +429 -0
  279. package/dist/taco/svg-import/paint/paint.d.ts +32 -0
  280. package/dist/taco/svg-import/paint/paint.js +162 -0
  281. package/dist/taco/svg-import/paths/paths.d.ts +58 -0
  282. package/dist/taco/svg-import/paths/paths.js +205 -0
  283. package/dist/taco/svg-import/public-types.d.ts +279 -0
  284. package/dist/taco/svg-import/public-types.js +56 -0
  285. package/dist/taco/svg-import/report/report.d.ts +44 -0
  286. package/dist/taco/svg-import/report/report.js +79 -0
  287. package/dist/taco/svg-import/reuse/reuse.d.ts +99 -0
  288. package/dist/taco/svg-import/reuse/reuse.js +840 -0
  289. package/dist/taco/svg-import/shapes/shapes.d.ts +35 -0
  290. package/dist/taco/svg-import/shapes/shapes.js +386 -0
  291. package/dist/taco/svg-import/structure/effects.d.ts +26 -0
  292. package/dist/taco/svg-import/structure/effects.js +618 -0
  293. package/dist/taco/svg-import/structure/image.d.ts +24 -0
  294. package/dist/taco/svg-import/structure/image.js +495 -0
  295. package/dist/taco/svg-import/structure/walk.d.ts +85 -0
  296. package/dist/taco/svg-import/structure/walk.js +544 -0
  297. package/dist/taco/svg-import/style/color.d.ts +40 -0
  298. package/dist/taco/svg-import/style/color.js +275 -0
  299. package/dist/taco/svg-import/style/computed.d.ts +214 -0
  300. package/dist/taco/svg-import/style/computed.js +1534 -0
  301. package/dist/taco/svg-import/style/css.d.ts +127 -0
  302. package/dist/taco/svg-import/style/css.js +541 -0
  303. package/dist/taco/svg-import/style/media.d.ts +25 -0
  304. package/dist/taco/svg-import/style/media.js +307 -0
  305. package/dist/taco/svg-import/style/properties.d.ts +43 -0
  306. package/dist/taco/svg-import/style/properties.js +135 -0
  307. package/dist/taco/svg-import/style/selector-reach.d.ts +30 -0
  308. package/dist/taco/svg-import/style/selector-reach.js +169 -0
  309. package/dist/taco/svg-import/style/selector-validity.d.ts +14 -0
  310. package/dist/taco/svg-import/style/selector-validity.js +307 -0
  311. package/dist/taco/svg-import/text/text.d.ts +29 -0
  312. package/dist/taco/svg-import/text/text.js +545 -0
  313. package/dist/taco/svg-import/transform/matrix.d.ts +111 -0
  314. package/dist/taco/svg-import/transform/matrix.js +229 -0
  315. package/dist/taco/svg-import/tree/element-data.d.ts +52 -0
  316. package/dist/taco/svg-import/tree/element-data.js +155 -0
  317. package/dist/taco/svg-import/tree/elements.d.ts +36 -0
  318. package/dist/taco/svg-import/tree/elements.js +121 -0
  319. package/dist/taco/svg-import/units/length.d.ts +43 -0
  320. package/dist/taco/svg-import/units/length.js +125 -0
  321. package/dist/taco/svg-import/units/viewbox.d.ts +35 -0
  322. package/dist/taco/svg-import/units/viewbox.js +77 -0
  323. package/package.json +54 -4
@@ -0,0 +1,200 @@
1
+ // The two expression forms the editor shows as plain controls
2
+ // (docs/components/design.md 8.8): the value table, a `match` on one enum
3
+ // or boolean property whose arms are constants, and the "Show when"
4
+ // conditions. Each has a reader that recognises exactly its form in source
5
+ // text and a writer that builds the source again. Anything else stays an
6
+ // expression.
7
+ import { formatExpressionName } from "./names.js";
8
+ import { parseExpression } from "./parser.js";
9
+ /** The rows a value table over this subject has, without values. */
10
+ export function valueTableKeys(subject) {
11
+ if (subject.kind === "boolean") {
12
+ return [
13
+ { key: "true", label: "on" },
14
+ { key: "false", label: "off" },
15
+ ];
16
+ }
17
+ return subject.type.options.map((option) => ({ key: option, label: option }));
18
+ }
19
+ /** The subject for a property, or undefined when a table cannot vary by it (it is not an enum or boolean). */
20
+ export function valueTableSubject(property, environment) {
21
+ const type = environment.propertyType(property);
22
+ if (type?.kind === "enum")
23
+ return { kind: "enum", type };
24
+ if (type?.kind === "boolean")
25
+ return { kind: "boolean" };
26
+ return undefined;
27
+ }
28
+ /** True when the expression reads nothing that changes with property values: no property, `stored`, `if`, `let` or `match`. */
29
+ export function isConstantExpression(expression) {
30
+ switch (expression.kind) {
31
+ case "boolean":
32
+ case "number":
33
+ case "string":
34
+ case "color":
35
+ case "option":
36
+ case "variable":
37
+ return true;
38
+ case "name":
39
+ case "stored":
40
+ case "if":
41
+ case "let":
42
+ case "match":
43
+ return false;
44
+ case "call":
45
+ return expression.args.every((arg) => isConstantExpression(arg.value));
46
+ case "list":
47
+ return expression.items.every(isConstantExpression);
48
+ case "field":
49
+ return isConstantExpression(expression.target);
50
+ case "with":
51
+ return isConstantExpression(expression.target) && expression.updates.every((u) => isConstantExpression(u.value));
52
+ case "unary":
53
+ return isConstantExpression(expression.operand);
54
+ case "binary":
55
+ return isConstantExpression(expression.left) && isConstantExpression(expression.right);
56
+ }
57
+ }
58
+ function readTable(source, environment) {
59
+ const parsed = parseExpression(source);
60
+ if (!parsed.ok)
61
+ return undefined;
62
+ const match = parsed.expression;
63
+ if (match.kind !== "match" || match.otherwise !== undefined || match.subject.kind !== "name")
64
+ return undefined;
65
+ const property = match.subject.name;
66
+ const subject = valueTableSubject(property, environment);
67
+ if (subject === undefined)
68
+ return undefined;
69
+ const keys = valueTableKeys(subject);
70
+ const spans = new Map();
71
+ for (const arm of match.arms) {
72
+ if (arm.patterns.length !== 1 || !isConstantExpression(arm.body))
73
+ return undefined;
74
+ const pattern = arm.patterns[0];
75
+ if ((pattern.kind === "option") !== (subject.kind === "enum"))
76
+ return undefined;
77
+ const key = pattern.kind === "option" ? pattern.name : String(pattern.value);
78
+ if (!keys.some((k) => k.key === key) || spans.has(key))
79
+ return undefined;
80
+ spans.set(key, arm.body.span);
81
+ }
82
+ const rows = keys.map(({ key, label }) => {
83
+ const span = spans.get(key);
84
+ return span === undefined ? { key, label } : { key, label, source: source.slice(span.start, span.end) };
85
+ });
86
+ return { table: { property, subject, rows }, spans };
87
+ }
88
+ /**
89
+ * Recognises a value table: a `match` on one enum or boolean property,
90
+ * one pattern per arm, no `else`, every arm a constant. Rows follow the
91
+ * property's option order ("on" before "off"); an option the `match` does
92
+ * not cover yet (added to the enum later) is a row without `source`.
93
+ * Returns undefined for anything else, which the editor shows as an
94
+ * expression.
95
+ */
96
+ export function readValueTable(source, environment) {
97
+ return readTable(source, environment)?.table;
98
+ }
99
+ const TABLE_LINE_LIMIT = 80;
100
+ /**
101
+ * Writes a value table as a `match`, one arm per row in row order; rows
102
+ * without `source` are left out. It fits on one line when short enough,
103
+ * as in `match size { .small => 4px, .large => 8px }`, and otherwise puts
104
+ * each arm on its own line.
105
+ */
106
+ export function writeValueTable(table) {
107
+ const arms = table.rows
108
+ .filter((row) => row.source !== undefined)
109
+ .map((row) => `${table.subject.kind === "enum" ? `.${formatExpressionName(row.key)}` : row.key} => ${row.source.trim()}`);
110
+ const line = `match ${formatExpressionName(table.property)} { ${arms.join(", ")} }`;
111
+ if (line.length <= TABLE_LINE_LIMIT && !arms.some((arm) => arm.includes("\n")))
112
+ return line;
113
+ return `match ${formatExpressionName(table.property)} {\n${arms.map((arm) => ` ${arm}`).join(",\n")}\n}`;
114
+ }
115
+ /** A new value table over the property with every row holding `rowSource`, the field's current value. */
116
+ export function startValueTable(property, subject, rowSource) {
117
+ return writeValueTable({
118
+ property,
119
+ subject,
120
+ rows: valueTableKeys(subject).map(({ key, label }) => ({ key, label, source: rowSource })),
121
+ });
122
+ }
123
+ /**
124
+ * Replaces one row's constant. A row that is already written is replaced
125
+ * in place, so the rest of the text (comments, line breaks) stays; a row
126
+ * the `match` does not cover yet is added by writing the table again.
127
+ * Throws when `source` is not a value table or `key` is not one of its
128
+ * rows: the caller only offers rows of a table it read.
129
+ */
130
+ export function setValueTableRow(source, environment, key, rowSource) {
131
+ const read = readTable(source, environment);
132
+ if (read === undefined)
133
+ throw new Error("The expression is not a value table.");
134
+ if (!read.table.rows.some((row) => row.key === key))
135
+ throw new Error(`The value table has no row \`${key}\`.`);
136
+ const span = read.spans.get(key);
137
+ if (span !== undefined)
138
+ return source.slice(0, span.start) + rowSource.trim() + source.slice(span.end);
139
+ return writeValueTable({
140
+ ...read.table,
141
+ rows: read.table.rows.map((row) => (row.key === key ? { ...row, source: rowSource } : row)),
142
+ });
143
+ }
144
+ function enumComparison(expression) {
145
+ if (expression.kind !== "binary" || expression.operator !== "==")
146
+ return undefined;
147
+ if (expression.left.kind !== "name" || expression.right.kind !== "option")
148
+ return undefined;
149
+ return { property: expression.left.name, option: expression.right.name };
150
+ }
151
+ function orOperands(expression) {
152
+ if (expression.kind === "binary" && expression.operator === "or") {
153
+ return [...orOperands(expression.left), ...orOperands(expression.right)];
154
+ }
155
+ return [expression];
156
+ }
157
+ /**
158
+ * Recognises a "Show when" condition: `showIcon`, `not showIcon`,
159
+ * `size == .large`, or `size == .a or size == .b ...` with one property,
160
+ * known options and no option twice. Options come back in the order
161
+ * written. Returns undefined for anything else.
162
+ */
163
+ export function readShowWhen(source, environment) {
164
+ const parsed = parseExpression(source);
165
+ if (!parsed.ok)
166
+ return undefined;
167
+ const expression = parsed.expression;
168
+ const boolean = (name) => environment.propertyType(name)?.kind === "boolean";
169
+ if (expression.kind === "name") {
170
+ return boolean(expression.name) ? { kind: "boolean", property: expression.name, on: true } : undefined;
171
+ }
172
+ if (expression.kind === "unary" && expression.operator === "not" && expression.operand.kind === "name") {
173
+ return boolean(expression.operand.name) ? { kind: "boolean", property: expression.operand.name, on: false } : undefined;
174
+ }
175
+ const comparisons = orOperands(expression).map(enumComparison);
176
+ const first = comparisons[0];
177
+ if (first === undefined)
178
+ return undefined;
179
+ const type = environment.propertyType(first.property);
180
+ if (type?.kind !== "enum")
181
+ return undefined;
182
+ const options = [];
183
+ for (const comparison of comparisons) {
184
+ if (comparison === undefined || comparison.property !== first.property)
185
+ return undefined;
186
+ if (!type.options.includes(comparison.option) || options.includes(comparison.option))
187
+ return undefined;
188
+ options.push(comparison.option);
189
+ }
190
+ return { kind: "enum", property: first.property, options };
191
+ }
192
+ /** Writes a "Show when" condition. Throws for an enum condition without options, which would never show the layer. */
193
+ export function writeShowWhen(condition) {
194
+ const property = formatExpressionName(condition.property);
195
+ if (condition.kind === "boolean")
196
+ return condition.on ? property : `not ${property}`;
197
+ if (condition.options.length === 0)
198
+ throw new RangeError("A \"Show when\" condition needs at least one option.");
199
+ return condition.options.map((option) => `${property} == .${formatExpressionName(option)}`).join(" or ");
200
+ }
@@ -0,0 +1,14 @@
1
+ export * from "./ast.ts";
2
+ export * from "./types.ts";
3
+ export { EXPRESSION_KEYWORDS, parseColorLiteral, type ExpressionKeyword } from "./lexer.ts";
4
+ export { EXPRESSION_NAME_MAX_LENGTH, expressionNameProblem, formatExpressionName, isBareExpressionName, isExpressionNamePart, isExpressionNameStart, normalizeExpressionName, } from "./names.ts";
5
+ export * from "./parser.ts";
6
+ export * from "./library.ts";
7
+ export * from "./check.ts";
8
+ export * from "./rename.ts";
9
+ export * from "./values.ts";
10
+ export * from "./evaluate.ts";
11
+ export * from "./cache.ts";
12
+ export * from "./source.ts";
13
+ export * from "./forms.ts";
14
+ export * from "./completion.ts";
@@ -0,0 +1,17 @@
1
+ // The component expression language (docs/components/design.md 8.4 to 8.6):
2
+ // syntax tree, types, parser, type checker, the rename rewrite, values, the
3
+ // evaluator with its standard library, and the parse-and-check cache.
4
+ export * from "./ast.js";
5
+ export * from "./types.js";
6
+ export { EXPRESSION_KEYWORDS, parseColorLiteral } from "./lexer.js";
7
+ export { EXPRESSION_NAME_MAX_LENGTH, expressionNameProblem, formatExpressionName, isBareExpressionName, isExpressionNamePart, isExpressionNameStart, normalizeExpressionName, } from "./names.js";
8
+ export * from "./parser.js";
9
+ export * from "./library.js";
10
+ export * from "./check.js";
11
+ export * from "./rename.js";
12
+ export * from "./values.js";
13
+ export * from "./evaluate.js";
14
+ export * from "./cache.js";
15
+ export * from "./source.js";
16
+ export * from "./forms.js";
17
+ export * from "./completion.js";
@@ -0,0 +1,42 @@
1
+ import type { ExpressionColor, ExpressionError, ExpressionSpan, ExpressionUnit } from "./ast.ts";
2
+ import { EXPRESSION_KEYWORDS, type ExpressionKeyword } from "./names.ts";
3
+ export { EXPRESSION_KEYWORDS, type ExpressionKeyword };
4
+ export type ExpressionPunctuation = "(" | ")" | "[" | "]" | "{" | "}" | "," | ":" | "." | "=" | "=>" | "==" | "!=" | "<" | "<=" | ">" | ">=" | "++" | "+" | "-" | "*" | "/";
5
+ export type ExpressionToken = {
6
+ readonly kind: "number";
7
+ readonly value: number;
8
+ readonly unit?: ExpressionUnit;
9
+ readonly span: ExpressionSpan;
10
+ } | {
11
+ readonly kind: "string";
12
+ readonly value: string;
13
+ readonly span: ExpressionSpan;
14
+ } | {
15
+ readonly kind: "color";
16
+ readonly value: ExpressionColor;
17
+ readonly span: ExpressionSpan;
18
+ } | {
19
+ readonly kind: "name";
20
+ readonly value: string;
21
+ readonly span: ExpressionSpan;
22
+ } | {
23
+ readonly kind: "keyword";
24
+ readonly value: ExpressionKeyword;
25
+ readonly span: ExpressionSpan;
26
+ } | {
27
+ readonly kind: "punctuation";
28
+ readonly value: ExpressionPunctuation;
29
+ readonly span: ExpressionSpan;
30
+ } | {
31
+ readonly kind: "end";
32
+ readonly span: ExpressionSpan;
33
+ };
34
+ /** Thrown by the lexer and parser; `parseExpression` catches it and reports the error. */
35
+ export declare class ExpressionSyntaxError extends Error {
36
+ readonly error: ExpressionError;
37
+ constructor(error: ExpressionError);
38
+ }
39
+ /** Reads a `#rgb`, `#rgba`, `#rrggbb` or `#rrggbbaa` literal into channels from 0 to 1. */
40
+ export declare function parseColorLiteral(text: string): ExpressionColor | undefined;
41
+ /** Splits the source into tokens; the last token is always `end`. */
42
+ export declare function tokenizeExpression(source: string): ExpressionToken[];
@@ -0,0 +1,213 @@
1
+ // Turns expression source text into tokens. Throws ExpressionSyntaxError on
2
+ // a character or literal it cannot read; the parser turns that into a result.
3
+ import { EXPRESSION_KEYWORDS, expressionNameProblem, isExpressionNamePart, isExpressionNameStart, normalizeExpressionName, } from "./names.js";
4
+ export { EXPRESSION_KEYWORDS };
5
+ /** Thrown by the lexer and parser; `parseExpression` catches it and reports the error. */
6
+ export class ExpressionSyntaxError extends Error {
7
+ error;
8
+ constructor(error) {
9
+ super(error.message);
10
+ this.name = "ExpressionSyntaxError";
11
+ this.error = error;
12
+ }
13
+ }
14
+ const KEYWORDS = new Set(EXPRESSION_KEYWORDS);
15
+ const PUNCTUATION_2 = ["=>", "==", "!=", "<=", ">=", "++"];
16
+ const PUNCTUATION_1 = "()[]{},:.=<>+-*/";
17
+ /** The code point at `i` as a string (two UTF-16 units for one outside the BMP), or "" at the end. */
18
+ function codePointAt(source, i) {
19
+ const code = source.codePointAt(i);
20
+ return code === undefined ? "" : String.fromCodePoint(code);
21
+ }
22
+ function isAsciiNamePart(ch) {
23
+ return /^[A-Za-z0-9_]$/.test(ch);
24
+ }
25
+ /** The end of the bare name part that starts at `i`: past every code point that can continue a name. */
26
+ function namePartEnd(source, i) {
27
+ let end = i;
28
+ for (let ch = codePointAt(source, end); ch !== "" && isExpressionNamePart(ch); ch = codePointAt(source, end))
29
+ end += ch.length;
30
+ return end;
31
+ }
32
+ const BACKTICK_HINT = "A name with spaces or other characters is written in backticks, as `` `Primary / Hover` ``.";
33
+ /**
34
+ * Reads the name in backticks that starts at `start`. Inside, `` \` `` and
35
+ * `\\` are the only escapes; the name is returned in NFC.
36
+ */
37
+ function readQuotedName(source, start) {
38
+ let value = "";
39
+ let i = start + 1;
40
+ for (;;) {
41
+ if (i >= source.length)
42
+ fail("This name in backticks never closes.", start, source.length, "Add a closing `` ` ``.");
43
+ const c = source[i];
44
+ if (c === "`") {
45
+ i += 1;
46
+ break;
47
+ }
48
+ if (c === "\\") {
49
+ const next = source[i + 1];
50
+ if (next === "`" || next === "\\")
51
+ value += next;
52
+ else
53
+ fail(`Unknown escape \`\\${next ?? ""}\` in a name.`, i, Math.min(i + 2, source.length), "In backticks the escapes are `` \\` `` and `\\\\`.");
54
+ i += 2;
55
+ continue;
56
+ }
57
+ value += c;
58
+ i += 1;
59
+ }
60
+ // No length limit here: an expression only reads names, and a stored one may be longer than a new name can be.
61
+ const problem = expressionNameProblem(value, { stored: true });
62
+ if (problem !== undefined)
63
+ fail(`${problem}.`, start, i);
64
+ return { value: normalizeExpressionName(value), end: i };
65
+ }
66
+ function isDigit(ch) {
67
+ return ch >= "0" && ch <= "9";
68
+ }
69
+ function fail(message, start, end, hint) {
70
+ throw new ExpressionSyntaxError(hint === undefined ? { message, span: { start, end } } : { message, span: { start, end }, hint });
71
+ }
72
+ /** Reads a `#rgb`, `#rgba`, `#rrggbb` or `#rrggbbaa` literal into channels from 0 to 1. */
73
+ export function parseColorLiteral(text) {
74
+ const hex = text.slice(1);
75
+ if (!/^[0-9A-Fa-f]+$/.test(hex))
76
+ return undefined;
77
+ let digits;
78
+ if (hex.length === 3 || hex.length === 4)
79
+ digits = [...hex].map((d) => d + d).join("");
80
+ else if (hex.length === 6 || hex.length === 8)
81
+ digits = hex;
82
+ else
83
+ return undefined;
84
+ const channel = (i) => parseInt(digits.slice(i * 2, i * 2 + 2), 16) / 255;
85
+ return { red: channel(0), green: channel(1), blue: channel(2), alpha: digits.length === 8 ? channel(3) : 1 };
86
+ }
87
+ /** Splits the source into tokens; the last token is always `end`. */
88
+ export function tokenizeExpression(source) {
89
+ const tokens = [];
90
+ let i = 0;
91
+ while (i < source.length) {
92
+ const ch = source[i];
93
+ if (ch === " " || ch === "\t" || ch === "\n" || ch === "\r") {
94
+ i += 1;
95
+ continue;
96
+ }
97
+ // A line comment runs to the end of the line and is kept in the stored source.
98
+ if (ch === "/" && source[i + 1] === "/") {
99
+ while (i < source.length && source[i] !== "\n")
100
+ i += 1;
101
+ continue;
102
+ }
103
+ const start = i;
104
+ if (isDigit(ch)) {
105
+ while (i < source.length && isDigit(source[i]))
106
+ i += 1;
107
+ if (source[i] === "." && isDigit(source[i + 1] ?? "")) {
108
+ i += 1;
109
+ while (i < source.length && isDigit(source[i]))
110
+ i += 1;
111
+ }
112
+ const value = Number(source.slice(start, i));
113
+ let unit;
114
+ if (source.startsWith("px", i))
115
+ unit = "px";
116
+ else if (source.startsWith("deg", i))
117
+ unit = "deg";
118
+ else if (source[i] === "%")
119
+ unit = "%";
120
+ if (unit !== undefined)
121
+ i += unit.length;
122
+ if (namePartEnd(source, i) > i) {
123
+ i = namePartEnd(source, i);
124
+ fail(`Unknown unit in \`${source.slice(start, i)}\`.`, start, i, "The units are `px`, `%` and `deg`. A name that starts with a digit is written in backticks, as `` `2x` ``.");
125
+ }
126
+ if (!Number.isFinite(value))
127
+ fail(`\`${source.slice(start, i)}\` is too large for a number.`, start, i);
128
+ tokens.push(unit === undefined ? { kind: "number", value, span: { start, end: i } } : { kind: "number", value, unit, span: { start, end: i } });
129
+ continue;
130
+ }
131
+ if (ch === '"') {
132
+ let value = "";
133
+ i += 1;
134
+ for (;;) {
135
+ if (i >= source.length)
136
+ fail("This string never closes.", start, source.length, 'Add a closing `"`.');
137
+ const c = source[i];
138
+ if (c === '"') {
139
+ i += 1;
140
+ break;
141
+ }
142
+ if (c === "\\") {
143
+ const next = source[i + 1];
144
+ if (next === '"' || next === "\\")
145
+ value += next;
146
+ else if (next === "n")
147
+ value += "\n";
148
+ else
149
+ fail(`Unknown escape \`\\${next ?? ""}\`.`, i, Math.min(i + 2, source.length), 'The escapes are `\\"`, `\\\\` and `\\n`.');
150
+ i += 2;
151
+ continue;
152
+ }
153
+ value += c;
154
+ i += 1;
155
+ }
156
+ tokens.push({ kind: "string", value, span: { start, end: i } });
157
+ continue;
158
+ }
159
+ if (ch === "#") {
160
+ i += 1;
161
+ while (i < source.length && isAsciiNamePart(source[i]))
162
+ i += 1;
163
+ const text = source.slice(start, i);
164
+ const color = parseColorLiteral(text);
165
+ if (color === undefined)
166
+ fail(`\`${text}\` is not a colour.`, start, i, "Write `#rgb`, `#rgba`, `#rrggbb` or `#rrggbbaa`.");
167
+ tokens.push({ kind: "color", value: color, span: { start, end: i } });
168
+ continue;
169
+ }
170
+ const point = codePointAt(source, i);
171
+ if (isExpressionNameStart(point)) {
172
+ i = namePartEnd(source, i + point.length);
173
+ const written = source.slice(start, i);
174
+ const span = { start, end: i };
175
+ if (KEYWORDS.has(written)) {
176
+ tokens.push({ kind: "keyword", value: written, span });
177
+ continue;
178
+ }
179
+ // A bare name holds no character a name may not; it may be longer than a new name can be, as one an older document stores.
180
+ tokens.push({ kind: "name", value: normalizeExpressionName(written), span });
181
+ continue;
182
+ }
183
+ if (ch === "`") {
184
+ const { value, end } = readQuotedName(source, start);
185
+ i = end;
186
+ tokens.push({ kind: "name", value, span: { start, end: i } });
187
+ continue;
188
+ }
189
+ const two = source.slice(i, i + 2);
190
+ if (PUNCTUATION_2.includes(two)) {
191
+ i += 2;
192
+ tokens.push({ kind: "punctuation", value: two, span: { start, end: i } });
193
+ continue;
194
+ }
195
+ if (PUNCTUATION_1.includes(ch)) {
196
+ i += 1;
197
+ if (ch === "." && isDigit(source[i] ?? "")) {
198
+ while (i < source.length && isDigit(source[i]))
199
+ i += 1;
200
+ fail(`\`${source.slice(start, i)}\` is not a number.`, start, i, `Write \`0${source.slice(start, i)}\`.`);
201
+ }
202
+ tokens.push({ kind: "punctuation", value: ch, span: { start, end: i } });
203
+ continue;
204
+ }
205
+ if (expressionNameProblem(point) !== undefined) {
206
+ const code = point.codePointAt(0).toString(16).toUpperCase().padStart(4, "0");
207
+ fail(`Unexpected character U+${code}.`, start, i + point.length, "Control, line break and bidirectional formatting characters can only be in a string, never in a name.");
208
+ }
209
+ fail(`Unexpected character \`${point}\`.`, start, i + point.length, BACKTICK_HINT);
210
+ }
211
+ tokens.push({ kind: "end", span: { start: source.length, end: source.length } });
212
+ return tokens;
213
+ }
@@ -0,0 +1,33 @@
1
+ import { type ExpressionType } from "./types.ts";
2
+ /** One parameter of a signature. A parameter with a default may be left out. */
3
+ export interface ExpressionParameter {
4
+ readonly name: string;
5
+ readonly type: ExpressionType;
6
+ /** The default, as expression source text (`0px`, `.center`); absent for a required parameter. */
7
+ readonly defaultSource?: string;
8
+ }
9
+ /** One way to call a function: its parameters, in order, and its result type. */
10
+ export interface ExpressionSignature {
11
+ readonly parameters: readonly ExpressionParameter[];
12
+ /** When set, any number of extra positional arguments of this type may follow the parameters. */
13
+ readonly rest?: ExpressionType;
14
+ readonly result: ExpressionType;
15
+ }
16
+ /** A constructor or standard library function with its overloads, tried in order. */
17
+ export interface ExpressionFunction {
18
+ readonly name: string;
19
+ readonly group: "numbers" | "colours" | "strings" | "constructors";
20
+ readonly summary: string;
21
+ readonly overloads: readonly ExpressionSignature[];
22
+ }
23
+ /** Every constructor and standard library function, by name. A null prototype keeps `toString` and the like out of lookups. */
24
+ export declare const EXPRESSION_LIBRARY: Readonly<Record<string, ExpressionFunction>>;
25
+ /**
26
+ * True when this many positional arguments plus these named ones fill the
27
+ * signature without gaps or extras: every parameter is given once, by
28
+ * position, by name or by its default. The checker and the evaluator pick
29
+ * an overload with it.
30
+ */
31
+ export declare function expressionSignatureAccepts(signature: ExpressionSignature, positionalCount: number, named: ReadonlySet<string>): boolean;
32
+ /** A signature as the language writes it: `min(a: number, b: number, ...) -> number`. */
33
+ export declare function formatExpressionSignature(name: string, signature: ExpressionSignature): string;
@@ -0,0 +1,117 @@
1
+ // The signatures of every constructor and standard library function
2
+ // (docs/components/design.md 8.4). The checker resolves calls against them;
3
+ // the evaluator (part 2) implements them and reads the defaults from here.
4
+ import { EXPRESSION_TYPES as T, STROKE_ALIGN_TYPE, expressionEnumType, formatExpressionType } from "./types.js";
5
+ const NUMERIC_TYPES = [T.number, T.length, T.percent, T.angle];
6
+ function p(name, type, defaultSource) {
7
+ return defaultSource === undefined ? { name, type } : { name, type, defaultSource };
8
+ }
9
+ function sig(parameters, result, rest) {
10
+ return rest === undefined ? { parameters, result } : { parameters, result, rest };
11
+ }
12
+ /** One overload per numeric type, all with the same shape. */
13
+ function numeric(shape) {
14
+ return NUMERIC_TYPES.map(shape);
15
+ }
16
+ function fn(name, group, summary, overloads) {
17
+ return { name, group, summary, overloads };
18
+ }
19
+ const SHADOW_PARAMETERS = [
20
+ p("x", T.length, "0px"),
21
+ p("y", T.length, "0px"),
22
+ p("blur", T.length, "0px"),
23
+ p("spread", T.length, "0px"),
24
+ p("color", T.color, "#00000040"),
25
+ ];
26
+ const FUNCTIONS = [
27
+ // Numbers and unit types
28
+ fn("min", "numbers", "The smallest of two or more values of one numeric type.", numeric((t) => sig([p("a", t), p("b", t)], t, t))),
29
+ fn("max", "numbers", "The largest of two or more values of one numeric type.", numeric((t) => sig([p("a", t), p("b", t)], t, t))),
30
+ fn("clamp", "numbers", "`x` held between `low` and `high`.", numeric((t) => sig([p("x", t), p("low", t), p("high", t)], t))),
31
+ fn("round", "numbers", "Rounds to the nearest whole number, halves away from zero.", numeric((t) => sig([p("x", t)], t))),
32
+ fn("floor", "numbers", "Rounds down.", numeric((t) => sig([p("x", t)], t))),
33
+ fn("ceil", "numbers", "Rounds up.", numeric((t) => sig([p("x", t)], t))),
34
+ fn("abs", "numbers", "The value without its sign.", numeric((t) => sig([p("x", t)], t))),
35
+ // Colours
36
+ fn("mix", "colours", "A straight sRGB blend of two colours; `t` from 0 (all `a`) to 1 (all `b`).", [
37
+ sig([p("a", T.color), p("b", T.color), p("t", T.number)], T.color),
38
+ ]),
39
+ fn("with_alpha", "colours", "The colour with its alpha replaced.", [sig([p("color", T.color), p("alpha", T.number)], T.color)]),
40
+ fn("lighten", "colours", "Raises the OKLCH lightness by `amount`.", [sig([p("color", T.color), p("amount", T.percent)], T.color)]),
41
+ fn("darken", "colours", "Lowers the OKLCH lightness by `amount`.", [sig([p("color", T.color), p("amount", T.percent)], T.color)]),
42
+ fn("rgb", "colours", "A colour from channels 0 to 255.", [sig([p("red", T.number), p("green", T.number), p("blue", T.number)], T.color)]),
43
+ fn("rgba", "colours", "A colour from channels 0 to 255 and an alpha 0 to 1.", [
44
+ sig([p("red", T.number), p("green", T.number), p("blue", T.number), p("alpha", T.number)], T.color),
45
+ ]),
46
+ // Strings
47
+ fn("text", "strings", "A number written as text, with an optional number of decimals.", [
48
+ sig([p("value", T.number)], T.string),
49
+ sig([p("value", T.number), p("decimals", T.number)], T.string),
50
+ ]),
51
+ fn("upper", "strings", "The string in upper case.", [sig([p("value", T.string)], T.string)]),
52
+ fn("lower", "strings", "The string in lower case.", [sig([p("value", T.string)], T.string)]),
53
+ // Constructors
54
+ fn("point", "constructors", "A point from two lengths.", [sig([p("x", T.length), p("y", T.length)], T.point)]),
55
+ fn("size", "constructors", "A size from two lengths.", [sig([p("width", T.length), p("height", T.length)], T.size)]),
56
+ fn("insets", "constructors", "Insets from one length (all sides), two (vertical, horizontal) or four (top, right, bottom, left).", [
57
+ sig([p("all", T.length)], T.insets),
58
+ sig([p("vertical", T.length), p("horizontal", T.length)], T.insets),
59
+ sig([p("top", T.length), p("right", T.length), p("bottom", T.length), p("left", T.length)], T.insets),
60
+ ]),
61
+ fn("corners", "constructors", "Corner radii from one length (all corners) or four (top left, top right, bottom right, bottom left).", [
62
+ sig([p("all", T.length)], T.corners),
63
+ sig([p("top_left", T.length), p("top_right", T.length), p("bottom_right", T.length), p("bottom_left", T.length)], T.corners),
64
+ ]),
65
+ fn("image", "constructors", "An image paint from an asset ID.", [sig([p("asset", T.string), p("fit", expressionEnumType("image fit", ["contain", "cover", "fill"]), ".cover")], T.paint)]),
66
+ fn("solid", "constructors", "A solid paint.", [sig([p("color", T.color), p("opacity", T.number, "1")], T.paint)]),
67
+ fn("linear", "constructors", "A linear gradient paint at an angle, through two or more evenly spaced colours.", [
68
+ sig([p("angle", T.angle), p("from", T.color), p("to", T.color)], T.paint, T.color),
69
+ ]),
70
+ fn("radial", "constructors", "A radial gradient paint from the centre outwards, through two or more evenly spaced colours.", [
71
+ sig([p("from", T.color), p("to", T.color)], T.paint, T.color),
72
+ ]),
73
+ fn("angular", "constructors", "An angular gradient paint starting at an angle, through two or more evenly spaced colours.", [
74
+ sig([p("angle", T.angle), p("from", T.color), p("to", T.color)], T.paint, T.color),
75
+ ]),
76
+ fn("stroke", "constructors", "A stroke from a paint, a width and an alignment.", [
77
+ sig([p("paint", T.paint), p("width", T.length, "1px"), p("align", STROKE_ALIGN_TYPE, ".center")], T.stroke),
78
+ ]),
79
+ fn("shadow", "constructors", "A drop shadow.", [sig(SHADOW_PARAMETERS, T.effect)]),
80
+ fn("inner_shadow", "constructors", "An inner shadow.", [sig(SHADOW_PARAMETERS, T.effect)]),
81
+ fn("layer_blur", "constructors", "A blur of the layer itself.", [sig([p("radius", T.length)], T.effect)]),
82
+ fn("background_blur", "constructors", "A blur of what lies behind the layer.", [sig([p("radius", T.length)], T.effect)]),
83
+ fn("text_style", "constructors", "A text style; every part but the family has a default.", [
84
+ sig([
85
+ p("family", T.string),
86
+ p("weight", T.number, "400"),
87
+ p("size", T.length, "16px"),
88
+ p("line_height", T.length, "24px"),
89
+ p("letter_spacing", T.length, "0px"),
90
+ ], T.text_style),
91
+ ]),
92
+ ];
93
+ /** Every constructor and standard library function, by name. A null prototype keeps `toString` and the like out of lookups. */
94
+ export const EXPRESSION_LIBRARY = Object.assign(Object.create(null), Object.fromEntries(FUNCTIONS.map((f) => [f.name, f])));
95
+ /**
96
+ * True when this many positional arguments plus these named ones fill the
97
+ * signature without gaps or extras: every parameter is given once, by
98
+ * position, by name or by its default. The checker and the evaluator pick
99
+ * an overload with it.
100
+ */
101
+ export function expressionSignatureAccepts(signature, positionalCount, named) {
102
+ if (positionalCount > signature.parameters.length && signature.rest === undefined)
103
+ return false;
104
+ for (const name of named) {
105
+ const index = signature.parameters.findIndex((param) => param.name === name);
106
+ if (index < 0 || index < positionalCount)
107
+ return false;
108
+ }
109
+ return signature.parameters.every((param, index) => index < positionalCount || param.defaultSource !== undefined || named.has(param.name));
110
+ }
111
+ /** A signature as the language writes it: `min(a: number, b: number, ...) -> number`. */
112
+ export function formatExpressionSignature(name, signature) {
113
+ const parts = signature.parameters.map((param) => `${param.name}: ${formatExpressionType(param.type)}${param.defaultSource === undefined ? "" : ` = ${param.defaultSource}`}`);
114
+ if (signature.rest !== undefined)
115
+ parts.push("...");
116
+ return `${name}(${parts.join(", ")}) -> ${formatExpressionType(signature.result)}`;
117
+ }