ghc-syntax-highlighter

Syntax highlighter for Haskell using the lexer of GHC

https://github.com/mrkkrp/ghc-syntax-highlighter

Version on this page:0.0.8.0
LTS Haskell 22.40:0.0.10.0
Stackage Nightly 2024-11-02:0.0.11.0
Latest on Hackage:0.0.12.0

See all snapshots ghc-syntax-highlighter appears in

BSD-3-Clause licensed and maintained by Mark Karpov
This version can be pinned in stack with:ghc-syntax-highlighter-0.0.8.0@sha256:28cbe1d66767102bb31f90e7ba8369ca71e6392a824502f964c41033c9ee0503,1805

Module documentation for 0.0.8.0

Used by 2 packages in lts-20.26(full list with versions):

GHC syntax highligher

License FreeBSD Hackage Stackage Nightly Stackage LTS CI

This is a syntax highlighter library for Haskell using the lexer of GHC.

Here is a blog post announcing the package, the readme is mostly derived from it:

Motivation

Parsing Haskell is hard, because Haskell is a complex language with countless features. The only way to get it right 100% is to use the parser of GHC itself. Fortunately, now there is the ghc package, which as of version 8.4.1 exports enough of GHC’s source code to allow us use its lexer.

Alternative approaches, even decent ones like highlight.js either don’t support cutting-edge features or do their work without sufficient precision so that many tokens end up combined and the end result is typically still hard to read.

How to use it in your blog

Depends on your markdown processor. If you’re an mmark user, good news, since version 0.2.1.0 of mmark-ext it includes the ghcSyntaxHighlighter extension. Due to flexibility of MMark, it’s possible to use this highlighter for Haskell and skylighting as a fall-back for everything else. Consult the docs for more information.

skylighting is what Pandoc uses. And from what I can tell it’s hardcoded to use only that library for highlighting, so some creativity may be necessary to get it work.

Limitations

CPP directives are not lexed correctly, because the GHC parser is not designed to parse them.

Contribution

Issues, bugs, and questions may be reported in the GitHub issue tracker for this project.

Pull requests are also welcome.

License

Copyright © 2018–present Mark Karpov

Distributed under BSD 3 clause license.

Changes

GHC syntax highlighter 0.0.8.0

  • Uses ghc-lib-parser-9.2.1.x.

GHC syntax highlighter 0.0.7.0

  • Uses ghc-lib-parser-9.0.1.x.

GHC syntax highlighter 0.0.6.0

  • Uses ghc-lib-parser-8.10.x.

GHC syntax highlighter 0.0.5.0

  • Uses ghc-lib-parser.

GHC syntax highlighter 0.0.4.1

  • Works with GHC 8.8.

  • Dropped support for GHC 8.2.

GHC syntax highlighter 0.0.4.0

  • Implemented highlighting of file header pragmas such as OPTIONS_GHC and LANGUAGE. They are not handled by the GHC lexer, so custom code were added for this purpose.

GHC syntax highlighter 0.0.3.1

  • Fixed the bug when certain extensions such as -XLambdaCase were not enabled when the code was compiled with GHC 8.6.

GHC syntax highlighter 0.0.3.0

  • Compiles with GHC 8.6.

GHC syntax highlighter 0.0.2.0

  • Added Loc and tokenizeHaskellLoc.

GHC syntax highlighter 0.0.1.0

  • Initial release.