@@ -26,66 +26,85 @@ import Data.Maybe (fromJust)
26
26
import qualified Data.Text as T (unpack )
27
27
import Data.UUID (UUID )
28
28
import qualified Data.UUID as U (fromString )
29
- import Data.Void (Void )
30
- import Text.Megaparsec (Parsec , anySingle ,
31
- errorBundlePretty , runParser ,
32
- try )
29
+ import Text.Megaparsec (Parsec , ShowErrorComponent ,
30
+ anySingle , errorBundlePretty ,
31
+ runParser , try )
33
32
import Text.Megaparsec.Char (char , digitChar , hexDigitChar ,
34
33
string' )
35
34
36
35
-- | Parse a case-insensitive human-readable boolean, including C-style numbers
37
36
-- and English yes-no.
38
- boolParser :: Parsec Void String Bool
37
+ boolParser
38
+ :: Ord e
39
+ => Parsec e String Bool
39
40
boolParser = true <|> false
40
41
where true = True <$ choice (map string' [" true" , " y" , " yes" , " 1" ])
41
42
false = False <$ choice (map string' [" false" , " n" , " no" , " 0" ])
42
43
43
44
-- | Parse a 'Bounded' 'Enum' type that has a 'Show' instance, trying all
44
45
-- possibilities, case-insensitive, in the 'Enum' order.
45
46
boundedEnumShowParser
46
- :: Bounded a
47
+ :: Ord e
48
+ => Bounded a
47
49
=> Enum a
48
50
=> Show a
49
- => Parsec Void String a
51
+ => Parsec e String a
50
52
boundedEnumShowParser =
51
53
choice . map parseShow $ sortOn (negate . length . show ) [minBound .. ]
52
54
where parseShow a = string' (show a) $> a
53
55
54
56
-- | Parse a comma-separated list of items.
55
- commaSeparated :: Parsec Void String a -> Parsec Void String (NonEmpty a )
57
+ commaSeparated
58
+ :: Ord e
59
+ => Parsec e String a
60
+ -> Parsec e String (NonEmpty a )
56
61
commaSeparated p = (:|) <$> p <*> many (char ' ,' >> p)
57
62
58
63
-- | Parse any occurrence of a given parser. Consumes any input before occurence.
59
- occurrence :: Parsec Void String a -> Parsec Void String a
64
+ occurrence
65
+ :: Ord e
66
+ => Parsec e String a
67
+ -> Parsec e String a
60
68
occurrence p = go
61
69
where go = p <|> (anySingle >> go)
62
70
63
71
-- | Parse all occurrences of a given parser.
64
- occurrences :: Parsec Void String a -> Parsec Void String [a ]
72
+ occurrences
73
+ :: Ord e
74
+ => Parsec e String a
75
+ -> Parsec e String [a ]
65
76
occurrences = some . try . occurrence . try
66
77
67
78
-- | Parse a positive number with decimals.
68
- posDecNumParser :: Parsec Void String Double
79
+ posDecNumParser
80
+ :: Ord e
81
+ => Parsec e String Double
69
82
posDecNumParser = do
70
83
num <- some digitChar
71
84
den <- maybe " " (" ." <> ) <$> optional (char ' .' >> some digitChar)
72
85
73
86
return . read $ num <> den
74
87
75
88
-- | Parse a positive integer.
76
- posNumParser :: Read a => Parsec Void String a
89
+ posNumParser
90
+ :: Ord e
91
+ => Read a
92
+ => Parsec e String a
77
93
posNumParser = read <$> some digitChar
78
94
79
95
-- | Parse an integer, without any spaces between minus sign and digits.
80
- numParser :: Parsec Void String Int
96
+ numParser
97
+ :: Ord e
98
+ => Parsec e String Int
81
99
numParser = (char ' -' >> negate <$> posNumParser) <|> posNumParser
82
100
83
101
-- | Convert a 'Parsec' parser into a 'Parser' suited for 'FromJSON' instances.
84
102
parsecToJSONParser
103
+ :: ShowErrorComponent e
85
104
-- ^ Parser name.
86
- :: String
105
+ => String
87
106
-- ^ Parser.
88
- -> Parsec Void String a
107
+ -> Parsec e String a
89
108
-- ^ Input value.
90
109
-> Value
91
110
-> Parser a
@@ -94,11 +113,15 @@ parsecToJSONParser n p =
94
113
95
114
-- | Convert a 'Parsec' parser into a 'ReadS' parser. Useful for defining 'Read'
96
115
-- instances with 'Megaparsec'.
97
- parsecToReadsPrec :: Parsec Void String a -> ReadS a
116
+ parsecToReadsPrec
117
+ :: Parsec e String a
118
+ -> ReadS a
98
119
parsecToReadsPrec p = either (const [] ) (\ x -> [(x, " " )]) . runParser p " string"
99
120
100
121
-- | Parse a RFC4122-compliant UUID.
101
- uuidParser :: Parsec Void String UUID
122
+ uuidParser
123
+ :: Ord e
124
+ => Parsec e String UUID
102
125
uuidParser = do
103
126
part1 <- replicateM 8 hexDigitChar
104
127
void $ char ' -'
0 commit comments