Jump-to-definition in vim for Happy/Alex macros

Vanessa McHale
2024-09-15T14:38:13Z

CTAGS.md

Universal ctags can be extended to generate tags for Happy and Alex macros. ghc-tags can already handle functions and types defined therein.

Put the following in ~/.ctags.d/alex.ctags:

--langdef=ALEX
--langmap=ALEX:.x
--regex-ALEX=/(\$[[:lower:]][[:alnum:]_]*)\s*=/\1/macro/
--regex-ALEX=/(@[[:lower:]][[:alnum:]_]*)\s*=/\1/macro/

And in ~/.ctags.d/happy.ctags:

--langdef=HAPPY
--langmap=HAPPY:.y

--_tabledef-HAPPY=z
--_tabledef-HAPPY=tokens

--_mtable-regex-HAPPY=z/%token//{tenter=tokens}
--_mtable-regex-HAPPY=tokens/%%//{tleave}
--_mtable-regex-HAPPY=z/.//

--_mtable-regex-HAPPY=tokens/([[:lower:]][[:alpha:]]*)\s*\{/\1/macro/
--_mtable-regex-HAPPY=tokens/.//

--regex-HAPPY=/([[:upper:]][[:alpha:]]*)\s*:/\1/v,var/
--regex-HAPPY=/^\s*([[:lower:]][[:alpha:]]*)\s*\(/\1/macro/

:h tjump and :h CTRL-] explain how to jump to an identifier in Vim. You will also need au FileType alex setl iskeyword+=36 for vim to recognize macro names under the cursor. (My hs-err plugin does this out-of-the-box).

To generate (augmented) tags on file write, put the following in your ~/.vimrc:

au BufRead,BufWritePost *.y silent !ctags --append --languages=HAPPY %:p
au BufRead,BufWritePost *.x silent !ctags --append --languages=ALEX %:p

My personal ctags definitions, including Dhall (for instance), are on darcshub.