-
Notifications
You must be signed in to change notification settings - Fork 2
/
java-syntax.lisp
1105 lines (958 loc) · 37.2 KB
/
java-syntax.lisp
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; Package: CLIMACS-JAVA-SYNTAX -*-
;;; (c) copyright 2005 by
;;; Robert Strandh ([email protected])
;;; (c) copyright 2006 by
;;; Troels Henriksen ([email protected])
;;; (c) copyright 2007 by
;;; John Q Splittist ([email protected])
;;;
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Library General Public
;;; License as published by the Free Software Foundation; either
;;; version 2 of the License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; Library General Public License for more details.
;;;
;;; You should have received a copy of the GNU Library General Public
;;; License along with this library; if not, write to the
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;;; Boston, MA 02111-1307 USA.
;;;# Syntax module for analysing Java(TM)
(in-package :climacs-java-syntax)
;;;# The command table.
(define-syntax-command-table java-table
:errorp nil)
;;;# The syntax object.
;;;
;;; We could add options here.
(define-syntax java-syntax (lr-syntax-mixin fundamental-syntax)
((package :accessor package-of
:documentation "A list of strings being the components of
the `package' definition, if any."))
(:name "Java")
(:pathname-types "java" "jav")
(:command-table java-table)
(:default-initargs :initial-state |initial-state |))
;;; Now some ways to indicate what the syntax is. Extra details could be
;;; added. For now we'll show the package, if any.
(defmethod name-for-info-pane ((syntax java-syntax) &key pane)
(declare (ignore pane))
(update-parse syntax)
(format nil "Java~@[:~{~A~^.~}~]"
(package-of syntax)))
;;;# Lexing.
;;;
;;; First we define the different states the lexer can be in (as triggered
;;; by the parser.)
(define-lexer-state lexer-string-state ()
()
(:documentation "In this state, the lexer is working inside a string
delimited by double quote characters."))
(define-lexer-state lexer-line-comment-state ()
()
(:documentation "In this state, the lexer is working inside a line
comment starting with //."))
(define-lexer-state lexer-long-comment-state ()
()
(:documentation "In this state, the lexer is working inside a long
comment delimited by /* and */."))
;;; And then we define the various elements of the language.
;;;
;;; First, some high-level concepts:
(defclass java-nonterminal (nonterminal) ())
(defclass form (java-nonterminal) ())
;;; Since we're dealing with things that might not be finished,
;;; we allow for incomplete forms at the end of the buffer.
(defclass complete-form-mixin () ())
(defclass incomplete-form-mixin () ())
(defclass comment (java-nonterminal) ())
(defclass line-comment (java-comment) ())
(defclass long-comment (java-comment) ())
;;; Of course, sometimes people type things that don't (yet) comply
;;; with the language specification.
(defclass error-symbol (java-nonterminal) ())
;;; Finally, we define the relevant lexeme. We will check the `ink' and
;;; and the `face' later during redisplay.
(defclass java-lexeme (lexeme)
((ink)
(face)))
(defclass form-lexeme (form java-lexeme) ())
;;; Keywords come in various flavours.
(defclass keyword-lexeme (form-lexeme) ())
(defclass basic-type () ())
(defclass modifier () ())
(defclass operator () ())
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun spelling-to-symbol (name)
(intern (concatenate 'string name "-LEXEME") #.*package*)))
(defmacro define-keywords (&rest keyword-names)
`(progn
,@(loop for (name . supers) in keyword-names
for real-name = (spelling-to-symbol name)
collecting `(defclass ,real-name (,@ supers keyword-lexeme) ())
into defclasses
collecting name into names
finally (return (cons `(defparameter *keyword-spellings* ',names)
defclasses)))))
(define-keywords
("abstract" modifier)
("assert" operator)
("boolean" basic-type)
("break" operator)
("byte" basic-type)
("case" operator)
("catch" operator)
("char" basic-type)
("class" operator)
("const") ; reserved but not used
("continue" operator)
("default" operator)
("do" operator)
("double" basic-type)
("else" operator)
("enum" operator)
("extends" operator)
("final" modifier)
("finally" operator)
("float" basic-type)
("for" operator)
("if" operator)
("int" basic-type)
("goto") ; reserved but not used
("implements" operator)
("import" operator)
("instanceof" operator)
("interface" operator)
("long" basic-type)
("native" basic-type)
("new" operator)
("package" operator)
("private" operator)
("package" operator)
("private" modifier)
("protected" modifier)
("public" modifier)
("return" operator)
("short" basic-type)
("static" modifier)
("striftfp" modifier)
("super" operator)
("switch" operator)
("synchronized" modifier)
("this" operator)
("throw" operator)
("throws" operator)
("transient" modifier)
("try" operator)
("void" operator)
("volatile" modifier)
("while" operator))
(defclass identifier-lexeme (form-lexeme) ())
(defclass literal-lexeme (form-lexeme) ())
(defclass integer-literal-lexeme (literal-lexeme) ())
(defclass decimal-integer-literal-lexeme (integer-literal-lexeme) ())
(defclass octal-integer-literal-lexeme (integer-literal-lexeme) ())
(defclass hex-integer-literal-lexeme (integer-literal-lexeme) ())
(defclass floating-point-literal-lexeme (literal-lexeme) ())
(defclass decimal-floating-point-literal-lexeme (floating-point-literal-lexeme) ())
(defclass hexidecimal-floating-point-literal-lexeme (floating-point-literal-lexeme) ())
;;; A badly formed, or perhaps unfinished, number.
(defclass bad-number-literal-lexeme (literal-lexeme) ())
(defclass boolean-literal-lexeme (literal-lexeme) ())
(defclass character-literal-lexeme (literal-lexeme) ())
(defclass incomplete-character-literal-lexeme (literal-lexeme incomplete-form-mixin) ())
(defclass string-literal-lexeme (literal-lexeme) ())
(defclass null-literal-lexeme (literal-lexeme) ())
(defclass separator-lexeme (form-lexeme) ())
(defclass punctuator-lexeme (form-lexeme) ())
;;; Separators: ( ) { } [ ] ; , .
(defclass semi-colon-lexeme (separator-lexeme) ())
(defclass comma-lexeme (separator-lexeme) ())
(defclass dot-lexeme (separator-lexeme) ())
(defclass delimiter-mixin () ())
(defclass opening-delimiter-mixin (delimiter-mixin) ())
(defclass closing-delimiter-mixin (delimiter-mixin) ())
(defclass left-bracket-lexeme (separator-lexeme opening-delimiter-mixin) ())
(defclass right-bracket-lexeme (separator-lexeme closing-delimiter-mixin) ())
(defclass left-parenthesis-lexeme (separator-lexeme opening-delimiter-mixin) ())
(defclass right-parenthesis-lexeme (separator-lexeme closing-delimiter-mixin) ())
(defclass left-brace-lexeme (separator-lexeme opening-delimiter-mixin) ())
(defclass right-brace-lexeme (separator-lexeme closing-delimiter-mixin) ())
;;; Operators:
;;; = < > ! ~ ? :
;;; == <= >= != && || ++ --
;;; + - * / & | ^ % << >> >>>
;;; += -= *= /= &= |= ^= %= <<= >>= >>>=
(defmacro define-operators (&rest punctuator-names)
`(progn
,@(loop for name in punctuator-names
for real-name = (intern (concatenate 'string
(string name) "-LEXEME")
#.*package*)
collecting `(defclass ,real-name (punctuator-lexeme) ()))))
(define-operators
equal left-angle-bracket right-angle-bracket exclamation tilde question
colon
eq leq geq neq and-and or-or increment decrement
plus minus asterisk slash ampersand pipe circumflex percent
left-shift right-shift unsigned-right-shift
plus-equal minus-equal asterisk-equal slash-equal ampersand-equal pipe-equal
circumflex-equal percent-equal left-shift-equal right-shift-equal
unsigned-right-shift-equal)
;;; This for annotated interfaces.
(defclass ampersand-lexeme (punctuator-lexeme) ())
;;; And something for when we come across something completely wrong.
(defclass error-lexeme (java-lexeme) ())
;;; Some lexemes that will drive the parser and lexer.
(defclass line-comment-start-lexeme (java-lexeme) ())
(defclass long-comment-start-lexeme (java-lexeme) ())
(defclass comment-end-lexeme (java-lexeme) ())
(defclass string-start-lexeme (java-lexeme) ())
(defclass string-end-lexeme (java-lexeme) ())
;;; And some lexemes used inside strings and comments.
(defclass word-lexeme (java-lexeme) ())
(defclass delimiter-lexeme (java-lexeme) ())
(defclass text-lexeme (java-lexeme) ())
;;; Some predicates for recognizing the constituents of identifiers.
;;; "The $ character should be used only in mechanically generated
;;; source code or, rarely, to access preexisting names on legacy
;;; systems."
(defun java-letter-p (ch)
(and (characterp ch)
(or (alpha-char-p ch)
(char= ch #\_)
(char= ch #\$))))
(defun java-letter-or-digit-p (ch)
(and (characterp ch)
(or (alphanumericp ch)
(char= ch #\_)
(char= ch #\$))))
;;; Something to recognise escapes, including unicode escapes (which may
;;; have multiple #\u characters).
(defun eat-escape (scan)
"Advance over an escape (after the #\\), returning T if valid so far, or NIL."
(macrolet ((fo () `(forward-object scan)))
(case (object-after scan)
((#\b #\t #\n #\f #\r #\" #\' #\\)
(fo) t)
(#\u
(loop until (end-of-buffer-p scan)
while (eql (object-after scan) #\u)
do (fo))
(loop until (end-of-buffer-p scan)
for char = (object-after scan)
with count = 0
while (and (characterp char)
(digit-char-p char 16))
do (fo) (incf count)
finally (return (or (and (end-of-buffer-p scan)
(< count 4))
(= count 4)))))
((#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7)
(loop repeat 3
until (end-of-buffer-p scan)
for char = (object-after scan)
while (and (characterp char)
(digit-char-p char 8))
do (fo))
t)
(t nil))))
;;; The default method for skipping whitespace.
(defmethod skip-inter ((syntax java-syntax) state scan)
(macrolet ((fo () `(forward-object scan)))
(loop when (end-of-buffer-p scan)
do (return nil)
until (not (whitespacep syntax (object-after scan)))
do (fo)
finally (return t))))
;;; The lexing procedure used at the toplevel. Dispatches to lex-token
;;; at the appropriate time - except for standalone dots (where the lexer
;;; doesn't know whether it's looking at a potential number or the
;;; separator in a QualifiedIdentifier).
(defmethod lex ((syntax java-syntax) (state lexer-toplevel-state) scan)
(macrolet ((fo () `(forward-object scan)))
(let ((object (object-after scan)))
(case object
(#\" (fo) (make-instance 'string-start-lexeme))
(#\' (fo)
(cond ((end-of-buffer-p scan)
(make-instance 'incomplete-character-literal-lexeme))
(t (cond ((eql (object-after scan) #\\)
(fo)
(if (not (end-of-buffer-p scan))
(unless (eat-escape scan)
(return-from lex
(make-instance 'error-lexeme)))))
(t (fo)))
(cond ((end-of-buffer-p scan)
(make-instance 'incomplete-character-literal-lexeme))
((eql (object-after scan) #\')
(fo)
(make-instance 'character-literal-lexeme))
(t (make-instance 'error-lexeme))))))
(#\[ (fo) (make-instance 'left-bracket-lexeme))
(#\] (fo) (make-instance 'right-bracket-lexeme))
(#\( (fo) (make-instance 'left-parenthesis-lexeme))
(#\) (fo) (make-instance 'right-parenthesis-lexeme))
(#\{ (fo) (make-instance 'left-brace-lexeme))
(#\} (fo) (make-instance 'right-brace-lexeme))
(#\@ (fo) (make-instance 'ampersand-lexeme))
(#\. (fo) (if (end-of-buffer-p scan)
(make-instance 'dot-lexeme)
(cond ((and (characterp (object-after scan))
(digit-char-p (object-after scan)))
(backward-object scan)
(lex-token syntax scan))
(t (make-instance 'dot-lexeme)))))
(#\- (fo) (if (end-of-buffer-p scan)
(make-instance 'minus-lexeme)
(case (object-after scan)
(#\- (fo) (make-instance 'decrement-lexeme))
(#\= (fo) (make-instance 'minus-equal-lexeme))
(t (make-instance 'minus-lexeme)))))
(#\+ (fo) (if (end-of-buffer-p scan)
(make-instance 'plus-lexeme)
(case (object-after scan)
(#\+ (fo) (make-instance 'increment-lexeme))
(#\= (fo) (make-instance 'plus-equal-lexeme))
(t (make-instance 'plus-lexeme)))))
(#\& (fo) (if (end-of-buffer-p scan)
(make-instance 'ampersand-lexeme)
(case (object-after scan)
(#\& (fo) (make-instance 'and-and-lexeme))
(#\= (fo) (make-instance 'ampersand-equal-lexeme))
(t (make-instance 'ampersand-lexeme)))))
(#\* (fo) (if (end-of-buffer-p scan)
(make-instance 'asterisk-lexeme)
(cond ((eql (object-after scan) #\=)
(fo)
(make-instance 'asterisk-equal-lexeme))
(t (make-instance 'asterisk-lexeme)))))
(#\~ (fo) (make-instance 'tilde-lexeme))
(#\! (fo) (if (end-of-buffer-p scan)
(make-instance 'exclamation-lexeme)
(cond ((eql (object-after scan) #\=)
(fo)
(make-instance 'neq-lexeme))
(t (make-instance 'exclamation-lexeme)))))
(#\/ (fo) (if (end-of-buffer-p scan)
(make-instance 'slash-lexeme)
(case (object-after scan)
(#\= (fo) (make-instance 'slash-equal-lexeme))
(#\* (fo) (make-instance 'long-comment-start-lexeme))
(#\/ (fo) (make-instance 'line-comment-start-lexeme))
(t (make-instance 'slash-lexeme)))))
(#\% (fo) (if (end-of-buffer-p scan)
(make-instance 'percent-lexeme)
(case (object-after scan)
(#\= (fo) (make-instance 'percent-equal-lexeme))
(t (make-instance 'percent-lexeme)))))
(#\< (fo) (if (end-of-buffer-p scan)
(make-instance 'left-angle-bracket-lexeme)
(case (object-after scan)
(#\= (fo) (make-instance 'leq-lexeme))
(#\< (fo)
(cond ((eql (object-after scan) #\=)
(fo)
(make-instance 'left-shift-equal-lexeme))
(t (make-instance 'left-shift-lexeme))))
(t (make-instance 'left-angle-bracket-lexeme)))))
(#\> (fo) (if (end-of-buffer-p scan)
(make-instance 'right-angle-bracket-lexeme)
(case (object-after scan)
(#\= (fo) (make-instance 'geq-lexeme))
(#\> (fo)
(cond ((eql (object-after scan) #\=)
(fo)
(make-instance 'right-shift-equal-lexeme))
((eql (object-after scan) #\>)
(fo)
(cond ((eql (object-after scan) #\=)
(fo)
(make-instance 'unsigned-right-shift-equal-lexeme))
(t (make-instance 'unsigned-right-shift-lexeme))))
(t (make-instance 'right-shift-lexeme))))
(t (make-instance 'right-angle-bracket-lexeme)))))
(#\= (fo) (if (end-of-buffer-p scan)
(make-instance 'equal-lexeme)
(cond ((eql (object-after scan) #\=)
(fo)
(make-instance 'eq-lexeme))
(t (make-instance 'equal-lexeme)))))
(#\^ (fo) (if (end-of-buffer-p scan)
(make-instance 'circumflex-lexeme)
(cond ((eql (object-after scan) #\=)
(fo)
(make-instance 'circumflex-equal-lexeme))
(t (make-instance 'circumflex-lexeme)))))
(#\| (fo) (if (end-of-buffer-p scan)
(make-instance 'pipe-lexeme)
(case (object-after scan)
(#\| (fo) (make-instance 'or-or-lexeme))
(#\= (fo) (make-instance 'pipe-equal-lexeme))
(t (make-instance 'pipe-lexeme)))))
(#\? (fo) (make-instance 'question-lexeme))
(#\: (fo) (make-instance 'colon-lexeme))
(#\; (fo) (make-instance 'semi-colon-lexeme))
(#\, (fo) (make-instance 'comma-lexeme))
(t (cond ((or (java-letter-or-digit-p object)
(eql object #\\))
(lex-token syntax scan))
(t (fo) (make-instance 'error-lexeme))))))))
;;; Lexing in strings is essentially splitting the input into words,
;;; delimters and whitespace.
(defmethod lex ((syntax java-syntax) (state lexer-string-state) scan)
(macrolet ((fo () `(forward-object scan)))
(let ((object (object-after scan)))
(cond ((eql object #\") (fo) (make-instance 'string-end-lexeme))
((eql object #\\)
(fo)
(eat-escape scan)
(make-instance 'delimiter-lexeme))
((java-letter-or-digit-p object)
(loop until (or (end-of-buffer-p scan)
(not (java-letter-or-digit-p (object-after scan))))
do (fo))
(make-instance 'word-lexeme))
(t (fo) (make-instance 'delimiter-lexeme))))))
;;; Lexing in comments is similar to strings, but in long comments we
;;; need to detect the comment end.
(defmethod lex ((syntax java-syntax) (state lexer-long-comment-state) scan)
(flet ((fo () (forward-object scan)))
(let ((object (object-after scan)))
(cond ((eql object #\*)
(fo)
(cond ((or (end-of-buffer-p scan)
(not (eql (object-after scan) #\/)))
(make-instance 'delimiter-lexeme))
(t (fo) (make-instance 'comment-end-lexeme))))
((java-letter-or-digit-p object)
(loop until (or (end-of-buffer-p scan)
(not (java-letter-or-digit-p (object-after scan))))
do (fo))
(make-instance 'word-lexeme))
(t (fo) (make-instance 'delimiter-lexeme))))))
(defmethod skip-inter ((syntax java-syntax)
(state lexer-line-comment-state)
scan)
(macrolet ((fo () `(forward-object scan)))
(loop until (or (end-of-line-p scan)
(not (whitespacep syntax (object-after scan))))
do (fo)
finally (return t))))
(defmethod lex ((syntax java-syntax) (state lexer-line-comment-state) scan)
(macrolet ((fo () `(forward-object scan)))
(cond ((end-of-line-p scan)
(make-instance 'comment-end-lexeme))
((java-letter-or-digit-p (object-after scan))
(loop until (or (end-of-buffer-p scan)
(not (java-letter-or-digit-p (object-after scan))))
do (fo))
(make-instance 'word-lexeme))
(t (fo) (make-instance 'delimiter-lexeme)))))
;;; Recognise the various types of numbers, returning the appropriate
;;; class name. We return `'bad-number-lexeme' in some circumstances where
;;; the author might just not have finished typing in. The logic detects
;;; 'long' versions separately, although the same result as the non-long
;;; version is returned for now.
(defun lex-number (scan)
(let (hex oct dot exp float-suffix)
(labels ((fo () (forward-object scan))
(eat-digits (&optional (radix 10))
(loop until (end-of-buffer-p scan)
while (and (characterp (object-after scan))
(digit-char-p (object-after scan) radix))
do (fo))))
(when (eql (object-after scan) #\0)
(fo)
(cond ((end-of-buffer-p scan)
(return-from lex-number 'decimal-integer-literal-lexeme))
((equalp (object-after scan) #\X)
(fo)
(setf hex t))
((eql (object-after scan) #\.)
(fo)
(setf dot t))
((and (characterp (object-after scan))
(digit-char-p (object-after scan) 8))
(setf oct t))
((equalp (object-after scan) #\L)
(fo)
(return-from lex-number 'decimal-integer-literal-lexeme))))
(eat-digits (cond (hex 16) (oct 8) (t 10)))
(when (end-of-buffer-p scan)
(return-from lex-number
(cond (hex 'hex-integer-literal-lexeme)
(oct 'octal-integer-literal-lexeme)
(t 'decimal-integer-literal-lexeme))))
(cond ((equalp (object-after scan) #\L)
(fo)
(return-from lex-number
(cond (hex 'hex-integer-literal-lexeme)
(oct 'octal-integer-literal-lexeme)
(t 'decimal-integer-literal-lexeme))))
(oct (return-from lex-number 'octal-integer-literal-lexeme))
((eql (object-after scan) #\.)
(when dot
(return-from lex-number 'bad-number-literal-lexeme))
(setf dot t)
(fo)
(eat-digits (cond (hex 16) (oct 8) (t 10)))))
(when (end-of-buffer-p scan)
(return-from lex-number
(if (or dot exp float-suffix)
(if hex
'hexidecimal-floating-point-literal-lexeme
'decimal-floating-point-literal-lexeme)
(if hex
'hex-integer-literal-lexeme
'decimal-integer-literal-lexeme))))
(when (equalp (object-after scan) (if hex #\P #\E))
(setf exp t)
(fo)
(when (end-of-buffer-p scan)
(return-from lex-number 'bad-number-literal-lexeme))
(if (member (object-after scan) '(#\+ #\-))
(fo))
(when (end-of-buffer-p scan)
(return-from lex-number 'bad-number-literal-lexeme))
(eat-digits))
(unless (end-of-buffer-p scan)
(when (member (object-after scan) '(#\f #\F #\d #\D))
(setf float-suffix t)
(fo)))
(return-from lex-number
(if (or dot exp float-suffix)
(if hex
'hexidecimal-floating-point-literal-lexeme
'decimal-floating-point-literal-lexeme)
(if hex
'hex-integer-literal-lexeme
'decimal-integer-literal-lexeme))))))
;;; Decide whether we're lexing an identifier (or one of the textual literals)
;;; or a number.
(defun lex-token (syntax scan)
(declare (ignore syntax))
(labels ((fo () (forward-object scan)))
(cond ((java-letter-p (object-after scan))
(let ((token (make-array 32 :element-type 'character
:adjustable t :fill-pointer 0)))
(loop until (or (end-of-buffer-p scan)
(not (or (java-letter-or-digit-p
(object-after scan)))))
do (vector-push-extend (object-after scan) token)
(fo))
(cond ((find token *keyword-spellings* :test #'string=)
(make-instance (spelling-to-symbol token)))
((string= token "null")
(make-instance 'null-literal-lexeme))
((or (string= token "true")
(string= token "false"))
(make-instance 'boolean-literal-lexeme))
(t (make-instance 'identifier-lexeme)))))
(t
(make-instance (lex-number scan))))))
;;; In the error state, just slurp full lines.
(defmethod lex ((syntax java-syntax) (state lexer-error-state) scan)
(macrolet ((fo () `(forward-object scan)))
(loop until (end-of-line-p scan)
do (fo))
(make-instance 'error-lexeme)))
;;;# Parsing
(defmacro define-java-action ((state lexeme) &body body)
`(defmethod action ((syntax java-syntax) (state ,state) (lexeme ,lexeme))
,@body))
(defmacro define-new-java-state ((state parser-symbol) &body body)
`(defmethod new-state ((syntax java-syntax)
(state ,state)
(tree ,parser-symbol))
,@body))
(define-java-action (error-reduce-state (eql nil))
(throw 'done nil))
;;; The default action for any lexeme is shift.
(define-java-action (t java-lexeme)
lexeme)
;;; The action on end-of-buffer is to reduce to the error symbol.
(define-java-action (t (eql nil))
(reduce-all error-symbol))
;;; The default new state is the error state.
(define-new-java-state (t parser-symbol) error-state)
;;; The new state when an error-state
(define-new-java-state (t error-symbol) error-reduce-state)
;;;;;;;;;;;;;;;; Top-level
#| rules
form* ->
form* -> form* form
|#
;;; parse trees
(defclass form* (java-nonterminal) ())
(define-parser-state |form* | (lexer-toplevel-state parser-state) ())
(define-parser-state form-may-follow (lexer-toplevel-state parser-state) ())
(define-parser-state |initial-state | (form-may-follow) ())
(define-new-java-state (|initial-state | form) |initial-state |)
(define-new-java-state (|initial-state | comment) |initial-state |)
(define-java-action (|initial-state | (eql nil))
(reduce-all form*))
(define-new-java-state (|initial-state | form*) |form* | )
(define-java-action (|form* | (eql nil))
(throw 'done nil))
;;;;;;;;;;;;;;;; String
;;; parse trees
(defclass string-form (form) ())
(defclass complete-string-form (string-form complete-form-mixin) ())
(defclass incomplete-string-form (string-form incomplete-form-mixin) ())
(define-parser-state |" word* | (lexer-string-state parser-state) ())
(define-parser-state |" word* " | (lexer-toplevel-state parser-state) ())
(define-new-java-state (|" word* | word-lexeme) |" word* |)
(define-new-java-state (|" word* | delimiter-lexeme) |" word* |)
(define-new-java-state (form-may-follow string-start-lexeme) |" word* |)
(define-new-java-state (|" word* | string-end-lexeme) |" word* " |)
;;; reduce according to the rule form -> " word* "
(define-java-action (|" word* " | t)
(reduce-until-type complete-string-form string-start-lexeme))
;;; reduce at the end of the buffer
(define-java-action (|" word* | (eql nil))
(reduce-until-type incomplete-string-form string-start-lexeme))
;;;;;;;;;;;;;;;; Line comment
;;; parse trees
(defclass line-comment-form (comment) ())
(define-parser-state |// word* | (lexer-line-comment-state parser-state) ())
(define-parser-state |// word* NL | (lexer-toplevel-state parser-state) ())
(define-new-java-state (form-may-follow line-comment-start-lexeme) |// word* |)
(define-new-java-state (|// word* | word-lexeme) |// word* |)
(define-new-java-state (|// word* | delimiter-lexeme) |// word* |)
(define-new-java-state (|// word* | comment-end-lexeme) |// word* NL |)
;;; reduce according to the rule form -> // word* NL
(define-java-action (|// word* NL | t)
(reduce-until-type line-comment-form line-comment-start-lexeme))
;;;;;;;;;;;;;;;; Long comment
;;; parse trees
(defclass long-comment-form (comment) ())
(defclass complete-long-comment-form (long-comment-form complete-form-mixin) ())
(defclass incomplete-long-comment-form (long-comment-form incomplete-form-mixin) ())
(define-parser-state |/* word* | (lexer-long-comment-state parser-state) ())
(define-parser-state |/* word* */ | (lexer-toplevel-state parser-state) ())
(define-new-java-state (|/* word* | word-lexeme) |/* word* |)
(define-new-java-state (|/* word* | delimiter-lexeme) |/* word* |)
(define-new-java-state (|/* word* | long-comment-start-lexeme) |/* word* |)
(define-new-java-state (|/* word* | long-comment-form) |/* word* |)
(define-new-java-state (form-may-follow long-comment-start-lexeme) |/* word* |)
(define-new-java-state (|/* word* | comment-end-lexeme) |/* word* */ |)
;;; reduce according to the rule form -> /* word* */
(define-java-action (|/* word* */ | t)
(reduce-until-type complete-long-comment-form long-comment-start-lexeme))
;;; reduce at the end of the buffer
(define-java-action (|/* word* | (eql nil))
(reduce-until-type incomplete-long-comment-form long-comment-start-lexeme))
;;; Here we search for the package name.
(defun update-package-name (buffer syntax)
(declare (ignore buffer))
(setf (package-of syntax) nil)
(with-slots (stack-top) syntax
(loop for (token . rest) on (children stack-top)
when (typep token '|package|-LEXEME)
do (loop for component in rest
until (typep component 'semi-colon-lexeme)
while (or (typep component 'dot-lexeme)
(typep component 'identifier-lexeme)
(typep component 'comment))
when (typep component 'identifier-lexeme)
collect (form-string syntax component) into components
finally (setf (package-of syntax) components)))))
(defmethod update-syntax :after ((syntax java-syntax) prefix-size suffix-size
&optional begin end)
(declare (ignore begin end))
(update-package-name (buffer syntax) syntax))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; display
(defun form-string (syntax form)
"Return the string that correspond to `form' in the buffer of
`syntax'."
(buffer-substring (buffer syntax) (start-offset form) (end-offset form)))
(define-syntax-highlighting-rules default-java-highlighting
(error-symbol (*error-drawing-options*))
(string-form (*string-drawing-options*))
(operator (*special-operator-drawing-options*))
(basic-type (:face :ink +dark-blue+))
(modifier (:face :ink +dark-green+))
(comment (*comment-drawing-options*))
(integer-literal-lexeme (:face :ink +gray50+))
(floating-point-literal-lexeme (:face :ink +gray50+)))
(defparameter *syntax-highlighting-rules* 'default-java-highlighting
"The syntax highlighting rules used for highlighting Java
syntax.")
(defmethod syntax-highlighting-rules ((syntax java-syntax))
*syntax-highlighting-rules*)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; exploit the parse
(defun form-string-p (form)
(typep form 'string-form))
(defun commentp (form)
(typep form 'comment))
(defun top-level-vector (syntax)
(coerce (children (slot-value syntax 'stack-top)) 'simple-vector))
(defun top-level-form-before-in-vector (tlv
offset
&optional ignore-comments-p)
"Return top-level form in top-level-vector `tlv' around or before `offset'
together with index of form in `tlv', or nil. If `ignore-comments-p', don't
treat comments as forms."
(loop for count from (1- (length tlv)) downto 0
for tlf = (aref tlv count)
when (and (or (not ignore-comments-p) (not (commentp tlf)))
(< (start-offset tlf) offset (end-offset tlf)))
return (values tlf count)
when (and (or (not ignore-comments-p) (not (commentp tlf)))
(<= (end-offset tlf) offset))
return (values tlf count)
finally (return nil)))
(defun top-level-form-after-in-vector (tlv
offset
&optional ignore-comments-p)
"Return top-level form in top-level-vector `tlv' around or after `offset'
together with index of form in `tlv', or nil. If `ignore-comments-p', don't
treat comments as forms."
(loop for tlf across tlv
for count from 0
when (and (or (not ignore-comments-p) (not (commentp tlf)))
(< (start-offset tlf) offset (end-offset tlf)))
return (values tlf count)
when (and (or (not ignore-comments-p) (not (commentp tlf)))
(>= (start-offset tlf) offset))
return (values tlf count)
finally (return nil)))
(defun top-level-form-around-in-vector (tlv
offset
&optional ignore-comments-p)
"Return top-level form in top-level-vector `tlv' around `offset'
together with index of form in `tlv', or nil. If `ignore-comments-p', don't
treat comments as forms."
(loop for tlf across tlv
for count from 0
when (and (or (not ignore-comments-p) (not (commentp tlf)))
(< (start-offset tlf) offset (end-offset tlf)))
return (values tlf count)
when (and (or (not ignore-comments-p) (not (commentp tlf)))
(>= (start-offset tlf) offset))
return nil
finally (return nil)))
(defun form-around (syntax offset &optional ignore-comments-p)
(top-level-form-around-in-vector
(top-level-vector syntax)
offset
ignore-comments-p))
(defgeneric opening-delimiter-p (token)
(:documentation "Is `token' an opening delimiter."))
(defmethod opening-delimiter-p (token)
nil)
(defmethod opening-delimiter-p ((token opening-delimiter-mixin))
t)
(defgeneric closing-delimiter-p (token)
(:documentation "Is `token' a closing delimiter."))
(defmethod closing-delimiter-p (token)
nil)
(defmethod closing-delimiter-p ((token closing-delimiter-mixin))
t)
(defgeneric matching-delimiter-p (token match)
(:documentation "Is `match' a matching delimiter of `token'."))
(defmethod matching-delimiter-p (token match)
nil)
(defmethod matching-delimiter-p ((token closing-delimiter-mixin)
(match opening-delimiter-mixin))
(matching-delimiter-p match token))
(defmethod matching-delimiter-p ((token left-parenthesis-lexeme)
(match right-parenthesis-lexeme))
t)
(defmethod matching-delimiter-p ((token left-bracket-lexeme)
(match right-bracket-lexeme))
t)
(defmethod matching-delimiter-p ((token left-brace-lexeme)
(match right-brace-lexeme))
t)
(defmethod backward-one-expression ((mark mark) (syntax java-syntax))
(let ((tlv (top-level-vector syntax)))
(multiple-value-bind (form count)
(top-level-form-before-in-vector tlv (offset mark) t)
(when form
(if (closing-delimiter-p form)
(loop for index from count downto 0
for match = (aref tlv index)
with delims = 0
when (eql (class-of match)
(class-of form))
do (incf delims)
when (matching-delimiter-p form match)
do (decf delims)
until (zerop delims)
finally (cond ((zerop delims)
(setf (offset mark) (start-offset match))
(return t))
(t (return nil))))
(setf (offset mark) (start-offset form)))))))
(defmethod forward-one-expression ((mark mark) (syntax java-syntax))
(let ((tlv (top-level-vector syntax)))
(multiple-value-bind (form count)
(top-level-form-after-in-vector tlv (offset mark) t)
(when form
(if (opening-delimiter-p form)
(loop for index from count below (length tlv)
for match = (aref tlv index)
with delims = 0
when (eql (class-of match)
(class-of form))
do (incf delims)
when (matching-delimiter-p form match)
do (decf delims)
until (zerop delims)
finally (cond ((zerop delims)
(setf (offset mark) (end-offset match))
(return t))
(t (return nil))))
(setf (offset mark) (end-offset form)))))))
(defmethod forward-one-list (mark (syntax java-syntax))
(let ((tlv (top-level-vector syntax)))
(multiple-value-bind (form count)
(top-level-form-after-in-vector tlv (offset mark))
(when form
(loop for index from count below (length tlv)
for match = (aref tlv index)
with delims = ()
when (opening-delimiter-p match)
do (push match delims)
when (closing-delimiter-p match)
do (cond ((null delims)
(return nil))
(t (cond ((matching-delimiter-p match
(car delims))
(pop delims)
(when (null delims)
(setf (offset mark) (end-offset match))
(return t)))
(t (return nil)))))
finally (return nil))))))
(defmethod backward-one-list (mark (syntax java-syntax))
(let ((tlv (top-level-vector syntax)))
(multiple-value-bind (form count)
(top-level-form-before-in-vector tlv (offset mark))
(when form
(loop for index from count downto 0
for match = (aref tlv index)
with delims = ()
when (closing-delimiter-p match)
do (push match delims)
when (opening-delimiter-p match)
do (cond
((null delims)
(return nil))
(t (cond ((matching-delimiter-p match
(car delims))
(pop delims)
(when (null delims)
(setf (offset mark) (start-offset match))
(return t)))
(t (return nil)))))
finally (return nil))))))
(drei-motion:define-motion-fns list)
(defmethod backward-one-down ((mark mark) (syntax java-syntax))
(let ((tlv (top-level-vector syntax)))
(multiple-value-bind (form count)
(top-level-form-before-in-vector tlv (offset mark))
(when form
(loop for index from count downto 0
for match = (aref tlv index)
when (closing-delimiter-p match)