@native-sdk/cli 0.0.0 → 0.4.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 (367) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +50 -1
  3. package/app.zon +30 -0
  4. package/bin/native.js +156 -0
  5. package/build/app.zig +716 -0
  6. package/build.zig +2593 -0
  7. package/build.zig.zon +24 -0
  8. package/native-sdk.d.ts +615 -0
  9. package/package.json +44 -8
  10. package/skill-data/automation/SKILL.md +248 -0
  11. package/skill-data/core/SKILL.md +249 -0
  12. package/skill-data/core/references/app-model-runtime.md +141 -0
  13. package/skill-data/core/references/bridge-security-native-capabilities.md +230 -0
  14. package/skill-data/core/references/frontend-assets.md +124 -0
  15. package/skill-data/core/references/project-anatomy.md +108 -0
  16. package/skill-data/core/references/web-engines-packaging-debugging.md +199 -0
  17. package/skill-data/native-ui/SKILL.md +1080 -0
  18. package/skills/native-sdk/SKILL.md +33 -0
  19. package/src/app_runner/root.zig +740 -0
  20. package/src/assets/root.zig +20 -0
  21. package/src/automation/protocol.zig +285 -0
  22. package/src/automation/root.zig +13 -0
  23. package/src/automation/server.zig +506 -0
  24. package/src/automation/snapshot.zig +1202 -0
  25. package/src/automation/watcher.zig +165 -0
  26. package/src/bridge/root.zig +507 -0
  27. package/src/debug/root.zig +246 -0
  28. package/src/embed/app_exports.zig +11 -0
  29. package/src/embed/c_api.zig +793 -0
  30. package/src/embed/c_exports.zig +10 -0
  31. package/src/embed/chrome.zig +168 -0
  32. package/src/embed/conversions.zig +359 -0
  33. package/src/embed/host.zig +960 -0
  34. package/src/embed/root.zig +81 -0
  35. package/src/embed/tests.zig +2552 -0
  36. package/src/embed/types.zig +382 -0
  37. package/src/embed/ui_host.zig +289 -0
  38. package/src/extensions/root.zig +197 -0
  39. package/src/frontend/root.zig +48 -0
  40. package/src/js/root.zig +65 -0
  41. package/src/platform/android/NativeSdkActivity.java +1370 -0
  42. package/src/platform/android/android_host.c +696 -0
  43. package/src/platform/android/files.zig +9 -0
  44. package/src/platform/android/native_sdk_app.h +237 -0
  45. package/src/platform/ios/files.zig +8 -0
  46. package/src/platform/ios/native_sdk_app.h +289 -0
  47. package/src/platform/ios/uikit_host.m +2739 -0
  48. package/src/platform/linux/cef_host.cpp +599 -0
  49. package/src/platform/linux/gtk_host.c +5362 -0
  50. package/src/platform/linux/gtk_host.h +265 -0
  51. package/src/platform/linux/root.zig +1519 -0
  52. package/src/platform/macos/appkit_host.h +604 -0
  53. package/src/platform/macos/appkit_host.m +10392 -0
  54. package/src/platform/macos/automation_host.h +14 -0
  55. package/src/platform/macos/automation_host.m +12 -0
  56. package/src/platform/macos/cef_host.mm +3028 -0
  57. package/src/platform/macos/root.zig +2260 -0
  58. package/src/platform/null_platform.zig +2232 -0
  59. package/src/platform/null_platform_tests.zig +670 -0
  60. package/src/platform/policy_values.zig +14 -0
  61. package/src/platform/root.zig +181 -0
  62. package/src/platform/types.zig +2681 -0
  63. package/src/platform/windows/cef_host.cpp +3 -0
  64. package/src/platform/windows/root.zig +1521 -0
  65. package/src/platform/windows/webview2_host.cpp +6230 -0
  66. package/src/primitives/app_dirs/root.zig +440 -0
  67. package/src/primitives/app_manifest/root.zig +109 -0
  68. package/src/primitives/app_manifest/tests.zig +758 -0
  69. package/src/primitives/app_manifest/types.zig +587 -0
  70. package/src/primitives/app_manifest/validation.zig +828 -0
  71. package/src/primitives/assets/root.zig +520 -0
  72. package/src/primitives/canvas/a11y_audit.zig +390 -0
  73. package/src/primitives/canvas/a11y_audit_tests.zig +225 -0
  74. package/src/primitives/canvas/app_icon.zig +1590 -0
  75. package/src/primitives/canvas/chart.zig +392 -0
  76. package/src/primitives/canvas/chart_tests.zig +751 -0
  77. package/src/primitives/canvas/commands.zig +446 -0
  78. package/src/primitives/canvas/drawing.zig +309 -0
  79. package/src/primitives/canvas/equality.zig +316 -0
  80. package/src/primitives/canvas/events.zig +696 -0
  81. package/src/primitives/canvas/font_coverage.zig +118 -0
  82. package/src/primitives/canvas/font_ttf.zig +510 -0
  83. package/src/primitives/canvas/font_ttf_tests.zig +280 -0
  84. package/src/primitives/canvas/fonts/Geist-Regular.ttf +0 -0
  85. package/src/primitives/canvas/fonts/GeistMono-Regular.ttf +0 -0
  86. package/src/primitives/canvas/frame.zig +679 -0
  87. package/src/primitives/canvas/frame_metrics.zig +462 -0
  88. package/src/primitives/canvas/gpu.zig +639 -0
  89. package/src/primitives/canvas/hash.zig +112 -0
  90. package/src/primitives/canvas/icons/alert.svg +1 -0
  91. package/src/primitives/canvas/icons/archive.svg +1 -0
  92. package/src/primitives/canvas/icons/arrow-down.svg +1 -0
  93. package/src/primitives/canvas/icons/arrow-right.svg +1 -0
  94. package/src/primitives/canvas/icons/arrow-up.svg +1 -0
  95. package/src/primitives/canvas/icons/check-circle.svg +1 -0
  96. package/src/primitives/canvas/icons/check.svg +1 -0
  97. package/src/primitives/canvas/icons/chevron-down.svg +1 -0
  98. package/src/primitives/canvas/icons/chevron-left.svg +1 -0
  99. package/src/primitives/canvas/icons/chevron-right.svg +1 -0
  100. package/src/primitives/canvas/icons/chevron-up.svg +1 -0
  101. package/src/primitives/canvas/icons/circle-dot.svg +1 -0
  102. package/src/primitives/canvas/icons/clock.svg +1 -0
  103. package/src/primitives/canvas/icons/copy.svg +1 -0
  104. package/src/primitives/canvas/icons/download.svg +1 -0
  105. package/src/primitives/canvas/icons/edit.svg +1 -0
  106. package/src/primitives/canvas/icons/ellipsis.svg +1 -0
  107. package/src/primitives/canvas/icons/external-link.svg +1 -0
  108. package/src/primitives/canvas/icons/eye.svg +1 -0
  109. package/src/primitives/canvas/icons/file-text.svg +1 -0
  110. package/src/primitives/canvas/icons/folder-open.svg +1 -0
  111. package/src/primitives/canvas/icons/folder.svg +1 -0
  112. package/src/primitives/canvas/icons/git-branch.svg +1 -0
  113. package/src/primitives/canvas/icons/git-merge.svg +1 -0
  114. package/src/primitives/canvas/icons/git-pull-request.svg +1 -0
  115. package/src/primitives/canvas/icons/info.svg +1 -0
  116. package/src/primitives/canvas/icons/menu.svg +1 -0
  117. package/src/primitives/canvas/icons/missing.svg +1 -0
  118. package/src/primitives/canvas/icons/moon.svg +1 -0
  119. package/src/primitives/canvas/icons/music.svg +1 -0
  120. package/src/primitives/canvas/icons/panel-left.svg +1 -0
  121. package/src/primitives/canvas/icons/panel-right.svg +1 -0
  122. package/src/primitives/canvas/icons/pause.svg +1 -0
  123. package/src/primitives/canvas/icons/play.svg +1 -0
  124. package/src/primitives/canvas/icons/plus.svg +1 -0
  125. package/src/primitives/canvas/icons/refresh-cw.svg +1 -0
  126. package/src/primitives/canvas/icons/repeat.svg +1 -0
  127. package/src/primitives/canvas/icons/save.svg +1 -0
  128. package/src/primitives/canvas/icons/search.svg +1 -0
  129. package/src/primitives/canvas/icons/send.svg +1 -0
  130. package/src/primitives/canvas/icons/settings.svg +1 -0
  131. package/src/primitives/canvas/icons/shuffle.svg +1 -0
  132. package/src/primitives/canvas/icons/skip-back.svg +1 -0
  133. package/src/primitives/canvas/icons/skip-forward.svg +1 -0
  134. package/src/primitives/canvas/icons/sun.svg +1 -0
  135. package/src/primitives/canvas/icons/terminal.svg +1 -0
  136. package/src/primitives/canvas/icons/trash.svg +1 -0
  137. package/src/primitives/canvas/icons/volume.svg +1 -0
  138. package/src/primitives/canvas/icons/wrench.svg +1 -0
  139. package/src/primitives/canvas/icons/x-circle.svg +1 -0
  140. package/src/primitives/canvas/icons/x.svg +1 -0
  141. package/src/primitives/canvas/icons.zig +249 -0
  142. package/src/primitives/canvas/layout_audit.zig +744 -0
  143. package/src/primitives/canvas/layout_audit_tests.zig +426 -0
  144. package/src/primitives/canvas/markdown.zig +1031 -0
  145. package/src/primitives/canvas/markdown_hostile_tests.zig +471 -0
  146. package/src/primitives/canvas/markdown_tests.zig +538 -0
  147. package/src/primitives/canvas/plan_key_index.zig +120 -0
  148. package/src/primitives/canvas/png.zig +355 -0
  149. package/src/primitives/canvas/reference.zig +1457 -0
  150. package/src/primitives/canvas/reference_blur.zig +104 -0
  151. package/src/primitives/canvas/reference_memo.zig +322 -0
  152. package/src/primitives/canvas/reference_paths.zig +184 -0
  153. package/src/primitives/canvas/reference_tests.zig +1650 -0
  154. package/src/primitives/canvas/render.zig +747 -0
  155. package/src/primitives/canvas/render_effects.zig +255 -0
  156. package/src/primitives/canvas/render_fingerprints.zig +256 -0
  157. package/src/primitives/canvas/render_generic_resources.zig +376 -0
  158. package/src/primitives/canvas/render_images.zig +247 -0
  159. package/src/primitives/canvas/render_layers.zig +260 -0
  160. package/src/primitives/canvas/render_paths.zig +347 -0
  161. package/src/primitives/canvas/render_resources.zig +51 -0
  162. package/src/primitives/canvas/render_tests.zig +3186 -0
  163. package/src/primitives/canvas/root.zig +744 -0
  164. package/src/primitives/canvas/serialization.zig +1528 -0
  165. package/src/primitives/canvas/svg_icon.zig +636 -0
  166. package/src/primitives/canvas/svg_icon_tests.zig +312 -0
  167. package/src/primitives/canvas/test_support.zig +422 -0
  168. package/src/primitives/canvas/testdata/markdown_document.md +83 -0
  169. package/src/primitives/canvas/tests.zig +30 -0
  170. package/src/primitives/canvas/text.zig +82 -0
  171. package/src/primitives/canvas/text_atlas.zig +408 -0
  172. package/src/primitives/canvas/text_batch_tests.zig +431 -0
  173. package/src/primitives/canvas/text_interaction.zig +615 -0
  174. package/src/primitives/canvas/text_layout.zig +1069 -0
  175. package/src/primitives/canvas/text_layout_cache.zig +281 -0
  176. package/src/primitives/canvas/text_layout_hash.zig +113 -0
  177. package/src/primitives/canvas/text_layout_types.zig +128 -0
  178. package/src/primitives/canvas/text_measure_cache.zig +279 -0
  179. package/src/primitives/canvas/text_metrics.zig +309 -0
  180. package/src/primitives/canvas/text_metrics_tests.zig +262 -0
  181. package/src/primitives/canvas/text_span_tests.zig +608 -0
  182. package/src/primitives/canvas/text_spans.zig +1010 -0
  183. package/src/primitives/canvas/text_tests.zig +1949 -0
  184. package/src/primitives/canvas/themes/geist.zig +470 -0
  185. package/src/primitives/canvas/tokens.zig +1522 -0
  186. package/src/primitives/canvas/ui.zig +2686 -0
  187. package/src/primitives/canvas/ui_markup.zig +3791 -0
  188. package/src/primitives/canvas/ui_markup_binary.zig +522 -0
  189. package/src/primitives/canvas/ui_markup_binary_tests.zig +591 -0
  190. package/src/primitives/canvas/ui_markup_compiled.zig +2565 -0
  191. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1669 -0
  192. package/src/primitives/canvas/ui_markup_contract.zig +1620 -0
  193. package/src/primitives/canvas/ui_markup_contract_tests.zig +953 -0
  194. package/src/primitives/canvas/ui_markup_edit.zig +416 -0
  195. package/src/primitives/canvas/ui_markup_edit_tests.zig +341 -0
  196. package/src/primitives/canvas/ui_markup_expr.zig +1152 -0
  197. package/src/primitives/canvas/ui_markup_expr_tests.zig +779 -0
  198. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +505 -0
  199. package/src/primitives/canvas/ui_markup_reflect.zig +127 -0
  200. package/src/primitives/canvas/ui_markup_tests.zig +1545 -0
  201. package/src/primitives/canvas/ui_markup_view.zig +2352 -0
  202. package/src/primitives/canvas/ui_markup_view_tests.zig +3709 -0
  203. package/src/primitives/canvas/ui_provenance.zig +96 -0
  204. package/src/primitives/canvas/ui_provenance_tests.zig +186 -0
  205. package/src/primitives/canvas/ui_schema.zig +825 -0
  206. package/src/primitives/canvas/ui_schema_tests.zig +128 -0
  207. package/src/primitives/canvas/ui_tests.zig +1432 -0
  208. package/src/primitives/canvas/vector.zig +919 -0
  209. package/src/primitives/canvas/vector_tests.zig +547 -0
  210. package/src/primitives/canvas/virtual_extent_tests.zig +317 -0
  211. package/src/primitives/canvas/virtual_extents.zig +540 -0
  212. package/src/primitives/canvas/widget_access.zig +208 -0
  213. package/src/primitives/canvas/widget_anchored_tests.zig +264 -0
  214. package/src/primitives/canvas/widget_builtin_tests.zig +3160 -0
  215. package/src/primitives/canvas/widget_invalidation.zig +748 -0
  216. package/src/primitives/canvas/widget_layout.zig +2055 -0
  217. package/src/primitives/canvas/widget_layout_tests.zig +2691 -0
  218. package/src/primitives/canvas/widget_metrics.zig +228 -0
  219. package/src/primitives/canvas/widget_render.zig +2534 -0
  220. package/src/primitives/canvas/widget_render_controls.zig +1563 -0
  221. package/src/primitives/canvas/widget_render_scroll.zig +136 -0
  222. package/src/primitives/canvas/widget_render_style.zig +626 -0
  223. package/src/primitives/canvas/widget_render_surfaces.zig +794 -0
  224. package/src/primitives/canvas/widget_routing.zig +475 -0
  225. package/src/primitives/canvas/widget_runtime.zig +307 -0
  226. package/src/primitives/canvas/widget_runtime_tests.zig +3015 -0
  227. package/src/primitives/canvas/widget_semantics.zig +480 -0
  228. package/src/primitives/canvas/widget_semantics_tests.zig +2129 -0
  229. package/src/primitives/canvas/widget_tests.zig +8 -0
  230. package/src/primitives/canvas/widget_text_input.zig +351 -0
  231. package/src/primitives/canvas/widget_text_select.zig +147 -0
  232. package/src/primitives/canvas/widget_tree.zig +232 -0
  233. package/src/primitives/canvas/widgets.zig +1319 -0
  234. package/src/primitives/diagnostics/root.zig +445 -0
  235. package/src/primitives/geometry/root.zig +889 -0
  236. package/src/primitives/json/root.zig +272 -0
  237. package/src/primitives/platform_info/root.zig +450 -0
  238. package/src/primitives/trace/root.zig +569 -0
  239. package/src/root.zig +165 -0
  240. package/src/runtime/api.zig +431 -0
  241. package/src/runtime/async_bridge.zig +49 -0
  242. package/src/runtime/automation_commands.zig +686 -0
  243. package/src/runtime/automation_liveness_tests.zig +140 -0
  244. package/src/runtime/automation_snapshot.zig +246 -0
  245. package/src/runtime/automation_widget_dispatch.zig +617 -0
  246. package/src/runtime/bridge_dispatch_tests.zig +202 -0
  247. package/src/runtime/bridge_payload.zig +194 -0
  248. package/src/runtime/bridge_permission_tests.zig +551 -0
  249. package/src/runtime/bridge_responses.zig +414 -0
  250. package/src/runtime/builtin_bridge.zig +554 -0
  251. package/src/runtime/canvas_budget_edge_tests.zig +415 -0
  252. package/src/runtime/canvas_font_tests.zig +410 -0
  253. package/src/runtime/canvas_fonts.zig +229 -0
  254. package/src/runtime/canvas_frame.zig +1734 -0
  255. package/src/runtime/canvas_frame_animation_tests.zig +609 -0
  256. package/src/runtime/canvas_frame_cache_tests.zig +333 -0
  257. package/src/runtime/canvas_frame_helpers.zig +414 -0
  258. package/src/runtime/canvas_frame_invalidation_tests.zig +544 -0
  259. package/src/runtime/canvas_frame_packet_tests.zig +1353 -0
  260. package/src/runtime/canvas_frame_patch_tests.zig +1051 -0
  261. package/src/runtime/canvas_frame_retained_tests.zig +753 -0
  262. package/src/runtime/canvas_frame_tests.zig +9 -0
  263. package/src/runtime/canvas_frame_text_layout_budget_tests.zig +149 -0
  264. package/src/runtime/canvas_image_tests.zig +362 -0
  265. package/src/runtime/canvas_images.zig +224 -0
  266. package/src/runtime/canvas_limits.zig +317 -0
  267. package/src/runtime/canvas_screenshot_tests.zig +289 -0
  268. package/src/runtime/canvas_widget_accessibility_tests.zig +1494 -0
  269. package/src/runtime/canvas_widget_anchored_tests.zig +678 -0
  270. package/src/runtime/canvas_widget_chart_tests.zig +249 -0
  271. package/src/runtime/canvas_widget_clipboard_tests.zig +399 -0
  272. package/src/runtime/canvas_widget_context_menu.zig +318 -0
  273. package/src/runtime/canvas_widget_context_menu_tests.zig +462 -0
  274. package/src/runtime/canvas_widget_control_tests.zig +2164 -0
  275. package/src/runtime/canvas_widget_disclosure_tests.zig +440 -0
  276. package/src/runtime/canvas_widget_display.zig +677 -0
  277. package/src/runtime/canvas_widget_event_tests.zig +1779 -0
  278. package/src/runtime/canvas_widget_events.zig +1098 -0
  279. package/src/runtime/canvas_widget_floating_tests.zig +695 -0
  280. package/src/runtime/canvas_widget_layout_tests.zig +26 -0
  281. package/src/runtime/canvas_widget_runtime.zig +1335 -0
  282. package/src/runtime/canvas_widget_scroll_driver_tests.zig +582 -0
  283. package/src/runtime/canvas_widget_scroll_drivers.zig +202 -0
  284. package/src/runtime/canvas_widget_scroll_tests.zig +1549 -0
  285. package/src/runtime/canvas_widget_semantics_tests.zig +810 -0
  286. package/src/runtime/canvas_widget_split_tree_tests.zig +1031 -0
  287. package/src/runtime/canvas_widget_state.zig +798 -0
  288. package/src/runtime/canvas_widget_text_tests.zig +2104 -0
  289. package/src/runtime/canvas_widget_window_drag_tests.zig +284 -0
  290. package/src/runtime/clock.zig +231 -0
  291. package/src/runtime/core.zig +927 -0
  292. package/src/runtime/effects.zig +4197 -0
  293. package/src/runtime/effects_audio_tests.zig +690 -0
  294. package/src/runtime/effects_clipboard_tests.zig +317 -0
  295. package/src/runtime/effects_fetch_tests.zig +1031 -0
  296. package/src/runtime/effects_file_tests.zig +407 -0
  297. package/src/runtime/effects_tests.zig +1251 -0
  298. package/src/runtime/flow.zig +1175 -0
  299. package/src/runtime/frame_profile.zig +221 -0
  300. package/src/runtime/gpu_surface_events.zig +639 -0
  301. package/src/runtime/launch_timing.zig +59 -0
  302. package/src/runtime/markdown_app_tests.zig +224 -0
  303. package/src/runtime/native_command_event_tests.zig +431 -0
  304. package/src/runtime/platform_bridge_tests.zig +8 -0
  305. package/src/runtime/platform_config_tests.zig +392 -0
  306. package/src/runtime/root.zig +150 -0
  307. package/src/runtime/runtime_event_tests.zig +1079 -0
  308. package/src/runtime/session_journal.zig +1522 -0
  309. package/src/runtime/session_record.zig +332 -0
  310. package/src/runtime/session_replay.zig +274 -0
  311. package/src/runtime/session_state.zig +30 -0
  312. package/src/runtime/session_tests.zig +399 -0
  313. package/src/runtime/shell_layout.zig +472 -0
  314. package/src/runtime/shell_layout_tests.zig +1246 -0
  315. package/src/runtime/shell_view_tests.zig +604 -0
  316. package/src/runtime/state.zig +110 -0
  317. package/src/runtime/system_services.zig +392 -0
  318. package/src/runtime/test_support.zig +79 -0
  319. package/src/runtime/tests.zig +30 -0
  320. package/src/runtime/ui_app.zig +3290 -0
  321. package/src/runtime/ui_app_huge_model_tests.zig +351 -0
  322. package/src/runtime/ui_app_provenance.zig +370 -0
  323. package/src/runtime/ui_app_tests.zig +3254 -0
  324. package/src/runtime/ui_app_virtual_variable_tests.zig +485 -0
  325. package/src/runtime/ui_app_window_tests.zig +442 -0
  326. package/src/runtime/validation.zig +206 -0
  327. package/src/runtime/view.zig +908 -0
  328. package/src/runtime/view_canvas.zig +850 -0
  329. package/src/runtime/view_widget_control.zig +433 -0
  330. package/src/runtime/view_widget_scroll.zig +360 -0
  331. package/src/runtime/view_widget_text.zig +275 -0
  332. package/src/runtime/view_widget_tree.zig +843 -0
  333. package/src/runtime/webview_view_bridge_tests.zig +664 -0
  334. package/src/runtime/widget_bridge.zig +231 -0
  335. package/src/runtime/window_command_bridge_tests.zig +403 -0
  336. package/src/runtime/window_storage.zig +255 -0
  337. package/src/runtime/window_view_runtime.zig +727 -0
  338. package/src/runtime/window_views.zig +334 -0
  339. package/src/security/root.zig +118 -0
  340. package/src/tooling/android.zig +1172 -0
  341. package/src/tooling/assets.zig +128 -0
  342. package/src/tooling/buildgraph.zig +379 -0
  343. package/src/tooling/cef.zig +671 -0
  344. package/src/tooling/codesign.zig +172 -0
  345. package/src/tooling/components/identity_tests.zig +185 -0
  346. package/src/tooling/components/stepper.zig +113 -0
  347. package/src/tooling/components/timeline.native +27 -0
  348. package/src/tooling/components/timeline_item.zig +131 -0
  349. package/src/tooling/default_icon.icns +0 -0
  350. package/src/tooling/default_icon.png +0 -0
  351. package/src/tooling/dev.zig +172 -0
  352. package/src/tooling/doctor.zig +349 -0
  353. package/src/tooling/eject_components.zig +170 -0
  354. package/src/tooling/embedlib.zig +290 -0
  355. package/src/tooling/ios.zig +528 -0
  356. package/src/tooling/manifest.zig +1939 -0
  357. package/src/tooling/package.zig +2388 -0
  358. package/src/tooling/process_tree.zig +114 -0
  359. package/src/tooling/raw_manifest.zig +193 -0
  360. package/src/tooling/root.zig +22 -0
  361. package/src/tooling/templates.zig +3191 -0
  362. package/src/tooling/toolchain.zig +371 -0
  363. package/src/tooling/verbs.zig +204 -0
  364. package/src/tooling/web_engine.zig +135 -0
  365. package/src/tooling/xcodeproj.zig +462 -0
  366. package/src/window_state/root.zig +507 -0
  367. package/index.js +0 -2
