-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathverilog-ts-mode.el
2653 lines (2362 loc) · 112 KB
/
verilog-ts-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
;;; verilog-ts-mode.el --- Verilog Tree-sitter major mode -*- lexical-binding: t -*-
;; Copyright (C) 2022-2024 Gonzalo Larumbe
;; Author: Gonzalo Larumbe <gonzalomlarumbe@gmail.com>
;; URL: https://github.com/gmlarumbe/verilog-ts-mode
;; Version: 0.3.0
;; Keywords: Verilog, IDE, Tools
;; Package-Requires: ((emacs "29.1") (verilog-mode "2024.3.1.121933719"))
;; 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. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Major mode to navigate and edit SystemVerilog files with tree-sitter.
;;
;; Provides tree-sitter based implementations for the following features:
;; - Syntax highlighting
;; - Indentation
;; - `imenu'
;; - `which-func'
;; - Navigation functions
;; - Prettify and beautify
;; - Completion at point
;;
;;
;; Contributions:
;; This major mode is still under active development!
;; Check contributing guidelines:
;; - https://github.com/gmlarumbe/verilog-ts-mode#contributing
;;; Code:
;;; Requirements
(require 'treesit)
(require 'imenu)
(require 'verilog-mode)
(declare-function treesit-parser-create "treesit.c")
(declare-function treesit-induce-sparse-tree "treesit.c")
(declare-function treesit-node-parent "treesit.c")
(declare-function treesit-node-start "treesit.c")
(declare-function treesit-node-end "treesit.c")
(declare-function treesit-node-child "treesit.c")
(declare-function treesit-node-child-by-field-name "treesit.c")
(declare-function treesit-node-type "treesit.c")
;;; Customization
(defgroup verilog-ts nil
"SystemVerilog tree-sitter mode."
:group 'languages)
(defcustom verilog-ts-indent-level 4
"Tree-sitter indentation of Verilog statements with respect to containing block."
:group 'verilog-ts
:type 'integer)
(defcustom verilog-ts-file-extension-re "\\.s?vh?\\'"
"(SystemVerilog) file extensions.
Defaults to .v, .vh, .sv and .svh."
:group 'verilog-ts
:type 'string)
(defcustom verilog-ts-imenu-style 'tree
"Style of generated Imenu index for current Verilog buffer.
- Simple: default basic grouping into categories.
- Tree: tree-like structure without further processing.
- Tree-group: tree-like structure with some processing to group same type
elements into subcategories (useful for display with third party
packages such as `imenu-list')."
:type '(choice (const :tag "simple" simple)
(const :tag "tree" tree)
(const :tag "tree-group" tree-group))
:group 'verilog-ts)
(defcustom verilog-ts-which-func-style 'custom
"Style of `which-func' display for current Verilog buffer.
- Simple: show last element of current Imenu entry
- Breadcrumb: display hierarchy of current Imenu entry
- Custom: use custom `verilog-ts-mode' implementation for `which-func':
- Format A:B
- A represents the type of current node, B its name
- For instances, A is the instance name and B the instance type."
:type '(choice (const :tag "simple" simple)
(const :tag "breadcrumb" breadcrumb)
(const :tag "custom" custom))
:group 'verilog-ts)
(defcustom verilog-ts-align-decl-expr-comments t
"Non-nil means align declaration and expressions comments.
Alignment is performed after execution of `verilog-ts-pretty-declarations' and
`verilog-ts-pretty-expr'."
:group 'verilog-ts
:type 'boolean)
(defcustom verilog-ts-beautify-instance-extra nil
"Set to non-nil to perform extra formatting on instances."
:type 'boolean
:group 'verilog-ts)
(defcustom verilog-ts-linter-enable t
"Non-nil means enable tree-sitter based linting."
:group 'verilog-ts
:type 'boolean)
;;; Utils
;;;; Core
(defconst verilog-ts-instance-re "\\_<\\(module\\|interface\\|program\\|gate\\|udp\\|checker\\)_instantiation\\_>")
(defconst verilog-ts-port-header-ts-re "\\_<\\(variable\\|net\\|interface\\)_port_header\\_>")
(defconst verilog-ts-declaration-node-re
(eval-when-compile
(regexp-opt
'("package_declaration"
"class_declaration"
"interface_class_declaration"
"function_body_declaration"
"task_body_declaration"
"function_prototype"
"task_prototype"
"checker_declaration"
"config_declaration")
'symbols)))
(defun verilog-ts--node-at-point (&optional bound)
"Return tree-sitter node at point.
If optional BOUND is non-nil, return nil if point is not over a symbol."
(let* ((pos (point))
(node (treesit-node-at pos 'verilog)))
(if bound
(when (and (>= pos (treesit-node-start node))
(<= pos (treesit-node-end node)))
node)
node)))
(defun verilog-ts--highest-node-at-pos (pos)
"Return highest node starting at POS in the parsed tree.
Return nil if POS is at a whitespace character.
Snippet fetched from `treesit--indent-1'."
(let* ((smallest-node (verilog-ts--node-at-point))
(node (treesit-parent-while
smallest-node
(lambda (node)
(eq pos (treesit-node-start node))))))
node))
(defun verilog-ts--highest-node-at-point (&optional bound)
"Return highest node at point.
If optional BOUND is non-nil, return nil if point is not over a symbol."
(verilog-ts--highest-node-at-pos (treesit-node-start (verilog-ts--node-at-point bound))))
(defun verilog-ts--node-at-bol (&optional skip-comment)
"Return node at first non-blank character of current line.
Snippet fetched from `treesit--indent-1'.
If optional SKIP-COMMENT is non-nil return first node without taking comments
into account."
(let* ((bol (save-excursion
(forward-line 0)
(skip-chars-forward " \t")
(point)))
(smallest-node
(cond ((null (treesit-parser-list)) nil)
((eq 1 (length (treesit-parser-list)))
(treesit-node-at bol))
((treesit-language-at (point))
(treesit-node-at bol (treesit-language-at (point))))
(t (treesit-node-at bol))))
(node (treesit-parent-while
smallest-node
(lambda (node)
(eq bol (treesit-node-start node))))))
(if (and skip-comment
(string= (treesit-node-type node) "comment")
(> (line-end-position) (treesit-node-end node)))
(progn
(goto-char (treesit-node-end node))
(skip-chars-forward " \t")
(verilog-ts--highest-node-at-pos (point)))
node)))
(defun verilog-ts--node-has-parent-recursive (node node-type)
"Return non-nil if NODE is part of NODE-TYPE in the parsed tree."
(treesit-parent-until
node
(lambda (node) ; Third argument must be a function
(string-match node-type (treesit-node-type node)))))
(defun verilog-ts--node-has-child-recursive (node node-type)
"Return first node of type NODE-TYPE that is a child of NODE in the parsed tree.
If none is found, return nil."
(when node
(treesit-search-subtree node node-type nil :all)))
(defun verilog-ts--node-parents-list (node node-type)
"Return NODE parents that match NODE-TYPE as a list of nodes."
(let (parent parent-list)
(while (setq parent (verilog-ts--node-has-parent-recursive node node-type))
(push parent parent-list)
(setq node parent))
(nreverse parent-list)))
(defun verilog-ts--node-identifier-name (node)
"Return identifier name of NODE."
(when node
(cond (;; module/interface/program
(string-match "\\_<\\(module\\|interface\\|program\\)_declaration\\_>" (treesit-node-type node))
(or (treesit-node-text (treesit-node-child-by-field-name node "name") :no-props) ; extern module instantiation
(treesit-node-text (treesit-node-child-by-field-name (treesit-search-subtree node "\\_<module_\\(non\\)?ansi_header") "name") :no-props)))
;; package/class/function/task/checker/config
((string-match verilog-ts-declaration-node-re (treesit-node-type node))
(treesit-node-text (treesit-node-child-by-field-name node "name") :no-props))
;; class constructor
((string-match "\\_<class_constructor_\\(prototype\\|declaration\\)\\_>" (treesit-node-type node))
"new")
;; typedefs
((string-match "\\_<type_declaration\\_>" (treesit-node-type node))
(let* ((named-nodes (treesit-node-children node :named))
(type-node (car (seq-filter (lambda (named-node)
(string= (treesit-node-field-name named-node) "type_name"))
named-nodes))))
(treesit-node-text type-node :no-prop)))
;; function/task declaration (body/prototype in previous case)
((string-match "\\_<\\(task\\|function\\)_declaration\\_>" (treesit-node-type node))
(verilog-ts--node-identifier-name (treesit-search-subtree node "\\_<\\(task\\|function\\)_body_declaration\\_>")))
;; class property
((string-match "\\_<class_property\\_>" (treesit-node-type node))
(or (treesit-node-text (treesit-node-child-by-field-name (treesit-search-subtree node "\\_<variable_decl_assignment\\_>") "name") :no-props)
(verilog-ts--node-identifier-name (treesit-search-subtree node "\\_<type_declaration\\_>"))))
;; instances
((string-match verilog-ts-instance-re (treesit-node-type node))
(treesit-node-text (treesit-node-child-by-field-name node "instance_type") :no-props))
;; ports
((string-match "\\_<tf_port_item\\_>" (treesit-node-type node))
(treesit-node-text (treesit-node-child-by-field-name node "name") :no-props))
((string-match "\\_<ansi_port_declaration\\_>" (treesit-node-type node))
(treesit-node-text (treesit-node-child-by-field-name node "port_name") :no-props))
;; parameter/localparam
((string-match "\\_<\\(local_\\)?parameter_declaration\\_>" (treesit-node-type node))
(let* ((param-name-node (treesit-search-subtree node "\\_<\\(param\\|type\\)_assignment\\_>"))
(node-identifier (treesit-search-subtree param-name-node "\\_<simple_identifier\\_>")))
(treesit-node-text node-identifier :no-prop)))
;; default
(t
(treesit-node-text (treesit-search-subtree node "\\_<simple_identifier\\_>") :no-prop)))))
(defun verilog-ts--node-identifier-type (node)
"Return identifier type of NODE."
(let ((type (treesit-node-type node)))
(cond (;; Variables
(string-match "\\_<variable_decl_assignment\\_>" type)
(let* ((start-node (or (verilog-ts--node-has-parent-recursive node "\\_<class_property\\_>") ; Place it before to get qualifiers in case it's a property
(verilog-ts--node-has-parent-recursive node "\\_<data_declaration\\_>")))
(end-node (treesit-search-subtree start-node "\\_<variable_decl_assignment\\_>"))) ; Get first declaration of a list of variables to make it more generic
(string-trim-right (buffer-substring-no-properties (treesit-node-start start-node) (treesit-node-start end-node)))))
(;; Nets
(string-match "\\_<net_decl_assignment\\_>" type)
(treesit-node-text (verilog-ts--node-has-child-recursive (verilog-ts--node-has-parent-recursive node "\\_<net_declaration\\_>") "\\_<\\(net_type\\|simple_identifier\\)\\_>") :no-prop))
(;; Module/interface/program ports
(string-match "\\_<ansi_port_declaration\\_>" type)
(treesit-node-text (treesit-search-subtree node verilog-ts-port-header-ts-re) :no-prop))
(;; Task/function arguments
(string-match "\\_<tf_port_item\\_>" type)
(let ((port-direction (treesit-node-text (treesit-search-subtree node "\\_<tf_port_direction\\_>") :no-prop)))
(concat (when port-direction (concat port-direction " "))
(treesit-node-text (treesit-search-subtree node "\\_<data_type_or_implicit\\_>") :no-prop))))
(;; Typedefs
(string-match "\\_<type_declaration\\_>" type)
(treesit-node-text (verilog-ts--node-has-child-recursive (verilog-ts--node-has-parent-recursive node "\\_<data_declaration\\_>") "\\_<\\(class\\|data\\)_type\\_>") :no-prop))
(t ;; Default
type))))
(defun verilog-ts--node-instance-name (node)
"Return identifier name of NODE.
Node must be of type `verilog-ts-instance-re'. Otherwise return nil."
(unless (and node (string-match verilog-ts-instance-re (treesit-node-type node)))
(error "Wrong node type: %s" (treesit-node-type node)))
(let ((child-node (treesit-search-subtree node "name_of_instance")))
(treesit-node-text (treesit-node-child-by-field-name child-node "instance_name") :no-props)))
(defun verilog-ts--inside-module-or-interface-p ()
"Return non-nil if point is inside a module or interface construct."
(verilog-ts--node-has-parent-recursive (verilog-ts--node-at-point) "\\_<\\(module\\|interface\\)_declaration\\_>"))
(defun verilog-ts--node-is-typedef-class (node)
"Return declared class name if NODE is a typedef class declaration."
(let ((type (treesit-node-type node)))
(when (and node
(string-match "\\_<type_declaration\\_>" type)
(string= (treesit-node-text (treesit-node-child node 0) :no-prop) "typedef")
(string= (treesit-node-text (treesit-node-child node 1) :no-prop) "class"))
(treesit-node-text (treesit-node-child-by-field-name node "type_name") :no-prop))))
;;;; Context
(defconst verilog-ts-block-at-point-re
(eval-when-compile
(regexp-opt
'("module_declaration"
"interface_declaration"
"program_declaration"
"package_declaration"
"class_declaration"
"function_declaration"
"task_declaration"
"class_constructor_declaration"
"function_prototype"
"task_prototype"
"class_constructor_prototype"
"module_instantiation"
"interface_instantiation"
"always_construct"
"initial_construct"
"final_construct"
"generate_region"
"seq_block")
'symbols)))
(defun verilog-ts-nodes (pred &optional start)
"Return current buffer NODES that match PRED.
If optional arg START is non-nil, use it as the initial node to search in the
tree."
(let ((root-node (or start (treesit-buffer-root-node))))
(mapcar #'car (cdr (treesit-induce-sparse-tree root-node pred)))))
(defun verilog-ts-nodes-props (pred &optional start)
"Return nodes and properties that satisfy PRED in current buffer.
If optional arg START is non-nil, use it as the initial node to search in the
tree.
Returned properties are a property list that include node name, start position
and end position."
(mapcar (lambda (node)
`(,node :name ,(verilog-ts--node-identifier-name node)
:start-pos ,(treesit-node-start node)
:end-pos ,(treesit-node-end node)))
(verilog-ts-nodes pred start)))
(defun verilog-ts-module-at-point ()
"Return node of module at point."
(let ((module (verilog-ts--node-has-parent-recursive (verilog-ts--node-at-point) "module_declaration"))
(pos (point)))
(when (and module
(>= pos (treesit-node-start module))
(<= pos (treesit-node-end module)))
module)))
(defun verilog-ts-instance-at-point ()
"Return node of instance at point."
(let ((instance (verilog-ts--node-has-parent-recursive (verilog-ts--node-at-point) verilog-ts-instance-re))
(pos (point)))
(when (and instance
(>= pos (treesit-node-start instance))
(<= pos (treesit-node-end instance)))
instance)))
(defun verilog-ts--block-at-point (regex)
"Return deepest node of block at point that matches REGEX."
(let ((blocks (verilog-ts--node-parents-list (verilog-ts--node-at-point) regex))
(pos (point)))
(catch 'block
(mapc (lambda (block)
(when (and block
(>= pos (treesit-node-start block))
(<= pos (treesit-node-end block)))
(throw 'block block)))
blocks)
nil)))
(defun verilog-ts-block-at-point ()
"Return node of block at point."
(verilog-ts--block-at-point verilog-ts-block-at-point-re))
(defun verilog-ts-nodes-block-at-point (pred)
"Return block at point NODES that match PRED."
(let ((block (verilog-ts-block-at-point)))
(when block
(mapcar #'car (cdr (treesit-induce-sparse-tree block pred))))))
(defun verilog-ts-children-block-at-point (pred)
"Return block at point children NODES that match PRED."
(let ((block (verilog-ts-block-at-point)))
(when block
(mapcar #'car (cdr (treesit-filter-child block pred))))))
(defun verilog-ts-search-node-block-at-point (pred &optional backward all)
"Search forward for node matching PRED inside block at point.
By default, only search for named nodes, but if ALL is non-nil, search
for all nodes. If BACKWARD is non-nil, search backwards."
(treesit-search-forward (verilog-ts--node-at-point)
(lambda (node)
(and (string-match pred (treesit-node-type node))
(< (treesit-node-end node) (treesit-node-end (verilog-ts-block-at-point)))))
backward
all))
;; Some examples using previous API
(defun verilog-ts-module-declarations-nodes-current-buffer ()
"Return module declaration nodes of current file."
(verilog-ts-nodes "module_declaration"))
(defun verilog-ts-module-declarations-current-buffer ()
"Return module declaration names of current file."
(mapcar (lambda (node-and-props)
(plist-get (cdr node-and-props) :name))
(verilog-ts-nodes-props "module_declaration")))
(defun verilog-ts-module-instances-nodes (module-node)
"Return instance nodes of MODULE-NODE."
(unless (and module-node (string= "module_declaration" (treesit-node-type module-node)))
(error "Wrong module-node: %s" module-node))
(verilog-ts-nodes verilog-ts-instance-re module-node))
(defun verilog-ts-module-instances (module-node)
"Return instances of MODULE-NODE."
(unless (and module-node (string= "module_declaration" (treesit-node-type module-node)))
(error "Wrong module-node: %s" module-node))
(mapcar (lambda (node-and-props)
(plist-get (cdr node-and-props) :name))
(verilog-ts-nodes-props verilog-ts-instance-re module-node)))
(defun verilog-ts-module-always-blocks (module-node)
"Return always blocks of MODULE-NODE."
(unless (and module-node (string= "module_declaration" (treesit-node-type module-node)))
(error "Wrong module-node: %s" module-node))
(mapcar (lambda (node-and-props)
(plist-get (cdr node-and-props) :name))
(verilog-ts-nodes-props "always_keyword" module-node)))
(defun verilog-ts-class-properties (class-node)
"Return properties of CLASS-NODE."
(unless (and class-node (string= "class_declaration" (treesit-node-type class-node)))
(error "Wrong class-node: %s" class-node))
(mapcar (lambda (node-and-props)
(plist-get (cdr node-and-props) :name))
(verilog-ts-nodes-props "class_property" class-node)))
(defun verilog-ts-class-methods (class-node)
"Return methods of CLASS-NODE."
(unless (and class-node (string= "class_declaration" (treesit-node-type class-node)))
(error "Wrong class-node: %s" class-node))
(mapcar (lambda (node-and-props)
(plist-get (cdr node-and-props) :name))
(verilog-ts-nodes-props "class_\\(constructor\\|method\\)" class-node)))
(defun verilog-ts-class-constraints (class-node)
"Return constraints of CLASS-NODE."
(unless (and class-node (string= "class_declaration" (treesit-node-type class-node)))
(error "Wrong class-node: %s" class-node))
(mapcar (lambda (node-and-props)
(plist-get (cdr node-and-props) :name))
(verilog-ts-nodes-props "constraint_declaration" class-node)))
;;; Font-lock
;;;; Faces
(defgroup verilog-ts-faces nil
"Verilog-ts faces."
:group 'verilog-ts)
(defvar verilog-ts-font-lock-grouping-keywords-face 'verilog-ts-font-lock-grouping-keywords-face)
(defface verilog-ts-font-lock-grouping-keywords-face
'((t (:inherit font-lock-misc-punctuation-face)))
"Face for grouping keywords: begin, end."
:group 'verilog-ts-faces)
(defvar verilog-ts-font-lock-punctuation-face 'verilog-ts-font-lock-punctuation-face)
(defface verilog-ts-font-lock-punctuation-face
'((t (:inherit font-lock-punctuation-face)))
"Face for punctuation symbols, e.g:
!,;:?'=<>*"
:group 'verilog-ts-faces)
(defvar verilog-ts-font-lock-operator-face 'verilog-ts-font-lock-operator-face)
(defface verilog-ts-font-lock-operator-face
'((t (:inherit font-lock-operator-face)))
"Face for operator symbols, such as &^~+-/|."
:group 'verilog-ts-faces)
(defvar verilog-ts-font-lock-brackets-face 'verilog-ts-font-lock-brackets-face)
(defface verilog-ts-font-lock-brackets-face
'((t (:inherit font-lock-bracket-face)))
"Face for brackets []."
:group 'verilog-ts-faces)
(defvar verilog-ts-font-lock-parenthesis-face 'verilog-ts-font-lock-parenthesis-face)
(defface verilog-ts-font-lock-parenthesis-face
'((t (:inherit font-lock-bracket-face)))
"Face for parenthesis ()."
:group 'verilog-ts-faces)
(defvar verilog-ts-font-lock-curly-braces-face 'verilog-ts-font-lock-curly-braces-face)
(defface verilog-ts-font-lock-curly-braces-face
'((t (:inherit font-lock-bracket-face)))
"Face for curly braces {}."
:group 'verilog-ts-faces)
(defvar verilog-ts-font-lock-port-connection-face 'verilog-ts-font-lock-port-connection-face)
(defface verilog-ts-font-lock-port-connection-face
'((t (:inherit font-lock-constant-face)))
"Face for port connections of instances.
.portA (signalA),
.portB (signalB)
);"
:group 'verilog-ts-faces)
(defvar verilog-ts-font-lock-dot-name-face 'verilog-ts-font-lock-dot-name-face)
(defface verilog-ts-font-lock-dot-name-face
'((t (:inherit font-lock-property-name-face)))
"Face for dot-name regexps:
- Interface signals, classes attributes/methods and hierarchical refs.
axi_if.Ready <= 1'b1;
obj.method();"
:group 'verilog-ts-faces)
(defvar verilog-ts-font-lock-brackets-content-face 'verilog-ts-font-lock-brackets-content-face)
(defface verilog-ts-font-lock-brackets-content-face
'((t (:inherit font-lock-number-face)))
"Face for content between brackets: arrays, bit vector width and indexing."
:group 'verilog-ts-faces)
(defvar verilog-ts-font-lock-width-num-face 'verilog-ts-font-lock-width-num-face)
(defface verilog-ts-font-lock-width-num-face
'((t (:inherit font-lock-number-face)))
"Face for the bit width number expressions.
{1}'b1,
{4}'hF,
{3}'o7,"
:group 'verilog-ts-faces)
(defvar verilog-ts-font-lock-width-type-face 'verilog-ts-font-lock-width-type-face)
(defface verilog-ts-font-lock-width-type-face
'((t (:inherit font-lock-builtin-face)))
"Face for the bit width type expressions.
1'{b}1,
4'{h}F,
3'{o}7,"
:group 'verilog-ts-faces)
(defvar verilog-ts-font-lock-module-face 'verilog-ts-font-lock-module-face)
(defface verilog-ts-font-lock-module-face
'((t (:inherit font-lock-function-call-face)))
"Face for module names."
:group 'verilog-ts-faces)
(defvar verilog-ts-font-lock-instance-face 'verilog-ts-font-lock-instance-face)
(defface verilog-ts-font-lock-instance-face
'((t (:inherit font-lock-variable-use-face)))
"Face for instance names."
:group 'verilog-ts-faces)
(defvar verilog-ts-font-lock-time-event-face 'verilog-ts-font-lock-time-event-face)
(defface verilog-ts-font-lock-time-event-face
'((t (:inherit font-lock-property-name-face)))
"Face for time-events: @ and #."
:group 'verilog-ts-faces)
(defvar verilog-ts-font-lock-time-unit-face 'verilog-ts-font-lock-time-unit-face)
(defface verilog-ts-font-lock-time-unit-face
'((t (:inherit font-lock-property-use-face)))
"Face for time-units: ms, us, ns, ps, fs (delays and timescale/timeprecision)."
:group 'verilog-ts-faces)
(defvar verilog-ts-font-lock-preprocessor-face 'verilog-ts-font-lock-preprocessor-face)
(defface verilog-ts-font-lock-preprocessor-face
'((t (:inherit font-lock-preprocessor-face)))
"Face for preprocessor compiler directives (`include, `define, UVM macros...)."
:group 'verilog-ts-faces)
(defvar verilog-ts-font-lock-modport-face 'verilog-ts-font-lock-modport-face)
(defface verilog-ts-font-lock-modport-face
'((t (:inherit font-lock-type-face)))
"Face for interface modports."
:group 'verilog-ts-faces)
(defvar verilog-ts-font-lock-direction-face 'verilog-ts-font-lock-direction-face)
(defface verilog-ts-font-lock-direction-face
'((t (:inherit font-lock-keyword-face)))
"Face for direction of ports/functions/tasks args."
:group 'verilog-ts-faces)
(defvar verilog-ts-font-lock-translate-off-face 'verilog-ts-font-lock-translate-off-face)
(defface verilog-ts-font-lock-translate-off-face
'((t (:slant italic)))
"Face for pragmas between comments, e.g:
* translate_off / * translate_on"
:group 'verilog-ts-faces)
(defvar verilog-ts-font-lock-attribute-face 'verilog-ts-font-lock-attribute-face)
(defface verilog-ts-font-lock-attribute-face
'((t (:inherit font-lock-property-name-face)))
"Face for RTL attributes."
:group 'verilog-ts-faces)
;;;; Keywords
;; There are some keywords that are not recognized by tree-sitter grammar.
;; For these ones, use regexp matching patterns inside tree-sitter (:match "^foo$")
(defconst verilog-ts-keywords
'("alias" "and" "assert" "assign" "assume" "before" "bind" "binsof" "break"
"case" "checker" "class" "class" "clocking" "config" "const" "constraint"
"cover" "covergroup" "coverpoint" "cross" "default" "defparam" "disable"
"do" "else" "endcase" "endchecker" "endclass" "endclocking" "endconfig"
"endfunction" "endgenerate" "endgroup" "endinterface" "endmodule"
"endpackage" "endprogram" "endproperty" "endsequence" "endtask" "enum"
"extends" "extern" "final" "first_match" "for" "foreach" "forever" "fork"
"forkjoin" "function" "generate" "genvar" "if" "iff" "illegal_bins"
"implements" "import" "initial" "inside" "interconnect" "interface"
"intersect" "join" "join_any" "join_none" "local" "localparam" "modport"
"new" "null" "option" "or" "package" "packed" "parameter" "program"
"property" "pure" "randcase" "randomize" "repeat" "return" "sequence"
"showcancelled" "soft" "solve" "struct" "super" "tagged" "task"
"timeprecision" "timeunit" "type" "typedef" "union" "unique" "virtual"
"wait" "while" "with"
(always_keyword) ; always, always_comb, always_latch, always_ff
(bins_keyword) ; bins, illegal_bins, ignore_bins
(case_keyword) ; case, casez, casex
(class_item_qualifier) ; static, protected, local
(edge_identifier) ; posedge, negedge, edge
(lifetime) ; static, automatic
(module_keyword) ; module, macromodule
(random_qualifier) ; rand, randc
(unique_priority))) ; unique, unique0, priority
(defconst verilog-ts-operators-arithmetic
'("+" "-" "*" "/" "%" "**"))
(defconst verilog-ts-operators-relational
'("<" "<=" ">" ">="))
(defconst verilog-ts-operators-equality
'("===" "!==" "==" "!="))
(defconst verilog-ts-operators-logical
'("&&" "||" "!"))
(defconst verilog-ts-operators-bitwise
'("~" "&" "~&" "|" "~|" "^" "~^"))
(defconst verilog-ts-operators-shift
'("<<" ">>" "<<<" ">>>"))
(defconst verilog-ts-punctuation
'(";" ":" "," "::"
"=" "?" "|=" "&=" "^="
"|->" "|=>" "->"
":=" ":/" "-:" "+:"))
(defconst verilog-ts-directives
(eval-when-compile
(mapcar (lambda (elm)
(concat "directive_" elm))
'("include" "define" "ifdef" "ifndef" "timescale" "default_nettype"
"elsif" "undef" "resetall" "undefineall" "endif" "else"
"unconnected_drive" "celldefine" "endcelldefine" "end_keywords"
"unconnected_drive" "line" "begin_keywords" "pragma" "__FILE__"
"__LINE__"))))
;;;; Functions
(defun verilog-ts--fontify-error (node _override start end &rest _)
"Fontify a syntax error with a red wavy underline.
For NODE,OVERRIDE, START, END, and ARGS, see `treesit-font-lock-rules'."
(when verilog-ts-linter-enable
(treesit-fontify-with-override (treesit-node-start node)
(treesit-node-end node)
'(:underline (:style wave :color "Red1"))
'append
start end)))
;;;; Treesit-settings
(defvar verilog-ts--font-lock-settings
(treesit-font-lock-rules
:feature 'comment
:language 'verilog
'((comment) @font-lock-comment-face)
:feature 'string
:language 'verilog
'([(string_literal)
(quoted_string) ; `include strings
(system_lib_string)]
@font-lock-string-face)
:feature 'keyword
:language 'verilog
`((["begin" "end" "this"] @verilog-ts-font-lock-grouping-keywords-face)
(["input" "output" "inout" "ref"] @verilog-ts-font-lock-direction-face)
([,@verilog-ts-keywords] @font-lock-keyword-face))
:feature 'preprocessor
:language 'verilog
`(([,@verilog-ts-directives] @verilog-ts-font-lock-preprocessor-face)
(text_macro_usage
(simple_identifier) @verilog-ts-font-lock-preprocessor-face))
:feature 'punctuation
:language 'verilog
`(([,@verilog-ts-punctuation] @verilog-ts-font-lock-punctuation-face)
(["."] @verilog-ts-font-lock-operator-face)
(["(" ")"] @verilog-ts-font-lock-parenthesis-face)
(["[" "]"] @verilog-ts-font-lock-brackets-face)
(["{" "}" "'{"] @verilog-ts-font-lock-curly-braces-face)
(["@" "#" "##"] @verilog-ts-font-lock-time-event-face))
:feature 'operator
:language 'verilog
`(;; INFO: Some of these might be redundant
([,@verilog-ts-operators-arithmetic] @verilog-ts-font-lock-operator-face)
([,@verilog-ts-operators-relational] @verilog-ts-font-lock-punctuation-face)
([,@verilog-ts-operators-equality] @verilog-ts-font-lock-punctuation-face)
([,@verilog-ts-operators-shift] @verilog-ts-font-lock-punctuation-face)
([,@verilog-ts-operators-bitwise] @verilog-ts-font-lock-operator-face)
([,@verilog-ts-operators-logical] @verilog-ts-font-lock-operator-face)
;; Operators (A.8.6):
((assignment_operator) @verilog-ts-font-lock-punctuation-face)
((unary_operator) @verilog-ts-font-lock-operator-face)
;; ((binary_operator) @verilog-ts-font-lock-operator-face) ; INFO: Unused in the grammar, left as a reference
((inc_or_dec_operator) @verilog-ts-font-lock-operator-face)
((stream_operator) @verilog-ts-font-lock-operator-face)
((event_trigger (["->" "->>"]) @verilog-ts-font-lock-operator-face)))
:feature 'declaration
:language 'verilog
'(;; Module/interface/program/package/class/checker
(module_nonansi_header
name: (simple_identifier) @font-lock-function-name-face)
(module_ansi_header
name: (simple_identifier) @font-lock-function-name-face)
(interface_nonansi_header
name: (simple_identifier) @font-lock-function-name-face)
(interface_ansi_header
name: (simple_identifier) @font-lock-function-name-face)
(program_nonansi_header
name: (simple_identifier) @font-lock-function-name-face)
(program_ansi_header
name: (simple_identifier) @font-lock-function-name-face)
(package_declaration
name: (simple_identifier) @font-lock-function-name-face)
(class_declaration
name: (simple_identifier) @font-lock-function-name-face)
(interface_class_declaration
name: (simple_identifier) @font-lock-function-name-face)
(checker_declaration
name: (simple_identifier) @font-lock-function-name-face)
(class_declaration
(class_type
(simple_identifier) @font-lock-type-face)) ; Parent class
;; Function/task/methods
(function_body_declaration
name: (simple_identifier) @font-lock-function-name-face)
(task_body_declaration
name: (simple_identifier) @font-lock-function-name-face)
(function_prototype
(data_type_or_void)
name: (simple_identifier) @font-lock-function-name-face)
(task_prototype
name: (simple_identifier) @font-lock-function-name-face)
(class_scope ; Definition of extern defined methods
(class_type
(simple_identifier) @verilog-ts-font-lock-dot-name-face)))
:feature 'type
:language 'verilog
`(([(integer_vector_type) ; bit, logic, reg
(integer_atom_type) ; byte, shortint, int, longint, integer, time
(non_integer_type) ; shortreal, real, realtime
(net_type) ; supply0, supply1, tri, triand, trior, trireg, tri0, tri1, uwire, wire, wand, wor
["string" "event" "signed" "unsigned" "chandle"]]
@font-lock-type-face)
(data_type_or_implicit
(data_type
(simple_identifier) @font-lock-type-face))
(data_type
(class_type
(simple_identifier) @font-lock-type-face))
(net_port_header ; port with user net type
(net_port_type
(simple_identifier) @font-lock-type-face))
(variable_port_header ; port with user variable type
(variable_port_type
(data_type
(simple_identifier) @font-lock-type-face)))
(["void'" (data_type_or_void)] @font-lock-type-face) ; void cast of task called as a function
(interface_port_header ; Interfaces with modports
interface_name: (simple_identifier) @verilog-ts-font-lock-dot-name-face
modport_name: (simple_identifier) @verilog-ts-font-lock-modport-face)
(type_assignment
name: (simple_identifier) @font-lock-type-face)
(net_declaration ; User type variable declaration
(simple_identifier) @font-lock-type-face)
(enum_base_type ; Enum base type with user type
(simple_identifier) @font-lock-type-face))
:feature 'instance
:language 'verilog
'(;; Module names
(module_instantiation
instance_type: (simple_identifier) @verilog-ts-font-lock-module-face)
(interface_instantiation
instance_type: (simple_identifier) @verilog-ts-font-lock-module-face)
(program_instantiation
instance_type: (simple_identifier) @verilog-ts-font-lock-module-face)
(checker_instantiation
instance_type: (simple_identifier) @verilog-ts-font-lock-module-face)
(udp_instantiation ; INFO: UDPs: Adding field to udp_identifier is still buggy in the grammar
(simple_identifier) @verilog-ts-font-lock-module-face)
(gate_instantiation
[(cmos_switchtype)
(mos_switchtype)
(enable_gatetype)
(n_input_gatetype)
(n_output_gatetype)
(pass_en_switchtype)
(pass_switchtype)
"pulldown" "pullup"]
@verilog-ts-font-lock-module-face)
;; Instance names
(name_of_instance
instance_name: (simple_identifier) @verilog-ts-font-lock-instance-face)
;; Instance parameters
(module_instantiation ; Assume module_instantiation has higher dynamic precedence than other instantiations
(parameter_value_assignment
(list_of_parameter_value_assignments
(named_parameter_assignment
(simple_identifier) @verilog-ts-font-lock-port-connection-face))))
(module_instantiation ; Assume module_instantiation has higher dynamic precedence than other instantiations
(parameter_value_assignment
(list_of_parameter_value_assignments
(ordered_parameter_assignment
(param_expression
(data_type
(simple_identifier) @verilog-ts-font-lock-port-connection-face))))))
;; Port names
(named_port_connection
port_name: (simple_identifier) @verilog-ts-font-lock-port-connection-face)
(named_parameter_assignment
(simple_identifier) @verilog-ts-font-lock-port-connection-face)
(named_checker_port_connection
port_name: (simple_identifier) @verilog-ts-font-lock-port-connection-face)
;; Bind statements
(bind_directive
(bind_target_scope
(simple_identifier) @font-lock-type-face)))
:feature 'number
:language 'verilog
'((hex_number
size: (unsigned_number) @verilog-ts-font-lock-width-num-face
base: (hex_base) @verilog-ts-font-lock-width-type-face)
(decimal_number
size: (unsigned_number) @verilog-ts-font-lock-width-num-face
base: (decimal_base) @verilog-ts-font-lock-width-type-face)
(octal_number
size: (unsigned_number) @verilog-ts-font-lock-width-num-face
base: (octal_base) @verilog-ts-font-lock-width-type-face)
(binary_number
size: (unsigned_number) @verilog-ts-font-lock-width-num-face
base: (binary_base) @verilog-ts-font-lock-width-type-face)
;; Same as before but without the width (width extension)
(hex_number
base: (hex_base) @verilog-ts-font-lock-width-type-face)
(decimal_number
base: (decimal_base) @verilog-ts-font-lock-width-type-face)
(octal_number
base: (octal_base) @verilog-ts-font-lock-width-type-face)
(binary_number
base: (binary_base) @verilog-ts-font-lock-width-type-face))
:feature 'array
:language 'verilog
:override t
'((unpacked_dimension
[(constant_expression) (constant_range)] @verilog-ts-font-lock-brackets-content-face)
(packed_dimension
(constant_range) @verilog-ts-font-lock-brackets-content-face)
(select
(constant_range) @verilog-ts-font-lock-brackets-content-face)
(constant_select
(constant_range
(constant_expression) @verilog-ts-font-lock-brackets-content-face))
(constant_bit_select
(constant_expression) @verilog-ts-font-lock-brackets-content-face)
(bit_select
(expression) @verilog-ts-font-lock-brackets-content-face)
(indexed_range
(expression) @verilog-ts-font-lock-brackets-content-face
(constant_expression) @verilog-ts-font-lock-brackets-content-face)
(constant_indexed_range
(constant_expression) @verilog-ts-font-lock-brackets-content-face)
(value_range ; inside {[min_range:max_range]}, place here to apply override
(expression) @font-lock-constant-face)
(dynamic_array_new
(expression) @font-lock-constant-face))
:feature 'misc
:language 'verilog
'(;; Timeunit
((time_unit) @font-lock-constant-face)
;; Enum labels
(enum_name_declaration
(simple_identifier) @font-lock-constant-face)
;; Case item label (not radix)
(case_item_expression
(expression
(primary
(hierarchical_identifier
(simple_identifier) @font-lock-constant-face))))
;; Hierarchical references, interface signals, class members, package scope
(hierarchical_identifier
(simple_identifier) @verilog-ts-font-lock-dot-name-face
"."
(simple_identifier))
(method_call
(primary) @verilog-ts-font-lock-dot-name-face
(["." "::"])
(method_call_body))
(package_scope
(simple_identifier) @verilog-ts-font-lock-dot-name-face)
(method_call
(select "." (simple_identifier) @verilog-ts-font-lock-dot-name-face))
;; Attributes
(["(*" "*)"] @font-lock-constant-face)
(attribute_instance
(attr_spec (simple_identifier) @verilog-ts-font-lock-attribute-face))
;; Typedefs
(type_declaration
(class_type (simple_identifier) @font-lock-type-face)
type_name: (simple_identifier) @font-lock-constant-face)
(type_declaration
type_name: (simple_identifier) @font-lock-constant-face)
("typedef" "class" (simple_identifier) @font-lock-constant-face)
;; Coverpoint & cross labels
(cover_point
name: (simple_identifier) @font-lock-constant-face)
(cover_cross
name: (simple_identifier) @font-lock-constant-face)
;; Loop variables (foreach[i])
(loop_variables
(simple_identifier) @font-lock-constant-face)
;; Bins values
(bins_or_options
(expression
(primary
(concatenation
(expression) @font-lock-constant-face))))
;; Bins ranges
(covergroup_value_range
(expression) @font-lock-constant-face)
;; Queue dimension
(("$") @font-lock-constant-face)
;; Parameterized classes (e.g: uvm_config_db #(axi_stream_agent_config))
(class_type
(parameter_value_assignment
(list_of_parameter_value_assignments) @verilog-ts-font-lock-dot-name-face)))
:feature 'system-tf
:language 'verilog
:override t ; Highlight system_tf calls inside array ranges
'([(system_tf_identifier) ; System task/function
"$fatal" "$error" "$warning" "$info" ; (severity_system_task)
"$stop" "$finish" "$exit"] ; (simulation_control_task)
@font-lock-builtin-face)
:feature 'error
:language 'verilog
:override t
'((ERROR) @verilog-ts--fontify-error)))
;;; Indent
;;;; Matchers
(defun verilog-ts--matcher-unit-scope (node parent &rest _)
"A tree-sitter simple indent matcher for NODE and PARENT.