Skip to content

Instantly share code, notes, and snippets.

@vmchale
Last active September 15, 2024 16:14
Show Gist options
  • Save vmchale/5f08a819f2c6d463887a0fa3eb7b2527 to your computer and use it in GitHub Desktop.
Save vmchale/5f08a819f2c6d463887a0fa3eb7b2527 to your computer and use it in GitHub Desktop.
Jump-to-definition in vim for Happy/Alex macros

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment