From 060b986c641cd496395b2d13dc316fc84462a7a4 Mon Sep 17 00:00:00 2001 From: Ɓukasz Hanuszczak Date: Wed, 15 Jul 2015 20:25:41 +0200 Subject: Implement tuple syntax sugaring logic for specialized types. --- .../src/Haddock/Backends/Xhtml/Specialize.hs | 36 ++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'haddock-api/src/Haddock/Backends/Xhtml') diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs index 30501a13..a2cb8799 100644 --- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs +++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs @@ -15,6 +15,7 @@ import Haddock.Syb import GHC import Name +import Control.Monad import Data.Data @@ -64,5 +65,36 @@ sugarListsStep (HsAppTy (L _ (HsTyVar name)) ltyp) sugarListsStep typ = typ -sugarTuples :: HsType name -> HsType name -sugarTuples = id +sugarTuples :: forall name. (NamedThing name, DataId name) + => HsType name -> HsType name +sugarTuples = everywhere $ + mkT (sugarTuplesStep :: HsType name -> HsType name) + + +sugarTuplesStep :: NamedThing name => HsType name -> HsType name +sugarTuplesStep typ = + aux [] typ + where + aux apps (HsAppTy (L _ ftyp) atyp) = aux (atyp:apps) ftyp + aux apps (HsParTy (L _ typ')) = aux apps typ' + aux apps (HsTyVar name) + | isBuiltInSyntax name' && suitable = HsTupleTy HsBoxedTuple apps + where + name' = getName name + strName = occNameString . nameOccName $ name' + suitable = case parseTupleArity strName of + Just arity -> arity == length apps + Nothing -> False + aux _ _ = typ + + +parseTupleArity :: String -> Maybe Int +parseTupleArity ('(':commas) = do + n <- parseCommas commas + guard $ n /= 0 + return $ n + 1 + where + parseCommas (',':rest) = (+ 1) <$> parseCommas rest + parseCommas ")" = Just 0 + parseCommas _ = Nothing +parseTupleArity _ = Nothing -- cgit v1.2.3