html-syntax.lisp 15.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
;;; -*- Mode: Lisp; Package: COMMON-LISP-USER -*-

;;;  (c) copyright 2005 by
;;;           Robert Strandh (strandh@labri.fr)

;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Library General Public
;;; License as published by the Free Software Foundation; either
;;; version 2 of the License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Library General Public License for more details.
;;;
;;; You should have received a copy of the GNU Library General Public
;;; License along with this library; if not, write to the
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;;; Boston, MA  02111-1307  USA.

;;; Syntax for analysing HTML

23
(in-package :climacs-html-syntax)
24 25 26 27 28 29 30 31 32 33 34 35 36

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; grammar classes

(defclass html-sym ()
  ((badness :initform 0 :initarg :badness :reader badness)
   (message :initform "" :initarg :message :reader message)))

(defmethod parse-tree-better ((t1 html-sym) (t2 html-sym))
  (and (eq (class-of t1) (class-of t2))
       (< (badness t1) (badness t2))))

37 38 39 40 41
(defclass html-nonterminal (html-sym)
  ((start-offset :initarg :start-offset :reader start-offset)
   (end-offset :initarg :end-offset :reader end-offset)))

(defclass words (html-nonterminal) ())
42 43 44 45 46 47 48

(defclass empty-words (words) ())

(defclass nonempty-words (words)
  ((words :initarg :words)
   (word :initarg :word)))

49
(defclass html-balanced (html-nonterminal)
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
  ((start :initarg :start)
   (end :initarg :end)))

(defclass html (html-balanced)
  ((head :initarg :head)
   (body :initarg :body)))

(defclass head (html-balanced)
  ((title :initarg :title)))

(defclass html-words (html-balanced)
  ((words :initarg :words)))

(defclass title (html-words) ())
(defclass body (html-words) ())
(defclass h1 (html-words) ())
(defclass h2 (html-words) ())
(defclass h3 (html-words) ())
(defclass para (html-words) ())

(defclass html-token (html-sym)
  ((start-mark :initarg :start-mark :reader start-mark)
   (size :initarg :size)))

(defgeneric end-offset (html-token))

(defmethod end-offset ((token html-token))
  (with-slots (start-mark size) token
     (+ (offset start-mark) size)))

(defgeneric start-offset (html-token))

(defmethod start-offset ((token html-token))
  (offset (start-mark token)))

(defclass <html> (html-token) () (:default-initargs :size 6))
(defclass </html> (html-token) ()(:default-initargs :size 7))
(defclass <head> (html-token) () (:default-initargs :size 6))
(defclass </head> (html-token) () (:default-initargs :size 7))
(defclass <title> (html-token) () (:default-initargs :size 7))
(defclass </title> (html-token) () (:default-initargs :size 8))
(defclass <body> (html-token) () (:default-initargs :size 6))
(defclass </body> (html-token) () (:default-initargs :size 7))
(defclass <h1> (html-token) () (:default-initargs :size 4))
(defclass </h1> (html-token) () (:default-initargs :size 5))
(defclass <h2> (html-token) () (:default-initargs :size 4))
(defclass </h2> (html-token) () (:default-initargs :size 5))
(defclass <h3> (html-token) () (:default-initargs :size 4))
(defclass </h3> (html-token) () (:default-initargs :size 5))
(defclass <p> (html-token) () (:default-initargs :size 3))
(defclass </p> (html-token) () (:default-initargs :size 4))
(defclass <ul> (html-token) () (:default-initargs :size 4))
(defclass </ul> (html-token) () (:default-initargs :size 5))
(defclass <li> (html-token) () (:default-initargs :size 4))
(defclass </li> (html-token) () (:default-initargs :size 5))
105 106 107 108 109

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; lexer

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
(defclass html-element (html-token)
  ((state :initarg :state)))

(defclass start-element (html-element) ())
(defclass tag-start (html-element) ())
(defclass tag-end (html-element) ())
(defclass slash (html-element) ())
(defclass word (html-element) ())
(defclass delimiter (html-element) ())

