prolog-syntax.lisp 43.9 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 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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 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 286 287 288 289 290 291 292 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 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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
;;; -*- Mode: Lisp; Package: CLIMACS-HTML-SYNTAX -*-

;;;  (c) copyright 2005 by
;;;           Christophe Rhodes (c.rhodes@gold.ac.uk)
;;;           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 ISO Prolog

(in-package "CLIMACS-PROLOG-SYNTAX")

(defclass prolog-parse-tree (parse-tree)
  ())

(define-syntax prolog-syntax ("Prolog" (basic-syntax))
  ((lexer :reader lexer)
   (valid-parse :initform 1)
   (parser)))

(defparameter *prolog-grammar* (grammar))

(defmethod initialize-instance :after ((syntax prolog-syntax) &rest args)
  (declare (ignore args))
  (with-slots (parser lexer buffer) syntax
     (setf parser (make-instance 'parser
		     :grammar *prolog-grammar*
		     :target 'prolog-text))
     (setf lexer (make-instance 'prolog-lexer :buffer (buffer syntax)))
     (let ((m (clone-mark (low-mark buffer) :left))
	   (lexeme (make-instance 'start-lexeme :state (initial-state parser))))
       (setf (offset m) 0)
       (setf (start-offset lexeme) m
	     (end-offset lexeme) 0)
       (insert-lexeme lexer 0 lexeme))))

;;; grammar

(defclass prolog-nonterminal (prolog-parse-tree)
  ())

(defclass prolog-token (prolog-parse-tree)
  ())

(defclass prolog-operator (prolog-token)
  ())

;;; lexer

(defclass prolog-lexeme (prolog-token)
  ((state :initarg :state)))

(defclass start-lexeme (prolog-lexeme) ())

(defgeneric display-parse-tree (entity syntax pane))

(defclass layout-text (prolog-nonterminal)
  ((comment :initarg :comment :accessor comment :initform nil)
   (cont :initarg :cont :accessor cont)))
(defmethod display-parse-tree
    ((entity layout-text) (syntax prolog-syntax) pane)
  (when (comment entity)
    (with-drawing-options (pane :ink (make-rgb-color 0.7 0.0 0.0))
      (display-parse-tree (comment entity) syntax pane)))
  (when (cont entity)
    (display-parse-tree (cont entity) syntax pane)))

(defgeneric syntactic-lexeme (thing))
(defmethod syntactic-lexeme ((lexeme prolog-lexeme))
  lexeme)
(macrolet ((def ((name &optional tokenp) &rest subs)
	     (flet ((f (x) (intern (format nil "~A-LEXEME" x))))
	       `(progn
		  (defclass ,(f name) (prolog-lexeme) ())

                  ,@(when tokenp
                      `((defclass ,name (prolog-nonterminal)
                          ((layout-text :initarg :layout-text :accessor layout-text :initform nil)
                           (syntactic-lexeme :initarg :syntactic-lexeme :accessor syntactic-lexeme)))
                        (defmethod display-parse-tree
                            ((entity ,name) (syntax prolog-syntax) pane)
                          (when (layout-text entity)
                            (display-parse-tree
                             (layout-text entity) syntax pane))
                          (display-parse-tree
                           (syntactic-lexeme entity) syntax pane))
                        (add-rule (grammar-rule (,name -> (,(f name))
                                                       (make-instance ',name
                                                                      :syntactic-lexeme ,(f name))))
                         *prolog-grammar*)
                        (add-rule (grammar-rule (,name -> (layout-text ,(f name))
                                                       (make-instance ',name
                                                                      :layout-text layout-text
                                                                      :syntactic-lexeme ,(f name))))
                         *prolog-grammar*)))
		  ,@(loop for sub in subs collect
                          `(defclass ,(f sub) (,(f name)) ()))))))
  (def (comment) single-line-comment bracketed-comment)
  
  (def (name t) identifier graphic quoted semicolon cut)
  (def (variable t) anonymous named)
  (def (integer t))
  (def (float-number t))
  (def (char-code-list t))
  (def (open-ct))
  (def (open t))
  (def (close t))
  (def (open-list t))
  (def (close-list t))
  (def (open-curly t))
  (def (close-curly t))
  (def (head-tail-separator t))
  (def (comma t))
  (def (end t))
  
  (def (error)))

(add-rule (grammar-rule (layout-text -> (comment-lexeme layout-text)
                                     (make-instance 'layout-text
                                                    :comment comment-lexeme
                                                    :cont layout-text)))
          *prolog-grammar*)
(add-rule (grammar-rule (layout-text -> ()
                                     (make-instance 'layout-text
                                                    :cont nil)))
          *prolog-grammar*)

(defclass prolog-lexer (incremental-lexer) ())

(defmethod next-lexeme ((lexer prolog-lexer) scan)
  (let ((string (make-array 0 :element-type 'character
			    :fill-pointer 0 :adjustable t)))
    (flet ((fo ()
	     (vector-push-extend (object-after scan) string)
	     (forward-object scan))
           #+nil ; we might need this later for float-number tokens
	   (bo ()
	     (vector-pop string)
	     (backward-object scan)))
      (let ((object (object-after scan)))
	(block nil
	  (tagbody
	   START
	     (cond
	       ((lower-case-p object) (fo) (go IDENTIFIER))
               ((eql object #\/) (fo) (go COMMENT-OR-GRAPHIC))
               ((eql object #\%) (fo) (go LINE-COMMENT))
	       ((position object "#$&*+-./:<=>?@^~\\") (fo) (go GRAPHIC-TOKEN))
	       ((eql object #\') (fo) (go QUOTED-TOKEN))
	       ((eql object #\;)
		(fo) (return (make-instance 'semicolon-lexeme)))
	       ((eql object #\!)
		(fo) (return (make-instance 'cut-lexeme)))
	       ((eql object #\_) (fo) (go VARIABLE))
	       ((upper-case-p object) (fo) (go NAMED-VARIABLE))
	       ((digit-char-p object) (fo) (go NUMBER))
	       ((eql object #\") (fo) (go CHAR-CODE-LIST))
	       ((eql object #\()
		(if (or (beginning-of-buffer-p scan)
			(not (member (object-before scan) '(#\Space #\Newline))))
		    (progn (fo) (return (make-instance 'open-ct-lexeme)))
		    (progn (fo) (return (make-instance 'open-lexeme)))))
	       ((eql object #\)) (fo) (return (make-instance 'close-lexeme)))
	       ((eql object #\[) (fo) (return (make-instance 'open-list-lexeme)))
	       ((eql object #\]) (fo) (return (make-instance 'close-list-lexeme)))
	       ((eql object #\{) (fo) (return (make-instance 'open-curly-lexeme)))
	       ((eql object #\}) (fo) (return (make-instance 'close-curly-lexeme)))
	       ((eql object #\|)
		(fo) (return (make-instance 'head-tail-separator-lexeme)))
	       ((eql object #\,) (fo) (return (make-instance 'comma-lexeme)))
	       ((eql object #\.) (error "shouldn't get here"))
	       (t (fo) (return (make-instance 'error-lexeme))))
	   IDENTIFIER
	     (loop until (end-of-buffer-p scan)
		while (alphanumericp (object-after scan))
		do (fo))
	     (return (make-instance 'identifier-lexeme))
           LINE-COMMENT
             (loop until (end-of-buffer-p scan)
                   until (eql (object-after scan) #\Newline)
                   do (fo))
             (if (end-of-buffer-p scan)
                 (return (make-instance 'error-lexeme))
                 (return (make-instance 'single-line-comment-lexeme)))
           COMMENT-OR-GRAPHIC
             (if (end-of-buffer-p scan)
                 (return (make-instance 'graphic-lexeme))
                 (let ((object (object-after scan)))
                   (cond
                     ((eql object #\*) (fo) (go COMMENT))
                     ((not (position object "#$&*+-./:<=>?@^~\\"))
                      (return (make-instance 'graphic-lexeme)))
                     (t (fo) (go GRAPHIC-TOKEN)))))
           COMMENT
             (cond
               ((end-of-buffer-p scan)
                (return (make-instance 'error-lexeme)))
               ((eql (object-after scan) #\*)
                (fo)
                (cond
                  ((end-of-buffer-p scan)
                   (return (make-instance 'error-lexeme)))
                  ((eql (object-after scan) #\/)
                   (fo)
                   (return (make-instance 'bracketed-comment-lexeme)))
                  (t (fo) (go COMMENT))))
               (t (fo) (go COMMENT)))
           GRAPHIC-TOKEN
	     (loop until (end-of-buffer-p scan)
		while (position (object-after scan) "#$&*+-./:<=>?@^~\\")
		do (fo))
	     (cond
	       ((end-of-buffer-p scan)
		(cond
		  ((string= string ".")
		   (return (make-instance 'end-lexeme)))
		  (t (return (make-instance 'graphic-lexeme)))))
	       (t
		(cond
		  ((and (string= string ".") (whitespacep (object-after scan)))
		   (return (make-instance 'end-lexeme)))
		  (t (return (make-instance 'graphic-lexeme))))))
	   QUOTED-TOKEN
	     (loop until (end-of-buffer-p scan)
		;; FIXME
		until (eql (object-after scan) #\')
		do (fo))
	     (if (end-of-buffer-p scan)
		 (return (make-instance 'error-lexeme))
                 (progn (fo)
                        (return (make-instance 'quoted-lexeme))))
	   VARIABLE
	     (if (or (end-of-buffer-p scan)
		     (not (alphanumericp (object-after scan))))
		 (return (make-instance 'anonymous-lexeme))
		 (go NAMED-VARIABLE))
	   NAMED-VARIABLE
	     (loop until (end-of-buffer-p scan)
		while (alphanumericp (object-after scan))
		do (fo))
	     (return (make-instance 'named-lexeme))
	   NUMBER
	     (loop until (end-of-buffer-p scan)
		while (digit-char-p (object-after scan))
		do (fo))
	     (return (make-instance 'integer-lexeme))
	   CHAR-CODE-LIST
	     (loop until (end-of-buffer-p scan)
		;; FIXME
		until (eql (object-after scan) #\")
		do (fo))
	     (if (end-of-buffer-p scan)
		 (return (make-instance 'error-lexeme))
		 (return (make-instance 'char-code-list-lexeme)))))))))

;;; parser

(defclass prolog-text (prolog-nonterminal)
  ())
(defclass empty-prolog-text (prolog-text)
  ())
(defclass clause-prolog-text (prolog-text)
  ((clause :initarg :clause :accessor clause)
   (text-rest :initarg :text-rest :accessor text-rest)))
(defclass directive-prolog-text (prolog-text)
  ((directive :initarg :directive :accessor directive)
   (text-rest :initarg :text-rest :accessor text-rest)))

(defmethod display-parse-tree
    ((entity empty-prolog-text) (syntax prolog-syntax) pane)
  (declare (ignore pane))
  nil)
(defmethod display-parse-tree
    ((entity clause-prolog-text) (syntax prolog-syntax) pane)
  (display-parse-tree (clause entity) syntax pane)
  (display-parse-tree (text-rest entity) syntax pane))
(defmethod display-parse-tree
    ((entity directive-prolog-text) (syntax prolog-syntax) pane)
  (with-text-face (pane :italic)
    (display-parse-tree (directive entity) syntax pane))
  (display-parse-tree (text-rest entity) syntax pane))

(defclass directive (prolog-nonterminal)
  ((directive-term :initarg :directive-term :accessor directive-term)
   (end :initarg :end :accessor end)))
(defclass directive-term (prolog-nonterminal)
  ((term :initarg :term :accessor term)))
(defclass clause (prolog-nonterminal)
  ((clause-term :initarg :clause-term :accessor clause-term)
   (end :initarg :end :accessor end)))
(defclass clause-term (prolog-nonterminal)
  ((term :initarg :term :accessor term)))

(defmethod display-parse-tree ((entity directive) (syntax prolog-syntax) pane)
  (with-text-face (pane :italic)
    (display-parse-tree (directive-term entity) syntax pane))
  (display-parse-tree (end entity) syntax pane))
(defmethod display-parse-tree
    ((entity directive-term) (syntax prolog-syntax) pane)
  (display-parse-tree (term entity) syntax pane))
(defmethod display-parse-tree ((entity clause) (syntax prolog-syntax) pane)
  (display-parse-tree (clause-term entity) syntax pane)
  (display-parse-tree (end entity) syntax pane))
(defmethod display-parse-tree
    ((entity clause-term) (syntax prolog-syntax) pane)
  (display-parse-tree (term entity) syntax pane))

(defgeneric functor (term))
(defgeneric arity (term))
(defclass term (prolog-nonterminal)
  ((priority :initarg :priority :accessor priority)))
(defclass constant-term (term)
  ((value :initarg :value :accessor value)))
(defclass variable-term (term)
  ((name :initarg :name :accessor name)))
(defclass compound-term (term)
  ())
(defgeneric compound-term-p (term))
(defmethod compound-term-p ((term term))
  nil)
(defmethod compound-term-p ((c compound-term))
  t)
(defclass functional-compound-term (compound-term)
  ((functor :initarg :functor :accessor functor)
   (open-ct :initarg :open-ct :accessor open-ct)
   (arg-list :initarg :arg-list :accessor arg-list)
   (close :initarg :close :accessor close)))
(defclass bracketed-term (term)
  ((open :initarg :open :accessor open)
   (term :initarg :term :accessor term)
   (close :initarg :close :accessor close)))
(defclass operator-compound-term (compound-term)
  ((operator :initarg :operator :accessor operator)))
(defmethod functor ((o operator-compound-term))
  (operator o))
(defclass binary-operator-compound-term (operator-compound-term)
  ((left :initarg :left :accessor left)
   (right :initarg :right :accessor right)))
(defmethod arity ((b binary-operator-compound-term))
  2)
(defclass prefix-operator-compound-term (operator-compound-term)
  ((right :initarg :right :accessor right)))
(defmethod arity ((p prefix-operator-compound-term))
  1)
(defclass postfix-operator-compound-term (operator-compound-term)
  ((left :initarg :left :accessor left)))
(defmethod arity ((p postfix-operator-compound-term))
  1)
(defclass list-compound-term (compound-term)
  (([ :initarg :[ :accessor [)
   (items :initarg :items :accessor items)
   (] :initarg :] :accessor ])))
(defmethod functor ((l list-compound-term))
  ".")
(defmethod arity ((l list-compound-term))
  2)
(defclass curly-compound-term (compound-term)
  (({ :initarg :{ :accessor {)
   (term :initarg :term :accessor term)
   (} :initarg :} :accessor })))
(defmethod functor ((c curly-compound-term))
  "{}")
(defmethod arity ((c curly-compound-term))
  1)
(defclass char-code-list-compound-term (compound-term)
  ((ccl :initarg :ccl :accessor ccl)))
(defmethod functor ((c char-code-list-compound-term))
  ".")
(defmethod arity ((l char-code-list-compound-term))
  2)

(defmethod display-parse-tree
    ((entity constant-term) (syntax prolog-syntax) pane)
  (display-parse-tree (value entity) syntax pane))
(defmethod display-parse-tree 
    ((entity variable-term) (syntax prolog-syntax) pane)
  (display-parse-tree (name entity) syntax pane))
(defmethod display-parse-tree 
    ((entity functional-compound-term) (syntax prolog-syntax) pane)
  (with-drawing-options (pane :ink (make-rgb-color 0.9 0 0.9))
    (display-parse-tree (functor entity) syntax pane))
  (display-parse-tree (open-ct entity) syntax pane)
  (display-parse-tree (arg-list entity) syntax pane)
  (display-parse-tree (close entity) syntax pane))
(defmethod display-parse-tree
    ((entity bracketed-term) (syntax prolog-syntax) pane)
  (display-parse-tree (open entity) syntax pane)
  (display-parse-tree (term entity) syntax pane)
  (display-parse-tree (close entity) syntax pane))
(defmethod display-parse-tree
    ((entity binary-operator-compound-term) (syntax prolog-syntax) pane)
  (display-parse-tree (left entity) syntax pane)
  (display-parse-tree (operator entity) syntax pane)
  (display-parse-tree (right entity) syntax pane))
(defmethod display-parse-tree
    ((entity prefix-operator-compound-term) (syntax prolog-syntax) pane)
  (display-parse-tree (operator entity) syntax pane)
  (display-parse-tree (right entity) syntax pane))
(defmethod display-parse-tree
    ((entity postfix-operator-compound-term) (syntax prolog-syntax) pane)
  (display-parse-tree (left entity) syntax pane)
  (display-parse-tree (operator entity) syntax pane))
(defmethod display-parse-tree
    ((entity list-compound-term) (syntax prolog-syntax) pane)
  (with-drawing-options (pane :ink (make-rgb-color 0.0 0.0 0.8))
    (display-parse-tree ([ entity) syntax pane)
    (display-parse-tree (items entity) syntax pane)
    (display-parse-tree (] entity) syntax pane)))
(defmethod display-parse-tree
    ((entity curly-compound-term) (syntax prolog-syntax) pane)
  (display-parse-tree ({ entity) syntax pane)
  (display-parse-tree (term entity) syntax pane)
  (display-parse-tree (} entity) syntax pane))

(defclass atom (prolog-nonterminal)
  ((value :initarg :value :accessor value)))
(defmethod syntactic-lexeme ((thing atom))
  ;; FIXME: wrong for empty-list atom and curly-brackets atom
  (syntactic-lexeme (value thing)))
(defclass empty-list (prolog-nonterminal)
  (([ :initarg :[ :accessor [)
   (] :initarg :] :accessor ])))
(defclass curly-brackets (prolog-nonterminal)
  (({ :initarg :{ :accessor {)
   (} :initarg :} :accessor })))
(defmethod display-parse-tree ((entity atom) (syntax prolog-syntax) pane)
  (display-parse-tree (value entity) syntax pane))
(defmethod display-parse-tree ((entity empty-list) (syntax prolog-syntax) pane)
  (display-parse-tree ([ entity) syntax pane)
  (display-parse-tree (] entity) syntax pane))
(defmethod display-parse-tree
    ((entity curly-brackets) (syntax prolog-syntax) pane)
  (display-parse-tree ({ entity) syntax pane)
  (display-parse-tree (} entity) syntax pane))

(defclass arg-list (prolog-nonterminal)
  ((exp :initarg :exp :accessor exp)))
(defclass arg-list-pair (arg-list)
  ((comma :initarg :comma :accessor comma)
   (arg-list :initarg :arg-list :accessor arg-list)))

(defmethod display-parse-tree ((entity arg-list) (syntax prolog-syntax) pane)
  (display-parse-tree (exp entity) syntax pane))
(defmethod display-parse-tree
    ((entity arg-list-pair) (syntax prolog-syntax) pane)
  (display-parse-tree (exp entity) syntax pane)
  (display-parse-tree (comma entity) syntax pane)
  (display-parse-tree (arg-list entity) syntax pane))

(defclass exp (prolog-nonterminal) ())
(defclass exp-atom (exp)
  ((atom :initarg :atom :accessor atom)))
(defclass exp-term (exp)
  ((term :initarg :term :accessor term)))

(defmethod display-parse-tree ((entity exp-atom) (syntax prolog-syntax) pane)
  (display-parse-tree (atom entity) syntax pane))
(defmethod display-parse-tree ((entity exp-term) (syntax prolog-syntax) pane)
  (display-parse-tree (term entity) syntax pane))

(defclass lterm (term)
  ((term :initarg :term :accessor term)))
(defmethod compound-term-p ((l lterm))
  (compound-term-p (term l)))
(defmethod functor ((l lterm))
  (functor (term l)))
(defmethod arity ((l lterm))
  (arity (term l)))

(defmethod display-parse-tree ((entity lterm) (syntax prolog-syntax) pane)
  (display-parse-tree (term entity) syntax pane))

(defclass op (prolog-nonterminal)
  ((name :initarg :name :accessor name)
   (priority :initarg :priority :accessor priority)
   (specifier :initarg :specifier :accessor specifier)))
(defmethod syntactic-lexeme ((thing op))
  (syntactic-lexeme (name thing)))
(defclass prefix-op (op) ())
(defclass binary-op (op) ())
(defclass postfix-op (op) ())

(defmethod display-parse-tree ((entity op) (syntax prolog-syntax) pane)
  (display-parse-tree (name entity) syntax pane))

(defclass items (prolog-nonterminal)
  ((exp :initarg :exp :accessor exp)))
(defclass items-pair (items)
  ((htsep :initarg :htsep :accessor htsep)
   (texp :initarg :texp :accessor texp)))
(defclass items-list (items)
  ((comma :initarg :comma :accessor comma)
   (tlist :initarg :tlist :accessor tlist)))

(defmethod display-parse-tree ((entity items) (syntax prolog-syntax) pane)
  (display-parse-tree (exp entity) syntax pane))
(defmethod display-parse-tree
    ((entity items-pair) (syntax prolog-syntax) pane)
  (display-parse-tree (exp entity) syntax pane)
  (display-parse-tree (htsep entity) syntax pane)
  (display-parse-tree (texp entity) syntax pane))
(defmethod display-parse-tree
    ((entity items-list) (syntax prolog-syntax) pane)
  (display-parse-tree (exp entity) syntax pane)
  (display-parse-tree (comma entity) syntax pane)
  (display-parse-tree (tlist entity) syntax pane))

;;; 6.2.1
(add-rule (grammar-rule (prolog-text -> (directive prolog-text)
                                     (make-instance 'directive-prolog-text
                                                    :directive directive
                                                    :text-rest prolog-text)))
	  *prolog-grammar*)
(add-rule (grammar-rule (prolog-text -> (clause prolog-text)
                                     (make-instance 'clause-prolog-text
                                                    :clause clause
                                                    :text-rest prolog-text)))
	  *prolog-grammar*)
(add-rule (grammar-rule (prolog-text -> ()
                                     (make-instance 'empty-prolog-text)))
	  *prolog-grammar*)

;;; 6.2.1.1
(add-rule (grammar-rule (directive -> (directive-term end)
                                   (make-instance 'directive
                                                  :directive-term directive-term
                                                  :end end)))
	  *prolog-grammar*)
(add-rule (grammar-rule (directive-term -> ((term (and (compound-term-p term)
                                                       (string= (lexeme-string (syntactic-lexeme (functor term))) ":-")
						       (= (arity term) 1))))
                                        (make-instance 'directive-term
                                                       :term term)))
	  *prolog-grammar*)

;;; 6.2.1.2
(add-rule (grammar-rule (clause -> (clause-term end)
                                (make-instance 'clause
                                               :clause-term clause-term
                                               :end end)))
	  *prolog-grammar*)
(add-rule (grammar-rule (clause-term -> ((term (not (and (compound-term-p term)
                                                         (string= (lexeme-string (syntactic-lexeme (functor term))) ":-")
							 (= (arity term) 1)))))
                                     (make-instance 'clause-term
                                                    :term term)))
	  *prolog-grammar*)

;;; 6.3.1.1
(add-rule (grammar-rule (term -> (integer)
                              (make-instance 'constant-term
                                             :priority 0
                                             :value integer)))
	  *prolog-grammar*)
;;; 6.3.1.2
(add-rule (grammar-rule (term -> ((atom (string= (lexeme-string (syntactic-lexeme atom)) "-"))
                                  integer)
                              (make-instance 'constant-term
                                             :priority 0
                                             :value (list atom integer))))
	  *prolog-grammar*)
;;; 6.3.1.3
(add-rule (grammar-rule (term -> ((atom (not (operatorp atom))))
                              (make-instance 'constant-term
                                             :priority 0
                                             :value atom)))
	  *prolog-grammar*)
(add-rule (grammar-rule (term -> ((atom (operatorp atom)))
                              (make-instance 'constant-term
                                             :priority 1201
                                             :value atom)))
	  *prolog-grammar*)

(add-rule (grammar-rule (atom -> (name)
                              (make-instance 'atom :value name)))
	  *prolog-grammar*)
(add-rule (grammar-rule (atom -> (empty-list)
                              (make-instance 'atom :value empty-list)))
	  *prolog-grammar*)
(add-rule (grammar-rule (atom -> (curly-brackets)
                              (make-instance 'atom :value curly-brackets)))
	  *prolog-grammar*)
(add-rule (grammar-rule (empty-list -> (open-list close-list)
                                    (make-instance 'empty-list
                                                   :[ open-list
                                                   :] close-list)))
	  *prolog-grammar*)
(add-rule (grammar-rule (curly-brakets -> (open-curly close-curly)
                                       (make-instance 'curly-brackets
                                                      :{ open-curly
                                                      :} close-curly)))
	  *prolog-grammar*)

;;; 6.3.2
(add-rule (grammar-rule (term -> (variable)
                              (make-instance 'variable-term
                                             :priority 0
                                             :name variable)))
	  *prolog-grammar*)

;;; 6.3.3
(add-rule (grammar-rule (term -> (atom open-ct-lexeme arg-list close)
                              (make-instance 'functional-compound-term
                                             :priority 0
                                             :functor atom
                                             :arg-list arg-list
                                             :open-ct open-ct-lexeme
                                             :close close)))
	  *prolog-grammar*)
(add-rule (grammar-rule (arg-list -> (exp)
                                  (make-instance 'arg-list :exp exp)))
	  *prolog-grammar*)
(add-rule (grammar-rule (arg-list -> (exp comma arg-list)
                                  (make-instance 'arg-list-pair
                                                 :exp exp
                                                 :comma comma
                                                 :arg-list arg-list)))
	  *prolog-grammar*)

;;; 6.3.3.1
(add-rule (grammar-rule (exp -> ((atom (and (operatorp atom)
                                            (not (typep (value atom) 'comma)))))
                             (make-instance 'exp-atom :atom atom)))
	  *prolog-grammar*)
(add-rule (grammar-rule (exp -> ((term (<= (priority term) 999)))
                             (make-instance 'exp-term :term term)))
	  *prolog-grammar*)

;;; 6.3.4.1

;;; NOTE NOTE NOTE
;;;
;;; Handling the production rules
;;;
;;;   term  -> lterm
;;;   n        n
;;;
;;; and
;;;
;;;   lterm -> term
;;;   n        n-1
;;;
;;; is done by making LTERM a subclass of TERM (for the first) so that
;;; any LTERM produced by operator rules is acceptable where a regular
;;; term would be, by explicitly writing the second production rule
;;; out here, and by using inegality tests rather than equalities for
;;; priorities elsewhere.  LTERMs act as containers for terms.
(add-rule (grammar-rule (lterm -> (term)
                               (make-instance 'lterm
                                              :term term
                                              :priority (1+ (priority term)))))
          *prolog-grammar*)

(add-rule (grammar-rule (term -> (open (term (<= (priority term) 1201))
                                              close)
                              (make-instance 'bracketed-term
                                             :priority 0
                                             :open open
                                             :term term
                                             :close close)))
          *prolog-grammar*)
(add-rule (grammar-rule (term -> (open-ct-lexeme
                                  (term (<= (priority term) 1201))
                                  close)
                              (make-instance 'bracketed-term
                                             :priority 0
                                             :open open-ct-lexeme
                                             :term term
                                             :close close)))
          *prolog-grammar*)

;;; 6.3.4.2
;;;
;;; NOTE NOTE NOTE
;;;
;;; We rely here on the (undocumented) fact that returning NIL from
;;; the body of these rules implies a failure.
(add-rule (grammar-rule (lterm -> ((left term) (op (eql (specifier op) :xfx)) (right term))
                               (when (and (< (priority left) (priority op))
                                          (< (priority right) (priority op)))
                                 (make-instance 'lterm
                                                :priority (priority op)
                                                :term
                                                (make-instance 'binary-operator-compound-term
                                                               :left left
                                                               :operator op
                                                               :right right)))))
          *prolog-grammar*)
(add-rule (grammar-rule (lterm -> ((left lterm) (op (eql (specifier op) :yfx)) (right term))
                               (when (and (<= (priority left) (priority op))
                                          (< (priority right) (priority op)))
                                 (make-instance 'lterm
                                                :priority (priority op)
                                                :term
                                                (make-instance 'binary-operator-compound-term
                                                               :left left
                                                               :operator op
                                                               :right right)))))
          *prolog-grammar*)
(add-rule (grammar-rule (term -> ((left term) (op (eql (specifier op) :xfy)) (right term))
                              (when (and (< (priority left) (priority op))
                                         (<= (priority right) (priority op)))
                                (make-instance 'binary-operator-compound-term
                                               :priority (priority op)
                                               :left left
                                               :operator op
                                               :right right))))
          *prolog-grammar*)
(add-rule (grammar-rule (lterm -> (lterm (op (eql (specifier op) :yf)))
                               (when (<= (priority lterm) (priority op))
                                 (make-instance 'lterm
                                                :priority (priority op)
                                                :term
                                                (make-instance 'postfix-operator-compound-term
                                                               :left lterm
                                                               :operator op)))))
          *prolog-grammar*)
(add-rule (grammar-rule (lterm -> (term (op (eql (specifier op) :xf)))
                               (when (< (priority term) (priority op))
                                 (make-instance 'lterm
                                                :priority (priority op)
                                                :term
                                                (make-instance 'postfix-operator-compound-term
                                                               :left term
                                                               :operator op)))))
          *prolog-grammar*)
(add-rule (grammar-rule (term -> ((op (eql (specifier op) :fy)) term)
                              (when (and (or (not (string= (lexeme-string (syntactic-lexeme op)) "-"))
                                             (not (numeric-constant-p term)))
                                         (not (typep (first-lexeme term) 'open-ct-lexeme))
                                         (<= (priority term) (priority op)))
                                (make-instance 'prefix-operator-compound-term
                                               :right term
                                               :operator op
                                               :priority (priority op)))))
          *prolog-grammar*)
(add-rule (grammar-rule (lterm -> ((op (eql (specifier op) :fx)) term)
                               (when (and (or (not (string= (lexeme-string (syntactic-lexeme op)) "-"))
                                              (not (numeric-constant-p term)))
                                          (not (typep (first-lexeme term) 'open-ct-lexeme))
                                          (< (priority term) (priority op)))
                                 (make-instance 'lterm
                                                :priority (priority op)
                                                :term
                                                (make-instance 'prefix-operator-compound-term
                                                               :right term
                                                               :operator op)))))
          *prolog-grammar*)

;;; 6.3.4.3
(macrolet ((add (class &rest specifiers)
             `(progn
               (add-rule (grammar-rule (,class -> (name)
                                               (let ((opspec (find-predefined-operator name ',specifiers)))
                                                 (when opspec
                                                   (make-instance ',class
                                                                  :name name
                                                                  :priority (opspec-priority opspec)
                                                                  :specifier (opspec-specifier opspec))))))
                *prolog-grammar*)
               (add-rule (grammar-rule (,class -> (name)
                                               (let ((opspec (find-defined-operator name ',specifiers)))
                                                 (when opspec
                                                   (make-instance ',class
                                                                  :name name
                                                                  :priority (opspec-priority opspec)
                                                                  :specifier (opspec-specifier opspec))))))
                *prolog-grammar*))))
  (add prefix-op :fx :fy)
  (add binary-op :xfx :xfy :yfx)
  (add postfix-op :xf :yf))
(add-rule (grammar-rule (op -> (comma)
                            (make-instance 'op
                                           :name comma
                                           :priority 1000
                                           :specifier :xfy)))
          *prolog-grammar*)

;;; 6.3.5
(add-rule (grammar-rule (term -> (open-list items close-list)
                              (make-instance 'list-compound-term
                                             :priority 0
                                             :[ open-list
                                             :items items
                                             :] close-list)))
          *prolog-grammar*)
(add-rule (grammar-rule (items -> (exp comma items)
                               (make-instance 'items-list
                                              :exp exp
                                              :comma comma
                                              :tlist items)))
          *prolog-grammar*)
(add-rule (grammar-rule (items -> ((left exp) head-tail-separator (right exp))
                               (make-instance 'items-pair
                                              :exp left
                                              :htsep head-tail-separator
                                              :texp right)))
          *prolog-grammar*)

;;; 6.3.6
(add-rule (grammar-rule (term -> (open-curly term close-curly)
                              (make-instance 'curly-compound-term
                                             :priority 0
                                             :{ open-curly
                                             :term term
                                             :} close-curly)))
          *prolog-grammar*)

;;; 6.3.7
(add-rule (grammar-rule (term -> (char-code-list)
                              (make-instance 'char-code-list-compound-term
                                             :priority 0
                                             :ccl char-code-list)))
          *prolog-grammar*)

(defparameter *predefined-operators* nil)
(defstruct (opspec (:type list))
  name
  priority
  specifier)
(macrolet ((def (priority specifier &rest names)
               (let (result)
                 (dolist (name names `(progn ,@(nreverse result)))
                   (push
                    `(push (make-opspec :name ',name :priority ,priority
                            :specifier ,specifier)
                      *predefined-operators*)
                    result)))))
  ;; Table 5 -- The predefined operators
  (def 1200 :xfx ":-" "-->")
  (def 1200 :fx ":-" "?-")
  (def 1100 :xfy ";")
  (def 1050 :xfy "->")
  #+nil ; handled specially (FIXME: is this the right way?)
  (def 1000 :xfy ",")
  (def 700 :xfx "=" "\\=")
  (def 700 :xfx "==" "\\==" "@<" "@=<" "@>" "@>=")
  (def 700 :xfx "=..")
  (def 700 :xfx "is" "=:=" "=\\=" "<" "=<" ">" ">=")
  (def 500 :yfx "+" "-" "/\\" "\\/")
  (def 400 :yfx "*" "/" "//" "rem" "mod" "<<" ">>")
  (def 200 :xfx "**")
  (def 200 :xfy "^")
  (def 200 :fy "-" "\\")
  (def 100 :xfx "@")
  (def 50 :xfx ":"))

(defun find-predefined-operator (name specifiers)
  (find (lexeme-string (syntactic-lexeme name))
        (remove-if-not (lambda (x) (member (opspec-specifier x) specifiers))
                       *predefined-operators*)
        :key #'opspec-name :test #'string=))
(defun find-defined-operator (name specifiers)
  (declare (ignore name specifiers))
  nil)
(defun operatorp (name)
  (or (find-predefined-operator name '(:xf :yf :fx :fx :xfx :xfy :yfx))
      (find-defined-operator name '(:xf :yf :fx :fx :xfx :xfy :yfx))))

(defun lexeme-string (thing)
  (check-type thing prolog-lexeme)
  (coerce
   (buffer-sequence (buffer thing)
                    (start-offset thing)
                    (end-offset thing))
   'string))

(defun numeric-constant-p (thing)
  (and (typep thing 'constant-term)
       (let ((value (value thing)))
         (or (typep value 'integer)
             (and (consp value)
                  (typep (car value) 'atom)
                  (typep (cadr value) 'integer))))))
  
(defun first-lexeme (thing)
  ;; FIXME: we'll need to implement this.
  (declare (ignore thing))
  nil)
  
;;; update syntax

(defmethod update-syntax-for-display (buffer (syntax prolog-syntax) top bot)
  (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)))
		(setf (slot-value next-lexeme 'state)
		      (advance-parse parser (list next-lexeme) (slot-value current-token 'state))))
	      (incf valid-parse))))

(defmethod inter-lexeme-object-p ((lexer prolog-lexer) object)
  (member object '(#\Space #\Newline)))

(defmethod update-syntax (buffer (syntax prolog-syntax))
  (with-slots (lexer valid-parse) syntax
     (let* ((low-mark (low-mark buffer))
	    (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))))))

;;; display

(defvar *white-space-start* nil)

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

(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)
			  (setf (aref *cursor-positions* (incf *current-line*))
				(multiple-value-bind (x y) (stream-cursor-position pane)
				  (declare (ignore x))
				  y)))
	       (#\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))))		    

#+nil
(defmethod display-parse-tree :around ((entity prolog-parse-tree) syntax pane)
  (with-slots (top bot) pane
     (when (and (end-offset entity) (mark> (end-offset entity) top))
       (call-next-method))))

(defmethod display-parse-tree ((entity prolog-token) (syntax prolog-syntax) pane)
  (flet ((cache-test (t1 t2)
	   (and (eq t1 t2)
		#+nil
		(eq (slot-value t1 'ink)
		    (medium-ink (sheet-medium pane)))
		#+nil
		(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
	 #+nil
	 (setf ink (medium-ink (sheet-medium pane))
	       face (text-style-face (medium-text-style (sheet-medium pane))))
	 (present (coerce (buffer-sequence (buffer syntax)
					   (start-offset entity)
					   (end-offset entity))
			  'string)
		  'string
		  :stream pane))))

;;; KLUDGE: below this line, this is just s/html/prolog/ on the
;;; definitions in html-syntax.lips

(defmethod display-parse-tree :before ((entity prolog-token) (syntax prolog-syntax) pane)
  (handle-whitespace pane (buffer pane) *white-space-start* (start-offset entity))
  (setf *white-space-start* (end-offset entity)))

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

(defmethod display-parse-stack (symbol stack (syntax prolog-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))))

(defmethod redisplay-pane-with-syntax ((pane climacs-pane) (syntax prolog-syntax) current-p)
  (with-slots (top bot) pane
     (setf *cursor-positions* (make-array (1+ (number-of-lines-in-region top bot)))
	   *current-line* 0
	   (aref *cursor-positions* 0) (stream-cursor-position pane))
     (with-slots (lexer) syntax
	(let ((average-token-size (max (float (/ (size (buffer pane)) (nb-lexemes lexer)))
				       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 (lexeme lexer (1- end-token-index))) bot)
		  do (decf end-token-index))
	    ;; go forward to the last token before bot
	    (loop until (or (= end-token-index (nb-lexemes lexer))
			    (mark> (start-offset (lexeme lexer end-token-index)) bot))
		  do (incf end-token-index))
	    (let ((start-token-index end-token-index))
	      ;; go back to the first token after top, or until the previous token
	      ;; contains a valid parser state
	      (loop until (or (mark<= (end-offset (lexeme lexer (1- start-token-index))) top)
			      (not (parse-state-empty-p 
				    (slot-value (lexeme lexer (1- start-token-index)) 'state))))
		    do (decf start-token-index))
	      (let ((*white-space-start* (offset top)))
		;; display the parse tree if any
		(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)
				       syntax
				       pane))
		;; display the lexemes
		(with-drawing-options (pane :ink +red+)
		  (loop while (< start-token-index end-token-index)
			do (let ((token (lexeme lexer 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+))))))