@@ -0,0 +1,2565 @@
1
+ //! Comptime-compiled markup views: parse a `.native` source entirely at
2
+ //! comptime and emit a `build(ui, model)` whose output is identical to the
3
+ //! interpreter's (ui_markup_view.zig) for the same model — same structural
4
+ //! widget ids node for node, same handler table, same dispatch results —
5
+ //! with no parser or interpreter in the binary.
6
+ //!
7
+ //! The runtime interpreter stays the dev/hot-reload engine; this is the
8
+ //! release engine. Both share one grammar: the parser's token-level
9
+ //! helpers, the expression parsers (`parseAttrExpression`,
10
+ //! `parseMessageExpression`), the element/attribute tables, and the value
11
+ //! conversion code (`Value`, `valueOf`, `literalValue`, `appendValue`) are
12
+ //! the interpreter's own, so the engines cannot drift.
13
+ //!
14
+ //! Errors: everything the interpreter reports as a runtime `MarkupBuild`
15
+ //! failure whose cause is knowable from the source and the Model/Msg types
16
+ //! — unknown elements/attributes, malformed expressions, bindings that
17
+ //! don't name model fields, unknown message tags, payload type mismatches —
18
+ //! becomes a compile error carrying the node's line/column and the
19
+ //! interpreter's message. That is also the compile-error test strategy:
20
+ //! invalid constructs are structurally unreachable at runtime because the
21
+ //! comptime walk `@compileError`s on them while resolving the tree (Zig
22
+ //! cannot unit-test `@compileError`, so ui_markup_compiled_tests.zig covers
23
+ //! the accepting side exhaustively and the interpreter's failure tests
24
+ //! enumerate the constructs this path rejects at compile time). The only
25
+ //! failures left for runtime are value-dependent ones the interpreter also
26
+ //! discovers at runtime (an optional binding or non-tag string feeding an
27
+ //! enum), which latch `ui.failed` exactly like the builder's own sugar.
28
+
29
+ const std = @import("std");
30
+ const builtin = @import("builtin");
31
+ const canvas = @import("root.zig");
32
+ const markup = @import("ui_markup.zig");
33
+ const interpreter = @import("ui_markup_view.zig");
34
+
35
+ const Value = interpreter.Value;
36
+
37
+ /// A markup view compiled against a concrete Model/Msg pair. `source` is
38
+ /// parsed at comptime (no parser in the binary) and `build` unrolls binding
39
+ /// and message resolution to direct field/method access — what an
40
+ /// equivalent hand-written `view(ui, model)` compiles to. Single-file
41
+ /// documents only; a document with `<import>`s names its source set
42
+ /// through `CompiledMarkupImports`.
43
+ pub fn CompiledMarkupView(comptime ModelT: type, comptime MsgT: type, comptime source: []const u8) type {
44
+ const parsed = markup.parseComptime(source);
45
+ if (parsed.imports.len > 0) {
46
+ @compileError("this markup imports other files - compile it with canvas.CompiledMarkupImports(Model, Msg, \"root.native\", &sources), where sources is a markup.SourceFile set embedding the root and every imported file");
47
+ }
48
+ return CompiledMarkupEngine(ModelT, MsgT, parsed, source, &.{});
49
+ }
50
+
51
+ /// The compiled engine's side of the import resolver seam (see the
52
+ /// "imports" section of ui_markup.zig): the app assembles a comptime
53
+ /// source set with `@embedFile` — one entry per file, paths relative to
54
+ /// the root file's directory — and resolution merges the closure at
55
+ /// comptime. The same set drives the runtime interpreter's embedded
56
+ /// resolution (`MarkupOptions.sources`), so both engines see one document.
57
+ pub fn CompiledMarkupImports(comptime ModelT: type, comptime MsgT: type, comptime root_name: []const u8, comptime sources: []const markup.SourceFile) type {
58
+ return CompiledMarkupEngine(ModelT, MsgT, markup.resolveImportsComptime(root_name, sources), rootSourceOf(root_name, sources), sources);
59
+ }
60
+
61
+ /// The root file's embedded bytes out of a comptime source set — the
62
+ /// hot-reload baseline `fragment()` carries. The resolver already
63
+ /// requires the root to be present (with its own teaching error), so a
64
+ /// miss here is unreachable in practice.
65
+ fn rootSourceOf(comptime root_name: []const u8, comptime sources: []const markup.SourceFile) []const u8 {
66
+ for (sources) |file| {
67
+ if (std.mem.eql(u8, file.path, root_name)) return file.source;
68
+ }
69
+ return "";
70
+ }
71
+
72
+ /// Shared engine over an already-resolved comptime document, without a
73
+ /// hot-reload baseline: a fragment compiled through this shape has no
74
+ /// embedded source to compare a watched file against, so it cannot
75
+ /// register with the fragment watch (`CompiledMarkupView` and
76
+ /// `CompiledMarkupImports` are the registrable shapes).
77
+ pub fn CompiledMarkupDocument(comptime ModelT: type, comptime MsgT: type, comptime resolved_document: markup.MarkupDocument) type {
78
+ return CompiledMarkupEngine(ModelT, MsgT, resolved_document, "", &.{});
79
+ }
80
+
81
+ /// The engine over an already-resolved comptime document. The document
82
+ /// is canonicalized first (the typed-document pass), so both engines
83
+ /// consume the same typed form; this engine's binding/message resolution
84
+ /// stays comptime-unrolled, so the pass changes nothing about the code it
85
+ /// emits — the parity suite is the proof. `fragment_source` and
86
+ /// `fragment_sources` are the embedded bytes the document was compiled
87
+ /// from, referenced only by the Debug-only fragment watch registration.
88
+ fn CompiledMarkupEngine(comptime ModelT: type, comptime MsgT: type, comptime resolved_document: markup.MarkupDocument, comptime fragment_source: []const u8, comptime fragment_sources: []const markup.SourceFile) type {
89
+ return struct {
90
+ pub const Ui = canvas.Ui(MsgT);
91
+
92
+ pub const document = markup.canonicalizeComptime(resolved_document);
93
+
94
+ /// Debug-only registration handle for the runtime's fragment
95
+ /// hot-reload watch: `.fragment("src/header.native")` in
96
+ /// `UiApp.Options.fragment_watch` names the on-disk source this
97
+ /// fragment was compiled from, so a dev run reloads it in place
98
+ /// when the file (or any file its imports reach) changes.
99
+ /// Outside Debug this returns an empty handle — no path bytes,
100
+ /// no embedded-baseline references — so release binaries carry
101
+ /// no watch plumbing.
102
+ pub fn fragment(comptime path: []const u8) markup.MarkupFragment {
103
+ comptime {
104
+ if (fragment_source.len == 0) @compileError("this compiled markup view has no embedded source baseline - only CompiledMarkupView / CompiledMarkupImports fragments can register with the fragment watch");
105
+ }
106
+ if (comptime builtin.mode != .Debug) return .{};
107
+ return .{
108
+ .key = fragmentKey(),
109
+ .path = path,
110
+ .source = fragment_source,
111
+ .sources = fragment_sources,
112
+ };
113
+ }
114
+
115
+ /// This fragment's identity for the watch's override lookup: the
116
+ /// address of the comptime document, unique per compiled fragment
117
+ /// type and shared by registration (`fragment`) and the build-time
118
+ /// check, so the two cannot disagree.
119
+ fn fragmentKey() *const anyopaque {
120
+ return @ptrCast(&document);
121
+ }
122
+
123
+ /// Loop variables, template args, and slot captures in scope at a
124
+ /// point in the tree. Names, kinds, and item types are comptime;
125
+ /// the runtime value is a nested struct with one payload per
126
+ /// entry: a `*const Item` for `for` items, a `Value` for scalar
127
+ /// template args, a `[]const Item` for slice-valued template args,
128
+ /// and the use-site scope chain for slot captures.
129
+ const ScopeEntry = struct {
130
+ name: []const u8,
131
+ kind: Kind,
132
+ Item: type = void,
133
+ /// For value args: the comptime-known Value variant of the
134
+ /// use-site expression (null when only runtime-known, e.g. a
135
+ /// binding through an optional).
136
+ variant: ?ValueVariant = null,
137
+ /// For slot captures: the `<use>` site's children, the scope
138
+ /// entries they must build under (the consumer's), and the
139
+ /// runtime type of the consumer's scope chain. The capture's
140
+ /// name is empty, which no binding head can equal, so lookups
141
+ /// skip it.
142
+ slot_nodes: []const markup.MarkupNode = &.{},
143
+ slot_entries: []const ScopeEntry = &.{},
144
+ SiteScope: type = void,
145
+
146
+ const Kind = enum { item, value_arg, slice_arg, slot };
147
+ };
148
+
149
+ fn EntryPayload(comptime entry: ScopeEntry) type {
150
+ return switch (entry.kind) {
151
+ .item => *const entry.Item,
152
+ .value_arg => Value,
153
+ .slice_arg => []const entry.Item,
154
+ .slot => entry.SiteScope,
155
+ };
156
+ }
157
+
158
+ // Runtime scopes are anonymous `{ parent, item }` chains passed as
159
+ // `anytype`: one link per entry, innermost last, so a link's type
160
+ // never depends on comptime slice identity (a child list created
161
+ // with `entries ++ ...` re-slices a fresh array, which would not
162
+ // unify with the parent's `entries` under generic instantiation).
163
+
164
+ const no_entries: []const ScopeEntry = &.{};
165
+
166
+ /// Build the view for the current model. Signature-compatible with
167
+ /// a hand-written view, so it slots into `UiApp.Options.view`
168
+ /// directly. Markup mistakes are compile errors; the only runtime
169
+ /// failures latch `ui.failed` (surfaced by `finalize`) exactly like
170
+ /// the builder's own sugar (`ui.fmt`, `ui.each`).
171
+ pub fn build(ui: *Ui, model: *const ModelT) Ui.Node {
172
+ // Debug-only hot-reload seam: when the app registered this
173
+ // fragment with the runtime's fragment watch (`fragment(path)`
174
+ // in `UiApp.Options.fragment_watch`) and the watch adopted a
175
+ // changed on-disk source, the interpreter builds the reloaded
176
+ // document instead of the comptime tree. The engines are
177
+ // parity-proven, so pixels and structural ids match until the
178
+ // edit itself changes them; release builds compile this
179
+ // branch out entirely.
180
+ if (comptime builtin.mode == .Debug) {
181
+ if (ui.markup_fragment_host) |host| {
182
+ if (host.override(host.context, fragmentKey())) |override_ptr| {
183
+ const live_document: *const markup.MarkupDocument = @ptrCast(@alignCast(override_ptr));
184
+ var live = interpreter.MarkupView(ModelT, MsgT).fromDocument(live_document.*);
185
+ return live.build(ui, model) catch {
186
+ // The reloaded source parses but cannot build
187
+ // against this Model/Msg (a binding naming no
188
+ // model field, an unknown message tag — what
189
+ // the compiled engine catches at comptime).
190
+ // Report the teaching diagnostic through the
191
+ // host and latch `ui.failed` so the frame
192
+ // aborts and the last good tree stays up; the
193
+ // next good save reloads and recovers.
194
+ host.report(host.context, .{
195
+ .line = live.diagnostic.line,
196
+ .column = live.diagnostic.column,
197
+ .message = live.diagnostic.message,
198
+ .path = live.diagnostic.path,
199
+ });
200
+ ui.failed = true;
201
+ return ui.column(.{}, .{});
202
+ };
203
+ }
204
+ }
205
+ }
206
+ const root = comptime (document.root orelse @compileError("markup error: " ++ markup.component_file_view_message));
207
+ comptime {
208
+ checkTemplates();
209
+ switch (root.kind) {
210
+ .element, .use_block => {},
211
+ .template_block => fail(root, markup.template_top_level_message),
212
+ .import_block => fail(root, markup.import_top_level_message),
213
+ .slot_block => fail(root, markup.slot_outside_template_message),
214
+ .text => fail(root, "text content is only allowed inside text-bearing elements"),
215
+ .for_block, .if_block, .else_block => fail(root, "structure tags are only allowed inside an element"),
216
+ }
217
+ }
218
+ if (comptime (root.kind == .use_block)) {
219
+ return buildUse(root, no_entries, ui, model, .{});
220
+ }
221
+ return buildElement(root, no_entries, ui, model, .{});
222
+ }
223
+
224
+ /// Comptime template wiring checks, mirroring the validator: a
225
+ /// name, exactly one element child, at most one slot, and uses
226
+ /// inside template bodies referencing only earlier templates —
227
+ /// which also guarantees comptime expansion terminates (slot
228
+ /// content is lexical, so it cannot re-enter an expansion).
229
+ fn checkTemplates() void {
230
+ comptime {
231
+ @setEvalBranchQuota(50_000 + document.templates.len * 20_000);
232
+ for (document.templates, 0..) |template_node, index| {
233
+ const name = template_node.attr("name") orelse fail(template_node, markup.template_name_message);
234
+ for (document.templates[0..index]) |earlier| {
235
+ const earlier_name = earlier.attr("name") orelse continue;
236
+ if (std.mem.eql(u8, earlier_name, name)) fail(template_node, markup.template_unique_name_message);
237
+ }
238
+ if (template_node.children.len != 1 or template_node.children[0].kind != .element) {
239
+ fail(template_node, markup.template_one_child_message);
240
+ }
241
+ if (markup.templateSecondSlot(template_node.children[0])) |second| {
242
+ fail(second, markup.template_one_slot_message);
243
+ }
244
+ checkUseOrder(template_node.children[0], index);
245
+ }
246
+ }
247
+ }
248
+
249
+ fn checkUseOrder(comptime node: markup.MarkupNode, comptime limit: usize) void {
250
+ comptime {
251
+ if (node.kind == .use_block) {
252
+ const name = node.attr("template") orelse fail(node, markup.use_template_attr_message);
253
+ const index = document.templateIndex(name) orelse fail(node, markup.use_undefined_template_message);
254
+ if (index >= limit) fail(node, markup.use_earlier_template_message);
255
+ }
256
+ for (node.children) |child| checkUseOrder(child, limit);
257
+ }
258
+ }
259
+
260
+ // ------------------------------------------------------ building
261
+
262
+ fn buildElement(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype) Ui.Node {
263
+ if (comptime std.mem.eql(u8, node.name, "markdown")) {
264
+ return buildMarkdown(node, entries, ui, model, scope);
265
+ }
266
+ if (comptime std.mem.eql(u8, node.name, "stepper")) {
267
+ return buildStepper(node, entries, ui, model, scope);
268
+ }
269
+ if (comptime std.mem.eql(u8, node.name, "step")) {
270
+ // Steps inside a stepper are consumed by buildStepper.
271
+ comptime fail(node, markup.step_parent_message);
272
+ }
273
+ if (comptime std.mem.eql(u8, node.name, "timeline")) {
274
+ return buildTimeline(node, entries, ui, model, scope);
275
+ }
276
+ if (comptime std.mem.eql(u8, node.name, "timeline-item")) {
277
+ return buildTimelineItem(node, entries, ui, model, scope);
278
+ }
279
+ if (comptime std.mem.eql(u8, node.name, "chart")) {
280
+ return buildChart(node, entries, ui, model, scope);
281
+ }
282
+ if (comptime std.mem.eql(u8, node.name, "series")) {
283
+ // Series inside a chart are consumed by buildChart.
284
+ comptime fail(node, markup.series_parent_message);
285
+ }
286
+ if (comptime std.mem.eql(u8, node.name, "context-menu")) {
287
+ // Direct context-menu children are consumed by their host
288
+ // element below; one reaching here is misplaced.
289
+ comptime fail(node, markup.context_menu_parent_message);
290
+ }
291
+ if (comptime std.mem.eql(u8, node.name, "input-group")) {
292
+ return buildInputGroup(node, entries, ui, model, scope);
293
+ }
294
+ if (comptime std.mem.eql(u8, node.name, "input-group-actions")) {
295
+ // Actions rows inside an input-group are consumed by
296
+ // buildInputGroup.
297
+ comptime fail(node, markup.input_group_actions_parent_message);
298
+ }
299
+ if (comptime std.mem.eql(u8, node.name, "span")) {
300
+ // Spans inside a text paragraph are consumed by
301
+ // buildSpanParagraph; one reaching here has no text
302
+ // parent.
303
+ comptime fail(node, markup.span_parent_message);
304
+ }
305
+ if (comptime std.mem.eql(u8, node.name, "reactions")) {
306
+ // Reactions inside a bubble are consumed by the bubble's
307
+ // build below; one reaching here has no bubble parent.
308
+ comptime fail(node, markup.reactions_parent_message);
309
+ }
310
+ const kind = comptime (interpreter.elementKind(node.name) orelse fail(node, "unknown element"));
311
+ // Interpreter parity: extract a direct context-menu child —
312
+ // metadata on this element (lowered to the declared
313
+ // platform-menu items), not content — so every content rule
314
+ // below sees the remaining children only.
315
+ const context_menu_split = comptime splitContextMenuChild(node);
316
+ // Interpreter parity: extract a direct reactions child the
317
+ // same way — the pill is bubble CHROME (it lowers onto the
318
+ // bubble widget's chrome-text channel), not content.
319
+ const reactions_split = comptime splitReactionsChild(context_menu_split.inner, kind);
320
+ const inner = comptime reactions_split.inner;
321
+ comptime {
322
+ // Interpreter parity: the bubble's chrome-text channel
323
+ // belongs to the reaction pill; a bare text attribute
324
+ // would silently do nothing, so it is a compile error.
325
+ if (kind == .bubble and node.attr("text") != null) {
326
+ fail(node, markup.bubble_text_attr_message);
327
+ }
328
+ }
329
+ comptime {
330
+ // Interpreter parity: value/text handlers on
331
+ // non-hit-target kinds can never fire, so a dead handler
332
+ // is a compile error here. on-press/on-toggle are exempt
333
+ // — a bound press handler makes any element a hit target
334
+ // and presses fall through to it.
335
+ if (!canvas.widgetKindHitTarget(kind)) {
336
+ for (node.attrs) |attribute| {
337
+ if (std.mem.startsWith(u8, attribute.name, "on-") and markup.deadHandlerOnNonHitTarget(attribute.name)) {
338
+ fail(node, markup.non_hit_target_handler_message);
339
+ }
340
+ // Autofocus can never land here: nothing about
341
+ // this element is focusable.
342
+ if (std.mem.eql(u8, attribute.name, "autofocus")) {
343
+ fail(node, markup.autofocus_element_message);
344
+ }
345
+ }
346
+ }
347
+ // Interpreter parity: stacking kinds give every child the
348
+ // full content box, so a gap can never space them — dead
349
+ // layout data is a compile error here.
350
+ if (canvas.widgetKindStacksChildren(kind)) {
351
+ for (node.attrs) |attribute| {
352
+ if (std.mem.eql(u8, attribute.name, "gap")) {
353
+ fail(node, markup.stack_container_gap_message);
354
+ }
355
+ }
356
+ }
357
+ // Interpreter parity: only plain text leaves word-wrap
358
+ // or elide; anywhere else the options are silently inert
359
+ // dead layout data — a compile error here.
360
+ if (kind != .text) {
361
+ for (node.attrs) |attribute| {
362
+ if (std.mem.eql(u8, attribute.name, "wrap")) {
363
+ fail(node, markup.wrap_element_message);
364
+ }
365
+ if (std.mem.eql(u8, attribute.name, "overflow")) {
366
+ fail(node, markup.overflow_element_message);
367
+ }
368
+ }
369
+ }
370
+ // Interpreter parity: the overflow policy's closed
371
+ // literal vocabulary. A compile error here; bindings
372
+ // resolve at runtime like any enum option.
373
+ for (node.attrs) |attribute| {
374
+ if (!std.mem.eql(u8, attribute.name, "overflow")) continue;
375
+ const expression = markup.parseAttrExpression(attribute.value) orelse continue;
376
+ if (expression != .literal) continue;
377
+ if (!markup.nameInList(expression.literal, &markup.overflow_value_names)) {
378
+ fail(node, markup.overflow_value_message);
379
+ }
380
+ }
381
+ // Interpreter parity: the size register's closed literal
382
+ // vocabulary — the control scale everywhere, the
383
+ // typography rungs (heading/display) on text only. A
384
+ // compile error here; bindings resolve at runtime like
385
+ // any enum option.
386
+ for (node.attrs) |attribute| {
387
+ if (!std.mem.eql(u8, attribute.name, "size")) continue;
388
+ const expression = markup.parseAttrExpression(attribute.value) orelse continue;
389
+ if (expression != .literal) continue;
390
+ if (markup.nameInList(expression.literal, &markup.known_text_size_value_names)) {
391
+ if (kind != .text) fail(node, markup.text_size_element_message);
392
+ } else if (!markup.nameInList(expression.literal, &markup.known_control_size_value_names)) {
393
+ fail(node, markup.size_value_message);
394
+ }
395
+ }
396
+ // Interpreter parity: splits take exactly two static
397
+ // pane children (the divider sits between fixed panes).
398
+ if (kind == .split) {
399
+ var pane_count: usize = 0;
400
+ for (inner.children) |child| {
401
+ switch (child.kind) {
402
+ .element, .use_block => pane_count += 1,
403
+ else => fail(child, markup.split_children_message),
404
+ }
405
+ }
406
+ if (pane_count != 2) fail(node, markup.split_children_message);
407
+ }
408
+ // Interpreter parity: the a11y lint's error half — an
409
+ // unnamed interactive control or a misused role ships a
410
+ // view a screen reader user cannot operate, so it is a
411
+ // compile error here.
412
+ if (markup.a11yNameError(node)) |message| {
413
+ fail(node, message);
414
+ }
415
+ if (markup.a11yRoleError(node)) |message| {
416
+ fail(node, message);
417
+ }
418
+ }
419
+ var options: Ui.ElementOptions = .{};
420
+ applyAttrs(node, entries, ui, model, scope, &options);
421
+ // Interpreter parity: the extracted context-menu lowers
422
+ // through the ordinary element path — its menu-items build
423
+ // like any element — and the built nodes become the host's
424
+ // declared items. An empty runtime result declares no menu.
425
+ if (comptime (context_menu_split.menu != null)) {
426
+ var menu_children: std.ArrayListUnmanaged(Ui.Node) = .empty;
427
+ buildChildList(comptime context_menu_split.menu.?.children, entries, ui, model, scope, &menu_children);
428
+ options.context_menu = ui.contextMenuItemsFromNodes(menu_children.items);
429
+ }
430
+
431
+ if (comptime (kind == .icon)) {
432
+ // The shared icon value grammar, resolved at comptime
433
+ // where it can be: a typo in a built-in name is a compile
434
+ // error, while app: names and bound names ride the
435
+ // explicit icon channel and degrade at draw time to the
436
+ // missing-icon fallback plus a Debug warning naming the
437
+ // value (interpreter parity).
438
+ const icon_value = comptime blk: {
439
+ const raw = node.attr("name") orelse fail(node, markup.icon_missing_name_message);
440
+ if (inner.children.len > 0) fail(node, markup.icon_children_message);
441
+ break :blk iconValueChecked(node, raw, markup.icon_name_message);
442
+ };
443
+ switch (comptime icon_value) {
444
+ .builtin => |name| {
445
+ var built = ui.el(kind, options, .{});
446
+ built.widget.text = name;
447
+ return built;
448
+ },
449
+ .app => |spelled| options.icon = spelled,
450
+ .binding => options.icon = stringAttr(node, entries, comptime node.attr("name").?, ui, model, scope, markup.icon_name_message),
451
+ .invalid => unreachable,
452
+ }
453
+ return ui.el(kind, options, .{});
454
+ }
455
+
456
+ // Interpreter parity: the span paragraph — a text element
457
+ // with inline <span> children lowers through Ui.paragraph,
458
+ // exactly like a builder span paragraph.
459
+ if (comptime (kind == .text and markup.nodeHasSpanChildren(inner))) {
460
+ return buildSpanParagraph(inner, entries, ui, model, scope, options);
461
+ }
462
+
463
+ // Interpreter parity: the list-row composite — a text-taking
464
+ // element whose content is element children instead of the
465
+ // text run flows those children inside its own chrome, and
466
+ // mixing text and elements is a compile error here.
467
+ const composite_children = comptime (interpreter.elementTakesChildren(kind) and markup.nodeHasElementContent(inner));
468
+ comptime {
469
+ if (composite_children) {
470
+ for (inner.children) |child| {
471
+ if (child.kind == .text) fail(child, markup.text_or_children_content_message);
472
+ }
473
+ }
474
+ }
475
+ if (comptime (interpreter.elementTakesText(kind) and !composite_children)) {
476
+ var built = ui.el(kind, options, .{});
477
+ built.widget.text = interpolatedText(inner, entries, ui, model, scope);
478
+ // Avatars clip their runtime image to the avatar circle,
479
+ // exactly like `Ui.avatar` and the interpreter (a no-op
480
+ // while the id is 0 and the initials fallback renders).
481
+ if (comptime (kind == .avatar)) built.widget.image_fit = .cover;
482
+ return built;
483
+ }
484
+
485
+ var children: std.ArrayListUnmanaged(Ui.Node) = .empty;
486
+ buildChildren(inner, entries, ui, model, scope, &children);
487
+ // Interpreter parity: tab triggers ARE segmented controls -
488
+ // `<button>` children of a `<tabs>` strip lower to the
489
+ // widget kind tab strips are built on (see
490
+ // `interpreter.lowerTabsTriggers`).
491
+ if (comptime (kind == .tabs)) interpreter.lowerTabsTriggers(children.items);
492
+ var built = ui.el(kind, options, @as([]const Ui.Node, children.items));
493
+ // Interpreter parity: the extracted reactions run lands on
494
+ // the bubble widget's chrome-text channel — the render pass
495
+ // draws it as the docked pill — and the dock rides
496
+ // text_alignment (end is the default: the trailing dock
497
+ // reactions conventionally hang from). The dock literal
498
+ // resolves at comptime; the run interpolates like any text.
499
+ if (comptime (reactions_split.pill != null)) {
500
+ built.widget.text = interpolatedText(comptime reactions_split.pill.?, entries, ui, model, scope);
501
+ built.widget.text_alignment = comptime blk: {
502
+ const raw = reactions_split.pill.?.attr("text-alignment") orelse break :blk .end;
503
+ break :blk std.meta.stringToEnum(canvas.TextAlign, raw) orelse
504
+ fail(reactions_split.pill.?, markup.reactions_alignment_value_message);
505
+ };
506
+ }
507
+ return built;
508
+ }
509
+
510
+ /// One runtime step per child: elements and `use` expansions append
511
+ /// a node, `slot` splices the use-site children in place, `for`
512
+ /// blocks append per item (with an adjacent `else` paired at
513
+ /// comptime for the empty case), and an `if` (with its adjacent
514
+ /// `else` paired at comptime) branches on the test binding.
515
+ const ChildStep = union(enum) {
516
+ element: markup.MarkupNode,
517
+ use: markup.MarkupNode,
518
+ slot: markup.MarkupNode,
519
+ for_block: struct { node: markup.MarkupNode, else_block: ?markup.MarkupNode },
520
+ conditional: struct { if_block: markup.MarkupNode, else_block: ?markup.MarkupNode },
521
+ };
522
+
523
+ fn childSteps(comptime children: []const markup.MarkupNode) []const ChildStep {
524
+ comptime {
525
+ @setEvalBranchQuota(10_000);
526
+ var steps: []const ChildStep = &.{};
527
+ var index: usize = 0;
528
+ while (index < children.len) : (index += 1) {
529
+ const child = children[index];
530
+ switch (child.kind) {
531
+ .element => steps = steps ++ &[_]ChildStep{.{ .element = child }},
532
+ .use_block => steps = steps ++ &[_]ChildStep{.{ .use = child }},
533
+ .slot_block => {
534
+ // Interpreter and validator parity: a slot is
535
+ // an attribute-less, childless leaf.
536
+ if (child.attrs.len > 0) fail(child, markup.slot_attrs_message);
537
+ if (child.children.len > 0) fail(child.children[0], markup.slot_children_message);
538
+ steps = steps ++ &[_]ChildStep{.{ .slot = child }};
539
+ },
540
+ .template_block => fail(child, markup.template_top_level_message),
541
+ .import_block => fail(child, markup.import_top_level_message),
542
+ .for_block => {
543
+ var else_block: ?markup.MarkupNode = null;
544
+ if (index + 1 < children.len and children[index + 1].kind == .else_block) {
545
+ else_block = children[index + 1];
546
+ index += 1;
547
+ }
548
+ steps = steps ++ &[_]ChildStep{.{ .for_block = .{ .node = child, .else_block = else_block } }};
549
+ },
550
+ .if_block => {
551
+ var else_block: ?markup.MarkupNode = null;
552
+ if (index + 1 < children.len and children[index + 1].kind == .else_block) {
553
+ else_block = children[index + 1];
554
+ index += 1;
555
+ }
556
+ steps = steps ++ &[_]ChildStep{.{ .conditional = .{ .if_block = child, .else_block = else_block } }};
557
+ },
558
+ .else_block => fail(child, markup.else_placement_message),
559
+ .text => fail(child, "text content is only allowed inside text-bearing elements"),
560
+ }
561
+ }
562
+ return steps;
563
+ }
564
+ }
565
+
566
+ fn buildChildren(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype, out: *std.ArrayListUnmanaged(Ui.Node)) void {
567
+ buildChildList(comptime node.children, entries, ui, model, scope, out);
568
+ }
569
+
570
+ fn buildChildList(comptime children: []const markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype, out: *std.ArrayListUnmanaged(Ui.Node)) void {
571
+ const steps = comptime childSteps(children);
572
+ inline for (0..steps.len) |index| {
573
+ const step = comptime steps[index];
574
+ if (comptime (step == .element)) {
575
+ const built = buildElement(comptime step.element, entries, ui, model, scope);
576
+ out.append(ui.arena, built) catch {
577
+ ui.failed = true;
578
+ return;
579
+ };
580
+ } else if (comptime (step == .use)) {
581
+ const built = buildUse(comptime step.use, entries, ui, model, scope);
582
+ out.append(ui.arena, built) catch {
583
+ ui.failed = true;
584
+ return;
585
+ };
586
+ } else if (comptime (step == .slot)) {
587
+ buildSlot(comptime step.slot, entries, ui, model, scope, out);
588
+ } else if (comptime (step == .for_block)) {
589
+ const item_count = buildFor(comptime step.for_block.node, entries, ui, model, scope, out);
590
+ if (comptime (step.for_block.else_block != null)) {
591
+ if (item_count == 0) {
592
+ buildChildren(comptime step.for_block.else_block.?, entries, ui, model, scope, out);
593
+ }
594
+ }
595
+ } else {
596
+ const conditional = comptime step.conditional;
597
+ const test_value = comptime (conditional.if_block.attr("test") orelse fail(conditional.if_block, "if requires a test attribute"));
598
+ const condition = evalExpr(conditional.if_block, entries, test_value, ui, model, scope);
599
+ if (condition.truthy()) {
600
+ buildChildren(conditional.if_block, entries, ui, model, scope, out);
601
+ } else if (comptime (conditional.else_block != null)) {
602
+ buildChildren(comptime conditional.else_block.?, entries, ui, model, scope, out);
603
+ }
604
+ }
605
+ }
606
+ }
607
+
608
+ /// `<slot/>` in a template body: splice the use-site children (the
609
+ /// innermost slot capture) IN THE CONSUMER'S SCOPE — the capture
610
+ /// carries the consumer's comptime entries and its runtime scope
611
+ /// chain, so content sees the model paths and loop variables where
612
+ /// the `<use>` was written, while its nodes land at the slot's
613
+ /// position (structural ids hash identically to the interpreter's).
614
+ fn buildSlot(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype, out: *std.ArrayListUnmanaged(Ui.Node)) void {
615
+ const capture_index = comptime (innermostSlotIndex(entries) orelse fail(node, markup.slot_outside_template_message));
616
+ const capture = comptime entries[capture_index];
617
+ if (comptime (capture.slot_nodes.len == 0)) return;
618
+ const site_scope = scopePayload(entries, capture_index, scope);
619
+ buildChildList(comptime capture.slot_nodes, comptime capture.slot_entries, ui, model, site_scope, out);
620
+ }
621
+
622
+ fn innermostSlotIndex(comptime entries: []const ScopeEntry) ?usize {
623
+ comptime {
624
+ var index = entries.len;
625
+ while (index > 0) {
626
+ index -= 1;
627
+ if (entries[index].kind == .slot) return index;
628
+ }
629
+ return null;
630
+ }
631
+ }
632
+
633
+ /// Expands a `for` block; returns the item count so the caller can
634
+ /// render a trailing `<else>` for the empty case.
635
+ fn buildFor(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype, out: *std.ArrayListUnmanaged(Ui.Node)) usize {
636
+ const each = comptime (node.attr("each") orelse fail(node, "for requires an each attribute"));
637
+ comptime {
638
+ _ = node.attr("as") orelse fail(node, "for requires an as attribute");
639
+ if (node.children.len == 0) fail(node, markup.for_children_message);
640
+ for (node.children) |child| {
641
+ switch (child.kind) {
642
+ .element, .use_block, .for_block, .if_block, .else_block => {},
643
+ else => fail(child, markup.for_children_message),
644
+ }
645
+ }
646
+ }
647
+ // Comptime mirror of the interpreter's `each` resolution:
648
+ // slice-valued template args in scope shadow model iterables.
649
+ const scope_index_opt = comptime scopeIndex(entries, each);
650
+ if (comptime (scope_index_opt != null)) {
651
+ const scope_index = comptime scope_index_opt.?;
652
+ comptime {
653
+ if (entries[scope_index].kind != .slice_arg) {
654
+ fail(node, "each does not name an iterable (a model slice, array, or fn - or a slice-valued template arg)");
655
+ }
656
+ }
657
+ const items = scopePayload(entries, scope_index, scope);
658
+ buildForItems(comptime entries[scope_index].Item, node, entries, items, ui, model, scope, out);
659
+ return items.len;
660
+ }
661
+ const info = comptime (eachInfo(each) orelse fail(node, "each does not name an iterable (a model slice, array, or fn - or a slice-valued template arg)"));
662
+ const items = eachItems(info, ui, model);
663
+ buildForItems(info.Item, node, entries, items, ui, model, scope, out);
664
+ return items.len;
665
+ }
666
+
667
+ fn buildForItems(comptime ItemT: type, comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, items: []const ItemT, ui: *Ui, model: *const ModelT, scope: anytype, out: *std.ArrayListUnmanaged(Ui.Node)) void {
668
+ const as_name = comptime node.attr("as").?;
669
+ const child_entries = comptime (entries ++ &[_]ScopeEntry{.{ .name = as_name, .kind = .item, .Item = ItemT }});
670
+ for (items) |*item| {
671
+ const child_scope = .{ .parent = scope, .item = @as(*const ItemT, item) };
672
+ const first_emitted = out.items.len;
673
+ buildChildren(node, child_entries, ui, model, child_scope, out);
674
+ if (comptime (node.attr("key") != null)) {
675
+ // Mirror of the interpreter: the item key stamps every
676
+ // node this item emitted (unless the node claims its
677
+ // own identity); later slots get a slot-suffixed key.
678
+ const base = itemKey(ItemT, node, comptime node.attr("key").?, ui, item);
679
+ for (out.items[first_emitted..], 0..) |*built, slot| {
680
+ if (built.key == null and built.global_key == null) {
681
+ built.key = canvas.forSlotKey(ui.arena, base, slot) catch {
682
+ ui.failed = true;
683
+ return;
684
+ };
685
+ }
686
+ }
687
+ }
688
+ }
689
+ }
690
+
691
+ // ------------------------------------------------------- markdown
692
+
693
+ const Md = canvas.markdown.Markdown(MsgT);
694
+
695
+ /// Comptime mirror of the interpreter's `buildMarkdown`: attrs,
696
+ /// message tags, and the details-expanded source resolve at
697
+ /// comptime; only the source string and expanded flags are read at
698
+ /// runtime. Misuse fails compilation with the interpreter's
699
+ /// message.
700
+ fn buildMarkdown(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype) Ui.Node {
701
+ comptime {
702
+ if (node.children.len != 0) fail(node.children[0], markup.markdown_children_message);
703
+ for (node.attrs) |attribute| {
704
+ if (std.mem.eql(u8, attribute.name, "kind")) continue;
705
+ if (std.mem.eql(u8, attribute.name, "source")) continue;
706
+ if (std.mem.eql(u8, attribute.name, "on-link")) continue;
707
+ if (std.mem.eql(u8, attribute.name, "on-details")) continue;
708
+ if (std.mem.eql(u8, attribute.name, "details-expanded")) continue;
709
+ if (std.mem.eql(u8, attribute.name, "issue-link-base")) continue;
710
+ fail(node, markup.markdown_attr_message);
711
+ }
712
+ }
713
+ const source_path = comptime blk: {
714
+ const raw = node.attr("source") orelse fail(node, markup.markdown_source_message);
715
+ const expression = markup.parseAttrExpression(raw) orelse fail(node, markup.markdown_source_message);
716
+ if (expression != .binding) fail(node, markup.markdown_source_message);
717
+ break :blk expression.binding;
718
+ };
719
+ comptime requireVariant(pathVariant(node, entries, source_path, true), &.{.string}, node, markup.markdown_source_message);
720
+ const source_text = switch (bindingValue(node, entries, source_path, ui, model, scope, true)) {
721
+ .string => |text| text,
722
+ else => runtimeFail([]const u8, ui),
723
+ };
724
+
725
+ var options: Md.Options = .{};
726
+ if (comptime (node.attr("on-link") != null)) {
727
+ options.on_link = comptime markdownLinkConstructor(node, node.attr("on-link").?);
728
+ }
729
+ if (comptime (node.attr("on-details") != null)) {
730
+ options.on_details = comptime markdownDetailsConstructor(node, node.attr("on-details").?);
731
+ }
732
+ if (comptime (node.attr("details-expanded") != null)) {
733
+ options.details_expanded = detailsExpandedItems(node, entries, comptime node.attr("details-expanded").?, ui, model, scope);
734
+ }
735
+ if (comptime (node.attr("issue-link-base") != null)) {
736
+ const raw = comptime node.attr("issue-link-base").?;
737
+ comptime {
738
+ const expression = markup.parseAttrExpression(raw) orelse fail(node, markup.markdown_issue_link_base_message);
739
+ if (expression == .equals) fail(node, markup.markdown_issue_link_base_message);
740
+ }
741
+ comptime requireVariant(exprVariant(node, entries, raw), &.{.string}, node, markup.markdown_issue_link_base_message);
742
+ const base = switch (evalExpr(node, entries, raw, ui, model, scope)) {
743
+ .string => |text| text,
744
+ else => runtimeFail([]const u8, ui),
745
+ };
746
+ if (base.len > 0) options.issue_link_base = base;
747
+ }
748
+ return Md.view(ui, source_text, options);
749
+ }
750
+
751
+ fn markdownLinkConstructor(comptime node: markup.MarkupNode, comptime raw: []const u8) Ui.LinkMsgFn {
752
+ comptime {
753
+ @setEvalBranchQuota(10_000);
754
+ const expression = markup.parseMessageExpression(raw) orelse fail(node, markup.markdown_on_link_message);
755
+ if (expression.payload.len != 0) fail(node, markup.markdown_on_link_message);
756
+ for (@typeInfo(MsgT).@"union".fields) |field| {
757
+ if (field.type == []const u8 and std.mem.eql(u8, field.name, expression.tag)) {
758
+ return Ui.linkMsg(@field(std.meta.Tag(MsgT), field.name));
759
+ }
760
+ }
761
+ fail(node, markup.markdown_on_link_message);
762
+ }
763
+ }
764
+
765
+ fn markdownDetailsConstructor(comptime node: markup.MarkupNode, comptime raw: []const u8) *const fn (index: usize) MsgT {
766
+ comptime {
767
+ @setEvalBranchQuota(10_000);
768
+ const expression = markup.parseMessageExpression(raw) orelse fail(node, markup.markdown_on_details_message);
769
+ if (expression.payload.len != 0) fail(node, markup.markdown_on_details_message);
770
+ for (@typeInfo(MsgT).@"union".fields) |field| {
771
+ if (field.type == usize and std.mem.eql(u8, field.name, expression.tag)) {
772
+ return Md.detailsMsg(@field(std.meta.Tag(MsgT), field.name));
773
+ }
774
+ }
775
+ fail(node, markup.markdown_on_details_message);
776
+ }
777
+ }
778
+
779
+ /// Resolve `details-expanded` through the same sources `for each`
780
+ /// accepts (scope slice args shadow model iterables), requiring a
781
+ /// bool element type at comptime.
782
+ fn detailsExpandedItems(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, comptime raw: []const u8, ui: *Ui, model: *const ModelT, scope: anytype) []const bool {
783
+ const path = comptime blk: {
784
+ const expression = markup.parseAttrExpression(raw) orelse fail(node, markup.markdown_details_expanded_message);
785
+ if (expression != .binding) fail(node, markup.markdown_details_expanded_message);
786
+ break :blk expression.binding;
787
+ };
788
+ const scope_index_opt = comptime scopeIndex(entries, path);
789
+ if (comptime (scope_index_opt != null)) {
790
+ const scope_index = comptime scope_index_opt.?;
791
+ comptime {
792
+ if (entries[scope_index].kind != .slice_arg or entries[scope_index].Item != bool) {
793
+ fail(node, markup.markdown_details_expanded_message);
794
+ }
795
+ }
796
+ return scopePayload(entries, scope_index, scope);
797
+ }
798
+ const info = comptime (eachInfo(path) orelse fail(node, markup.markdown_details_expanded_message));
799
+ comptime {
800
+ if (info.Item != bool) fail(node, markup.markdown_details_expanded_message);
801
+ }
802
+ return eachItems(info, ui, model);
803
+ }
804
+
805
+ // ------------------------------------------------ stepper/timeline
806
+
807
+ /// Comptime mirror of the interpreter's `buildStepper`: attrs and
808
+ /// step structure resolve at comptime; the active index and step
809
+ /// labels are read at runtime. Misuse fails compilation with the
810
+ /// interpreter's message.
811
+ fn buildStepper(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype) Ui.Node {
812
+ comptime {
813
+ for (node.attrs) |attribute| {
814
+ if (std.mem.eql(u8, attribute.name, "kind")) continue;
815
+ if (std.mem.eql(u8, attribute.name, "active")) continue;
816
+ if (std.mem.eql(u8, attribute.name, "key")) continue;
817
+ if (std.mem.eql(u8, attribute.name, "global-key")) continue;
818
+ if (std.mem.eql(u8, attribute.name, "label")) continue;
819
+ fail(node, markup.stepper_attr_message);
820
+ }
821
+ if (node.attr("active") == null) fail(node, markup.stepper_active_message);
822
+ for (node.children) |child| {
823
+ if (child.kind != .element or !std.mem.eql(u8, child.name, "step")) {
824
+ fail(child, markup.stepper_children_message);
825
+ }
826
+ for (child.attrs) |attribute| {
827
+ if (!std.mem.eql(u8, attribute.name, "kind")) fail(child, markup.step_attr_message);
828
+ }
829
+ }
830
+ }
831
+ var options: Ui.StepperOptions = .{};
832
+ const active_raw = comptime node.attr("active").?;
833
+ comptime requireVariant(exprVariant(node, entries, active_raw), &.{.integer}, node, markup.stepper_active_message);
834
+ options.active = switch (evalExpr(node, entries, active_raw, ui, model, scope)) {
835
+ .integer => |int| if (int < 0) 0 else @intCast(int),
836
+ else => runtimeFail(usize, ui),
837
+ };
838
+ if (comptime (node.attr("key") != null)) {
839
+ options.key = attrKey(node, entries, comptime node.attr("key").?, ui, model, scope, "keys must be integers or strings");
840
+ }
841
+ if (comptime (node.attr("global-key") != null)) {
842
+ options.global_key = attrKey(node, entries, comptime node.attr("global-key").?, ui, model, scope, "keys must be integers or strings");
843
+ }
844
+ if (comptime (node.attr("label") != null)) {
845
+ options.semantics.label = stringAttr(node, entries, comptime node.attr("label").?, ui, model, scope, "label expects text");
846
+ }
847
+ const steps = ui.arena.alloc(Ui.StepperStep, node.children.len) catch {
848
+ ui.failed = true;
849
+ return ui.el(.row, .{}, .{});
850
+ };
851
+ inline for (0..node.children.len) |index| {
852
+ steps[index] = .{ .label = interpolatedText(comptime node.children[index], entries, ui, model, scope) };
853
+ }
854
+ return ui.stepper(options, steps);
855
+ }
856
+
857
+ /// Comptime mirror of the interpreter's `buildInputGroup`: the
858
+ /// closed attribute set and the static child shape (one textarea
859
+ /// first, then at most one actions row) check at comptime with
860
+ /// the interpreter's messages; the textarea builds through the
861
+ /// ordinary element path and the group lowers through
862
+ /// `Ui.inputGroup`.
863
+ fn buildInputGroup(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype) Ui.Node {
864
+ comptime {
865
+ for (node.attrs) |attribute| {
866
+ if (std.mem.eql(u8, attribute.name, "kind")) continue;
867
+ if (std.mem.eql(u8, attribute.name, "label")) continue;
868
+ if (std.mem.eql(u8, attribute.name, "width")) continue;
869
+ if (std.mem.eql(u8, attribute.name, "height")) continue;
870
+ if (std.mem.eql(u8, attribute.name, "min-width")) continue;
871
+ if (std.mem.eql(u8, attribute.name, "grow")) continue;
872
+ if (std.mem.eql(u8, attribute.name, "key")) continue;
873
+ if (std.mem.eql(u8, attribute.name, "global-key")) continue;
874
+ fail(node, markup.input_group_attr_message);
875
+ }
876
+ var textarea_count: usize = 0;
877
+ var actions_count: usize = 0;
878
+ for (node.children) |child| {
879
+ if (child.kind != .element) fail(child, markup.input_group_children_message);
880
+ if (std.mem.eql(u8, child.name, "textarea")) {
881
+ if (textarea_count > 0 or actions_count > 0) fail(child, markup.input_group_children_message);
882
+ textarea_count += 1;
883
+ continue;
884
+ }
885
+ if (std.mem.eql(u8, child.name, "input-group-actions")) {
886
+ if (textarea_count == 0) fail(child, markup.input_group_textarea_message);
887
+ if (actions_count > 0) fail(child, markup.input_group_children_message);
888
+ actions_count += 1;
889
+ continue;
890
+ }
891
+ fail(child, markup.input_group_children_message);
892
+ }
893
+ if (textarea_count == 0) fail(node, markup.input_group_textarea_message);
894
+ }
895
+ var options: Ui.InputGroupOptions = .{};
896
+ if (comptime (node.attr("label") != null)) {
897
+ options.semantics.label = stringAttr(node, entries, comptime node.attr("label").?, ui, model, scope, "label expects text");
898
+ }
899
+ if (comptime (node.attr("width") != null)) {
900
+ options.width = floatAttr(node, entries, comptime node.attr("width").?, ui, model, scope);
901
+ }
902
+ if (comptime (node.attr("height") != null)) {
903
+ options.height = floatAttr(node, entries, comptime node.attr("height").?, ui, model, scope);
904
+ }
905
+ if (comptime (node.attr("min-width") != null)) {
906
+ options.min_width = floatAttr(node, entries, comptime node.attr("min-width").?, ui, model, scope);
907
+ }
908
+ if (comptime (node.attr("grow") != null)) {
909
+ options.grow = floatAttr(node, entries, comptime node.attr("grow").?, ui, model, scope);
910
+ }
911
+ if (comptime (node.attr("key") != null)) {
912
+ options.key = attrKey(node, entries, comptime node.attr("key").?, ui, model, scope, "keys must be integers or strings");
913
+ }
914
+ if (comptime (node.attr("global-key") != null)) {
915
+ options.global_key = attrKey(node, entries, comptime node.attr("global-key").?, ui, model, scope, "keys must be integers or strings");
916
+ }
917
+ const entry = buildElement(comptime node.children[0], entries, ui, model, scope);
918
+ var actions: ?Ui.Node = null;
919
+ if (comptime (node.children.len > 1)) {
920
+ actions = buildInputGroupActions(comptime node.children[1], entries, ui, model, scope);
921
+ }
922
+ return ui.inputGroup(options, entry, actions);
923
+ }
924
+
925
+ /// Comptime mirror of the interpreter's `buildInputGroupActions`.
926
+ fn buildInputGroupActions(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype) Ui.Node {
927
+ comptime {
928
+ for (node.attrs) |attribute| {
929
+ if (std.mem.eql(u8, attribute.name, "kind")) continue;
930
+ if (std.mem.eql(u8, attribute.name, "gap")) continue;
931
+ if (std.mem.eql(u8, attribute.name, "key")) continue;
932
+ if (std.mem.eql(u8, attribute.name, "global-key")) continue;
933
+ fail(node, markup.input_group_actions_attr_message);
934
+ }
935
+ for (node.children) |child| {
936
+ if (child.kind == .text) fail(child, markup.input_group_actions_children_message);
937
+ }
938
+ }
939
+ var options: Ui.InputGroupActionsOptions = .{};
940
+ if (comptime (node.attr("gap") != null)) {
941
+ options.gap = floatAttr(node, entries, comptime node.attr("gap").?, ui, model, scope);
942
+ }
943
+ if (comptime (node.attr("key") != null)) {
944
+ options.key = attrKey(node, entries, comptime node.attr("key").?, ui, model, scope, "keys must be integers or strings");
945
+ }
946
+ if (comptime (node.attr("global-key") != null)) {
947
+ options.global_key = attrKey(node, entries, comptime node.attr("global-key").?, ui, model, scope, "keys must be integers or strings");
948
+ }
949
+ var children: std.ArrayListUnmanaged(Ui.Node) = .empty;
950
+ buildChildren(node, entries, ui, model, scope, &children);
951
+ return ui.inputGroupActions(options, @as([]const Ui.Node, children.items));
952
+ }
953
+
954
+ /// Comptime mirror of the interpreter's `buildTimeline`.
955
+ fn buildTimeline(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype) Ui.Node {
956
+ comptime {
957
+ for (node.attrs) |attribute| {
958
+ if (std.mem.eql(u8, attribute.name, "kind")) continue;
959
+ if (std.mem.eql(u8, attribute.name, "gap")) continue;
960
+ if (std.mem.eql(u8, attribute.name, "grow")) continue;
961
+ if (std.mem.eql(u8, attribute.name, "key")) continue;
962
+ if (std.mem.eql(u8, attribute.name, "global-key")) continue;
963
+ if (std.mem.eql(u8, attribute.name, "label")) continue;
964
+ fail(node, markup.timeline_attr_message);
965
+ }
966
+ }
967
+ var options: Ui.TimelineOptions = .{};
968
+ if (comptime (node.attr("gap") != null)) {
969
+ options.gap = floatAttr(node, entries, comptime node.attr("gap").?, ui, model, scope);
970
+ }
971
+ if (comptime (node.attr("grow") != null)) {
972
+ options.grow = floatAttr(node, entries, comptime node.attr("grow").?, ui, model, scope);
973
+ }
974
+ if (comptime (node.attr("key") != null)) {
975
+ options.key = attrKey(node, entries, comptime node.attr("key").?, ui, model, scope, "keys must be integers or strings");
976
+ }
977
+ if (comptime (node.attr("global-key") != null)) {
978
+ options.global_key = attrKey(node, entries, comptime node.attr("global-key").?, ui, model, scope, "keys must be integers or strings");
979
+ }
980
+ if (comptime (node.attr("label") != null)) {
981
+ options.semantics.label = stringAttr(node, entries, comptime node.attr("label").?, ui, model, scope, "label expects text");
982
+ }
983
+ var children: std.ArrayListUnmanaged(Ui.Node) = .empty;
984
+ buildChildren(node, entries, ui, model, scope, &children);
985
+ return ui.timeline(options, @as([]const Ui.Node, children.items));
986
+ }
987
+
988
+ /// Comptime mirror of the interpreter's `buildTimelineItem`.
989
+ fn buildTimelineItem(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype) Ui.Node {
990
+ comptime {
991
+ if (node.children.len != 0) fail(node.children[0], markup.timeline_item_children_message);
992
+ if (node.attr("title") == null) fail(node, markup.timeline_item_title_message);
993
+ for (node.attrs) |attribute| {
994
+ if (std.mem.eql(u8, attribute.name, "kind")) continue;
995
+ if (std.mem.eql(u8, attribute.name, "title")) continue;
996
+ if (std.mem.eql(u8, attribute.name, "description")) continue;
997
+ if (std.mem.eql(u8, attribute.name, "meta")) continue;
998
+ if (std.mem.eql(u8, attribute.name, "indicator")) continue;
999
+ if (std.mem.eql(u8, attribute.name, "icon")) continue;
1000
+ if (std.mem.eql(u8, attribute.name, "variant")) continue;
1001
+ if (std.mem.eql(u8, attribute.name, "connector")) continue;
1002
+ if (std.mem.eql(u8, attribute.name, "selected")) continue;
1003
+ if (std.mem.eql(u8, attribute.name, "on-press")) continue;
1004
+ if (std.mem.startsWith(u8, attribute.name, "on-")) fail(node, markup.timeline_item_press_only_message);
1005
+ if (std.mem.eql(u8, attribute.name, "key")) continue;
1006
+ if (std.mem.eql(u8, attribute.name, "global-key")) continue;
1007
+ fail(node, markup.timeline_item_attr_message);
1008
+ }
1009
+ }
1010
+ var options: Ui.TimelineItemOptions = .{ .title = "" };
1011
+ options.title = stringAttr(node, entries, comptime node.attr("title").?, ui, model, scope, markup.timeline_item_text_attr_message);
1012
+ if (comptime (node.attr("description") != null)) {
1013
+ options.description = stringAttr(node, entries, comptime node.attr("description").?, ui, model, scope, markup.timeline_item_text_attr_message);
1014
+ }
1015
+ if (comptime (node.attr("meta") != null)) {
1016
+ options.meta = stringAttr(node, entries, comptime node.attr("meta").?, ui, model, scope, markup.timeline_item_text_attr_message);
1017
+ }
1018
+ if (comptime (node.attr("indicator") != null)) {
1019
+ options.indicator = stringAttr(node, entries, comptime node.attr("indicator").?, ui, model, scope, markup.timeline_item_text_attr_message);
1020
+ }
1021
+ if (comptime (node.attr("icon") != null)) {
1022
+ // Vector icon indicator: the shared icon value grammar,
1023
+ // resolved at comptime like every icon attribute.
1024
+ switch (comptime iconValueChecked(node, node.attr("icon").?, markup.button_icon_message)) {
1025
+ .builtin, .app => |name| options.icon = name,
1026
+ .binding => options.icon = stringAttr(node, entries, comptime node.attr("icon").?, ui, model, scope, markup.button_icon_message),
1027
+ .invalid => unreachable,
1028
+ }
1029
+ }
1030
+ if (comptime (node.attr("variant") != null)) {
1031
+ const raw = comptime node.attr("variant").?;
1032
+ comptime requireVariant(exprVariant(node, entries, raw), &.{.string}, node, "expected an option name");
1033
+ const expression = comptime markup.parseAttrExpression(raw).?;
1034
+ if (comptime (expression == .literal)) {
1035
+ options.variant = comptime (std.meta.stringToEnum(canvas.WidgetVariant, expression.literal) orelse fail(node, "unknown option value"));
1036
+ } else {
1037
+ const text = switch (evalExpr(node, entries, raw, ui, model, scope)) {
1038
+ .string => |text| text,
1039
+ else => runtimeFail([]const u8, ui),
1040
+ };
1041
+ options.variant = std.meta.stringToEnum(canvas.WidgetVariant, text) orelse runtimeFail(canvas.WidgetVariant, ui);
1042
+ }
1043
+ }
1044
+ if (comptime (node.attr("connector") != null)) {
1045
+ options.connector = evalExpr(node, entries, comptime node.attr("connector").?, ui, model, scope).truthy();
1046
+ }
1047
+ if (comptime (node.attr("selected") != null)) {
1048
+ options.selected = evalExpr(node, entries, comptime node.attr("selected").?, ui, model, scope).truthy();
1049
+ }
1050
+ if (comptime (node.attr("on-press") != null)) {
1051
+ // Reuse the full message-attr machinery (payload bindings
1052
+ // included) through a scratch options value.
1053
+ const press_index = comptime blk: {
1054
+ for (node.attrs, 0..) |attribute, index| {
1055
+ if (std.mem.eql(u8, attribute.name, "on-press")) break :blk index;
1056
+ }
1057
+ unreachable;
1058
+ };
1059
+ var scratch: Ui.ElementOptions = .{};
1060
+ applyMessageAttr(node, comptime node.attrs[press_index], entries, ui, model, scope, &scratch);
1061
+ options.on_press = scratch.on_press;
1062
+ }
1063
+ if (comptime (node.attr("key") != null)) {
1064
+ options.key = attrKey(node, entries, comptime node.attr("key").?, ui, model, scope, "keys must be integers or strings");
1065
+ }
1066
+ if (comptime (node.attr("global-key") != null)) {
1067
+ options.global_key = attrKey(node, entries, comptime node.attr("global-key").?, ui, model, scope, "keys must be integers or strings");
1068
+ }
1069
+ return ui.timelineItem(options);
1070
+ }
1071
+
1072
+ // ---------------------------------------------------------- chart
1073
+
1074
+ /// Comptime mirror of the interpreter's `buildChart`: the closed
1075
+ /// attribute set, the static series children, and every series'
1076
+ /// kind/color/values resolution happen at comptime; only the data
1077
+ /// slices and scalar bindings are read at runtime. Misuse fails
1078
+ /// compilation with the interpreter's message.
1079
+ fn buildChart(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype) Ui.Node {
1080
+ comptime {
1081
+ for (node.attrs) |attribute| {
1082
+ if (std.mem.eql(u8, attribute.name, "kind")) continue;
1083
+ if (std.mem.startsWith(u8, attribute.name, "on-")) {
1084
+ fail(node, markup.chart_display_only_message);
1085
+ }
1086
+ const known = std.mem.eql(u8, attribute.name, "y-min") or
1087
+ std.mem.eql(u8, attribute.name, "y-max") or
1088
+ std.mem.eql(u8, attribute.name, "grid-lines") or
1089
+ std.mem.eql(u8, attribute.name, "baseline") or
1090
+ std.mem.eql(u8, attribute.name, "x-labels") or
1091
+ std.mem.eql(u8, attribute.name, "y-labels") or
1092
+ std.mem.eql(u8, attribute.name, "hover-details") or
1093
+ std.mem.eql(u8, attribute.name, "stroke-width") or
1094
+ std.mem.eql(u8, attribute.name, "width") or
1095
+ std.mem.eql(u8, attribute.name, "height") or
1096
+ std.mem.eql(u8, attribute.name, "grow") or
1097
+ std.mem.eql(u8, attribute.name, "padding") or
1098
+ std.mem.eql(u8, attribute.name, "key") or
1099
+ std.mem.eql(u8, attribute.name, "global-key") or
1100
+ std.mem.eql(u8, attribute.name, "label");
1101
+ if (!known) fail(node, markup.chart_attr_message);
1102
+ }
1103
+ if (node.children.len == 0) fail(node, markup.chart_series_required_message);
1104
+ for (node.children) |child| {
1105
+ if (child.kind != .element or !std.mem.eql(u8, child.name, "series")) {
1106
+ fail(child, markup.chart_children_message);
1107
+ }
1108
+ }
1109
+ }
1110
+ var options: Ui.ChartOptions = .{};
1111
+ if (comptime (node.attr("y-min") != null)) {
1112
+ options.y_min = floatAttr(node, entries, comptime node.attr("y-min").?, ui, model, scope);
1113
+ }
1114
+ if (comptime (node.attr("y-max") != null)) {
1115
+ options.y_max = floatAttr(node, entries, comptime node.attr("y-max").?, ui, model, scope);
1116
+ }
1117
+ if (comptime (node.attr("grid-lines") != null)) {
1118
+ const raw = comptime node.attr("grid-lines").?;
1119
+ comptime requireVariant(exprVariant(node, entries, raw), &.{.integer}, node, "expected a whole number");
1120
+ options.grid_lines = switch (evalExpr(node, entries, raw, ui, model, scope)) {
1121
+ .integer => |int| if (int < 0 or int > std.math.maxInt(u8)) runtimeFail(u8, ui) else @intCast(int),
1122
+ else => runtimeFail(u8, ui),
1123
+ };
1124
+ }
1125
+ if (comptime (node.attr("baseline") != null)) {
1126
+ options.baseline = evalExpr(node, entries, comptime node.attr("baseline").?, ui, model, scope).truthy();
1127
+ }
1128
+ if (comptime (node.attr("x-labels") != null)) {
1129
+ options.x_labels = chartLabelsItems(node, entries, comptime node.attr("x-labels").?, ui, model, scope);
1130
+ }
1131
+ if (comptime (node.attr("y-labels") != null)) {
1132
+ options.y_labels = evalExpr(node, entries, comptime node.attr("y-labels").?, ui, model, scope).truthy();
1133
+ }
1134
+ if (comptime (node.attr("hover-details") != null)) {
1135
+ options.hover_details = evalExpr(node, entries, comptime node.attr("hover-details").?, ui, model, scope).truthy();
1136
+ }
1137
+ if (comptime (node.attr("stroke-width") != null)) {
1138
+ options.stroke_width = floatAttr(node, entries, comptime node.attr("stroke-width").?, ui, model, scope);
1139
+ }
1140
+ if (comptime (node.attr("width") != null)) {
1141
+ options.width = floatAttr(node, entries, comptime node.attr("width").?, ui, model, scope);
1142
+ }
1143
+ if (comptime (node.attr("height") != null)) {
1144
+ options.height = floatAttr(node, entries, comptime node.attr("height").?, ui, model, scope);
1145
+ }
1146
+ if (comptime (node.attr("grow") != null)) {
1147
+ options.grow = floatAttr(node, entries, comptime node.attr("grow").?, ui, model, scope);
1148
+ }
1149
+ if (comptime (node.attr("padding") != null)) {
1150
+ options.padding = floatAttr(node, entries, comptime node.attr("padding").?, ui, model, scope);
1151
+ }
1152
+ if (comptime (node.attr("key") != null)) {
1153
+ options.key = attrKey(node, entries, comptime node.attr("key").?, ui, model, scope, "keys must be integers or strings");
1154
+ }
1155
+ if (comptime (node.attr("global-key") != null)) {
1156
+ options.global_key = attrKey(node, entries, comptime node.attr("global-key").?, ui, model, scope, "keys must be integers or strings");
1157
+ }
1158
+ if (comptime (node.attr("label") != null)) {
1159
+ options.semantics.label = stringAttr(node, entries, comptime node.attr("label").?, ui, model, scope, "label expects text");
1160
+ }
1161
+ const series = ui.arena.alloc(canvas.ChartSeries, node.children.len) catch {
1162
+ ui.failed = true;
1163
+ return ui.el(.row, .{}, .{});
1164
+ };
1165
+ inline for (0..node.children.len) |index| {
1166
+ series[index] = buildSeries(comptime node.children[index], entries, ui, model, scope);
1167
+ }
1168
+ return ui.chart(options, series);
1169
+ }
1170
+
1171
+ /// Comptime mirror of the interpreter's `buildSeries`: kind and
1172
+ /// color are closed literal vocabularies resolved at comptime,
1173
+ /// values must name an f32 iterable, label is ordinary text.
1174
+ fn buildSeries(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype) canvas.ChartSeries {
1175
+ comptime {
1176
+ if (node.children.len != 0) fail(node.children[0], markup.series_children_message);
1177
+ if (node.attr("values") == null) fail(node, markup.series_values_message);
1178
+ for (node.attrs) |attribute| {
1179
+ if (std.mem.eql(u8, attribute.name, "kind")) continue;
1180
+ if (std.mem.eql(u8, attribute.name, "values")) continue;
1181
+ if (std.mem.eql(u8, attribute.name, "color")) continue;
1182
+ if (std.mem.eql(u8, attribute.name, "label")) continue;
1183
+ fail(node, markup.series_attr_message);
1184
+ }
1185
+ }
1186
+ var series = canvas.ChartSeries{};
1187
+ if (comptime (node.attr("kind") != null)) {
1188
+ const spelled = comptime blk: {
1189
+ const expression = markup.parseAttrExpression(node.attr("kind").?) orelse fail(node, markup.series_kind_message);
1190
+ if (expression != .literal) fail(node, markup.series_kind_message);
1191
+ break :blk expression.literal;
1192
+ };
1193
+ if (comptime std.mem.eql(u8, spelled, "line")) {
1194
+ series.kind = .line;
1195
+ } else if (comptime std.mem.eql(u8, spelled, "area")) {
1196
+ // Area is the markup spelling of a filled line.
1197
+ series.kind = .line;
1198
+ series.fill = true;
1199
+ } else if (comptime std.mem.eql(u8, spelled, "bar")) {
1200
+ series.kind = .bar;
1201
+ } else {
1202
+ comptime fail(node, markup.series_kind_message);
1203
+ }
1204
+ }
1205
+ series.values = chartValuesItems(node, entries, comptime node.attr("values").?, ui, model, scope);
1206
+ if (comptime (node.attr("color") != null)) {
1207
+ series.color = comptime blk: {
1208
+ const expression = markup.parseAttrExpression(node.attr("color").?) orelse fail(node, markup.series_color_message);
1209
+ if (expression != .literal) fail(node, markup.series_color_message);
1210
+ break :blk std.meta.stringToEnum(canvas.ChartSeriesColor, expression.literal) orelse
1211
+ fail(node, markup.series_color_message);
1212
+ };
1213
+ }
1214
+ if (comptime (node.attr("label") != null)) {
1215
+ series.label = stringAttr(node, entries, comptime node.attr("label").?, ui, model, scope, markup.series_label_message);
1216
+ }
1217
+ return series;
1218
+ }
1219
+
1220
+ /// Resolve a chart `x-labels` binding through the same sources
1221
+ /// `for each` accepts, requiring a string element type at
1222
+ /// comptime — the label twin of `chartValuesItems`.
1223
+ fn chartLabelsItems(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, comptime raw: []const u8, ui: *Ui, model: *const ModelT, scope: anytype) []const []const u8 {
1224
+ const path = comptime blk: {
1225
+ const expression = markup.parseAttrExpression(raw) orelse fail(node, markup.chart_x_labels_message);
1226
+ if (expression != .binding) fail(node, markup.chart_x_labels_message);
1227
+ break :blk expression.binding;
1228
+ };
1229
+ const scope_index_opt = comptime scopeIndex(entries, path);
1230
+ if (comptime (scope_index_opt != null)) {
1231
+ const scope_index = comptime scope_index_opt.?;
1232
+ comptime {
1233
+ if (entries[scope_index].kind != .slice_arg or entries[scope_index].Item != []const u8) {
1234
+ fail(node, markup.chart_x_labels_message);
1235
+ }
1236
+ }
1237
+ return scopePayload(entries, scope_index, scope);
1238
+ }
1239
+ const info = comptime (eachInfo(path) orelse fail(node, markup.chart_x_labels_message));
1240
+ comptime {
1241
+ if (info.Item != []const u8) fail(node, markup.chart_x_labels_message);
1242
+ }
1243
+ return eachItems(info, ui, model);
1244
+ }
1245
+
1246
+ /// Resolve a series `values` binding through the same sources
1247
+ /// `for each` accepts (scope slice args shadow model iterables),
1248
+ /// requiring an f32 element type at comptime.
1249
+ fn chartValuesItems(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, comptime raw: []const u8, ui: *Ui, model: *const ModelT, scope: anytype) []const f32 {
1250
+ const path = comptime blk: {
1251
+ const expression = markup.parseAttrExpression(raw) orelse fail(node, markup.series_values_message);
1252
+ if (expression != .binding) fail(node, markup.series_values_message);
1253
+ break :blk expression.binding;
1254
+ };
1255
+ const scope_index_opt = comptime scopeIndex(entries, path);
1256
+ if (comptime (scope_index_opt != null)) {
1257
+ const scope_index = comptime scope_index_opt.?;
1258
+ comptime {
1259
+ if (entries[scope_index].kind != .slice_arg or entries[scope_index].Item != f32) {
1260
+ fail(node, markup.series_values_message);
1261
+ }
1262
+ }
1263
+ return scopePayload(entries, scope_index, scope);
1264
+ }
1265
+ const info = comptime (eachInfo(path) orelse fail(node, markup.series_values_message));
1266
+ comptime {
1267
+ if (info.Item != f32) fail(node, markup.series_values_message);
1268
+ }
1269
+ return eachItems(info, ui, model);
1270
+ }
1271
+
1272
+ /// Comptime icon-value classification with the invalid arm turned
1273
+ /// into a positioned compile error: the icon leaf, the inline
1274
+ /// icon attribute, and the timeline-item indicator all funnel
1275
+ /// through the one shared grammar (`markup.iconValueOf`).
1276
+ fn iconValueChecked(comptime node: markup.MarkupNode, comptime raw: []const u8, comptime base_message: []const u8) markup.IconValue {
1277
+ comptime {
1278
+ const value = markup.iconValueOf(raw, base_message);
1279
+ if (value == .invalid) fail(node, value.invalid);
1280
+ return value;
1281
+ }
1282
+ }
1283
+
1284
+ fn stringAttr(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, comptime raw: []const u8, ui: *Ui, model: *const ModelT, scope: anytype, comptime message: []const u8) []const u8 {
1285
+ comptime requireVariant(exprVariant(node, entries, raw), &.{.string}, node, message);
1286
+ return switch (evalExpr(node, entries, raw, ui, model, scope)) {
1287
+ .string => |text| text,
1288
+ else => runtimeFail([]const u8, ui),
1289
+ };
1290
+ }
1291
+
1292
+ fn floatAttr(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, comptime raw: []const u8, ui: *Ui, model: *const ModelT, scope: anytype) f32 {
1293
+ comptime requireVariant(exprVariant(node, entries, raw), &.{ .float, .integer }, node, "expected a number");
1294
+ return switch (evalExpr(node, entries, raw, ui, model, scope)) {
1295
+ .float => |float| float,
1296
+ .integer => |int| @floatFromInt(int),
1297
+ else => runtimeFail(f32, ui),
1298
+ };
1299
+ }
1300
+
1301
+ // ------------------------------------------------------ templates
1302
+
1303
+ /// A `<use>` arg as resolved at comptime against the use site: a
1304
+ /// scalar `Value` (literal, equality, scalar binding, or the
1305
+ /// declaration's literal default when the use site omits it) or a
1306
+ /// slice (a model iterable path, or a slice arg re-passed from an
1307
+ /// enclosing template).
1308
+ const ArgSpec = struct {
1309
+ name: []const u8,
1310
+ raw: []const u8,
1311
+ kind: Kind,
1312
+ Item: type = void,
1313
+ variant: ?ValueVariant = null,
1314
+ each: ?EachInfo = null,
1315
+ site_index: ?usize = null,
1316
+ /// True when `raw` is the declaration's default literal (the
1317
+ /// use site omitted the arg): evaluated as a literal, never
1318
+ /// parsed as an expression.
1319
+ defaulted: bool = false,
1320
+
1321
+ const Kind = enum { value, slice };
1322
+ };
1323
+
1324
+ /// Expand a `<use>` site: resolve the template, check its declared
1325
+ /// args against the use attributes, evaluate the args against the
1326
+ /// use-site scope, and inline the template's single element child
1327
+ /// in place — structural ids hash through the parent chain at the
1328
+ /// expansion site, exactly as if the body were written inline. The
1329
+ /// body's scope holds the args plus the slot capture (the use-site
1330
+ /// children and the consumer's scope chain, consumed by the body's
1331
+ /// `<slot/>`), never the use site's loop variables directly.
1332
+ fn buildUse(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype) Ui.Node {
1333
+ const template_name = comptime (node.attr("template") orelse fail(node, markup.use_template_attr_message));
1334
+ const template_index = comptime (document.templateIndex(template_name) orelse fail(node, markup.use_undefined_template_message));
1335
+ const template_node = comptime document.templates[template_index];
1336
+ comptime {
1337
+ if (template_node.children.len != 1 or template_node.children[0].kind != .element) {
1338
+ fail(template_node, markup.template_one_child_message);
1339
+ }
1340
+ if (node.children.len != 0 and markup.templateSlot(template_node) == null) {
1341
+ fail(node.children[0], markup.use_children_without_slot_message);
1342
+ }
1343
+ }
1344
+ const specs = comptime useArgSpecs(node, template_node, entries);
1345
+ const body_entries = comptime (argEntries(specs) ++ &[_]ScopeEntry{.{
1346
+ .name = "",
1347
+ .kind = .slot,
1348
+ .slot_nodes = node.children,
1349
+ .slot_entries = entries,
1350
+ .SiteScope = @TypeOf(scope),
1351
+ }});
1352
+ const body_scope = .{
1353
+ .parent = buildArgScope(specs, entries, node, ui, model, scope),
1354
+ .item = scope,
1355
+ };
1356
+ return buildElement(comptime template_node.children[0], body_entries, ui, model, body_scope);
1357
+ }
1358
+
1359
+ fn useArgSpecs(comptime node: markup.MarkupNode, comptime template_node: markup.MarkupNode, comptime site_entries: []const ScopeEntry) []const ArgSpec {
1360
+ comptime {
1361
+ @setEvalBranchQuota(10_000);
1362
+ for (node.attrs) |attribute| {
1363
+ if (std.mem.eql(u8, attribute.name, "template")) continue;
1364
+ if (!markup.templateDeclaresArg(template_node, attribute.name)) {
1365
+ fail(node, markup.use_extra_arg_message);
1366
+ }
1367
+ }
1368
+ var specs: []const ArgSpec = &.{};
1369
+ var args = markup.templateArgs(template_node);
1370
+ while (args.next()) |token| {
1371
+ const arg = markup.parseTemplateArg(token);
1372
+ if (node.attr(arg.name)) |raw| {
1373
+ specs = specs ++ &[_]ArgSpec{argSpec(node, site_entries, arg.name, raw)};
1374
+ continue;
1375
+ }
1376
+ const default = arg.default orelse fail(node, markup.use_missing_arg_message);
1377
+ // Defaults are literals only — a default cannot see
1378
+ // any scope (interpreter and validator parity).
1379
+ if (std.mem.indexOfScalar(u8, default, '{') != null) {
1380
+ fail(template_node, markup.template_default_literal_message);
1381
+ }
1382
+ // Quotes are not string delimiters in a default; they
1383
+ // would render verbatim (interpreter and validator
1384
+ // parity).
1385
+ if (default.len > 0 and (default[0] == '\'' or default[0] == '"')) {
1386
+ fail(template_node, markup.template_default_quoted_message);
1387
+ }
1388
+ specs = specs ++ &[_]ArgSpec{.{
1389
+ .name = arg.name,
1390
+ .raw = default,
1391
+ .kind = .value,
1392
+ .variant = literalVariant(default),
1393
+ .defaulted = true,
1394
+ }};
1395
+ }
1396
+ return specs;
1397
+ }
1398
+ }
1399
+
1400
+ fn literalVariant(comptime text: []const u8) ValueVariant {
1401
+ return switch (interpreter.literalValue(text)) {
1402
+ .string => .string,
1403
+ .integer => .integer,
1404
+ .float => .float,
1405
+ .boolean => .boolean,
1406
+ };
1407
+ }
1408
+
1409
+ /// Comptime mirror of the interpreter's `argPayload` resolution
1410
+ /// order: scope entries shadow model iterables; a binding naming
1411
+ /// an iterable becomes a slice arg, anything else a value arg.
1412
+ fn argSpec(comptime node: markup.MarkupNode, comptime site_entries: []const ScopeEntry, comptime arg_name: []const u8, comptime raw: []const u8) ArgSpec {
1413
+ comptime {
1414
+ const expression = markup.parseAttrExpression(raw) orelse fail(node, invalid_expression_message);
1415
+ if (expression == .binding) {
1416
+ const path = expression.binding;
1417
+ const head = interpreter.pathHead(path);
1418
+ if (scopeIndex(site_entries, head)) |index| {
1419
+ if (site_entries[index].kind == .slice_arg and interpreter.pathTail(path) == null) {
1420
+ return .{ .name = arg_name, .raw = raw, .kind = .slice, .Item = site_entries[index].Item, .site_index = index };
1421
+ }
1422
+ } else if (eachInfo(path)) |info| {
1423
+ // Strings stay scalars (interpreter parity): a
1424
+ // binding producing []const u8 binds as a value
1425
+ // arg, never as an iterable of bytes.
1426
+ if (info.Item != u8) {
1427
+ return .{ .name = arg_name, .raw = raw, .kind = .slice, .Item = info.Item, .each = info };
1428
+ }
1429
+ }
1430
+ }
1431
+ return .{ .name = arg_name, .raw = raw, .kind = .value, .variant = exprVariant(node, site_entries, raw) };
1432
+ }
1433
+ }
1434
+
1435
+ fn argEntries(comptime specs: []const ArgSpec) []const ScopeEntry {
1436
+ comptime {
1437
+ var entries: []const ScopeEntry = &.{};
1438
+ for (specs) |spec| {
1439
+ entries = entries ++ &[_]ScopeEntry{switch (spec.kind) {
1440
+ .value => .{ .name = spec.name, .kind = .value_arg, .variant = spec.variant },
1441
+ .slice => .{ .name = spec.name, .kind = .slice_arg, .Item = spec.Item },
1442
+ }};
1443
+ }
1444
+ return entries;
1445
+ }
1446
+ }
1447
+
1448
+ fn ArgPayload(comptime spec: ArgSpec) type {
1449
+ return switch (spec.kind) {
1450
+ .value => Value,
1451
+ .slice => []const spec.Item,
1452
+ };
1453
+ }
1454
+
1455
+ /// The scope chain type for a template body: one link per arg,
1456
+ /// nothing from the use site — a template sees the model and its
1457
+ /// args, never the loop variables where it is used.
1458
+ fn ArgScope(comptime specs: []const ArgSpec) type {
1459
+ if (specs.len == 0) return struct {};
1460
+ return struct {
1461
+ parent: ArgScope(specs[0 .. specs.len - 1]),
1462
+ item: ArgPayload(specs[specs.len - 1]),
1463
+ };
1464
+ }
1465
+
1466
+ fn buildArgScope(comptime specs: []const ArgSpec, comptime site_entries: []const ScopeEntry, comptime node: markup.MarkupNode, ui: *Ui, model: *const ModelT, site_scope: anytype) ArgScope(specs) {
1467
+ if (comptime (specs.len == 0)) return .{};
1468
+ return .{
1469
+ .parent = buildArgScope(comptime specs[0 .. specs.len - 1], site_entries, node, ui, model, site_scope),
1470
+ .item = argPayloadValue(comptime specs[specs.len - 1], site_entries, node, ui, model, site_scope),
1471
+ };
1472
+ }
1473
+
1474
+ fn argPayloadValue(comptime spec: ArgSpec, comptime site_entries: []const ScopeEntry, comptime node: markup.MarkupNode, ui: *Ui, model: *const ModelT, site_scope: anytype) ArgPayload(spec) {
1475
+ if (comptime (spec.kind == .slice)) {
1476
+ if (comptime (spec.site_index != null)) {
1477
+ return scopePayload(site_entries, comptime spec.site_index.?, site_scope);
1478
+ }
1479
+ return eachItems(comptime spec.each.?, ui, model);
1480
+ }
1481
+ if (comptime spec.defaulted) {
1482
+ // The declaration's literal default, exactly as the
1483
+ // interpreter evaluates it.
1484
+ return comptime interpreter.literalValue(spec.raw);
1485
+ }
1486
+ return evalExpr(node, site_entries, spec.raw, ui, model, site_scope);
1487
+ }
1488
+
1489
+ // -------------------------------------------------- `for` sources
1490
+
1491
+ const EachKind = enum { field, decl_slice, decl_fn, decl_fn_arena };
1492
+ const EachInfo = struct { Item: type, kind: EachKind, name: []const u8 };
1493
+
1494
+ /// Comptime mirror of the interpreter's `iterateItems` resolution:
1495
+ /// a Model slice/array field, a public array/slice declaration, or
1496
+ /// a public function returning a slice (optionally taking an
1497
+ /// arena).
1498
+ fn eachInfo(comptime each: []const u8) ?EachInfo {
1499
+ comptime {
1500
+ @setEvalBranchQuota(10_000);
1501
+ for (@typeInfo(ModelT).@"struct".fields) |field| {
1502
+ if (!std.mem.eql(u8, field.name, each)) continue;
1503
+ if (interpreter.sliceElement(field.type)) |Element| {
1504
+ return .{ .Item = Element, .kind = .field, .name = field.name };
1505
+ }
1506
+ }
1507
+ for (@typeInfo(ModelT).@"struct".decls) |decl| {
1508
+ if (!std.mem.eql(u8, decl.name, each)) continue;
1509
+ const DeclType = @TypeOf(@field(ModelT, decl.name));
1510
+ if (interpreter.sliceElement(DeclType)) |Element| {
1511
+ return .{ .Item = Element, .kind = .decl_slice, .name = decl.name };
1512
+ }
1513
+ switch (@typeInfo(DeclType)) {
1514
+ .@"fn" => |fn_info| {
1515
+ const Return = fn_info.return_type orelse continue;
1516
+ const Element = interpreter.sliceElement(Return) orelse continue;
1517
+ if (interpreter.isItemFn(DeclType, Element, false)) {
1518
+ return .{ .Item = Element, .kind = .decl_fn, .name = decl.name };
1519
+ }
1520
+ if (interpreter.isItemFn(DeclType, Element, true)) {
1521
+ return .{ .Item = Element, .kind = .decl_fn_arena, .name = decl.name };
1522
+ }
1523
+ },
1524
+ else => {},
1525
+ }
1526
+ }
1527
+ return null;
1528
+ }
1529
+ }
1530
+
1531
+ fn eachItems(comptime info: EachInfo, ui: *Ui, model: *const ModelT) []const info.Item {
1532
+ return switch (comptime info.kind) {
1533
+ .field => interpreter.asSlice(info.Item, &@field(model, info.name)),
1534
+ .decl_slice => interpreter.asSlice(info.Item, &@field(ModelT, info.name)),
1535
+ .decl_fn => @field(ModelT, info.name)(model),
1536
+ .decl_fn_arena => @field(ModelT, info.name)(model, ui.arena),
1537
+ };
1538
+ }
1539
+
1540
+ fn itemKey(comptime Item: type, comptime node: markup.MarkupNode, comptime field_path: []const u8, ui: *Ui, item: *const Item) canvas.UiKey {
1541
+ // Keys stay identity-stable data: fields and zero-arg
1542
+ // methods only, never arena-computed values (the interpreter
1543
+ // resolves keys with a null arena for the same reason).
1544
+ const Leaf = comptime (OnType(Item, field_path, false) orelse fail(node, "key does not name a field on the item"));
1545
+ comptime requireVariant(bindingVariant(Leaf), &.{ .integer, .string }, node, "key fields must be integers or strings");
1546
+ const value = interpreter.valueOf(Leaf, valueOn(Item, field_path, item, ui.arena)) orelse unreachable;
1547
+ return uiKeyFromValue(value, ui);
1548
+ }
1549
+
1550
+ // ---------------------------------------------------- attributes
1551
+
1552
+ fn applyAttrs(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype, options: *Ui.ElementOptions) void {
1553
+ inline for (0..node.attrs.len) |attr_index| {
1554
+ const attribute = comptime node.attrs[attr_index];
1555
+ if (comptime std.mem.eql(u8, attribute.name, "kind")) {
1556
+ // Engine hint for tooling; the interpreter skips it too.
1557
+ } else if (comptime std.mem.startsWith(u8, attribute.name, "on-")) {
1558
+ applyMessageAttr(node, attribute, entries, ui, model, scope, options);
1559
+ } else if (comptime std.mem.eql(u8, attribute.name, "key")) {
1560
+ options.key = attrKey(node, entries, attribute.value, ui, model, scope, "keys must be integers or strings");
1561
+ } else if (comptime std.mem.eql(u8, attribute.name, "global-key")) {
1562
+ options.global_key = attrKey(node, entries, attribute.value, ui, model, scope, "keys must be integers or strings");
1563
+ } else if (comptime std.mem.eql(u8, attribute.name, "role")) {
1564
+ options.semantics.role = roleValue(node, entries, attribute.value, ui, model, scope);
1565
+ } else if (comptime std.mem.eql(u8, attribute.name, "label")) {
1566
+ comptime requireVariant(exprVariant(node, entries, attribute.value), &.{.string}, node, "label expects text");
1567
+ options.semantics.label = switch (evalExpr(node, entries, attribute.value, ui, model, scope)) {
1568
+ .string => |text| text,
1569
+ else => runtimeFail([]const u8, ui),
1570
+ };
1571
+ } else if (comptime std.mem.eql(u8, attribute.name, "image")) {
1572
+ applyImageAttr(node, attribute.value, entries, ui, model, scope, options);
1573
+ } else if (comptime std.mem.eql(u8, attribute.name, "name")) {
1574
+ // Consumed by the icon branch in buildElement; a
1575
+ // compile error on any other element (interpreter and
1576
+ // validator parity).
1577
+ comptime if (!std.mem.eql(u8, node.name, "icon")) fail(node, markup.icon_name_element_message);
1578
+ } else if (comptime std.mem.eql(u8, attribute.name, "icon")) {
1579
+ // Inline icon scoped to the labeled interactive
1580
+ // elements (button, toggle-button, list-item,
1581
+ // menu-item): the same icon value grammar as
1582
+ // <icon name>, checked at comptime so a built-in typo
1583
+ // is a compile error (interpreter and validator
1584
+ // parity); app: names and bound names resolve at draw
1585
+ // time.
1586
+ comptime if (!markup.iconAttrElement(node.name)) fail(node, markup.button_icon_element_message);
1587
+ switch (comptime iconValueChecked(node, attribute.value, markup.button_icon_message)) {
1588
+ .builtin, .app => |name| options.icon = name,
1589
+ .binding => options.icon = stringAttr(node, entries, attribute.value, ui, model, scope, markup.button_icon_message),
1590
+ .invalid => unreachable,
1591
+ }
1592
+ } else if (comptime std.mem.eql(u8, attribute.name, "anchor")) {
1593
+ // Anchored floating placement, dropdown-menu-scoped:
1594
+ // a literal side resolved at comptime (interpreter
1595
+ // and validator parity).
1596
+ options.anchor = comptime blk: {
1597
+ if (!markup.anchorElement(node.name)) fail(node, markup.anchor_element_message);
1598
+ break :blk std.meta.stringToEnum(canvas.WidgetAnchorPlacement, attribute.value) orelse
1599
+ fail(node, markup.anchor_value_message);
1600
+ };
1601
+ } else if (comptime std.mem.eql(u8, attribute.name, "anchor-alignment")) {
1602
+ options.anchor_alignment = comptime blk: {
1603
+ if (!markup.anchorElement(node.name)) fail(node, markup.anchor_element_message);
1604
+ if (node.attr("anchor") == null) fail(node, markup.anchor_dependent_attr_message);
1605
+ break :blk std.meta.stringToEnum(canvas.WidgetAnchorAlignment, attribute.value) orelse
1606
+ fail(node, markup.anchor_alignment_value_message);
1607
+ };
1608
+ } else if (comptime std.mem.eql(u8, attribute.name, "anchor-offset")) {
1609
+ options.anchor_offset = comptime blk: {
1610
+ if (!markup.anchorElement(node.name)) fail(node, markup.anchor_element_message);
1611
+ if (node.attr("anchor") == null) fail(node, markup.anchor_dependent_attr_message);
1612
+ break :blk std.fmt.parseFloat(f32, attribute.value) catch
1613
+ fail(node, markup.anchor_offset_value_message);
1614
+ };
1615
+ } else if (comptime (colorStyleField(attribute.name) != null)) {
1616
+ // Style token refs resolve entirely at comptime: a typo
1617
+ // in a token name is a compile error.
1618
+ @field(options.style_tokens, colorStyleField(attribute.name).?) =
1619
+ comptime colorTokenRef(node, attribute.value);
1620
+ } else if (comptime std.mem.eql(u8, attribute.name, "radius")) {
1621
+ options.style_tokens.radius = comptime radiusTokenRef(node, attribute.value);
1622
+ } else {
1623
+ setOption(node, comptime optionFieldName(node, attribute.name), attribute.value, entries, ui, model, scope, options);
1624
+ }
1625
+ }
1626
+ }
1627
+
1628
+ /// Comptime mirror of the interpreter's `applyImageAttr`:
1629
+ /// `image="{binding}"` on avatar resolves to a `u64` ImageId the
1630
+ /// app registered at runtime — avatar-only, binding-only, and the
1631
+ /// binding must produce an integer, all checked at comptime with
1632
+ /// the interpreter's messages.
1633
+ fn applyImageAttr(comptime node: markup.MarkupNode, comptime raw: []const u8, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype, options: *Ui.ElementOptions) void {
1634
+ comptime {
1635
+ if (!std.mem.eql(u8, node.name, "avatar")) fail(node, markup.avatar_image_element_message);
1636
+ const expression = markup.parseAttrExpression(raw) orelse fail(node, markup.avatar_image_message);
1637
+ if (expression != .binding) fail(node, markup.avatar_image_message);
1638
+ }
1639
+ const path = comptime markup.parseAttrExpression(raw).?.binding;
1640
+ comptime requireVariant(pathVariant(node, entries, path, true), &.{.integer}, node, markup.avatar_image_message);
1641
+ options.image = switch (bindingValue(node, entries, path, ui, model, scope, true)) {
1642
+ .integer => |int| @intCast(int),
1643
+ else => runtimeFail(canvas.ImageId, ui),
1644
+ };
1645
+ }
1646
+
1647
+ fn colorStyleField(comptime attr_name: []const u8) ?[]const u8 {
1648
+ comptime {
1649
+ @setEvalBranchQuota(10_000);
1650
+ for (interpreter.color_style_attr_fields) |entry| {
1651
+ if (std.mem.eql(u8, attr_name, entry.markup)) return entry.zig;
1652
+ }
1653
+ return null;
1654
+ }
1655
+ }
1656
+
1657
+ fn styleTokenLiteral(comptime node: markup.MarkupNode, comptime raw: []const u8) []const u8 {
1658
+ comptime {
1659
+ const expression = markup.parseAttrExpression(raw) orelse fail(node, markup.style_token_literal_message);
1660
+ if (expression != .literal) fail(node, markup.style_token_literal_message);
1661
+ return expression.literal;
1662
+ }
1663
+ }
1664
+
1665
+ fn colorTokenRef(comptime node: markup.MarkupNode, comptime raw: []const u8) canvas.ColorTokenName {
1666
+ comptime {
1667
+ return std.meta.stringToEnum(canvas.ColorTokenName, styleTokenLiteral(node, raw)) orelse
1668
+ fail(node, markup.unknown_color_token_message);
1669
+ }
1670
+ }
1671
+
1672
+ fn radiusTokenRef(comptime node: markup.MarkupNode, comptime raw: []const u8) canvas.RadiusTokenName {
1673
+ comptime {
1674
+ return std.meta.stringToEnum(canvas.RadiusTokenName, styleTokenLiteral(node, raw)) orelse
1675
+ fail(node, markup.unknown_radius_token_message);
1676
+ }
1677
+ }
1678
+
1679
+ fn optionFieldName(comptime node: markup.MarkupNode, comptime attr_name: []const u8) []const u8 {
1680
+ comptime {
1681
+ @setEvalBranchQuota(10_000);
1682
+ for (interpreter.attr_names) |name| {
1683
+ if (std.mem.eql(u8, attr_name, name.markup)) return name.zig;
1684
+ }
1685
+ fail(node, "unknown attribute for this element");
1686
+ }
1687
+ }
1688
+
1689
+ fn setOption(comptime node: markup.MarkupNode, comptime zig_field: []const u8, comptime raw: []const u8, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype, options: *Ui.ElementOptions) void {
1690
+ const FieldType = @FieldType(Ui.ElementOptions, zig_field);
1691
+ const variant = comptime exprVariant(node, entries, raw);
1692
+ switch (comptime @typeInfo(FieldType)) {
1693
+ .float => {
1694
+ comptime requireVariant(variant, &.{ .float, .integer }, node, "expected a number");
1695
+ @field(options, zig_field) = switch (evalExpr(node, entries, raw, ui, model, scope)) {
1696
+ .float => |float| float,
1697
+ .integer => |int| @floatFromInt(int),
1698
+ else => runtimeFail(FieldType, ui),
1699
+ };
1700
+ },
1701
+ .bool => @field(options, zig_field) = evalExpr(node, entries, raw, ui, model, scope).truthy(),
1702
+ // Optional bools (`expanded`): the attribute's PRESENCE
1703
+ // makes the state non-null; the value sets it.
1704
+ .optional => @field(options, zig_field) = evalExpr(node, entries, raw, ui, model, scope).truthy(),
1705
+ .int => {
1706
+ comptime requireVariant(variant, &.{.integer}, node, "expected a whole number");
1707
+ @field(options, zig_field) = switch (evalExpr(node, entries, raw, ui, model, scope)) {
1708
+ .integer => |int| if (int < 0) runtimeFail(FieldType, ui) else @intCast(int),
1709
+ else => runtimeFail(FieldType, ui),
1710
+ };
1711
+ },
1712
+ .@"enum" => {
1713
+ comptime requireVariant(variant, &.{.string}, node, "expected an option name");
1714
+ const expression = comptime markup.parseAttrExpression(raw).?;
1715
+ if (comptime (expression == .literal)) {
1716
+ // Literal option names resolve at comptime: a typo
1717
+ // is a compile error, not a failed rebuild.
1718
+ @field(options, zig_field) = comptime (std.meta.stringToEnum(FieldType, expression.literal) orelse fail(node, "unknown option value"));
1719
+ } else {
1720
+ const text = switch (evalExpr(node, entries, raw, ui, model, scope)) {
1721
+ .string => |text| text,
1722
+ else => runtimeFail([]const u8, ui),
1723
+ };
1724
+ @field(options, zig_field) = std.meta.stringToEnum(FieldType, text) orelse runtimeFail(FieldType, ui);
1725
+ }
1726
+ },
1727
+ .pointer => {
1728
+ comptime requireVariant(variant, &.{.string}, node, "expected text");
1729
+ @field(options, zig_field) = switch (evalExpr(node, entries, raw, ui, model, scope)) {
1730
+ .string => |text| text,
1731
+ else => runtimeFail(FieldType, ui),
1732
+ };
1733
+ },
1734
+ else => comptime fail(node, "attribute is not settable from markup"),
1735
+ }
1736
+ }
1737
+
1738
+ fn attrKey(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, comptime raw: []const u8, ui: *Ui, model: *const ModelT, scope: anytype, comptime message: []const u8) canvas.UiKey {
1739
+ comptime requireVariant(exprVariant(node, entries, raw), &.{ .integer, .string }, node, message);
1740
+ return uiKeyFromValue(evalExpr(node, entries, raw, ui, model, scope), ui);
1741
+ }
1742
+
1743
+ fn uiKeyFromValue(value: Value, ui: *Ui) canvas.UiKey {
1744
+ return switch (value) {
1745
+ .integer => |int| canvas.uiKey(@as(u64, @intCast(int))),
1746
+ .string => |text| canvas.uiKey(text),
1747
+ else => blk: {
1748
+ ui.failed = true;
1749
+ break :blk canvas.uiKey(@as(u64, 0));
1750
+ },
1751
+ };
1752
+ }
1753
+
1754
+ fn roleValue(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, comptime raw: []const u8, ui: *Ui, model: *const ModelT, scope: anytype) canvas.WidgetRole {
1755
+ comptime requireVariant(exprVariant(node, entries, raw), &.{.string}, node, "role expects a role name");
1756
+ const expression = comptime (markup.parseAttrExpression(raw) orelse fail(node, invalid_expression_message));
1757
+ if (comptime (expression == .literal)) {
1758
+ return comptime (std.meta.stringToEnum(canvas.WidgetRole, expression.literal) orelse fail(node, "unknown role"));
1759
+ }
1760
+ const text = switch (evalExpr(node, entries, raw, ui, model, scope)) {
1761
+ .string => |text| text,
1762
+ else => runtimeFail([]const u8, ui),
1763
+ };
1764
+ return std.meta.stringToEnum(canvas.WidgetRole, text) orelse runtimeFail(canvas.WidgetRole, ui);
1765
+ }
1766
+
1767
+ // ------------------------------------------------------ messages
1768
+
1769
+ fn applyMessageAttr(comptime node: markup.MarkupNode, comptime attribute: markup.MarkupAttr, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype, options: *Ui.ElementOptions) void {
1770
+ const expression = comptime (markup.parseMessageExpression(attribute.value) orelse fail(node, "invalid message expression: on-* takes a Msg tag (\"add\") or tag with one binding payload (\"toggle:{item.id}\")"));
1771
+ const event = comptime attribute.name[3..];
1772
+ if (comptime std.mem.eql(u8, event, "input")) {
1773
+ options.on_input = comptime (inputConstructor(expression.tag) orelse fail(node, "on-input tag must carry a TextInputEvent payload"));
1774
+ return;
1775
+ }
1776
+ if (comptime std.mem.eql(u8, event, "scroll")) {
1777
+ comptime {
1778
+ if (!std.mem.eql(u8, node.name, "scroll")) fail(node, markup.on_scroll_element_message);
1779
+ }
1780
+ options.on_scroll = comptime (scrollConstructor(expression.tag) orelse fail(node, markup.on_scroll_payload_message));
1781
+ return;
1782
+ }
1783
+ if (comptime std.mem.eql(u8, event, "resize")) {
1784
+ comptime {
1785
+ if (!std.mem.eql(u8, node.name, "split")) fail(node, markup.on_resize_element_message);
1786
+ }
1787
+ options.on_resize = comptime (resizeConstructor(expression.tag) orelse fail(node, markup.on_resize_payload_message));
1788
+ return;
1789
+ }
1790
+ const msg = constructMessage(node, expression, entries, ui, model, scope);
1791
+ if (comptime std.mem.eql(u8, event, "press")) {
1792
+ options.on_press = msg;
1793
+ } else if (comptime std.mem.eql(u8, event, "toggle")) {
1794
+ options.on_toggle = msg;
1795
+ } else if (comptime std.mem.eql(u8, event, "change")) {
1796
+ options.on_change = msg;
1797
+ } else if (comptime std.mem.eql(u8, event, "submit")) {
1798
+ options.on_submit = msg;
1799
+ } else if (comptime std.mem.eql(u8, event, "dismiss")) {
1800
+ // Only dismissible surfaces are ever dismissed by the
1801
+ // runtime (interpreter and validator parity).
1802
+ comptime {
1803
+ if (!markup.dismissEventElement(node.name)) fail(node, markup.on_dismiss_element_message);
1804
+ }
1805
+ options.on_dismiss = msg;
1806
+ } else if (comptime std.mem.eql(u8, event, "hold")) {
1807
+ // Press family: like on-press, a bound hold makes any
1808
+ // element pressable.
1809
+ options.on_hold = msg;
1810
+ } else if (comptime std.mem.eql(u8, event, "reach-end")) {
1811
+ // The approach-end signal (infinite-scroll fetch) is
1812
+ // emitted for scroll containers only (interpreter and
1813
+ // validator parity).
1814
+ comptime {
1815
+ if (!std.mem.eql(u8, node.name, "scroll")) fail(node, markup.on_reach_end_element_message);
1816
+ }
1817
+ options.on_reach_end = msg;
1818
+ } else {
1819
+ comptime fail(node, "unknown event attribute");
1820
+ }
1821
+ }
1822
+
1823
+ fn inputConstructor(comptime tag: []const u8) ?Ui.InputMsgFn {
1824
+ comptime {
1825
+ @setEvalBranchQuota(10_000);
1826
+ for (@typeInfo(MsgT).@"union".fields) |field| {
1827
+ if (field.type == canvas.TextInputEvent and std.mem.eql(u8, field.name, tag)) {
1828
+ return Ui.inputMsg(@field(std.meta.Tag(MsgT), field.name));
1829
+ }
1830
+ }
1831
+ return null;
1832
+ }
1833
+ }
1834
+
1835
+ fn scrollConstructor(comptime tag: []const u8) ?Ui.ScrollMsgFn {
1836
+ comptime {
1837
+ @setEvalBranchQuota(10_000);
1838
+ for (@typeInfo(MsgT).@"union".fields) |field| {
1839
+ if (field.type == canvas.ScrollState and std.mem.eql(u8, field.name, tag)) {
1840
+ return Ui.scrollMsg(@field(std.meta.Tag(MsgT), field.name));
1841
+ }
1842
+ }
1843
+ return null;
1844
+ }
1845
+ }
1846
+
1847
+ fn resizeConstructor(comptime tag: []const u8) ?Ui.ValueMsgFn {
1848
+ comptime {
1849
+ @setEvalBranchQuota(10_000);
1850
+ for (@typeInfo(MsgT).@"union".fields) |field| {
1851
+ if (field.type == f32 and std.mem.eql(u8, field.name, tag)) {
1852
+ return Ui.valueMsg(@field(std.meta.Tag(MsgT), field.name));
1853
+ }
1854
+ }
1855
+ return null;
1856
+ }
1857
+ }
1858
+
1859
+ fn msgTagIndex(comptime tag: []const u8) ?usize {
1860
+ comptime {
1861
+ @setEvalBranchQuota(10_000);
1862
+ for (@typeInfo(MsgT).@"union".fields, 0..) |field, index| {
1863
+ if (std.mem.eql(u8, field.name, tag)) return index;
1864
+ }
1865
+ return null;
1866
+ }
1867
+ }
1868
+
1869
+ fn constructMessage(comptime node: markup.MarkupNode, comptime expression: markup.MessageExpression, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype) MsgT {
1870
+ const tag_index = comptime (msgTagIndex(expression.tag) orelse fail(node, "unknown message tag"));
1871
+ const field = comptime @typeInfo(MsgT).@"union".fields[tag_index];
1872
+ if (comptime (field.type == void)) {
1873
+ comptime {
1874
+ if (expression.payload.len > 0) fail(node, "message does not take a payload");
1875
+ }
1876
+ return @unionInit(MsgT, field.name, {});
1877
+ }
1878
+ comptime {
1879
+ if (expression.payload.len == 0) fail(node, "message requires a payload");
1880
+ }
1881
+ const variant = comptime pathVariant(node, entries, expression.payload, true);
1882
+ const value = bindingValue(node, entries, expression.payload, ui, model, scope, true);
1883
+ return @unionInit(MsgT, field.name, coerce(field.type, node, variant, ui, value));
1884
+ }
1885
+
1886
+ /// Runtime mirror of the interpreter's `coerce`, with the
1887
+ /// type-determined mismatches promoted to compile errors. Only
1888
+ /// value-dependent conversions (optional bindings, enum tags from
1889
+ /// arbitrary strings) can still fail, latching `ui.failed`.
1890
+ fn coerce(comptime T: type, comptime node: markup.MarkupNode, comptime variant: ?ValueVariant, ui: *Ui, value: Value) T {
1891
+ switch (comptime @typeInfo(T)) {
1892
+ .int => {
1893
+ comptime requireVariant(variant, &.{.integer}, node, "payload type does not match the message");
1894
+ return switch (value) {
1895
+ .integer => |int| @intCast(int),
1896
+ else => runtimeFail(T, ui),
1897
+ };
1898
+ },
1899
+ .float => {
1900
+ comptime requireVariant(variant, &.{ .float, .integer }, node, "payload type does not match the message");
1901
+ return switch (value) {
1902
+ .float => |float| @floatCast(float),
1903
+ .integer => |int| @floatFromInt(int),
1904
+ else => runtimeFail(T, ui),
1905
+ };
1906
+ },
1907
+ .@"enum" => {
1908
+ comptime requireVariant(variant, &.{.string}, node, "payload type does not match the message");
1909
+ return switch (value) {
1910
+ .string => |text| std.meta.stringToEnum(T, text) orelse runtimeFail(T, ui),
1911
+ else => runtimeFail(T, ui),
1912
+ };
1913
+ },
1914
+ .pointer => {
1915
+ comptime requireVariant(variant, &.{.string}, node, "payload type does not match the message");
1916
+ return switch (value) {
1917
+ .string => |text| text,
1918
+ else => runtimeFail(T, ui),
1919
+ };
1920
+ },
1921
+ .bool => return value.truthy(),
1922
+ else => comptime fail(node, "payload type does not match the message"),
1923
+ }
1924
+ }
1925
+
1926
+ // --------------------------------------------------- expressions
1927
+
1928
+ const invalid_expression_message = markup.invalid_expression_message;
1929
+
1930
+ fn evalExpr(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, comptime raw: []const u8, ui: *Ui, model: *const ModelT, scope: anytype) Value {
1931
+ const expression = comptime (markup.parseAttrExpression(raw) orelse fail(node, invalid_expression_message));
1932
+ if (comptime (expression == .literal)) {
1933
+ return comptime interpreter.literalValue(expression.literal);
1934
+ }
1935
+ if (comptime (expression == .binding)) {
1936
+ return bindingValue(node, entries, comptime expression.binding, ui, model, scope, true);
1937
+ }
1938
+ if (comptime (expression == .expression)) {
1939
+ return evalExpressionTree(node, entries, comptime expression.expression, ui, model, scope);
1940
+ }
1941
+ // Arena-computed bindings are excluded from equality on
1942
+ // purpose (same rule as the interpreter): comparing freshly
1943
+ // formatted strings is a smell — compare source fields, or
1944
+ // bind a bool-returning fn.
1945
+ const sides = comptime expression.equals;
1946
+ return .{ .boolean = Value.eql(
1947
+ bindingValue(node, entries, sides.left, ui, model, scope, false),
1948
+ bindingValue(node, entries, sides.right, ui, model, scope, false),
1949
+ ) };
1950
+ }
1951
+
1952
+ /// Evaluate a full `{expression}`: the tree parses at comptime
1953
+ /// (syntax, bounds, function names/arity are compile errors with
1954
+ /// the evaluator's teaching messages), binding nodes resolve
1955
+ /// through the same comptime-unrolled path access as bare
1956
+ /// bindings, and the SHARED evaluator computes the result at
1957
+ /// runtime — identical Value inputs through identical arithmetic
1958
+ /// and formatting code as the interpreter, so results match bit
1959
+ /// for bit. Value-dependent failures (division by zero, overflow)
1960
+ /// latch `ui.failed` exactly like the engine's other runtime
1961
+ /// conversions.
1962
+ fn evalExpressionTree(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, comptime inner: []const u8, ui: *Ui, model: *const ModelT, scope: anytype) Value {
1963
+ const tree = comptime parsedExpression(node, inner);
1964
+ // Static type discipline: definite mismatches are compile
1965
+ // errors carrying the same message the interpreter reports.
1966
+ _ = comptime expressionTreeVariant(node, entries, inner);
1967
+ var values: [markup.expr.max_expression_nodes]Value = undefined;
1968
+ inline for (0..markup.expr.max_expression_nodes) |index| {
1969
+ if (comptime (index < tree.len and tree.nodes[index].kind == .binding)) {
1970
+ // Comparison operands reject arena-computed scalars,
1971
+ // the same teaching rule as `{a == b}`.
1972
+ values[index] = bindingValue(node, entries, comptime tree.nodes[index].text, ui, model, scope, comptime !tree.nodes[index].comparison_operand);
1973
+ }
1974
+ }
1975
+ const outcome = markup.expr.eval(&tree, &values, ui.arena) catch {
1976
+ ui.failed = true;
1977
+ return .{ .boolean = false };
1978
+ };
1979
+ return switch (outcome) {
1980
+ .value => |value| value,
1981
+ .fail => blk: {
1982
+ ui.failed = true;
1983
+ break :blk .{ .boolean = false };
1984
+ },
1985
+ };
1986
+ }
1987
+
1988
+ fn parsedExpression(comptime node: markup.MarkupNode, comptime inner: []const u8) markup.expr.ExprTree {
1989
+ comptime {
1990
+ @setEvalBranchQuota(4_000 + inner.len * 400);
1991
+ var tree: markup.expr.ExprTree = .{};
1992
+ var diagnostic: markup.expr.Diagnostic = .{};
1993
+ if (!markup.expr.parse(inner, &tree, &diagnostic)) fail(node, diagnostic.message);
1994
+ const frozen = tree;
1995
+ return frozen;
1996
+ }
1997
+ }
1998
+
1999
+ /// The comptime-known result kind of a full expression: binding
2000
+ /// kinds come from the same leaf-type resolution as bare bindings
2001
+ /// (so an unresolvable path is a compile error with the
2002
+ /// interpreter's message), and the shared type checker promotes
2003
+ /// every definite mismatch to a compile error.
2004
+ fn expressionTreeVariant(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, comptime inner: []const u8) ?ValueVariant {
2005
+ comptime {
2006
+ @setEvalBranchQuota(4_000 + inner.len * 400);
2007
+ const tree = parsedExpression(node, inner);
2008
+ var kinds: [markup.expr.max_expression_nodes]?markup.expr.ValueKind = @splat(null);
2009
+ for (tree.nodes[0..tree.len], 0..) |expr_node, index| {
2010
+ if (expr_node.kind != .binding) continue;
2011
+ kinds[index] = kindFromVariant(pathVariant(node, entries, expr_node.text, !expr_node.comparison_operand));
2012
+ }
2013
+ var diagnostic: markup.expr.Diagnostic = .{};
2014
+ const result = markup.expr.checkTypes(&tree, &kinds, &diagnostic) catch fail(node, diagnostic.message);
2015
+ return variantFromKind(result);
2016
+ }
2017
+ }
2018
+
2019
+ fn kindFromVariant(variant: ?ValueVariant) ?markup.expr.ValueKind {
2020
+ return switch (variant orelse return null) {
2021
+ .string => .string,
2022
+ .integer => .integer,
2023
+ .float => .float,
2024
+ .boolean => .boolean,
2025
+ };
2026
+ }
2027
+
2028
+ fn variantFromKind(kind: ?markup.expr.ValueKind) ?ValueVariant {
2029
+ return switch (kind orelse return null) {
2030
+ .string => .string,
2031
+ .integer => .integer,
2032
+ .float => .float,
2033
+ .boolean => .boolean,
2034
+ };
2035
+ }
2036
+
2037
+ /// The comptime-known `Value` variant an expression produces, or
2038
+ /// null when it is only runtime-known (a binding through an
2039
+ /// optional). Used to promote type mismatches to compile errors.
2040
+ fn exprVariant(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, comptime raw: []const u8) ?ValueVariant {
2041
+ comptime {
2042
+ const expression = markup.parseAttrExpression(raw) orelse fail(node, invalid_expression_message);
2043
+ return switch (expression) {
2044
+ .literal => |text| @as(ValueVariant, switch (interpreter.literalValue(text)) {
2045
+ .string => .string,
2046
+ .integer => .integer,
2047
+ .float => .float,
2048
+ .boolean => .boolean,
2049
+ }),
2050
+ .binding => |path| pathVariant(node, entries, path, true),
2051
+ .equals => .boolean,
2052
+ .expression => |inner| expressionTreeVariant(node, entries, inner),
2053
+ };
2054
+ }
2055
+ }
2056
+
2057
+ /// Innermost scope entry whose name matches `head`.
2058
+ fn scopeIndex(comptime entries: []const ScopeEntry, comptime head: []const u8) ?usize {
2059
+ comptime {
2060
+ @setEvalBranchQuota(10_000);
2061
+ var index = entries.len;
2062
+ while (index > 0) {
2063
+ index -= 1;
2064
+ if (std.mem.eql(u8, entries[index].name, head)) return index;
2065
+ }
2066
+ return null;
2067
+ }
2068
+ }
2069
+
2070
+ fn scopePayload(comptime entries: []const ScopeEntry, comptime index: usize, scope: anytype) EntryPayload(entries[index]) {
2071
+ if (comptime (index == entries.len - 1)) return scope.item;
2072
+ return scopePayload(entries[0 .. entries.len - 1], index, scope.parent);
2073
+ }
2074
+
2075
+ /// Comptime mirror of the interpreter's `evalBinding` resolution
2076
+ /// for loop items and model paths: the Zig type a binding path
2077
+ /// resolves to, or a compile error with the interpreter's message.
2078
+ /// Value args have no leaf type; `pathVariant` handles them.
2079
+ /// `allow_arena` gates arena-taking scalar fns (allowed everywhere
2080
+ /// except inside `{a == b}` equality, with a teaching error).
2081
+ fn BindingLeaf(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, comptime path: []const u8, comptime allow_arena: bool) type {
2082
+ comptime {
2083
+ const head = interpreter.pathHead(path);
2084
+ if (scopeIndex(entries, head)) |index| {
2085
+ const entry = entries[index];
2086
+ if (entry.kind == .slice_arg) fail(node, "slice-valued template args are only usable with for each");
2087
+ if (entry.kind == .value_arg) fail(node, "template arg values have no fields");
2088
+ const Item = entry.Item;
2089
+ if (interpreter.pathTail(path)) |tail| {
2090
+ return OnType(Item, tail, allow_arena) orelse {
2091
+ if (!allow_arena and OnType(Item, tail, true) != null) {
2092
+ fail(node, markup.arena_scalar_equality_message);
2093
+ }
2094
+ fail(node, "binding does not name a field on the loop item");
2095
+ };
2096
+ }
2097
+ if (!supportedValue(Item)) fail(node, "loop items of this type cannot be used as values");
2098
+ return Item;
2099
+ }
2100
+ return OnType(ModelT, path, allow_arena) orelse {
2101
+ if (!allow_arena and OnType(ModelT, path, true) != null) {
2102
+ fail(node, markup.arena_scalar_equality_message);
2103
+ }
2104
+ if (interpreter.fieldIsTextBuffer(ModelT, head)) {
2105
+ fail(node, markup.binding_text_buffer_message);
2106
+ }
2107
+ fail(node, "binding does not name a model field");
2108
+ };
2109
+ }
2110
+ }
2111
+
2112
+ /// The comptime-known Value variant a binding path produces:
2113
+ /// template value args carry their use-site variant, loop items
2114
+ /// and model paths derive it from the resolved leaf type.
2115
+ fn pathVariant(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, comptime path: []const u8, comptime allow_arena: bool) ?ValueVariant {
2116
+ comptime {
2117
+ const head = interpreter.pathHead(path);
2118
+ if (scopeIndex(entries, head)) |index| {
2119
+ if (entries[index].kind == .value_arg) {
2120
+ if (interpreter.pathTail(path) != null) fail(node, "template arg values have no fields");
2121
+ return entries[index].variant;
2122
+ }
2123
+ }
2124
+ return bindingVariant(BindingLeaf(node, entries, path, allow_arena));
2125
+ }
2126
+ }
2127
+
2128
+ fn bindingValue(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, comptime path: []const u8, ui: *Ui, model: *const ModelT, scope: anytype, comptime allow_arena: bool) Value {
2129
+ const head = comptime interpreter.pathHead(path);
2130
+ const index_opt = comptime scopeIndex(entries, head);
2131
+ if (comptime (index_opt != null)) {
2132
+ const index = comptime index_opt.?;
2133
+ const entry = comptime entries[index];
2134
+ if (comptime (entry.kind == .value_arg)) {
2135
+ comptime {
2136
+ if (interpreter.pathTail(path) != null) fail(node, "template arg values have no fields");
2137
+ }
2138
+ return scopePayload(entries, index, scope);
2139
+ }
2140
+ if (comptime (entry.kind == .slice_arg)) {
2141
+ comptime fail(node, "slice-valued template args are only usable with for each");
2142
+ }
2143
+ const Leaf = comptime BindingLeaf(node, entries, path, allow_arena);
2144
+ const item = scopePayload(entries, index, scope);
2145
+ if (comptime (interpreter.pathTail(path) != null)) {
2146
+ const tail = comptime interpreter.pathTail(path).?;
2147
+ return interpreter.valueOf(Leaf, valueOn(entry.Item, tail, item, ui.arena)) orelse unreachable;
2148
+ }
2149
+ return interpreter.valueOf(Leaf, item.*) orelse unreachable;
2150
+ }
2151
+ const Leaf = comptime BindingLeaf(node, entries, path, allow_arena);
2152
+ return interpreter.valueOf(Leaf, valueOn(ModelT, path, model, ui.arena)) orelse unreachable;
2153
+ }
2154
+
2155
+ // ----------------------------------------------- path resolution
2156
+
2157
+ /// Comptime mirror of the interpreter's `resolveOn`: the type a
2158
+ /// dotted path resolves to on T — struct fields, zero-arg methods,
2159
+ /// and (when `allow_arena`) arena-taking scalar methods — or null
2160
+ /// when the path names nothing `valueOf` can represent (which is
2161
+ /// exactly when the interpreter fails).
2162
+ fn OnType(comptime T: type, comptime path: []const u8, comptime allow_arena: bool) ?type {
2163
+ comptime {
2164
+ @setEvalBranchQuota(10_000);
2165
+ if (@typeInfo(T) != .@"struct") return null;
2166
+ const head = interpreter.pathHead(path);
2167
+ const tail_opt = interpreter.pathTail(path);
2168
+ for (@typeInfo(T).@"struct".fields) |field| {
2169
+ if (!std.mem.eql(u8, field.name, head)) continue;
2170
+ if (tail_opt) |tail| {
2171
+ if (@typeInfo(field.type) != .@"struct") return null;
2172
+ return OnType(field.type, tail, allow_arena);
2173
+ }
2174
+ if (!supportedValue(field.type)) return null;
2175
+ return field.type;
2176
+ }
2177
+ for (@typeInfo(T).@"struct".decls) |decl| {
2178
+ const DeclType = @TypeOf(@field(T, decl.name));
2179
+ switch (@typeInfo(DeclType)) {
2180
+ .@"fn" => |fn_info| {
2181
+ if (fn_info.params.len == 1 and fn_info.return_type != null and fn_info.params[0].type == *const T) {
2182
+ if (std.mem.eql(u8, decl.name, head) and tail_opt == null) {
2183
+ if (!supportedValue(fn_info.return_type.?)) return null;
2184
+ return fn_info.return_type.?;
2185
+ }
2186
+ }
2187
+ if (allow_arena and interpreter.isArenaScalarFn(T, DeclType)) {
2188
+ if (std.mem.eql(u8, decl.name, head) and tail_opt == null) {
2189
+ if (!supportedValue(fn_info.return_type.?)) return null;
2190
+ return fn_info.return_type.?;
2191
+ }
2192
+ }
2193
+ },
2194
+ else => {},
2195
+ }
2196
+ }
2197
+ return null;
2198
+ }
2199
+ }
2200
+
2201
+ /// Direct access for a path `OnType` resolved: field chains compile
2202
+ /// to member access, method leaves to a direct call (arena-taking
2203
+ /// leaves receive the build arena).
2204
+ fn valueOn(comptime T: type, comptime path: []const u8, ptr: *const T, arena: std.mem.Allocator) (OnType(T, path, true).?) {
2205
+ const head = comptime interpreter.pathHead(path);
2206
+ if (comptime (interpreter.pathTail(path) != null)) {
2207
+ const tail = comptime interpreter.pathTail(path).?;
2208
+ return valueOn(@FieldType(T, head), tail, &@field(ptr, head), arena);
2209
+ }
2210
+ if (comptime hasField(T, head)) return @field(ptr, head);
2211
+ if (comptime interpreter.isArenaScalarFn(T, @TypeOf(@field(T, head)))) {
2212
+ return @field(T, head)(ptr, arena);
2213
+ }
2214
+ return @field(T, head)(ptr);
2215
+ }
2216
+
2217
+ fn hasField(comptime T: type, comptime name: []const u8) bool {
2218
+ comptime {
2219
+ @setEvalBranchQuota(10_000);
2220
+ for (@typeInfo(T).@"struct".fields) |field| {
2221
+ if (std.mem.eql(u8, field.name, name)) return true;
2222
+ }
2223
+ return false;
2224
+ }
2225
+ }
2226
+
2227
+ // --------------------------------------------------------- text
2228
+
2229
+ const TextSegment = union(enum) {
2230
+ literal: []const u8,
2231
+ binding: []const u8,
2232
+ expression: []const u8,
2233
+ };
2234
+
2235
+ fn textSegments(comptime node: markup.MarkupNode, comptime text: []const u8) []const TextSegment {
2236
+ comptime {
2237
+ @setEvalBranchQuota(comptime_text_quota_base + text.len * comptime_text_quota_per_byte);
2238
+ var segments: []const TextSegment = &.{};
2239
+ var rest = text;
2240
+ while (std.mem.indexOfScalar(u8, rest, '{')) |open| {
2241
+ segments = segments ++ &[_]TextSegment{.{ .literal = rest[0..open] }};
2242
+ const close = std.mem.indexOfScalarPos(u8, rest, open, '}') orelse fail(node, "unterminated interpolation");
2243
+ const inner = std.mem.trim(u8, rest[open + 1 .. close], " ");
2244
+ if (markup.isBindingPath(inner)) {
2245
+ segments = segments ++ &[_]TextSegment{.{ .binding = inner }};
2246
+ } else {
2247
+ segments = segments ++ &[_]TextSegment{.{ .expression = inner }};
2248
+ }
2249
+ rest = rest[close + 1 ..];
2250
+ }
2251
+ segments = segments ++ &[_]TextSegment{.{ .literal = rest }};
2252
+ return segments;
2253
+ }
2254
+ }
2255
+
2256
+ const comptime_text_quota_base = 2_000;
2257
+ const comptime_text_quota_per_byte = 100;
2258
+
2259
+ // ------------------------------------------------ span paragraphs
2260
+
2261
+ /// Comptime mirror of the interpreter's `buildSpanParagraph`: a
2262
+ /// text element with inline `<span>` children lowers through
2263
+ /// `Ui.paragraph` — plain runs (parser-spliced separators
2264
+ /// included) keep the paragraph style, span children carry their
2265
+ /// own channels — so byte rebasing, span-aware wrapping, and
2266
+ /// semantics match a builder span paragraph exactly: the widget
2267
+ /// announces as ONE text run (spans are visual, never semantic
2268
+ /// children). Misuse fails compilation with the interpreter's
2269
+ /// message.
2270
+ fn buildSpanParagraph(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype, options: Ui.ElementOptions) Ui.Node {
2271
+ comptime {
2272
+ // A span paragraph always word-wraps (builder parity), so
2273
+ // the single-line policies are dead data here.
2274
+ for (node.attrs) |attribute| {
2275
+ if (std.mem.eql(u8, attribute.name, "wrap") or std.mem.eql(u8, attribute.name, "overflow")) {
2276
+ fail(node, markup.span_paragraph_wrap_message);
2277
+ }
2278
+ }
2279
+ for (node.children) |child| {
2280
+ if (child.kind != .text and !markup.nodeIsSpan(child)) {
2281
+ fail(child, markup.text_inline_children_message);
2282
+ }
2283
+ }
2284
+ }
2285
+ const spans = ui.arena.alloc(canvas.TextSpan, node.children.len) catch {
2286
+ ui.failed = true;
2287
+ return ui.el(.text, options, .{});
2288
+ };
2289
+ inline for (0..node.children.len) |index| {
2290
+ const child = comptime node.children[index];
2291
+ if (comptime (child.kind == .text)) {
2292
+ spans[index] = .{ .text = interpolatedRun(node, comptime child.text, entries, ui, model, scope) };
2293
+ } else {
2294
+ spans[index] = buildSpan(child, entries, ui, model, scope);
2295
+ }
2296
+ }
2297
+ return ui.paragraph(options, spans);
2298
+ }
2299
+
2300
+ /// Comptime mirror of the interpreter's `buildSpan`: the shared
2301
+ /// shape check at comptime, literal weight, scale, and foreground
2302
+ /// resolved at comptime (a typo or dead multiplier is a compile
2303
+ /// error), bound weight, scale, and the flags resolved at runtime
2304
+ /// like any option attribute.
2305
+ fn buildSpan(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype) canvas.TextSpan {
2306
+ comptime {
2307
+ if (markup.spanShapeError(node)) |info| failInfo(info);
2308
+ }
2309
+ var span = canvas.TextSpan{};
2310
+ if (comptime (node.attr("weight") != null)) {
2311
+ const raw = comptime node.attr("weight").?;
2312
+ const expression = comptime (markup.parseAttrExpression(raw) orelse fail(node, invalid_expression_message));
2313
+ if (comptime (expression == .literal)) {
2314
+ span.weight = comptime (std.meta.stringToEnum(canvas.TextSpanWeight, expression.literal) orelse
2315
+ fail(node, markup.span_weight_value_message));
2316
+ } else {
2317
+ comptime requireVariant(exprVariant(node, entries, raw), &.{.string}, node, markup.span_weight_value_message);
2318
+ const text = switch (evalExpr(node, entries, raw, ui, model, scope)) {
2319
+ .string => |value| value,
2320
+ else => runtimeFail([]const u8, ui),
2321
+ };
2322
+ span.weight = std.meta.stringToEnum(canvas.TextSpanWeight, text) orelse runtimeFail(canvas.TextSpanWeight, ui);
2323
+ }
2324
+ }
2325
+ if (comptime (node.attr("scale") != null)) {
2326
+ const raw = comptime node.attr("scale").?;
2327
+ const expression = comptime (markup.parseAttrExpression(raw) orelse fail(node, invalid_expression_message));
2328
+ if (comptime (expression == .literal)) {
2329
+ // A literal multiplier resolves at comptime; the shared
2330
+ // shape check already proved it positive and finite.
2331
+ span.scale = comptime (std.fmt.parseFloat(f32, expression.literal) catch
2332
+ fail(node, markup.span_scale_value_message));
2333
+ } else {
2334
+ // Bound scale: the binding must be a number, and the
2335
+ // value is held to the validator's positive-finite
2336
+ // bound (interpreter parity) — the engine draws
2337
+ // anything else at the base size, and a silently dead
2338
+ // binding is worse than a failed build.
2339
+ comptime requireVariant(exprVariant(node, entries, raw), &.{ .float, .integer }, node, markup.span_scale_value_message);
2340
+ const multiplier: f32 = switch (evalExpr(node, entries, raw, ui, model, scope)) {
2341
+ .float => |float| float,
2342
+ .integer => |int| @floatFromInt(int),
2343
+ else => runtimeFail(f32, ui),
2344
+ };
2345
+ span.scale = if (std.math.isFinite(multiplier) and multiplier > 0) multiplier else runtimeFail(f32, ui);
2346
+ }
2347
+ }
2348
+ if (comptime (node.attr("mono") != null)) {
2349
+ span.monospace = evalExpr(node, entries, comptime node.attr("mono").?, ui, model, scope).truthy();
2350
+ }
2351
+ if (comptime (node.attr("italic") != null)) {
2352
+ span.italic = evalExpr(node, entries, comptime node.attr("italic").?, ui, model, scope).truthy();
2353
+ }
2354
+ if (comptime (node.attr("underline") != null)) {
2355
+ span.underline = evalExpr(node, entries, comptime node.attr("underline").?, ui, model, scope).truthy();
2356
+ }
2357
+ if (comptime (node.attr("foreground") != null)) {
2358
+ span.color = comptime (std.meta.stringToEnum(canvas.TextSpanColor, node.attr("foreground").?) orelse
2359
+ fail(node, markup.unknown_color_token_message));
2360
+ }
2361
+ span.text = interpolatedRun(node, comptime node.children[0].text, entries, ui, model, scope);
2362
+ return span;
2363
+ }
2364
+
2365
+ fn interpolatedText(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype) []const u8 {
2366
+ const text = comptime blk: {
2367
+ if (node.children.len > 1) fail(node, "text elements take a single run of text");
2368
+ var content: []const u8 = "";
2369
+ for (node.children) |child| {
2370
+ if (child.kind != .text) {
2371
+ // A span here sits inside a single-style label,
2372
+ // not a paragraph (interpreter and validator
2373
+ // parity).
2374
+ if (markup.nodeIsSpan(child)) fail(child, markup.span_text_only_message);
2375
+ fail(node, "text elements may only contain text");
2376
+ }
2377
+ content = child.text;
2378
+ }
2379
+ break :blk content;
2380
+ };
2381
+ return interpolatedRun(node, text, entries, ui, model, scope);
2382
+ }
2383
+
2384
+ /// Interpolate ONE text run (`{...}` bindings and expressions)
2385
+ /// into the build arena: the shared body behind plain text leaves
2386
+ /// and every span-paragraph run. `node` positions diagnostics.
2387
+ fn interpolatedRun(comptime node: markup.MarkupNode, comptime text: []const u8, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype) []const u8 {
2388
+ if (comptime (std.mem.indexOfScalar(u8, text, '{') == null)) return text;
2389
+
2390
+ var out: std.ArrayListUnmanaged(u8) = .empty;
2391
+ const segments = comptime textSegments(node, text);
2392
+ inline for (0..segments.len) |index| {
2393
+ const segment = comptime segments[index];
2394
+ if (comptime (segment == .literal)) {
2395
+ out.appendSlice(ui.arena, comptime segment.literal) catch return runtimeFail([]const u8, ui);
2396
+ } else if (comptime (segment == .binding)) {
2397
+ const value = bindingValue(node, entries, comptime segment.binding, ui, model, scope, true);
2398
+ interpreter.appendValue(&out, ui.arena, value) catch return runtimeFail([]const u8, ui);
2399
+ } else {
2400
+ const value = evalExpressionTree(node, entries, comptime segment.expression, ui, model, scope);
2401
+ interpreter.appendValue(&out, ui.arena, value) catch return runtimeFail([]const u8, ui);
2402
+ }
2403
+ }
2404
+ return out.items;
2405
+ }
2406
+
2407
+ // ------------------------------------------------------- values
2408
+
2409
+ const ValueVariant = enum { string, integer, float, boolean };
2410
+
2411
+ /// Types `valueOf` can represent, mirroring the interpreter's
2412
+ /// runtime acceptance (unsupported types make it return null, which
2413
+ /// the interpreter reports as an unresolvable binding).
2414
+ fn supportedValue(comptime T: type) bool {
2415
+ return switch (@typeInfo(T)) {
2416
+ .bool, .int, .float, .comptime_int, .@"enum" => true,
2417
+ .pointer => |info| info.size == .slice and info.child == u8,
2418
+ .optional => |info| supportedValue(info.child),
2419
+ else => false,
2420
+ };
2421
+ }
2422
+
2423
+ /// The `Value` variant a leaf type produces, or null for optionals
2424
+ /// (none resolves to boolean, some to the child's variant — only
2425
+ /// known at runtime).
2426
+ fn bindingVariant(comptime T: type) ?ValueVariant {
2427
+ return switch (@typeInfo(T)) {
2428
+ .bool => .boolean,
2429
+ .int, .comptime_int => .integer,
2430
+ .float => .float,
2431
+ .@"enum" => .string,
2432
+ .pointer => .string,
2433
+ .optional => null,
2434
+ else => null,
2435
+ };
2436
+ }
2437
+
2438
+ fn requireVariant(comptime variant: ?ValueVariant, comptime allowed: []const ValueVariant, comptime node: markup.MarkupNode, comptime message: []const u8) void {
2439
+ comptime {
2440
+ const known = variant orelse return; // runtime-known: checked when the value flows
2441
+ for (allowed) |candidate| {
2442
+ if (known == candidate) return;
2443
+ }
2444
+ fail(node, message);
2445
+ }
2446
+ }
2447
+
2448
+ /// A runtime conversion the interpreter would fail the build on:
2449
+ /// latch `ui.failed` (finalize surfaces it) and produce an inert
2450
+ /// placeholder that never escapes the failed build.
2451
+ fn runtimeFail(comptime T: type, ui: *Ui) T {
2452
+ ui.failed = true;
2453
+ return zeroValue(T);
2454
+ }
2455
+
2456
+ fn zeroValue(comptime T: type) T {
2457
+ return switch (comptime @typeInfo(T)) {
2458
+ .int, .float => 0,
2459
+ .bool => false,
2460
+ .@"enum" => |info| @field(T, info.fields[0].name),
2461
+ .pointer => "",
2462
+ else => comptime @compileError("no placeholder for " ++ @typeName(T)),
2463
+ };
2464
+ }
2465
+
2466
+ // -------------------------------------------------- diagnostics
2467
+
2468
+ /// Comptime mirror of the interpreter's context-menu extraction:
2469
+ /// splits one direct `<context-menu>` child off the element (the
2470
+ /// menu is metadata, not content) after running the SAME shared
2471
+ /// checks — single menu, eligible host, and the closed item
2472
+ /// shape (`markup.contextMenuShapeError`).
2473
+ const ContextMenuSplit = struct {
2474
+ inner: markup.MarkupNode,
2475
+ menu: ?markup.MarkupNode,
2476
+ };
2477
+
2478
+ fn splitContextMenuChild(comptime node: markup.MarkupNode) ContextMenuSplit {
2479
+ comptime {
2480
+ var menu: ?markup.MarkupNode = null;
2481
+ for (node.children) |child| {
2482
+ if (!markup.nodeIsContextMenu(child)) continue;
2483
+ if (menu != null) fail(child, markup.context_menu_single_message);
2484
+ if (!markup.contextMenuHostEligible(node)) fail(child, markup.context_menu_host_message);
2485
+ if (markup.contextMenuShapeError(child)) |info| failInfo(info);
2486
+ menu = child;
2487
+ }
2488
+ if (menu == null) return .{ .inner = node, .menu = null };
2489
+ var filtered: []const markup.MarkupNode = &.{};
2490
+ for (node.children) |child| {
2491
+ if (markup.nodeIsContextMenu(child)) continue;
2492
+ filtered = filtered ++ &[_]markup.MarkupNode{child};
2493
+ }
2494
+ var inner = node;
2495
+ inner.children = filtered;
2496
+ return .{ .inner = inner, .menu = menu };
2497
+ }
2498
+ }
2499
+
2500
+ /// Comptime mirror of the interpreter's reactions extraction:
2501
+ /// splits one direct `<reactions>` child off a bubble (the pill
2502
+ /// is chrome, not content) after the SAME shared checks — bubble
2503
+ /// host only, a single pill, and the closed shape
2504
+ /// (`markup.reactionsShapeError`).
2505
+ const ReactionsSplit = struct {
2506
+ inner: markup.MarkupNode,
2507
+ pill: ?markup.MarkupNode,
2508
+ };
2509
+
2510
+ fn splitReactionsChild(comptime node: markup.MarkupNode, comptime kind: canvas.WidgetKind) ReactionsSplit {
2511
+ comptime {
2512
+ var pill: ?markup.MarkupNode = null;
2513
+ for (node.children) |child| {
2514
+ if (!markup.nodeIsReactions(child)) continue;
2515
+ if (kind != .bubble) fail(child, markup.reactions_parent_message);
2516
+ if (pill != null) fail(child, markup.reactions_single_message);
2517
+ if (markup.reactionsShapeError(child)) |info| failInfo(info);
2518
+ pill = child;
2519
+ }
2520
+ if (pill == null) return .{ .inner = node, .pill = null };
2521
+ var filtered: []const markup.MarkupNode = &.{};
2522
+ for (node.children) |child| {
2523
+ if (markup.nodeIsReactions(child)) continue;
2524
+ filtered = filtered ++ &[_]markup.MarkupNode{child};
2525
+ }
2526
+ var inner = node;
2527
+ inner.children = filtered;
2528
+ return .{ .inner = inner, .pill = pill };
2529
+ }
2530
+ }
2531
+
2532
+ /// `fail` from a shared shape check's positioned error info.
2533
+ fn failInfo(comptime info: markup.MarkupErrorInfo) noreturn {
2534
+ if (info.path.len > 0) {
2535
+ @compileError(std.fmt.comptimePrint("markup error in {s} at line {d}, column {d}: {s}", .{
2536
+ info.path,
2537
+ info.line,
2538
+ info.column,
2539
+ info.message,
2540
+ }));
2541
+ }
2542
+ @compileError(std.fmt.comptimePrint("markup error at line {d}, column {d}: {s}", .{
2543
+ info.line,
2544
+ info.column,
2545
+ info.message,
2546
+ }));
2547
+ }
2548
+
2549
+ fn fail(comptime node: markup.MarkupNode, comptime message: []const u8) noreturn {
2550
+ if (node.src_path.len > 0) {
2551
+ @compileError(std.fmt.comptimePrint("markup error in {s} at line {d}, column {d}: {s}", .{
2552
+ node.src_path,
2553
+ node.line,
2554
+ node.column,
2555
+ message,
2556
+ }));
2557
+ }
2558
+ @compileError(std.fmt.comptimePrint("markup error at line {d}, column {d}: {s}", .{
2559
+ node.line,
2560
+ node.column,
2561
+ message,
2562
+ }));
2563
+ }
2564
+ };
2565
+ }