66module Data.Uri.Quack
77 ( Parser
88 , runParser
9+ , decodeUtf8Query
910 , ParserError (.. )
1011 , -- * Combining
1112 unlabeled
@@ -21,15 +22,18 @@ module Data.Uri.Quack
2122import qualified Data.Attoparsec.Text as Atto (Parser , parseOnly )
2223import qualified Data.Aeson as Aeson (FromJSON , eitherDecode )
2324
24- import Data.Text as T
25- import Data.Text.Lazy as LT
26- import Data.Text.Lazy.Encoding as LT
25+ import qualified Data.Text as T
26+ import qualified Data.Text.Encoding as T
27+ import qualified Data.Text.Lazy as LT
28+ import qualified Data.Text.Lazy.Encoding as LT
2729import Data.Functor.Identity
2830import Data.String (IsString (fromString ))
2931import Control.Applicative
3032import Control.Monad.State
3133import Control.Monad.Except
3234
35+ import Network.HTTP.Types.URI (Query )
36+
3337
3438
3539data ParserState = ParserState
@@ -43,6 +47,10 @@ initParserState xs = ParserState
4347 }
4448
4549
50+ decodeUtf8Query :: Query -> [(T. Text , Maybe T. Text )]
51+ decodeUtf8Query = map (\ (l,r) -> (T. decodeUtf8 l, T. decodeUtf8 <$> r))
52+
53+
4654data ParserError
4755 = NoParse String
4856 deriving (Show , Eq )
@@ -96,11 +104,15 @@ instance Alternative Parser where
96104 pure []
97105
98106-- | Parse /only/ the label, disregarding any @=@ value;
99- -- .
107+ --
100108-- > unlabeled (attoparsec double)
101- -- . would parse something like
109+ --
110+ -- would parse something like
111+ --
102112-- > "/foo?1234"
103- -- . /or/
113+ --
114+ -- /or/
115+ --
104116-- > "/foo?1234=asdf"
105117unlabeled :: PieceParser a -> Parser a
106118unlabeled (PieceParser f) = Parser $ do
@@ -118,9 +130,11 @@ unlabeled (PieceParser f) = Parser $ do
118130
119131-- | Parse with a label, but throw the parse result of the
120132-- label away;
121- -- .
133+ --
122134-- > "foo" .= attoparsec double
123- -- . would parse something like
135+ --
136+ -- would parse something like
137+ --
124138-- > "/foo?foo=1234"
125139(.=) :: PieceParser a -> PieceParser b -> Parser b
126140(PieceParser l) .= (PieceParser f) = Parser $ do
@@ -143,10 +157,12 @@ unlabeled (PieceParser f) = Parser $ do
143157
144158
145159-- | @liftM2@ for parse results /between/ @=@, for instance:
146- -- .
160+ --
147161-- > overEquals (,) (attoparsec $ double <* endOfInput)
148162-- > (attoparsec $ double <* endOfInput)
149- -- . would parse something like
163+ --
164+ -- would parse something like
165+ --
150166-- > "/foo?1234=1234"
151167overEquals :: (a -> b -> c ) -> PieceParser a -> PieceParser b -> Parser c
152168overEquals f (PieceParser l) (PieceParser r) = Parser $ do
0 commit comments