summaryrefslogtreecommitdiff
path: root/autoload/filetype/vhdl.kak
blob: 71aa7ed85136f267bded8ed86ad6a6702f06c065 (plain)
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
# Based on IEEE Std 1076‐2019

# Detection
hook global BufCreate .*[.](vhd[l]?) %[
    set-option buffer filetype vhdl
]

# Initialization
hook global WinSetOption filetype=vhdl %[
    require-module vhdl
    set-option window static_words %opt{vhdl_static_words}
    hook -group vhdl-indent window InsertChar \n vhdl-indent-on-new-line
    hook -group vhdl-indent window InsertChar \) vhdl-indent-on-closing-parenthesis
    hook -group vhdl-insert window InsertChar \n vhdl-insert-on-new-line
    # Cleanup trailing whitespaces on current line insert end.
    hook -group vhdl-trim-indent window ModeChange pop:insert:.* %[ try %[ execute-keys -draft <semicolon> x s ^\h+$ <ret> d ] ]
    hook -once -always window WinSetOption filetype=.* %[ remove-hooks window vhdl-.+ ]
]

hook -group vhdl-highlight global WinSetOption filetype=vhdl %[
    add-highlighter window/vhdl ref vhdl
    hook -once -always window WinSetOption filetype=.* %[ remove-highlighter window/vhdl ]
]

provide-module vhdl %§

# Highlighters & Completion
add-highlighter shared/vhdl regions
add-highlighter shared/vhdl/code default-region group
add-highlighter shared/vhdl/comment_line region '--' $ fill comment
add-highlighter shared/vhdl/comment region /\* \*/ fill comment

# Integer formats
add-highlighter shared/vhdl/code/ regex '(?i)\b0b[01]+l?\b' 0:value
add-highlighter shared/vhdl/code/ regex '(?i)\b0x[\da-f]+l?\b' 0:value
add-highlighter shared/vhdl/code/ regex '(?i)\b0o?[0-7]+l?\b' 0:value
add-highlighter shared/vhdl/code/ regex '(?i)\b([1-9]\d*|0)l?\b' 0:value
# Float formats
add-highlighter shared/vhdl/code/ regex '\b\d+[eE][+-]?\d+\b' 0:value
add-highlighter shared/vhdl/code/ regex '(\b\d+)?\.\d+\b' 0:value
add-highlighter shared/vhdl/code/ regex '\b\d+\.' 0:value
# Imaginary formats
add-highlighter shared/vhdl/code/ regex '\b\d+\+\d+[jJ]\b' 0:value

evaluate-commands %sh[
    values="true false note warning error failure nul"

    # LRM 5.2.4.1
    units="fs ps ns us ms sec min hr Å nm um mm cm m km"

    # LRM 16.2
    predefined_attributes="
        base left right high low ascending length range reverse_range
        subtype image pos succ pred leftof rightof value val
        designated_subtype reflect high low index element delayed
        stable quiet transaction event active last_event last_active
        last_value driving driving_value simple_name instance_name
        path_name record signal converse
    "

    libraries="ieee std work"

    packages="
        math_real math_complex std_logic_1164 std_logic_textio numeric_bit numeric_std
        numeric_bit_unsigned numeric_std_unsigned fixed_float_types fixed_generic_pkg
        fixed_pkg float_generic_pkg float_pkg
        standard textio env
    "

     # LRM 15.10
    reserved_words="
        abs access after alias all and architecture array assert assume assume_guarantee attribute
        begin block body buffer bus
        case component configuration constant context cover
        default disconnect downto
        else elsif end entity exit
        fairness file for force function
        generate generic group guarded
        if impure in inertial inout is
        label library linkage literal loop
        map mod
        nand new next nor not null
        of on open or others out
        package parameter port postponed procedure process property protected pure
        range record register reject release rem report restrict restrict_guarantee return rol ror
        select sequence severity signal shared sla sll sra srl strong subtype
        then to transport type
        unaffected units until use
        variable view vpkg vmode vprop vunit
        wait when while with
        xnor xor
    "

    types="
        bit bit_vector
        boolean boolean_vector
        character
        file_open_state file_origin_kind
        integer integer_vector
        natural positive
        line line_vector
        real real_vector
        std_logic std_logic_vector
        std_ulogic std_ulogic_vector
        side
        signed unsigned
        string text
        time time_vector
    "

    functions="
        find_leftmost find_rightmost divide reciprocal remainder modulo minimum maximum
        std_match add_carry scalb
        resize to_ufixed to_sfixed to_unsigned to_signed to_real to_integer to_slv
        to_std_logic_vector to_stdlogicvector to_sulv to_std_ulogic_vector to_std_ulogicvector
        to_01 is_x to_x01 to_ux01 to_x01z
        ufixed_high ufixed_low sfixed_high sfixed_low to_ufix to_sfix ufix_high ufix_low
        sfix_high sfix_low
        write read bwrite binary_write bread binary_read owrite oread octal_write octal_read
        hwrite hread hex_write hex_read to_string to_bstring to_binary_string to_ostring
        to_octal_string to_hstring to_hex_string from_string from_bstring from_binary_string
        from_ostring from_octal_string from_hstring from_hex_string
        rising_edge falling_edge
    "

    join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; }

    # Add the language's grammar to the static completion list
    printf %s\\n "declare-option str-list vhdl_static_words $(join "${values} ${units} ${predefined_attributes} ${libraries} ${packages} ${reserved_words} ${types} ${functions}" ' ')"

     # Highlight keywords
    printf %s "
        add-highlighter shared/vhdl/code/ regex '(?i)\b($(join "${values}" '|'))\b' 0:value
        add-highlighter shared/vhdl/code/ regex '(?i)\b($(join "${units}" '|'))\b' 0:meta
        add-highlighter shared/vhdl/code/ regex \"'(?i)\b($(join "${predefined_attributes}" '|'))\b\" 0:attribute
        add-highlighter shared/vhdl/code/ regex '(?i)\b($(join "${libraries}" '|'))\b' 0:builtin
        add-highlighter shared/vhdl/code/ regex '(?i)\b($(join "${packages}" '|'))\b' 0:builtin
        add-highlighter shared/vhdl/code/ regex '(?i)\b($(join "${reserved_words}" '|'))\b' 0:keyword
        add-highlighter shared/vhdl/code/ regex '(?i)\b($(join "${functions}" '|'))\b\(' 1:builtin
        add-highlighter shared/vhdl/code/ regex '(?i)\b($(join "${types}" '|'))\b' 0:type
        add-highlighter shared/vhdl/code/ regex '^\h*(@[\w_.]+))' 1:attribute
    "
]

add-highlighter shared/vhdl/code/ regex \(|\)|\;|\.|,|:|\| 0:attribute

add-highlighter shared/vhdl/code/ regex \?\?|=|/=|<|<=|>|>=|\?=|\?/=|\?<|\?<=|\?>|\?>=|\+|-|&|\*|/|:= 0:operator

# Meta values highlight.
# The values 'U', 'X', 'W', and '–' are metalogical values; they define the behavior of the model itself rather than the behavior of the hardware being synthesized.
add-highlighter shared/vhdl/code/ regex "(?i)'[U|X|W|-]'" 0:meta
# Highlight other logical values.
add-highlighter shared/vhdl/code/ regex "(?i)'[0|1|Z|L|H]'" 0:value

# String
add-highlighter shared/vhdl/code/ regex '"[^"]*"' 0:string

# Binary vector.
add-highlighter shared/vhdl/code/ regex '[bB]"[01_]*"' 0:value

# Octal vector.
add-highlighter shared/vhdl/code/ regex '[oO]"[01234567_]*"' 0:value

# Hex vector.
add-highlighter shared/vhdl/code/ regex '(?i)x"[0123456789abcdef_]*"' 0:value

