prolog-syntax.lisp 41.8 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
;;; -*- 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))

36 37 38
(defmacro define-prolog-rule ((&rest rule) &body body)
  `(add-rule (grammar-rule (,@rule ,@body)) *prolog-grammar*))

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
(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)))
68 69 70
(defmethod print-object ((o prolog-lexeme) s)
  (print-unreadable-object (o s :type t)
    (format s (lexeme-string o))))
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

(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))
106 107 108 109 110
                        (define-prolog-rule (,name -> (,(f name)))
                          (make-instance ',name :syntactic-lexeme ,(f name)))
                        (define-prolog-rule (,name -> (layout-text ,(f name)))
                          (make-instance ',name :layout-text layout-text
                           :syntactic-lexeme ,(f name)))))
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
		  ,@(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)))

133 134 135 136 137 138 139 140 141 142 143
;;; open-ct is a special case: by 6.5.1 it cannot be preceded by
;;; layout text.  We could elide this and its grammar rules, but this
;;; way we get a clearer relationship between the standard and its
;;; expression here.
(defclass open-ct (prolog-nonterminal)
  ((syntactic-lexeme :initarg :syntactic-lexeme :accessor syntactic-lexeme)))
(defmethod display-parse-tree ((entity open-ct) (syntax prolog-syntax) pane)
  (display-parse-tree (syntactic-lexeme entity) syntax pane))
(define-prolog-rule (open-ct -> (open-ct-lexeme))
  (make-instance 'open-ct :syntactic-lexeme open-ct-lexeme))

144 145 146 147 148
;;; 6.4.1
(define-prolog-rule (layout-text -> (comment-lexeme layout-text))
  (make-instance 'layout-text :comment comment-lexeme :cont layout-text))
(define-prolog-rule (layout-text -> ())
  (make-instance 'layout-text :cont nil))
149

150 151
(defclass prolog-lexer (incremental-lexer)
  ((valid-lex :initarg :valid-lex :accessor valid-lex :initform 1)))
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

(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)
198 199 200 201
		   while (let ((object (object-after scan)))
			   (or (alphanumericp object)
			       (eql object #\_)))
		   do (fo))
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
	     (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)
259 260 261
                     (let ((object (object-after scan)))
                       (not (or (alphanumericp object)
                                (eql object #\_)))))
262 263 264 265
		 (return (make-instance 'anonymous-lexeme))
		 (go NAMED-VARIABLE))
	   NAMED-VARIABLE
	     (loop until (end-of-buffer-p scan)
266 267 268
		while (let ((object (object-after scan)))
                        (or (alphanumericp object)
                            (eql object #\_)))
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
		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)))
Christophe Rhodes's avatar
Christophe Rhodes committed
446 447 448 449 450
(defmethod canonical-name ((thing atom))
  (canonical-name (value thing)))
(defmethod canonical-name ((thing name))
  ;; FIXME: should canonize
  (lexeme-string (syntactic-lexeme thing)))
451 452 453
(defclass empty-list (prolog-nonterminal)
  (([ :initarg :[ :accessor [)
   (] :initarg :] :accessor ])))
Christophe Rhodes's avatar
Christophe Rhodes committed
454
(defmethod canonical-name ((thing empty-list))
Christophe Rhodes's avatar
Christophe Rhodes committed
455
  ;; FIXME: this clashes with the canonical name for the atom '[]'
Christophe Rhodes's avatar
Christophe Rhodes committed
456
  "[]")
457 458 459
(defclass curly-brackets (prolog-nonterminal)
  (({ :initarg :{ :accessor {)
   (} :initarg :} :accessor })))
Christophe Rhodes's avatar
Christophe Rhodes committed
460
(defmethod canonical-name ((thing curly-brackets))
Christophe Rhodes's avatar
Christophe Rhodes committed
461
  ;; FIXME: see comment in CANONICAL-NAME (EMPTY-LIST)
Christophe Rhodes's avatar
Christophe Rhodes committed
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
(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))

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
;;; FIXME: the need for these is because it is a protocol violation to
;;; create nested nonterminals from one rule.
(defclass operator-compound-lterm (lterm)
  ((operator :initarg :operator :accessor operator)))
(defmethod compound-term-p ((l operator-compound-lterm))
  t)
(defmethod functor ((l operator-compound-lterm))
  (operator l))
(defclass binary-operator-compound-lterm (operator-compound-lterm)
  ((left :initarg :left :accessor left)
   (right :initarg :right :accessor right)))
(defmethod arity ((l binary-operator-compound-lterm))
  2)
(defclass prefix-operator-compound-lterm (operator-compound-lterm)
  ((right :initarg :right :accessor right)))
(defmethod arity ((l prefix-operator-compound-lterm))
  1)
(defclass postfix-operator-compound-lterm (operator-compound-lterm)
  ((left :initarg :left :accessor left)))
(defmethod arity ((l postfix-operator-compound-lterm))
  1)

(defmethod display-parse-tree
    ((entity binary-operator-compound-lterm) (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-lterm) (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-lterm) (syntax prolog-syntax) pane)
  (display-parse-tree (left entity) syntax pane)
  (display-parse-tree (operator entity) syntax pane))

546 547 548 549
(defclass op (prolog-nonterminal)
  ((name :initarg :name :accessor name)
   (priority :initarg :priority :accessor priority)
   (specifier :initarg :specifier :accessor specifier)))
Christophe Rhodes's avatar
Christophe Rhodes committed
550 551
(defmethod canonical-name ((thing op))
  (canonical-name (name thing)))
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
(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
582 583 584 585 586 587 588
(define-prolog-rule (prolog-text -> (directive prolog-text))
  (make-instance 'directive-prolog-text :directive directive
                 :text-rest prolog-text))
(define-prolog-rule (prolog-text -> (clause prolog-text))
  (make-instance 'clause-prolog-text :clause clause :text-rest prolog-text))
(define-prolog-rule (prolog-text -> ())
  (make-instance 'empty-prolog-text))
589 590

;;; 6.2.1.1
591 592
(defun term-directive-p (term)
  (and (compound-term-p term)
Christophe Rhodes's avatar
Christophe Rhodes committed
593
       (string= (canonical-name (functor term)) ":-")
594 595 596 597 598 599
       (= (arity term) 1)))

(define-prolog-rule (directive -> (directive-term end))
  (make-instance 'directive :directive-term directive-term :end end))
(define-prolog-rule (directive-term -> ((term (term-directive-p term))))
  (make-instance 'directive-term :term term))
600 601

;;; 6.2.1.2
602 603 604 605
(define-prolog-rule (clause -> (clause-term end))
  (make-instance 'clause :clause-term clause-term :end end))
(define-prolog-rule (clause-term -> ((term (not (term-directive-p term)))))
  (make-instance 'clause-term :term term))
606 607

;;; 6.3.1.1
608 609 610
(define-prolog-rule (term -> (integer))
  (make-instance 'constant-term :priority 0 :value integer))

611
;;; 6.3.1.2
612
(define-prolog-rule (term -> ((atom
Christophe Rhodes's avatar
Christophe Rhodes committed
613
                               (string= (canonical-name atom) "-"))
614 615 616 617
                              integer))
  ;; FIXME: this doesn't really look right.
  (make-instance 'constant-term :priority 0 :value (list atom integer)))

618
;;; 6.3.1.3
619 620 621 622 623 624 625 626 627 628 629 630 631
(define-prolog-rule (term -> ((atom (not (operatorp atom)))))
  (make-instance 'constant-term :priority 0 :value atom))
(define-prolog-rule (term -> ((atom (operatorp atom))))
  (make-instance 'constant-term :priority 1201 :value atom))

(define-prolog-rule (atom -> (name))
  (make-instance 'atom :value name))
(define-prolog-rule (atom -> (empty-list))
  (make-instance 'atom :value empty-list))
(define-prolog-rule (atom -> (curly-brackets))
  (make-instance 'atom :value curly-brackets))
(define-prolog-rule (empty-list -> (open-list close-list))
  (make-instance 'empty-list :[ open-list :] close-list))
632
(define-prolog-rule (curly-brackets -> (open-curly close-curly))
633
  (make-instance 'curly-brackets :{ open-curly :} close-curly))
634 635

;;; 6.3.2
636 637
(define-prolog-rule (term -> (variable))
  (make-instance 'variable-term :priority 0 :name variable))
638 639

;;; 6.3.3
640
(define-prolog-rule (term -> (atom open-ct arg-list close))
641
  (make-instance 'functional-compound-term :priority 0 :functor atom
642
                 :arg-list arg-list :open-ct open-ct :close close))
643 644 645 646
(define-prolog-rule (arg-list -> (exp))
  (make-instance 'arg-list :exp exp))
(define-prolog-rule (arg-list -> (exp comma arg-list))
  (make-instance 'arg-list-pair :exp exp :comma comma :arg-list arg-list))
647 648

;;; 6.3.3.1
649 650 651 652 653
(define-prolog-rule (exp -> ((atom (and (operatorp atom)
                                        (not (typep (value atom) 'comma))))))
  (make-instance 'exp-atom :atom atom))
(define-prolog-rule (exp -> ((term (<= (priority term) 999))))
  (make-instance 'exp-term :term term))
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673

;;; 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.
674 675 676 677
;;;
;;; FIXME: why on earth doesn't this cause infinite recursion?  If
;;; LTERM is a subtype of TERM, as it is, this rule should surely be
;;; always applicable.
678 679 680 681 682 683
(define-prolog-rule (lterm -> (term))
  (make-instance 'lterm :term term :priority (1+ (priority term))))

(define-prolog-rule (term -> (open (term (<= (priority term) 1201)) close))
  (make-instance 'bracketed-term :priority 0
                 :open open :term term :close close))
684
(define-prolog-rule (term -> (open-ct
685 686 687
                              (term (<= (priority term) 1201))
                              close))
  (make-instance 'bracketed-term :priority 0
688
                 :open open-ct :term term :close close))
689 690 691 692 693

;;; 6.3.4.2
;;;
;;; NOTE NOTE NOTE
;;;
694
;;; We rely here on the (undocumented?) fact that returning NIL from
695
;;; the body of these rules implies a failure.
696 697 698 699 700
(define-prolog-rule (lterm -> ((left term)
                               (op (eql (specifier op) :xfx))
                               (right term)))
  (when (and (< (priority left) (priority op))
             (< (priority right) (priority op)))
701 702
    (make-instance 'binary-operator-compound-lterm :priority (priority op)
		   :left left :operator op :right right)))
703 704 705 706 707
(define-prolog-rule (lterm -> ((left lterm)
                               (op (eql (specifier op) :yfx))
                               (right term)))
  (when (and (<= (priority left) (priority op))
             (< (priority right) (priority op)))
708 709
    (make-instance 'binary-operator-compound-lterm :priority (priority op)
		   :left left :operator op :right right)))
710 711 712 713 714 715 716 717 718
(define-prolog-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)))
(define-prolog-rule (lterm -> (lterm (op (eql (specifier op) :yf))))
  (when (<= (priority lterm) (priority op))
719 720
    (make-instance 'postfix-operator-compound-lterm :priority (priority op)
		   :left lterm :operator op)))
721 722
(define-prolog-rule (lterm -> (term (op (eql (specifier op) :xf))))
  (when (< (priority term) (priority op))
723 724
    (make-instance 'postfix-operator-compound-lterm :priority (priority op)
		   :left term :operator op)))
725
(define-prolog-rule (term -> ((op (eql (specifier op) :fy)) term))
Christophe Rhodes's avatar
Christophe Rhodes committed
726
  (when (and (or (not (string= (canonical-name op) "-"))
727 728 729 730 731 732
                 (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))))
(define-prolog-rule (lterm -> ((op (eql (specifier op) :fx)) term))
Christophe Rhodes's avatar
Christophe Rhodes committed
733
  (when (and (or (not (string= (canonical-name op) "-"))
734 735 736
                 (not (numeric-constant-p term)))
             (not (typep (first-lexeme term) 'open-ct-lexeme))
             (< (priority term) (priority op)))
737 738
    (make-instance 'prefix-operator-compound-lterm :priority (priority op)
		   :right term :operator op)))
739 740

;;; 6.3.4.3
741
(macrolet ((def (class &rest specifiers)
742
             `(progn
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
               (define-prolog-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)))))
               (define-prolog-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))))))))
  (def prefix-op :fx :fy)
  (def binary-op :xfx :xfy :yfx)
  (def postfix-op :xf :yf))
