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

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

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

29
(defclass html-parse-tree (parse-tree)
30
  ((badness :initform 0 :initarg :badness :reader badness)))
31

32
(defmethod parse-tree-better ((t1 html-parse-tree) (t2 html-parse-tree))
33 34 35
  (and (eq (class-of t1) (class-of t2))
       (< (badness t1) (badness t2))))

36
(defclass html-nonterminal (html-parse-tree) ())
37

38
(defclass html-token (html-parse-tree)
39 40 41 42
  ((ink) (face)))

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

43 44 45 46
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; lexer

47
(defclass html-lexeme (html-token)
48 49
  ((state :initarg :state)))

50
(defclass start-lexeme (html-lexeme) ())
51 52
(defclass start-tag-start (html-lexeme) ())
(defclass end-tag-start (html-lexeme) ())
53 54 55
(defclass tag-end (html-lexeme) ())
(defclass word (html-lexeme) ())
(defclass delimiter (html-lexeme) ())
56

Robert Strandh's avatar
Robert Strandh committed
57 58
(defclass html-lexer (incremental-lexer) ())     

59
(defmethod next-lexeme ((lexer html-lexer) scan)
60 61 62
  (flet ((fo () (forward-object scan)))
    (let ((object (object-after scan)))
      (case object
63 64 65 66 67
	(#\< (fo) (cond ((or (end-of-buffer-p scan)
			     (not (eql (object-after scan) #\/)))
			 (make-instance 'start-tag-start))
			(t (fo)
			   (make-instance 'end-tag-start))))
68 69 70 71 72 73 74 75
	(#\> (fo) (make-instance 'tag-end))
	(t (cond ((alphanumericp object)
		  (loop until (end-of-buffer-p scan)
			while (alphanumericp (object-after scan))
			do (fo))
		  (make-instance 'word))
		 (t
		  (fo) (make-instance 'delimiter))))))))
76 77

(define-syntax html-syntax ("HTML" (basic-syntax))
78
  ((lexer :reader lexer)
79 80
   (valid-parse :initform 1)
   (parser)))
81 82 83 84 85

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

86
(defparameter *html-grammar* (grammar))
87

88 89 90
(defmacro add-html-rule (rule)
  `(add-rule (grammar-rule ,rule) *html-grammar*))

91
(defun word-is (word string)
92
  (string-equal (coerce (buffer-sequence (buffer word) (start-offset word) (end-offset word)) 'string)
93
		string))
94

95 96 97 98
(defmacro define-start-tag (name string)
  `(progn
     (defclass ,name (html-tag) ())

99
     (add-html-rule
100 101
      (,name -> (start-tag-start
		 (word (and (= (end-offset start-tag-start) (start-offset word))
102 103
			    (word-is word ,string)))
		 (tag-end (= (end-offset word) (start-offset tag-end))))))))
104 105 106 107 108

(defmacro define-end-tag (name string)
  `(progn
     (defclass ,name (html-tag) ())

109
     (add-html-rule
110 111
      (,name -> (end-tag-start
		 (word (and (= (end-offset end-tag-start) (start-offset word))
112 113
			    (word-is word ,string)))
		 (tag-end (= (end-offset word) (start-offset tag-end))))))))
114 115 116 117 118 119 120 121 122

(defmacro define-tag-pair (start-name end-name string)
  `(progn (define-start-tag ,start-name ,string)
	  (define-end-tag ,end-name ,string)))

(define-tag-pair <head> </head> "head")
(define-tag-pair <title> </title> "title")
(define-tag-pair <body> </body> "body")

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
(defmacro define-list (name item-name)
  (let ((empty-name (gensym))
	(nonempty-name (gensym)))
    `(progn
       (defclass ,name (html-nonterminal) ())
       (defclass ,empty-name (,name) ())
       
       (defclass ,nonempty-name (,name)
	 ((items :initarg :items)
	  (item :initarg :item)))
       
       (add-html-rule (,name -> ()
			     (make-instance ',empty-name)))
       
       (add-html-rule (,name -> (,name ,item-name)
			     (make-instance ',nonempty-name
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
				:items ,name :item ,item-name)))
       
       (defmethod display-parse-tree ((entity ,empty-name) (syntax html-syntax) pane)
	 (declare (ignore pane))
	 nil)
       
       (defmethod display-parse-tree ((entity ,nonempty-name) (syntax html-syntax) pane)
	 (with-slots (items item) entity
	   (display-parse-tree items syntax pane)
	   (display-parse-tree item syntax pane))))))

(defmacro define-nonempty-list (name item-name)
  (let ((empty-name (gensym))
	(nonempty-name (gensym)))
    `(progn
       (defclass ,name (html-nonterminal) ())
       (defclass ,empty-name (,name) ())
       
       (defclass ,nonempty-name (,name)
	 ((items :initarg :items)
	  (item :initarg :item)))
       
       (add-html-rule (,name -> (,item-name)
			     (make-instance ',nonempty-name
				:items (make-instance ',empty-name)
				:item ,item-name)))
       
       (add-html-rule (,name -> (,name ,item-name)
			     (make-instance ',nonempty-name
				:items ,name :item ,item-name)))
169 170 171 172 173 174 175 176 177
       
       (defmethod display-parse-tree ((entity ,empty-name) (syntax html-syntax) pane)
	 (declare (ignore pane))
	 nil)
       
       (defmethod display-parse-tree ((entity ,nonempty-name) (syntax html-syntax) pane)
	 (with-slots (items item) entity
	   (display-parse-tree items syntax pane)
	   (display-parse-tree item syntax pane))))))
178

179 180 181 182 183 184 185 186 187 188 189
;;;;;;;;;;;;;;; string

(defclass string-lexeme (html-lexeme) ())

(add-html-rule (string-lexeme -> ((html-lexeme (not (word-is html-lexeme "\""))))))

(defclass html-string (html-token)
  ((start :initarg :start)
   (lexemes :initarg :lexemes)
   (end :initarg :end)))

190
(define-list string-lexemes string-lexeme)
191 192 193 194 195 196 197 198 199 200 201 202 203

(add-html-rule (html-string -> ((start delimiter (word-is start "\""))
				string-lexemes
				(end delimiter (word-is end "\"")))
			    :start start :lexemes string-lexemes :end end))

(defmethod display-parse-tree ((entity html-string) (syntax html-syntax) pane)
  (with-slots (start lexemes end) entity
     (display-parse-tree start syntax pane)
     (with-text-face (pane :italic)
       (display-parse-tree lexemes syntax pane))
     (display-parse-tree end syntax pane)))

204 205 206 207 208 209 210 211 212 213 214
;;;;;;;;;;;;;;; attributes

(defclass html-attribute (html-nonterminal)
  ((name :initarg :name)
   (equals :initarg :equals)))

(defmethod display-parse-tree :before ((entity html-attribute) (syntax html-syntax) pane)
  (with-slots (name equals) entity
     (display-parse-tree name syntax pane)
     (display-parse-tree equals syntax pane)))

215 216 217 218 219 220 221
(defclass common-attribute (html-attribute) ())

(defclass core-attribute (common-attribute) ())
(defclass i18n-attribute (common-attribute) ())
(defclass scripting-event (common-attribute) ())

(define-list common-attributes common-attribute)
222

223 224
;;;;;;;;;;;;;;; lang attribute

225
(defclass lang-attr (i18n-attribute)
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
  ((lang :initarg :lang)))

(add-html-rule (lang-attr -> ((name word (word-is name "lang"))
			      (equals delimiter (and (= (end-offset name) (start-offset equals))
						     (word-is equals "=")))
			      (lang word (and (= (end-offset equals) (start-offset lang))
					      (= (- (end-offset lang) (start-offset lang))
						 2))))
			  :name name :equals equals :lang lang))

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

;;;;;;;;;;;;;;; dir attribute

242
(defclass dir-attr (i18n-attribute)
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
  ((dir :initarg :dir)))

(add-html-rule (dir-attr -> ((name word (word-is name "dir"))
			     (equals delimiter (and (= (end-offset name) (start-offset equals))
						    (word-is equals "=")))
			     (dir word (and (= (end-offset equals) (start-offset dir))
					    (or (word-is dir "rtl")
						(word-is dir "ltr")))))
			 :name name :equals equals :dir dir))

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


258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
;;;;;;;;;;;;;;; href attribute

(defclass href-attr (html-attribute)
  ((href :initarg :href)))

(add-html-rule (href-attr -> ((name word (word-is name "href"))
			      (equals delimiter (and (= (end-offset name) (start-offset equals))
						     (word-is equals "=")))
			      (href html-string))
			  :name name :equals equals :href href))

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


274
;;;;;;;;;;;;;;; title
275

276 277 278
(defclass title-item (html-nonterminal)
  ((item :initarg :item)))

279 280
(add-html-rule (title-item -> (word) :item word))
(add-html-rule (title-item -> (delimiter) :item delimiter))
281 282 283 284 285

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

286
(define-list title-items title-item)
287 288 289 290 291 292

(defclass title (html-nonterminal)
  ((<title> :initarg :<title>)
   (items :initarg :items)
   (</title> :initarg :</title>)))

293 294
(add-html-rule (title -> (<title> title-items </title>)
		      :<title> <title> :items title-items :</title> </title>))
295 296 297 298 299 300 301 302

(defmethod display-parse-tree ((entity title) (syntax html-syntax) pane)
  (with-slots (<title> items </title>) entity
     (display-parse-tree <title> syntax pane)
     (with-text-face (pane :bold)
       (display-parse-tree items syntax pane))
     (display-parse-tree </title> syntax pane)))

303 304 305 306 307
;;;;;;;;;;;;;;; inline-element, block-level-element

(defclass inline-element (html-nonterminal) ())
(defclass block-level-element (html-nonterminal) ())

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
;;;;;;;;;;;;;;; %inline

(defclass $inline (html-nonterminal)
  ((contents :initarg :contents)))
     
(add-html-rule ($inline -> (inline-element) :contents inline-element))
(add-html-rule ($inline -> (word) :contents word))
(add-html-rule ($inline -> (delimiter) :contents delimiter))

(defmethod display-parse-tree ((entity $inline) (syntax html-syntax) pane)
  (with-slots (contents) entity
     (display-parse-tree contents syntax pane)))

(define-list $inlines $inline)

;;;;;;;;;;;;;;; %flow
324

325
(defclass $flow (html-nonterminal)
Robert Strandh's avatar
Robert Strandh committed
326
  ((contents :initarg :contents)))
327
     
328 329
(add-html-rule ($flow -> ($inline) :contents $inline))
(add-html-rule ($flow -> (block-level-element) :contents block-level-element))
330

331
(defmethod display-parse-tree ((entity $flow) (syntax html-syntax) pane)
332 333 334
  (with-slots (contents) entity
     (display-parse-tree contents syntax pane)))

335
(define-list $flows $flow)
Robert Strandh's avatar
Robert Strandh committed
336 337 338 339 340 341 342 343 344 345 346

;;;;;;;;;;;;;;; headings

(defclass heading (block-level-element)
  ((start :initarg :start)
   (contents :initarg :contents)
   (end :initarg :end)))

(defmethod display-parse-tree ((entity heading) (syntax html-syntax) pane)
  (with-slots (start contents end) entity
     (display-parse-tree start syntax pane)
347 348
     (with-text-face (pane :bold)
       (display-parse-tree contents syntax pane))
Robert Strandh's avatar
Robert Strandh committed
349 350 351 352 353 354 355 356 357
     (display-parse-tree end syntax pane)))
	      
(defmacro define-heading (class-name tag-string start-tag-name end-tag-name)
  `(progn
     (define-tag-pair ,start-tag-name ,end-tag-name ,tag-string)

     (defclass ,class-name (heading) ())

     (add-html-rule
358 359
      (,class-name -> (,start-tag-name $inlines ,end-tag-name)
		   :start ,start-tag-name :contents $inlines :end ,end-tag-name))))
Robert Strandh's avatar
Robert Strandh committed
360 361 362 363 364 365 366 367 368


(define-heading h1 "h1" <h1> </h1>)
(define-heading h2 "h2" <h2> </h2>)
(define-heading h3 "h3" <h3> </h3>)
(define-heading h4 "h4" <h4> </h4>)
(define-heading h5 "h5" <h5> </h5>)
(define-heading h6 "h6" <h6> </h6>)

369
;;;;;;;;;;;;;;; a element
370

371 372
(defclass <a>-attribute (html-nonterminal)
  ((attribute :initarg :attribute)))
373

374
(add-html-rule (<a>-attribute -> (href-attr) :attribute href-attr))
375

376 377 378
(defmethod display-parse-tree ((entity <a>-attribute) (syntax html-syntax) pane)
  (with-slots (attribute) entity
     (display-parse-tree attribute syntax pane)))
379

380
(define-list <a>-attributes <a>-attribute)
381 382 383 384

(defclass <a> (html-tag)
  ((start :initarg :start)
   (name :initarg :name)
385
   (attributes :initarg :attributes)
386 387
   (end :initarg :end)))

388 389
(add-html-rule (<a> -> (start-tag-start
			(word (and (= (end-offset start-tag-start) (start-offset word))
390 391 392
				   (word-is word "a")))
			<a>-attributes
			tag-end)
393
		    :start start-tag-start :name word :attributes <a>-attributes :end tag-end))
394 395

(defmethod display-parse-tree ((entity <a>) (syntax html-syntax) pane)
396
  (with-slots (start name attributes end) entity
397 398
    (display-parse-tree start syntax pane)
    (display-parse-tree name syntax pane)
399
    (display-parse-tree attributes syntax pane)
400 401 402 403
    (display-parse-tree end syntax pane)))

(define-end-tag </a> "a")

404
(defclass a-element (inline-element)
405 406 407 408
  ((<a> :initarg :<a>)
   (items :initarg :items)
   (</a> :initarg :</a>)))

409 410
(add-html-rule (a-element -> (<a> $inlines </a>)
			  :<a> <a> :items $inlines :</a> </a>))
411

412
(defmethod display-parse-tree ((entity a-element) (syntax html-syntax) pane)
413 414
  (with-slots (<a> items </a>) entity
     (display-parse-tree <a> syntax pane)
415 416
     (with-text-face (pane :bold)
       (display-parse-tree items syntax pane))
417 418
     (display-parse-tree </a> syntax pane)))

419 420 421 422 423 424 425 426 427 428 429 430 431
;;;;;;;;;;;;;;; br element

(defclass br-element (inline-element)
  ((<br> :initarg :<br>)))
     
(define-start-tag <br> "br")

(add-html-rule (br-element -> (<br>) :<br> <br>))

(defmethod display-parse-tree ((entity br-element) (syntax html-syntax) pane)
  (with-slots (<br>) entity
     (display-parse-tree <br> syntax pane)))

432 433 434 435 436 437 438 439
;;;;;;;;;;;;;;; p element

(defclass <p> (html-tag)
  ((start :initarg :start)
   (name :initarg :name)
   (attributes :initarg :attributes)
   (end :initarg :end)))

440 441
(add-html-rule (<p> -> (start-tag-start
			(word (and (= (end-offset start-tag-start) (start-offset word))
442 443 444
				   (word-is word "p")))
			common-attributes
			tag-end)
445
		    :start start-tag-start :name word :attributes common-attributes :end tag-end))
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460

(defmethod display-parse-tree ((entity <p>) (syntax html-syntax) pane)
  (with-slots (start name attributes end) entity
    (display-parse-tree start syntax pane)
    (display-parse-tree name syntax pane)
    (display-parse-tree attributes syntax pane)
    (display-parse-tree end syntax pane)))

(define-end-tag </p> "p")

(defclass p-element (block-level-element)
  ((<p> :initarg :<p>)
   (contents :initarg :contents)
   (</p> :initarg :</p>)))

461 462
(add-html-rule (p-element -> (<p> $inlines </p>)
			  :<p> <p> :contents $inlines :</p> </p>))
463 464 465 466 467 468 469

(defmethod display-parse-tree ((entity p-element) (syntax html-syntax) pane)
  (with-slots (<p> contents </p>) entity
    (display-parse-tree <p> syntax pane)
    (display-parse-tree contents syntax pane)
    (display-parse-tree </p> syntax pane)))

470 471 472 473 474 475 476 477
;;;;;;;;;;;;;;; li element

(defclass <li> (html-tag)
  ((start :initarg :start)
   (name :initarg :name)
   (attributes :initarg :attributes)
   (end :initarg :end)))

478 479
(add-html-rule (<li> -> (start-tag-start
			 (word (and (= (end-offset start-tag-start) (start-offset word))
480 481 482
				    (word-is word "li")))
			 common-attributes
			 tag-end)
483
		     :start start-tag-start
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
		     :name word
		     :attributes common-attributes
		     :end tag-end))

(defmethod display-parse-tree ((entity <li>) (syntax html-syntax) pane)
  (with-slots (start name attributes end) entity
    (display-parse-tree start syntax pane)
    (display-parse-tree name syntax pane)
    (display-parse-tree attributes syntax pane)
    (display-parse-tree end syntax pane)))

(define-end-tag </li> "li")

(defclass li-element (html-nonterminal)
  ((<li> :initarg :<li>)
   (items :initarg :items)
   (</li> :initarg :</li>)))

502 503 504 505
(add-html-rule (li-element -> (<li> $flows </li>)
			   :<li> <li> :items $flows :</li> </li>))
(add-html-rule (li-element -> (<li> $flows)
			   :<li> <li> :items $flows :</li> nil))
506 507 508 509 510

(defmethod display-parse-tree ((entity li-element) (syntax html-syntax) pane)
  (with-slots (<li> items </li>) entity
     (display-parse-tree <li> syntax pane)
     (display-parse-tree items syntax pane)     
511 512
     (when </li>
       (display-parse-tree </li> syntax pane))))
513 514 515 516 517 518 519 520 521

;;;;;;;;;;;;;;; ul element

(defclass <ul> (html-tag)
  ((start :initarg :start)
   (name :initarg :name)
   (attributes :initarg :attributes)
   (end :initarg :end)))

522 523
(add-html-rule (<ul> -> (start-tag-start
			 (word (and (= (end-offset start-tag-start) (start-offset word))
524 525 526
				    (word-is word "ul")))
			 common-attributes
			 tag-end)
527
		     :start start-tag-start
528 529 530 531 532 533 534 535 536 537 538 539 540
		     :name word
		     :attributes common-attributes
		     :end tag-end))

(defmethod display-parse-tree ((entity <ul>) (syntax html-syntax) pane)
  (with-slots (start name attributes end) entity
    (display-parse-tree start syntax pane)
    (display-parse-tree name syntax pane)
    (display-parse-tree attributes syntax pane)
    (display-parse-tree end syntax pane)))

(define-end-tag </ul> "ul")

541
(define-nonempty-list li-elements li-element)
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556

(defclass ul-element (block-level-element)
  ((<ul> :initarg :<ul>)
   (items :initarg :items)
   (</ul> :initarg :</ul>)))

(add-html-rule (ul-element -> (<ul> li-elements </ul>)
			   :<ul> <ul> :items li-elements :</ul> </ul>))

(defmethod display-parse-tree ((entity ul-element) (syntax html-syntax) pane)
  (with-slots (<ul> items </ul>) entity
     (display-parse-tree <ul> syntax pane)
     (display-parse-tree items syntax pane)     
     (display-parse-tree </ul> syntax pane)))

557 558 559 560 561 562 563 564 565 566 567 568 569
;;;;;;;;;;;;;;; hr element

(defclass hr-element (block-level-element)
  ((<hr> :initarg :<hr>)))

(define-start-tag <hr> "hr")

(add-html-rule (hr-element -> (<hr>) :<hr> <hr>))

(defmethod display-parse-tree ((entity hr-element) (syntax html-syntax) pane)
  (with-slots (<hr>) entity
     (display-parse-tree <hr> syntax pane)))

570
;;;;;;;;;;;;;;; body element
Robert Strandh's avatar
Robert Strandh committed
571 572 573 574 575 576 577 578 579 580

(defclass body-item (html-nonterminal)
  ((item :initarg :item)))

(add-html-rule (body-item -> ((element block-level-element)) :item element))

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

581
(define-list body-items body-item)
Robert Strandh's avatar
Robert Strandh committed
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596

(defclass body (html-nonterminal)
  ((<body> :initarg :<body>)
   (items :initarg :items)
   (</body> :initarg :</body>)))

(add-html-rule (body -> (<body> body-items </body>)
		     :<body> <body> :items body-items :</body> </body>))

(defmethod display-parse-tree ((entity body) (syntax html-syntax) pane)
  (with-slots (<body> items </body>) entity
     (display-parse-tree <body> syntax pane)
     (display-parse-tree items syntax pane)     
     (display-parse-tree </body> syntax pane)))

597 598 599 600 601 602 603
;;;;;;;;;;;;;;; head

(defclass head (html-nonterminal)
  ((<head> :initarg :<head>)
   (title :initarg :title)
   (</head> :initarg :</head>)))

604 605
(add-html-rule (head -> (<head> title </head>)
		     :<head> <head> :title title :</head> </head>))
606

607 608 609 610 611 612 613 614
(defmethod display-parse-tree ((entity head) (syntax html-syntax) pane)
  (with-slots (<head> title </head>) entity
     (display-parse-tree <head> syntax pane)
     (display-parse-tree title syntax pane)     
     (display-parse-tree </head> syntax pane)))

;;;;;;;;;;;;;;; html

615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
(defclass <html>-attribute (html-nonterminal)
  ((attribute :initarg :attribute)))

(defmethod display-parse-tree ((entity <html>-attribute) (syntax html-syntax) pane)
  (with-slots (attribute) entity
     (display-parse-tree attribute syntax pane)))

(add-html-rule (<html>-attribute -> (lang-attr) :attribute lang-attr))
(add-html-rule (<html>-attribute -> (dir-attr) :attribute dir-attr))

(define-list <html>-attributes <html>-attribute)

(defclass <html> (html-tag)
  ((start :initarg :start)
   (name :initarg :name)
   (attributes :initarg :attributes)
   (end :initarg :end)))

633 634
(add-html-rule (<html> -> (start-tag-start
			   (word (and (= (end-offset start-tag-start) (start-offset word))
635 636 637
				      (word-is word "html")))
			   <html>-attributes
			   tag-end)
638
		       :start start-tag-start :name word :attributes <html>-attributes :end tag-end))
639 640 641 642 643 644 645 646 647 648

(defmethod display-parse-tree ((entity <html>) (syntax html-syntax) pane)
  (with-slots (start name attributes end) entity
    (display-parse-tree start syntax pane)
    (display-parse-tree name syntax pane)
    (display-parse-tree attributes syntax pane)
    (display-parse-tree end syntax pane)))

(define-end-tag </html> "html")

649 650 651 652 653 654
(defclass html (html-nonterminal)
  ((<html> :initarg :<html>)
   (head :initarg :head)
   (body :initarg :body)
   (</html> :initarg :</html>)))

655 656
(add-html-rule (html -> (<html> head body </html>)
		     :<html> <html> :head head :body body :</html> </html>))
657

658 659 660 661 662 663 664 665 666
(defmethod display-parse-tree ((entity html) (syntax html-syntax) pane)
  (with-slots (<html> head body </html>) entity
     (display-parse-tree <html> syntax pane)
     (display-parse-tree head syntax pane)     
     (display-parse-tree body syntax pane)     
     (display-parse-tree </html> syntax pane)))

;;;;;;;;;;;;;;;

667 668
(defmethod initialize-instance :after ((syntax html-syntax) &rest args)
  (declare (ignore args))
669
  (with-slots (parser lexer buffer) syntax
670 671 672
     (setf parser (make-instance 'parser
		     :grammar *html-grammar*
		     :target 'html))
673
     (setf lexer (make-instance 'html-lexer :buffer (buffer syntax)))
674 675
     (let ((m (clone-mark (low-mark buffer) :left))
	   (lexeme (make-instance 'start-lexeme :state (initial-state parser))))
676
       (setf (offset m) 0)
677 678 679
       (setf (start-offset lexeme) m
	     (end-offset lexeme) 0)
       (insert-lexeme lexer 0 lexeme))))
680

681 682 683 684
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; update syntax

685

686
(defmethod update-syntax-for-display (buffer (syntax html-syntax) top bot)
687 688 689 690 691
  (with-slots (parser lexer valid-parse) syntax
     (loop until (= valid-parse (nb-lexemes lexer))
	   while (mark<= (end-offset (lexeme lexer valid-parse)) bot)
	   do (let ((current-token (lexeme lexer (1- valid-parse)))
		    (next-lexeme (lexeme lexer valid-parse)))
692 693
		(setf (slot-value next-lexeme 'state)
		      (advance-parse parser (list next-lexeme) (slot-value current-token 'state))))
694
	      (incf valid-parse))))
695

696
(defmethod inter-lexeme-object-p ((lexer html-lexer) object)
697 698
  (whitespacep object))

699
(defmethod update-syntax (buffer (syntax html-syntax))
700
  (with-slots (lexer valid-parse) syntax
701
     (let* ((low-mark (low-mark buffer))
702 703 704 705 706
	    (high-mark (high-mark buffer)))
       (when (mark<= low-mark high-mark)
	 (let ((first-invalid-position (delete-invalid-lexemes lexer low-mark high-mark)))
	   (setf valid-parse first-invalid-position)
	   (update-lex lexer first-invalid-position high-mark))))))
707

708 709 710 711
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; display

712 713 714 715 716
(defvar *white-space-start* nil)

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

717 718 719 720 721
(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)
722 723 724
	       (#\Newline (terpri pane)
			  (setf (aref *cursor-positions* (incf *current-line*))
				(multiple-value-bind (x y) (stream-cursor-position pane)
725
				  (declare (ignore x))
726
				  y)))
727 728 729 730 731 732 733
	       (#\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))))		    

734
(defmethod display-parse-tree :around ((entity html-parse-tree) syntax pane)
735
  (with-slots (top bot) pane
736
     (when (and (end-offset entity) (mark> (end-offset entity) top))
737 738 739
       (call-next-method))))

(defmethod display-parse-tree ((entity html-token) (syntax html-syntax) pane)
740 741 742 743 744 745 746 747 748 749 750 751 752
  (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))))
753 754 755
	 (present (coerce (buffer-sequence (buffer syntax)
					   (start-offset entity)
					   (end-offset entity))
756 757 758 759 760
			  'string)
		  'string
		  :stream pane)))))

(defmethod display-parse-tree :around ((entity html-tag) (syntax html-syntax) pane)
761
  (with-drawing-options (pane :ink +green4+)
762 763 764 765 766
    (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)))
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782

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

783 784
(defmethod redisplay-pane-with-syntax ((pane climacs-pane) (syntax html-syntax) current-p)
  (with-slots (top bot) pane
785 786 787
     (setf *cursor-positions* (make-array (1+ (number-of-lines-in-region top bot)))
	   *current-line* 0
	   (aref *cursor-positions* 0) (stream-cursor-position pane))
788 789
     (with-slots (lexer) syntax
	(let ((average-token-size (max (float (/ (size (buffer pane)) (nb-lexemes lexer)))
790 791 792 793
				       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
794
	    (loop until (mark<= (end-offset (lexeme lexer (1- end-token-index))) bot)
795
		  do (decf end-token-index))
796
	    ;; go forward to the last token before bot
797 798
	    (loop until (or (= end-token-index (nb-lexemes lexer))
			    (mark> (start-offset (lexeme lexer end-token-index)) bot))
799 800
		  do (incf end-token-index))
	    (let ((start-token-index end-token-index))
801 802
	      ;; go back to the first token after top, or until the previous token
	      ;; contains a valid parser state
803
	      (loop until (or (mark<= (end-offset (lexeme lexer (1- start-token-index))) top)
804
			      (not (parse-state-empty-p 
805
				    (slot-value (lexeme lexer (1- start-token-index)) 'state))))
806
		    do (decf start-token-index))
807 808
	      (let ((*white-space-start* (offset top)))
		;; display the parse tree if any
809 810
		(unless (parse-state-empty-p (slot-value (lexeme lexer (1- start-token-index)) 'state))
		  (display-parse-state (slot-value (lexeme lexer (1- start-token-index)) 'state)
811 812
				       syntax
				       pane))
813
		;; display the lexemes
814 815
		(with-drawing-options (pane :ink +red+)
		  (loop while (< start-token-index end-token-index)
816
			do (let ((token (lexeme lexer start-token-index)))
817 818 819 820 821 822 823 824 825 826 827 828
			     (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+))))))
829