define-command -hidden vhdl-insert-on-new-line %[
    # Handle comment lines.
    evaluate-commands -itersel %[
        # Copy '--' comment prefix and following white spaces.
        try %[
            # <a-lt> is needed because of "Preserve previous line indent" command.
            try %[ execute-keys -draft k x s ^\h*--\h* <ret> y j <a-lt> gh P ]
        ]
    ]

    evaluate-commands -save-regs x %[
        # Save previous line indent in register x.
        try %[ execute-keys -draft kxs^\h+<ret>"xy ] catch %[ reg x '' ]

        # All "wisely add" commands share the same concept.
        # Only "end if" has extra comments.
        # Wisely add "end if;".
        evaluate-commands %[
            try %[
                # Validate previous line and that it is not closed yet.
                execute-keys -draft kx <a-k>^\h*(?i)((then|(.*:\h*)?if\b.*\bthen)$)<ret> j}ijx <a-K>^<c-r>x(?i)(elsif|else|end)\b<ret>
                # Don't add for "if ... generate", it requires "end generate;".
                execute-keys -draft kx <a-K>(?i)\bgenerate\b<ret>
                execute-keys -draft o<c-r>xend<space>if<semicolon><esc>
            ]
        ]
        # Wisely add "end generate;".
        evaluate-commands %[
            try %[
                execute-keys -draft kx <a-k>^\h*(?i).*\bgenerate$<ret> j}ijx <a-K>^<c-r>x(?i)(begin|elsif|else|end)\b<ret>
                # Don't add in case of comment line.
                execute-keys -draft kx <a-K>^\h*--<ret>
                execute-keys -draft o<c-r>xend<space>generate<semicolon><esc>
            ]
        ]
        # Wisely add "when" and "end case;".
        evaluate-commands %[
            try %[
                # TODO: Case needs special handling.
                execute-keys -draft kx <a-k>^\h*(?i)(case|.*\h*:\h*case)\b<ret> jwx <a-K>^<c-r>x(?i)(end|when)<ret>
                execute-keys -draft <c-r>xo<c-r>xend<space>case<semicolon><esc>kAwhen<space><esc>
            ]
        ]
        # Wisely add "begin" and "end block;".
        evaluate-commands %[
            try %[
                execute-keys -draft kx <a-k>^\h*(?i)((block|.*:\h*block)\b)<ret> j}ijx <a-K>^<c-r>x(?i)(begin|end)\b<ret>
                execute-keys -draft o<c-r>xbegin<ret><c-r>xend<space>block<semicolon><esc>
            ]
        ]
        # Wisely add "begin" and "end process;".
        evaluate-commands %[
            try %[
                execute-keys -draft kx <a-k>^\h*(?i)(.*:\h*)?(postponed\h+)?process\b<ret> j}ijx <a-K>^<c-r>x(?i)(begin|end)<ret>
                execute-keys -draft o<c-r>xbegin<ret><c-r>xend<space>process<semicolon><esc>
            ]
        ]
        # Wisely add "end loop;".
        evaluate-commands %[
            try %[
                execute-keys -draft kx <a-k>^\h*(?i)(.*\bloop|.*\h*:\h*(for|loop))$<ret> j}ijx <a-K>^<c-r>x(?i)(end)<ret>
                execute-keys -draft o<c-r>xend<space>loop<semicolon><esc>
            ]
        ]
        # Wisely add "end protected;".
        evaluate-commands %[
            try %[
                execute-keys -draft kx <a-k>^\h*(?i)(type\b.*\bis\h+protected)$<ret> j}ijx <a-K>^<c-r>x(?i)(end)<ret>
                execute-keys -draft o<c-r>xend<space>protected<semicolon><esc>
            ]
        ]
        # Wisely add "end protected body;".
        evaluate-commands %[
            try %[
                execute-keys -draft kx <a-k>^(?i)(\h*type\h+\w+\h+is\h+protected\h+body$)<ret> j}ijx <a-K>^<c-r>x(?i)end\h+protected\h+body\b<ret>
                execute-keys -draft o<c-r>xend<space>protected<space>body<semicolon><esc>
            ]
        ]
        # Wisely add "end record;".
        evaluate-commands %[
            try %[
                execute-keys -draft kx <a-k>^\h*(?i)(type\b.*\bis\h+record\h*)$<ret> j}ijx <a-K>^<c-r>x(?i)(end)<ret>
                execute-keys -draft o<c-r>xend<space>record<semicolon><esc>
            ]
        ]
        # Wisely add ");" for "type ... is (".
        evaluate-commands %[
            try %[
                execute-keys -draft kx <a-k>^\h*(?i)(type\b.*\bis\h+\(\h*)$<ret> j}ijx <a-K>^<c-r>x(\))<ret>
                execute-keys -draft o<c-r>x)<semicolon><esc>
            ]
        ]
        # Wisely add "end entity;".
        evaluate-commands %[
            try %[
                execute-keys -draft kx <a-k>^(?i)\h*entity\b.*\bis$<ret> j}ijx <a-K>^<c-r>x(?i)(begin|end)\b<ret>
                execute-keys -draft o<c-r>xend<space>entity<semicolon><esc>
            ]
        ]
        # Wisely add "begin" and "end function;".
        evaluate-commands %[
            try %[
                execute-keys -draft kx <a-k>^(?i)(\h*\)?\h*return\b.*\bis$)<ret> j}ijx <a-K>^<c-r>x(?i)(begin|end)\b<ret>
                execute-keys -draft o<c-r>xbegin<ret><c-r>xend<space>function<semicolon><esc>
            ]
            try %[
                execute-keys -draft kx <a-k>^(?i)(\h*((pure|impure)\h+)?function\b.*\bis$)<ret> j}ijx <a-K>^<c-r>x(?i)(begin|end)\b<ret>
                execute-keys -draft o<c-r>xbegin<ret><c-r>xend<space>function<semicolon><esc>
            ]
        ]
        # Wisely add "begin" and "end procedure;".
        evaluate-commands %[
            try %[
                execute-keys -draft kx <a-k>^(?i)(\h*procedure\b.*\bis$)<ret> j}ijx <a-K>^<c-r>x(?i)\b(begin|end)\b<ret>
                execute-keys -draft o<c-r>xbegin<ret><c-r>xend<space>procedure<semicolon><esc>
            ]
            try %[
                execute-keys -draft kx <a-k>^(?i)\h*\)\h*\bis$<ret> j}ijx <a-K>^<c-r>x(?i)\b(begin|end)\b<ret>
                # Verify that line with opening parenthesis contains "procedure" keyword.
                execute-keys -draft kx s\)<ret> <a-m><semicolon> x<a-k> (?i)\bprocedure\b<ret>
                execute-keys -draft o<c-r>xbegin<ret><c-r>xend<space>procedure<semicolon><esc>
            ]
        ]
        # Wisely add "end package;".
        evaluate-commands %[
            try %[
                execute-keys -draft kx <a-k>^(?i)(package\b)<ret> j}ijx <a-K>^<c-r>x(?i)(end)\b<ret>
                # Make sure it is not package body.
                execute-keys -draft kx<a-K>(?i)\bbody\b<ret>
                execute-keys -draft oend<space>package<semicolon><esc>
            ]
        ]
        # Wisely add "end package body;".
        evaluate-commands %[
            try %[
                execute-keys -draft kx <a-k>^(?i)(package\h+body\b)<ret> j}ijx <a-K>^<c-r>x(?i)(end)\b<ret>
                execute-keys -draft oend<space>package<space>body<semicolon><esc>
            ]
        ]
        # Wisely add "begin" and "end architecture;".
        evaluate-commands %[
            try %[
            execute-keys -draft kx <a-k>^(?i)\h*architecture\b<ret> j}ijx <a-K>^<c-r>x(?i)(begin|end)\b<ret>
            execute-keys -draft o<c-r>xbegin<ret><c-r>xend<space>architecture<semicolon><esc>
            ]
        ]
        # Wisely add ");" for "port (".
        evaluate-commands %[
            try %[
                execute-keys -draft kx <a-k>^\h*(?i)port\h*\($<ret> j}ijx <a-K>^<c-r>x(\)\;)<ret>
                execute-keys -draft o<c-r>x)<semicolon><esc>
            ]
        ]
        # Wisely add ");" for "port map (".
        evaluate-commands %[
            try %[
                execute-keys -draft kx <a-k>^\h*(?i)port\h+map\h*\($<ret> j}ijx <a-K>^<c-r>x(\)\;)<ret>
                execute-keys -draft o<c-r>x)<semicolon><esc>
            ]
        ]
        # Wisely add ");" for "generic (".
        evaluate-commands %[
            try %[
                execute-keys -draft kx <a-k>^\h*(?i)generic\h*\($<ret> j}ijx <a-K>^<c-r>x(\)\;)<ret>
                execute-keys -draft o<c-r>x)<semicolon><esc>
            ]
        ]
        # Wisely add ")" for "generic map (".
        evaluate-commands %[
            try %[
                execute-keys -draft kx <a-k>^\h*(?i)generic\h+map\h*\($<ret> j}ijx <a-K>^<c-r>x(\))<ret>
                execute-keys -draft o<c-r>x)<esc>
            ]
        ]
        # Wisely add ") return ;" for "[pure|impure] function ... (".
        evaluate-commands %[
            try %[
                execute-keys -draft kx <a-k>^\h*(?i)(pure\b|impure\b)?\h*function\b.*\h*\($<ret> j}ijx <a-K>^<c-r>x(\)\h*return.*)<ret>
                execute-keys -draft o<c-r>x)<space>return<space><semicolon><esc>
            ]
        ]
        # Wisely add ");" for "procedure ... (".
        evaluate-commands %[
            try %[
                execute-keys -draft kx <a-k>^\h*(?i)procedure\b.*\h*\($<ret> j}ijx <a-K>^<c-r>x(\)\h*\;)<ret>
                execute-keys -draft o<c-r>x)<semicolon><esc>
            ]
        ]
    ]
]

