symbol-parser: Type level string parser combinators

[ data, library, mit, types ] [ Propose Tags ]
Versions [RSS] 0.1.0, 0.2.0, 0.3.0
Change log CHANGELOG.md
Dependencies base (>=4.16 && <5), defun-core (>=0.1 && <0.2) [details]
License MIT
Author Ben Orchard
Maintainer Ben Orchard <thefirstmuffinman@gmail.com>
Category Types, Data
Home page https://github.com/raehik/symbol-parser#readme
Bug tracker https://github.com/raehik/symbol-parser/issues
Source repo head: git clone https://github.com/raehik/symbol-parser
Uploaded by raehik at 2024-04-19T02:22:29Z
Distributions
Downloads 37 total (37 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for symbol-parser-0.2.0

[back to package description]

symbol-parser

Type level string (Symbol) parser combinators with nice error messages.

This library is effectively made possible by UnconsSymbol :: Symbol -> Maybe (Char, Symbol) introduced in GHC 9.2. A big thank you to all GHC developers!!!

Examples

ghci> import Data.Type.Symbol.Parser
ghci> :k! RunParser (Drop 3 :*>: Isolate 2 NatDec :<*>: (Drop 3 :*>: NatHex)) "___10___FF"
...
= Right '( '(10, 255), "")

Design

type Parser s r = Char -> s -> Result s r
data Result s r = Cont s | Done r | Err ErrorMessage

Symbols are parsed Char by Char. For each Char, the parser is called with the Char in question and the current parser state. Parsing continues based on the result.

We pass parsers around via defunctionalization symbols. The plumbing here is provided by Oleg's fantastic [defun-core][hackage-defun-core].

I try to keep the internals as simple as possible. Hopefully, this library looks like any other parser combinator, except reflected up to the type level.

Why?

Via GHC.Generics, we may inspect Haskell data types on the type level. Constructor names are Symbols. Ever reify these, then perform some sort of checking or parsing on the term level? Now you can do it on the type level instead. Catch bugs earlier, get faster runtime.

I want to help

I would gladly accept further combinators or other suggestions. Please add an issue or pull request, or contact me via email or whatever (I'm raehik everywhere).

License

Provided under the MIT license. See LICENSE for license text.