(defun next-token (scan)
  (let ((start-mark (clone-mark scan)))
    (flet ((fo () (forward-object scan)))
      (macrolet ((make-entry (type)
		   `(return-from next-token
		      (make-instance ,type :start-mark start-mark
				     :size (- (offset scan) (offset start-mark))))))
	(loop with object = (object-after scan)
	      until (end-of-buffer-p scan)
	      do (case object
		   (#\< (fo) (make-entry 'tag-start))
		   (#\> (fo) (make-entry 'tag-end))
		   (#\/ (fo) (make-entry 'slash))
		   (t (cond ((alphanumericp object)
			     (loop until (end-of-buffer-p scan)
				   while (alphanumericp (object-after scan))
				   do (fo))
			     (make-entry 'word))
			    (t
			     (fo) (make-entry 'delimiter))))))))))

(define-syntax html-syntax ("HTML" (basic-syntax))
  ((tokens :initform (make-instance 'standard-flexichain))
   (guess-pos :initform 1)
   (valid-parse :initform 1)
   (parser)))
146 147 148 149 150

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; parser

151 152 153 154
(defun word-is (word string)
  (string-equal (coerce (region-to-sequence (start-mark word) (end-offset word)) 'string)
		string))

155 156
(defparameter *html-grammar*
  (grammar
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
    (<html> -> (tag-start
		(word (and (= (end-offset tag-start) (start-offset word))
			   (word-is word "html")))
		(tag-end (= (end-offset word) (start-offset tag-end))))
	    :start-mark (start-mark tag-start))
    (</html> -> (tag-start
		 (slash (= (end-offset tag-start) (start-offset slash)))
		 (word (and (= (end-offset slash) (start-offset word))
			    (word-is word "html")))
		 (tag-end (= (end-offset word) (start-offset tag-end))))
	     :start-mark (start-mark tag-start))
    (<head> -> (tag-start
		(word (and (= (end-offset tag-start) (start-offset word))
			   (word-is word "head")))
		(tag-end (= (end-offset word) (start-offset tag-end))))
	    :start-mark (start-mark tag-start))
    (</head> -> (tag-start
		 (slash (= (end-offset tag-start) (start-offset slash)))
		 (word (and (= (end-offset slash) (start-offset word))
			    (word-is word "head")))
		 (tag-end (= (end-offset word) (start-offset tag-end))))
	     :start-mark (start-mark tag-start))
    (<title> -> (tag-start
		 (word (and (= (end-offset tag-start) (start-offset word))
			    (word-is word "title")))
		 (tag-end (= (end-offset word) (start-offset tag-end))))
	     :start-mark (start-mark tag-start))
    (</title> -> (tag-start
		  (slash (= (end-offset tag-start) (start-offset slash)))
		  (word (and (= (end-offset slash) (start-offset word))
			     (word-is word "title")))
		  (tag-end (= (end-offset word) (start-offset tag-end))))
	      :start-mark (start-mark tag-start))
    (<body> -> (tag-start
		(word (and (= (end-offset tag-start) (start-offset word))
			   (word-is word "body")))
		(tag-end (= (end-offset word) (start-offset tag-end))))
	    :start-mark (start-mark tag-start))
    (</body> -> (tag-start
		 (slash (= (end-offset tag-start) (start-offset slash)))
		 (word (and (= (end-offset slash) (start-offset word))
			    (word-is word "body")))
		 (tag-end (= (end-offset word) (start-offset tag-end))))
	     :start-mark (start-mark tag-start))
    (html -> (<html> head body </html>)
202
	  :start-offset (start-offset <html>) :end-offset (end-offset </html>)
203 204
	  :start <html> :head head :body body :end </html>)
    (head -> (<head> title </head>)
205
	  :start-offset (start-offset <head>) :end-offset (end-offset </head>)
206 207
	  :start <head> :title title :end </head>)
    (title -> (<title> words </title>)
208
	   :start-offset (start-offset <title>) :end-offset (end-offset </title>)
209 210
	   :start <title> :words words :end </title>)
    (body -> (<body> words </body>)
211
	  :start-offset (start-offset <body>) :end-offset (end-offset </body>)
212 213
	  :start <body> :words words :end </body>)
    (words -> ()
214
	   (make-instance 'empty-words :start-offset nil))
215
    (words -> (words word)
216 217 218 219
	   (make-instance 'nonempty-words
	      :start-offset (or (start-offset words) (start-offset word))
	      :end-offset (end-offset word)
	      :words words :word word))))
220 221 222

(defmethod initialize-instance :after ((syntax html-syntax) &rest args)
  (declare (ignore args))
223
  (with-slots (parser tokens buffer) syntax
224 225 226
     (setf parser (make-instance 'parser
		     :grammar *html-grammar*
		     :target 'html))
227 228 229 230 231 232 233
     (insert* tokens 0 (make-instance 'start-element
			  :start-mark (make-instance 'standard-left-sticky-mark
					 :buffer buffer
					 :offset 0)
			  :size 0
			  :state (initial-state parser)))))

234 235 236 237
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; update syntax

238 239 240
(defmethod update-syntax-for-display (buffer (syntax html-syntax) top bot)
  (with-slots (parser tokens valid-parse) syntax
     (loop until (= valid-parse (nb-elements tokens))
241
	   while (mark<= (end-offset (element* tokens valid-parse)) bot)
242 243 244 245 246
	   do (let ((current-token (element* tokens (1- valid-parse)))
		    (next-token (element* tokens valid-parse)))
		(setf (slot-value next-token 'state)
		      (advance-parse parser (list next-token) (slot-value current-token 'state))))
	      (incf valid-parse))))
247 248

(defmethod update-syntax (buffer (syntax html-syntax))
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
  (let ((low-mark (low-mark buffer))
	(high-mark (high-mark buffer))
	(scan))
    (with-slots (tokens guess-pos valid-parse) syntax
       (when (mark<= low-mark high-mark)
	 ;; go back to a position before low-mark
	 (loop until (or (= guess-pos 1)
			 (mark< (end-offset (element* tokens (1- guess-pos))) low-mark))
	       do (decf guess-pos))
	 ;; go forward to the last position before low-mark
	 (loop with nb-elements = (nb-elements tokens)
	       until (or (= guess-pos nb-elements)
			 (mark>= (end-offset (element* tokens guess-pos)) low-mark))
	       do (incf guess-pos))
	 ;; mark valid parse
	 (setf valid-parse guess-pos)
	 ;; delete entries that must be reparsed
	 (loop until (or (= guess-pos (nb-elements tokens))
			 (mark> (start-mark (element* tokens guess-pos)) high-mark))
	       do (delete* tokens guess-pos))
	 (setf scan (make-instance 'standard-left-sticky-mark
		       :buffer buffer
		       :offset (if (zerop guess-pos)
				   0
				   (end-offset (element* tokens (1- guess-pos))))))
	 ;; scan
	 (loop with start-mark = nil
	       do (loop until (end-of-buffer-p scan)
			while (whitespacep (object-after scan))
			do (forward-object scan))
	       until (if (end-of-buffer-p high-mark)
			 (end-of-buffer-p scan)
			 (mark> scan high-mark))
	       do (setf start-mark (clone-mark scan))
		  (insert* tokens guess-pos (next-token scan))
		  (incf guess-pos))))))

286 287 288 289
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; display

290 291 292 293 294 295 296 297 298 299 300 301 302
(defun handle-whitespace (pane buffer start end)
  (let ((space-width (space-width pane))
	(tab-width (tab-width pane)))
    (loop while (< start end)
	  do (ecase (buffer-object buffer start)
	       (#\Newline (terpri pane))
	       (#\Space (stream-increment-cursor-position
			 pane space-width 0))
	       (#\Tab (let ((x (stream-cursor-position pane)))
			(stream-increment-cursor-position
			 pane (- tab-width (mod x tab-width)) 0))))
	     (incf start))))		    

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
(defmethod display-parse-tree :around ((entity html-sym) syntax pane)
  (with-slots (top bot) pane
     (when (mark> (end-offset entity) top)
       (call-next-method))))

(defmethod display-parse-tree :around ((entity empty-words) syntax pane)
  (declare (ignore syntax pane))
  nil)

(defmethod display-parse-tree ((entity html-token) (syntax html-syntax) pane)
  (updating-output (pane :unique-id entity
			 :id-test #'eq
			 :cache-value entity
			 :cache-test #'eq)
    (present (coerce (region-to-sequence (start-mark entity)
					 (end-offset entity))
		     'string)
	     'string
	     :stream pane)))

(defmethod display-parse-tree :before ((entity html-balanced) (syntax html-syntax) pane)
  (with-slots (start) entity
     (display-parse-tree start syntax pane)))

(defmethod display-parse-tree :after ((entity html-balanced) (syntax html-syntax) pane)
  (with-slots (end) entity
     (display-parse-tree end syntax pane)))

(defmethod display-parse-tree ((entity html-words) (syntax html-syntax) pane)
  (with-slots (words) entity
     (display-parse-tree words syntax pane)))

(defmethod display-parse-tree ((entity empty-words) (syntax html-syntax) pane)
  (declare (ignore pane))
  nil)

(defmethod display-parse-tree ((entity nonempty-words) (syntax html-syntax) pane)
  (with-slots (words word) entity
     (display-parse-tree words syntax pane)
     (display-parse-tree word syntax pane)))

(defmethod display-parse-tree ((entity html) (syntax html-syntax) pane)
  (with-slots (head body) entity
     (display-parse-tree head syntax pane)
     (display-parse-tree body syntax pane)))

(defmethod display-parse-tree ((entity head) (syntax html-syntax) pane)
  (with-slots (title) entity
     (display-parse-tree title syntax pane)))

(defgeneric display-parse-stack (symbol stack syntax pane))

(defmethod display-parse-stack (symbol stack (syntax html-syntax) pane)
  (let ((next (parse-stack-next stack)))
    (unless (null next)
      (display-parse-stack (parse-stack-symbol next) next syntax pane))
    (loop for parse-tree in (reverse (parse-stack-parse-trees stack))
	  do (display-parse-tree parse-tree syntax pane))))  

(defun display-parse-state (state syntax pane)
  (let ((top (parse-stack-top state)))
    (if (not (null top))
	(display-parse-stack (parse-stack-symbol top) top syntax pane)
	(display-parse-tree (target-parse-tree state) syntax pane))))

368 369 370 371 372 373 374 375 376 377
(defmethod redisplay-pane-with-syntax ((pane climacs-pane) (syntax html-syntax) current-p)
  (with-slots (top bot) pane
     (with-slots (tokens) syntax
	(let ((average-token-size (max (float (/ (size (buffer pane)) (nb-elements tokens)))
				       1.0)))
	  ;; find the last token before bot
	  (let ((end-token-index (max (floor (/ (offset bot) average-token-size)) 1)))
	    ;; go back to a token before bot
	    (loop until (mark<= (end-offset (element* tokens (1- end-token-index))) bot)
		  do (decf end-token-index))
378
	    ;; go forward to the last token before bot
379 380 381 382
	    (loop until (or (= end-token-index (nb-elements tokens))
			    (mark> (start-offset (element* tokens end-token-index)) bot))
		  do (incf end-token-index))
	    (let ((start-token-index end-token-index))
383 384 385 386 387
	      ;; go back to the first token after top, or until the previous token
	      ;; contains a valid parser state
	      (loop until (or (mark<= (end-offset (element* tokens (1- start-token-index))) top)
			      (not (null (parse-stack-top
					  (slot-value (element* tokens (1- start-token-index)) 'state)))))
388
		    do (decf start-token-index))
389 390 391 392 393
	      ;; display the parse tree if any
	      (unless (parse-state-empty-p (slot-value (element* tokens (1- start-token-index)) 'state))
		(display-parse-state (slot-value (element* tokens (1- start-token-index)) 'state)
				     syntax
				     pane))
394
	      ;; display the tokens
395
	      (loop with prev-offset = (end-offset (element* tokens (1- start-token-index)))
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
		    while (< start-token-index end-token-index)
		    do (let ((token (element* tokens start-token-index)))
			 (handle-whitespace pane (buffer pane) prev-offset (start-offset token))
			 (updating-output (pane :unique-id token 
						:id-test #'eq
						:cache-value token
						:cache-test #'eq)
			   (present (coerce (region-to-sequence (start-mark token)
								(end-offset token))
					    'string)
				    'string
				    :stream pane))
			 (setf prev-offset (end-offset token)))
		       (incf start-token-index))))))))