define-command -hidden vhdl-indent-on-new-line %{
    evaluate-commands -itersel %{
        # Align "then" to previous "if|elsif".
        evaluate-commands -itersel -save-regs x %[
            try %[
                execute-keys -draft k x <a-k> (?i)^\h*then$ <ret>
                try %[ execute-keys -draft <a-/>(?i)\b(if|elsif)\b<ret>xs^\h+<ret>"xy ] catch %[ reg x '' ]
                try %[ execute-keys -draft k xs^\h+<ret>d ] catch %[ ]
                execute-keys -draft kgh i<c-r>x<esc>
            ]
        ]

         # Align "generate" to previous "if|for".
        evaluate-commands -itersel -save-regs x %[
            try %[
                execute-keys -draft k x <a-k> (?i)^\h*generate$ <ret>
                try %[ execute-keys -draft <a-/>(?i)\b(if|for)\b<ret>xs^\h+<ret>"xy ] catch %[ reg x '' ]
                try %[ execute-keys -draft k xs^\h+<ret>d ] catch %[ ]
                execute-keys -draft kgh i<c-r>x<esc>
            ]
        ]

         # Preserve previous line indent.
        try %[ execute-keys -draft <semicolon> K <a-&> ]

         # Cleanup trailing whitespaces from previous line.
        try %[ execute-keys -draft k x s \h+$ <ret> d ]

         # Increase indent after some keywords.
        try %[
            execute-keys -draft kx<a-k> (?i)\b(begin|block|body|else|for|generate|if|is|loop|process|protected|record|select|then)$ <ret>
            # Do not indent if in comment line.
            execute-keys -draft kx<a-K>(?i)^\h*--<ret>
            # Do not indent for "case ... is".
            execute-keys -draft kx<a-K>^\h*(?i)(case|.*\h*:\h*case)\b<ret>
            execute-keys -draft <semicolon> <a-gt>
        ]
        # Copy the indentation of the matching if.
        try %{ execute-keys -draft , k x <a-k> ^\h*(elsif\b|else$) <ret> gh [c^\h*(\S*\h*:\h*)?if\b,\bend\sif\b <ret> x <a-S> 1<a-&> , j K <a-&> }

        # Increase indent after some operators.
        try %[ execute-keys -draft <semicolon> k x <a-k> (\(|=>|<=|:=)$ <ret> j <a-gt> ]
     }
}

define-command vhdl-indent-on-closing-parenthesis %[
    evaluate-commands -itersel %[
        # Decrease indent after ")" at the beginning of line.
        try %[ execute-keys -draft x <a-k> (^\h+\)$) <ret> <a-lt> ]
    ]
]

§