Using Parsec, I'm able to write a function of type String -> Maybe MyType
with relative ease. I would now like to create a Read
instance for my type based on that; however, I don't understand how readsPrec
works or what it is supposed to do.
My best guess right now is that readsPrec
is used to build a recursive parser from scratch to traverse a string, building up the desired datatype in Haskell. However, I already have a very robust parser who does that very thing for me. So how do I tell readsPrec
to use my parser? What is the "operator precedence" parameter it takes, and what is it good for in my context?
If it helps, I've created a minimal example on Github. It contains a type, a parser, and a blank Read instance, and reflects quite well where I'm stuck.
(Background: The real parser is for Scheme.)
However, I already have a very robust parser who does that very thing for me.
It's actually not that robust, your parser has problems with superfluous parentheses, it won't parse
((1) (2))
for example, and it will throw an exception on some malformed inputs, because
singleP = Single . read <$> many digit
may use read "" :: Int
.
That out of the way, the precedence argument is used to determine whether parentheses are necessary in some place, e.g. if you have
infixr 6 :+:
data a :+: b = a :+: b
data C = C Int
data D = D C
you don't need parentheses around a C 12
as an argument of (:+:)
, since the precedence of application is higher than that of (:+:)
, but you'd need parentheses around C 12
as an argument of D
.
So you'd usually have something like
readsPrec p = needsParens (p >= precedenceLevel) someParser
where someParser
parses a value from the input without enclosing parentheses, and needsParens True thing
parses a thing
between parentheses, while needsParens False thing
parses a thing
optionally enclosed in parentheses [you should always accept more parentheses than necessary, ((((((1))))))
should parse fine as an Int
].
Since the readsPrec p
parsers are used to parse parts of the input as parts of the value when reading lists, tuples etc., they must return not only the parsed value, but also the remaining part of the input.
With that, a simple way to transform a parsec
parser to a readsPrec
parser would be
withRemaining :: Parser a -> Parser (a, String)
withRemaining p = (,) <$> p <*> getInput
parsecToReadsPrec :: Parser a -> Int -> ReadS a
parsecToReadsPrec parsecParser prec input
= case parse (withremaining $ needsParens (prec >= threshold) parsecParser) "" input of
Left _ -> []
Right result -> [result]
If you're using GHC, it may however be preferable to use a ReadPrec / ReadP
parser (built using Text.ParserCombinators.ReadP[rec]
) instead of a parsec
parser and define readPrec
instead of readsPrec
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With