@@ -49,6 +49,7 @@ import Text.Megaparsec.Char (
4949 hexDigitChar ,
5050 string' ,
5151 )
52+ import Text.Read (readMaybe )
5253
5354-- | Parse a case-insensitive human-readable boolean, including C-style numbers
5455-- and English yes-no.
@@ -97,22 +98,30 @@ occurrences
9798 -> Parsec e String [a ]
9899occurrences = some . try . occurrence . try
99100
100- -- | Parse a positive number with decimals.
101+ -- | Parse a positive number, with or without decimals prefixed by a @.@ .
101102posDecNumParser
102103 :: Ord e
103- => Parsec e String Double
104+ => Read a
105+ => Parsec e String a
104106posDecNumParser = do
105107 num <- some digitChar
106- den <- maybe " " (" ." <> ) <$> optional (char ' .' >> some digitChar)
108+ dec <- maybe " " (" ." <> ) <$> optional (char ' .' >> some digitChar)
109+
110+ let str = num <> dec
107111
108- return . read $ num <> den
112+ maybe ( fail ( " could not read from input: " <> str)) pure (readMaybe str)
109113
110114-- | Parse a positive integer.
111115posNumParser
112116 :: Ord e
113117 => Read a
114118 => Parsec e String a
115- posNumParser = read <$> some digitChar
119+ posNumParser = do
120+ digits <- some digitChar
121+ maybe
122+ (fail (" could not read from digits: " <> digits))
123+ pure
124+ (readMaybe digits)
116125
117126-- | Parse an integer, without any space between minus sign and digits.
118127numParser
@@ -132,7 +141,7 @@ parsecToJSONParser
132141 -- ^ Parser.
133142 -> (Value -> Parser a )
134143parsecToJSONParser n p =
135- withText n $ either (fail . errorBundlePretty) pure . runParser p n . T. unpack
144+ withText n ( either (fail . errorBundlePretty) pure . runParser p n . T. unpack)
136145
137146-- | Convert a 'Parsec' parser into a 'ReadS' parser. Useful for defining 'Read'
138147-- instances with 'Text.Megaparsec'.
@@ -152,4 +161,7 @@ uuidParser = do
152161 part4 <- replicateM 4 hexDigitChar <* char ' -'
153162 part5 <- replicateM 12 hexDigitChar
154163
155- pure . fromJust . U. fromString $ intercalate " -" [part1, part2, part3, part4, part5]
164+ pure
165+ (fromJust
166+ (U. fromString
167+ (intercalate " -" [part1, part2, part3, part4, part5])))
0 commit comments