1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
#if __GLASGOW_HASKELL__ < 800
{-# LANGUAGE RecordWildCards, TemplateHaskell, ViewPatterns #-}
#else
{-# LANGUAGE RecordWildCards, TemplateHaskellQuotes, ViewPatterns #-}
#endif
#if MIN_VERSION_template_haskell(2,12,0)
{-# LANGUAGE Safe #-}
#elif __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Trustworthy #-}
#endif
module Network.URI.Static
(
-- * Absolute URIs
uri
#if __GLASGOW_HASKELL__ >= 708
, staticURI
#endif
, staticURI'
-- * Relative URIs
, relativeReference
#if __GLASGOW_HASKELL__ >= 708
, staticRelativeReference
#endif
, staticRelativeReference'
) where
import Language.Haskell.TH.Lib (ExpQ)
import Language.Haskell.TH.Quote (QuasiQuoter(..))
import Network.URI (URI(..), parseURI, parseRelativeReference)
#if __GLASGOW_HASKELL__ >= 708
import Language.Haskell.TH.Syntax.Compat (SpliceQ, unTypeCode, toCode)
#endif
-- $setup
-- >>> :set -XTemplateHaskell
-- >>> :set -XQuasiQuotes
----------------------------------------------------------------------------
-- Absolute URIs
----------------------------------------------------------------------------
#if __GLASGOW_HASKELL__ >= 708
-- | 'staticURI' parses a specified string at compile time
-- and return an expression representing the URI when it's a valid URI.
-- Otherwise, it emits an error.
--
-- >>> $$(staticURI "http://www.google.com/")
-- http://www.google.com/
--
-- >>> $$(staticURI "http://www.google.com/##")
-- <BLANKLINE>
-- <interactive>...
-- ... Invalid URI: http://www.google.com/##
-- ...
staticURI :: String -- ^ String representation of a URI
-> SpliceQ URI -- ^ URI
staticURI (parseURI -> Just u) = [|| u ||]
staticURI s = error $ "Invalid URI: " ++ s
#endif
-- | 'staticURI'' parses a specified string at compile time.
--
-- The typed template haskell 'staticURI' is available only with GHC-7.8+.
staticURI' :: String -- ^ String representation of a URI
-> ExpQ -- ^ URI
#if __GLASGOW_HASKELL__ >= 708
staticURI' = unTypeCode . toCode . staticURI
#else
staticURI' (parseURI -> Just u) = [| u |]
staticURI' s = fail $ "Invalid URI: " ++ s
#endif
-- | 'uri' is a quasi quoter for 'staticURI'.
--
-- >>> [uri|http://www.google.com/|]
-- http://www.google.com/
--
-- >>> [uri|http://www.google.com/##|]
-- <BLANKLINE>
-- <interactive>...
-- ... Invalid URI: http://www.google.com/##
-- ...
uri :: QuasiQuoter
uri = QuasiQuoter {
quoteExp = staticURI',
quotePat = undefined,
quoteType = undefined,
quoteDec = undefined
}
----------------------------------------------------------------------------
-- Relative URIs
----------------------------------------------------------------------------
#if __GLASGOW_HASKELL__ >= 708
-- | 'staticRelativeReference' parses a specified string at compile time and
-- return an expression representing the URI when it's a valid relative
-- reference. Otherwise, it emits an error.
--
-- >>> $$(staticRelativeReference "/foo?bar=baz#quux")
-- /foo?bar=baz#quux
--
-- >>> $$(staticRelativeReference "http://www.google.com/")
-- <BLANKLINE>
-- <interactive>...
-- ... Invalid relative reference: http://www.google.com/
-- ...
staticRelativeReference :: String -- ^ String representation of a reference
-> SpliceQ URI -- ^ Refererence
staticRelativeReference (parseRelativeReference -> Just ref) = [|| ref ||]
staticRelativeReference ref = error $ "Invalid relative reference: " ++ ref
#endif
-- | 'staticRelativeReference'' parses a specified string at compile time and
-- return an expression representing the URI when it's a valid relative
-- reference. Otherwise, it emits an error.
--
-- The typed template haskell 'staticRelativeReference' is available only with GHC-7.8+.
staticRelativeReference' :: String -- ^ String representation of a reference
-> ExpQ -- ^ Refererence
#if __GLASGOW_HASKELL__ >= 708
staticRelativeReference' = unTypeCode . toCode . staticRelativeReference
#else
staticRelativeReference' (parseRelativeReference -> Just ref) = [| ref |]
staticRelativeReference' ref = fail $ "Invalid relative reference: " ++ ref
#endif
-- | 'relativeReference' is a quasi quoter for 'staticRelativeReference'.
--
-- >>> [relativeReference|/foo?bar=baz#quux|]
-- /foo?bar=baz#quux
--
-- >>> [relativeReference|http://www.google.com/|]
-- <BLANKLINE>
-- <interactive>...
-- ... Invalid relative reference: http://www.google.com/
-- ...
relativeReference :: QuasiQuoter
relativeReference = QuasiQuoter {
quoteExp = staticRelativeReference',
quotePat = undefined,
quoteType = undefined,
quoteDec = undefined
}
|