forked from hackonteur/specman-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecman-mode.el
5299 lines (4574 loc) · 192 KB
/
specman-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
;; -*- lexical-binding: t -*-
;; This file is not part of Emacs
;;
;; Copyright (C) 2013 Cadence Design Systems, Inc.
;; 2012-2022 Verilab, Inc.
;; Authors: Uri Maoz <urim@cadence.com>
;; Michael McNamara <mac@cadence.com>
;; Yaron Peri <yperi@cadence.com>
;; Scott Roland <scott.roland@verilab.com>
;; Marcus Harnisch <marcus.harnisch@verilab.com>
;; Maintainer: Marcus Harnisch <marcus.harnisch@verilab.com>
;; Created: Jul 09 2022
;; COPYRIGHT NOTICE
;;
;; 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 2 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 GNU Emacs. If you did not, write to the Free Software Foundation,
;; Inc., 675 Mass Ave., Cambridge, MA 02139, USA.
;;
;; =============================================================================
;; SPECMAN MODE - A Major mode for writing in 'e'
;; =============================================================================
;;
;; Put this file on your Emacs-Lisp load path, add following into your
;; Emacs initialization file (e.g. ~/.emacs, ~/.emacs.d/init.el):
;;
;; (autoload 'specman-mode "specman-mode" "Specman code editing mode" t)
;;
;; (add-to-list 'auto-mode-alist '("\\.e\\'" . specman-mode))
;; (add-to-list 'auto-mode-alist '("\\.ecom\\'" . specman-mode))
;; (add-to-list 'auto-mode-alist '("\\.erld\\'" . specman-mode))
;;
;; Alternative and arguably more elegant approach based on `use-package':
;;
;; (use-package specman-mode
;; :mode ("\\.e\\(?:com\\|rld\\)?\\'" . specman-mode))
;;
;; Commentary:
;;
;; specman-mode is a major mode for editing code written in the 'e' language
;;
(defconst specman-mode-version "2.0"
"Version of this Specman mode.")
(defun specman-version ()
"Inform caller of the version of this file"
(interactive)
(message (concat "Specman Mode " specman-mode-version))
)
(eval-after-load "speedbar"
'(add-hook 'speedbar-load-hook
(lambda ()
(speedbar-add-supported-extension ".e"))))
(eval-when-compile
(require 'cl-lib))
(require 'imenu)
(require 'easymenu)
(eval-when-compile
(require 'compile))
(eval-and-compile
(if (featurep 'xemacs)
(defalias 'specman--char= 'char=) ; XEmacs
(defalias 'specman--char= '=)) ; FSF Emacs
)
(eval-when-compile
;; Try hard to inline the alias as efficiently as possible
;; (see https://nullprogram.com/blog/2019/12/10/)
(put 'specman--char= 'byte-optimizer 'byte-compile-inline-expand))
(if (< max-specpdl-size 3000)
(setq max-specpdl-size 3000))
;; =================================================
;; SPECMAN CUSTOMIZABLE VARS
;; =================================================
;;; - Customizable variables
(defgroup specman-mode nil
"Facilitates easy editing of Specman source text"
:group 'languages
)
(defcustom specman-basic-offset 4
"*Indentation of Specman statements with respect to containing block."
:group 'specman-mode
:type 'integer
)
(put 'specman-basic-offset 'safe-local-variable #'integerp)
(defconst specman-tab-width specman-basic-offset
"*Tab stop width"
)
(defcustom specman-continued-line-offset 2
"*Indentation of continued Specman statements with respect to first line of statement."
:group 'specman-mode
:type 'integer
)
(defcustom specman-default-line-comment-starter "--"
"*Default character sequence to start line comments.
Used in comment inserting functions and for creating comments
from templates."
:group 'specman-mode
:type '(choice (const :tag "-- (VHDL)" "--")
(const :tag "// (Verilog)" "//"))
)
(defcustom specman-command-prefix "\C-c"
"Prefix for `specman-mode' commands.
Used to avoid key binding clashes with minor modes. Note that
changing the value of this variable will have no effect in the
current session as keymaps have already been set up."
:group 'specman-mode
:type '(choice (const :tag "C-c s" "\C-cs")
(const :tag "C-c" "\C-c" )
(const :tag "none" "")
string))
(defcustom specman-auto-newline nil
"*ON (or non nil) means to automatically newline after inserting a semicolon."
:group 'specman-mode
:type 'boolean
)
(defcustom specman-auto-endcomments nil
"*ON (or non nil) means to automatically add a comment when closing a code block."
:group 'specman-mode
:type 'boolean
)
(defcustom specman-auto-endcomments-for-major-scopes-only t
"*ON (or non nil) means that end-comments are only applied to struct and method scopes."
:group 'specman-mode
:type 'boolean
)
(defcustom specman-auto-endcomments-kill-existing-comment t
"*ON (or non nil) means that redoing an end-comment replaces an existing end-comment."
:group 'specman-mode
:type 'boolean
)
(defcustom specman-max-line-length 80
"*The maximum number of characters that should be in a line."
:group 'specman-mode
:type 'integer
)
(defcustom specman-highlight-beyond-max-line-length t
"*ON (or non nil) means to highlight text beyond specman-max-line-length."
:group 'specman-mode
:type 'boolean
)
(defface specman-highlight-beyond-max-line-length-face
'((t (:underline t)))
"Face used to highlight text beyond the defined maximum line length."
:group 'specman-mode
)
(defcustom specman-highlight-all-caps nil
"*ON (or non nil) means to highlight words in all-caps, commonly used
for defines and enum members."
:group 'specman-mode
:type 'boolean
)
(defcustom specman-highlight-punctuation t
"*ON (or non nil) means to highlight punctuation symbols."
:group 'specman-mode
:type 'boolean
)
(defface specman-punctuation-face
'((t (:foreground "SteelBlue")))
"Face used to highlight punctuation symbols - significant because they are mostly a single char."
:group 'specman-mode
)
(defcustom specman-compile-command "${SPECMAN_HOME}/bin/sn_compile.sh"
"String used to compile e files."
:group 'specman-mode
:type 'string
)
(defcustom specman-make-command "/usr/bin/make"
"String used to invoke make"
:group 'specman-mode
:type 'string
)
(defcustom specman-index-menu-active t
"*ON (or non nil) means that the 'Specman-Index' menu is active."
:group 'specman-mode
:type 'boolean
)
(defcustom specman-date-scientific-format nil
"*If non-nil, dates are written in scientific format (e.g. 1997/09/17),
in european format otherwise (e.g. 17.09.1997). The braindead american
format (e.g. 09/17/1997) is not supported."
:group 'specman-mode
:type 'boolean
)
(defcustom specman-company "ACME Company"
"*Default name of Company for specman header. If set will become buffer local. "
:group 'specman-mode
:type 'string
)
(defcustom specman-project "Roadrunner"
"*Default name of Project for specman header. If set will become buffer local."
:group 'specman-mode
:type 'string
)
;; =================================================
;; SPECMAN REGULAR EXPRESSIONS
;; =================================================
(defconst specman-legal-name-regexp "\\(?:[a-zA-Z][a-zA-Z0-9_]*\\)"
"e legal name")
;; Matthew Lovell <lovell@fc.hp.com>
;; Up to one level of parentheses is allowed in the parameter
;; list to a method. The regex pattern for that portion if
;; from J. Friedl's "Mastering Regular Expressions"
(defconst specman-imenu-method-definition-regexp
"\\(?:\\(?:private\\|package\\|protected\\)[ \t\n]+\\)?\\([A-Za-z0-9_]+\\)[ \t\n]*([^()]*\\(?:([^()]*)[^()]*\\)*)[^-/;]*?[ \t\n]+is[a-z \t\n]*{"
"Regexp that identifies methods (arg 1)")
(defconst specman-method-definition-regexp
(concat
"\\(?:\\(?:private\\|package\\|protected\\)[ \t\n]+\\)?"
"\\(?:\\(?:final\\|static\\)[ \t\n]+\\)?"
"\\([A-Za-z0-9_]+\\)" ;; method name
"[ \t\n]*([^()]*\\(?:([^()]*)[^()]*\\)*)" ;; parameter list
"[^-/;]*?" ;; skip over optional return type
"\\<is\\>[ \t\n]*"
"\\(?:"
"\\(?:\\(?:also\\|only\\|first\\)[ \t\n]*\\)?{" ;; regular method
"\\|"
"\\(?:empty\\|undefined\\)[ \t\n]*;" ;; prototype/virtual
"\\)"
)
"Regexp that identifies methods definitions (arg 1)")
(defconst specman-interface-method-definition-regexp
(concat
"\\([A-Za-z0-9_]+\\)" ;; method name
"[ \t\n]*([^()]*\\(?:([^()]*)[^()]*\\)*)" ;; parameter list
".*?;" ;; skip over optional return type
)
"Regexp that identifies interface method definitions (arg 1)
Use this regexp only within interface declarations as it also
matches method call syntax.")
(defconst specman-method-type-definition-regexp
(concat
"\\<method_type[ \t\n]+"
"\\([A-Za-z0-9_]+\\)" ;; method name
"[ \t\n]*([^()]*\\(?:([^()]*)[^()]*\\)*)" ;; parameter list
".*?;" ;; skip over optional return type
)
"Regexp that identifies method type definitions (arg 1)")
(defconst specman-imenu-on-event-method-definition-regexp
"\\(on[ \t\n]+[A-Za-z0-9_]+\\)[ \t\n]*{"
"Regexp that identifies on-event methods (arg 1)")
(defconst specman-on-event-method-definition-regexp
(concat
"\\(?:static[ \t\n]*\\)?"
"on[ \t\n]+"
"\\(?:\\(?:\\sw+\\.\\)+\\|\\sw+::\\)?"
"\\(\\sw+\\)\\$?" ;; Event (port) name
"\\(?:[ \t\n]+with[ \t\n]+any[ \t\n]+\\sw+\\(?:[ \t\n]*,[ \t\n]*\\sw+\\)*\\)?"
"[ \t\n]*{"
)
"Regexp that identifies on-event methods")
(defconst specman-subtype-determinant-regexp
"\\<\\(\\sw+\\)\\(?:'\\(\\sw+\\)\\)?\\>"
"Regexp that identifies a canonical subtype determinant in
struct/unit definitions.")
(defconst specman-struct-type-regexp
"\\<\\sw+\\>"
"Regexp that identifies type and optionally base-type of
struct/unit definitions. Notice the third match group to identify
the new starting point.")
(defconst specman-when-extension-regexp
"\\<when\\>"
"Regexp that matches \"when\" struct members.")
(defconst specman-class-end-of-name-regexp
"\\(?:\\<\\(?:like\\|implementing\\|of\\|is\\|using\\|with\\)\\>\\|=\\|(\\|)\\|{\\|}\\|;\\|,\\|:\\|{\\|@\\)"
"Regexp that identifies the end of a class name.")
(defconst specman-class-definition-regexp
(concat
"\\(?:\\(?:\\<package[ \t\n]+\\)?"
"\\(?:\\<template[ \t\n]+\\)?"
"\\<\\(sequence\\|struct\\|unit\\|interface\\|numeric_type\\|extend\\)\\>"
"[ \t\n][^:{]*?\\(\\<\\sw+\\>\\)[ \t\n]*\\(?:"
specman-class-end-of-name-regexp
"\\)\\)"
)
"Regexp that identifies major class scopes (arg 1)")
(defconst specman-when-subtype-definition-regexp
"\\(when[ \t\n]+[^{]+\\)"
"Regexp that identifies when subtypes (arg 1)")
(defconst specman-define-regexp
"\\(define[ \t]*<[^>]+>\\)"
"Regexp that identifies define scopes (arg 1)")
(defconst specman-macro-and-define-regexp
"\\(#?define\\|#ifn?def\\|#undef\\)[ \t\n]+\\<\\(\\sw+\\)\\>"
"Regexp that identifies preprocessor definition keywords (arg 1) and names (arg 2)")
(defconst specman-top-level-container-definition-regexp
(concat
"\\("
specman-class-definition-regexp
"\\|"
"\\(?:\\(?:#?define\\|#ifn?def\\|#else\\)[ \t\n]+[^ \t\n]+\\)"
"\\)")
"Regexp that identifies all specman container scopes (arg 1)")
(defconst specman-container-scope-definition-regexp
(concat
"\\("
"\\(?:\\(?:package[ \t\n]*\\)?struct[ \t\n]+[A-Za-z0-9_]+\\)"
"\\|"
"\\(?:extend[ \t\n]+[^{]+\\)"
"\\|"
"\\(?:\\(?:package[ \t\n]*\\)?unit[ \t\n]+[A-Za-z0-9_]+\\)"
"\\|"
"\\(?:\\(?:#?define\\|#ifn?def\\|#else\\)[ \t\n]+[^ \t\n]+\\)"
"\\|"
"\\(?:when[ \t\n]+[^{]+\\)"
"\\)")
"Regexp that identifies all specman container scopes (arg 1)")
(defconst specman-field-definition-regexp
(concat
"\\(?:\\<\\(?:private\\|package\\|protected\\)\\>[ \t\n]+\\)?"
"\\(?:\\<static\\>[ \t\n]+\\)?"
"\\(?:\\<const\\>[ \t\n]+\\)?"
"\\(?:[~%!]+[ \t\n]*\\)?"
"\\([A-Za-z0-9_]+\\)" ;; field name
"\\(?:[ \t\n]*\\[.*?\\]\\)*" ;; list lengths
"[ \t\n]*[:;]")
"Regexp that identifies field definitions (arg1)")
(defconst specman-variable-definition-regexp
"var[ \t\n]+\\([A-Za-z0-9_]+\\)[ \t\n]*[:;]"
"Regexp that identifies variable definitions (arg 1)")
(defconst specman-event-definition-regexp
"\\(?:\\(?:private\\|package\\|protected\\)[ \t\n]+\\)?event[ \t\n]+\\([A-Za-z0-9_]+\\)"
"Regexp that identifies event definitions (arg 1)")
(defconst specman-type-definition-regexp
"\\(?:\\(?:package[ \t\n]*\\)?\\(?:method_\\)?type\\|extend\\)[ \t\n]+\\([A-Za-z0-9_]+\\)[ \t\n]*:[ \t\n]*\\["
"Regexp that identifies type definitions (arg 1)")
(defconst specman-cover-definition-regexp
"cover[ \t\n]+\\([A-Za-z0-9_]+\\)[ \t\n]+is"
"Regexp that identifies cover definitions (arg 1)")
(defconst specman-number-regexp
(concat
"\\("
"\\(?:"
"\\<\\(?:"
"0b[01_]+" ;; unsized binary
"\\|"
"0o[0-7_]+" ;; unsized octal
"\\|"
"0x[0-9a-fA-F_]+" ;; unsized hexadecimal
"\\|"
"[0-9]+'[bB][01hHlLnNuUwWxXzZ_]+" ;; sized binary (incl MVL)
"\\|"
"[0-9]+'[oO][0-7hHlLnNuUwWxXzZ_]+" ;; sized octal (incl MVL)
"\\|"
"[0-9]+'[xXhH][0-9a-fA-FhHlLnNuUwWxXzZ_]+" ;; sized hexadecimal (incl MVL)
"\\|"
"[0-9]+'[dD][0-9][0-9_]*" ;; sized decimal
"\\)\\>"
"\\)"
"\\|"
"-?\\<[0-9_]+[kKmM]" ;; unsized decimal w/ multiplier
"\\|"
"-?\\<[0-9_]+\\(?:\\.[0-9_]+\\)?\\(?:[eE][+-]?[0-9]+\\)?" ;; other unsized decimal and real
"\\)\\>"
)
"Regexp that identifies numbers (arg1)")
(defconst specman-literal-character-regexp
(concat
"\\("
"\\<0c\"\\(?:[ -Z^-~]\\|[][]\\|\\\\[fntr\\\"]\\)\"" ;; literal character (printable ASCII + few escape seqs)
"\\)"
)
"Regexp that identifies numbers (arg1)")
(defconst specman-method-definition-regexp-full
(concat "^[ \t]*" specman-method-definition-regexp))
(defconst specman-interface-method-definition-regexp-full
(concat "^[ \t]*" specman-interface-method-definition-regexp))
(defconst specman-method-type-definition-regexp-full
(concat "^[ \t]*" specman-method-type-definition-regexp))
(defconst specman-on-event-method-definition-regexp-full
(concat "^[ \t]*" specman-on-event-method-definition-regexp))
(defconst specman-class-definition-regexp-full
(concat "^[ \t]*" specman-class-definition-regexp))
(defconst specman-when-subtype-definition-regexp-full
(concat "^[ \t]*" specman-when-subtype-definition-regexp))
(defconst specman-when-extension-regexp-full
(concat "^[ \t]*" specman-when-extension-regexp))
(defconst specman-define-regexp-full
(concat "^[ \t]*" specman-define-regexp))
(defconst specman-macro-and-define-regexp-full
(concat "^[ \t]*" specman-macro-and-define-regexp))
(defconst specman-container-scope-definition-regexp-full
(concat "^[ \t]*" specman-container-scope-definition-regexp))
(defconst specman-field-definition-regexp-full
(concat "^[ \t]*" specman-field-definition-regexp))
(defconst specman-variable-definition-regexp-full
(concat "^[ \t]*" specman-variable-definition-regexp))
(defconst specman-event-definition-regexp-full
(concat "^[ \t]*" specman-event-definition-regexp))
(defconst specman-type-definition-regexp-full
(concat "^[ \t]*" specman-type-definition-regexp))
(defconst specman-cover-definition-regexp-full
(concat "^[ \t]*" specman-cover-definition-regexp))
;; =================================================
;; SPECMAN SEARCHES AND QUERIES
;; =================================================
(defsubst specman-within-comment-p ()
(or (specman-within-line-comment-p)
(specman-within-region-comment-p))
)
(defsubst specman-empty-line-p ()
(looking-at "^[ \t]*$"))
(defun specman-within-string-p ()
(nth 3 (syntax-ppss (point))))
(defsubst specman-line-within-string-p ()
(save-excursion
(beginning-of-line)
(specman-within-string-p)))
(defsubst specman-within-square-brackets-p ()
(and (not (specman-within-comment-p))
(specman-safe-char=
?\[
(char-after (nth 1 (syntax-ppss (point)))))))
(defsubst specman-prepared-buffer-substring (beg end)
"Remove extra spaces and new-lines from strings."
(save-match-data
(replace-regexp-in-string "[ \t\n]+"
" "
(replace-regexp-in-string "^[ \t\n]+\\|[ \t\n]+$"
""
(buffer-substring beg
end)))))
(defsubst specman-re-search-forward (REGEXP BOUND NOERROR &optional within-code-region)
"Like re-search-forward, but skips over matches in comments or strings"
(let ((start-pos
(point))
(full-regexp
(concat
"\\(//\\|--\\)\\|\\(^'>\\)\\|\\(\"\\)\\|\\(/\\*\\)\\|\\("
REGEXP
"\\)"))
)
(unless within-code-region
(specman-skip-forward-comment-or-string))
(store-match-data '(nil nil))
(while (and (if BOUND
(<= (point) BOUND)
t)
(re-search-forward full-regexp BOUND NOERROR)
(cond
(;; e line comment
(match-beginning 1)
(progn
(forward-line 1)
t)
)
(;; ex-code region
(match-beginning 2)
(progn
(re-search-forward "^<'" BOUND 'move)
t)
)
(;; string
(match-beginning 3)
(progn
;; to also consider the next character, for ""
(goto-char (match-beginning 3))
(re-search-forward "\\(?:[^\\]\\|[\\][\\]\\)\"" BOUND 'move)
t)
)
(;; c multi-line comment
(match-beginning 4)
(progn
(re-search-forward "\\*/" BOUND 'move)
t)
)
(;; the regexp
(match-beginning 5)
(progn
(goto-char (match-beginning 5))
(looking-at REGEXP) ;; to reset match-data
nil)
)
)
(progn
(store-match-data '(nil nil))
(if BOUND
(< (point) BOUND)
t))
))
(goto-char (if (match-end 0)
(match-end 0)
start-pos))
(match-end 0)))
(defsubst specman-re-search-backward (REGEXP BOUND NOERROR &optional within-code-region)
"Like re-search-backward, but skips over matches in comments or strings"
(let ((start-pos
(point))
(full-regexp
(concat
"\\(^<'\\)\\|\\(\"\\)\\|\\(\\*/\\)\\|\\("
REGEXP
"\\)"))
)
(unless within-code-region
(specman-skip-backward-comment-or-string))
(store-match-data '(nil nil))
(while (and (if BOUND
(>= (point) BOUND)
t)
(re-search-backward full-regexp BOUND NOERROR)
(cond
(;; ex-code region
(match-beginning 1)
(progn
(re-search-backward "^'>" BOUND 'move)
t)
)
(;; string
(match-beginning 2)
(progn
;; NOTE: this approach is more elegant, but slower than the code below
;;(if (specman-within-string-p)
;; (re-search-backward "[^\\]\"" BOUND 'move)
;; (re-search-backward "//\\|--" (line-beginning-position) 'move))
(when (save-match-data
(back-to-indentation)
(re-search-forward "\\(//\\|--\\)\\|\\([^\\]\"\\)" (match-beginning 2) 'move)
(if (match-beginning 1) ;; a comment and not a string
(progn
(goto-char (match-beginning 1)) ;; go to the comment beginning
nil) ;; and fail, staying there
t))
(goto-char (match-beginning 2)) ;; return to the string
(when ;; and find the string opener
(re-search-backward "\\(?:[^\\]\\|[\\][\\]\\)\\(\"\\)" BOUND 'move)
(goto-char (match-beginning 1))))
t)
)
(;; c multi-line comment
(match-beginning 3)
(progn
(re-search-backward "/\\*" BOUND 'move)
t)
)
(;; the regexp
(match-beginning 4)
(let ((failed-match nil)
)
(goto-char (match-beginning 4))
(looking-at REGEXP) ;; to reset match-data
(save-match-data
;; if a comment is not found then point is left on the
;; beginning of the line but the goto-char below fixes this.
;; note: the while checks that the comment prefix is not in a string
(while (and (re-search-backward "//\\|--" (line-beginning-position) 'move)
(if (specman-within-string-p)
t
(progn
(setq failed-match t)
nil)))))
failed-match
)
)
)
(progn
(store-match-data '(nil nil))
(if BOUND
(> (point) BOUND)
t))
))
(goto-char (if (match-beginning 0)
(match-beginning 0)
start-pos))
(match-beginning 0)))
;; =================================================
;; SPECMAN SYNTAX TABLE
;; =================================================
(defun specman-populate-syntax-table (table)
(modify-syntax-entry ?_ "w" table) ;; underscore is a part of words
(modify-syntax-entry ?` "w" table) ;; backtick is a part of words
(modify-syntax-entry ?{ "(}" table)
(modify-syntax-entry ?} "){" table)
(modify-syntax-entry ?\( "()" table)
(modify-syntax-entry ?\) ")(" table)
(modify-syntax-entry ?\[ "(]" table)
(modify-syntax-entry ?\] ")[" table)
(modify-syntax-entry ?\% "." table)
(modify-syntax-entry ?\@ "." table) ;; for events
(modify-syntax-entry ?\- "." table)
(modify-syntax-entry ?\' "." table)
(modify-syntax-entry ?\< "." table)
(modify-syntax-entry ?\> "." table)
(modify-syntax-entry ?\\ "\\" table)
(modify-syntax-entry ?\n "." table)
table
)
(defun specman-setup-dual-comments (table)
;; Set up TABLE to handle comments
;;
;; Specman (IEEE1647-2019) supports three different comment styles:
;;
;; 1. Line comments starting with "--"
;; 2. Line comments starting with "//"
;; 3. Block comments framed by "/*", and "*/"
;;
;; In addition there are regions between code segments (here
;; historically referred to as ex-code), which are effectively
;; comments, too.
;;
;; The way both Emacs and XEmacs can deal with multiple different
;; comment styles is not flexible enough for our purposes, so we
;; will have to use workarounds.
;;
;; Previous versions of specman-mode.el were setting up the syntax
;; table for the latter two styles, while simply "painting" comments
;; starting with "--", as well as ex-code regions in
;; `font-lock-comment-face' (search for
;; `specman-match-ex-code-regions' and
;; `specman-match-minus-comments' in previous revisions of this
;; file).
;;
;; While this helps syntax highlighting, it carries absolutely no
;; syntactic information which could be used for parsing/motion
;; purposes in other places of specman-mode.el.
;;
;; At this point it stops making sense to continue to maintain
;; XEmacs compatibility. Conditional code will be removed gradually.
(modify-syntax-entry ?/ ". 14" table)
(modify-syntax-entry ?* ". 23b" table)
(modify-syntax-entry ?\n ">" table)
(modify-syntax-entry ?\f ">" table)
)
;; Emacs 24.1 and beyond support syntax table text-properties,
;; i.e. the syntax class of text can be modified locally, based on
;; rules.
;;
;; Ideal for our purposes is the addition of "generic comment
;; delimiters" which allows us to distinguish line comments, block
;; comments, and ex-code immediately in the parser state returned by
;; `syntax-ppss', they are defined as comment-style "a", "b" and
;; generic comment, respectively.
(defvar specman--ecom-syntax-highlight nil
"True if the current buffer is an .ecom file and should be syntax
highlighted as such (i.e. ignore ex-code-regions).")
(make-variable-buffer-local 'specman--ecom-syntax-highlight)
(defalias 'specman-syntax-propertize-function
;; This lambda has been obtained by first trying to get this sorted
;; using `syntax-propertize-rules', then fine-tuning the result.
;; The tricky part is matching ex-code end with the delimiter at the
;; beginning of buffer.
;;
;; (syntax-propertize-rules
;; ;; Make sure all groups are uniquely identified. This macro
;; ;; creates one big mess of all regexps below.
;; ("\\(?:\\(?1:-\\)-\\|\\(?1:/\\)/\\)" (1 "<")) ;; line comment
;; ("\\(?2:\\`<\\)'\\s-*\\(?3:\n\\)" (2 "!") (3 "!")) ;; ex-code begin/end
;; ("\\(?:\\(?2:\\`\\(?:.\\|\n\\)\\)\\|\\(?2:^'\\)>\\)" (2 "!")) ;; ex-code begin
;; ("\\(?:\\(?3:\\(?:.\\|\n\\)\\'\\)\\|^<'\\s-*\\(?3:\n\\)\\)" (3 "!")) ;; ex-code end
;; )
(lambda (start end)
;; the regexp contains references to the beginning of buffer,
;; which is affected by narrowing. Temporarily widen the buffer
;; and adjust START and END accordingly, before applying the
;; regexp search.
(save-restriction
(let ((start start)
(end end))
(when (buffer-narrowed-p)
(widen)
(setq start (+ (point-min) (1- start))
end (+ (point-min) (1- end)))
)
(goto-char start)
(while (and (< (point) end) (re-search-forward
"\\(?2:\\`<\\)'\\s-*\\(?3:
\\)\\|\\(?:\\(?3:\\(?:.\\|
\\)\\'\\)\\|^<'\\s-*\\(?3:
\\)\\)\\|\\(?:\\(?2:\\`\\(?:.\\|
\\)\\)\\|\\(?2:^'\\)>\\)\\|\\(?:\\(?1:-\\)-\\|\\(?1:/\\)/\\)" end t))
;; Group 1 is exclusive, groups 2 and 3 can be matched together.
(if (match-beginning 1)
(put-text-property (match-beginning 1)
(match-end 1)
'syntax-table
'(11))
(when (not specman--ecom-syntax-highlight)
(if (match-beginning 2)
(put-text-property (match-beginning 2)
(match-end 2)
'syntax-table
'(14)))
(if (match-beginning 3)
(put-text-property (match-beginning 3)
(match-end 3)
'syntax-table
'(14)))))
)))))
;; =================================================
;; SPECMAN IMENU FEATURE
;; =================================================
(defun specman-imenu-split-menu (index-alist max-items)
(let ((result-alist
nil)
(sub-alist
nil)
(counter
max-items)
)
;; break the index list into sub-lists in the result list
(while index-alist
(if (> counter 0)
(progn
(setq counter (1- counter))
(push (car index-alist)
sub-alist)
(setq index-alist (cdr index-alist)))
(progn
(setq counter max-items)
(setq sub-alist (reverse sub-alist))
(push (cons (format "From: %s" (caar sub-alist))
sub-alist)
result-alist)
(setq sub-alist nil))))
;; handle what is left in the sub-list
(if result-alist
(progn
(setq sub-alist (reverse sub-alist))
(push (cons (format "From: %s" (caar sub-alist))
sub-alist)
result-alist))
(setq result-alist sub-alist))
(setq index-alist (reverse result-alist))
;; return result
index-alist))
(defun specman-imenu-create-menu-for-region (reg-start reg-end)
;; function accepts a region because it is used recursively
(let ((scope-regexp
specman-container-scope-definition-regexp)
(method-regexp
specman-imenu-method-definition-regexp)
(on-event-regexp
specman-imenu-on-event-method-definition-regexp)
(event-regexp
specman-event-definition-regexp)
(type-regexp
specman-type-definition-regexp)
(cover-regexp
specman-cover-definition-regexp)
(field-regexp
specman-field-definition-regexp)
;; these are used to avoid possible parsing errors on the blocks they start
(keep-soft-select-regexp
"\\(keep[ \t\n]+soft[ \t\n]+[^;]+==[ \t\n]+select[ \t\n]*{\\)")
(gen-keeping-regexp
"gen[ \t\n]*\\([A-Za-z0-9_]+\\)[ \t\n]*keeping[ \t\n]*{")
(index-alist
nil)
(event-alist
nil)
(type-alist
nil)
(cover-alist
nil)
(field-alist
nil)
)
(let ((common-regexp
(concat
"^[ \t]*"
"\\(?:"
"\\(?:"
type-regexp
"\\)\\|\\(?:"
scope-regexp
"\\)\\|\\(?:"
method-regexp
"\\)\\|\\(?:"
on-event-regexp
"\\)\\|\\(?:"
event-regexp
"\\)\\|\\(?:"
cover-regexp
"\\)\\|\\(?:"
field-regexp
"\\)\\|\\(?:"
keep-soft-select-regexp
"\\)\\|\\(?:"
gen-keeping-regexp
"\\)"
"\\)"))
)
;; containers and methods are inserted as they occur into the main list,
;; all other types get their own sub-menus.
(save-excursion
(goto-char reg-start)
(specman-skip-forward-comment-or-string)
(while (specman-re-search-forward common-regexp
reg-end
t
t)
(cond
(;; type
(match-beginning 1)
(push (cons (specman-prepared-buffer-substring (match-beginning 1)
(match-end 1))
(copy-marker (match-beginning 1)))
type-alist)
)
(;; container
(match-beginning 2)
(progn
(save-match-data
(specman-re-search-forward "{"
(point-max)
t
t))
(push (cons (specman-prepared-buffer-substring (match-beginning 2)
(match-end 2))
(cons (cons "== DEFINITION =="
(copy-marker (match-beginning 2)))
(specman-imenu-create-menu-for-region
(point)
(save-excursion
(specman-down-scope)))))
index-alist)
(specman-down-scope)
)
)
(;; method
(match-beginning 3)
(push (cons (concat
(specman-prepared-buffer-substring (match-beginning 3)
(match-end 3))
"()")
(copy-marker (match-beginning 3)))
index-alist)
(specman-down-scope)
)
(;; on event method
(match-beginning 4)
(push (cons (specman-prepared-buffer-substring (match-beginning 4)
(match-end 4))
(copy-marker (match-beginning 4)))
index-alist)
)
(;; event
(match-beginning 5)
(push (cons (specman-prepared-buffer-substring (match-beginning 5)
(match-end 5))
(copy-marker (match-beginning 5)))
event-alist)
)
(;; cover group
(match-beginning 6)
(progn
(push (cons (specman-prepared-buffer-substring (match-beginning 6)
(match-end 6))
(copy-marker (match-beginning 6)))
cover-alist)
;; avoid parsing the cover group
(specman-re-search-forward "{"
(point-max)
t
t)
(specman-down-scope))
)
(;; field
(match-beginning 7)
(push (cons (specman-prepared-buffer-substring (match-beginning 7)
(match-end 7))