Question on parsing configuration files

I’m currently learning Haskell by building a small project and i’d like to parse a configuration file.
Is there a way to have haskell .hs source code files as configuration files and if not, what are some good libraries for common file types like yaml, toml, ini that don’t have a lot of dependencies? Also, it would be helpful if anyone could suggest resources learn more on parsing.

3 Likes

To parse ini files I used (and was happy with) config-ini.

For general parsing, megaparsec is a good, well documented, actively maintained choice.

2 Likes

For small projects configurator might also work decently.

1 Like

I use toml-parser for TOML. And dhall is relatively unique to the Haskell ecosystem but IMO it fulfills its goal of being a better YAML.

I don’t know of any good way to have .hs as configuration files.

The computer science literature on parsing is vast, though parser combinators less well-documented. Perhaps this?

Relatively unique to Haskell is monadic parser/lexer generators, I know the Happy user guide has a little.

3 Likes

I’d suggest dhall. It’s not exactly Haskell, but it’s the nearest configuration language to it. The configuration loading code would be as simple as with aeson.

2 Likes

I would recommend using tomland for TOML since it supports the latest TOML specification, and it’s also quite fast. In all my projects where I need a static configuration, I’m using the TOML format since it’s much simpler and cleaner than YAML. I didn’t have a case where I need dhall yet, but it can be useful if your configuration can be boilerplate-heavy.

3 Likes

One benefit of Dhall is that you can use its TH function makeHaskellTypes to automatically create the Haskell data types associated with the Dhall config. Here’s an example: Haskell part, and the Dhall part.