This repository has been archived by the owner on Sep 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
scala-mode-syntax.el
1047 lines (901 loc) · 42.3 KB
/
scala-mode-syntax.el
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
;;;; scala-mode-syntax.el - Major mode for editing scala, syntax
;;; Copyright (c) 2012 Heikki Vesalainen
;;; For information on the License, see the LICENSE file
;;; Based on Scala Language Specification (SLS) Version 2.9
;;;;
;;;; Scala syntax regular expressions
;;;;
;;; Based on the Scala language specification 2.9. Note: order is not
;;; the same as in the document, as here things are declared before
;;; used.
;;; A note on naming. Things that end with '-re' are regular
;;; expressions. Things that end with '-group' are regular expression
;;; character groups without the enclosing [], i.e. they are not
;;; regular expressions, but can be used in declaring one.
;; single letter matching groups (Chapter 1)
(defconst scala-syntax:hexDigit-group "0-9A-Fa-f")
(defconst scala-syntax:UnicodeEscape-re (concat "\\\\u[" scala-syntax:hexDigit-group "]\\{4\\}"))
(defconst scala-syntax:upper-group "[:upper:]\\$") ;; missing _ to make ids work
(defconst scala-syntax:upperAndUnderscore-group (concat "_" scala-syntax:upper-group ))
(defconst scala-syntax:lower-group "[:lower:]")
(defconst scala-syntax:letter-group (concat scala-syntax:lower-group scala-syntax:upper-group)) ;; TODO: add Lt, Lo, Nl
(defconst scala-syntax:digit-group "0-9")
(defconst scala-syntax:letterOrDigit-group (concat
scala-syntax:upperAndUnderscore-group
scala-syntax:lower-group
scala-syntax:digit-group))
(defconst scala-syntax:opchar-safe-group "!%&*+/?\\\\^|~-") ;; TODO: Sm, So
(defconst scala-syntax:opchar-unsafe-group "#:<=>@")
(defconst scala-syntax:opchar-group (concat scala-syntax:opchar-unsafe-group
scala-syntax:opchar-safe-group))
;; Scala delimiters (Chapter 1), but no quotes
(defconst scala-syntax:delimiter-group ".,;")
;; Integer Literal (Chapter 1.3.1)
(defconst scala-syntax:nonZeroDigit-group "1-9")
(defconst scala-syntax:octalDigit-group "0-7")
(defconst scala-syntax:decimalNumeral-re
(concat "0"
"\\|[" scala-syntax:nonZeroDigit-group "][" scala-syntax:digit-group "]*"))
(defconst scala-syntax:hexNumeral-re (concat "0x[" scala-syntax:hexDigit-group "]+"))
(defconst scala-syntax:octalNumeral-re (concat "0[" scala-syntax:octalDigit-group "]+"))
(defconst scala-syntax:integerLiteral-re (concat "-?" ;; added from definition of literal
"\\(" scala-syntax:hexNumeral-re
"\\|" scala-syntax:octalNumeral-re
"\\|" scala-syntax:decimalNumeral-re
"\\)[Ll]?"))
;; Floating Point Literal (Chapter 1.3.2)
(defconst scala-syntax:exponentPart-re (concat "\\([eE][+-]?[" scala-syntax:digit-group "]+\\)"))
(defconst scala-syntax:floatType-re "[fFdD]")
(defconst scala-syntax:floatingPointLiteral-re
(concat "-?" ;; added from definition of literal
"\\([" scala-syntax:digit-group "]+\\.[" scala-syntax:digit-group "]*"
scala-syntax:exponentPart-re "?" scala-syntax:floatType-re "?"
"\\|" "\\.[" scala-syntax:digit-group "]+"
scala-syntax:exponentPart-re "?" scala-syntax:floatType-re "?"
"\\|" "[" scala-syntax:digit-group "]+" scala-syntax:exponentPart-re
"\\|" "[" scala-syntax:digit-group "]+" scala-syntax:floatType-re "\\)"))
(defconst scala-syntax:number-safe-start-re
(concat "[^_" scala-syntax:letter-group "]"))
;; Boolean Literals (Chapter 1.3.3)
(defconst scala-syntax:booleanLiteral-re "true|false")
;; Escape Sequences (Chapter 1.3.6)
(defconst scala-syntax:escapeSequence-re "\\\\['btnfr\"\\\\]")
;; Octal Escape Sequences (Chapter 1.3.6)
(defconst scala-syntax:octalEscape-re (concat "\\\\[" scala-syntax:octalDigit-group "\\]\\{1,3\\}"))
;; Character Literals (Chapter 1.3.4)
(defconst scala-syntax:characterLiteral-re
(concat "\\('\\)\\(" "[^\\\\]" ;; should be just printable char, but this is faster
"\\|" scala-syntax:escapeSequence-re
"\\|" scala-syntax:octalEscape-re
"\\|" scala-syntax:UnicodeEscape-re "\\)\\('\\)"))
(defconst scala-syntax:string-escape-re
(concat scala-syntax:escapeSequence-re
"\\|" scala-syntax:octalEscape-re
"\\|" scala-syntax:UnicodeEscape-re))
;; String Literals (Chapter 1.3.5)
(defconst scala-syntax:stringElement-re
(concat "\\(" "[^\n\"\\\\]"
"\\|" scala-syntax:string-escape-re "\\)"))
(defconst scala-syntax:oneLineStringLiteral-re (concat "\\(\"\\)" scala-syntax:stringElement-re "*\\(\"\\)"))
(defconst scala-syntax:multiLineStringLiteral-start-re
"\\(\"\\)\"\"\\(\"?\"?[^\"]\\)*")
(defconst scala-syntax:multiLineStringLiteral-end-re
"\"\"+\\(\"\\)")
(defconst scala-syntax:multiLineStringLiteral-re
(concat scala-syntax:multiLineStringLiteral-start-re
scala-syntax:multiLineStringLiteral-end-re))
(defconst scala-syntax:stringLiteral-re
(concat "\\(" scala-syntax:multiLineStringLiteral-re
"\\|" scala-syntax:oneLineStringLiteral-re "\\)" ))
;; If you change this or any of the used regex, be sure to
;; maintain this or update propertize function accordingly:
;; group 1 = char start, 3 = char end
;; group 4 = multi-line string start, 6 = end
;; group 7 = string start, 9 = end
(defconst scala-syntax:relaxed-char-and-string-literal-re
(concat scala-syntax:characterLiteral-re
"\\|" scala-syntax:multiLineStringLiteral-start-re
"\\(?:" scala-syntax:multiLineStringLiteral-end-re "\\)?"
"\\|\\(\"\\)" "\\(\\\\.\\|[^\"\n\\]\\)*" "\\(\"\\)"))
;; Identifiers (Chapter 1.1)
(defconst scala-syntax:op-re
(concat "[" scala-syntax:opchar-group "]+" ))
(defconst scala-syntax:idrest-re
;; Eagerness of regexp causes problems with _. The following is a workaround,
;; but the resulting regexp matches only what SLS demands.
(concat "\\(" "[_]??" "[" scala-syntax:letter-group scala-syntax:digit-group "]+" "\\)*"
"\\(" "_+" scala-syntax:op-re "\\|" "_" "\\)?"))
(defconst scala-syntax:varid-re (concat "[" scala-syntax:lower-group "]" scala-syntax:idrest-re))
(defconst scala-syntax:capitalid-re (concat "[" scala-syntax:upperAndUnderscore-group "]" scala-syntax:idrest-re))
;; alphaid introduce by SIP11
(defconst scala-syntax:alphaid-re (concat "\\(" "[" scala-syntax:lower-group scala-syntax:upperAndUnderscore-group "]" scala-syntax:idrest-re "\\)"))
(defconst scala-syntax:plainid-re (concat "\\(" scala-syntax:alphaid-re "\\|" scala-syntax:op-re "\\)"))
;; stringlit is referred to, but not defined Scala Language Specification 2.9
;; we define it as consisting of anything but '`' and newline
(defconst scala-syntax:stringlit-re "[^`\n\r]")
(defconst scala-syntax:quotedid-re (concat "`" scala-syntax:stringlit-re "+`"))
(defconst scala-syntax:id-re (concat "\\(" scala-syntax:plainid-re
"\\|" scala-syntax:quotedid-re "\\)"))
(defconst scala-syntax:id-first-char-group
(concat scala-syntax:lower-group
scala-syntax:upperAndUnderscore-group
scala-syntax:opchar-group))
;; Symbol literals (Chapter 1.3.7)
(defconst scala-syntax:symbolLiteral-re
;; must end with non-' to not conflict with scala-syntax:characterLiteral-re
(concat "\\('" scala-syntax:plainid-re "\\)\\([^']\\|$\\)"))
;; Literals (Chapter 1.3)
(defconst scala-syntax:literal-re
(concat "\\(" scala-syntax:integerLiteral-re
"\\|" scala-syntax:floatingPointLiteral-re
"\\|" scala-syntax:booleanLiteral-re
"\\|" scala-syntax:characterLiteral-re
"\\|" scala-syntax:stringLiteral-re
"\\|" scala-syntax:symbolLiteral-re
"\\|" "null" "\\)"))
(defconst scala-syntax:interpolation-re
(concat "\\(" "\\$" scala-syntax:id-re "\\|" "\\${[^}\n\\\\]*}" "\\)"))
(defun scala-syntax:interpolation-matcher (end)
(let* ((pos nil)
(syntax nil)
(str-start nil)
(char-before-str nil))
(while (and
(setq pos (re-search-forward scala-syntax:interpolation-re end t))
(setq syntax (syntax-ppss pos))
(if (nth 3 syntax) ;; "is string"
(progn
(setq str-start (nth 8 syntax))
;; s"foo"
;; ^-- `char-before-str', must be identifier
(setq char-before-str (char-after (1- str-start)))
;; break if match
(null (string-match-p
scala-syntax:id-re (string char-before-str))))
t))) ;; keep going
pos))
;; Paths (Chapter 3.1)
;; emacs has a problem with these regex, don't use them
;; (defconst scala-syntax:classQualifier-re (concat "[[]" scala-syntax:id-re "[]]"))
;; (defconst scala-syntax:stableId-re
;; (concat "\\(\\(" "this"
;; "\\|" "super" scala-syntax:classQualifier-re
;; "\\|" scala-syntax:id-re
;; "\\)\\.\\)*"
;; scala-syntax:id-re))
;; (defconst scala-syntax:path-re
;; (concat "\\(" scala-syntax:stableId-re
;; "\\|" "\\(" scala-syntax:id-re "\\." "\\)?" "this" "\\)"))
(defun scala-syntax:looking-at-super ()
(save-excursion
(when (looking-at "\\<super\\>")
(let ((beg (match-beginning 0)))
(when (and (goto-char (match-end 0))
(or (when (= (char-after) ?.)
(forward-char)
t)
(and (when (and (not (eobp)) (= (char-after) ?\[))
(forward-char)
t)
(progn (scala-syntax:skip-forward-ignorable)
(looking-at scala-syntax:id-re))
(progn (goto-char (match-end 0))
(scala-syntax:skip-forward-ignorable)
(when (and (not (eobp)) (= (char-after) ?\]))
(forward-char)
t))
(when (and (not (eobp)) (= (char-after) ?.))
(forward-char)
t)))
(looking-at scala-syntax:id-re))
(set-match-data `(,beg ,(match-end 0)))
t)))))
(defun scala-syntax:looking-at-stableIdOrPath (&optional path-p beg)
(unless beg (setq beg (point)))
(save-excursion
(cond ((looking-at "\\<this\\>")
(goto-char (match-end 0))
(if (and (not (eobp)) (= (char-after) ?.))
(progn (forward-char)
(scala-syntax:looking-at-stableIdOrPath path-p beg))
path-p))
((or (scala-syntax:looking-at-super)
(and (not (or (looking-at scala-syntax:keywords-unsafe-re)
(scala-syntax:looking-at-reserved-symbol nil)))
(looking-at scala-syntax:id-re)))
(goto-char (match-end 0))
(if (and (not (eobp)) (= (char-after) ?.))
(progn (forward-char)
(scala-syntax:looking-at-stableIdOrPath path-p beg))
(set-match-data `(,beg ,(match-end 0)))
(point))))))
(defun scala-syntax:looking-at-simplePattern-beginning ()
(or (looking-at "[_(]")
(looking-at scala-syntax:literal-re)
(scala-syntax:looking-at-stableIdOrPath)))
(defun scala-syntax:regexp-for-id (id)
(let ((prefix-regex
(if (string-match scala-syntax:alphaid-re id)
"\\b" (concat "\\(^\\|[^" scala-syntax:opchar-group "]\\)")))
(suffix-regex
(if (string-match scala-syntax:op-re (substring id -1 nil))
(concat "\\([^" scala-syntax:opchar-group "]\\|$\\)") "\\b")))
(concat prefix-regex id suffix-regex)))
;;;
;;; Other regular expressions
;;;
(defconst scala-syntax:preamble-start-re
"\#\!")
(defconst scala-syntax:empty-line-re
"^\\s *$")
(defconst scala-syntax:comment-start-re
"/[/*]")
(defconst scala-syntax:end-of-code-line-re
(concat "\\([ ]\\|$\\|" scala-syntax:comment-start-re "\\)")
"A special regexp that can be concatenated to an other regular
expression when used with scala-syntax:looking-back-token. Not
meaningfull in other contexts.")
(defconst scala-syntax:path-keywords-unsafe-re
(regexp-opt '("super" "this") 'words))
(defconst scala-syntax:path-keywords-re
(concat "\\(^\\|[^`'_]\\)\\(" scala-syntax:path-keywords-unsafe-re "\\)"))
(defconst scala-syntax:value-keywords-unsafe-re
(regexp-opt '("false" "null" "true") 'words))
(defconst scala-syntax:value-keywords-re
(concat "\\(^\\|[^`'_]\\)\\(" scala-syntax:value-keywords-unsafe-re "\\)"))
(defconst scala-syntax:other-keywords-unsafe-re
(regexp-opt '("abstract" "case" "catch" "class" "def" "do" "else" "extends"
"final" "finally" "for" "forSome" "if" "implicit" "import"
"lazy" "match" "new" "object" "override" "package" "private"
"protected" "return" "sealed" "throw" "trait" "try" "type"
"val" "var" "while" "with" "yield" "inline") 'words))
(defconst scala-syntax:other-keywords-re
(concat "\\(^\\|[^`'_]\\)\\(" scala-syntax:other-keywords-unsafe-re "\\)"))
(defconst scala-syntax:keywords-unsafe-re
(concat "\\(" scala-syntax:path-keywords-unsafe-re
"\\|" scala-syntax:value-keywords-unsafe-re
"\\|" scala-syntax:other-keywords-unsafe-re
"\\)"))
;; TODO: remove
;; (defconst scala-syntax:keywords-re
;; (concat "\\(^\\|[^`'_]\\)\\(" scala-syntax:value-keywords-unsafe-re
;; "\\|" scala-syntax:path-keywords-unsafe-re
;; "\\|" scala-syntax:other-keywords-unsafe-re "\\)"))
(defconst scala-syntax:after-reserved-symbol-underscore-re
(concat "$\\|" scala-syntax:comment-start-re
"\\|[^" scala-syntax:letterOrDigit-group "]"))
(defconst scala-syntax:reserved-symbol-underscore-re
;; reserved symbol _
(concat "\\(^\\|[^" scala-syntax:letterOrDigit-group "]\\)"
"\\(_\\)"
"\\(" scala-syntax:after-reserved-symbol-underscore-re "\\)"))
(defconst scala-syntax:reserved-symbols-unsafe-re
;; reserved symbols. The regexp is unsafe as it does not
;; check the context.
"\\([:#@\u21D2\u2190]\\|=>?\\|<[:%!?\\-]\\|>:\\)" )
(defconst scala-syntax:double-arrow-unsafe-re
"\\(=>\\|\u21D2\\)")
(defconst scala-syntax:after-reserved-symbol-re
(concat "\\($\\|" scala-syntax:comment-start-re
"\\|[^" scala-syntax:opchar-group "]\\)"))
(defconst scala-syntax:reserved-symbols-re
;; reserved symbols and XML starts ('<!' and '<?')
(concat "\\(^\\|[^" scala-syntax:opchar-group "]\\)"
scala-syntax:reserved-symbols-unsafe-re
"\\(" scala-syntax:after-reserved-symbol-re "\\)"))
(defconst scala-syntax:colon-re
(concat "\\(^\\|[^" scala-syntax:opchar-group "]\\)"
"\\(:\\)"
"\\(" scala-syntax:after-reserved-symbol-re "\\)"))
(defconst scala-syntax:override-unsafe-re
(regexp-opt '("override") 'words))
(defconst scala-syntax:override-re
(concat "\\(^\\|[^`'_]\\)\\(" scala-syntax:override-unsafe-re "\\)"))
(defconst scala-syntax:abstract-unsafe-re
(regexp-opt '("abstract") 'words))
(defconst scala-syntax:abstract-re
(concat "\\(^\\|[^`'_]\\)\\(" scala-syntax:abstract-unsafe-re "\\)"))
(defconst scala-syntax:final-unsafe-re
(regexp-opt '("final") 'words))
(defconst scala-syntax:final-re
(concat "\\(^\\|[^`'_]\\)\\(" scala-syntax:final-unsafe-re "\\)"))
(defconst scala-syntax:sealed-unsafe-re
(regexp-opt '("sealed") 'words))
(defconst scala-syntax:sealed-re
(concat "\\(^\\|[^`'_]\\)\\(" scala-syntax:sealed-unsafe-re "\\)"))
(defconst scala-syntax:implicit-unsafe-re
(regexp-opt '("implicit") 'words))
(defconst scala-syntax:implicit-re
(concat "\\(^\\|[^`'_]\\)\\(" scala-syntax:implicit-unsafe-re "\\)"))
(defconst scala-syntax:lazy-unsafe-re
(regexp-opt '("lazy") 'words))
(defconst scala-syntax:lazy-re
(concat "\\(^\\|[^`'_]\\)\\(" scala-syntax:lazy-unsafe-re "\\)"))
(defconst scala-syntax:private-unsafe-re
(regexp-opt '("private") 'words))
(defconst scala-syntax:private-re
(concat "\\(^\\|[^`'_]\\)\\(" scala-syntax:private-unsafe-re "\\)"))
(defconst scala-syntax:protected-unsafe-re
(regexp-opt '("protected") 'words))
(defconst scala-syntax:protected-re
(concat "\\(^\\|[^`'_]\\)\\(" scala-syntax:protected-unsafe-re "\\)"))
(defconst scala-syntax:modifiers-unsafe-re
(regexp-opt '("override" "abstract" "final" "sealed" "implicit" "lazy"
"private" "protected") 'words))
(defconst scala-syntax:modifiers-re
(concat "\\(^\\|[^`'_]\\)\\(" scala-syntax:modifiers-unsafe-re "\\)"))
(defconst scala-syntax:body-start-re
(concat "=" scala-syntax:end-of-code-line-re)
"A regexp for detecting if a line ends with '='")
(defconst scala-syntax:list-keywords-re
(regexp-opt '("var" "val" "import") 'words)
("Keywords that can start a list"))
(defconst scala-syntax:case-re
"\\<case\\>")
(defconst scala-syntax:for-re
"\\<for\\>")
(defconst scala-syntax:class-or-object-re
(regexp-opt '("class" "object") 'words))
;;;;
;;;; Character syntax table and related syntax-propertize functions
;;;;
;;; The syntax table relies havily on the syntax-propertize-functions being
;;; run. Hence this syntax requires at least emacs 24, which introduced
;;; this new facility.
(defvar scala-syntax:syntax-table nil
"Syntax table used in `scala-mode' buffers.")
(when (not scala-syntax:syntax-table)
(let ((syntab (make-syntax-table)))
;; 1. start by reseting the syntax table: only (){}[] are
;; parentheses, so all others marked as parentheses in the parent
;; table must be marked as symbols, nothing is a punctuation
;; unless otherwise stated
(map-char-table
#'(lambda (key value)
(when (or (= (syntax-class value) 4) ; open
(= (syntax-class value) 5) ; close
(= (syntax-class value) 1)) ; punctuation
(modify-syntax-entry key "_" syntab)))
(char-table-parent syntab))
;; Below 'space', everything is either illegal or whitespace.
;; Consider as whitespace, unless otherwise stated below.
(modify-syntax-entry '(0 . 32) " " syntab)
;; The scala parentheses
(modify-syntax-entry ?\( "()" syntab)
(modify-syntax-entry ?\[ "(]" syntab)
(modify-syntax-entry ?\{ "(}" syntab)
(modify-syntax-entry ?\) ")(" syntab)
(modify-syntax-entry ?\] ")[" syntab)
(modify-syntax-entry ?\} "){" syntab)
;; _ is upper-case letter, but will be modified to be symbol
;; constituent when in reserved symbol position by
;; syntax-propertize-function
(modify-syntax-entry ?\_ "w" syntab)
;; by default all opchars are punctuation, but they will be
;; modified by syntax-propertize-function to be symbol
;; constituents when a part of varid or capitalid
(dolist (char (mapcar 'identity "!#%&*+/:<=>?@^|~-\u21D2\u2190")) ;; TODO: Sm, So
(modify-syntax-entry char "." syntab))
;; for clarity, the \ is alone here and not in the string above
(modify-syntax-entry ?\\ "." syntab)
;; scala strings cannot span lines, so we mark
;; " as punctuation, but do the real stuff
;; in syntax-propertize-function for properly
;; formatted strings.
(modify-syntax-entry ?\" "." syntab)
;; backquote is given paired delimiter syntax so that
;; quoted ids are parsed as one sexp. Fontification
;; is done separately.
(modify-syntax-entry ?\` "$" syntab)
;; ' is considered an expression prefix, since it can
;; both start a Symbol and is a char quote. It
;; will be given string syntax by syntax-propertize-function
;; for properly formatted char literals.
(modify-syntax-entry ?\' "'" syntab)
;; punctuation as specified by SLS
(modify-syntax-entry ?\. "." syntab)
(modify-syntax-entry ?\; "." syntab)
(modify-syntax-entry ?\, "." syntab)
;; comments
;; the `n' means that comments can be nested
(modify-syntax-entry ?\/ ". 124b" syntab)
(modify-syntax-entry ?\* ". 23n" syntab)
(modify-syntax-entry ?\n "> b" syntab)
(modify-syntax-entry ?\r "> b" syntab)
(setq scala-syntax:syntax-table syntab)))
(defun scala-syntax:propertize-extend-region (start end)
"See syntax-propertize-extend-region-functions"
;; nothing yet
nil)
(defmacro scala-syntax:put-syntax-table-property (match-group value)
"Add 'syntax-table entry 'value' to the region marked by the
match-group 'match-group'"
`(put-text-property (match-beginning ,match-group)
(match-end ,match-group)
'syntax-table
,value))
(defun scala-syntax:propertize-char-and-string-literals (start end)
"Mark start and end of character literals as well as one-line
and multi-line string literals. One-line strings and characters
use syntax class 7 (string quotes), while multi-line strings are
marked with 15 (generic string delimiter). Multi-line string
literals are marked even if they are unbalanced. One-line string
literals have to be balanced to get marked. This means invalid
characters and one-line strings will not be fontified."
(let* ((string-state (nth 3 (syntax-ppss start)))
(unbalanced-p (eq string-state t)))
(if (and string-state (not unbalanced-p))
;; a normal string is open, let's de-propertize
(remove-text-properties start end '(syntax-table nil))
(save-excursion
(goto-char start)
;; close the closing for the unbalanced multi-line literal
(when (and unbalanced-p
(re-search-forward scala-syntax:multiLineStringLiteral-end-re end t))
(scala-syntax:put-syntax-table-property 1 '(15 . nil)))
;; match any balanced one-line or multi-line literals
(catch 'break
(while (re-search-forward
scala-syntax:relaxed-char-and-string-literal-re end t)
;; Expects the following groups:
;; group 1 = char start, 3 = char end
;; group 4 = multi-line string start, 6 = end
;; group 7 = string start, 9 = end
(cond
((match-beginning 1)
(scala-syntax:put-syntax-table-property 1 '(7 . nil))
(scala-syntax:put-syntax-table-property 3 '(7 . nil)))
((match-beginning 4) ;; start of multi-line literal
(scala-syntax:put-syntax-table-property 4 '(15 . nil))
(if (match-beginning 6)
;; balanced multi-line
(scala-syntax:put-syntax-table-property 6 '(15 . nil))
;; un-balanced multi-line
(throw 'break nil)))
((or
;; normal string, content is not empty
(match-beginning 8)
;; empty string at line end
(= (match-end 9) (line-end-position))
;; no " after empty string
(not (= (char-after (match-end 10)) ?\")))
(when (save-excursion
(goto-char (match-beginning 7))
;; really valid?
(looking-at-p scala-syntax:oneLineStringLiteral-re))
(scala-syntax:put-syntax-table-property 7 '(7 . nil))
(scala-syntax:put-syntax-table-property 9 '(7 . nil))))
(t (throw 'break nil)))))))))
(defun scala-syntax:propertize-shell-preamble (start end)
"Mark a shell preamble (#!) at the beginning of a script as a line comment."
(save-excursion
(goto-char start)
(when (and (= start 1)
(looking-at scala-syntax:preamble-start-re))
(scala-syntax:put-syntax-table-property 0 '(11 . nil))
(end-of-line)
(when (re-search-forward "\n" end t)
(scala-syntax:put-syntax-table-property 0 '(12 . nil))))))
(defun scala-syntax:propertize-underscore-and-idrest (start end)
"Mark all underscores (_) as symbol constituents (syntax 3) or
upper case letter (syntax 2). Also mark opchars in idrest as
symbol constituents (syntax 3)."
(save-excursion
(goto-char start)
(while (re-search-forward "_" end t)
(let ((match-beg (match-beginning 0))
(match-end (match-end 0)))
(put-text-property
match-beg match-end 'syntax-table
(if (= match-beg (line-beginning-position))
(if (looking-at scala-syntax:after-reserved-symbol-underscore-re)
'(3 . nil) ; symbol constituent
'(2 . nil)) ; word syntax
(save-excursion
(goto-char (1- match-beg))
(if (looking-at scala-syntax:reserved-symbol-underscore-re)
'(3 . nil) ; symbol constituent
;; check for opchars that should be marked as symbol constituents (3)
(goto-char match-end)
(when (looking-at scala-syntax:op-re)
(scala-syntax:put-syntax-table-property 0 '(3 . nil)))
'(3 . nil))))))))) ;; symbol constituent syntax (3) also for the '_'
(defun scala-syntax:propertize-special-symbols (start end)
(save-excursion
(goto-char start)
(while (re-search-forward (concat "[" scala-syntax:opchar-group "]" scala-syntax:op-re) end t)
(let ((match-beg (match-beginning 0))
(match-end (match-end 0))
(match (match-string 0)))
(unless (or
(string-suffix-p "*/" match)
(member match '("</"))
(member 0 (mapcar (lambda (regexp) (string-match regexp match)) '("^*+/$" "^//.*$" "^/\\*+$")))
(equal 2 (syntax-class (syntax-after match-end)))
(equal 2 (syntax-class (syntax-after (1- match-beg)))))
(put-text-property match-beg match-end 'syntax-table '(3 . nil)))))))
(defun scala-syntax:propertize-quotedid (start end)
"Mark all `scala-syntax:quotedid-re' as symbol constituents (syntax 3)"
(save-excursion
(goto-char start)
(while (re-search-forward scala-syntax:quotedid-re end t)
(scala-syntax:put-syntax-table-property 0 '(3 . nil)))))
(defun scala-syntax:propertize-dollar (start end)
"Mark all $ occurences as punctuation (syntax 1)"
(save-excursion
(goto-char start)
(while (re-search-forward "\\$" end t)
(scala-syntax:put-syntax-table-property 0 '(1 . nil)))))
(defun scala-syntax:propertize (start end)
"See syntax-propertize-function"
(scala-syntax:propertize-char-and-string-literals start end)
(scala-syntax:propertize-shell-preamble start end)
(scala-syntax:propertize-underscore-and-idrest start end)
(scala-syntax:propertize-special-symbols start end)
(scala-syntax:propertize-quotedid start end)
(scala-syntax:propertize-dollar start end))
;;;;
;;;; Syntax navigation functions
;;;;
(defun scala-syntax:beginning-of-code-line ()
(interactive)
"Move to the beginning of code on the line, or to the end of
the line, if the line is empty. Return the new point. Not to be
called on a line whose start is inside a comment, i.e. a comment
begins on the previous line and continues past the start of this
line."
;; TODO: make it work even if the start IS inside a comment
(beginning-of-line)
(let ((eol (line-end-position))
(pos (point)))
(while (and (forward-comment 1)
(< (point) eol))
(setq pos (point)))
;; Now we are either on a different line or at eol.
;; Pos is the last point one the starting line.
(if (> (point) eol)
(goto-char pos)
(skip-syntax-forward " " eol)
(point))))
(defun scala-syntax:looking-at-varid-p (&optional point)
"Return true if looking-at varid, and it is not the start of a
stableId"
(save-excursion
(when point (goto-char point))
(scala-syntax:skip-forward-ignorable)
(let ((case-fold-search nil))
(when (looking-at scala-syntax:varid-re)
(save-match-data
(if (or (= (char-after (match-end 0)) ?.)
(looking-at "\\<\\(this\\|super\\)\\>"))
nil
t))))))
(defun scala-syntax:looking-at-empty-line-p ()
(save-excursion
(or (bolp)
(skip-syntax-forward " >" (1+ (line-end-position))))
(looking-at scala-syntax:empty-line-re)))
(defun scala-syntax:looking-at-reserved-symbol (re &optional point)
(interactive)
(unless re (setq re scala-syntax:reserved-symbols-unsafe-re))
(save-excursion
(when point (goto-char point))
(scala-syntax:skip-forward-ignorable)
(and (looking-at re)
(goto-char (match-end 0))
(looking-at-p scala-syntax:after-reserved-symbol-re))))
(defun scala-syntax:looking-at-case-p (&optional point)
(save-excursion
(when point (goto-char point))
(scala-syntax:skip-forward-ignorable)
(and (looking-at scala-syntax:case-re)
(goto-char (match-end 0))
(scala-syntax:skip-forward-ignorable)
(not (looking-at-p scala-syntax:class-or-object-re)))))
(defun scala-syntax:looking-back-empty-line-p ()
"Return t if the previous line is empty"
(save-excursion
(skip-syntax-backward " " (line-beginning-position))
(and (bolp)
(forward-line -1)
(looking-at-p scala-syntax:empty-line-re))))
(defun scala-syntax:skip-forward-ignorable ()
"Moves forward over ignorable whitespace and comments. A
completely empty line is not ignorable and will not be mobed over."
(interactive)
(save-match-data
(while (and (not (scala-syntax:looking-at-empty-line-p))
(forward-comment 1)))
(skip-syntax-forward " " (line-end-position))))
(defun scala-syntax:skip-backward-ignorable ()
"Move backwards over ignorable whitespace and comments. A
completely empty line is not ignorable and will not be moved
over. Returns the number of points moved (will be negative)."
(save-match-data
(while (and (not (scala-syntax:looking-back-empty-line-p))
(forward-comment -1)))
(skip-syntax-backward " " (line-beginning-position))))
(defun scala-syntax:looking-at (re)
"Return the end position of the matched re, if the current
position is followed by it, or nil if not. All ignorable comments
and whitespace are skipped before matching."
(save-excursion
(scala-syntax:skip-forward-ignorable)
(looking-at re)))
(defun scala-syntax:looking-back-token (re &optional max-chars)
"Return the start position of the token matched by re, if the
current position is preceeded by it, or nil if not. All ignorable
comments and whitespace are ignored, i.e. does not search past an
empty line. Expects to be outside of comment. A limit for the
search is calculated based on max-chars. The function won't look
further than max-chars starting after skipping any ignorable."
(save-excursion
;; skip back all comments
(scala-syntax:skip-backward-ignorable)
(let ((end (point))
(limit (when max-chars (- (point) max-chars))))
;; skip back punctuation or ids (words and related symbols and delimiters)
(if (or (/= 0 (skip-chars-backward scala-syntax:delimiter-group limit))
(/= 0 (skip-syntax-backward "." limit))
(/= 0 (skip-syntax-backward "(" limit))
(/= 0 (skip-syntax-backward ")" limit))
(/= 0 (skip-syntax-backward "w_'$" limit)))
(if (looking-at re) (point) nil)
nil))))
(defun scala-syntax:backward-parameter-groups ()
"Move back over all parameter groups to the start of the first
one."
(save-match-data
(while (scala-syntax:looking-back-token "[])]" 1)
(backward-list))))
(defun scala-syntax:forward-parameter-groups ()
"Move back over all parameter groups to the end of the last
one."
(save-match-data
(while (scala-syntax:looking-at "[[(]")
(forward-list))))
(defun scala-syntax:forward-modifiers ()
"Move forward over any modifiers."
(save-match-data
(while (scala-syntax:looking-at scala-syntax:modifiers-re)
(scala-syntax:forward-sexp)
(when (scala-syntax:looking-at "[[]")
(forward-list)))))
(defun scala-syntax:looking-back-else-if-p ()
;; TODO: rewrite using (scala-syntax:if-skipped (scala:syntax:skip-backward-else-if))
(save-excursion
(if (and (scala-syntax:looking-back-token "\\s)" 1)
(backward-list)
(prog1 (scala-syntax:looking-back-token "if")
(goto-char (match-beginning 0)))
(prog1 (scala-syntax:looking-back-token "else")
(goto-char (match-beginning 0))))
(point) nil)))
(defun scala-syntax:newlines-disabled-p (&optional point)
"Return true if newlines are disabled at the current point (or
point 'point') as specified by SLS chapter 1.2"
;; newlines are disabled if
;; - in '()' or '[]'
;; - between 'case' and '=>'
;; - XML mode (not implemented here)
(unless point (setq point (point)))
(save-excursion
(let* ((state (syntax-ppss point))
(parenthesisPos (nth 1 state)))
(when parenthesisPos ;; if no parenthesis, then this cannot be a case block either
(goto-char parenthesisPos)
(or
;; the trivial cases of being inside ( or [
(= (char-after) ?\()
(= (char-after) ?\[)
;; else we have to see about case
(progn
(forward-char)
(forward-comment (buffer-size))
(skip-syntax-forward " >")
(when (looking-at scala-syntax:case-re)
(let ((limit (match-beginning 0)))
(goto-char (or (nth 8 state) point))
;; go to the start of => or 'case'
(while (> (point) limit)
(scala-syntax:backward-sexp)
(when (or (looking-at scala-syntax:case-re)
(scala-syntax:looking-at-reserved-symbol
scala-syntax:double-arrow-unsafe-re))
(setq limit (point))))
;; unless we found '=>', check if we found 'case' (but
;; 'case class' or 'case object')
(unless (scala-syntax:looking-at-reserved-symbol
scala-syntax:double-arrow-unsafe-re)
(scala-syntax:forward-sexp)
(and (<= (point) point) ;; check that we were inside in the first place
(progn (scala-syntax:skip-forward-ignorable)
(not (looking-at scala-syntax:class-or-object-re)))))))))))))
(defun scala-syntax:forward-sexp ()
"Move forward one scala expression. It can be: parameter list (value or type),
id, reserved symbol, keyword, block, or literal. Punctuation (.,;)
and comments are skipped silently. Position is placed at the
end of the skipped expression."
(interactive)
(syntax-propertize (point-max))
;; emacs knows how to properly skip: lists, varid, capitalid,
;; strings, symbols, chars, quotedid. What we have to handle here is
;; most of all ids made of op chars
;; skip comments, whitespace and scala delimiter chars .,; so we
;; will be at the start of something interesting
(forward-comment (buffer-size))
(while (< 0 (+ (skip-syntax-forward " ")
(skip-chars-forward scala-syntax:delimiter-group))))
;; emacs can handle everything but opchars
(when (= (skip-syntax-forward ".") 0)
(goto-char (or (scan-sexps (point) 1) (buffer-end 1)))))
(defun scala-syntax:forward-token ()
"Move forward one scala token, comment word or string word. It
can be: start or end of list (value or type), id, reserved
symbol, keyword, block, or literal. Punctuation (.,;), comment
delimiters and string delimiters are skipped silently. Position
is placed at the end of the skipped token."
(interactive)
(syntax-propertize (point-max))
(skip-syntax-forward " >" (point-max))
(when (looking-at
(concat "\\([#@:]\\|" scala-syntax:double-arrow-unsafe-re
"\\|:>\\|<:\\)" scala-syntax:after-reserved-symbol-re))
(goto-char (match-end 1)))
(let ((syntax (char-syntax (char-after)))
(state (syntax-ppss)))
(cond
((or (nth 4 state) (nth 3 state))
;; inside a string or comment, skip words as normal unless that
;; would end up outside the string. Then leave point at end of
;; string delimiter.
(let ((start (nth 8 state))
(end (save-excursion (forward-word) (point))))
(if (eq (nth 8 (save-excursion (syntax-ppss end))) start)
(goto-char end)
(while (eq (nth 8 (syntax-ppss)) start)
(forward-char)))))
;; list start or end
((or (= syntax ?\)) (= syntax ?\()) (forward-char))
;; comment or string start is skipped
((looking-at "\\(//\\|/\\*+\\|\"\\(\"\"\\)?\\)")
(goto-char (match-end 1)))
;; otherwise forward-sexp
(t (forward-sexp)))))
(defun scala-syntax:backward-sexp ()
"Move backward one scala expression. It can be: parameter
list (value or type), id, reserved symbol, keyword, block, or
literal. Delimiters (.,;) and comments are skipped
silently. Position is placed at the beginning of the skipped
expression."
(interactive)
(syntax-propertize (point))
;; for implementation comments, see scala-syntax:forward-sexp
(forward-comment (- (buffer-size)))
(while (> 0 (+ (skip-syntax-backward " ")
(skip-chars-backward scala-syntax:delimiter-group))))
(when (= (skip-syntax-backward ".") 0)
(goto-char (or (scan-sexps (point) -1) (buffer-end -1)))
(backward-prefix-chars)))
(defun scala-syntax:has-char-before (char end)
(save-excursion
(while (and (< (point) end)
(or (bobp)
(/= (char-before) char)))
(scala-syntax:forward-sexp))
(when (= (char-before) char)
(scala-syntax:skip-forward-ignorable)
(> end (point)))))
(defun scala-syntax:search-backward-sexp (re)
"Searches backward sexps until it reaches re, empty line or ;.
If re is found, point is set to beginning of re and the position
is returned, otherwise nil is returned"
(let ((found (save-excursion
(while (not (or (bobp)
(scala-syntax:looking-back-empty-line-p)
(scala-syntax:looking-back-token "[;,]")
(looking-at re)))
(scala-syntax:backward-sexp))
(if (looking-at re)
(point)
nil))))
(when found (goto-char found))))
(defun scala-syntax:list-p (&optional point)
"Returns the start of the list, if the current point (or point
'point') is on the first line of a list element > 1, or nil if
not. A list must be either enclosed in parentheses or start with
'val', 'var' or 'import'."
(save-excursion
;; first check that the previous line ended with ','
(when point (goto-char point))
(scala-syntax:beginning-of-code-line)
(when (scala-syntax:looking-back-token "," 1)
(goto-char (match-beginning 0))
(let ((parenpoint (nth 1 (syntax-ppss))))
(if (and parenpoint (or (= (char-after parenpoint) ?\()
(= (char-after parenpoint) ?\[)))
(1+ parenpoint)
(ignore-errors ; catches when we get at parentheses
(while (not (or (bobp)
(looking-at scala-syntax:list-keywords-re)
(scala-syntax:looking-back-empty-line-p)
(scala-syntax:looking-back-token ";")))
(scala-syntax:backward-sexp)))
(when (looking-at scala-syntax:list-keywords-re)
(goto-char (match-end 0))))))))
;; Functions to help with finding the beginning and end of scala definitions.
(defconst scala-syntax:modifiers-re
(regexp-opt '("override" "abstract" "final" "sealed" "implicit" "lazy"
"private" "protected" "case") 'words))
(defconst scala-syntax:whitespace-delimeted-modifiers-re
(concat "\\(?:" scala-syntax:modifiers-re "\\(?: *\\)" "\\)*"))
(defconst scala-syntax:definition-words-re
(mapconcat 'regexp-quote '("class" "object" "trait" "val" "var" "def" "type") "\\|"))
(defun scala-syntax:build-definition-re (words-re)
(concat " *"
scala-syntax:whitespace-delimeted-modifiers-re
words-re
"\\(?: *\\)"
"\\(?2:"
scala-syntax:id-re
"\\)"))
(defconst scala-syntax:all-definition-re
(scala-syntax:build-definition-re
(concat "\\(?1:" scala-syntax:definition-words-re "\\)\\b")))
;; Functions to help with beginning and end of definitions.
(defun scala-syntax:backward-sexp-forcing ()
(condition-case ex (backward-sexp) ('error (backward-char))))
(defun scala-syntax:forward-sexp-or-next-line ()
(interactive)
(cond ((looking-at "\n") (next-line) (beginning-of-line))
(t (forward-sexp))))
(defun scala-syntax:beginning-of-definition ()
"This function may not work properly with certain types of scala definitions.
For example, no care has been taken to support multiple assignments to vals such as
val a, b = (1, 2)
"
(interactive)
(let ((found-position
(save-excursion
(scala-syntax:backward-sexp-forcing)
(scala-syntax:movement-function-until-re scala-syntax:all-definition-re
'scala-syntax:backward-sexp-forcing))))