(define-prolog-rule (op -> (comma))
  (make-instance 'op :name comma :priority 1000 :specifier :xfy))
760 761

;;; 6.3.5
762 763 764 765 766 767 768 769
(define-prolog-rule (term -> (open-list items close-list))
  (make-instance 'list-compound-term :priority 0
                 :[ open-list :items items :] close-list))
(define-prolog-rule (items -> (exp comma items))
  (make-instance 'items-list :exp exp :comma comma :tlist items))
(define-prolog-rule (items -> ((left exp) head-tail-separator (right exp)))
  (make-instance 'items-pair :exp left
                 :htsep head-tail-separator :texp right))
770 771
(define-prolog-rule (items -> (exp))
  (make-instance 'items :exp exp))
772 773

;;; 6.3.6
774 775 776
(define-prolog-rule (term -> (open-curly term close-curly))
  (make-instance 'curly-compound-term :priority 0
                 :{ open-curly :term term :} close-curly))
777 778

;;; 6.3.7
779 780 781
(define-prolog-rule (term -> (char-code-list))
  (make-instance 'char-code-list-compound-term
                 :priority 0 :ccl char-code-list))
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

(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)
Christophe Rhodes's avatar
Christophe Rhodes committed
816
  (find (canonical-name name)
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
        (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))))))
842

843 844 845 846 847 848 849 850 851
(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
852 853 854 855 856 857 858 859 860 861 862
    (with-slots (climacs-syntax::lexemes valid-lex) lexer
      (let ((scan (clone-mark (low-mark buffer) :left)))
        (setf (offset scan)
              (end-offset (lexeme lexer (1- valid-lex))))
	;; lex as far as we need.  We actually win quite a lot if we
	;; can implement the splicing described in the FIXME note,
	;; below, because there's then a good chance that CLIM's
	;; incremental redisplay will Do The Right Thing (on the EQ
	;; lexemes)
        (loop do (skip-inter-lexeme-objects lexer scan)
              until (end-of-buffer-p scan)
863
	      until (mark<= bot (start-offset (lexeme lexer (1- valid-lex))))
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
	      ;; FIXME: a further criterion is when scan matches the
	      ;; start-offset of an element in lexemes, at which point
	      ;; we know that the entirety of the rest of the old lex
	      ;; is valid without doing any further work.
	      do (let* ((start-mark (clone-mark scan))
			(lexeme (next-lexeme lexer scan))
			(size (- (offset scan) (offset start-mark))))
		   (setf (slot-value lexeme 'climacs-syntax::start-mark) start-mark
			 (slot-value lexeme 'climacs-syntax::size) size)
		   (insert-lexeme lexer valid-lex lexeme)
		   (incf valid-lex)))
	;; remove lexemes which we know to be invalid
	(let ((end (end-offset (lexeme lexer (1- valid-lex)))))
	  (loop until (= (nb-lexemes lexer) valid-lex)
		while (< (start-offset (lexeme lexer valid-lex)) end)
		do (delete* climacs-syntax::lexemes valid-lex))))
      ;; parse up to the limit of validity imposed by the lexer, or
      ;; the bottom of the visible area
      (loop until (= valid-parse valid-lex)
883 884 885
	    ;; NOTE: this ceases being the same condition as the above
	    ;; as soon as the FIXME note above is implemented.
	    until (mark<= bot (start-offset (lexeme lexer (1- valid-parse))))
886 887 888 889 890 891
	    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))))))
892 893 894 895 896 897

(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
898 899 900 901 902 903 904 905 906 907 908 909 910 911
    (let* ((low-mark (low-mark buffer))
	   (high-mark (high-mark buffer)))
      (when (mark<= low-mark high-mark)
	(with-slots (climacs-syntax::lexemes valid-lex) lexer
	  (let ((start 1)
		(end (nb-elements climacs-syntax::lexemes)))
	    (loop while (< start end)
		  do (let ((middle (floor (+ start end) 2)))
		       (if (mark< (end-offset (element* climacs-syntax::lexemes middle))
				  low-mark)
			   (setf start (1+ middle))
			   (setf end middle))))
	    (setf valid-lex start)
	    (setf valid-parse start)))))))
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

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

(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))))
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
	 (let ((string (coerce (buffer-sequence (buffer syntax)
						(start-offset entity)
						(end-offset entity))
			       'string)))
	   (with-slots (top bot) pane
	     (let (start end)
	       (setf start (max 0 (- (offset top) (start-offset entity))))
	       (setf end (- (length string) (max 0 (- (end-offset entity) (offset bot)))))
	       (loop
		(when (>= start end)
		  (return))
		(let ((nl (position #\Newline string
				    :start start :end end)))
		  (unless nl
		    (present (subseq string start end) 'string :stream pane)
		    (return))
		  (present (subseq string start nl) 'string :stream pane)
		  (handle-whitespace pane (buffer pane)
				     (+ (start-offset entity) nl)
				     (+ (start-offset entity) nl 1))
		  (setf start (+ nl 1))))))))))
980

981
(defmethod display-parse-tree :before ((entity prolog-lexeme) (syntax prolog-syntax) pane)
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
  (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))))

1000 1001 1002
(defun nb-valid-lexemes (lexer)
  (slot-value lexer 'valid-lex))

1003 1004 1005 1006 1007 1008
(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
1009
	(let ((average-token-size (max (float (/ (size (buffer pane)) (nb-valid-lexemes lexer)))
1010 1011 1012 1013 1014 1015 1016
				       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
1017
	    (loop until (or (= end-token-index (nb-valid-lexemes lexer))
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
			    (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+))))))
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062

#|
(climacs-gui::define-named-command com-inspect-lex ()
  (with-slots (lexer) (slot-value (buffer (climacs-gui::current-window)) 'climacs-syntax::syntax)
    (let ((*standard-input* *query-io*)
	  (*standard-output* *query-io*))
      (inspect lexer))))

(climacs-gui::define-named-command com-inspect-parse ()
  (with-slots (parser) (slot-value (buffer (climacs-gui::current-window)) 'climacs-syntax::syntax)
    (let ((*standard-input* *query-io*)
	  (*standard-output* *query-io*))
      (inspect parser))))
|#