forked from metawilm/cl-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsepython.cl
More file actions
1245 lines (1046 loc) · 38.4 KB
/
Copy pathparsepython.cl
File metadata and controls
1245 lines (1046 loc) · 38.4 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
;;; Parsing Python
(in-package :python)
(eval-when (:compile-toplevel)
;; update when yacc integrated
(require :yacc (merge-pathnames #p"yacc.fasl" *compile-file-pathname*))
(use-package :yacc))
(eval-when (:load-toplevel :execute)
;; update when yacc integrated
(require :yacc (merge-pathnames #p"yacc.fasl" *load-pathname*))
(use-package :yacc))
(defvar *reserved-words*
;; Well, a few of these are not actually reserved words in Python
;; yet, because of backward compatilibity reasons, but they will
;; be in the future (`as' is an example).
'(def class return yield lambda
and or not for in is
print import from as assert break continue global del exec pass
try except finally raise
if elif else while))
;; NEW!!
(defgrammar python-grammar (grammar)
()
(:left-associative or)
(:left-associative and)
(:left-associative not)
(:left-associative in) ;; and "not in"
(:left-associative is) ;; and "is not"
(:left-associative < <= > >= <> != ==)
(:left-associative |\|| )
(:left-associative ^)
(:left-associative &)
(:left-associative << >> )
(:left-associative + -)
(:left-associative * / % //)
(:left-associative unary-plusmin)
(:left-associative ~)
(:right-associative **)
(:non-associative high-prec)
(:lexemes identifier number string #|punctuation|# newline indent dedent
;; punctuation:
= [ ] |(| |)| < > { } |.| |,| |:| |\|| ^ % + - * / ~ & |`|
&= // << >> <> != += -= *= /= //= %= ** <= >= ^= |\|=| ==
**= <<= >>= |...|
|;| ;; multiple statements same line
@ ;; decorator
;; keywords:
def class return yield lambda
and or not for in is
print import from as assert break continue global del exec pass
try except finally raise
if elif else while )
)
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun generate-python-rule (name)
(check-type name symbol)
(let* ((name (intern name #.*package*))
(str (symbol-name name))
(len (length str))
(item (intern (subseq str 0 (- len 1)) #.*package*))
;;(name-2 (intern (concatenate 'string str "2") #.*package*))
(dp 'defproduction))
(ecase (aref str (1- len))
(#\+ `((,dp (,name python-grammar) (,item) ($1))
(,dp (,name python-grammar) (,name |,| ,item) ((append $1 (list $3))))))
(#\* `((,dp (,name python-grammar) ())
(,dp (,name python-grammar) (,name ,item) ((append $1 (list $2))))))
(#\? `((,dp (,name python-grammar) ())
(,dp (,name python-grammar) (,item) ($1))))))))
(defmacro python-prods (&rest prodrules)
(let ((res ()))
(loop for (name . rest) in prodrules
if (keywordp name)
do (assert (null rest))
(dolist (rule (generate-python-rule name))
(push rule res))
else if (eq (car rest) ':or)
do (dolist (x (cdr rest))
(push `(defproduction (,name python-grammar)
,(if (symbolp x) (list x) (car x))
,(if (symbolp x) '($1) (cdr x)))
res))
else
do (push `(defproduction (,name python-grammar) ,@rest)
res))
`(progn ,@(nreverse res))))
(python-prods
;; Rules (including their names) are taken from the Python
;; grammar in file in CPython CVS, file Python/Grammar/Grammar, 20040827.
;; The start symbol: regular python files
(python-grammar ( file-input ) ((cons 'file-input (nreverse $1))))
(file-input () ())
(file-input (file-input newline) ($1))
(file-input (file-input stmt) ((cons $2 $1)))
(decorator (@ dotted-name newline) ((list 'decorator $1 $2)))
(decorator (@ dotted-name |(| arglist |)| newline) ((list 'decorator $2 $4)))
(:decorator+)
(:decorator+?)
;; XXX for now: ignore decorators above function definition
(funcdef (decorator+? def identifier parameters |:| suite) ((list 'funcdef $3 $4 $6)))
;;(parameters (|(| varargslist? |)|) ($2))
(parameters ( |(| |)| ) ((list nil nil nil)))
(parameters ( |(| parameter-list5 |)| ) ($2))
(parameter-list5 (defparameter+) ((list $1 nil nil)))
(parameter-list5 (defparameter+ ni-*-ident ni-**-ident) ((list $1 $2 $3)))
(parameter-list5 (defparameter+ ni-*-ident ) ((list $1 $2 nil)))
(parameter-list5 (defparameter+ ni-**-ident) ((list $1 nil $2)))
(parameter-list5 ( *-ident ni-**-ident) ((list nil $1 $2)))
(parameter-list5 ( *-ident ) ((list nil $1 nil)))
(parameter-list5 ( **-ident ) ((list nil nil $1)))
(defparameter+ (defparameter) ((list $1)))
(defparameter+ (defparameter+ |,| defparameter) ((append $1 (list $3))))
(ni-*-ident ( |,| *-ident ) ($2))
(*-ident ( * identifier ) ($2))
(ni-**-ident ( |,| **-ident) ($2))
(**-ident ( ** identifier ) ($2))
(defparameter (fpdef) ($1))
(defparameter (fpdef = test) ((cons $1 $3)))
#+(or)
(progn (varargslist4 :or
((|(| |)|) . (nil))
((|(| va4-item |)|))
(va4va4-item+ |)|) ($2))
(:va4-item+)
(va4-item :or
fpdef comma?
((fpdef = test comma?))
((* identifier comma?))
((** identifier comma?)))
(varargslist4 (|(| stuff-c |)|) ($2))
(varargslist4 (|(| * identifier |)|) ((list $2 $4)))
(varargslist4 (|(| stuff-c * identifier |)|) ((list $2 $4)))
(varargslist4 (|(| ** identifier |)|) ((list $2 $4)))
(varargslist4 (|(| stuff-c ** identifier |)|) ((list $2 $4)))
(varargslist4 (|(| * identifier |,| ** identifier |)|) ((list $2 $4 $7)))
(varargslist4 (|(| stuff-c * identifier |,| ** identifier |)|) ((list $2 $4 $7)))
(varargslist3 (|(| stuff |)|) ($2))
(varargslist3 (|(| stuff-c |)|) ($2))
(varargslist3 (|(| * identifier |)|) ((list $2 $4)))
(varargslist3 (|(| stuff-c * identifier |)|) ((list $2 $4)))
(varargslist3 (|(| ** identifier |)|) ((list $2 $4)))
(varargslist3 (|(| stuff-c ** identifier |)|) ((list $2 $4)))
(varargslist3 (|(| * identifier |,| ** identifier |)|) ((list $2 $4 $7)))
(varargslist3 (|(| stuff-c * identifier |,| ** identifier |)|) ((list $2 $4 $7)))
(stuff ())
(stuff (stuff+) ($1))
(stuff+ (fpdef) ($1))
(stuff+ (fpdef = test) ((cons $1 $3)))
(stuff+ (stuff+ |,| fpdef) ((list $1 $3)))
(stuff+ (stuff+ |,| fpdef = test) ((list $1 (cons $3 $5))))
(stuff-c (fpdef |,|) ($1))
(stuff-c (fpdef = test |,|) ((list $1 $3)))
(stuff-c (stuff-c fpdef |,|) ((list $1 $2)))
(stuff-c (stuff-c fpdef = test |,|) ((list $1 $2 $4)))
(varargslist2 (|(| |fpdef--=--test--,*| fpdef comma? |)|) ((list $1 $2 nil nil)))
(varargslist2 (|(| |fpdef--=--test--,*| fpdef = test comma? |)|) ((list $1 $2 $4 nil nil)))
(varargslist2 (|(| |fpdef--=--test--,+| * identifier |,| ** identifier |)|)
((list $1 $3 $6)))
(varargslist2 (|(| |fpdef--=--test--,+| * identifier |)|)
((list $1 $3 nil)))
(varargslist2 (|(| |fpdef--=--test--,+| ** identifier |)|)
((list $1 nil $3)))
(varargslist2 (|(| * identifier |,| ** identifier |)|)
((list nil $2 $5)))
(varargslist2 (|(| * identifier |)|)
((list nil $2 nil)))
(varargslist2 (|(| ** identifier |)|)
((list nil nil $2)))
;;(:comma?)
;;(comma (|,|))
(:varargslist?)
(varargslist (|fpdef--=--test--,*| ) ((list $1 nil nil)))
(varargslist (|fpdef--=--test--,+| * identifier |,| ** identifier ) ((list $1 $3 $6)))
(varargslist (|fpdef--=--test--,+| * identifier ) ((list $1 $3 nil)))
(varargslist (|fpdef--=--test--,+| ** identifier ) ((list $1 nil $3)))
(varargslist ( * identifier |,| ** identifier ) ((list nil $2 $5)))
(varargslist ( * identifier ) ((list nil $2 nil)))
(varargslist ( ** identifier ) ((list nil nil $2)))
(|fpdef--=--test--,| :or
((fpdef |,|) . ($1))
((fpdef = test |,|) . ((list $1 $2 $3))))
(:|fpdef--=--test--,+|)
(:|fpdef--=--test--,*|))
(fpdef :or
((identifier) . ($1))
((|(| fplist |)|) . (`(list ,$2)))) ;; WWW or just $2
(fplist (fpdef comma--fpdef* comma?) ((cons $1 $2)))
(:comma--fpdef*)
(comma--fpdef (|,| fpdef) ($2))
(:comma?)
(comma (|,|) ((list $1)))
(stmt :or simple-stmt compound-stmt)
(simple-stmt (small-stmt semi--small-stmt* semi? newline) ((if $2 (list $1 $2) $1)))
(semi--small-stmt (|;| small-stmt) ($2))
(:semi--small-stmt*)
(:semi?)
(semi (|;|))
(small-stmt :or expr-stmt print-stmt del-stmt pass-stmt flow-stmt
import-stmt global-stmt exec-stmt assert-stmt)
;; XXX assign-expr only is correct for 1 level assignents (a = b, not for a=b=c)
(expr-stmt (testlist expr-stmt2) ((cond ((and $2 (eq (car $2) '=))
(list 'assign-expr $1 (caadr $2))) ;; XXX caadr ok?
($2
(list 'augassign-expr (car $2) (car $1) (cdr $2)))
(t
$1))))
(expr-stmt2 (augassign testlist) ((cons $1 $2)))
(expr-stmt2 (=--testlist*) ((when $1 (list '= $1))))
(:=--testlist*)
(=--testlist (= testlist) ($2))
(augassign :or += -= *= /= %= &= |\|=| ^= <<= >>= **= //= )
(print-stmt :or
((print test |,--test*| comma?) . ((list 'print (cons $2 $3) (if $4 t nil))))
((print >> test |,--test*| comma?) . ((list 'print->> $3 $4 (if $5 t nil)))))
(:|,--test*|)
;;(:|,--test+|)
(|,--test| (|,| test) ($2))
(del-stmt (del exprlist) ((list $1 $2)))
(pass-stmt (pass) ((list $1)))
(flow-stmt :or break-stmt continue-stmt return-stmt raise-stmt yield-stmt)
(break-stmt (break) ((list $1)))
(continue-stmt (continue) ((list $1)))
(return-stmt (return testlist?) ((list $1 $2)))
(:testlist?)
(yield-stmt (yield testlist?) ((list $1 $2)))
(raise-stmt (raise) ((list $1)))
(raise-stmt (raise test) ((list $1 $2)))
(raise-stmt (raise test |,| test) ((list $1 $2 $4)))
(raise-stmt (raise test |,| test |,| test) ((list $1 $2 $4 $6)))
;; "import" module ["as" name] ( "," module ["as" name] )*
(import-stmt :or import-normal import-from)
(import-normal (import dotted-as-name comma--dotted-as-name*) ((list $1 $2 $3)))
(:comma--dotted-as-name*)
(comma--dotted-as-name ( |,| dotted-as-name) ($2))
(import-from (from dotted-name import import-from-2) ((list $1 $2 $3)))
(import-from-2 :or
*
((import-as-name comma--import-as-name*) . ((list $1 $2))))
(:comma--import-as-name*)
(comma--import-as-name (|,| import-as-name) ($2))
(import-as-name (identifier) ((list 'normal $1)))
(import-as-name (identifier as identifier) ((list 'as $2 $3)))
(dotted-as-name (dotted-name) ((list 'normal $1)))
(dotted-as-name (dotted-name as identifier) ((list 'as $1 $2 $3)))
(dotted-name (identifier dot--name*) ((list $1 $2)))
(:dot--name*)
(dot--name (|.| identifier))
(global-stmt (global identifier comma--identifier*) ((list $1 $2 $3)))
(:comma--identifier*)
(comma--identifier (|,| identifier) ($2))
(exec-stmt (exec expr) ((list $1 $2)))
(exec-stmt (exec expr in test) ((list $1 $2 $4)))
(exec-stmt (exec expr in test |,| test) ((list $1 $2 $4 $6)))
(assert-stmt (assert test comma--test?) ((list $1 $2)))
(:comma--test?)
(comma--test (|,| test) ($2))
(compound-stmt :or if-stmt while-stmt for-stmt try-stmt funcdef classdef)
(if-stmt (if test |:| suite elif--test--suite* else--suite)
(`(if ((,$2 ,$4) ,@$5) ,$6)))
(if-stmt (if test |:| suite elif--test--suite*)
(`(if ((,$2 ,$4) ,@$5) nil)))
(:elif--test--suite*)
(elif--test--suite (elif test |:| suite) ((list $2 $4)))
(else--suite (else |:| suite) ($3))
(while-stmt (while test |:| suite else--suite?) ((list $1 $2 $4 $5)))
(:else--suite?)
(for-stmt (for exprlist in testlist |:| suite else--suite?)
((list 'for-in $2 $4 $6 $7))
(:precedence high-prec))
(try-stmt :or
((try |:| suite except--suite+ else--suite?)
. ((list 'try-except $3 $4 $5)))
((try |:| suite finally |:| suite)
. ((list 'try-finally $3 $6))))
;;(:except--suite+)
;;(except--suite (except |:| suite) ((list $1 $3)))
(except--suite (except |:| suite) (`(except (nil nil) ,$3)))
(except--suite (except test |:| suite) (`(except (,$2 nil) ,$4)))
(except--suite (except test |,| test |:| suite) (`(except (,$2 ,$4) ,$6)))
(except--suite+ (except--suite) ((list $1)))
(except--suite+ (except--suite+ except--suite) ((append $1 (list $2))))
;;; (except--suite+ (except |:| suite) (`(except (nil nil) ,$3)))
;;; (except--suite+ (except test |:| suite) (`(except (,$2 nil) ,$4)))
;;; (except--suite+ (except test |,| test |:| suite) (`(except (,$2 ,$4) $6)))
;;;
;;; (except--suite+ (except--suite+ except |:| suite) ((append $1 (list $2 $4))))
(suite :or
((simple-stmt) . ((list 'suite (list $1))))
((newline indent stmt+ dedent) . ((list 'suite $3))))
;; old (:stmt+)
(stmt+ (stmt) ((list $1)))
(stmt+ (stmt+ stmt) ((append $1 (list $2))))
#|
(test :or
((and-test or--and-test*) . ((if $2 (list $1 $2) $1)))
lambdef)
(:or--and-test*)
(or--and-test (or and-test) ((list $1 $2)))
(and-test (not-test and--not-test*) ((if $2 (list $1 $2) $1)))
(:and--not-test*)
(and--not-test (and not-test) ((list $1 $2)))
(not-test :or
((not not-test) . ((list $1 $2)))
comparison)
(comparison (expr comp-op--expr*) ((if $2 (list $1 $2) $1)))
(:comp-op--expr*)
(comp-op--expr (comp-op expr) ((list $1 $2)))
(comp-op :or < > == >= <= <> != in is
((not in) . ((list 'not-in)))
((is not) . ((list 'is-not))))
(expr (xor-expr XOR--xor-expr*) ((if $2 (list $1 $2) $1)))
(:XOR--xor-expr*)
(XOR--xor-expr (|\|| xor-expr) ((list 'xor $2)))
(xor-expr (and-expr AND--and-expr*) ((if $2 (list $1 $2) $1)))
(:AND--and-expr*)
(AND--and-expr (^ and-expr) ((list $1 $2)))
(and-expr (shift-expr AMP--shift-expr*) ((if $2 (list $1 $2) $1)))
(:AMP--shift-expr*)
(AMP--shift-expr (& shift-expr) ((list $1 $2)))
(shift-expr (arith-expr <<\|>>--arith-expr*) ((if $2 (list $1 $2) $1)))
(:<<\|>>--arith-expr*)
(<<\|>>--arith-expr :or
((<< arith-expr) . ((list $1 $2)))
((>> arith-expr) . ((list $1 $2))))
(arith-expr (term +---term*) ((format t "~&Here ~s ~s~%" $1 $2)
(if $2
(if (and (consp $1) (eq (car $1) (car $2)))
(append $1 (cdr $2))
`(,(car $2) ,$1 ,@(cdr $2)))
$1)))
(:+---term*)
(+---term :or
((+ term) . ((list $1 $2)))
((- term) . ((list $1 $2))))
(term (factor termop--factor*) ((if $2 `(,(car $2) ,$1 ,@(cdr $2)) $1)))
(:termop--factor*)
(termop--factor (termop factor) ((list $1 $2)))
(termop :or * / % //)
(factor :or
((factorop factor) . ((list $1 $2)))
power)
(factorop :or |+| |-| |~|) ;; XXX set precedence
(power (atom trailer*) ((if $2 (list $1 $2) $1)))
(:trailer*)
(power (atom trailer* **--factor) ((cond ($3 (list $1 $2 $3))
($2 (list $1 $2))
($1 $1))))
(**--factor (** factor) ((list $1 $2)))
|#
(expr (binop2-expr) ($1)) ;; YYY: binop-2 2 2 2 2
;; WWW new: using precedences
(test :or
lambdef
binop-expr)
(binop-expr :or
((binop-expr and binop-expr) . ((list 'binary $2 $1 $3)))
((binop-expr or binop-expr) . ((list 'binary $2 $1 $3)))
((not binop-expr) . ((list 'unary $1 $2)))
((binop-expr < binop-expr) . ((list 'comparison $2 $1 $3)))
((binop-expr <= binop-expr) . ((list 'comparison $2 $1 $3)))
((binop-expr > binop-expr) . ((list 'comparison $2 $1 $3)))
((binop-expr >= binop-expr) . ((list 'comparison $2 $1 $3)))
((binop-expr != binop-expr) . ((list 'comparison $2 $1 $3)))
((binop-expr <> binop-expr) . ((list 'comparison '!= $1 $3)))
((binop-expr == binop-expr) . ((list 'comparison $2 $1 $3)))
((binop-expr in binop-expr) . ((list 'comparison $2 $1 $3)))
((binop-expr is binop-expr) . ((list 'comparison $2 $2 $1 $3))))
(binop-expr (binop2-expr) ($1) (:precedence or))
(binop2-expr :or
atom
((atom trailer+) . ((parse-trailers (list 'trailers $1 $2))))
((binop2-expr + binop2-expr) . ((list 'binary $2 $1 $3)))
((binop2-expr - binop2-expr) . ((list 'binary $2 $1 $3)))
((binop2-expr * binop2-expr) . ((list 'binary $2 $1 $3)))
((binop2-expr / binop2-expr) . ((list 'binary $2 $1 $3)))
((binop2-expr ** binop2-expr) . ((list 'binary $2 $1 $3)))
((binop2-expr // binop2-expr) . ((list 'binary $2 $1 $3)))
((binop2-expr << binop2-expr) . ((list 'binary $2 $1 $3)))
((binop2-expr >> not binop2-expr) . ((list 'binary $2 $1 $3)))
((binop2-expr & binop2-expr) . ((list 'binary $2 $1 $3)))
((binop2-expr ^ binop2-expr) . ((list 'binary $2 $1 $3)))
((binop2-expr \| binop2-expr) . ((list 'binary $2 $1 $3)))
((binop2-expr ~ binop2-expr) . ((list 'binary $2 $1 $3)))
((binop2-expr % binop2-expr) . ((list 'binary $2 $1 $3)))
)
;; some with explicit precedences
(binop2-expr (binop2-expr not in binop2-expr) ((list 'comparison 'not-in $1 $4)) (:precedence in))
(binop2-expr (binop2-expr is not binop2-expr) ((list 'comparison 'not-in $1 $4)) (:precedence is))
(binop2-expr (+ binop2-expr) ((list 'unary $1 $2)) (:precedence unary-plusmin))
(binop2-expr (- binop2-expr) ((list 'unary $1 $2)) (:precedence unary-plusmin))
#+(or)(bin-op :or
and or not
< <= > >= == != <>
in is
((not in) . ('not-in))
((is not) . ('is-not))
+ -
* / // **
^ & << >>)
(atom :or
((|(| |)|) . ((list 'testlist nil t)))
((|(| testlist-gexp |)|) . ((cons 'testlist $2)))
((|[| |]|) . ((list 'list nil)))
((|[| listmaker |]|) . ((list 'list $2)))
((|{| |}|) . ((list 'dict nil)))
((|{| dictmaker |}|) . ((list 'dict $2)))
((|`| testlist1 |`|) . ((list 'repr-list $2)))
((identifier) . ((list 'identifier $1)))
((number) . ($1))
((string+) . ($1)))
(string+ (string) ($1))
(string+ (string+ string) ((concatenate 'string $1 $2)))
(listmaker (test list-for) ((list $1 $2)))
(listmaker (test comma--test* comma?) ((cons $1 $2)))
(:comma--test*)
(testlist-gexp (test gen-for) ((list $1 $2)))
(testlist-gexp (test comma--test* comma?) ((list (cons $1 $2) (if $3 t nil))))
;; new:
;;(testlist-gexp (test) ($1))
;;(testlist-gexp (testlist-gexp |,| test) ((cons $3 $1)))
(lambdef (lambda #+(or)varargslist? |:| test))
#+(or)(progn (:trailer+)
(trailer :or
((|(| arglist? |)|) . ((list 'call $2)))
((|[| subscriptlist |]|) . (`(subscription . ,$2)))
((|.| identifier) . (`(attributeref (identifier ,$2))))))
(trailer+ :or
((|(| arglist? |)|) . ((list (list 'call $2))))
((|[| subscriptlist |]|) . ((list (list 'subscription $2))))
((|.| identifier) . (`((attributeref (identifier ,$2)))))
((trailer+ |(| arglist? |)|) . ((append $1 (list (list 'call $3)))))
((trailer+ |[| subscriptlist |]|) . ((append $1 (list (list 'subscription $3)))))
((trailer+ |.| identifier) . ((append $1 `((attributeref (identifier ,$3)))))))
(:arglist?)
;;(subscriptlist (subscript comma--subscript*) ((append (list $1) $2)))
(subscriptlist (subscript comma--subscript* comma?) ((list (append (list $1) $2)
(if $3 t nil))))
(:comma--subscript*)
(comma--subscript (|,| subscript) ($2))
(subscript :or
|...|
test
((test? |:| test? sliceop?) . (`(slice ,$1 ,$3 . ,$4))))
(:sliceop?)
(sliceop (|:| test?))
(:test?)
;; old:
;; (exprlist (expr comma--expr* comma?) ((cons $1 $2)))
;; new:
(exprlist (expr exprlist2) ((list 'exprlist
(cons $1 (butlast $2))
(car (last $2)))))
(exprlist2 :or
(() . (nil))
((|,|) . ((list t)))
((|,| expr exprlist2) . ((list $2 $3))))
#|
(testlist (test comma--test* comma?) ((if $2 (cons $1 $2) $1)))
|#
#|(testlist (test) ($1))
(testlist (test |,|) ($1 :comma)) ; n
(testlist (testlist |,| test) ((list $1 $3)))
|#
(testlist (test testlist2) ((list 'testlist
(cons $1 (butlast $2))
(car (last $2)))))
(testlist2 :or
(() . ((list nil)))
((|,|) . ((list t)))
((|,| test testlist2) . ((cons $2 $3))))
(testlist-safe :or
((test) . ($1))
((test comma--test+ comma?) . ((list $1 $2))))
(:comma--test+)
;;(:comma--expr*)
;;(comma--expr (|,| expr) ($2))
(dictmaker :or
;;((test |:| test comma?) . ((cons $1 $3)))
((test |:| test comma--test--\:--test* comma?) . ((cons (cons $1 $3) $4))))
(:comma--test--\:--test*)
(comma--test--\:--test (|,| test |:| test) ((cons $2 $4)))
(classdef (class identifier OB--testlist--CB? |:| suite)
((list $1 $2 $3 $5)))
(:OB--testlist--CB?)
(OB--testlist--CB (|(| testlist |)|) ($2))
(arglist (argument--comma* arglist-2) ((append $1 $2)))
(arglist-2 :or
;; optional comma is semantically meaningless
((argument comma?) . ((list $1)))
((|*| test comma--**--test?) . ((append (list (list $1 $2))
$3
nil)))
((|**| test) . ((list (list $1 $2)))))
(:argument--comma*)
(argument--comma (argument |,|) ($1))
(:comma--**--test?)
(comma--**--test (|,| ** test) ((list (list $2 $3))))
;; old:
;; (argument (test--=? test gen-for?) ((list $1 $2 $3)))
;; new:
(argument (test) ($1))
(argument (test = test) ((list $2 $1 $3)))
(argument (test gen-for) ((list 'gen-for $1 $2)))
;;test--=? test gen-for?) ((list $1 $2 $3)))
#+(or) ;; unused now
(progn (:test--=?)
(test--= (test =) ((list $1 $2)))
(:gen-for?))
(list-iter :or list-for list-if)
(list-for (for exprlist in testlist-safe list-iter?) (`((list-for-in ,$2 ,$4) . ,$5)))
(:list-iter?)
(list-if (if test list-iter?) (`((list-if ,$2) . ,$3)))
(gen-iter :or gen-for gen-if)
(gen-for (for exprlist in test gen-iter?))
(gen-if (if test gen-iter?) ((list $1 $2 $3)))
(:gen-iter?)
(testlist1 (test |,--test*|) ((list $1 $2))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; AST manipulating
(defun parse-trailers (ast)
;; foo[x].a => (trailers (id foo) ((subscription (id x)) (attributeref (id a))))
;; => (attributeref (subscription (id foo) (id x)) (id a))
(assert (eq (car ast) 'trailers))
(destructuring-bind (primary trailers)
(cdr ast)
(loop for tr in trailers
with res = primary
do (setf res `(,(car tr) ,res ,@(cdr tr)))
finally (return res))))
#+(or) ;; test
(parse-trailers '(trailers (id foo) ((subscription (id x)) (attributeref (id a)))))
#|
1 char: =[]()<>{}.,:|^%+-*/~
2 chars: != // << >> <> != ==
+= -= *= /= //= %= ^= |= **= <<= >>=
3 chars: **= <<= >>= "
|#
(defvar *tab-width-spaces* 8)
(defmacro read-char-nil ()
"Returns a character, or NIL on eof"
`(read-char *standard-input* nil nil t))
(defmacro identifier-char1-p (c)
"Says whether C is a valid character to start an identifier with. ~@
C must be either a character or nil."
`(and ,c
(or (alpha-char-p ,c)
(eql ,c #\_))))
(defmacro identifier-char2-p (c)
"Says whether C is a valid character to occur in an identifier as ~@
second or later character. C must be either a character or nil."
`(and ,c
(or (alphanumericp ,c)
(eql ,c #\_))))
(defun read-identifier (first-char)
"Returns the identifier read as string. ~@
Identifiers start start with an underscore or alphabetic character; ~@
second and further characters must be alphanumeric or underscore."
(assert (identifier-char1-p first-char))
(let ((res (make-array 6
:element-type 'character
:adjustable t
:fill-pointer 0)))
(vector-push-extend first-char res)
(let ((c (read-char-nil)))
(excl:while (identifier-char2-p c)
(vector-push-extend c res)
(setf c (read-char-nil)))
(unless (null c)
(unread-char c)))
(intern res #.*package*)))
;;; STRINGS
(defun read-string (first-char)
"Returns string as a string"
(assert (member first-char '(#\' #\") :test 'eql))
(macrolet ((read-char-error () `(or (read-char-nil)
(error "Unexpected end of file"))))
(let ((second (read-char-error))
(third (read-char-nil)))
;; "" ('') but not """ (''') --> empty string
(when (and (char= first-char second)
(or (null third)
(char/= first-char third)))
(when third
(unread-char third))
(return-from read-string ""))
;;; """ or ''': a long? multi-line? string
(when (char= first-char second third)
(let ((res (make-array 50
:element-type 'character
:adjustable t
:fill-pointer 0)))
(let ((x (read-char-error))
(y (read-char-error))
(z (read-char-error)))
(loop
(when (char= first-char x y z)
(return))
(vector-push-extend
(shiftf x y z (read-char-error))
res))
(return-from read-string res))))
;; non-empty string with one starting quote
(let ((res (make-array 30
:element-type 'character
:adjustable t
:fill-pointer 0)))
(vector-push-extend second res)
(let ((c third))
(excl:while (char/= c first-char)
(vector-push-extend c res)
(setf c (read-char-error)))
(return-from read-string res))))))
#+(or)
(defun read-number (first-char)
;; Actually, parsing numbers is a weird (dare I say, wrong) in
;; Python. These examples make that clear (note the !'s) (using
;; Python 2.3.4):
;;
;; integers: input -> base-10 val system used
;; 11 11 10 (decimal)
;; 011 9 8 (octal)
;; 0x11 17 16 (hexadecimal)
;;
;; integers with exponent:
;; 11e3 = 011e3 = 11E3 = 11e+3 == 11E+3 = 11000.0 (a float)
;; 11e-3 = 0.011 (approx)
;; 0x11e3 = regular hex 4579
;;
;; floats: input -> base-10 val system used
;; 11.3 11.3 10
;; 011.3 11.3 10 ! (exp: error)
;; 0x11.3 *syntax error*
;;
;; floats with exponent:
;; 011.3e3 = 11.3e3 = 011.3e+3 = 11.3e+3 = 11300
;; 011.3e-3 = 11.3e-3 = 11300
;;
;; imag ints: input -> base-10 val system used
;; 11j 11j 10
;; 011j 11j 10 ! (exp: 9j)
;; 0x11j *syntax error* ! (exp: 17j)
;;
;; imag ints with exponent:
;; 1e3j = 1000j etc.
;;
;; imag floats: input -> base-10 val system used
;; 11.3j 11.3j 10
;; 011.3j 11.3j 10 ! (exp: error)
;; 0x11.3j *syntax error*
;;
;; imag floats with exp:
;; 1.0e-3j -> 0.001j etc.
;;
;; Integers (dec, oct, hex) may have the suffix `l' or `L', meaning
;; `long', though the difference between regular and long integers
;; has mostly ceased to exist (operations on regular integers that
;; return an integer in the range of long integers, implicitly
;; convert the result to a long). We ignore any difference between
;; regular and long integers.
;; The algorithm:
;; keep reading digits, or one dot
;; until next char is neither `j' nor hexdigit
;; if there's a dot -> ...
)
(defun read-number (first-char)
(assert (digit-char-p first-char 10))
(let* ((res (digit-char-p first-char 10))
(base 10))
(when (char= first-char #\0)
(let ((second (read-char-nil)))
(cond
((not second)
(return-from read-number 0))
((member second '(#\x #\X) :test #'char=)
(setf base 16))
((digit-char-p second 8)
(setf base 8)
(setf res (digit-char-p second 8)))
((char= second #\.) ;; <digit>.<something> like 1.2 or 1.23 or 1.234 etc
(unread-char second)) ;; and continue
(t ;; non-number stuff after a zero, like `]' in `x[0]'
(unread-char second)
(return-from read-number 0)))))
(let* ((c (read-char-nil))
(d (and c (digit-char-p c base))))
(excl:while d
(setf res (+ (* res base) d)
c (read-char-nil)
d (and c (digit-char-p c base))))
;; only decimal numbers may have a fraction
(if (and c
(= base 10)
(char= c #\.))
;; let's collect all digits and call the Lisp reader on it
(let ((fraction-v (make-array 4
:element-type 'character
:adjustable t
:fill-pointer 0)))
(vector-push #\0 fraction-v)
(vector-push #\. fraction-v)
(let ((c (read-char-nil)))
(excl:while (and c (digit-char-p c 10))
(vector-push-extend c fraction-v)
(setf c (read-char-nil)))
(incf res (read-from-string fraction-v))
(when (and c
(not (digit-char-p c 10)))
(unread-char c))))
(when c
(unread-char c))))
res))
;;; punctuation
(defmacro punct-char1-p (c)
`(and ,c
(find ,c "`=[]()<>{}.,:|^&%+-*/~;"))) ;; XXX `;' included
(defmacro punct-char-not-punct-char1-p (c)
"Punctuation ! may only occur in the form != "
`(and ,c
(char= ,c #\!)))
(defmacro punct-char2-p (c1 c2)
"Recognizes: // << >> <> != <= >=
== += -= *= /= %= ^= |= &= ** **= <<= >>= "
`(and ,c1 ,c2
(or (and (char= ,c2 #\= )
(member ,c1 '( #\+ #\- #\* #\/ #\% #\^ #\&
#\| #\! #\= #\< #\> )))
(and (char= ,c1 ,c2)
(member ,c1 '( #\* #\< #\> #\/ #\< #\> )))
(and (char= ,c1 #\< )
(char= ,c2 #\> )))))
(defmacro punct-char3-p (c1 c2 c3)
"Recognizes: **= <<= >>= //="
`(and ,c1 ,c2 ,c3
(char= ,c3 #\= )
(char= ,c1 ,c2)
(member ,c1 '( #\* #\< #\> #\/ ))))
(defun read-punctuation (c1)
"Returns puncutation as a string."
(assert (or (punct-char1-p c1)
(punct-char-not-punct-char1-p c1)))
(let ((c2 (read-char-nil)))
(if (punct-char2-p c1 c2)
(let ((c3 (read-char-nil)))
(if (punct-char3-p c1 c2 c3)
;; 3 chars
(make-array 3 :element-type 'character
:initial-contents (list c1 c2 c3))
;; 2 chars
(progn (when c3 (unread-char c3))
(make-array 2 :element-type 'character
:initial-contents (list c1 c2)))))
;; 1 char, or two of three dots
(if (and c2 (char= #\. c1 c2))
(let ((c3 (read-char-nil)))
(if (and c3 (char= c3 #\.))
"..."
(error "Characters `..' may only occur as in `...'")))
(if (punct-char1-p c1)
(progn (when c2 (unread-char c2))
(string c1))
(error
"Character `!' may only occur as in `!=', not standalone"))))))
(defun punctuation->token (punc-str)
" Punctuation
1 char: =[]()<>{}.,:|^%+-*/~ and also: ;
2 chars: != // << >> <> !=
+= -= *= /= %= ^= |= **= <<= >>=
3 chars: **= <<= >>= "
(intern punc-str)) ;; easiest!
#+(or)
(cdr (assoc punc-str '(("=" . =) ("[" . [) ("]" . ]) ("(" . |(|) (")" . |)|)
("<" . <) (">" . >) ("{" . {) ("}" . }) ("." . |.|)
("," . |,|) (":" . |:|) ("|" . |\||) ("^" . ^)
("%" . %) ("+" . +) ("-" . -) ("*" . *) ("/" . /) ("~" . ~))
:test #'string=))
(defun read-whitespace ()
"Reads all whitespace, until first non-whitespace character.
If Newline was found inside whitespace, values returned are (t N)
where N is the amount of whitespace after the Newline (N >= 0)
before the first non-whitespace character (in other words, the
indentation of the first non-whitespace character) measured in
spaces, where each Tab is equivalent to *tab-width-spaces* spaces.
If no Newline was encountered, returns NIL.
If EOF encountered, NIL is returned."
(let ((found-newline nil)
(c (read-char-nil))
(n 1))
(loop
(cond ((not c) (return-from read-whitespace
(if found-newline (values t n) nil)))
((char= c #\Newline) (setf found-newline t
n 0))
((char= c #\Space) (incf n))
((char= c #\Tab) (incf n *tab-width-spaces*))
(t (unread-char c)
(return-from read-whitespace
(if found-newline (values t n) nil))))
(setf c (read-char-nil)))))
(defun read-comment-line (c)
"Read until the end of the line, excluding the Newline."
(assert (char= c #\#))
(let ((c (read-char-nil)))
(excl:while (and c (char/= c #\Newline))
(setf c (read-char-nil)))
(unread-char c)))
#+(or)
(defun test-prog ()
"if a == 2:
if b > 3:
pass
else:
pass
else:
pass")
#+(or)
(defun test-prog ()
"if a == 2:
b = 24
if b > 3:
pass
else:
pass
else:
pass")
#+(or)
(defun test-prog ()
"def foo(a, b):
if a == 1:
a = 2
if b > 3:
3 + 23
b
else:
6 + 23
a = 5
return a
elif a == 6:
pass71
pass72
elif a == 8:
b = 9
else:
pass10
pass11")