@@ -49,6 +49,7 @@ import Text.Megaparsec.Char (
49
49
hexDigitChar ,
50
50
string' ,
51
51
)
52
+ import Text.Read (readMaybe )
52
53
53
54
-- | Parse a case-insensitive human-readable boolean, including C-style numbers
54
55
-- and English yes-no.
@@ -97,22 +98,30 @@ occurrences
97
98
-> Parsec e String [a ]
98
99
occurrences = some . try . occurrence . try
99
100
100
- -- | Parse a positive number with decimals.
101
+ -- | Parse a positive number, with or without decimals prefixed by a @.@ .
101
102
posDecNumParser
102
103
:: Ord e
103
- => Parsec e String Double
104
+ => Read a
105
+ => Parsec e String a
104
106
posDecNumParser = do
105
107
num <- some digitChar
106
- den <- maybe " " (" ." <> ) <$> optional (char ' .' >> some digitChar)
108
+ dec <- maybe " " (" ." <> ) <$> optional (char ' .' >> some digitChar)
109
+
110
+ let str = num <> dec
107
111
108
- return . read $ num <> den
112
+ maybe ( fail ( " could not read from input: " <> str)) pure (readMaybe str)
109
113
110
114
-- | Parse a positive integer.
111
115
posNumParser
112
116
:: Ord e
113
117
=> Read a
114
118
=> 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)
116
125
117
126
-- | Parse an integer, without any space between minus sign and digits.
118
127
numParser
@@ -132,7 +141,7 @@ parsecToJSONParser
132
141
-- ^ Parser.
133
142
-> (Value -> Parser a )
134
143
parsecToJSONParser 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)
136
145
137
146
-- | Convert a 'Parsec' parser into a 'ReadS' parser. Useful for defining 'Read'
138
147
-- instances with 'Text.Megaparsec'.
@@ -152,4 +161,7 @@ uuidParser = do
152
161
part4 <- replicateM 4 hexDigitChar <* char ' -'
153
162
part5 <- replicateM 12 hexDigitChar
154
163
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