Skip to content

Instantly share code, notes, and snippets.

@hasufell
Created February 19, 2020 22:15
Show Gist options
  • Save hasufell/17f468a7e8ff037dc12bbd2d5c9d645c to your computer and use it in GitHub Desktop.
Save hasufell/17f468a7e8ff037dc12bbd2d5c9d645c to your computer and use it in GitHub Desktop.
instance Lift Version where
lift (Version {..}) = [| Version {..} |]
instance Lift VUnit where
lift (Digits w) = [| Digits w |]
lift (Str t) = AppE <$> [| Str . T.pack |] <*> TH.lift (T.unpack t)
qq :: (Text -> Q Exp) -> QuasiQuoter
qq quoteExp' =
QuasiQuoter
{ quoteExp = (\s -> quoteExp' . T.pack $ s)
, quotePat = \_ ->
fail "illegal QuasiQuote (allowed as expression only, used as a pattern)"
, quoteType = \_ ->
fail "illegal QuasiQuote (allowed as expression only, used as a type)"
, quoteDec = \_ ->
fail "illegal QuasiQuote (allowed as expression only, used as a declaration)"
}
ver :: QuasiQuoter
ver = qq mkV
mkV :: Text -> Q Exp
mkV t = case version t of
Left e -> error . show $ e
Right v -> lift v
{-
error:
• Couldn't match type ‘t0 m0’ with ‘Q’
Expected type: Q Exp
Actual type: t0 m0 Exp
• In the expression: lift v
In a case alternative: Right v -> lift v
In the expression:
case version t of
Left e -> error . show $ e
Right v -> lift v
|
137 | Right v -> lift v
error:
• Couldn't match expected type ‘m0 Exp’ with actual type ‘Version’
• In the first argument of ‘lift’, namely ‘v’
In the expression: lift v
In a case alternative: Right v -> lift v
|
137 | Right v -> lift v
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment