html-syntax.lisp 18.4 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 37 38 39 40 41 42 43
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; this should really go in syntax.lisp

(defclass parse-tree ()
  ((start-mark :initarg :start-mark :reader start-mark)
   (size :initarg :size)))

(defgeneric start-offset (parse-tree))

(defmethod start-offset ((tree parse-tree))
  (offset (start-mark tree)))

(defgeneric end-offset (parse-tree))

(defmethod end-offset ((tree parse-tree))
  (with-slots (start-mark size) tree
     (+ (offset start-mark) size)))

44 45 46 47
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; grammar classes

48
(defclass html-sym (parse-tree)
49 50 51 52 53 54 55
  ((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))))

56
(defclass html-nonterminal (html-sym) ())
57 58

(defclass words (html-nonterminal) ())
59 60 61 62 63 64 65

(defclass empty-words (words) ())

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

66
(defclass html-balanced (html-nonterminal)
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  ((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) ())
85
(defclass a (html-words) ())
86 87 88
(defclass para (html-words) ())

(defclass html-token (html-sym)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  ((ink) (face)))

(defclass html-tag (html-token) ())

(defclass <html> (html-tag) () (:default-initargs :size 6))
(defclass </html> (html-tag) ()(:default-initargs :size 7))
(defclass <head> (html-tag) () (:default-initargs :size 6))
(defclass </head> (html-tag) () (:default-initargs :size 7))
(defclass <title> (html-tag) () (:default-initargs :size 7))
(defclass </title> (html-tag) () (:default-initargs :size 8))
(defclass <body> (html-tag) () (:default-initargs :size 6))
(defclass </body> (html-tag) () (:default-initargs :size 7))
(defclass <h1> (html-tag) () (:default-initargs :size 4))
(defclass </h1> (html-tag) () (:default-initargs :size 5))
(defclass <h2> (html-tag) () (:default-initargs :size 4))
(defclass </h2> (html-tag) () (:default-initargs :size 5))
(defclass <h3> (html-tag) () (:default-initargs :size 4))
(defclass </h3> (html-tag) () (:default-initargs :size 5))
(defclass <p> (html-tag) () (:default-initargs :size 3))
(defclass </p> (html-tag) () (:default-initargs :size 4))
(defclass <ul> (html-tag) () (:default-initargs :size 4))
(defclass </ul> (html-tag) () (:default-initargs :size 5))
(defclass <li> (html-tag) () (:default-initargs :size 4))
(defclass </li> (html-tag) () (:default-initargs :size 5))
113 114 115 116 117 118 119
(defclass <a> (html-tag)
  ((start :initarg :start)
   (word :initarg :word)
   (words :initarg :words)
   (end :initarg :end)))
(defclass </a> (html-tag) () (:default-initargs :size 4))

120 121 122 123 124

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

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
(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)))
161 162 163 164 165

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

166 167 168 169
(defun word-is (word string)
  (string-equal (coerce (region-to-sequence (start-mark word) (end-offset word)) 'string)
		string))

170 171
(defparameter *html-grammar*
  (grammar
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 209 210 211 212 213 214 215
    (<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))
216 217 218 219 220 221 222 223 224 225 226 227 228 229
    (<a> -> (tag-start
	     (word (and (= (end-offset tag-start) (start-offset word))
			(word-is word "a")))
	     words
	     tag-end)
	 :start-mark (start-mark tag-start)
	 :size (- (end-offset tag-end) (start-offset tag-start))
	 :start tag-start :word word :words words :end tag-end)
    (</a> -> (tag-start
	      (slash (= (end-offset tag-start) (start-offset slash)))
	      (word (and (= (end-offset slash) (start-offset word))
			 (word-is word "a")))
	      (tag-end (= (end-offset word) (start-offset tag-end))))
	  :start-mark (start-mark tag-start))
230
    (html -> (<html> head body </html>)
231 232
	  :start-mark (start-mark <html>)
	  :size (- (end-offset </html>) (start-offset <html>))
233 234
	  :start <html> :head head :body body :end </html>)
    (head -> (<head> title </head>)
235 236
	  :start-mark (start-mark <head>)
	  :size (- (end-offset </head>) (start-offset <head>))
237 238
	  :start <head> :title title :end </head>)
    (title -> (<title> words </title>)
239 240
	   :start-mark (start-mark <title>)
	   :size (- (end-offset </title>) (start-offset <title>))
241 242
	   :start <title> :words words :end </title>)
    (body -> (<body> words </body>)
243 244
	  :start-mark (start-mark <body>)
	  :size (- (end-offset </body>) (start-offset <body>))
245
	  :start <body> :words words :end </body>)
246 247 248 249
    (a -> (<a> words </a>)
       :start-mark (start-mark <a>)
       :size (- (end-offset </a>) (start-offset <a>))
       :start <a> :words words :end </a>)
250
    (words -> ()
251
	   (make-instance 'empty-words :start-mark nil))
252
    (words -> (words word)
253
	   (make-instance 'nonempty-words
254 255
	      :start-mark (or (start-mark words) (start-mark word))
	      :size (- (end-offset word) (offset (or (start-mark words) (start-mark word))))
256 257 258 259 260 261 262 263
	      :words words :word word))
    (word -> (a)
	  :start-mark (start-mark a)
	  :size (- (end-offset a) (start-offset a)))
    (word -> (delimiter)
	  :start-mark (start-mark delimiter)
	  :size (- (end-offset delimiter) (start-offset delimiter)))))
	  
264 265 266

(defmethod initialize-instance :after ((syntax html-syntax) &rest args)
  (declare (ignore args))
267
  (with-slots (parser tokens buffer) syntax
268 269 270
     (setf parser (make-instance 'parser
		     :grammar *html-grammar*
		     :target 'html))
271 272 273 274 275 276 277
     (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)))))

278 279 280 281
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; update syntax

282 283 284
(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))
285
	   while (mark<= (end-offset (element* tokens valid-parse)) bot)
286 287 288 289 290
	   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))))
