Skip to content

Instantly share code, notes, and snippets.

@lgastako
Created August 18, 2019 04:58
Show Gist options
  • Save lgastako/a471487c14fb4d9ba83110a5180b149b to your computer and use it in GitHub Desktop.
Save lgastako/a471487c14fb4d9ba83110a5180b149b to your computer and use it in GitHub Desktop.
{-# LANGUAGE LambdaCase #-}
module Loupe
( analyze
, analyze'
, analyzeMay
) where
import Language.Haskell.Exts.Parser
import Language.Haskell.Exts.SrcLoc
import Language.Haskell.Exts.Syntax
analyzeMay :: String -> Maybe String
analyzeMay = either (const Nothing) Just . analyze
analyze' :: String -> String
analyze' = either id id . analyze
analyze :: String -> Either String String
analyze s = case parse (prepare s) of
ParseFailed srcLoc errMsg -> Left $ renderError srcLoc errMsg
ParseOk result -> case result :: Module SrcSpanInfo of
XmlPage {} -> Left "Expected Module but got XmlPage."
XmlHybrid {} -> Left "Expected Module but got XmlHybrid."
Module _ _ _ _ (decl:_) -> case decl of
TypeSig _ _ type' -> handleTypeSig (unitize type')
other -> Left $ "Expected typeSig, got: " ++ show other
other -> error $ "Handling not implemented for: " ++ show other
unitize :: Functor f => f a -> f ()
unitize = fmap . const $ ()
handleTypeSig :: Show a => Type a -> Either String String
handleTypeSig type' = error "do something with type' here"
prepare :: String -> String
prepare typeSig = unlines
[ "module LoupeSpace where"
, "x :: " ++ typeSig
, "x = undefined"
]
renderError :: SrcLoc -> String -> String
renderError _srcLoc errMsg = "renderError not fully impl:" ++ errMsg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment