summaryrefslogtreecommitdiff
path: root/parser/lexer.scm
blob: 7230613f0bedc04891b50d08d454d6783e475152 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
;;; File: parser/lexer    Author: John

;;; token data structure: a list with the token type in the
;;; car and other information in the rest of the list.  Symbols
;;; designate the token type.

;;; Reserved tokens use the name as the type and have no args.
;;; Reserved tokens:
;;;  case class data default deriving else hiding if import in infix
;;;  infixl infixr instance interface let module of renaming then to
;;;  type where .. :: => = @ \ | ~ <- -> `
;;; Other tokens:
;;;  (file string)
;;;  (newline line indent-column)
;;;  (conid string)
;;;  (varid string)
;;;  (consym string)
;;;  (varsym string)
;;;  (comment string) ;;; not used at the moment
;;;  (integer integer)
;;;  (float integer fraction exponent) 
;;;  (string string)
;;;  (eof)


;;; *** All of the stuff for lexing character and string literals is
;;; *** broken because it assumes that the host Lisp uses the ASCII
;;; *** encoding for characters and supports at least 255 characters.
;;; *** I have marked the specific places in the code where these
;;; *** assumptions are made, but fixing the problem will probably
;;; *** require more drastic changes anyway -- such as using integers
;;; *** instead of characters and vectors of integers instead of characters
;;; *** throughout the compiler.

(define *max-char* 255)  ; highest char-code allowed.

;;; This defines the long names of the control chars.  Note that some of
;;; this duplicates the table above & the reader.

(define *control-char-names* '(
  ("NUL" . 0) ("SOH" . 1) ("STX" . 2) ("ETX" . 3)
  ("EOT" . 4) ("ENQ" . 5) ("ACK" . 6) ("BEL" . 7)
  ("BS" . 8) ("HT" . 9) ("LF" . 10) ("VT" . 11)
  ("FF" . 12) ("CR" . 13) ("SO" . 14) ("SI" . 15)
  ("DLE" . 16) ("DC1" . 17) ("DC2" . 18) ("DC3" . 19)
  ("DC4" . 20) ("NAK" . 21) ("SYN" . 22) ("ETB" . 23)
  ("CAN" . 24) ("EM" . 25) ("SUB" . 26) ("ESC" . 27)
  ("FS" . 28) ("GS" . 29) ("RS" . 30) ("US" . 31)
  ("SP" . 32) ("DEL" . 127)))

;;; This defines the short names for a few control chars.  This
;;; is keyed off the previous table

(define *short-control-char-names* '(
   (#\a . "BEL")    (#\b . "BS")    (#\f . "FF")    (#\n . "LF")
   (#\r . "CR") (#\t . "HT") (#\v . "VT")))

;;; This is used in the ^X construct.  Assume that ^X = code for ^A + X-A
;;; *** This is an invalid assumption.

(define *control-A* 1)

;;; This function is the interface between the lexer and the rest
;;; of the system.  Note that the `file' reported in error messages
;;; must be bound in an outer context.


;;; *** I think this function should be binding these variables and not
;;; *** just assigning them.

(define (lex-port port literate?)
  (setf *lex-literate?* literate?)
  (setf *current-line* 1)
  (setf *current-col* 0)
  (setf *on-new-line?* '#t)
  (setf *save-col?* '#f)
  (setf *port* port)
  (setf *tokens* '())
  (setf *char* (read-char *port*))
  (setf *peek-char* (read-char *port*))
  (when (eof-object? *char*)
	(setf *char* '#\space))
  (when (eof-object? *peek-char*)
	(setf *peek-char* '#\space))
  (setf *at-eof/p?* '#f)
  (setf *at-eof?* '#f)
  (when *lex-literate?*
     (process-literate-comments '#t))
  (parse-till-eof)
  (nreverse *tokens*))

(define (parse-till-eof)
  (cond (*at-eof?*
	 (emit-token 'eof)
	 '())
	(else
	 (lex-one-token)
	 (parse-till-eof))))

;;; There is an assumption that the scanner never peeks beyond a newline.
;;; In literate mode, this may reveal the wrong thing.

(define (advance-char)
  (if (and *lex-literate?* (eqv? *char* #\newline))
      (process-literate-comments '#f)
      (advance-char-1)))

(define (advance-char-1)
  (cond ((eqv? *char* #\newline)
	 (setf *on-new-line?* '#t)
	 (incf (the fixnum *current-line*))
	 (setf *current-col* 0))
	((eqv? *char* #\tab)
	 (incf (the fixnum *current-col*) (- 8 (modulo *current-col* 8))))
	(else
	 (incf (the fixnum *current-col*))))
  (setf *char* *peek-char*)
  (setf *at-eof?* *at-eof/p?*)
  (setf *peek-char* (read-char *port*))
  (when (eof-object? *peek-char*)
     (setf *at-eof/p?* '#t)
     (setf *peek-char* '#\space))
  *char*)

(define (peek-char-2)
  (let ((ch (peek-char *port*)))
    (if (eof-object? ch)
	'#\space
	ch)))

(define (lex-one-token)
 (setf *start-line* *current-line*) ; capture the loc at the start of the token
 (setf *start-col* *current-col*)
 (unless *at-eof?*
  (char-case *char*
    (whitechar
     (advance-char)
     (lex-one-token))
    (#\- (char-case *peek-char*
	    (#\- (lex-comment))
	    (#\> (advance-char)
		 (advance-char)
		 (emit-token '\-\>))
	    (#\} (signal-missing-begin-comment)
		 (advance-char)
		 (advance-char)
		 (lex-one-token))
	    (else
	     (lex-varsym))))
    (#\{ (cond ((char=? *peek-char* '#\-)
		(advance-char)
		(advance-char)
		(cond ((char=? *char* '#\#)
		       (advance-char)
		       (emit-token 'begin-annotation))
		      (else
		       (lex-ncomment)
		       (lex-one-token))))
	       (else
		(advance-char)
		(emit-token '\{ ))))
    (small (lex-varid))
    (large (lex-conid))
    (#\( (advance-char)
	 (emit-token '\())
    (#\: (lex-consym))
    (#\` (advance-char)
	 (emit-token '\`))
    ((symbol presymbol) (lex-varsym))
    (digit (lex-numeric))
    (#\' (lex-char))
    (#\" (lex-string))
    (#\) (advance-char)
	 (emit-token '\)))
    (#\, (advance-char)
	 (emit-token '\,))
    (#\; (advance-char)
	 (emit-token '\;))
    (#\[ (advance-char)
	 (emit-token '\[))
    (#\] (advance-char)
	 (emit-token '\]))
    (#\_ (advance-char)
	 (emit-token '\_))
    (#\} (advance-char)
	 (emit-token '\}))
    (else
     (signal-invalid-character *char*)
     (advance-char)
     (lex-one-token)))))

(define (signal-missing-begin-comment)
  (lexer-error 'missing-begin-comment
	       "`-}' appears outside of a nested comment."))

(define (signal-invalid-character ch)
  (lexer-error 'invalid-character 
	       "Invalid character `~a' appears in source program." ch))

(define (advance-past-white)
  (unless *at-eof?*
    (char-case *char*
      (whitechar
        (advance-char)
	(advance-past-white))
      (else
       '()))))

(define (process-literate-comments at-start?)
  (unless at-start? (advance-char-1))
  (let ((l (classify-line)))
    (cond ((or *at-eof?* (eq? l 'program))
	   '())
	  ((eq? l 'blank)
	   (skip-literate-comment '#t))
	  (else
	   (when (not at-start?)
		 (lexer-error 'blank-line-needed
		    "Literate comments must be preceeded by a blank line"))
	   (skip-literate-comment '#f)))))

(define (skip-literate-comment prev-blank)
  (skip-past-line)
  (let ((l (classify-line)))
    (cond (*at-eof?*
	   '())
	  ((eq? l 'comment)
	   (skip-literate-comment '#f))
	  ((eq? l 'blank)
	   (skip-literate-comment '#t))
	  (else
	   (when (not prev-blank)
	     (lexer-error 'blank-line-needed
		  "Literate comments must be followed by a blank line"))))))
  
(define (classify-line)
  (if *at-eof?*
      'blank
      (char-case *char*
       (#\>
	(advance-char-1)
	'program)
       (#\newline 'blank)
       (whitechar
	(classify-line-1))
       (else 'comment))))

(define (classify-line-1)
  (advance-char-1)
  (char-case *char*
    (#\newline 'blank)
    (whitechar (classify-line-1))
    (else 'comment)))

(define (skip-past-line)
  (when (not *at-eof?*)
    (char-case *char*
      (#\newline
       (advance-char-1))
      (else
       (advance-char-1)
       (skip-past-line)))))
	  
(define (lex-comment)  ;; a -- style comment
  (advance-char)
  (cond (*at-eof?* (lexer-eof-in-comment *current-line*))
	((char=? *char* #\newline)
	 (lex-one-token))
	(else
	 (lex-comment))))

(define (lexer-eof-in-comment start-line)
  (signal-eof-in-comment start-line)
  (lex-one-token))  ; will return the eof token

(define (signal-eof-in-comment start-line)
  (lexer-error 'eof-in-comment
	       "End of file in comment starting at line ~A." start-line))

;;; Here *char* and *peek-char* are the first two chars on a line.

(define (scan-symbol)
  (scan-list-of (symbol #\:)))

(define (scan-var-con)
  (scan-list-of (large small digit #\' #\_)))

(define (lex-ncomment)
  (lex-ncomment-1 *current-line*))

(define (lex-ncomment-1 start-line)
 (if *at-eof?*
  (lexer-eof-in-comment start-line)
  (char-case *char*
    (#\- (cond ((char=? *peek-char* #\})
		(advance-char)
		(advance-char))
	       (else
		(advance-char)
		(lex-ncomment-1 start-line))))
    (#\{ (cond ((char=? *peek-char* #\-)
		(advance-char)
		(advance-char)
		(lex-ncomment)
		(lex-ncomment-1 start-line))
	       (else
		(advance-char)
		(lex-ncomment-1 start-line))))
    (else
     (advance-char)
     (lex-ncomment-1 start-line)))))

(define (lex-varid)
  (let ((sym (scan-var-con)))
    (parse-reserved sym varid
       "case" "class"
       "data" "default" "deriving"
       "else"
       "hiding"
       "if" "import" "in" "infix" "infixl" "infixr" "instance" "interface"
       "let"
       "module"
       "of"
       "renaming"
       "then" "to" "type"
       "where")))

(define (lex-conid)
  (let ((sym (scan-var-con)))
    (emit-token/string 'conid sym)))

(define (lex-consym)
  (let ((sym (scan-symbol)))
    (cond ((string=/list? (cdr sym) ":")
	   (emit-token '\:\:))
	  (else
	   (emit-token/string 'consym sym)))))

(define (lex-varsym)
  (let ((sym (scan-symbol)))
    (cond ((and (string=/list? sym "<") (char=? *char* #\-))
	   (advance-char)
	   (emit-token '\<\-))
	  ((and (string=/list? sym "#")
		(char=? *char* #\-)
		(char=? *peek-char* #\}))
	   (advance-char)
	   (advance-char)
	   (emit-token 'end-annotation))
	  (else
	   (parse-reserved sym varsym
	      ".."
	      "=>" "="
	      "@"
	      "\\"
	      "|"
	      "~")))))

(define (lex-integer radix)
  (lex-integer-1 radix 0))

(define (lex-integer-1 radix psum)
  (declare (type fixnum radix)
	   (type integer psum))
  (let ((d  (char->digit *char* radix)))
    (if d
	(begin
	  (advance-char)
	  (lex-integer-1 radix (+ (* psum radix) (the fixnum d))))
	psum)))

(define (lex-fraction int-part denominator)
  (declare (type integer int-part denominator))
  (let ((d  (char->digit *char* 10)))
    (if d
	(begin
	  (advance-char)
	  (lex-fraction
	    (+ (* int-part 10) (the fixnum d)) (* denominator 10)))
	(values int-part denominator))))

(define (lex-numeric)
  (let ((int-part (lex-integer 10)))
    (if (and (char=? *char* #\.)
	     (char->digit *peek-char* 10))
	(lex-float int-part)
	(emit-token 'integer int-part))))

(define (lex-float int-part)
  (advance-char)
  (multiple-value-bind (numerator denominator) (lex-fraction int-part 1)
    (let ((no-exponent
	   (lambda () (emit-token 'float numerator denominator 0))))
      (char-case *char*
	(exponent
	  (char-case *peek-char*
	    (digit
	     (advance-char)
	     (lex-float/exp numerator denominator 1))
	    ((#\+ #\-)
	     (cond ((char->digit (peek-char-2) 10)
		    (let ((sign (if (char=? *peek-char* '#\+) 1 -1)))
		      (advance-char)
		      (advance-char)
		    (lex-float/exp numerator denominator sign)))
		 (else
		  (funcall no-exponent))))
	  (else
	   (funcall no-exponent))))
       (else
	(emit-token 'float numerator denominator 0))))))

(define (lex-float/exp numerator denominator sign)
  (let ((exponent (lex-integer 10)))
    (emit-token 'float numerator denominator (* sign exponent))))

(define (lex-char)
  (advance-char)
  (let ((c
    (char-case *char*
      (#\' (signal-null-character)
	   '#\?)
      (#\\ (lex-escaped-char '#f))
      ((#\space graphic)
       (let ((ch *char*))
	 (advance-char)
	 ch))
      (else
       (signal-bad-character-constant *char*)
       (advance-char)
       `#\?))))
    (cond ((char=? *char* '#\')
	   (advance-char)
	   (emit-token 'char c))
	  (else
	   (signal-missing-char-quote)
	   (skip-to-quote-or-eol)))))

(define (signal-null-character)
  (lexer-error 'null-character
	       "Null character '' is illegal - use '\\'' for a quote."))

(define (signal-bad-character-constant ch)
  (lexer-error 'bad-character-constant
	       "The character `~a' may not appear in a character literal." ch))

(define (signal-missing-char-quote)
  (lexer-error 'missing-char-quote
	       "Character constant has more than one character."))
  

(define (skip-to-quote-or-eol)
  (if *at-eof?*
      (lex-one-token)
      (char-case *char*
	 (#\' (advance-char)
	      (lex-one-token))
	 (#\newline (advance-char)
		    (lex-one-token))
	 (else
	  (advance-char)
	  (skip-to-quote-or-eol)))))

(define (lex-string)
  (advance-char)
  (emit-token 'string (list->string (gather-string-chars))))

(define (gather-string-chars)
  (char-case *char*
    (#\\
      (let ((ch (lex-escaped-char '#t)))
	(if (eq? ch 'null)
	    (gather-string-chars)
	    (cons ch (gather-string-chars)))))
    (#\"
      (advance-char)
      '())
    ((graphic #\space)
     (let ((ch *char*))
       (advance-char)
       (cons ch (gather-string-chars))))
    (#\newline
     (signal-missing-string-quote)
     '())
    (else
     (signal-bad-string-constant *char*)
     (advance-char)
     (gather-string-chars))))

(define (signal-missing-string-quote)
  (lexer-error 'missing-string-quote
	       "String continued over end of line."))

(define (signal-bad-string-constant ch)
  (lexer-error 'bad-string-constant
	       "The character `~a' may not appear in a string literal." ch))


(define (convert-stupid-control-character-names)
  (let ((c1 *char*)
	(c2 *peek-char*))
    (advance-char)
    (advance-char)
    (let ((s2 (string c1 c2))
	  (s3 (string c1 c2 *char*)))
      (let ((srch3 (assoc s3 *control-char-names*)))
	(cond (srch3
	       (advance-char)
	       (integer->char (cdr srch3)))
	      (else
	       (let ((srch2 (assoc s2 *control-char-names*)))
		 (cond (srch2
			(integer->char (cdr srch2)))
		       (else
			(signal-bad-control-char s3)
			`#\?)))))))))

(define (signal-bad-control-char name)
  (lexer-error 'invalid-control-char
	       "`~a' is not a recognized control character name." name))


(define (lex-escaped-char in-string?)
  (advance-char)
  (char-case *char*
    ((#\a #\b #\f #\n #\r #\t #\v)
     (let* ((ccode (cdr (assoc *char* *short-control-char-names*)))
	    (ccode1 (cdr (assoc ccode *control-char-names*))))
       (advance-char)
       (integer->char ccode1)))
    ((#\\ #\' #\")
     (let ((ch *char*))
       (advance-char)
       ch))
    (#\&
     (advance-char)
     (cond (in-string? 'null)
	   (else
	    (signal-bad-&-escape)
	    '#\?)))
    (#\^
     ;; *** This code is problematic because it assumes
     ;; *** (1) that you can do the arithmetic on the character codes
     ;; *** (2) that the resulting integer can actually be coerced to
     ;; ***     the right character object in the host Lisp.
     (advance-char)
     (char-case *char*
       ((large #\@ #\[ #\\ #\] #\^ #\_)
	(let ((code (+ (- (char->integer *char*)
			  (char->integer '#\A))
		       *control-A*)))
	  (advance-char)
	  (integer->char code)))
       (else
	(signal-bad-^-escape *char*)
	'#\?)))
    (large
     (convert-stupid-control-character-names))
    (digit
     (convert-num-to-char (lex-integer 10)))
    (#\o
     (advance-char)
     (cond ((char->digit *char* 8)
	    (convert-num-to-char (lex-integer 8)))
	   (else
	    (signal-missing-octal-digits)
	    '#\?)))
    (#\x
     (advance-char)
     (cond ((char->digit *char* 16)
	    (convert-num-to-char (lex-integer 16)))
	   (else
	    (signal-missing-hex-digits)
	    `#\?)))
    (whitechar
     (cond (in-string?
	    (lex-gap))
	   (else
	    (signal-bad-gap)
	    `#\?)))
    (else
     (signal-bad-escape *char*)
     `#\?)))

(define (signal-bad-&-escape)
  (lexer-error 'bad-&-escape
	       "The escape `\\&' is not allowed inside a character literal."))

(define (signal-bad-^-escape ch)
  (lexer-error 'bad-^-escape
	       "The escape `\\^~a' is not recognized." ch))

(define (signal-missing-octal-digits)
  (lexer-error 'missing-octal-digits
	       "No digits provided for `\\o' escape."))

(define (signal-missing-hex-digits)
  (lexer-error 'missing-hex-digits
	       "No digits provided for `\\x' escape."))

(define (signal-bad-gap)
  (lexer-error 'invalid-gap
	       "Gaps are not allowed inside character literals."))

(define (signal-bad-escape ch)
  (lexer-error 'bad-escape
	       "The escape `\\~a' is not recognized." ch))



;;; *** This code is problematic because it assumes that integers
;;; *** between 0 and 255 map on to characters with the corresponding
;;; *** ASCII encoding in the host Lisp, and that the host Lisp actually
;;; *** supports 255 characters.

(define (convert-num-to-char num)
  (cond ((and (>= num 0) (>= *max-char* num))
	 (integer->char num))
	(else
	 (signal-char-out-of-range num)
	 '#\?)))

(define (signal-char-out-of-range num)
  (lexer-error 'char-out-of-range
	       "There is no character corresponding to code ~s." num))


(define (lex-gap)
  (cond (*at-eof?*
	 (signal-eof-in-gap)
	 'null)
	(else
	 (char-case *char*
	   (whitechar
	    (advance-char)
	    (lex-gap))
	   (#\\
	    (advance-char)
	    'null)
	   (else
	    (signal-missing-gap)
	    'null)))))
  
      
(define (signal-eof-in-gap)
  (lexer-error 'eof-in-gap
	       "End of file encountered inside gap."))

(define (signal-missing-gap)
  (lexer-error 'missing-gap
	       "Missing gap delimiter, or junk inside gap."))