-
Notifications
You must be signed in to change notification settings - Fork 23
/
d-mode.el
1985 lines (1739 loc) · 63.1 KB
/
d-mode.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
;;; d-mode.el --- D Programming Language major mode for (X)Emacs
;;; Requires a cc-mode of version 5.30 or greater
;; Author: William Baxter
;; Contributor: Andrei Alexandrescu
;; Contributor: Russel Winder
;; Maintainer: Russel Winder <[email protected]>
;; Vladimir Panteleev <[email protected]>
;; Created: March 2007
;; Version: 202408131340
;; Keywords: D programming language emacs cc-mode
;; Package-Requires: ((emacs "25.1"))
;;;; NB Version number is date and time yyyymmddhhMM UTC.
;;;; A hook to update it automatically on save is available here:
;;;; https://gist.github.com/CyberShadow/28f60687c3bf83d32900cd6074c012cb
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program 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 General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Usage:
;; Put these lines in your init file.
;; (autoload 'd-mode "d-mode" "Major mode for editing D code." t)
;; (add-to-list 'auto-mode-alist '("\\.d[i]?\\'" . d-mode))
;;
;; Alternatively you can load d-mode.el explicitly:
;; (load "d-mode.el")
;;
;; cc-mode version 5.30 or greater is required.
;; You can check your cc-mode with the command M-x c-version.
;; You can get the latest version of cc-mode at http://cc-mode.sourceforge.net
;;; Commentary:
;; This mode supports most of D's syntax, including nested /+ +/
;; comments and backquote `string literals`.
;;
;; This mode has been dubbed "2.0" because it is a complete rewrite
;; from scratch. The previous d-mode was based on cc-mode 5.28 or
;; so. This version is based on the cc-mode 5.30 derived mode
;; example by Martin Stjernholm, 2002.
;;; Bugs:
;; Bug tracking is currently handled using the GitHub issue tracker at
;; https://github.com/Emacs-D-Mode-Maintainers/Emacs-D-Mode/issues
;;; Versions:
;; This mode is available on MELPA which tracks the mainline Git repository on GitHub, so there is a
;; rolling release system based on commits to the mainline. For those wanting releases, the repository is
;; tagged from time to time and this creates an entry in MELPA Stable and a tarball on GitHub.
;;; Notes:
;;; TODO:
;; Issues with this code are managed via the project issue management
;; on GitHub: https://github.com/Emacs-D-Mode-Maintainers/Emacs-D-Mode/issues?state=open
;;; History:
;; History is tracked in the Git repository rather than in this file.
;; See https://github.com/Emacs-D-Mode-Maintainers/Emacs-D-Mode/commits/master
;;; Code:
;; ----------------------------------------------------------------------------
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Required packages ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ----------------------------------------------------------------------------
(require 'cc-mode)
(require 'cc-langs)
;; Needed to prevent
;; "Symbol's value as variable is void: compilation-error-regexp-alist-alist" errors
(require 'compile)
;; Work around Emacs (cc-mode) bug #18845
(eval-when-compile
(when (and (= emacs-major-version 24) (>= emacs-minor-version 4))
(require 'cl)))
;; Used to specify regular expressions in a sane way.
(require 'rx)
;; These are only required at compile time to get the sources for the
;; language constants. (The cc-fonts require and the font-lock
;; related constants could additionally be put inside an
;; (eval-after-load "font-lock" ...) but then some trickery is
;; necessary to get them compiled.)
;; Comment out 'when-compile part for debugging
(eval-when-compile
(require 'cc-fonts))
;; ----------------------------------------------------------------------------
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;; cc-mode configuration ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ----------------------------------------------------------------------------
(eval-and-compile
;; Make our mode known to the language constant system. Use Java
;; mode as the fallback for the constants we don't change here.
;; This needs to be done also at compile time since the language
;; constants are evaluated then.
(c-add-language 'd-mode 'java-mode))
;; muffle the warnings about using free variables and undefined
;; functions
(defvar c-syntactic-element)
(declare-function c-populate-syntax-table "cc-langs.el" (table))
;; D has pointers
(c-lang-defconst c-type-decl-prefix-key
d (concat "\\("
"[*(~]"
"\\|"
(c-lang-const c-type-decl-prefix-key)
"\\)"
"\\([^=]\\|$\\)"))
(c-lang-defconst c-decl-start-re
d "[[:alpha:]_@~.]")
;; d "[[:alpha:]_@]")
(c-lang-defconst d-decl-end-re
d "[,;=(]")
;; D has fixed arrays
(c-lang-defconst c-opt-type-suffix-key
d "\\(\\[[^]]*\\]\\|\\.\\.\\.\\|\\*\\)")
(c-lang-defconst c-decl-prefix-re
d "\\([{}();:,=]+\\)")
(c-lang-defconst c-identifier-ops
;; For recognizing "~this", ".foo", and "foo.bar.baz" as identifiers
d '((left-assoc ".")))
(c-lang-defconst c-after-id-concat-ops
;; Also for handling ~this
d '("~"))
(c-lang-defconst c-string-escaped-newlines
;; Set to true to indicate the D handles backslash escaped newlines in strings
d t)
(c-lang-defconst c-multiline-string-start-char
;; Set to true to indicate that D doesn't mind raw embedded newlines in strings
d t)
;; Configure cc-mode multiline support (this overrides
;; c-multiline-string-start-char)
(c-lang-defconst c-ml-string-opener-re
;; " opens multiline strings in D
d "\\(\\(\"\\)\\)")
(c-lang-defconst c-ml-string-max-opener-len
d 1)
(c-lang-defconst c-ml-string-any-closer-re
;; Unescaped " closes the string
d "\\(?:\\=\\|[^\\]\\)\\(\\(\"\\)\\)")
(c-lang-defconst c-ml-string-max-closer-len
d 1)
(c-lang-defconst c-string-innards-re-alist
;; Note: the " rule doesn't cover r" literals.
d (list (cons ?\" "\\(\\\\\\([^z-a]\\)\\|[^\"\\]\\)*")
(cons ?` "\\([^`]\\)*")))
(c-lang-defconst c-opt-cpp-prefix
;; Preprocessor directive recognizer. D doesn't have cpp, but it has #line
d "\\s *#\\s *")
(c-lang-defconst c-cpp-message-directives d nil)
(c-lang-defconst c-cpp-include-directives d nil)
(c-lang-defconst c-opt-cpp-macro-define d nil)
(c-lang-defconst c-cpp-expr-directives d nil)
(c-lang-defconst c-cpp-expr-functions d nil)
(c-lang-defconst c-assignment-operators
;; List of all assignment operators.
d '("=" "*=" "/=" "%=" "+=" "-=" ">>=" "<<=" ">>>=" "&=" "^=" "^^="
"|=" "~="))
(c-lang-defconst c-other-op-syntax-tokens
"List of the tokens made up of characters in the punctuation or
parenthesis syntax classes that have uses other than as expression
operators."
d (append '("/+" "+/" "..." ".." "!" "*" "&")
(c-lang-const c-other-op-syntax-tokens)))
(c-lang-defconst c-block-comment-starter d "/*")
(c-lang-defconst c-block-comment-ender d "*/")
(c-lang-defconst c-comment-start-regexp d "/[*+/]")
(c-lang-defconst c-block-comment-start-regexp d "/[*+]")
(c-lang-defconst c-literal-start-regexp
;; Regexp to match the start of comments and string literals.
d "/[*+/]\\|\"\\|`")
(c-lang-defconst c-block-prefix-disallowed-chars
;; Allow ':' for inherit list starters.
d (c--set-difference (c-lang-const c-block-prefix-disallowed-chars)
'(?:)))
(c-lang-defconst c-post-protection-token
d ":")
;;----------------------------------------------------------------------------
;; Built-in basic types
(c-lang-defconst c-primitive-type-kwds
d '("bool" "byte" "ubyte" "char" "delegate" "double" "float"
"function" "int" "long" "short" "uint" "ulong" "ushort"
"cent" "ucent" "real" "ireal" "idouble" "ifloat" "creal" "cfloat" "cdouble"
"wchar" "dchar" "void" "string" "wstring" "dstring" "__vector"
"size_t" "ptrdiff_t" "noreturn"))
;; Keywords that can prefix normal declarations of identifiers.
;; Union of StorageClass, Attribute, and ParameterAttributes in D grammar.
;; References:
;; - https://dlang.org/spec/grammar.html#Attribute
;; - https://dlang.org/spec/declaration.html#StorageClass
;; - https://dlang.org/spec/grammar.html#ParameterAttributes
(c-lang-defconst c-modifier-kwds
d '("__gshared" "abstract" "align" "auto" "const" "deprecated"
"enum" "export" "extern" "final" "immutable" "in" "inout" "lazy"
"nothrow" "out" "override" "package" "pragma" "private"
"protected" "public" "pure" "ref" "return" "scope" "shared"
"static" "synchronized"
"@property" "@safe" "@trusted" "@system" "@nogc" "@disable"))
(c-lang-defconst c-class-decl-kwds
;; Keywords introducing declarations where the following block (if any)
;; contains another declaration level that should be considered a class.
d '("class" "struct" "union" "interface" "template"))
;; (c-lang-defconst c-brace-list-decl-kwds
;; d '("enum"))
(c-lang-defconst c-type-modifier-kwds
d nil)
(c-lang-defconst c-type-prefix-kwds
;; Keywords where the following name - if any - is a type name, and
;; where the keyword together with the symbol works as a type in
;; declarations. In this case, like "mixin foo!(x) bar;"
d '("mixin" "align"))
;; Remove "enum" from d-mode's value.
;; By default this c-typedef-decl-kwds includes c-brace-list-decl-kwds,
;; which is '("enum") by default.
;; Instead, parse enums manually (see d-font-lock-enum-body) to avoid
;; confusion with manifest constants.
(c-lang-defconst c-typedef-decl-kwds
;; Keywords introducing declarations where the identifier(s) being
;; declared are types.
d (append (c-lang-const c-class-decl-kwds)
'("typedef" "alias")))
(c-lang-defconst c-decl-hangon-kwds
d '("export"))
(c-lang-defconst c-protection-kwds
;; Access protection label keywords in classes.
d '("deprecated" "static" "extern" "final" "synchronized" "override"
"abstract" "scope"
"private" "package" "protected" "public" "export"))
(c-lang-defconst c-postfix-spec-kwds
;Keywords introducing extra declaration specifiers in the region
;between the header and the body (i.e. the "K&R-region") in
;declarations.
d '("if" "in" "out" "body"))
(c-lang-defconst c-recognize-knr-p
d t)
(c-lang-defconst c-type-list-kwds
d nil)
(c-lang-defconst c-ref-list-kwds
d '("import" "module"))
(c-lang-defconst c-colon-type-list-kwds
;; Keywords that may be followed (not necessarily directly) by a colon
;; and then a comma separated list of type identifiers.
d '("class" "enum" "interface"))
(c-lang-defconst c-paren-nontype-kwds
;;Keywords that may be followed by a parenthesis expression that doesn't
;; contain type identifiers.
d '("version" "debug" "extern" "pragma" "__traits" "scope"))
(c-lang-defconst d-type-modifier-kwds
;; D's type modifiers.
d '("const" "immutable" "inout" "shared"))
(c-lang-defconst d-type-modifier-key
;; Regex of `d-type-modifier-kwds'.
d (c-make-keywords-re t
(c-lang-const d-type-modifier-kwds)))
(c-lang-defconst d-common-storage-class-kwds
;; D's storage classes (keywords that can prefix or entirely
;; substitute a type in a parameter or variable declaration).
d `(;; Constness
,@(c-lang-const d-type-modifier-kwds)
;; Storage classes that apply to either parameters and declarations
"scope"))
(c-lang-defconst d-decl-storage-class-kwds
d `(;; Common keywords
,@(c-lang-const d-common-storage-class-kwds)
;; auto (no-effect placeholder)
"auto"
;; Storage class
"extern" "static" "__gshared"))
(c-lang-defconst d-param-storage-class-kwds
d `(;; Common keywords
,@(c-lang-const d-common-storage-class-kwds)
;; Function parameters
"in" "out" "ref" "lazy"))
(c-lang-defconst d-storage-class-kwds
d (c--delete-duplicates (append (c-lang-const d-decl-storage-class-kwds)
(c-lang-const d-param-storage-class-kwds))
:test 'string-equal))
(c-lang-defconst c-paren-type-kwds
;; Keywords that may be followed by a parenthesis expression containing
;; type identifiers separated by arbitrary tokens.
d (append (list "delete" "throw" "cast")
(c-lang-const d-type-modifier-kwds)))
;; D: Like `c-regular-keywords-regexp', but contains keywords which
;; cannot occur in a function type. For Emacs 25 imenu.
(c-lang-defconst d-non-func-type-kwds-re
d (concat "\\<"
(c-make-keywords-re t
(c--set-difference (c-lang-const c-keywords)
(append (c-lang-const c-primitive-type-kwds)
(c-lang-const d-decl-storage-class-kwds))
:test 'string-equal))))
;; D: Like `c-regular-keywords-regexp', but contains keywords which
;; cannot occur in a function name. For Emacs 25 imenu.
(c-lang-defconst d-non-func-name-kwds-re
d (concat "\\<"
(c-make-keywords-re t (c-lang-const c-keywords))))
(c-lang-defconst c-block-stmt-1-kwds
;; Statement keywords followed directly by a substatement.
d '("do" "else" "finally" "try" "in" "body"))
(c-lang-defconst c-block-stmt-2-kwds
;; Statement keywords followed by a paren sexp and then by a substatement.
d '("for" "if" "switch" "while" "catch" "synchronized" "scope" "version"
"foreach" "foreach_reverse" "with" "out" "invariant" "unittest"))
(c-lang-defconst c-simple-stmt-kwds
;; Statement keywords followed by an expression or nothing.
d '("break" "continue" "goto" "return" "throw"))
(c-lang-defconst c-paren-stmt-kwds
;; Statement keywords followed by a parenthesis expression that
;; nevertheless contains a list separated with ';' and not ','."
d '("for" "foreach" "foreach_reverse"))
(c-lang-defconst c-asm-stmt-kwds
;; Statement keywords followed by an assembler expression.
d '("asm"))
(c-lang-defconst c-label-kwds
;; Keywords introducing colon terminated labels in blocks.
d '("case" "default"))
(c-lang-defconst c-before-label-kwds
;; Keywords that might be followed by a label identifier.
d '("goto" "break" "continue"))
(c-lang-defconst c-constant-kwds
;; Keywords for constants.
d '("null" "true" "false"))
(c-lang-defconst c-primary-expr-kwds
;; Keywords besides constants and operators that start primary expressions.
d '("this" "super"))
(c-lang-defconst c-inexpr-class-kwds
;; Keywords that can start classes inside expressions.
d nil)
(c-lang-defconst c-inexpr-brace-list-kwds
;; Keywords that can start brace list blocks inside expressions.
d nil)
(c-lang-defconst c-other-decl-kwds
d (c-lang-const d-storage-class-kwds))
(c-lang-defconst c-decl-start-kwds
d '("debug" "else"))
(c-lang-defconst c-other-kwds
;; Keywords not accounted for by any other `*-kwds' language constant.
d '("__gshared" "__traits" "assert" "cast" "in" "is" "nothrow" "pure" "ref"
"sizeof" "typeid" "typeof"))
(c-lang-defconst c-recognize-post-brace-list-type-p
;; Set to t when we recognize a colon and then a type after an enum,
;; e.g., enum foo : int { A, B, C };"
d t)
;; Enabled for java-mode, but we don't need it.
;; (We can't reuse this for D templates because this is hard-wired to
;; the < and > characters.)
(c-lang-defconst c-recognize-<>-arglists
d nil)
;; ----------------------------------------------------------------------------
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; cc-mode patches ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ----------------------------------------------------------------------------
(defmacro d-make-keywords-re (adorn list)
"Helper to precompute regular expressions for inline keyword lists." ;; checkdoc-params: (adorn list)
(eval `(c-make-keywords-re ,adorn ,list 'd)))
(defmacro d--static-if (condition new-form &optional old-form)
"If CONDITION is non-nil at compile time, evaluate NEW-FORM, otherwise OLD-FORM."
(declare (indent 2))
(if (eval condition)
new-form
old-form))
(defmacro d--if-version>= (min-version new-form &optional old-form)
"Conditional compilation based on the current Emacs version.
Evaluate OLD-FORM if the Emacs version is older than MIN-VERSION,
otherwise NEW-FORM."
(declare (indent 2))
`(d--static-if (version<= ,min-version emacs-version) ,new-form ,old-form))
;;----------------------------------------------------------------------------
;;; Workaround for special case of 'else static if' not being handled properly
(defun d-special-case-looking-at (orig-fun &rest args)
;; checkdoc-params: (orig-fun args)
"Advice function for fixing cc-mode indentation in certain D constructs."
(let ((rxp (car args)))
(if (and (stringp rxp) (string= rxp "if\\>[^_]"))
(or (apply orig-fun '("static\\>\\s-+if\\>[^_]"))
(apply orig-fun '("version\\>[^_]"))
(apply orig-fun '("debug\\>[^_]"))
(apply orig-fun args))
(apply orig-fun args))))
(defun d-around--c-add-stmt-syntax (orig-fun &rest args)
;; checkdoc-params: (orig-fun args)
"Advice function for fixing cc-mode indentation in certain D constructs."
(if (not (c-major-mode-is 'd-mode))
(apply orig-fun args)
(add-function :around (symbol-function 'looking-at)
#'d-special-case-looking-at)
(unwind-protect
(apply orig-fun args)
(remove-function (symbol-function 'looking-at)
#'d-special-case-looking-at))))
(advice-add 'c-add-stmt-syntax :around #'d-around--c-add-stmt-syntax)
;;----------------------------------------------------------------------------
(defun d-forward-keyword-clause (match)
"D version (complement) of `c-forward-keyword-clause'." ;; checkdoc-params: match
(let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos)
(when kwd-sym
(goto-char (match-end match))
(c-forward-syntactic-ws)
(setq safe-pos (point))
(cond
;; module and import statements
((c-keyword-member kwd-sym 'c-ref-list-kwds)
(while
(progn
(c-forward-syntactic-ws)
(setq safe-pos (point))
(cond
((looking-at c-identifier-start)
;; identifier
(setq c-last-identifier-range nil)
(forward-char)
(c-end-of-current-token)
(when c-record-type-identifiers
(c-record-ref-id (cons safe-pos (point))))
t)
;; . or , or = (keep fontifying)
((memq (char-after) '(?. ?, ?=))
(forward-char)
t)
;; ; or : or anything else weird
(t
nil))))
(goto-char safe-pos)
t)
;; Special words after certain keywords
((and (c-keyword-member kwd-sym 'c-paren-nontype-kwds)
(eq (char-after) ?\())
(forward-char)
(c-forward-syntactic-ws)
(c-forward-keyword-prefixed-id ref)
(goto-char safe-pos)
(c-forward-sexp)
(c-forward-syntactic-ws)
t)))))
;----------------------------------------------------------------------------
(defun d-forward-decl-or-cast-1 (preceding-token-end context last-cast-end &optional inside-macro)
"D version of `c-forward-decl-or-cast-1'." ;; checkdoc-params: (preceding-token-end context last-cast-end)
;; (message "(d-forward-decl-or-cast-1 %S %S %S) @ %S" preceding-token-end context last-cast-end (point))
;; D: Restore our context, if any
(when (memq context '(arglist decl))
(save-excursion
(c-backward-syntactic-ws)
(when (> (point) (point-min))
(setq context (or (c-get-char-property (1- (point)) 'd-context) context)))))
;; D: Handle conditional compilation.
;; The "debug" keyword, as well as the "else" keyword following a
;; "version" or "static if", can start a declaration even without a
;; { } block.
;; For this reason, these keywords are in `c-decl-start-kwds'.
;; However, `c-decl-start-kwds' are considered as part of the
;; declaration, so `c-forward-decl-or-cast-1' will be invoked with
;; point at the keyword. Handle this case here.
;; Note that other cases (e.g. static if) are handled implicitly by
;; their requirement of a parenthesis (')' is in `c-decl-prefix-re').
(while (looking-at (d-make-keywords-re t (c-lang-const c-decl-start-kwds d)))
(goto-char (match-end 1))
(c-forward-syntactic-ws))
(let (maybe-auto enum decl-start type-start id-start make-top)
(let (kind)
(while (setq kind (d-forward-attribute-or-storage-class context))
(setq maybe-auto t
enum (eq kind 'enum))
;; Skip over "public:" and similar specifiers.
;; For some reason cc-mode wants us to do it here.
(when (looking-at ":")
(forward-char)
(c-forward-syntactic-ws))))
(setq decl-start (point))
;; Discover what kind of enum declaration this is.
;; (t if this is an actual list of enumerated values)
(setq enum
(and enum
(memq context '(top nil))
(save-excursion
(and
(d-forward-type)
(or (looking-at "[:{;]")
(and
(d-forward-identifier)
(progn
(c-forward-syntactic-ws)
(looking-at "[:{;]"))))))))
(when
;; The various kinds of declarations are handled here:
(cond
;; Aggregate declaration (class/struct ...)
;; Handle enumerations here too.
((and (memq context '(top nil))
(or enum
(looking-at (d-make-keywords-re t (c-lang-const c-class-decl-kwds d)))))
(unless enum
(goto-char (match-end 1))
(c-forward-syntactic-ws))
(setq type-start (point))
(setq id-start type-start)
(and
(d-forward-identifier)
(progn
(c-forward-syntactic-ws)
(looking-at "[:({;]"))))
;; Mixin templates
((and (memq context '(top nil))
(looking-at (d-make-keywords-re t '("mixin"))))
(goto-char (match-end 1))
(c-forward-syntactic-ws)
(and
(looking-at (d-make-keywords-re t '("template")))
(progn
(goto-char (match-end 1))
(c-forward-syntactic-ws)
(setq type-start (point))
(setq id-start type-start)
(d-forward-identifier))
(progn
(c-forward-syntactic-ws)
(looking-at "("))))
;; Constructors/destructors
((and (eq context 'top)
(looking-at (d-make-keywords-re t '("this" "~this"))))
(setq id-start decl-start)
(goto-char (match-end 1))
t)
;; Alias declarations
((and (memq context '(top nil))
(looking-at (d-make-keywords-re t '("alias"))))
(setq type-start decl-start) ; indicate declaration type for e.g. imenu
(goto-char (match-end 1))
(c-forward-syntactic-ws)
(setq id-start (point))
(d-forward-type) ; alias name or value
(cond
;; New style (alias name = value)
((memq (char-after) '(?= ?\())
t)
;; Old style (alias value name)
((looking-at c-symbol-key)
(setq id-start (point))
t)))
;; Lambda literal
((and (memq context '(top nil arglist))
(save-excursion
(and
(d-forward-identifier)
(progn
(c-forward-syntactic-ws)
(looking-at "=>")))))
(setq id-start decl-start)
t)
;; Function / variable / constant declaration, i.e. an
;; (optional) type followed by an identifier.
((and (memq context '(top nil))
;; Declaration type, or identifier for auto
;; declarations. Note that all identifiers
;; are also valid types.
(d-forward-type))
(setq type-start decl-start)
(setq id-start (point))
(cond
;; auto-declaration
((and maybe-auto
(looking-at (c-lang-const d-decl-end-re)))
(setq id-start type-start)
(setq type-start nil)
t)
;; regular declaration
((and (d-forward-identifier)
(progn
(c-forward-syntactic-ws)
(looking-at (c-lang-const d-decl-end-re))))
t)))
;; Function parameter list.
;; Note: single (type-less) identifiers in parameter lists
;; mean different things for function and lambda parameter
;; lists. For functions, they indicate the type of an
;; anonymous parameter; for lambdas, they indicate the name
;; of a parameter with an inferred type.
((and (memq context '(decl varlist))
(d-forward-type))
(setq type-start decl-start)
(setq id-start (point))
(cond
;; Type or name only
((looking-at "[,=);]")
(when (eq context 'varlist)
(setq id-start type-start)
(setq type-start nil))
t)
;; Parameter name
((d-forward-identifier)
(c-forward-syntactic-ws)
(looking-at "[,=);]")))))
;; Valid declaration, process it.
(when (and c-record-type-identifiers type-start)
(let ((c-promote-possible-types t))
(save-excursion
(goto-char type-start)
(d-forward-type))))
(when (and (memq context '(decl varlist))
(eq (char-after) ?,))
;; As in c-forward-decl-or-cast-1:
;; Make sure to propagate the `c-decl-arg-start' property to
;; the next argument if it's set in this one, to cope with
;; interactive refontification.
(c-put-c-type-property (point) 'c-decl-arg-start)
;; D: Also save whether we're in a varlist.
(c-put-char-property (point) 'd-context context))
(when (and (memq context '(top nil))
(eq (char-after) ?\())
;; Move point past the parameter list (end of declaration) to
;; communicate to cc-mode that the parens represent parameters,
;; and not arguments.
(c-forward-sexp)
(c-forward-syntactic-ws)
;; Template arguments?
(when (eq (char-after) ?\()
(c-forward-sexp)
(c-forward-syntactic-ws)))
(d--if-version>= "26.0"
(list id-start
nil
nil
type-start
(or (eq context 'top) make-top))
(list id-start)))))
(defun d-around--c-forward-decl-or-cast-1 (orig-fun &rest args)
;; checkdoc-params: (orig-fun args)
"Advice function for patching D support into cc-mode."
(apply
(if (c-major-mode-is 'd-mode)
#'d-forward-decl-or-cast-1
orig-fun)
args))
(advice-add 'c-forward-decl-or-cast-1 :around #'d-around--c-forward-decl-or-cast-1)
;----------------------------------------------------------------------------
(defun d-forward-identifier ()
"Advance point by one D identifier."
(when (and (not (looking-at c-keywords-regexp))
(looking-at c-symbol-key))
(goto-char (match-end 0))
t))
(defun d-forward-attribute-or-storage-class (context)
"Handle D attributes and storage classes in declarations.
Advance point and return non-nil if looking at something that may prefix a
declaration (or follow the argument list, in case of functions).
CONTEXT is as in `c-forward-decl-or-cast-1'."
;; Note that this includes UDAs.
(let ((start (point))
(kind t)
match-index)
(when
(cond
((looking-at (d-make-keywords-re t '("enum")))
(setq match-index 1
kind 'enum))
((looking-at (d-make-keywords-re t '("scope")))
(setq match-index 1
kind 'scope))
((and (eq context nil)
(looking-at (d-make-keywords-re t '("return"))))
nil)
((looking-at (d-make-keywords-re t (c-lang-const d-type-modifier-kwds d)))
(setq match-index 1
kind 'type-modifier))
((looking-at (d-make-keywords-re t (c-lang-const c-modifier-kwds d)))
(setq match-index 1))
((and (eq (char-after) ?@)
(looking-at c-symbol-key))
(setq match-index 0)))
(goto-char (match-end match-index))
(c-forward-syntactic-ws)
;; Skip parameters, such as:
;; - extern(C)
;; - deprecated("...")
;; - package(std)
;; - @SomeUDA(...)
(if (looking-at "(")
;; 1. Distinguish between "const x" and "const(x)". In the
;; former, "const" is an attribute, but in the latter it is
;; part of a type. This distinction is important when parsing
;; constructs where a type is required; a greedy attribute
;; match would leave only "(x)" which will not make sense.
;; 2. Distinguish between scope variables (e.g. "scope x = new
;; ...") and scope statements (e.g. "scope(exit) { ... }").
(if (memq kind '(type-modifier scope))
(progn
(goto-char start)
nil)
(c-go-list-forward)
(c-forward-syntactic-ws)
kind)
kind))))
;;----------------------------------------------------------------------------
(defun d-around--c-get-fontification-context (orig-fun match-pos &rest args)
;; checkdoc-params: (orig-fun match-pos args)
"Advice function for fixing cc-mode handling of D lambda parameter lists."
(let ((res (apply orig-fun match-pos args))
(type 'varlist))
;; (message "(c-get-fontification-context %S) @ %S -> %S" args (point) res)
(when (and
(c-major-mode-is 'd-mode)
(memq (car res) '(nil arglist))
(save-excursion
(and
(progn
(goto-char match-pos)
(c-backward-syntactic-ws)
(eq (char-before) ?\())
(progn
(backward-char)
(or
(save-excursion
(and
(c-backward-token-2)
(cond
((looking-at (d-make-keywords-re t '("if" "while" "for" "switch"
"with" "synchronized")))
(setq type nil)
t)
((looking-at (d-make-keywords-re t '("foreach" "foreach_reverse")))
t)
((looking-at (d-make-keywords-re t '("catch")))
(setq type 'decl)
t))))
(condition-case nil
(progn
(c-forward-sexp)
(c-forward-syntactic-ws)
(while (d-forward-attribute-or-storage-class 'top))
(or
(eq (char-after) ?\{)
(looking-at "=>")))
(error nil)))))))
(setq res (cons type t))
;; (message " patching -> %S" res)
)
res))
(advice-add 'c-get-fontification-context :around #'d-around--c-get-fontification-context)
;;----------------------------------------------------------------------------
;; Borrowed from https://github.com/josteink/csharp-mode/blob/master/csharp-mode.el
(defmacro d--syntax-propertize-string (open close)
(unless (eq (length close) 1)
(error "`close' should be a single character"))
(let ((syntax-punctuation (string-to-syntax "."))
(skip-chars-pattern (concat "^" close "\\\\"))
(close-char (elt close 0)))
`(progn
(goto-char beg)
(while (search-forward ,open end t)
(let ((in-comment-or-string-p (save-excursion
(goto-char (match-beginning 0))
(or (nth 3 (syntax-ppss))
(nth 4 (syntax-ppss))))))
(when (not in-comment-or-string-p)
(let (done)
(while (and (not done) (< (point) end))
(skip-chars-forward ',skip-chars-pattern end)
(cond
((eq (following-char) ?\\)
(put-text-property (point) (1+ (point))
'syntax-table ',syntax-punctuation)
(forward-char 1))
((eq (following-char) ',close-char)
(forward-char 1)
(setq done t)))))))))))
(defun d--syntax-propertize-function (beg end)
"Apply syntax table properties to special constructs in region BEG to END.
Handles `...` and r\"...\" WYSIWYG string literals."
(save-excursion
(d--syntax-propertize-string "`" "`")
(d--syntax-propertize-string "r\"" "\"")))
;;----------------------------------------------------------------------------
(defun d--on-func-identifier ()
"Version of `c-on-identifier', but also match D constructors."
(save-excursion
(skip-syntax-backward "w_")
;; Check for a normal (non-keyword) identifier.
(and (looking-at c-symbol-start)
(or
(looking-at (d-make-keywords-re t '("this" "~this")))
(not (looking-at c-keywords-regexp)))
(point))))
(defun d-in-knr-argdecl (&optional lim)
"Modified version of `c-in-knr-argdecl' for d-mode." ;; checkdoc-params: lim
(save-excursion
;; If we're in a macro, our search range is restricted to it. Narrow to
;; the searchable range.
(let* ((start (point))
before-lparen
after-rparen
(pp-count-out 20) ; Max number of paren/brace constructs before
; we give up.
knr-start
c-last-identifier-range)
(catch 'knr
(while (> pp-count-out 0) ; go back one paren/bracket pair each time.
(setq pp-count-out (1- pp-count-out))
(c-syntactic-skip-backward "^)]}=;")
(cond ((eq (char-before) ?\))
(setq after-rparen (point)))
((eq (char-before) ?\])
(setq after-rparen nil))
(t ; either } (hit previous defun) or = or no more
; parens/brackets.
(throw 'knr nil)))
(if after-rparen
;; We're inside a paren. Could it be our argument list....?
(if
(and
(progn
(goto-char after-rparen)
(unless (c-go-list-backward) (throw 'knr nil)) ;
;; FIXME!!! What about macros between the parens? 2007/01/20
(setq before-lparen (point)))
;; It can't be the arg list if next token is ; or {
(progn (goto-char after-rparen)
(c-forward-syntactic-ws)
(not (memq (char-after) '(?\; ?\{ ?\=))))
;; Is the thing preceding the list an identifier (the
;; function name), or a macro expansion?
(progn
(goto-char before-lparen)
(eq (c-backward-token-2) 0)
(or (eq (d--on-func-identifier) (point))
(and (eq (char-after) ?\))
(c-go-up-list-backward)
(eq (c-backward-token-2) 0)
(eq (d--on-func-identifier) (point)))))
;; Check that we're outside of the template arg list (D-specific).
(progn
(setq knr-start
(progn (goto-char after-rparen)
(c-forward-syntactic-ws)
(when (eq (char-after) ?\()
(c-go-list-forward)
(c-forward-syntactic-ws))
(point)))
(<= knr-start start))
;; (... original c-in-knr-argdecl logic omitted here ...)
t)
;; ...Yes. We've identified the function's argument list.
(throw 'knr knr-start)
;; ...No. The current parens aren't the function's arg list.
(goto-char before-lparen))
(or (c-go-list-backward) ; backwards over [ .... ]
(throw 'knr nil))))))))
(defun d-around--c-in-knr-argdecl (orig-fun &rest args)