-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse-loop.lisp
More file actions
1601 lines (1358 loc) · 60.3 KB
/
Copy pathparse-loop.lisp
File metadata and controls
1601 lines (1358 loc) · 60.3 KB
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
;;;; -*- Mode: Lisp -*-
;;;; parse-loop.lisp --
;;;; Parsing of iteration constructs.
;;;; See the file COPYING for copyright and license information.
;;;; Portions of code lifted from IT.BESE.ARNESI and CL-WALKER.
(in-package "CLAST")
;;;;===========================================================================
;;;; Protocol implementation.
;;;---------------------------------------------------------------------------
;;; parse-form LOOP
(defmethod parse-form ((op (eql 'loop)) form
&rest keys
&key
;; enclosing-form
;; macroexpand
;; (environment *cl-global-env*)
&allow-other-keys)
"The main entry method for the parsing of LOOP forms."
(if (is-loop-keyword (second form)) ; Should be enough...
(apply #'parse-extended-loop form keys)
(apply #'parse-simple-loop form keys)
))
(defun parse-simple-loop (loop-form
&rest keys
&key
enclosing-form
macroexpand
(environment *cl-global-env*)
&allow-other-keys)
(declare (ignore macroexpand))
(values
(make-instance 'simple-loop-form
:top enclosing-form
:source loop-form
:body-env environment
:progn (apply #'parse-form-seq
(rest loop-form)
keys))
environment)
)
(defun parse-extended-loop (loop-form
&rest keys
&key
enclosing-form
macroexpand
(environment *cl-global-env*)
&allow-other-keys
)
(declare (ignore enclosing-form macroexpand))
;; Now we have a bit of kruft.
;; Accumulation variables, those appearing after INTO in
;; accumulation clauses should be treated "as WITH variables",
;; according to the ANSI spec.
;; Unfortunately, the ANSI spec is quite unclear about "where" such
;; WITH declarations should go.
;;
;; I just decide to find such variables and have them in the initial
;; environment.
;;
;; NB. The initial implementation below is wrong as it could "mask"
;; some variables in initialization forms, as it is equivalent to
;; sticking a number of WITH clauses at the beginning of the LOOP
;; form.
;; The way to fix it would be to have the accumulation variables
;; appear only in the environments used to parse THEN forms in
;; FOR/AS-EQUALS clauses and then in regular clauses.
;;
;; FTTB I keep the simple (and "wrong") implementation, trusting
;; that it will not result in too many mishaps, given current
;; programming styles.
(let* ((acc-vars-n-types
(collect-accumulation-vars loop-form))
(loop-parsing-env
(augment-environment
environment
:variable (mapcar #'second acc-vars-n-types)
:declare acc-vars-n-types))
)
(start-loop-parsing (rest loop-form)
#| environment |#
loop-parsing-env
keys ())))
(defgeneric parse-loop-clause (clause-kwd
form ; (first form) == clause-kwd
clauses
env
&rest keys
&key
enclosing-form
macroexpand
environment
&allow-other-keys)
(:documentation "Parses a single LOOP 'clause'.
The methods of the generic function dispatch on the first argument,
which is EQL-specialized on various LOOP keywords. The FORM is
actually the rmaining LOOP form to be parsed. CLAUSES are the LOOP
clauses parsed so far and ENV is the resulting environment.
The methods return three values
Arguments and Values:
clause-kwd : the LOOP keyword to be dispatched on.
form : the part of the LOOP form to be parsed; (first form) == clause-kwd.
clauses : the clauses parsed so far.
env : the environment to be used while parsing.
keys : a plist of keyword arguments (the next ones)
enclosing-form : the form enclosing the LOOP.
macroexpand : if and how to macroexpand the various (sub)forms.
environment : the environment to be used while parsing (same as env).
parsed-clause : the clause just parsed.
remaining-loop-form : the rest of the LOOP form to be parsed.
new-env : a possibly augmented env.
"
)
(:method ((clause-kwd symbol)
form ; (first form) == clause-kwd
clauses
env
&rest keys
&key
&allow-other-keys)
(if (is-loop-keyword clause-kwd)
(apply #'parse-loop-clause (as-loop-kwd clause-kwd) form clauses env keys)
(error 'ast-parse-error
:format-control "Illegal LOOP top level clause keyword ~A."
:format-arguments (list clause-kwd))
))
)
(defmacro advance (forms-place &optional (n 1))
`(setf ,forms-place (nthcdr ,n ,forms-place)))
(defmacro next-form (forms-place &optional next-token)
(if (and next-token (symbolp next-token))
`(setf ,next-token (first ,forms-place))
`(first ,forms-place)))
(defun start-loop-parsing (loop-form env keys clauses
&aux
(loop-kwd (first loop-form)))
(if (is-loop-keyword loop-kwd)
(multiple-value-bind (clause rest-loop-form env)
(apply #'parse-loop-clause loop-kwd loop-form clauses env keys)
(continue-loop-parsing rest-loop-form
(cons clause clauses)
env
keys))
(error 'ast-parse-error
:format-control "Unrcognized LOOP keyword ~A."
:format-arguments (list loop-kwd)))
)
(defun continue-loop-parsing (loop-forms
clauses
env
keys)
(if (null loop-forms)
(finish-loop-parsing clauses env keys)
(let ((loop-kwd (next-form loop-forms)))
(unless (symbolp loop-kwd)
(error 'ast-parse-error
:format-control "Incorrect LOOP keyword ~S."
:format-arguments (list loop-kwd)
))
(multiple-value-bind (loop-clause rest-loop-form env)
(apply #'parse-loop-clause
(intern (symbol-name loop-kwd) "KEYWORD")
loop-forms
clauses
env
keys)
(continue-loop-parsing rest-loop-form
(cons loop-clause clauses)
env
keys)
))))
(defun finish-loop-parsing (clauses env keys)
(destructuring-bind (&key
environment
enclosing-form
macroexpand
&allow-other-keys)
keys
(declare (ignore macroexpand))
(values
(make-instance 'loop-form
:top enclosing-form
:loop-clauses (nreverse clauses)
:body-env env
)
environment)))
;;;===========================================================================
;;; named-clause --
(defgeneric named-clause-p (x)
(:method ((x loop-clause)) (eq :named (loop-clause-name x)))
(:method ((x t)) nil))
(defgeneric named-clause-name (x)
(:method ((x loop-clause)) (first (loop-clause-subclauses x))))
(defmethod parse-loop-clause ((loop-kwd (eql :named))
form
clauses
env
&rest keys
&key
enclosing-form
macroexpand
(environment env)
&allow-other-keys)
(declare (ignorable clauses env) ; Allegro 11.x whines otherwise.
(ignore macroexpand keys))
(let ((loop-name (second form)))
(unless (symbolp loop-name)
(error 'ast-parse-error
:format-control "Incorrect name for LOOP NAMED clause ~S."
:format-arguments (list loop-name)
))
(values ; (list loop-kwd loop-name)
(make-loop-clause :named (list loop-name) enclosing-form)
(cddr form)
(augment-environment environment
:block (list loop-name)))))
;;;===========================================================================
;;; intially/finally clause --
(defmethod parse-loop-clause ((loop-kwd (eql :initially))
form
clauses
env
&rest keys
&key
enclosing-form
macroexpand
(environment env)
&allow-other-keys)
(declare (ignore enclosing-form environment macroexpand))
(parse-loop-compound-forms-clause loop-kwd form clauses env keys))
(defmethod parse-loop-clause ((loop-kwd (eql :finally))
form
clauses
env
&rest keys
&key
enclosing-form
macroexpand
(environment env)
&allow-other-keys)
(declare (ignore enclosing-form environment macroexpand))
(parse-loop-compound-forms-clause loop-kwd form clauses env keys))
;;;===========================================================================
;;; variable-clause --
;;; The <variable-clause>'s all deal with "variables" that are
;;; introduced in a LOOP form.
;;;---------------------------------------------------------------------------
;;; with clause --
(defgeneric with-clause-p (x)
(:method ((x loop-clause)) (eq :with (loop-clause-name x)))
(:method ((x t)) nil))
(defmethod parse-loop-clause ((loop-kwd (eql :with))
form
clauses
env
&rest keys
&key
enclosing-form
macroexpand
(environment env)
&allow-other-keys)
(declare (ignorable clauses) ; Allegro 11.x whines about this.
(ignore enclosing-form macroexpand environment))
(labels ((parse-var-spec (rest-form with-subclauses vars)
(let ((var-spec (second rest-form))
(var-type t)
(var-form nil)
)
(unless (or (symbolp var-spec)
(consp var-spec))
(error 'ast-parse-error
:format-control "Incorrect variable name in LOOP ~S."
:format-arguments (list var-spec)
))
(advance rest-form 2)
(let ((next-token (next-form rest-form)))
(when (loop-kwd= next-token :of-type)
(setf var-type (second rest-form))
(advance rest-form 2)
(next-form rest-form next-token)
)
(setf vars
(nconc (associate-var-types var-spec var-type)
vars))
(when (loop-kwd= next-token '=)
(setf var-form (apply #'parse (second rest-form)
:environment env
keys))
(advance rest-form 2)
(next-form rest-form next-token)
)
(push (make-loop-var-subclause
var-spec
var-type
(and var-form
(list (make-loop-subclause ':=
(list var-form)))))
with-subclauses)
;; One var done, let's see if there are other ones.
(if (loop-kwd= next-token 'and)
(parse-var-spec rest-form with-subclauses vars)
(let* ((variables (mapcan #'rest vars))
(declares (remove t vars :key #'first))
(ne (augment-environment env
:variable variables
:declare declares))
)
(values (make-loop-clause :with (nreverse with-subclauses))
rest-form
ne)
)))
))
)
(parse-var-spec form () ())))
;;;---------------------------------------------------------------------------
;;; for/as clause --
;;; The most complicated one...
;;; I could probably break it up in all its components, but I prefer
;;; to jumble it all together and use subfunctions.
(defmethod parse-loop-clause ((loop-kwd (eql :as))
form
clauses
env
&rest keys
&key
;; (environment env)
&allow-other-keys)
(apply #'parse-loop-clause :for form clauses env keys))
(defmethod parse-loop-clause ((loop-kwd (eql :for))
form
clauses
env
&rest keys
&key
enclosing-form
macroexpand
(environment env)
&allow-other-keys
&aux
(rest-form form)
(next-token loop-kwd) ; EQL (first form)
(subclause-vars ())
(subclause-var-types ())
)
(declare (ignorable clauses) ; Avoid Allegro 11.x whining.
(ignore enclosing-form macroexpand environment))
;; The variable FOR-AS-CLAUSE in each subfunction below is an
;; initially empty list that contains the actual FOR clauses joined
;; by AND.
;; I.e., FOR V1 ... AND V2 ... AND VK
;; Will eventually be kept in the variable as
;;
;; (<VK ...> ... <V2 ...> <V1 ...>)
;;
;; Reversal of this list will happen in
;; CONTINUE-PARSE-FOR-AS-SUBCLAUSE.
(labels ((parse-var-spec (for-as-kwd rest-form for-as-clause)
(declare (type list for-as-clause))
(let ((var (second rest-form))
(var-type t)
)
(assert (member (as-loop-kwd for-as-kwd)
'(:for :as)
:test #'eq))
;; Ensure subclause-vars and subclause-var-types are
;; empty after an 'and'.
(setf subclause-vars ()
subclause-var-types ())
;; Remember the clause's vars.
;; This could be done just before, but it is clearer to make
;; each step explicit.
(setf subclause-vars
(nconc (flatten var)
subclause-vars))
(advance rest-form 2)
(next-form rest-form next-token)
(when (loop-kwd= next-token :of-type)
(setf var-type (second rest-form))
(advance rest-form 2)
(next-form rest-form next-token)
(setf subclause-var-types
(nconc (associate-var-types var var-type)
subclause-var-types)
)
)
(push (make-loop-var-subclause var var-type ()) for-as-clause)
(case (as-loop-kwd next-token)
((:from :downfrom
:to :upto :below :above :downto
:by
)
(parse-arithmetic-subclause next-token
rest-form
for-as-clause))
((:in :on)
(parse-list-subclause next-token
rest-form
for-as-clause))
(:=
(parse-equals-subclause next-token
rest-form
for-as-clause))
(:across
(parse-across-subclause next-token
rest-form
for-as-clause))
(:over ; Yep. This would be nice...
(parse-over-subclause next-token
rest-form
for-as-clause))
(:being
(parse-being-subclause next-token
rest-form
for-as-clause))
(t (error 'ast-parse-error
:format-control "Unexpected LOOP keyword ~A."
:format-arguments (list next-token)))
)
)
)
(continue-parse-for-as-subclause (rest-form
next-token
for-as-clause)
(declare (type list for-as-clause))
(if (loop-kwd= next-token :and)
(parse-var-spec :and rest-form for-as-clause)
(let ((ne (augment-environment env
:variable subclause-vars
:declare subclause-var-types))
)
(values (make-loop-clause :for
(mapcar #'reverse-subclauses
(nreverse for-as-clause)))
rest-form
ne)
)))
(parse-arithmetic-subclause (next-token rest-form for-as-clause)
(declare (type list for-as-clause))
(let ((value-form (second rest-form))
(var-clause (first for-as-clause))
)
;; We always have one artihmetic subclause.
(push (make-loop-subclause
(as-loop-kwd next-token)
(list
(apply #'parse value-form :environment env keys)))
(subclauses var-clause))
(advance rest-form 2)
(next-form rest-form next-token)
;; We may have a second...
(when (is-loop-arithmetic-keyword next-token)
(push (make-loop-subclause
next-token
(list
(apply #'parse (second rest-form) :environment env keys)))
(subclauses var-clause))
(advance rest-form 2)
(next-form rest-form next-token)
)
;; ... and possibly a third.
(when (is-loop-arithmetic-keyword next-token)
(push (make-loop-subclause
next-token
(list
(apply #'parse (second rest-form) :environment env keys)))
(subclauses var-clause))
(advance rest-form 2)
(next-form rest-form next-token)
)
(continue-parse-for-as-subclause rest-form
next-token
for-as-clause))
)
(parse-list-subclause (next-token rest-form for-as-clause)
(declare (type list for-as-clause))
(let ((value-form (second rest-form))
(var-clause (first for-as-clause))
)
(push (make-loop-subclause
next-token
(list
(apply #'parse value-form :environment env keys)))
(subclauses var-clause))
(advance rest-form 2)
(next-form rest-form next-token)
(when (loop-kwd= next-token :by)
(push (make-loop-subclause
:by
(list
(apply #'parse (second rest-form) :environment env keys)))
(subclauses var-clause))
(advance rest-form 2)
(next-form rest-form next-token)
)
(continue-parse-for-as-subclause rest-form
next-token
for-as-clause))
)
(parse-equals-subclause (next-token rest-form for-as-clause)
(declare (type list for-as-clause))
(let ((value-form (second rest-form))
(var-clause (first for-as-clause))
)
(push (make-loop-subclause
(as-loop-kwd next-token)
(list
(apply #'parse value-form :environment env keys)))
(subclauses var-clause))
(advance rest-form 2)
(next-form rest-form next-token)
(when (loop-kwd= next-token :then)
(push (make-loop-subclause
:then
(list
(apply #'parse (second rest-form) :environment env keys)))
(subclauses var-clause))
(advance rest-form 2)
(next-form rest-form next-token)
)
(continue-parse-for-as-subclause rest-form
next-token
for-as-clause))
)
(parse-across-subclause (next-token rest-form for-as-clause)
(declare (type list for-as-clause))
(let ((value-form (second rest-form))
(var-clause (first for-as-clause))
)
(assert (loop-kwd= next-token :across))
(push (make-loop-subclause
:across
(list
(apply #'parse value-form :environment env keys)))
(subclauses var-clause))
(advance rest-form 2)
(next-form rest-form next-token)
(continue-parse-for-as-subclause rest-form
next-token
for-as-clause))
)
(parse-over-subclause (next-token rest-form for-as-clause)
(parse-across-subclause next-token rest-form for-as-clause)
)
(parse-being-subclause (next-token rest-form for-as-clause)
(declare (type list for-as-clause))
(let ((value-form (second rest-form))
(var-clause (first for-as-clause))
)
(assert (loop-kwd= next-token :being))
;; Pointless to add the :BEING subclause, but I do it nevertheless.
(push (make-loop-subclause :being ())
(subclauses var-clause))
; (push next-token for-as-clause)
(case (as-loop-kwd value-form) ; Just a misnomer carried over from above...
((:each :the)
(advance rest-form 2)
(next-form rest-form next-token))
(t
(advance rest-form)
(next-form rest-form next-token))
)
(case (as-loop-kwd next-token)
((:hash-key :hash-keys :hash-value :hash-values)
(parse-hash-subclause next-token
rest-form
for-as-clause))
((:symbol :symbols
:present-symbol :present-symbols
:external-symbol :external-symbols)
(parse-package-subclause next-token
rest-form
for-as-clause))
((:record :records
:tuple :tuples)
(parse-sql-query-subclause next-token
rest-form
for-as-clause))
(t
(error 'ast-parse-error
:format-control "Unrecognized LOOP BEING keyword ~A."
:format-arguments (list next-token)))
)
))
(parse-hash-subclause (next-token rest-form for-as-clause)
(let ((var-form (second rest-form))
(var-clause (first for-as-clause))
)
(assert (member (as-loop-kwd next-token)
'(:hash-key :hash-keys :hash-value :hash-values)
:test #'eq))
(let ((hash-subclause (first (subclauses var-clause)))
(in-of-kwd var-form) ; Renaming...
(hash-kv-subclause
(make-loop-subclause (as-loop-kwd next-token) ()))
)
;; (push next-token for-as-clause)
(push hash-kv-subclause
(subclauses hash-subclause))
(case (as-loop-kwd in-of-kwd)
((:in :of)
(advance rest-form 2)
(next-form rest-form next-token)
)
(t
(error 'ast-parse-error
:format-control "Illigal LOOP BEING syntax at ~A."
:format-arguments (list in-of-kwd))))
(push (apply #'parse next-token :environment env keys)
(subclauses hash-kv-subclause))
(advance rest-form)
(next-form rest-form next-token)
(when (loop-kwd= next-token :using)
(let ((using-subclause
(make-loop-subclause :using ()))
)
(push using-subclause
(subclauses hash-subclause))
(advance rest-form)
(next-form rest-form next-token)
(handler-case
(destructuring-bind (htkv-kwd htkv-var)
next-token
(unless (or (loop-kwd= htkv-kwd :hash-value)
(loop-kwd= htkv-kwd :hash-key))
(error 'ast-parse-error
:format-control "Illegal LOOP HASH syntax ~S."
:format-arguments (list next-token)))
(push next-token (subclauses using-subclause))
(push htkv-var subclause-vars)
(advance rest-form)
(next-form rest-form next-token)
)
(ast-parse-error (ape)
;; Re-signal.
(error ape))
(error (e)
(format *error-output* "Error: CLAST:~%")
(error e)))
))
(reverse-subclauses hash-subclause)
(continue-parse-for-as-subclause rest-form
next-token
for-as-clause)
))
)
(parse-package-subclause (next-token rest-form for-as-clause)
(let ((var-form (second rest-form))
(var-clause (first for-as-clause))
)
(assert (member (as-loop-kwd next-token)
'(:symbol :symbols
:present-symbol :present-symbols
:external-symbol :external-symbols)
:test #'eq))
(let ((pkg-subclause (first (subclauses var-clause)))
(in-of-kwd var-form) ; Renaming...
(pkg-var-subclause
(make-loop-subclause (as-loop-kwd next-token) ()))
)
(push pkg-var-subclause
(subclauses pkg-subclause))
(case (as-loop-kwd in-of-kwd)
((:in :of)
(advance rest-form 2)
(next-form rest-form next-token)
(push (apply #'parse next-token :environment env keys)
(subclauses pkg-var-subclause))
)
)
(advance rest-form)
(next-form rest-form next-token)
(continue-parse-for-as-subclause rest-form
next-token
for-as-clause))
)
)
(parse-sql-query-subclause (next-token rest-form for-as-clause)
(let ((var-form (second rest-form))
(var-clause (first for-as-clause))
)
(assert (member (as-loop-kwd next-token)
'(:record :records
:tuple :tuples)
:test #'eq))
(let ((sql-subclause (first (subclauses var-clause)))
(in-of-kwd var-form) ; Renaming...
(sql-var-subclause
(make-loop-subclause (as-loop-kwd next-token) ()))
)
(push sql-var-subclause
(subclauses sql-subclause))
(case (as-loop-kwd in-of-kwd)
((:in :of)
(advance rest-form 2)
(next-form rest-form next-token))
(t
(error 'ast-parse-error
:format-control "Illegal LOOP SQL syntax ~A."
:format-arguments (list (second rest-form)))
)
)
(push (apply #'parse next-token :environment env keys) ; Query expr.
(subclauses sql-var-subclause))
(advance rest-form)
(next-form rest-form next-token)
(when (loop-kwd= next-token :not-inside-transaction)
(let ((nit-subclause
(make-loop-subclause :not-inside-transaction
(list
(apply #'parse (second rest-form)
:environment env
keys))))
)
(push nit-subclause (subclauses sql-subclause))
(advance rest-form 2)
(next-form rest-form next-token)))
(when (loop-kwd= next-token :get-all)
(let ((ga-subclause
(make-loop-subclause :get-all
(list
(apply #'parse (second rest-form)
:environment env
keys))))
)
(push ga-subclause (subclauses sql-subclause))
(advance rest-form 2)
(next-form rest-form next-token)))
(reverse-subclauses sql-subclause)
(continue-parse-for-as-subclause rest-form next-token for-as-clause)
))
)
)
(parse-var-spec loop-kwd rest-form ())
))
;;;---------------------------------------------------------------------------
;;; main clauses
(defun parse-loop-compound-forms-clause (cfc-kwd form clauses env keys)
(declare (ignore clauses))
(labels ((parse-compound-forms (rest-forms compound-forms)
(if (and rest-forms
(not (is-loop-keyword (first rest-forms))))
(parse-compound-forms (rest rest-forms)
(cons (apply #'parse (first rest-forms)
:environment env
keys)
compound-forms))
(values (make-loop-clause (as-loop-kwd cfc-kwd)
(nreverse compound-forms))
rest-forms
env)))
)
(parse-compound-forms (rest form) ()))
)
;;; unconditional clause --
(defmethod parse-loop-clause ((loop-kwd (eql :do))
form
clauses
env
&rest keys
&key
;; (environment env)
&allow-other-keys
)
(parse-loop-compound-forms-clause loop-kwd form clauses env keys)
)
(defmethod parse-loop-clause ((loop-kwd (eql :doing))
form
clauses
env
&rest keys
&key
;; (environment env)
&allow-other-keys
)
(parse-loop-compound-forms-clause loop-kwd form clauses env keys)
)
(defmethod parse-loop-clause ((loop-kwd (eql :return))
form
clauses
env
&rest keys
&key
enclosing-form
macroexpand
(environment env)
&allow-other-keys
)
(declare (ignore enclosing-form macroexpand environment))
(if (is-loop-keyword (second form))
(error 'ast-parse-error
:format-control "LOOP keyword RETURN followed by ~
another LOOP keyword (~A)."
:format-arguments (list (second form)))
(let ((return-clause
(make-loop-clause :return
(list (apply #'parse (second form) keys)))
#|(list :return
(apply #'parse (second form) keys))|#
)
)
(continue-loop-parsing (cddr form)
(cons return-clause clauses)
env
keys))
))
;;; accumulation clauses --
(defun parse-accumulation-clause (acc-kwd form clauses env keys)
(declare (ignore clauses))
(let* ((acc-clause (make-loop-clause (as-loop-kwd acc-kwd) ()))
(rest-form form)
(next-token (first rest-form))
)
(if (null (rest form))
(error 'ast-parse-error
:format-control "Missing accumulation form after ~A."
:format-arguments (list acc-kwd))
(let ((acc-form (second rest-form)))
(cond ((is-loop-keyword acc-form)
(error 'ast-parse-error
:format-control "LOOP keyword ~A found after ~A."
:format-arguments (list acc-form acc-kwd)))
(t
(push (apply #'parse acc-form
:environment env
keys)
(subclauses acc-clause))
(advance rest-form 2)
(next-form rest-form next-token)
))))
(when (loop-kwd= next-token :into)
;; This makes things very tricky!
;; The variable should be available in the various environments
;; used for parsing the "stepping" functions.
;; Of course this calls for a different, delayed, parsing
;; routine; maybe based on saving "parsing thunks".
(push (make-loop-subclause (as-loop-kwd next-token)
(list (apply #'parse (second rest-form)
:environment env
keys)))
(subclauses acc-clause))
(advance rest-form 2)
(next-form rest-form next-token)
;; Context dependency...
(case (as-loop-kwd acc-kwd)
((:collect :collecting :nconc :nconcing)
;; No type-spec expected.
(unless (or (null rest-form) ; We are done.
(and (is-loop-keyword next-token) ; No <type-spec>.
(not (eq (as-loop-kwd next-token) :of-type))))
(error 'ast-parse-error
:format-control "Illegal LOOP syntax after list collection (~A)."
:format-arguments (list next-token)))