Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;;;;;;;;; lQuery 1.0.4 macro ;;;;;;;;;;
- (defmacro $ (&rest actions)
- `(let ((working-nodes (list *lquery-master-document*)))
- (progn
- ,@(loop for action in actions collect
- `(setf working-nodes
- ,(if (and (listp action) (not (eq (first action) 'FUNCTION)))
- (let ((first (car action)))
- (cond ((dom:node-p first) action)
- ((find-symbol (mkstr 'nodefun- first) :lquery)
- `(,(find-symbol (mkstr 'nodefun- first) :lquery)
- working-nodes
- ,@(cdr action)))
- (T (loop with name = (mkstr first)
- for package in (list :lquery *PACKAGE* :cl)
- for symbol = (find-symbol name package)
- if symbol
- do (return `(,symbol ,@(cdr action)))))))
- `($-helper ,action :working-nodes working-nodes)))))
- working-nodes))
- (defgeneric $-helper (action &key &allow-other-keys))
- (defmethod $-helper (action &key)
- action)
- (defmethod $-helper ((action function) &key working-nodes)
- (funcall action working-nodes))
- (defmethod $-helper ((action string) &key working-nodes)
- (css:query action working-nodes))
- (defmethod $-helper ((action dom:node) &key)
- (list action))
- ;;; SAMPLE OUTPUT ;;;
- ;; LQUERY> (macroexpand-1 '($ "aaa" (add-class "test") (children) (remove) #'test test))
- ;; (LET ((WORKING-NODES (LIST *LQUERY-MASTER-DOCUMENT*)))
- ;; (PROGN
- ;; (SETF WORKING-NODES ($-HELPER "aaa" :WORKING-NODES WORKING-NODES))
- ;; (SETF WORKING-NODES (NODEFUN-ADD-CLASS WORKING-NODES "test"))
- ;; (SETF WORKING-NODES (NODEFUN-CHILDREN WORKING-NODES))
- ;; (SETF WORKING-NODES (NODEFUN-REMOVE WORKING-NODES))
- ;; (SETF WORKING-NODES ($-HELPER #'TEST :WORKING-NODES WORKING-NODES))
- ;; (SETF WORKING-NODES ($-HELPER TEST :WORKING-NODES WORKING-NODES)))
- ;; WORKING-NODES)
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;;;;;;;;; lQuery 2.0.0 macro ;;;;;;;;;;
- (defmacro $2 (&body actions)
- (%$2 (reverse actions)))
- (defun %$2 (actions)
- (if (zerop (length actions))
- `*lquery-master-document*
- (let ((action (car actions))
- (rest (cdr actions)))
- (determine-argument (%$2 rest) action))))
- (defgeneric determine-symbol (nodes symbol))
- (defgeneric determine-argument (nodes arg))
- (defmacro define-symbol-handler (type (symbolname nodesname) &body body)
- `(defmethod determine-symbol (,nodesname (,symbolname ,type))
- ,@body))
- (defmacro define-argument-handler (type (argname nodesname) &body body)
- `(defmethod determine-argument (,nodesname (,argname ,type))
- ,@body))
- (define-argument-handler list (list nodes)
- (when list
- (let ((function (car list)))
- (if (eq function 'FUNCTION)
- `(,(cadr list) ,nodes)
- (let ((nodefun (find-symbol (format NIL "NODEFUN-~a" function) :lquery)))
- (if nodefun
- (append `(,nodefun ,nodes) (cdr list))
- (append `(,function ,nodes) (cdr list))))))))
- (define-argument-handler symbol (symbol nodes)
- `(determine-symbol ,nodes ,symbol))
- (define-argument-handler string (string nodes)
- `(css:query ,string ,nodes))
- (define-symbol-handler string (selector nodes)
- (css:query selector nodes))
- (define-symbol-handler T (variable nodes)
- (declare (ignorable nodes))
- variable)
- (define-symbol-handler dom:node (node nodes)
- (declare (ignorable nodes))
- (list node))
- ;;; SAMPLE OUTPUT ;;;
- ;; LQUERY> (macroexpand-1 '($2 "aaa" (add-class "test") (children) (remove) #'test test))
- ;; (DETERMINE-SYMBOL
- ;; (TEST
- ;; (NODEFUN-REMOVE
- ;; (NODEFUN-CHILDREN
- ;; (NODEFUN-ADD-CLASS (QUERY "aaa" *LQUERY-MASTER-DOCUMENT*) "test"))))
- ;; TEST)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement