forked from audacity/audacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sal-parse.lsp
1827 lines (1631 loc) · 62.6 KB
/
sal-parse.lsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; SAL parser -- replaces original pattern-directed parser with
;; a recursive descent one
;;
;; Parse functions either parse correctly and return
;; compiled code as a lisp expression (which could be nil)
;; or else they call parse-error, which does not return
;; (instead, parse-error forces a return from parse)
;; In the original SAL parser, triples were returned
;; including the remainder if any of the tokens to be
;; parsed. In this parser, tokens are on the list
;; *sal-tokens*, and whatever remains on the list is
;; the list of unparsed tokens.
;; scanning delimiters.
(setfn nreverse reverse)
(defconstant +quote+ #\") ; "..." string
(defconstant +kwote+ #\') ; '...' kwoted expr
(defconstant +comma+ #\,) ; positional arg delimiter
(defconstant +pound+ #\#) ; for bools etc
(defconstant +semic+ #\;) ; comment char
(defconstant +lbrace+ #\{) ; {} list notation
(defconstant +rbrace+ #\})
(defconstant +lbrack+ #\[) ; unused for now
(defconstant +rbrack+ #\])
(defconstant +lparen+ #\() ; () expr and arg grouping
(defconstant +rparen+ #\))
;; these are defined so that SAL programs can name these symbols
;; note that quote(>) doesn't work, so you need quote(symbol:greater)
(setf symbol:greater '>)
(setf symbol:less '<)
(setf symbol:greater-equal '>=)
(setf symbol:less-equal '<=)
(setf symbol:equal '=)
(setf symbol:not '!)
(setf symbol:not-equal '/=)
(defparameter +whites+ (list #\space #\tab #\newline (code-char 13)))
(defparameter +kwstyle+ (list :suffix #\:)) ; let's try dylan
(defparameter +operators+
;; each op is: (<token-class> <sal-name> <lisp-form>)
'((:+ "+" sum)
(:- "-" diff)
(:* "*" mult)
(:/ "/" /)
(:% "%" rem)
(:^ "^" expt)
(:= "=" sal-equal) ; equality and assigment
(:!= "!=" not-sal-equal)
(:< "<" <)
(:> ">" >)
(:<= "<=" <=) ; leq and assignment minimization
(:>= ">=" >=) ; geq and assignment maximization
(:~= "~=" equal) ; general equality
(:+= "+=" +=) ; assignment increment-and-store
(:-= "-=" -=) ; assignment increment-and-store
(:*= "*=" *=) ; assignment multiply-and-store
(:/= "/=" /=) ; assignment multiply-and-store
(:&= "&=" &=) ; assigment list collecting
(:@= "@=" @=) ; assigment list prepending
(:^= "^=" ^=) ; assigment list appending
(:! "!" not)
(:& "&" and)
(:\| "|" or)
(:~ "~" sal-stretch)
(:~~ "~~" sal-stretch-abs)
(:@ "@" sal-at)
(:@@ "@@" sal-at-abs)
))
(setf *sal-local-variables* nil) ;; used to avoid warning about variable
;; names when the variable has been declared as a local
(defparameter *sal-operators*
'(:+ :- :* :/ :% :^ := :!= :< :> :<= :>= :~= :+= :*= :&= :@= :^= :! :& :\|
:~ :~~ :@ :@@))
(defparameter +delimiters+
'((:lp #\()
(:rp #\))
(:lc #\{) ; left curly
(:rc #\})
(:lb #\[)
(:rb #\])
(:co #\,)
(:kw #\') ; kwote
(nil #\") ; not token
; (nil #\#)
(nil #\;)
))
(setf *reserved-words* '((::+ ":+") (::- ":-") (::* ":*") (::/ ":/") (::= ":=")
(::!= ":!=") (::< ":<") (::> ":>") (::<= ":<=")
(::>= ":>=") (::~= ":~=") (::! ":!") (::& ":&")
(::\| ":|") (:IF "if") (:THEN "then") (:ELSE "else")
(:WHEN "when") (:UNLESS "unless") (:SET "set")
(:= "=") (:+= "+=") (:*= "*=") (:&= "&=") (:@= "@=")
(:^= "^=") (:<= "<=") (:>= ">=") (:PRINT "print")
(:LOOP "loop")
(:RUN "run") (:REPEAT "repeat") (:FOR "for")
(:FROM "from") (:IN "in") (:BELOW "below") (:TO "to")
(:ABOVE "above") (:DOWNTO "downto") (:BY "by")
(:OVER "over") (:WHILE "while") (:UNTIL "until")
(:FINALLY "finally") (:RETURN "return")
(:WAIT "wait") (:BEGIN "begin") (:WITH "with")
(:END "end") (:VARIABLE "variable")
(:FUNCTION "function") (:PROCESS "process")
(:CHDIR "chdir") (:DEFINE "define") (:LOAD "load")
(:PLAY "play")
(:EXEC "exec") (:exit "exit") (:DISPLAY "display")
(:~ "~") (:~~ "~~") (:@ ":@") (:@@ ":@@")))
(setf *sal-fn-name* nil)
(defun make-sal-error (&key type text (line nil) start)
; (error 'make-sal-error-was-called-break)
(list 'sal-error type text line start))
(setfn sal-error-type cadr)
(setfn sal-error-text caddr)
(setfn sal-error-line cadddr)
(defun sal-error-start (x) (cadddr (cdr x)))
(defun is-sal-error (x) (and x (eq (car x) 'sal-error)))
(defun sal-tokens-error-start (start)
(cond (start
start)
(*sal-tokens*
(token-start (car *sal-tokens*)))
(t
(length *sal-input-text*))))
(defmacro errexit (message &optional start)
`(parse-error (make-sal-error :type "parse"
:line *sal-input-text* :text ,message
:start ,(sal-tokens-error-start start))))
(defmacro sal-warning (message &optional start)
`(pperror (make-sal-error :type "parse" :line *sal-input-text*
:text ,message
:start ,(sal-tokens-error-start start))
"warning"))
(setf *pos-to-line-source* nil)
(setf *pos-to-line-pos* nil)
(setf *pos-to-line-line* nil)
(defun pos-to-line (pos source)
;; this is really inefficient to search every line from
;; the beginning, so cache results and search forward
;; from there if possible
(let ((i 0) (line-no 1)) ;; assume no cache
;; see if we can use the cache
(cond ((and (eq source *pos-to-line-source*)
*pos-to-line-pos* *pos-to-line-line*
(>= pos *pos-to-line-pos*))
(setf i *pos-to-line-pos*)
(setf line-no *pos-to-line-line*)))
;; count newlines up to pos
(while (< i pos)
(if (char= (char source i) #\newline)
(incf line-no))
(setf i (1+ i)))
;; save results in cache
(setf *pos-to-line-source* source
*pos-to-line-pos* pos
*pos-to-line-line* line-no)
;; return the line number at pos in source
line-no))
;; makes a string of n spaces, empty string if n <= 0
(defun make-spaces (n)
(cond ((> n 16)
(let* ((half (/ n 2))
(s (make-spaces half)))
(strcat s s (make-spaces (- n half half)))))
(t
(subseq " " 0 (max n 0)))))
(defun pperror (x &optional (msg-type "error"))
(let* ((source (sal-error-line x))
(llen (length source))
line-no
beg end)
; (display "pperror" x (strcat "|" (sal-error-line x) "|"))
;; isolate line containing error
(setf beg (sal-error-start x))
(setf beg (min beg (1- llen)))
(do ((i beg (- i 1))
(n nil)) ; n gets set when we find a newline
((or (< i 0) n)
(setq beg (or n 0)))
(if (char= (char source i) #\newline)
(setq n (+ i 1))))
(do ((i (sal-error-start x) (+ i 1))
(n nil))
((or (>= i llen) n)
(setq end (or n llen)))
(if (char= (char source i) #\newline)
(setq n i)))
(setf line-no (pos-to-line beg source))
; (display "pperror" beg end (sal-error-start x))
;; print the error. include the specfic line of input containing
;; the error as well as a line below it marking the error position
;; with an arrow: ^
(let* ((pos (- (sal-error-start x) beg))
(line (if (and (= beg 0) (= end llen))
source
(subseq source beg end)))
(mark (make-spaces pos)))
(format t "~%>>> ~A ~A: ~A.~%>>> in ~A, line ~A, col ~A.~%~%~A~%~A^~%"
(sal-error-type x) msg-type (sal-error-text x)
*sal-input-file-name* line-no (1+ pos)
line mark)
; (format t "~%>>> ~A error in \"~A\", line ~A, col ~A: ~A.~%~%~A~%~A^~%"
; (sal-error-type x) *sal-input-file-name* line-no pos
; (sal-error-text x) line mark)
x)))
;;;
;;; the lexer. right now it assumes input string is complete and ready
;;; to be processed as a valid expression.
;;;
(defun advance-white (str white start end)
;; skip "white" chars, where white can be a char, list of chars
;; or predicate test
(do ((i start )
(p nil))
((or p (if (< start end)
(not (< -1 i end))
(not (> i end -1))))
(or p end))
(cond ((consp white)
(unless (member (char str i) white :test #'char=)
(setq p i)))
((characterp white)
(unless (char= (char str i) white)
(setq p i)))
((functionp white)
(unless (funcall white (char str i))
(setq p i))))
(if (< start end)
(incf i)
(decf i))))
(defun search-delim (str delim start end)
;; find position of "delim" chars, where delim can be
;; a char, list of chars or predicate test
(do ((i start (+ i 1))
(p nil))
((or (not (< i end)) p)
(or p end))
(cond ((consp delim)
(if (member (char str i) delim :test #'char=)
(setq p i)))
((characterp delim)
(if (char= (char str i) delim)
(setq p i)))
((functionp delim)
(if (funcall delim (char str i))
(setq p i))))))
;; UNBALANCED-INPUT AND TOKENIZE HAVE BEEN REWRITTEN, SEE BELOW. THIS ONE IS
;; OLD AND JUST KEPT HERE FOR REFERENCE
#|
(defun unbalanced-input (errf line toks par bra brk kwo)
;; search input for the starting position of some unbalanced
;; delimiter, toks is reversed list of tokens with something
;; unbalanced
(let (char text targ othr levl pos)
(cond ((> par 0) (setq char #\( targ ':lp othr ':rp levl par))
((< par 0) (setq char #\) targ ':rp othr ':lp levl 0))
((> bra 0) (setq char #\{ targ ':lc othr ':rc levl bra))
((< bra 0) (setq char #\} targ ':rc othr ':lc levl 0))
((> brk 0) (setq char #\[ targ ':ls othr ':rs levl brk))
((< brk 0) (setq char #\] targ ':rs othr ':ls levl 0))
((> kwo 0) (setq char #\' targ ':kw othr ':kw levl kwo)))
(setq text (format nil "Unmatched '~A'" char))
;; search for start of error in token list
(do ((n levl)
(tail toks (cdr tail)))
((or (null tail) pos)
(or pos (error (format nil "Shouldn't! can't find op ~A in ~A."
targ (reverse toks)))))
(if (eql (token-type (car tail)) targ)
(if (= n levl)
(setq pos (token-start (car tail)))
(decf n))
(if (eql (token-type (car tail)) othr)
(incf n))))
(errexit text pos)))
(defun tokenize (str reserved error-fn)
;&key (start 0) (end (length str))
; (white-space +whites+) (delimiters +delimiters+)
; (operators +operators+) (null-ok t)
; (keyword-style +kwstyle+) (reserved nil)
; (error-fn nil)
; &allow-other-keys)
;; return zero or more tokens or a sal-error
(let ((toks (list t))
(start 0)
(end (length str))
(all-delimiters +whites+)
(errf (or error-fn
(lambda (x) (pperror x) (return-from tokenize x)))))
(dolist (x +delimiters+)
(push (cadr x) all-delimiters))
(do ((beg start)
(pos nil)
(all all-delimiters)
(par 0)
(bra 0)
(brk 0)
(kwo 0)
(tok nil)
(tail toks))
((not (< beg end))
;; since input is complete check parens levels.
(if (= 0 par bra brk kwo)
(if (null (cdr toks))
(list)
(cdr toks))
(unbalanced-input errf str (reverse (cdr toks))
par bra brk kwo)))
(setq beg (advance-white str +whites+ beg end))
(setf tok
(read-delimited str :start beg :end end
:white +whites+ :delimit all
:skip-initial-white nil :errorf errf))
;; multiple values are returned, so split them here:
(setf pos (second tok)) ; pos is the end of the token (!)
(setf tok (first tok))
;; tok now string, char (delimiter), :eof or token since input
;; is complete keep track of balancing delims
(cond ((eql tok +lbrace+) (incf bra))
((eql tok +rbrace+) (decf bra))
((eql tok +lparen+) (incf par))
((eql tok +rparen+) (decf par))
((eql tok +lbrack+) (incf brk))
((eql tok +rbrack+) (decf brk))
((eql tok +kwote+) (setq kwo (mod (+ kwo 1) 2))))
(cond ((eql tok ':eof)
(setq beg end))
(t
;; may have to skip over comments to reach token, so
;; token beginning is computed by backing up from current
;; position (returned by read-delimited) by string length
(setf beg (if (stringp tok)
(- pos (length tok))
(1- pos)))
(setq tok (classify-token tok beg str errf
+delimiters+ +operators+
+kwstyle+ reserved))
;(display "classify-token-result" tok)
(setf (cdr tail) (list tok ))
(setf tail (cdr tail))
(setq beg pos))))))
|#
;; old tokenize (above) counted delimiters to check for balance,
;; but that does not catch constructions like ({)}. I think
;; we could just leave this up to the parser, but this rewrite
;; uses a stack to check balanced parens, braces, quotes, etc.
;; The checking establishes at least some minimal global properties
;; of the input before evaluating anything, which might be good
;; even though it's doing some extra work. In fact, using a
;; stack rather than counts is doing even more work, but the
;; problem with counters is that some very misleading or just
;; plain wrong error messages got generated.
;;
;; these five delimiter- functions do checks on balanced parens,
;; braces, and brackets, leaving delimiter-mismatch set to bad
;; token if there is a mismatch
(defun delimiter-init ()
(setf delimiter-stack nil)
(setf delimiter-mismatch nil))
(defun delimiter-match (tok what)
(cond ((eql (token-string (first delimiter-stack)) what)
(pop delimiter-stack))
((null delimiter-mismatch)
;(display "delimiter-mismatch" tok)
(setf delimiter-mismatch tok))))
(defun delimiter-check (tok)
(let ((c (token-string tok)))
(cond ((member c '(#\( #\{ #\[))
(push tok delimiter-stack))
((eql c +rbrace+)
(delimiter-match tok +lbrace+))
((eql c +rparen+)
(delimiter-match tok +lparen+))
((eql c +rbrack+)
(delimiter-match tok +lbrack+)))))
(defun delimiter-error (tok)
(errexit (format nil "Unmatched '~A'" (token-string tok))
(token-start tok)))
(defun delimiter-finish ()
(if delimiter-mismatch
(delimiter-error delimiter-mismatch))
(if delimiter-stack
(delimiter-error (car delimiter-stack))))
(defun tokenize (str reserved error-fn)
;; return zero or more tokens or a sal-error
(let ((toks (list t))
(start 0)
(end (length str))
(all-delimiters +whites+)
(errf (or error-fn
(lambda (x) (pperror x) (return-from tokenize x)))))
(dolist (x +delimiters+)
(push (cadr x) all-delimiters))
(delimiter-init)
(do ((beg start)
(pos nil)
(all all-delimiters)
(tok nil)
(tail toks))
((not (< beg end))
;; since input is complete check parens levels.
(delimiter-finish)
(if (null (cdr toks)) nil (cdr toks)))
(setq beg (advance-white str +whites+ beg end))
(setf tok
(read-delimited str :start beg :end end
:white +whites+ :delimit all
:skip-initial-white nil :errorf errf))
;; multiple values are returned, so split them here:
(setf pos (second tok)) ; pos is the end of the token (!)
(setf tok (first tok))
(cond ((eql tok ':eof)
(setq beg end))
(t
;; may have to skip over comments to reach token, so
;; token beginning is computed by backing up from current
;; position (returned by read-delimited) by string length
(setf beg (if (stringp tok)
(- pos (length tok))
(1- pos)))
(setq tok (classify-token tok beg str errf
+delimiters+ +operators+
+kwstyle+ reserved))
(delimiter-check tok)
;(display "classify-token-result" tok)
(setf (cdr tail) (list tok ))
(setf tail (cdr tail))
(setq beg pos))))))
(defun read-delimited (input &key (start 0) end (null-ok t)
(delimit +delims+) ; includes whites...
(white +whites+)
(skip-initial-white t)
(errorf #'pperror))
;; read a substring from input, optionally skipping any white chars
;; first. reading a comment delim equals end-of-line, input delim
;; reads whole input, pound reads next token. call errf if error
;(FORMAT T "~%READ-DELIMITED: ~S :START ~S :END ~S" input start end)
(let ((len (or end (length input))))
(while t ;; loop over comment lines
(when skip-initial-white
(setq start (advance-white input white start len)))
(if (< start len)
(let ((char (char input start)))
(setq end (search-delim input delimit start len))
(if (equal start end) ; have a delimiter
(cond ((char= char +semic+)
;; comment skips to next line and trys again...
(while (and (< start len)
(char/= (char input start) #\newline))
(incf start))
(cond ((< start len) ;; advance past comment and iterate
(incf start)
(setf skip-initial-white t))
(null-ok
(return (list ':eof end)))
(t
(errexit "Unexpected end of input"))))
; ((char= char +pound+)
; ;; read # dispatch
; (read-hash input delimit start len errorf))
((char= char +quote+)
;; input delim reads whole input
(return (sal:read-string input delimit start len errorf)))
((char= char +kwote+)
(errexit "Illegal delimiter" start))
(t ;; all other delimiters are tokens in and of themselves
(return (list char (+ start 1)))))
; else part of (equal start end), so we have token before delimiter
(return (list (subseq input start end) end))))
; else part of (< start len)...
(if null-ok
(return (list ':eof end))
(errexit "Unexpected end of input" start))))))
(defparameter hash-readers
'(( #\t sal:read-bool)
( #\f sal:read-bool)
( #\? read-iftok)
))
(defun read-hash (str delims pos len errf)
(let ((e (+ pos 1)))
(if (< e len)
(let ((a (assoc (char str e) hash-readers)))
(if (not a)
(errexit "Illegal # character" e)
(funcall (cadr a) str delims e len errf)))
(errexit "Missing # character" pos))))
(defun read-iftok (str delims pos len errf)
str delims len errf
(list (make-token :type ':? :string "#?" :lisp 'if
:start (- pos 1))
(+ pos 1)))
; (sal:read-string str start len)
(defun sal:read-bool (str delims pos len errf)
delims len errf
(let ((end (search-delim str delims pos len)))
(unless (= end (+ pos 1))
(errexit "Illegal # expression" (- pos 1)))
(list (let ((t? (char= (char str pos) #\t) ))
(make-token :type ':bool
:string (if t? "#t" "#f")
:lisp t?
:start (- pos 1)))
(+ pos 1))))
(defun sal:read-string (str delims pos len errf)
(let* ((i (1+ pos)) ; i is index into string; start after open quote
c c2; c is the character at str[i]
(string (make-string-output-stream)))
;; read string, processing escaped characters
;; write the chars to string until end quote is found
;; then retrieve the string. quotes are not included in result token
;; in the loop, i is the next character location to examine
(while (and (< i len)
(not (char= (setf c (char str i)) +quote+)))
(if (char= c #\\) ;; escape character, does another character follow this?
(cond ((< (1+ i) len)
(incf i) ;; yes, set i so we'll get the escaped char
(setf c2 (char str i))
(setf c (assoc c2 `((#\n . #\newline) (#\t . #\tab)
(#\r . ,(char "\r" 0))
(#\f . ,(char "\f" 0)))))
(setf c (if c (cdr c) c2))) ;; use c2 if c wasn't listed
(t ;; no, we've hit the end of input too early
(errexit "Unmatched \"" i))))
;; we're good to take this character and move on to the next one
(write-char c string)
(incf i))
;; done with loop, so either we're out of string or we found end quote
(if (>= i len) (errexit "Unmatched \"" i))
;; must have found the quote
(setf string (get-output-stream-string string))
(list (make-token :type :string :start pos :string string :lisp string)
(1+ i))))
;;;
;;; tokens
;;;
(defun make-token (&key (type nil) (string "") start (info nil) lisp)
(list :token type string start info lisp))
(setfn token-type cadr)
(setfn token-string caddr)
(defun token-start (x) (cadddr x))
(defun token-info (token) (car (cddddr token)))
(defun token-lisp (token) (cadr (cddddr token)))
(defmacro set-token-type (tok val) `(setf (car (cdr ,tok)) ,val))
(defmacro set-token-lisp (tok val) `(setf (car (cdr (cddddr ,tok))) ,val))
(defun tokenp (tok) (and (consp tok) (eq (car tok) :token)))
(defun token=? (tok op)
(if (tokenp tok)
(equal (token-type tok) op)
(eql tok op)))
(defmethod token-print (obj stream)
(let ((*print-case* ':downcase))
(format stream "#<~s ~s>"
(token-type obj)
(token-string obj))))
(defun parse-token ()
(prog1 (car *sal-tokens*)
(setf *sal-tokens* (cdr *sal-tokens*))))
;;;
;;; token classification. types not disjoint!
;;;
(defun classify-token (str pos input errf delims ops kstyle res)
(let ((tok nil))
(cond ((characterp str)
;; normalize char delimiter tokens
(setq tok (delimiter-token? str pos input errf delims)))
((stringp str)
(setq tok (or (number-token? str pos input errf)
(operator-token? str pos input errf ops)
(keyword-token? str pos input errf kstyle)
(class-token? str pos input errf res)
(reserved-token? str pos input errf res)
(symbol-token? str pos input errf)
))
(unless tok
(errexit "Not an expression or symbol" pos)))
(t (setq tok str)))
tok))
(defun delimiter-token? (str pos input errf delims)
(let ((typ (member str delims :test (lambda (a b) (char= a (cadr b))))))
;; member returns remainder of the list
;(display "delimiter-token?" str delims typ)
(if (and typ (car typ) (caar typ))
(make-token :type (caar typ) :string str
:start pos)
(+ (break) (errexit "Shouldn't: non-token delimiter" pos)))))
(defun string-to-number (s)
(read (make-string-input-stream s)))
(defun number-token? (str pos input errf)
errf input
(do ((i 0 (+ i 1))
(len (length str))
(c nil)
(dot 0)
(typ ':int)
(sig 0)
(sla 0)
(dig 0)
(non nil))
((or (not (< i len)) non)
(if non nil
(if (> dig 0)
(make-token :type typ :string str
:start pos :lisp (string-to-number str))
nil)))
(setq c (char str i))
(cond ((member c '(#\+ #\-))
(if (> i 0) (setq non t)
(incf sig)))
((char= c #\.)
(if (> dot 0) (setq non t)
(if (> sla 0) (setq non t)
(incf dot))))
; xlisp does not have ratios
; ((char= c #\/)
; (setq typ ':ratio)
; (if (> sla 0) (setq non t)
; (if (= dig 0) (setq non t)
; (if (> dot 0) (setq non t)
; (if (= i (1- len)) (setq non t)
; (incf sla))))))
((digit-char-p c)
(incf dig)
(if (> dot 0) (setq typ ':float)))
(t (setq non t)))))
#||
(number-token? "" 0 "" #'pperror)
(number-token? " " 0 "" #'pperror)
(number-token? "a" 0 "" #'pperror)
(number-token? "1" 0 "" #'pperror)
(number-token? "+" 0 "" #'pperror)
(number-token? "-1/2" 0 "" #'pperror)
(number-token? "1." 0 "" #'pperror)
(number-token? "1.." 0 "" #'pperror)
(number-token? ".1." 0 "" #'pperror)
(number-token? ".1" 0 "" #'pperror)
(number-token? "-0.1" 0 "" #'pperror)
(number-token? "1/2" 0 "" #'pperror)
(number-token? "1//2" 0 "" #'pperror)
(number-token? "/12" 0 "" #'pperror)
(number-token? "12/" 0 "" #'pperror)
(number-token? "12/1" 0 "" #'pperror)
(number-token? "12./1" 0 "" #'pperror)
(number-token? "12/.1" 0 "" #'pperror)
||#
(defun operator-token? (str pos input errf ops)
;; tok can be string or char
(let ((typ (member str ops :test (lambda (a b) (equal a (cadr b))))))
(cond (typ
(setf typ (car typ)) ;; member returns remainder of list
(make-token :type (car typ) :string str
:start pos :lisp (or (third typ)
(read-from-string str)))))))
(defun str-to-keyword (str)
(intern (strcat ":" (string-upcase str))))
(defun keyword-token? (tok pos input errf style)
(let* ((tlen (length tok))
(keys (cdr style))
(klen (length keys)))
(cond ((not (< klen tlen)) nil)
((eql (car style) ':prefix)
(do ((i 0 (+ i 1))
(x nil))
((or (not (< i klen)) x)
(if (not x)
(let ((sym (symbol-token? (subseq tok i)
pos input errf )))
(cond (sym
(set-token-type sym ':key)
(set-token-lisp sym
(str-to-keyword (token-string sym)))
sym)))
nil))
(unless (char= (char tok i) (nth i keys))
(setq x t))))
((eql (car style) ':suffix)
(do ((j (- tlen klen) (+ j 1))
(i 0 (+ i 1))
(x nil))
((or (not (< i klen)) x)
(if (not x)
(let ((sym (symbol-token? (subseq tok 0 (- tlen klen))
pos input errf )))
(cond (sym
(set-token-type sym ':key)
(set-token-lisp sym
(str-to-keyword (token-string sym)))
sym)))
nil))
(unless (char= (char tok j) (nth i keys))
(setq x t)))))))
(setfn alpha-char-p both-case-p)
(defun class-token? (str pos input errf res)
res
(let ((a (char str 0)))
(if (char= a #\<)
(let* ((l (length str))
(b (char str (- l 1))))
(if (char= b #\>)
(let ((tok (symbol-token? (subseq str 1 (- l 1))
pos input errf)))
;; class token has <> removed!
(if tok (progn (set-token-type tok ':class)
tok)
(errexit "Not a class identifer" pos)))
(errexit "Not a class identifer" pos)))
nil)))
; (keyword-token? ":asd" '(:prefix #\:))
; (keyword-token? "asd" KSTYLE)
; (keyword-token? "asd:" KSTYLE)
; (keyword-token? "123:" KSTYLE)
; (keyword-token? ":foo" '(:prefix #\:))
; (keyword-token? "foo=" '(:suffix #\=))
; (keyword-token? "--foo" '(:prefix #\- #\-))
; (keyword-token? ":123" '(:suffix #\:))
; (keyword-token? "--asd" '(:prefix #\-)) ; ok since -asd is legal symbol
(defun reserved-token? (str pos input errf reserved)
errf input
(let ((typ (member str reserved :test (lambda (a b) (equal a (cadr b))))))
(if typ
(make-token :type (caar typ) :string str
:start pos)
nil)))
(defun sal-string-to-symbol (str)
(let ((sym (intern (string-upcase str)))
sal-sym)
(cond ((and sym ;; (it might be "nil")
(setf sal-sym (get sym :sal-name)))
sal-sym)
(t sym))))
(putprop 'simrep 'sal-simrep :sal-name)
(putprop 'seqrep 'sal-seqrep :sal-name)
(defun contains-op-char (s)
;; assume most identifiers are very short, so we search
;; over identifier letters, not over operator characters
;; Minus (-) is so common, we don't complain about it.
(dotimes (i (length s))
(if (string-search (subseq s i (1+ i)) "*/+=<>!%^&|")
(return t))))
(defun test-for-suspicious-symbol (token)
;; assume token is of type :id
(let ((sym (token-lisp token))
(str (token-string token))
(pos (token-start token)))
(cond ((and sym ; nil is not suspicious, but it's not "boundp"
(not (fboundp sym)) ; existing functions not suspicious
(not (boundp sym)) ; existing globals not suspicious
(not (member sym *sal-local-variables*))
(contains-op-char str)) ; suspicious if embedded operators
(sal-warning
(strcat "Identifier contains operator character(s).\n"
" Perhaps you omitted spaces around an operator")
pos)))))
(defun symbol-token? (str pos input errf)
;; if a potential symbol is preceded by #, drop the #
(if (and (> (length str) 1)
(char= (char str 0) #\#))
;; there are a couple of special cases: SAL defines #f and #?
(cond ((equal str "#f")
(return-from symbol-token?
(make-token :type ':id :string str :start pos :lisp nil)))
((equal str "#?")
(return-from symbol-token?
(make-token :type ':id :string str :start pos :lisp 'if)))
(t
(setf str (subseq str 1)))))
;; let's insist on at least one letter for sanity's sake
;; exception: allow '-> because it is used in markov pattern specs
(do ((i 0 (+ i 1)) ; i is index into string
(bad "Not an expression or symbol")
(chr nil)
(ltr 0) ; ltr is count of alphabetic letters in string
(dot nil) ; dot is index of "."
(pkg nil) ; pkg is index if package name "xxx:" found
(len (length str))
(err nil))
;; loop ends when i is at end of string or when err is set
((or (not (< i len)) err)
(if (or (> ltr 0) ; must be at least one letter, or
(equal str "->")) ; symbol can be "->"
(let ((info ()) sym)
(if pkg (push (cons ':pkg pkg) info))
(if dot (push (cons ':slot dot) info))
;(display "in symbol-token?" str)
(setf sym (sal-string-to-symbol str))
(make-token :type ':id :string str
:info info :start pos
:lisp sym))
nil))
(setq chr (char str i))
(cond ((alpha-char-p chr) (incf ltr))
; need to allow arbitrary lisp symbols
; ((member chr '(#\* #\+)) ;; special variable names can start/end
; (if (< 0 i (- len 2)) ;; with + or *
; (errexit bad pos)))
((char= chr #\/) ;; embedded / is not allowed
(errexit bad pos))
;((char= chr #\-) ;; hyphens are allowed anywhere in symbol
; (if (= ltr 0)
; (errexit errf input bad pos )
; (setq ltr 0)
; ))
((char= chr #\:)
; allowable forms are :foo, foo:bar, :foo:bar
(if (> i 0) ;; lisp keyword symbols ok
(cond ((= ltr 0)
(errexit bad pos))
((not pkg)
(setq pkg i))
(t (errexit errf input
(format nil "Too many colons in ~s" str)
pos))))
(setq ltr 0))
((char= chr #\.)
(if (or dot (= i 0) (= i (- len 1)))
(errexit bad pos)
(progn (setq dot i) (setq ltr 0)))))))
; (let ((i "foo")) (symbol-token? i 0 i #'pperror))
; (let ((i "foo..bar")) (symbol-token? i 0 i #'pperror))
; (let ((i ".bar")) (symbol-token? i 0 i #'pperror))
; (let ((i "bar.")) (symbol-token? i 0 i #'pperror))
; (let ((i "1...")) (symbol-token? i 0 i #'pperror))
; (let ((i "a1..." )) (symbol-token? i 0 i #'pperror))
; (let ((i "a{b")) (symbol-token? i 0 i #'pperror))
; (let ((i "foo-bar")) (symbol-token? i 0 i #'pperror))
; (let ((i "123-a")) (symbol-token? i 0 i #'pperror))
; (let ((i "1a23-a")) (symbol-token? i 0 i #'pperror))
; (let ((i "*foo*")) (symbol-token? i 0 i #'pperror))
; (let ((i "+foo+")) (symbol-token? i 0 i #'pperror))
; (let ((i "foo+bar")) (symbol-token? i 0 i #'pperror))
; (let ((i "foo/bar")) (symbol-token? i 0 i #'pperror))
; (let ((i ":bar")) (symbol-token? i 0 i #'pperror))
; (let ((i "::bar")) (symbol-token? i 0 i #'pperror))
; (let ((i "foo:bar")) (symbol-token? i 0 i #'pperror))
; (let ((i "cl-user:bar")) (symbol-token? i 0 i #'pperror))
; (let ((i "cl-user::bar")) (symbol-token? i 0 i #'pperror))
; (tokenize "aaa + bbb \"asdasdd\" aaa(1,2,3)")
; (tokenize "aaa+bbb \"asdasdd\" aaa(1,2,3)")
(setf *in-sal-parser* nil)
;; line number info for debugging
(setf *sal-line-number-info* t)
(setf *sal-line* 0)
(defun add-line-info-to-expression (expr token)
(let (line-no)
(cond ((and token ;; null token means do not change expr
*sal-line-number-info* ;; is this feature enabled?
(stringp *sal-input-text*))
;; first, get line number
(setf line-no (pos-to-line (token-start token) *sal-input-text*))
`(prog2 (setf *sal-line* ,line-no) ,expr))
(t expr))))
;; single statement is handled just like an expression
(setfn add-line-info-to-stmt add-line-info-to-expression)
;; list of statements is simple to handle: prepend SETF
(defun add-line-info-to-stmts (stmts token)
(let (line-no)
(cond ((and *sal-line-number-info* ;; is this feature enabled?
(stringp *sal-input-text*))
(setf line-no (pos-to-line (token-start token) *sal-input-text*))
(cons `(setf *sal-line* ,line-no) stmts))
(t stmts))))
;; PARSE-ERROR -- print error message, return from top-level
;;
(defun parse-error (e)
(unless (sal-error-line e)
(setf (sal-error-line e) *sal-input*))
(pperror e)
(return-from sal-parse (values nil e *sal-tokens*)))
;; SAL-PARSE -- parse string or token input, translate to Lisp
;;
;; If input is text, *sal-input-text* is set to the text and
;; read later (maybe) by ERREXIT.
;; If input is a token list, it is assumed these are leftovers
;; from tokenized text, so *sal-input-text* is already valid.
;; *Therfore*, do not call sal-parse with tokens unless
;; *sal-input-text* is set to the corresponding text.
;;
(defun sal-parse (grammar pat input multiple-statements file)
(progv '(*sal-input-file-name*) (list file)
(let (rslt expr rest)
; ignore grammar and pat (just there for compatibility)
; parse input and return lisp expression
(cond ((stringp input)
(setf *sal-input-text* input)
(setq input (tokenize input *reserved-words* #'parse-error))))
(setf *sal-input* input) ;; all input
(setf *sal-tokens* input) ;; current input
(cond ((null input)
(values t nil nil)) ; e.g. comments compile to nil
(t
(setf rslt (or (maybe-parse-command)
(maybe-parse-block)
(maybe-parse-conditional)
(maybe-parse-assignment)
(maybe-parse-loop)
(maybe-parse-exec)
(maybe-parse-exit)
(errexit "Syntax error")))
;; note: there is a return-from parse in parse-error that
;; returns (values nil error <unparsed-tokens>)
(cond ((and *sal-tokens* (not multiple-statements))
(errexit "leftover tokens")))
;((null rslt)
; (errexit "nothing to compile")))
(values t rslt *sal-tokens*))))))