parse: Simple way to parse strings with Python-like format strings.

[ bsd3, library, parser ] [ Propose Tags ] [ Report a vulnerability ]

Please refer to the README file that ships with this package.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.0.0
Change log ChangeLog.md
Dependencies base (>=4.7 && <5), split (==0.2.3.4), template-haskell (==2.16.0.0) [details]
License BSD-3-Clause
Copyright Jacob Lagares Pozo
Author Jacob Lagares Pozo
Maintainer jlagarespo@protonmail.com
Category Parser
Home page https://github.com/jlagarespo/parse#readme
Bug tracker https://github.com/jlagarespo/parse/issues
Source repo head: git clone https://github.com/jlagarespo/parse
Uploaded by moonsheep at 2021-12-11T23:43:38Z
Distributions
Downloads 142 total (4 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2021-12-12 [all 1 reports]

Readme for parse-0.1.0.0

[back to package description]

parse

Haskell, while notorious for its extremely powerful parser combinators library, lacks a quick-and-dirty way to parse simple-formatted strings in a one-liner. This is especially frustrating for Advent of Code, where the input to some puzzles, which is in other languages fairly painless to parse, requires some overly convoluted, hacky and noncompact solution.

Envious of Python's parse package, I decided to write my own, very stripped down version, in Haskell. This package provides a way to create a sort of template for how strings should look, and then parse them accodringly. While inspired by Python format strings, the type of each field is not specified in the string itself, and as a matter of fact, if the compiler can't guess them by their use, they must be specified explicitly (like in the example.)

Examples:

parse "It's {}, I love it!" "It's spam, I love it!" :: String
 == "spam"

parse "{} + {} = {}" "2.1568743 + 7.196057 = 9.3529313" :: (String, String, String)
 == ("2.1568743","7.196057","9.3529313")

A safe variant of parse is also provided, that returns Maybe instead of throwing an error:

parse "{} * {} = {}" "2 + 3 = 5" :: (String, String, String)
*** Exception: No candidates for "" in "2 + 3 = 5".

-- Whereas:
parseMaybe "{} * {} = {}" "2 + 3 = 5" :: Maybe (String, String, String)
 == Nothing

You can also parse lists quite easily:

map read $ parseList "{}, {}, {}, {}, {}, {}, {}, {}, {}, {}" "1, 2, 3, 5, 7, 11, 13, 17, 19, 23" :: [Int]
 == [1,2,3,5,7,11,13,17,19,23]

(Note: You're still parsing a fixed amount of values, but now you can, as shown in the example, map stuff to it.)

It should be noted that this package lacks most of the features supplied by parse, instead providing only basic parsing functionality. This may or may not change in the future.