lisp-syntax.lisp 160 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
;;; -*- Mode: Lisp; Package: CLIMACS-LISP-SYNTAX -*-

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

;;; Alternative syntax module for analysing Common Lisp

(in-package :climacs-lisp-syntax)

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Convenience functions and macros:

(defun unlisted (obj)
  (if (listp obj)
      (first obj)
      obj))

(defun listed (obj)
  (if (listp obj)
      obj
      (list obj)))

(defun usable-package (package-designator)
  "Return a usable package based on `package-designator'."
  (or (find-package package-designator)
      *package*))

(defmacro evaluating-interactively (&body body)
  `(handler-case (progn ,@body)
     (end-of-file ()
       (esa:display-message "Unbalanced parentheses in form."))))

49
50
51
52
53
54
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; The command table.

(make-command-table 'lisp-table
                    :errorp nil
55
                    :inherit-from '(global-climacs-table))
56

57
58
59
60
61
62
63
64
65
66
67
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; the syntax object

(define-syntax lisp-syntax (basic-syntax)
  ((stack-top :initform nil)
   (potentially-valid-trees)
   (lookahead-lexeme :initform nil :accessor lookahead-lexeme)
   (current-state)
   (current-start-mark)
   (current-size)
68
   (scan)
69
70
71
72
73
74
   (package-list :accessor package-list
                 :documentation "An alist mapping the end offset
                 of (in-package) forms to a string of the package
                 designator in the form. The list is sorted with
                 the earliest (in-package) forms last (descending
                 offset).")
75
   (base :initform nil
76
77
78
79
80
         :documentation "The base which numbers in the buffer are
         expected to be in.")
   (option-specified-package :accessor option-specified-package
                             :initform nil
                             :documentation "The package
81
                             specified in the attribute
82
                             line (may be overridden
83
84
85
86
87
88
                             by (in-package) forms).")
   (image :accessor image
          :initform nil
          :documentation "An image object (or NIL) that
          determines where and how Lisp code in the buffer of the
          syntax should be run."))
89
  (:name "Lisp")
90
91
  (:pathname-types "lisp" "lsp" "cl")
  (:command-table lisp-table))
92

93
94
95
96
97
98
99
(defgeneric base (syntax)
  (:documentation "Get the base `syntax' should interpret numbers
  in.")
  (:method ((syntax lisp-syntax))
    (or (slot-value syntax 'base)
        *read-base*)))

100
101
102
(defmethod (setf base) (base (syntax lisp-syntax))
  (setf (slot-value syntax 'base) base))

103
104
(define-option-for-syntax lisp-syntax "Package" (syntax package-name)
  (let ((specified-package (find-package package-name)))
105
    (setf (option-specified-package syntax) (or specified-package package-name))))
106
107
108
109

(define-option-for-syntax lisp-syntax "Base" (syntax base)
  (let ((integer-base (parse-integer base :junk-allowed t)))
    (when integer-base
110
111
112
      (if (typep integer-base '(integer 2 36))
          (setf (base syntax) integer-base)
          (esa:display-message "Invalid base specified: outside the interval 2 to 36.")))))
113

114
115
116
117
118
(defmethod initialize-instance :after ((syntax lisp-syntax) &rest args)
  (declare (ignore args))
  (with-slots (buffer scan) syntax
     (setf scan (clone-mark (low-mark buffer) :left))))

119
(defmethod name-for-info-pane ((syntax lisp-syntax) &key pane)
120
  (format nil "Lisp~@[:~(~A~)~]"
121
          (package-name (package-at-mark syntax (point pane)))))
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
(defgeneric default-image ()
  (:documentation "The default image for when the current syntax
  does not mandate anything itself (for example if it is not a
  Lisp syntax).")
  (:method ()
    t))

(defgeneric get-usable-image (syntax)
  (:documentation "Get usable image object from `syntax'.")
  (:method (syntax)
    (default-image))
  (:method ((syntax lisp-syntax))
    (or (image syntax)
        (default-image))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Swank interface functions:

(defgeneric eval-string-for-climacs (image string package)
  (:documentation "Evaluate `string' in `package'. A single value
is returned: The result of evaluating `string'.")
  (:method (image string package)
    (let ((*package* package))
      (eval-form-for-climacs image (read-from-string string)))))

(defgeneric eval-form-for-climacs (image form)
  (:documentation "Evaluate `string' in `package'. A single value
is returned: The result of evaluating `string'.")
  (:method (image form)
    (declare (ignore image))
    (eval form)))

(defgeneric compile-string-for-climacs (image string package buffer buffer-mark)
  (:documentation "Compile and evaluate `string' in
`package'. Two values are returned: The result of evaluating
`string' and a list of compiler notes. `Buffer' and `buffer-mark'
will be used for hyperlinking the compiler notes to the source
code.")
  (:method (image string package buffer buffer-mark)
    (declare (ignore image string package buffer buffer-mark))
    (error "Backend insufficient for this operation")))

(defgeneric compile-form-for-climacs (image form buffer buffer-mark)
  (:documentation "Compile and evaluate `form', which must be a
valid Lisp form. Two values are returned: The result of
evaluating `string' and a list of compiler notes. `Buffer' and
`buffer-mark' will be used for hyperlinking the compiler notes to
the source code.")
  (:method (image form buffer buffer-mark)
    (compile-string-for-climacs image
174
175
                                (let ((*print-base* (base (syntax buffer))))
                                  (write-to-string form))
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
                                *package* buffer buffer-mark)))

(defgeneric compile-file-for-climacs (image filepath package &optional load-p)
  (:documentation "Compile the file at `filepath' in
`package'. If `load-p' is non-NIL, also load the file at
`filepath'. Two values will be returned: the result of compiling
the file and a list of compiler notes.")
  (:method (image filepath package &optional load-p)
    (declare (ignore image filepath package load-p))
    (error "Backend insufficient for this operation")))

(defgeneric macroexpand-for-climacs (image form &optional full-p)
  (:documentation "Macroexpand `form' and return result.")
  (:method (image form &optional full-p)
    (declare (ignore image))
    (funcall (if full-p
                 #'macroexpand
                 #'macroexpand-1)
             form)))

(defgeneric find-definitions-for-climacs (image symbol)
  (:documentation "Return list of definitions for `symbol'.")
  (:method (image symbol)
    (declare (ignore image symbol))))

(defgeneric get-class-keyword-parameters (image class)
  (:documentation "Get a list of keyword parameters (possibly
along with any default values) that can be used in a
`make-instance' form for `class'.")
  (:method (image class)
    (declare (ignore image class))))

(defgeneric arglist (image symbol)
  (:documentation "Get plain arglist for symbol.")
  (:method (image symbol)
    (declare (ignore image symbol))))

(defgeneric simple-completions (image string default-package)
  (:documentation "Return a list of simple symbol-completions for
`string' in `default-package'.")
  (:method (image string default-package)
    (declare (ignore image string default-package))))

(defgeneric fuzzy-completions (image symbol-name default-package &optional limit)
  (:documentation "Return a list of fuzzy completions for `symbol-name'.")
  (:method (image symbol-name default-package &optional limit)
    (declare (ignore image symbol-name default-package limit))))

224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; lexer

(defgeneric skip-inter (syntax state scan)
  (:documentation "advance scan until the beginning of a new
    lexeme.  Return T if one can be found and NIL otherwise."))

(defgeneric lex (syntax state scan)
  (:documentation "Return the next lexeme starting at scan."))

(defclass lexer-state ()
  ()
  (:documentation "These states are used to determine how the lexer 
    should behave."))

(defmacro define-lexer-state (name superclasses &body body)
  `(defclass ,name (,@superclasses lexer-state)
      ,@body))

244
245
246
247
248
(define-lexer-state lexer-error-state ()
  ()
  (:documentation "In this state, the lexer returns error lexemes
    consisting of entire lines of text"))

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
(define-lexer-state lexer-toplevel-state ()
  ()
  (:documentation "In this state, the lexer assumes it can skip 
    whitespace and should recognize ordinary lexemes of the language
    except for the right parenthesis"))

(define-lexer-state lexer-list-state (lexer-toplevel-state)
  ()
  (:documentation "In this state, the lexer assumes it can skip 
    whitespace and should recognize ordinary lexemes of the language"))

(define-lexer-state lexer-string-state ()
  ()
  (:documentation "In this state, the lexer is working inside a string 
    delimited by double quote characters."))

(define-lexer-state lexer-line-comment-state ()
  ()
  (:documentation "In this state, the lexer is working inside a line 
    comment (starting with a semicolon."))

(define-lexer-state lexer-long-comment-state ()
  ()
  (:documentation "In this state, the lexer is working inside a long
    comment delimited by #| and |#."))

275
(define-lexer-state lexer-escaped-token-state ()
276
  ()
277
278
  (:documentation "In this state, the lexer is accumulating a token
    and an odd number of multiple escapes have been seen."))
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

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; this should go in syntax.lisp or lr-syntax.lisp
;;; and should inherit from parse-tree

(defclass parser-symbol ()
  ((start-mark :initform nil :initarg :start-mark :reader start-mark)
   (size :initform nil :initarg :size)
   (parent :initform nil :accessor parent)
   (children :initform '() :initarg :children :reader children)
   (preceding-parse-tree :initform nil :reader preceding-parse-tree)
   (parser-state :initform nil :initarg :parser-state :reader parser-state)))

(defmethod start-offset ((state parser-symbol))
  (let ((mark (start-mark state)))
    (when mark
      (offset mark))))

(defmethod end-offset ((state parser-symbol))
  (with-slots (start-mark size) state
     (when start-mark
       (+ (offset start-mark) size))))

(defgeneric action (syntax state lexeme))
(defgeneric new-state (syntax state parser-symbol))

(defclass parser-state () ())

(defmacro define-parser-state (name superclasses &body body)
  `(progn 
     (defclass ,name ,superclasses
	  ,@body)
     (defvar ,name (make-instance ',name))))

(defclass lexeme (parser-symbol) ())

(defmethod print-object ((lexeme lexeme) stream)
  (print-unreadable-object (lexeme stream :type t :identity t)
    (format stream "~s ~s" (start-offset lexeme) (end-offset lexeme))))

(defclass nonterminal (parser-symbol) ())

(defmethod initialize-instance :after ((parser-symbol nonterminal) &rest args)
  (declare (ignore args))
  (with-slots (children start-mark size) parser-symbol
     (loop for child in children
	   do (setf (parent child) parser-symbol))
     (let ((start (find-if-not #'null children :key #'start-offset))
	   (end (find-if-not #'null children :key #'end-offset :from-end t)))
       (when start
	 (setf start-mark (slot-value start 'start-mark)
	       size (- (end-offset end) (start-offset start)))))))  

;;; until here
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defclass lisp-nonterminal (nonterminal) ())     
(defclass form (lisp-nonterminal) ())
337
(defclass incomplete-form-mixin () ())
338

339
340
(defclass comment (lisp-nonterminal) ())

341
342
343
344
345
346
(defclass lisp-lexeme (lexeme)
  ((ink)
   (face)))

(defclass error-lexeme (lisp-lexeme) ())
(defclass left-parenthesis-lexeme (lisp-lexeme) ())
347
(defclass simple-vector-start-lexeme (lisp-lexeme) ())
348
349
350
351
(defclass right-parenthesis-lexeme (lisp-lexeme) ())
(defclass quote-lexeme (lisp-lexeme) ())
(defclass backquote-lexeme (lisp-lexeme) ())
(defclass comma-lexeme (lisp-lexeme) ())
352
353
(defclass comma-at-lexeme (lisp-lexeme) ())
(defclass comma-dot-lexeme (lisp-lexeme) ())
354
(defclass dot-lexeme (lisp-lexeme) ())
355
(defclass form-lexeme (form lisp-lexeme) ())
356
(defclass incomplete-character-lexeme (form-lexeme incomplete-form-mixin) ())
357
358
359
360
361
362
363
364
365
366
(defclass character-lexeme (form-lexeme) ())
(defclass function-lexeme (lisp-lexeme) ())
(defclass line-comment-start-lexeme (lisp-lexeme) ())
(defclass long-comment-start-lexeme (lisp-lexeme) ())
(defclass comment-end-lexeme (lisp-lexeme) ())
(defclass string-start-lexeme (lisp-lexeme) ())
(defclass string-end-lexeme (lisp-lexeme) ())
(defclass word-lexeme (lisp-lexeme) ())
(defclass delimiter-lexeme (lisp-lexeme) ())
(defclass text-lexeme (lisp-lexeme) ())
367
368
(defclass sharpsign-equals-lexeme (lisp-lexeme) ())
(defclass sharpsign-sharpsign-lexeme (form-lexeme) ())
369
370
(defclass reader-conditional-positive-lexeme (lisp-lexeme) ())
(defclass reader-conditional-negative-lexeme (lisp-lexeme) ())
371
(defclass uninterned-symbol-lexeme (lisp-lexeme) ())
372
373
374
375
376
377
(defclass readtime-evaluation-lexeme (lisp-lexeme) ())
(defclass array-start-lexeme (lisp-lexeme) ())
(defclass structure-start-lexeme (lisp-lexeme) ())
(defclass pathname-start-lexeme (lisp-lexeme) ())
(defclass undefined-reader-macro-lexeme (lisp-lexeme) ())
(defclass bit-vector-lexeme (form-lexeme) ())
378
(defclass number-lexeme (form-lexeme) ())
379
380
381
382
(defclass token-mixin () ())
(defclass complete-token-lexeme (token-mixin form-lexeme) ())
(defclass multiple-escape-start-lexeme (lisp-lexeme) ())
(defclass multiple-escape-end-lexeme (lisp-lexeme) ())
383
384
(defclass incomplete-lexeme (lisp-lexeme) ())
(defclass unmatched-right-parenthesis-lexeme (lisp-lexeme) ())
385
386
387
388
389

(defmethod skip-inter ((syntax lisp-syntax) state scan)
  (macrolet ((fo () `(forward-object scan)))
    (loop when (end-of-buffer-p scan)
	    do (return nil)
390
	  until (not (whitespacep syntax (object-after scan)))
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
	  do (fo)
	  finally (return t))))

(defmethod lex :around (syntax state scan)
  (when (skip-inter syntax state scan)
    (let* ((start-offset (offset scan))
	   (lexeme (call-next-method))
	   (new-size (- (offset scan) start-offset)))
      (with-slots (start-mark size) lexeme
	 (setf (offset scan) start-offset)
	 (setf start-mark scan
	       size new-size))
      lexeme)))		  

(defmethod lex ((syntax lisp-syntax) (state lexer-toplevel-state) scan)
  (macrolet ((fo () `(forward-object scan)))
    (let ((object (object-after scan)))
      (case object
	(#\( (fo) (make-instance 'left-parenthesis-lexeme))
410
	(#\) (fo) (make-instance 'unmatched-right-parenthesis-lexeme))
411
412
413
414
415
416
417
	(#\' (fo) (make-instance 'quote-lexeme))
	(#\; (fo)
	     (loop until (or (end-of-buffer-p scan)
			     (end-of-line-p scan)
			     (not (eql (object-after scan) #\;)))
		   do (fo))
	     (make-instance 'line-comment-start-lexeme))
418
419
	(#\" (fo) (make-instance 'string-start-lexeme))
	(#\` (fo) (make-instance 'backquote-lexeme))
420
421
	(#\, (fo)
	     (cond ((end-of-buffer-p scan)
422
		    (make-instance 'incomplete-lexeme))
423
424
425
426
427
		   (t
		    (case (object-after scan)
		      (#\@ (fo) (make-instance 'comma-at-lexeme))
		      (#\. (fo) (make-instance 'comma-dot-lexeme))
		      (t (make-instance 'comma-lexeme))))))
428
	(#\# (fo)
429
	     (cond ((end-of-buffer-p scan)
430
		    (make-instance 'incomplete-lexeme))
431
		   (t 
432
433
434
435
436
437
438
		    (let ((prefix 0))
		      (loop until (end-of-buffer-p scan)
			    while (digit-char-p (object-after scan))
			    do (setf prefix
				     (+ (* 10 prefix)
					(digit-char-p (object-after scan))))
			       (fo))
439
		    (if (end-of-buffer-p scan)
440
			(make-instance 'incomplete-lexeme)
441
442
443
444
445
446
447
			(case (object-after scan)
			  ((#\Backspace #\Tab #\Newline #\Linefeed 
			    #\Page #\Return #\Space #\))
			   (fo)
			   (make-instance 'error-lexeme))
			  (#\\ (fo)
			       (cond ((end-of-buffer-p scan)
448
				      (make-instance 'incomplete-character-lexeme))
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
				     ((not (constituentp (object-after scan)))
				      (fo) (make-instance 'character-lexeme))
				     (t (loop until (end-of-buffer-p scan)
					   while (constituentp (object-after scan))
					   do (fo))
					(make-instance 'character-lexeme))))
			  (#\' (fo)
			       (make-instance 'function-lexeme))
			  (#\( (fo)
			       (make-instance 'simple-vector-start-lexeme))
			  (#\* (fo)
			       (loop until (end-of-buffer-p scan)
				     while (or (eql (object-after scan) #\1)
					       (eql (object-after scan) #\0))
				     do (fo))
			       (if (and (not (end-of-buffer-p scan))
					(constituentp (object-after scan)))
				   (make-instance 'error-lexeme)
				   (make-instance 'bit-vector-lexeme)))
			  (#\: (fo)
			       (make-instance 'uninterned-symbol-lexeme))
			  (#\. (fo)
			       (make-instance 'readtime-evaluation-lexeme))
472
473
			  ((#\B #\b #\O #\o #\X #\x)
			   (let ((radix
474
				  (ecase (object-after scan)
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
				    ((#\B #\b) 2)
				    ((#\O #\o) 8)
				    ((#\X #\x) 16))))
			     (fo)
			     (loop until (end-of-buffer-p scan)
				   while (digit-char-p (object-after scan) radix)
				   do (fo)))
			   (if (and (not (end-of-buffer-p scan))
				    (constituentp (object-after scan)))
			       (make-instance 'error-lexeme)
			       (make-instance 'number-lexeme)))
			  ((#\R #\r)
			   (fo)
			   (cond
			     ((<= 2 prefix 36)
			      (loop until (end-of-buffer-p scan)
				    while (digit-char-p (object-after scan) prefix)
				    do (fo))
			      (if (and (not (end-of-buffer-p scan))
				       (constituentp (object-after scan)))
				  (make-instance 'error-lexeme)
				  (make-instance 'number-lexeme)))
			     (t (make-instance 'error-lexeme))))
498
499
500
501
502
503
504
505
			  ;((#\C #\c) )
			  ((#\A #\a) (fo)
			   (make-instance 'array-start-lexeme))
			  ((#\S #\s) (fo)
			   (cond ((and (not (end-of-buffer-p scan))
				       (eql (object-after scan) #\())
				  (fo)
				  (make-instance 'structure-start-lexeme))
506
507
				 ((end-of-buffer-p scan)
				  (make-instance 'incomplete-lexeme))
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
				 (t (make-instance 'error-lexeme))))
			  ((#\P #\p) (fo)
			   (make-instance 'pathname-start-lexeme))
			  (#\= (fo)
			       (make-instance 'sharpsign-equals-lexeme))
			  (#\# (fo)
			       (make-instance 'sharpsign-sharpsign-lexeme))
			  (#\+ (fo)
			       (make-instance 'reader-conditional-positive-lexeme))
			  (#\- (fo)
			       (make-instance 'reader-conditional-negative-lexeme))
			  (#\| (fo)
			       (make-instance 'long-comment-start-lexeme))
			  (#\< (fo)
			       (make-instance 'error-lexeme))
523
			  (t (fo) (make-instance 'undefined-reader-macro-lexeme))))))))
524
525
526
	(#\| (fo) (make-instance 'multiple-escape-start-lexeme))
	(t (cond ((or (constituentp object)
		      (eql object #\\))
527
                  (lex-token syntax scan))
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
		 (t (fo) (make-instance 'error-lexeme))))))))

(defmethod lex ((syntax lisp-syntax) (state lexer-list-state) scan)
  (macrolet ((fo () `(forward-object scan)))
    (let ((object (object-after scan)))
      (case object
	(#\) (fo) (make-instance 'right-parenthesis-lexeme))
	(t (call-next-method))))))

(defmethod lex ((syntax lisp-syntax) (state lexer-string-state) scan)
  (macrolet ((fo () `(forward-object scan)))
    (let ((object (object-after scan)))
      (cond ((eql object #\") (fo) (make-instance 'string-end-lexeme))
	    ((eql object #\\)
	     (fo)
	     (unless (end-of-buffer-p scan)
	       (fo))
	     (make-instance 'delimiter-lexeme))
	    ((constituentp object)
	     (loop until (or (end-of-buffer-p scan)
			     (not (constituentp (object-after scan))))
		   do (fo))
	     (make-instance 'word-lexeme))
	    (t (fo) (make-instance 'delimiter-lexeme))))))

(defmethod lex ((syntax lisp-syntax) (state lexer-long-comment-state) scan)
  (flet ((fo () (forward-object scan)))
    (let ((object (object-after scan)))
      (cond ((eql object #\|)
	     (fo)
	     (cond ((or (end-of-buffer-p scan)
			(not (eql (object-after scan) #\#)))
		    (make-instance 'delimiter-lexeme))
		   (t (fo) (make-instance 'comment-end-lexeme))))
	    ((eql object #\#)
	     (fo)
	     (cond ((or (end-of-buffer-p scan)
			(not (eql (object-after scan) #\|)))
		    (make-instance 'delimiter-lexeme))
		   (t (fo) (make-instance 'long-comment-start-lexeme))))
	    ((constituentp object)
	     (loop until (or (end-of-buffer-p scan)
			     (not (constituentp (object-after scan))))
		   do (fo))
	     (make-instance 'word-lexeme))
	    (t (fo) (make-instance 'delimiter-lexeme))))))

(defmethod skip-inter ((syntax lisp-syntax) (state lexer-line-comment-state) scan)
  (macrolet ((fo () `(forward-object scan)))
    (loop until (or (end-of-line-p scan)
578
		    (not (whitespacep syntax (object-after scan))))
579
580
581
582
583
584
585
586
587
588
589
590
591
592
	  do (fo)
	  finally (return t))))

(defmethod lex ((syntax lisp-syntax) (state lexer-line-comment-state) scan)
  (macrolet ((fo () `(forward-object scan)))
    (cond ((end-of-line-p scan)
	   (make-instance 'comment-end-lexeme))
	  ((constituentp (object-after scan))
	   (loop until (or (end-of-buffer-p scan)
			   (not (constituentp (object-after scan))))
		 do (fo))
	   (make-instance 'word-lexeme))
	  (t (fo) (make-instance 'delimiter-lexeme)))))

593
594
595
596
(defun lex-token (syntax scan)
  ;; May need more work. Can recognize symbols and numbers.
  (flet ((fo () (forward-object scan)))
    (let ((could-be-number t)
597
          sign-seen dot-seen slash-seen nondot-seen)
598
599
600
      (flet ((return-token-or-number-lexeme ()
               (return-from lex-token
                 (if could-be-number
601
602
603
                     (if nondot-seen
                         (make-instance 'number-lexeme)
                         (make-instance 'dot-lexeme))
604
605
606
607
608
609
610
611
                     (make-instance 'complete-token-lexeme))))
             (this-object ()
               (object-after scan)))
        (tagbody
         START
           (when (end-of-buffer-p scan)
             (return-token-or-number-lexeme))
           (when (constituentp (object-after scan))
612
613
             (when (not (eql (this-object) #\.))
               (setf nondot-seen t))
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
             (cond ((or (eql (this-object) #\+)
                        (eql (this-object) #\-))
                    (when sign-seen
                      (setf could-be-number nil))
                    (setf sign-seen t))
                   ((eql (this-object) #\.)
                    (when dot-seen
                      (setf could-be-number nil))
                    (setf dot-seen t))
                   ((eql (this-object) #\/)
                    (when slash-seen
                      (setf could-be-number nil))
                    (setf slash-seen t))
                   ;; We obey the base specified in the file when
                   ;; determining whether or not this character is an
                   ;; integer.
                   ((not (digit-char-p (this-object)
                                       (base syntax)))
                    (setf could-be-number nil)))
             (fo)
             (go START))
           (when (eql (object-after scan) #\\)
             (fo)
             (when (end-of-buffer-p scan)
               (return-from lex-token (make-instance 'incomplete-lexeme)))
             (fo)
             (go START))
           (when (eql (object-after scan) #\|)
             (fo)
             (return-from lex-token (make-instance 'multiple-escape-start-lexeme)))
           (return-token-or-number-lexeme))))))
645
646
647
648
649
650
651

(defmethod lex ((syntax lisp-syntax) (state lexer-escaped-token-state) scan)
  (let ((bars-seen 0))
    (macrolet ((fo () `(forward-object scan)))
      (tagbody
       start
	 (when (end-of-buffer-p scan)
652
	   (return-from lex (make-instance 'text-lexeme)))
653
654
655
	 (when (eql (object-after scan) #\\)
	   (fo)
	   (when (end-of-buffer-p scan)
656
	     (return-from lex (make-instance 'incomplete-lexeme)))
657
658
659
660
	   (fo)
	   (go start))
	 (when (eql (object-after scan) #\|)
	   (incf bars-seen)
661
	   (fo)
662
	   (go start))
663
         (if (evenp bars-seen)
664
             (unless (whitespacep syntax (object-after scan))
665
666
667
668
669
               (fo)
               (go start))
             (when (constituentp (object-after scan))
               (fo)
               (go start)))
670
671
672
673
	 (return-from lex 
	   (if (oddp bars-seen)
	       (make-instance 'multiple-escape-end-lexeme)
	       (make-instance 'text-lexeme)))))))
674

675
676
677
678
679
680
(defmethod lex ((syntax lisp-syntax) (state lexer-error-state) scan)
  (macrolet ((fo () `(forward-object scan)))
    (loop until (end-of-line-p scan)
	  do (fo))
    (make-instance 'error-lexeme)))

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

(defclass line-comment (lisp-nonterminal) ())
(defclass long-comment (lisp-nonterminal) ())  
(defclass error-symbol (lisp-nonterminal) ())

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

(defmacro define-lisp-action ((state lexeme) &body body)
  `(defmethod action ((syntax lisp-syntax) (state ,state) (lexeme ,lexeme))
     ,@body))

(defmacro define-new-lisp-state ((state parser-symbol) &body body)
  `(defmethod new-state ((syntax lisp-syntax) (state ,state) (tree ,parser-symbol))
     ,@body))

(defun pop-one (syntax)
  (with-slots (stack-top current-state) syntax
     (with-slots (preceding-parse-tree parser-state) stack-top
	(prog1 stack-top
	       (setf current-state parser-state
		     stack-top preceding-parse-tree)))))

(defun pop-number (syntax how-many)
  (loop with result = '()
	repeat how-many
	do (push (pop-one syntax) result)
	finally (return result)))

714
715
716
717
718
719
720
721
722
(defmacro reduce-fixed-number (symbol nb-children)
  `(let ((result (make-instance ',symbol :children (pop-number syntax ,nb-children))))
     (when (zerop ,nb-children)
       (with-slots (scan) syntax
	  (with-slots (start-mark size) result
	     (setf start-mark (clone-mark scan :right)
		   size 0))))
     result))

723
724
725
726
727
728
729
730
(defun pop-until-type (syntax type)
  (with-slots (stack-top) syntax
     (loop with result = '()
	   for child = stack-top
	   do (push (pop-one syntax) result)
	   until (typep child type)
	   finally (return result))))

731
732
733
734
735
736
737
738
739
740
(defmacro reduce-until-type (symbol type)
  `(let ((result (make-instance ',symbol
		    :children (pop-until-type syntax ',type))))
     (when (null (children result))
       (with-slots (scan) syntax
	  (with-slots (start-mark size) result
	     (setf start-mark (clone-mark scan :right)
		   size 0))))
     result))

741
742
743
744
745
746
747
(defun pop-all (syntax)
  (with-slots (stack-top) syntax
     (loop with result = '()
	   until (null stack-top)
	   do (push (pop-one syntax) result)
	   finally (return result))))

748
749
750
751
752
753
754
755
756
(defmacro reduce-all (symbol)
  `(let ((result (make-instance ',symbol :children (pop-all syntax))))
     (when (null (children result))
       (with-slots (scan) syntax
	  (with-slots (start-mark size) result
	     (setf start-mark (clone-mark scan :right)
		   size 0))))
     result))     

757
(define-parser-state error-state (lexer-error-state parser-state) ())
758
759
760
761
762
763
764
765
766
767
768
(define-parser-state error-reduce-state (lexer-toplevel-state parser-state) ())

(define-lisp-action (error-reduce-state (eql nil))
  (throw 'done nil)) 

;;; the default action for any lexeme is shift
(define-lisp-action (t lisp-lexeme)
  lexeme)

;;; the action on end-of-buffer is to reduce to the error symbol
(define-lisp-action (t (eql nil))
769
  (reduce-all error-symbol))
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789

;;; the default new state is the error state
(define-new-lisp-state (t parser-symbol) error-state)

;;; the new state when an error-state 
(define-new-lisp-state (t error-symbol) error-reduce-state)


;;;;;;;;;;;;;;;; Top-level 

#| rules
   form* -> 
   form* -> form* form
|#

;;; parse trees
(defclass form* (lisp-nonterminal) ())

(define-parser-state |form* | (lexer-toplevel-state parser-state) ())
(define-parser-state form-may-follow (lexer-toplevel-state parser-state) ())
790
(define-parser-state |initial-state | (form-may-follow) ())
791

792
(define-new-lisp-state (|initial-state | form) |initial-state |)
793
(define-new-lisp-state (|initial-state | comment) |initial-state |)
794
795
;; skip over unmatched right parentheses
(define-new-lisp-state (|initial-state | unmatched-right-parenthesis-lexeme) |initial-state |)
796

797
(define-lisp-action (|initial-state | (eql nil))
798
  (reduce-all form*))
799

800
(define-new-lisp-state (|initial-state | form*) |form* | )
801
802
803
804
805
806
807
808
809
810
811
812
  
(define-lisp-action (|form* | (eql nil))
  (throw 'done nil))

;;;;;;;;;;;;;;;; List

#| rules
   form -> ( form* )
|#

;;; parse trees
(defclass list-form (form) ())
813
814
(defclass complete-list-form (list-form) ())
(defclass incomplete-list-form (list-form incomplete-form-mixin) ())
815
816
817
818
819
820

(define-parser-state |( form* | (lexer-list-state form-may-follow) ())
(define-parser-state |( form* ) | (lexer-toplevel-state parser-state) ())

(define-new-lisp-state (form-may-follow left-parenthesis-lexeme) |( form* |)
(define-new-lisp-state (|( form* | form) |( form* |)
821
(define-new-lisp-state (|( form* | comment) |( form* |)
822
823
824
825
(define-new-lisp-state (|( form* | right-parenthesis-lexeme) |( form* ) |)

;;; reduce according to the rule form -> ( form* )
(define-lisp-action (|( form* ) | t)
826
827
828
829
830
  (reduce-until-type complete-list-form left-parenthesis-lexeme))

;;; reduce at the end of the buffer
(define-lisp-action (|( form* | (eql nil))
  (reduce-until-type incomplete-list-form left-parenthesis-lexeme))
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
;;;;;;;;;;;;;;;; Cons cell
;; Also (foo bar baz . quux) constructs.
;; (foo bar . baz quux) flagged as an error (too aggressively?).

;;; parse trees
(defclass cons-cell-form (form) ())
(defclass complete-cons-cell-form (cons-cell-form complete-list-form) ())
(defclass incomplete-cons-cell-form (cons-cell-form incomplete-list-form) ())

(define-parser-state |( form* dot-lexeme |
    (lexer-list-state form-may-follow) ())
(define-parser-state |( form* dot-lexeme form |
    (lexer-list-state form-may-follow) ())
(define-parser-state |( form* dot-lexeme form ) |
    (lexer-toplevel-state parser-state) ())

(define-new-lisp-state (|( form* | dot-lexeme)
  |( form* dot-lexeme |)
(define-new-lisp-state (|( form* dot-lexeme | form)
  |( form* dot-lexeme form |)
(define-new-lisp-state (|( form* dot-lexeme | comment)
  |( form* dot-lexeme |)
(define-new-lisp-state (|( form* dot-lexeme form | right-parenthesis-lexeme)
  |( form* dot-lexeme form ) |)
(define-new-lisp-state (|( form* dot-lexeme form | comment)
  |( form* dot-lexeme form |)

(define-lisp-action (|( form* dot-lexeme form ) | t)
  (reduce-until-type complete-cons-cell-form left-parenthesis-lexeme))

;;; Reduce at end of buffer.
(define-lisp-action (|( form* dot-lexeme | (eql nil))
  (reduce-until-type incomplete-cons-cell-form left-parenthesis-lexeme))
(define-lisp-action (|( form* dot-lexeme form | (eql nil))
  (reduce-until-type incomplete-cons-cell-form left-parenthesis-lexeme))

868
869
870
871
;;;;;;;;;;;;;;;; Simple Vector

;;; parse trees
(defclass simple-vector-form (list-form) ())
872
873
(defclass complete-simple-vector-form (complete-list-form simple-vector-form) ())
(defclass incomplete-simple-vector-form (incomplete-list-form simple-vector-form) ())
874
875
876
877
878
879

(define-parser-state |#( form* | (lexer-list-state form-may-follow) ())
(define-parser-state |#( form* ) | (lexer-toplevel-state parser-state) ())

(define-new-lisp-state (form-may-follow simple-vector-start-lexeme) |#( form* |)
(define-new-lisp-state (|#( form* | form) |#( form* |)
880
(define-new-lisp-state (|#( form* | comment) |#( form* |)
881
882
883
884
885
886
887
888
889
890
(define-new-lisp-state (|#( form* | right-parenthesis-lexeme) |#( form* ) |)

;;; reduce according to the rule form -> #( form* )
(define-lisp-action (|#( form* ) | t)
  (reduce-until-type complete-simple-vector-form simple-vector-start-lexeme))

;;; reduce at the end of the buffer
(define-lisp-action (|#( form* | (eql nil))
  (reduce-until-type incomplete-simple-vector-form simple-vector-start-lexeme))

891
892
893
894
;;;;;;;;;;;;;;;; String

;;; parse trees
(defclass string-form (form) ())
895
896
(defclass complete-string-form (string-form) ())
(defclass incomplete-string-form (string-form incomplete-form-mixin) ())
897
898
899
900
901
902
903
904
905
906
907

(define-parser-state |" word* | (lexer-string-state parser-state) ())
(define-parser-state |" word* " | (lexer-toplevel-state parser-state) ())

(define-new-lisp-state (|" word* | word-lexeme) |" word* |)
(define-new-lisp-state (|" word* | delimiter-lexeme) |" word* |)
(define-new-lisp-state (form-may-follow string-start-lexeme) |" word* |)
(define-new-lisp-state (|" word* | string-end-lexeme) |" word* " |)

;;; reduce according to the rule form -> " word* "
(define-lisp-action (|" word* " | t)
908
909
910
911
912
  (reduce-until-type complete-string-form string-start-lexeme))

;;; reduce at the end of the buffer 
(define-lisp-action (|" word* | (eql nil))
  (reduce-until-type incomplete-string-form string-start-lexeme))
913
914
915
916

;;;;;;;;;;;;;;;; Line comment

;;; parse trees
917
(defclass line-comment-form (comment) ())
918
919
920
921
922
923
924
925
926
927
928

(define-parser-state |; word* | (lexer-line-comment-state parser-state) ())
(define-parser-state |; word* NL | (lexer-toplevel-state parser-state) ())

(define-new-lisp-state (form-may-follow line-comment-start-lexeme) |; word* |)
(define-new-lisp-state (|; word* | word-lexeme) |; word* |)
(define-new-lisp-state (|; word* | delimiter-lexeme) |; word* |)
(define-new-lisp-state (|; word* | comment-end-lexeme) |; word* NL |)

;;; reduce according to the rule form -> ; word* NL
(define-lisp-action (|; word* NL | t)
929
  (reduce-until-type line-comment-form line-comment-start-lexeme))
930
931
932
933

;;;;;;;;;;;;;;;; Long comment

;;; parse trees
934
(defclass long-comment-form (comment) ())
935
936
(defclass complete-long-comment-form (long-comment-form) ())
(defclass incomplete-long-comment-form (long-comment-form incomplete-form-mixin) ())
937
938
939
940
941
942
943
944
945
946
947
948
949

(define-parser-state |#\| word* | (lexer-long-comment-state parser-state) ())
(define-parser-state |#\| word* \|# | (lexer-toplevel-state parser-state) ())

(define-new-lisp-state (|#\| word* | word-lexeme) |#\| word* |)
(define-new-lisp-state (|#\| word* | delimiter-lexeme) |#\| word* |)
(define-new-lisp-state (|#\| word* | long-comment-start-lexeme) |#\| word* |)
(define-new-lisp-state (|#\| word* | long-comment-form) |#\| word* |)
(define-new-lisp-state (form-may-follow long-comment-start-lexeme) |#\| word* |)
(define-new-lisp-state (|#\| word* | comment-end-lexeme) |#\| word* \|# |)

;;; reduce according to the rule form -> #| word* |#
(define-lisp-action (|#\| word* \|# | t)
950
951
952
953
954
  (reduce-until-type complete-long-comment-form long-comment-start-lexeme))

;;; reduce at the end of the buffer
(define-lisp-action (|#\| word* | (eql nil))
  (reduce-until-type incomplete-long-comment-form long-comment-start-lexeme))
955

956
;;;;;;;;;;;;;;;; Token (number or symbol)
957
958

;;; parse trees
959
960
961
(defclass token-form (form token-mixin) ())
(defclass complete-token-form (token-form) ())
(defclass incomplete-token-form (token-form) ())
962

963
964
(define-parser-state | m-e-start text* | (lexer-escaped-token-state parser-state) ())
(define-parser-state | m-e-start text* m-e-end | (lexer-toplevel-state parser-state) ())
965

966
967
968
(define-new-lisp-state (form-may-follow multiple-escape-start-lexeme) | m-e-start text* |)
(define-new-lisp-state (| m-e-start text* | text-lexeme) | m-e-start text* |)
(define-new-lisp-state (| m-e-start text* | multiple-escape-end-lexeme) | m-e-start text* m-e-end |)
969

970
971
972
;;; reduce according to the rule form -> m-e-start text* m-e-end
(define-lisp-action (| m-e-start text* m-e-end | t)
  (reduce-until-type complete-token-form multiple-escape-start-lexeme))
973
974

;;; reduce at the end of the buffer
975
976
(define-lisp-action (| m-e-start text* | (eql nil))
  (reduce-until-type incomplete-token-form multiple-escape-start-lexeme))
977
978
979
980
981
982
983
984
985
986
987

;;;;;;;;;;;;;;;; Quote

;;; parse trees
(defclass quote-form (form) ())

(define-parser-state |' | (form-may-follow) ())
(define-parser-state |' form | (lexer-toplevel-state parser-state) ())

(define-new-lisp-state (form-may-follow quote-lexeme) |' |)
(define-new-lisp-state (|' | form) |' form |)
988
989
(define-new-lisp-state (|' | comment) |' |)

990
991
992

;;; reduce according to the rule form -> ' form
(define-lisp-action (|' form | t)
993
  (reduce-until-type quote-form quote-lexeme))
994
995
996
997
998
999
1000
1001
1002
1003
1004

;;;;;;;;;;;;;;;; Backquote

;;; parse trees
(defclass backquote-form (form) ())

(define-parser-state |` | (form-may-follow) ())
(define-parser-state |` form | (lexer-toplevel-state parser-state) ())

(define-new-lisp-state (form-may-follow backquote-lexeme) |` |)
(define-new-lisp-state (|` | form) |` form |)
1005
(define-new-lisp-state (|` | comment) |` |)
1006
1007
1008

;;; reduce according to the rule form -> ` form
(define-lisp-action (|` form | t)
1009
  (reduce-until-type backquote-form backquote-lexeme))
1010
1011
1012
1013
1014

;;;;;;;;;;;;;;;; Comma

;;; parse trees
(defclass comma-form (form) ())
1015
1016
(defclass comma-at-form (form) ())
(defclass comma-dot-form (form) ())
1017
1018
1019

(define-parser-state |, | (form-may-follow) ())
(define-parser-state |, form | (lexer-toplevel-state parser-state) ())
1020
1021
1022
1023
(define-parser-state |,@ | (form-may-follow) ())
(define-parser-state |,@ form | (lexer-toplevel-state parser-state) ())
(define-parser-state |,. | (form-may-follow) ())
(define-parser-state |,. form | (lexer-toplevel-state parser-state) ())
1024
1025

(define-new-lisp-state (form-may-follow comma-lexeme) |, |)
1026
1027
(define-new-lisp-state (form-may-follow comma-at-lexeme) |,@ |)
(define-new-lisp-state (form-may-follow comma-dot-lexeme) |,. |)
1028
(define-new-lisp-state (|, | form) |, form |)
1029
1030
1031
1032
1033
(define-new-lisp-state (|, | comment) |, |)
(define-new-lisp-state (|,@ | form) |,@ form |)
(define-new-lisp-state (|,@ | comment) |,@ |)
(define-new-lisp-state (|,. | form) |,. form |)
(define-new-lisp-state (|,. | comment) |,. |)
1034
1035
1036

;;; reduce according to the rule form -> , form
(define-lisp-action (|, form | t)
1037
1038
1039
1040
1041
  (reduce-until-type comma-form comma-lexeme))
(define-lisp-action (|,@ form | t)
  (reduce-until-type comma-at-form comma-at-lexeme))
(define-lisp-action (|,. form | t)
  (reduce-until-type comma-dot-form comma-dot-lexeme))
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052

;;;;;;;;;;;;;;;; Function

;;; parse trees
(defclass function-form (form) ())

(define-parser-state |#' | (form-may-follow) ())
(define-parser-state |#' form | (lexer-toplevel-state parser-state) ())

(define-new-lisp-state (form-may-follow function-lexeme) |#' |)
(define-new-lisp-state (|#' | form) |#' form |)
1053
(define-new-lisp-state (|#' | comment) |#' |)
1054
1055
1056

;;; reduce according to the rule form -> #' form
(define-lisp-action (|#' form | t)
1057
  (reduce-until-type function-form function-lexeme))
1058
1059
1060
1061

;;;;;;;;;;;;;;;; Reader conditionals

;;; parse trees
1062
1063
1064
(defclass reader-conditional-form (form) ())
(defclass reader-conditional-positive-form (reader-conditional-form) ())
(defclass reader-conditional-negative-form (reader-conditional-form) ())
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075

(define-parser-state |#+ | (form-may-follow) ())
(define-parser-state |#+ form | (form-may-follow) ())
(define-parser-state |#+ form form | (lexer-toplevel-state parser-state) ())
(define-parser-state |#- | (form-may-follow) ())
(define-parser-state |#- form | (form-may-follow) ())
(define-parser-state |#- form form | (lexer-toplevel-state parser-state) ())

(define-new-lisp-state (form-may-follow reader-conditional-positive-lexeme) |#+ |)
(define-new-lisp-state (|#+ | form) |#+ form |)
(define-new-lisp-state (|#+ form | form) |#+ form form |)
1076
1077
(define-new-lisp-state (|#+ | comment) |#+ |)
(define-new-lisp-state (|#+ form | comment) |#+ form |)
1078
1079
1080
(define-new-lisp-state (form-may-follow reader-conditional-negative-lexeme) |#- |)
(define-new-lisp-state (|#- | form) |#- form |)
(define-new-lisp-state (|#- form | form) |#- form form |)
1081
1082
(define-new-lisp-state (|#- | comment) |#- |)
(define-new-lisp-state (|#- form | comment) |#- form |)
1083
1084
  
(define-lisp-action (|#+ form form | t)
1085
  (reduce-until-type reader-conditional-positive-form reader-conditional-positive-lexeme))
1086
1087

(define-lisp-action (|#- form form | t)
1088
  (reduce-until-type reader-conditional-negative-form reader-conditional-negative-lexeme))
1089

1090
1091
1092
1093
1094
1095
1096
1097
;;;;;;;;;;;;;;;; uninterned symbol

;;; parse trees
(defclass uninterned-symbol-form (form) ())

(define-parser-state |#: | (form-may-follow) ())
(define-parser-state |#: form | (lexer-toplevel-state parser-state) ())

1098
(define-new-lisp-state (form-may-follow uninterned-symbol-lexeme) |#: |)
1099
1100
(define-new-lisp-state (|#: | form) |#: form |)

1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
;;; reduce according to the rule form -> #: form
(define-lisp-action (|#: form | t)
  (reduce-fixed-number uninterned-symbol-form 2))

;;;;;;;;;;;;;;;; readtime evaluation

;;; parse trees
(defclass readtime-evaluation-form (form) ())

(define-parser-state |#. | (form-may-follow) ())
(define-parser-state |#. form | (lexer-toplevel-state parser-state) ())

1113
(define-new-lisp-state (form-may-follow readtime-evaluation-lexeme) |#. |)
1114
(define-new-lisp-state (|#. | form) |#. form |)
1115
(define-new-lisp-state (|#. | comment) |#. |)
1116
1117
1118

;;; reduce according to the rule form -> #. form
(define-lisp-action (|#. form | t)
1119
  (reduce-until-type readtime-evaluation-form readtime-evaluation-lexeme))
1120
1121
1122
1123
1124
1125
1126
1127
1128

;;;;;;;;;;;;;;;; sharpsign equals

;;; parse trees
(defclass sharpsign-equals-form (form) ())

(define-parser-state |#= | (form-may-follow) ())
(define-parser-state |#= form | (lexer-toplevel-state parser-state) ())

1129
(define-new-lisp-state (form-may-follow sharpsign-equals-lexeme) |#= |)
1130
(define-new-lisp-state (|#= | form) |#= form |)
1131
(define-new-lisp-state (|#= | comment) |#= |)
1132
1133
1134

;;; reduce according to the rule form -> #= form
(define-lisp-action (|#= form | t)
1135
  (reduce-until-type sharpsign-equals-form sharpsign-equals-lexeme))
1136
1137
1138
1139
1140
1141
1142
1143
1144

;;;;;;;;;;;;;;;; array

;;; parse trees
(defclass array-form (form) ())

(define-parser-state |#A | (form-may-follow) ())
(define-parser-state |#A form | (lexer-toplevel-state parser-state) ())

1145
(define-new-lisp-state (form-may-follow array-start-lexeme) |#A |)
1146
(define-new-lisp-state (|#A | form) |#A form |)
1147
(define-new-lisp-state (|#A | comment) |#A |)
1148
1149
1150

;;; reduce according to the rule form -> #A form
(define-lisp-action (|#A form | t)
1151
  (reduce-until-type array-form array-start-lexeme))
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177

;;;;;;;;;;;;;;;; structure

;;; parse trees
(defclass structure-form (list-form) ())
(defclass complete-structure-form (complete-list-form) ())
(defclass incomplete-structure-form (incomplete-list-form) ())

(define-parser-state |#S( form* | (lexer-list-state form-may-follow) ())
(define-parser-state |#S( form* ) | (lexer-toplevel-state parser-state) ())

(define-new-lisp-state (form-may-follow structure-start-lexeme) |#S( form* |)
(define-new-lisp-state (|#S( form* | form) |#S( form* |)
(define-new-lisp-state (|#S( form* | right-parenthesis-lexeme) |#S( form* ) |)

;;; reduce according to the rule form -> #S( form* )
(define-lisp-action (|#S( form* ) | t)
  (reduce-until-type complete-structure-form structure-start-lexeme))

;;; reduce at the end of the buffer
(define-lisp-action (|#S( form* | (eql nil))
  (reduce-until-type incomplete-structure-form structure-start-lexeme))


;;;;;;;;;;;;;;;; pathname

David Murray's avatar
David Murray committed
1178
1179
;;; NB: #P need not be followed by a string,
;;;  as it could be followed by a #. construct instead (or some other reader macro)
1180
1181
1182
1183
1184
1185
1186

;;; parse trees
(defclass pathname-form (form) ())

(define-parser-state |#P | (form-may-follow) ())
(define-parser-state |#P form | (lexer-toplevel-state parser-state) ())

1187
(define-new-lisp-state (form-may-follow pathname-start-lexeme) |#P |)
1188
(define-new-lisp-state (|#P | form) |#P form |)
1189
(define-new-lisp-state (|#P | comment) |#P |)
1190
1191
1192

;;; reduce according to the rule form -> #P form
(define-lisp-action (|#P form | t)
1193
  (reduce-until-type pathname-form pathname-start-lexeme))
1194
1195
1196
1197
1198
1199
1200
1201
1202

;;;;;;;;;;;;;;;; undefined reader macro

;;; parse trees
(defclass undefined-reader-macro-form (form) ())

(define-parser-state |#<other> | (form-may-follow) ())
(define-parser-state |#<other> form | (lexer-toplevel-state parser-state) ())

1203
(define-new-lisp-state (form-may-follow undefined-reader-macro-lexeme) |#<other> |)
1204
1205
(define-new-lisp-state (|#<other> | form) |#<other> form |)

1206
1207
1208
;;; reduce according to the rule form -> #<other> form
(define-lisp-action (|#<other> form | t)
  (reduce-fixed-number undefined-reader-macro-form 2))
1209
1210


1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; parser step

(defgeneric parser-step (syntax))

(defmethod parser-step ((syntax lisp-syntax))
  (with-slots (lookahead-lexeme stack-top current-state scan) syntax
     (setf lookahead-lexeme (lex syntax current-state (clone-mark scan :right)))
     (let* ((new-parser-symbol (action syntax current-state lookahead-lexeme))
	    (new-state (new-state syntax current-state new-parser-symbol)))
       (with-slots (parser-state parser-symbol preceding-parse-tree children) new-parser-symbol
	  (setf parser-state current-state
		current-state new-state
		preceding-parse-tree stack-top
1226
		stack-top new-parser-symbol)))
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
     (setf (offset scan) (end-offset stack-top))))

(defun prev-tree (tree)
  (assert (not (null tree)))
  (if (null (children tree))
      (preceding-parse-tree tree)
      (car (last (children tree)))))

(defun next-tree (tree)
  (assert (not (null tree)))
  (if (null (parent tree))
      nil
      (let* ((parent (parent tree))
	     (siblings (children parent)))
	(cond ((null parent) nil)
	      ((eq tree (car (last siblings))) parent)
	      (t (loop with new-tree = (cadr (member tree siblings :test #'eq))
		       until (null (children new-tree))
		       do (setf new-tree (car (children new-tree)))
		       finally (return new-tree)))))))	 

(defun find-last-valid-lexeme (parse-tree offset)
  (cond ((or (null parse-tree) (null (start-offset parse-tree))) nil)
	((> (start-offset parse-tree) offset)
	 (find-last-valid-lexeme (preceding-parse-tree parse-tree) offset))
	((not (typep parse-tree 'lexeme))
	 (find-last-valid-lexeme (car (last (children parse-tree))) offset))
	((>= (end-offset parse-tree) offset)
	 (find-last-valid-lexeme (preceding-parse-tree parse-tree) offset))
	(t parse-tree)))  

(defun find-first-potentially-valid-lexeme (parse-trees offset)
  (cond ((null parse-trees) nil)
	((or (null (start-offset (car parse-trees)))
	     (< (end-offset (car parse-trees)) offset))
	 (find-first-potentially-valid-lexeme (cdr parse-trees) offset))
	((not (typep (car parse-trees) 'lexeme))
	 (find-first-potentially-valid-lexeme (children (car parse-trees)) offset))
1265
	((<= (start-offset (car parse-trees)) offset)
1266
	 (loop with tree = (next-tree (car parse-trees))
1267
	       until (or (null tree) (> (start-offset tree) offset))
1268
1269
1270
1271
1272
1273
1274
1275
	       do (setf tree (next-tree tree))
	       finally (return tree)))
	(t (car parse-trees))))

(defun parse-tree-equal (tree1 tree2)
  (and (eq (class-of tree1) (class-of tree2))
       (eq (parser-state tree1) (parser-state tree2))
       (= (end-offset tree1) (end-offset tree2))))
1276
  
1277
1278
1279
1280
(defmethod print-object ((mark mark) stream)
  (print-unreadable-object (mark stream :type t :identity t)
    (format stream "~s" (offset mark))))

1281
1282
(defun parse-patch (syntax)
  (with-slots (current-state stack-top scan potentially-valid-trees) syntax
1283
1284
     (parser-step syntax)
     (finish-output *trace-output*)
1285
     (cond ((parse-tree-equal stack-top potentially-valid-trees)
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
	    (unless (or (null (parent potentially-valid-trees))
			(eq potentially-valid-trees
			    (car (last (children (parent potentially-valid-trees))))))
	      (loop for tree = (cadr (member potentially-valid-trees
					     (children (parent potentially-valid-trees))
					     :test #'eq))
		      then (car (children tree))
		    until (null tree)
		    do (setf (slot-value tree 'preceding-parse-tree)
			     stack-top))
	      (setf stack-top (prev-tree (parent potentially-valid-trees))))
1297
1298
	    (setf potentially-valid-trees (parent potentially-valid-trees))
	    (setf current-state (new-state syntax (parser-state stack-top) stack-top))
1299
	    (setf (offset scan) (end-offset stack-top)))
1300
1301
1302
1303
	   (t (loop until (or (null potentially-valid-trees)
			      (>= (start-offset potentially-valid-trees)
				  (end-offset stack-top)))
		    do (setf potentially-valid-trees
1304
			     (next-tree potentially-valid-trees)))))))	    
1305
1306
1307
1308
1309
1310
1311
1312

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; update syntax

(defmethod update-syntax-for-display (buffer (syntax lisp-syntax) top bot)
  nil)

1313
1314
1315
1316
1317
1318
1319
(defun package-at-mark (syntax mark-or-offset)
  "Get the specified Lisp package for the syntax. First, an
attempt will be made to find the package specified in
the (in-package) preceding `mark-or-offset'. If none can be
found, return the package specified in the attribute list. If no
package can be found at all, or the otherwise found packages are
invalid, return the CLIM-USER package."
1320
1321
1322
1323
1324
1325
  (as-offsets ((mark-or-offset offset))
   (let* ((designator (rest (find offset (package-list syntax)
                                  :key #'first
                                  :test #'>=))))
     (or (handler-case (find-package designator)
           (type-error ()
1326
             nil))
1327
1328
         (find-package (option-specified-package syntax))
         (find-package :clim-user)))))
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339

(defmacro with-syntax-package (syntax offset (package-sym) &body
                               body)
  "Evaluate `body' with `package-sym' bound to a valid package,
  preferably taken from `syntax' based on `offset'.."
  `(let ((,package-sym (package-at-mark ,syntax ,offset)))
     ,@body))

(defun need-to-update-package-list-p (buffer syntax)
  (let ((low-mark-offset (offset (low-mark buffer)))
        (high-mark-offset (offset (high-mark buffer))))
1340
    (flet ((test (x)
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
             (let ((start-offset (start-offset x))
                   (end-offset (end-offset x)))
              (when (and (or (<= start-offset
                                 low-mark-offset
                                 end-offset
                                 high-mark-offset)
                             (<= low-mark-offset
                                 start-offset
                                 high-mark-offset
                                 end-offset)
                             (<= low-mark-offset
                                 start-offset
                                 end-offset
                                 high-mark-offset)
                             (<= start-offset
                                 low-mark-offset
                                 high-mark-offset
                                 end-offset))
                         (typep x 'complete-list-form))
                (let ((candidate (first-form (children x))))
                  (and (typep candidate 'token-mixin)
                       (eq (token-to-object syntax candidate
                                            :no-error t)
                           'cl:in-package)))))))
1365
      (with-slots (stack-top) syntax
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
        (or (not (slot-boundp syntax 'package-list))
            (loop for child in (children stack-top)
               when (test child)
               do (return t)))))))

(defun update-package-list (buffer syntax)
  (declare (ignore buffer))
  (setf (package-list syntax) nil)
  (flet ((test (x)
           (when (typep x 'complete-list-form)
             (let ((candidate (first-form (children x))))
               (and (typep candidate 'token-mixin)
                    (eq (token-to-object syntax candidate
                                         :no-error t)
                        'cl:in-package)))))
         (extract (x)
           (let ((designator (second-form (children x))))
             (token-to-object syntax designator
                              :no-error t))))
    (with-slots (stack-top) syntax
      (loop for child in (children stack-top)
         when (test child)
         do (push (cons (end-offset child)
                        (extract child))
                  (package-list syntax))))))
1391

1392
1393
1394
1395
1396
1397
(defmethod update-syntax (buffer (syntax lisp-syntax))
  (let* ((low-mark (low-mark buffer))
	 (high-mark (high-mark buffer)))
    (when (mark<= low-mark high-mark)
      (catch 'done
	(with-slots (current-state stack-top scan potentially-valid-trees) syntax
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
          (setf potentially-valid-trees
                (if (null stack-top)
                    nil
                    (find-first-potentially-valid-lexeme (children stack-top)
                                                         (offset high-mark))))
          (setf stack-top (find-last-valid-lexeme stack-top (offset low-mark)))
          (setf (offset scan) (if (null stack-top) 0 (end-offset stack-top))
                current-state (if (null stack-top)
                                  |initial-state |
                                  (new-state syntax
                                             (parser-state stack-top)
                                             stack-top)))
          (loop do (parse-patch syntax))))))
  (when (need-to-update-package-list-p buffer syntax)
    (update-package-list buffer syntax)))
1413

1414
1415
1416
1417
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; accessing parser forms

1418
(defun first-noncomment (list)
1419
1420
1421
  "Returns the first non-comment in list."
  (find-if-not #'(lambda (item) (typep item 'comment)) list))

1422
(defun rest-noncomments (list)
1423
1424
1425
1426
1427
1428
1429
1430
  "Returns the remainder of the list after the first non-comment,
stripping leading comments."
  (loop for rest on list
	count (not (typep (car rest) 'comment))
	  into forms
	until (= forms 2)
	finally (return rest)))

1431
(defun nth-noncomment (n list)
1432
1433
1434
1435
  "Returns the nth non-comment in list."
  (loop for item in list
	count (not (typep item 'comment))
	  into forms
1436
	until (> forms n)
1437
1438
	finally (return item)))

1439
(defun elt-noncomment (list n)
1440
  "Returns the nth non-comment in list."
1441
  (nth-noncomment n list))
1442

1443
(defun second-noncomment (list)
1444
  "Returns the second non-comment in list."
1445
  (nth-noncomment 1 list))
1446

1447
(defun third-noncomment (list)
1448
  "Returns the third non-comment in list."
1449
  (nth-noncomment 2 list))
1450

1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
(defun rest-forms (list)
  "Returns the remainder of the list after the first form,
stripping leading non-forms."
  (loop for rest on list
     count (typep (car rest) 'form)
       into forms
     until (= forms 2)
     finally (return rest)))

(defun nth-form (n list)
1461
  "Returns the nth form in list or `nil'."
1462
1463
1464
1465
  (loop for item in list
     count (typep item 'form)
       into forms
     until (> forms n)
1466
1467
     finally (when (> forms n)
               (return item))))
1468
1469

(defun elt-form (list n)
1470
  "Returns the nth form in list or `nil'."
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
  (nth-form n list))

(defun first-form (list)
  "Returns the first form in list."
  (nth-form 0 list))

(defun second-form (list)
  "Returns the second form in list."
  (nth-form 1 list))

(defun third-form (list)
  "Returns the third formw in list."
  (nth-form 2 list))

1485
1486
1487
1488
1489
1490
1491
1492
(defgeneric form-operator (form syntax)
  (:documentation "Return the operator of `form' as a Lisp
object. Returns nil if none can be found.")
  (:method (form syntax) nil))

(defmethod form-operator ((form list-form) syntax)
  (let* ((operator-token (first-form (rest (children form))))
         (operator-symbol (when operator-token
Troels Henriksen's avatar
Troels Henriksen committed
1493
                            (token-to-object syntax operator-token :no-error t))))
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
    operator-symbol))

(defgeneric form-operands (form syntax)
  (:documentation "Returns the operands of `form' as a list of
  Lisp objects. Returns nil if none can be found.")
  (:method (form syntax) nil))

(defmethod form-operands ((form list-form) syntax)
  (mapcar #'(lambda (operand)
              (if (typep operand 'form)
1504
                  (token-to-object syntax operand :no-error t)))
1505
1506
1507
1508
1509
1510
1511
1512
          (rest-forms (children form))))

(defun form-toplevel (form syntax)
  "Return the top-level form of `form'."
  (if (null (parent (parent form)))
      form
      (form-toplevel (parent form) syntax)))

1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
(defgeneric operator-p (token syntax)
  (:documentation "Return true if `token' is the operator of its form. Otherwise,
  return nil.")
  (:method (token syntax)
    (with-accessors ((pre-token preceding-parse-tree)) token
      (cond ((typep pre-token 'left-parenthesis-lexeme)
             t)
            ((typep pre-token 'comment)
             (operator-p pre-token syntax))
            (t nil)))))

1524
1525
1526
1527
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Useful functions for selecting forms based on the mark.

1528
1529
(defun expression-at-mark (mark-or-offset syntax)
  "Return the form at `mark-or-offset'. If `mark-or-offset' is just after,
1530
or inside, a top-level-form, or if there are no forms after
1531
1532
`mark-or-offset', the form preceding `mark-or-offset' is
returned. Otherwise, the form following `mark-or-offset' is
1533
returned."
1534
1535
1536
1537
  (as-offsets ((mark-or-offset offset))
   (or (form-around syntax offset)
       (form-after syntax offset)
       (form-before syntax offset))))
1538

1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
(defun definition-at-mark (mark-or-offset syntax)
  "Return the top-level form at `mark-or-offset'. If `mark-or-offset' is just after,
or inside, a top-level-form, or if there are no forms after
`mark-or-offset', the top-level-form preceding `mark-or-offset'
is returned. Otherwise, the top-level-form following
`mark-or-offset' is returned."
  (form-toplevel (expression-at-mark mark-or-offset syntax) syntax))

(defun symbol-at-mark (mark-or-offset syntax)
  "Return a symbol token at `mark-or-offset'. This function will
  \"unwrap\" quote-forms in order to return the symbol token. If
  no symbol token can be found, NIL will be returned."
1551
1552
1553
1554
1555
  (labels ((unwrap-form (form)
             (cond ((typep form 'quote-form)
                    (unwrap-form (first-form (children form))))
                   ((typep form 'complete-token-lexeme)
                    form))))
1556
    (unwrap-form (expression-at-mark mark-or-offset syntax))))
1557

1558
1559
(defun this-form (mark-or-offset syntax)
  "Return a form at `mark-or-offset'. This function defines which
1560
  forms the COM-FOO-this commands affect."
1561
1562
1563
  (as-offsets ((mark-or-offset offset))
    (or (form-around syntax offset)
        (form-before syntax offset))))
1564

1565
1566
1567
1568
1569
(defun preceding-form (mark-or-offset syntax)
  "Return a form at `mark-or-offset'."
  (as-offsets ((mark-or-offset offset))
   (or (form-before syntax offset)
       (form-around syntax offset))))
Troels Henriksen's avatar