Hash :
987e989c
Author :
Thomas de Grivel
Date :
2017-02-13T12:13:55
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
(in-package :parse-css)
(defclass item ()
((parent :initarg :parent
:accessor item-parent
:type (or null item))))
(defclass named-item (item)
((name :initarg :name
:accessor item-name
:type string)))
(defclass with-prelude (item)
((prelude :initarg :prelude
:initform nil
:accessor item-prelude
:type list)))
(defclass with-items (item)
((items :initarg :items
:initform nil
:accessor item-items
:type list)))
(defclass stylesheet (with-items) ())
(defclass toplevel-flag (item)
((toplevel :initarg :toplevel
:initform nil
:accessor item-toplevel
:type boolean)))
(defclass at-rule (named-item with-prelude with-items toplevel-flag) ())
(defclass qualified-rule (with-prelude with-items toplevel-flag) ())
(defclass css-declaration (named-item)
((values :initarg :value
:initform nil
:accessor item-value
:type list)
(important-p :initarg :important
:initform nil
:accessor important-p
:type boolean)))
(defclass component-value (item) ())
(defclass values-block (item)
((values :initarg :values
:accessor item-values
:type list)))
(defclass {}-block (values-block) ())
(defclass paren-block (values-block) ())
(defclass []-block (values-block) ())
(defclass function-block (values-block)
((function :initarg :function
:reader item-function
:type function-token)))
(defun make-item-buffer ()
(make-array 64
:adjustable t
:fill-pointer 0))
(defmethod item-push-extend ((p parser) (item item))
(vector-push-extend item (parser-ib p) 64))
(defmethod item-input ((p parser) (length fixnum))
(unless (= 0 length)
(item-push-extend p (consume-token p))
(item-input p (1- length))))
(defmethod parser-match-item ((p parser))
(item-input p 1)
(aref (parser-ib p) (parser-item-match-start p)))
(defmethod item-match ((p parser) (type symbol))
(let ((item (parser-match-item p)))
(when (typep item type)
(incf (parser-item-match-start p))
item)))
(defmethod item-match ((p parser) (type list))
(let ((item (parser-match-item p)))
(when (typep item type)
(incf (parser-item-match-start p))
item)))
;; Parsing
(defgeneric parser-error (parser message))
(defgeneric assert-item (parser type))
(defgeneric parse-at-rule (parser &key toplevel))
(defgeneric parse-qualified-rule (parser &key toplevel))
(defgeneric parse-component-value (parser))
(defgeneric parse-component-value* (parser))
(defgeneric parse-{}-block (parser))
(defgeneric parse-paren-block (parser))
(defgeneric parse-[]-block (parser))
(defgeneric parse-function-block (parser))
(defgeneric parse-preserved-token (parser))
(defmethod parser-error ((p parser) (message string))
(error "CSS error ~A:~D:~D ~A"
(parser-input p)
(parser-input-line p)
(parser-input-character p)
message))
(defmethod assert-item ((p parser) (type symbol))
(or (typep (parser-item p) type)
(parser-error p (format nil "rule expected inside ~A" type))))
(defmethod pop-parser-item ((p parser))
(setf (parser-item p) (item-parent (parser-item p))))
(defmethod parse-component-value ((p parser))
(or (parse-function-block p)
(parse-[]-block p)
(parse-paren-block p)
(parse-{}-block p)
(parse-preserved-token p)))
(defmethod parse-component-value* ((p parser))
(let ((value (parse-component-value p)))
(when value
(cons value (parse-component-value* p)))))
(defmethod parse-{}-block ((p parser))
(when ({-token p)
(let ((values (parse-component-value* p)))
(if (}-token p)
(make-instance '{}-block
:values values)
(parser-error p "expected '}'")))))
(defmethod parse-paren-block ((p parser))
(when (left-paren-token p)
(let ((values (parse-component-value* p)))
(if (right-paren-token p)
(make-instance 'paren-block
:values values)
(parser-error p "expected ')'")))))
(defmethod parse-[]-block ((p parser))
(when ([-token p)
(let ((values (parse-component-value* p)))
(if (]-token p)
(make-instance '[]-block
:values values)
(parser-error p "expected ']'")))))
(defmethod parse-function-block ((p parser))
(let ((fun (function-token p)))
(when fun
(let ((values (parse-component-value* p)))
(if (right-paren-token p)
(make-instance 'function-block
:function fun
:values values)
(parser-error p "expected ')'"))))))
(defmethod parse-preserved-token ((p parser))
(let ((token (consume-token p)))
(if (typep token '(or function-token {-token left-paren-token [-token))
(parser-error p (format nil "unexpected '~A'" (token-string token)))
token)))
(defmethod parse-at-rule ((p parser) &key toplevel)
(let ((at-keyword (at-keyword-token p)))
(when at-keyword
(let* ((parent (parser-item p))
(name (token-string (token-ident at-keyword)))
(prelude (parse-component-value* p))
(item (make-instance 'at-rule
:parent parent
:name name
:prelude prelude
:toplevel toplevel)))
(setf (parser-item p) item)
(cond ((or (semicolon-token p)
(parse-{}-block p))
(push item (item-items parent))
(pop-parser-item p)
item)
(t
(pop-parser-item p)
nil))))))
(defmethod parse-rule-list ((p parser))
(if (or (parse-at-rule p)
(parse-qualified-rule p)
(whitespace-token p))
(parse-rule-list p))
t)
(defmethod parse-stylesheet ((p parser))
(setf (parser-item p) (make-instance 'stylesheet :parent nil))
(or (and (or (parse-at-rule p :toplevel t)
(parse-qualified-rule p :toplevel t)
(whitespace-token p)
(cdc-token p)
(cdc-token p))
(parse-stylesheet p))
(eof-token p)
(parser-error p "at top level")))