Mon Nov 16 09:02:50 UTC 2009 pix@kepibu.org * Status commit diff -rN -u old-Oh, Ducks!/regexp-template.lisp new-Oh, Ducks!/regexp-template.lisp --- old-Oh, Ducks!/regexp-template.lisp 2015-10-29 08:45:36.000000000 +0000 +++ new-Oh, Ducks!/regexp-template.lisp 2015-10-29 08:45:36.000000000 +0000 @@ -29,9 +29,6 @@ (destructuring-bind (re-kwd regexp &optional vars &rest keys) spec (declare (ignore re-kwd)) - (format t "rex: ~s, ~s~%" - (concatenate 'string "^(.*?)" regexp "$") - (append '(?&rest) vars)) (make-instance 'unify::regular-expression-template :spec (list* 'unify::regexp (concatenate 'string "^(.*?)" regexp "$") diff -rN -u old-Oh, Ducks!/selectors.lisp new-Oh, Ducks!/selectors.lisp --- old-Oh, Ducks!/selectors.lisp 2015-10-29 08:45:36.000000000 +0000 +++ new-Oh, Ducks!/selectors.lisp 2015-10-29 08:45:36.000000000 +0000 @@ -73,6 +73,8 @@ (defclass adjacent-combinator (combinator) ()) (defclass sibling-combinator (combinator) ()) +#+FIXME ; is this the right name? +(defclass universal-selector (simple-selector) ()) (defclass type-selector (simple-selector) ()) (defclass id-selector (simple-selector) ()) (defclass class-selector (simple-selector) ()) @@ -82,108 +84,37 @@ (let ((selector (template-spec template))) (setf (slot-value template 'matcher) (parse-selector (string-trim " " selector)))))) -;; forwards -#+(or) (defun parse-selector (selector) (match-case (selector) ;; combinators - (#T(regexp+ "^[ ]*[>][ ]*" ()) (list (make-instance 'child-combinator :matcher (parse-selector &rest)))) - (#T(regexp+ "^[ ]+" ()) (list (make-instance 'descendant-combinator :matcher (parse-selector &rest)))) + (#T(regexp$ "[ ]*[>][ ]*" ()) (list (make-instance 'child-combinator :matcher (parse-selector &rest)))) + (#T(regexp$ "[ ]+" ()) (list (make-instance 'descendant-combinator :matcher (parse-selector &rest)))) ;; simple selector - (#T(regexp+ "^(\\w+)" (?type)) (cons (make-instance 'type-selector :arg type) (parse-selector &rest))) - (#T(regexp+ "^[#](\\w+)" (?id)) (cons (make-instance 'id-selector :arg id) (parse-selector &rest))) - (#T(regexp+ "^[\\.](\\w+)" (?class)) (cons (make-instance 'class-selector :arg class) (parse-selector &rest))))) - -;; backwards -;; FIXME: somehow, selector is ending up as "NIL" -(defun parse-selector (selector) - (when (string= "NIL" selector) (error "selector is nil")) - (format t "selector: ~s~%" selector) - (macrolet ((prest (x) `(format t "rest~s: ~S~%" ,x &rest))) - (match-case (selector) - ;; combinators - (#T(regexp$ "[ ]*[>][ ]*" ()) (prest 'a) (list (make-instance 'child-combinator :matcher (parse-selector &rest)))) - (#T(regexp$ "[ ]+" ()) (prest 'b) (list (make-instance 'descendant-combinator :matcher (parse-selector &rest)))) - ;; simple selector - (#T(regexp$ "[#](\\w+)" (?id)) (prest 'c) (cons (make-instance 'id-selector :arg id) (parse-selector &rest))) - (#T(regexp$ "[\\.](\\w+)" (?class)) (prest 'd) (cons (make-instance 'class-selector :arg class) (parse-selector &rest))) - (#T(regexp$ "(\\w+)" (?type)) (prest 'e) (cons (make-instance 'type-selector :arg type) (parse-selector &rest)))))) - - -;; FIXME: the find/matches split seems to be causing me some mental -;; trouble. I'm not sure how to handle combinators now. (Not that I -;; was doing very well with them before.) -;; -;; Should probably map this out on a whiteboard. I'm not doing very -;; well with just trying to hack it. + (#T(regexp$ "[#](\\w+)" (?id)) (cons (make-instance 'id-selector :arg id) (parse-selector &rest))) + (#T(regexp$ "[\\.](\\w+)" (?class)) (cons (make-instance 'class-selector :arg class) (parse-selector &rest))) + (#T(regexp$ "(\\w+)" (?type)) (cons (make-instance 'type-selector :arg type) (parse-selector &rest))) + #+(or) + (#T(regexp$ "\\*" ()) (cons (make-instance 'universal-selector) (parse-selector &rest))))) (defgeneric find-matching-elements (selector elements)) -(defmethod find-matching-elements ((selectors list) element) - (call-next-method) - #+(or) - (prog1 - (when (element-matches-p selectors element) - (when (every (alexandria:rcurry #'find-matching-elements element) selectors) - element) - (format t "lv fme~%")))) - -(defmethod find-matching-elements ((selector selector) (elements list)) - (call-next-method)) - (defmethod find-matching-elements (selector (elements list)) - (format t "in fme: ~s~%" elements) - (prog1 (nconc (remove-if-not (lambda (el) (element-matches-p el selector)) elements) (reduce #'nconc (remove-if #'null (mapcar (lambda (element) (find-matching-elements selector (element-children element))) - elements)))) - (format t "lv fme~%"))) + elements))))) (defmethod find-matching-elements (selector (elements t)) (find-matching-elements selector (list elements))) -;; for lhtml compatibility -#|| -(defvar *ancestors* nil) -(defmethod find-matching-elements ((selector selector) (elements cons)) - (if (keywordp (car elements)) - (remove-if #'null - (nconc - (when (element-matches-p elements selector) (list elements)) - (let ((*ancestors* (cons elements nil))) - (mapcar (lambda (el) (find-matching-elements selector el)) - (element-children elements))))) - (call-next-method))) -||# - (defgeneric element-matches-p (element selector)) -(defmethod element-matches-p ((element t) (selector selector)) - (error "ar?") - nil) -(defmethod element-matches-p :around ((element string) (selector selector)) - (call-next-method)) - -(defmethod element-matches-p :around ((element t) (selector selector)) - (call-next-method) - #+(or) - (format t "c: ~s, ~s, ~s~%" - (class-of element) - (class-of selector) - (find-method #'element-matches-p '() (list (find-class t) (class-of selector)) nil)) - #+(or) - (when (find-method #'element-matches-p '() (mapcar #'class-of (list element selector)) nil) - (call-next-method))) - (defmethod element-matches-p (element (selector type-selector)) (element-type-equal element (selector-arg selector))) (defmethod element-matches-p (element (selector id-selector)) - ;(cerror "hrm" element) - (format t "id: ~s~%" (element-id element)) (string= (element-id element) (selector-arg selector))) (defmethod element-matches-p (element (selector class-selector)) @@ -191,23 +122,6 @@ (element-classes element) :test #'string=)) -#+(or) -(defmethod element-matches-p (element (selector child-combinator)) - (plusp (length (find-matching-elements selector (element-children element))))) - -#+TODO -(defmethod element-matches-p (element (selector descendant-combinator)) - (flet ((all-match (element) (every (lambda (m) (element-matches-p element m)) (matcher selector)))) - #+nil - (when (all-match element) element) - (let ((elements (cddr element))) - (format t "el: ~s~%" elements) - (case (selector-arg selector) - (:direct (loop :for element :in elements - :when (all-match element) - :collect element)) - (t (css-collect-elements selector elements)))))) - (defmethod element-matches-p (element (selector list)) (every (lambda (s) (element-matches-p element s)) selector)) diff -rN -u old-Oh, Ducks!/tests.lisp new-Oh, Ducks!/tests.lisp --- old-Oh, Ducks!/tests.lisp 2015-10-29 08:45:36.000000000 +0000 +++ new-Oh, Ducks!/tests.lisp 2015-10-29 08:45:36.000000000 +0000 @@ -28,9 +28,10 @@ i)) (equalp '((:i () "not")) - (match (#T(html ("div" ("> i" . ?i))) + (match (#T(html ("div" ("> i" . ?i) + ("span>i" . ?span))) "