291 292

(defmethod update-syntax (buffer (syntax html-syntax))
293 294 295 296 297 298 299 300 301 302 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
  (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))))))

330 331 332 333
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; display

334 335 336 337 338
(defvar *white-space-start* nil)

(defvar *cursor-positions* nil)
(defvar *current-line* 0)

339 340 341 342 343
(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)
344 345 346
	       (#\Newline (terpri pane)
			  (setf (aref *cursor-positions* (incf *current-line*))
				(multiple-value-bind (x y) (stream-cursor-position pane)
347
				  (declare (ignore x))
348
				  y)))
349 350 351 352 353 354 355
	       (#\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))))		    

356 357 358 359 360 361 362 363 364 365
(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)
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
  (flet ((cache-test (t1 t2)
	   (and (eq t1 t2)
		(eq (slot-value t1 'ink)
		    (medium-ink (sheet-medium pane)))
		(eq (slot-value t1 'face)
		    (text-style-face (medium-text-style (sheet-medium pane)))))))
    (updating-output (pane :unique-id entity
			   :id-test #'eq
			   :cache-value entity
			   :cache-test #'cache-test)
      (with-slots (ink face) entity
	 (setf ink (medium-ink (sheet-medium pane))
	       face (text-style-face (medium-text-style (sheet-medium pane))))
	 (present (coerce (region-to-sequence (start-mark entity)
					      (end-offset entity))
			  'string)
		  'string
		  :stream pane)))))

(defmethod display-parse-tree :around ((entity html-tag) (syntax html-syntax) pane)
  (with-drawing-options (pane :ink +green+)
    (call-next-method)))

(defmethod display-parse-tree :before ((entity html-token) (syntax html-syntax) pane)
  (handle-whitespace pane (buffer pane) *white-space-start* (start-offset entity))
  (setf *white-space-start* (end-offset entity)))
392 393 394 395 396 397 398 399 400

(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)))

401 402 403 404
(defmethod display-parse-tree :around ((entity title) (syntax html-syntax) pane)
  (with-text-face (pane :bold)
    (call-next-method)))

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
(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)))

427 428 429 430 431 432 433
(defmethod display-parse-tree ((entity <a>) (syntax html-syntax) pane)
  (with-slots (start word words end) entity
    (display-parse-tree start syntax pane)
    (display-parse-tree word syntax pane)
    (display-parse-tree words syntax pane)
    (display-parse-tree end syntax pane)))

434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
(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))))

449 450
(defmethod redisplay-pane-with-syntax ((pane climacs-pane) (syntax html-syntax) current-p)
  (with-slots (top bot) pane
451 452 453
     (setf *cursor-positions* (make-array (1+ (number-of-lines-in-region top bot)))
	   *current-line* 0
	   (aref *cursor-positions* 0) (stream-cursor-position pane))
454 455 456 457 458 459 460 461
     (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))
462
	    ;; go forward to the last token before bot
463 464 465 466
	    (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))
467 468 469
	      ;; 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)
470 471
			      (not (parse-state-empty-p 
				    (slot-value (element* tokens (1- start-token-index)) 'state))))
472
		    do (decf start-token-index))
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
	      (let ((*white-space-start* (offset top)))
		;; 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))
		;; display the tokens
		(with-drawing-options (pane :ink +red+)
		  (loop while (< start-token-index end-token-index)
			do (let ((token (element* tokens start-token-index)))
			     (display-parse-tree token syntax pane))
			   (incf start-token-index))))))))
     (let* ((cursor-line (number-of-lines-in-region top (point pane)))
	    (height (text-style-height (medium-text-style pane) pane))
	    (cursor-y (+ (* cursor-line (+ height (stream-vertical-spacing pane)))))
	    (cursor-column (column-number (point pane)))
	    (cursor-x (* cursor-column (text-style-width (medium-text-style pane) pane))))
       (updating-output (pane :unique-id -1)
	 (draw-rectangle* pane
			  (1- cursor-x) (- cursor-y (* 0.2 height))
			  (+ cursor-x 2) (+ cursor-y (* 0.8 height))
			  :ink (if current-p +red+ +blue+))))))
495