Change HSX parsing environment from compile-time to runtime

This commit is contained in:
Akira Tempaku 2024-05-30 12:55:13 +09:00
commit 8d3a2588d0
7 changed files with 109 additions and 128 deletions

View file

@ -9,23 +9,31 @@
#:defcomp))
(in-package #:hsx/defhsx)
(defun %create-element (type &rest body)
(multiple-value-bind (props children)
(parse-body body)
(create-element type props children)))
(defmacro defhsx (name element-type)
`(defmacro ,name (&body body)
(multiple-value-bind (props children)
(parse-body body)
`(create-element ,',element-type (list ,@props) ,@children))))
`(%create-element ,',element-type ,@body)))
(defun parse-body (body)
(if (keywordp (first body))
(loop :for thing :on body :by #'cddr
:for (k v) := thing
:when (and (keywordp k) v)
:append (list k v) :into props
:when (not (keywordp k))
:return (values props thing)
:finally (return (values props nil)))
(values nil body)))
(cond (; plist
(and (listp (first body))
(keywordp (first (first body))))
(values (first body) (rest body)))
(; inline-plist
(keywordp (first body))
(loop :for thing :on body :by #'cddr
:for (k v) := thing
:when (and (keywordp k) v)
:append (list k v) :into props
:when (not (keywordp k))
:return (values props thing)
:finally (return (values props nil))))
(t (values nil body))))
(defmacro deftag (name)
`(eval-when (:compile-toplevel :load-toplevel :execute)
(defhsx ,name ,(make-keyword name))))

View file

@ -1,6 +1,11 @@
(defpackage #:hsx/element
(:use #:cl)
(:export #:create-element
(:export #:element
#:tag
#:html-tag
#:fragment
#:component
#:create-element
#:element-type
#:element-props
#:element-children
@ -31,7 +36,7 @@
;;;; factory
(defun create-element (type props &rest children)
(defun create-element (type props children)
(make-instance (cond ((functionp type) 'component)
((eq type :<>) 'fragment)
((eq type :html) 'html-tag)

View file

@ -10,7 +10,9 @@
(defun find-builtin-symbols (node)
(if (atom node)
(or (find-symbol (string node) :hsx/builtin)
(or (and (symbolp node)
(not (keywordp node))
(find-symbol (string node) :hsx/builtin))
node)
(cons (find-builtin-symbols (car node))
(mapcar (lambda (n)