diff options
author | defanor <defanor@uberspace.net> | 2017-12-16 18:39:15 +0300 |
---|---|---|
committer | defanor <defanor@uberspace.net> | 2017-12-16 18:48:41 +0300 |
commit | 9a0b116cd7a78590cd46bb17bbd542d93de2acef (patch) | |
tree | 46c66d357fb731f0a0e32159325214391bc63a90 | |
parent | 6c55be7a476d04db91881bc8870d2a1e838ea521 (diff) |
Add the 'handleRecCon' function
Abstract the code with it.
-rw-r--r-- | Coalpit.hs | 44 | ||||
-rw-r--r-- | coalpit.cabal | 11 |
2 files changed, 25 insertions, 30 deletions
@@ -171,6 +171,21 @@ instance (GCoalpit a, GCoalpit b) => GCoalpit (a :*: b) where -- Sums +-- | Handles recursive constructors. +handleRecCon :: GCoalpit a + => String + -- ^ Constructor name + -> Options + -> [String] + -> Proxy (a p) + -> String +handleRecCon nameA opt path (Proxy :: Proxy (a p)) = conNameMod opt nameA + ++ if nameA `elem` path + then "..." + else case gArgHelper opt (nameA : path) (Proxy :: Proxy (a p)) of + "" -> "" + s -> ' ' : s + instance (Constructor conA, GCoalpit a, GCoalpit (b :+: c)) => GCoalpit ((b :+: c) :+: C1 conA a) where gArgParser opt = @@ -185,14 +200,9 @@ instance (Constructor conA, GCoalpit a, GCoalpit (b :+: c)) => in concat [ "(" , gArgHelper opt path (Proxy :: Proxy ((b :+: c) p)) , " | " - , conNameMod opt nameA - , if nameA `elem` path - then "..." - else spaceNonEmpty $ - gArgHelper opt (nameA : path) (Proxy :: Proxy (a p)) + , handleRecCon nameA opt path (Proxy :: Proxy (a p)) , ")"] - instance (Constructor conA, GCoalpit a, GCoalpit (b :+: c)) => GCoalpit (C1 conA a :+: (b :+: c)) where gArgParser opt = @@ -205,11 +215,7 @@ instance (Constructor conA, GCoalpit a, GCoalpit (b :+: c)) => gArgHelper opt path (Proxy :: Proxy ((C1 conA a :+: (b :+: c)) p)) = let nameA = conName (undefined :: C1 conA a p) in concat [ "(" - , conNameMod opt nameA - , if nameA `elem` path - then "..." - else spaceNonEmpty $ - gArgHelper opt (nameA : path) (Proxy :: Proxy (a p)) + , handleRecCon nameA opt path (Proxy :: Proxy (a p)) , " | " , gArgHelper opt path (Proxy :: Proxy ((b :+: c) p)) , ")"] @@ -230,23 +236,11 @@ instance (Constructor conA, Constructor conB, GCoalpit a, GCoalpit b) => let nameA = conName (undefined :: C1 conA a p) nameB = conName (undefined :: C1 conB b p) in concat [ "(" - , conNameMod opt nameA - , if nameA `elem` path - then "..." - else spaceNonEmpty $ - gArgHelper opt (nameA : path) (Proxy :: Proxy (a p)) + , handleRecCon nameA opt path (Proxy :: Proxy (a p)) , " | " - , conNameMod opt nameB - , if nameB `elem` path - then "..." - else spaceNonEmpty $ - gArgHelper opt (nameB : path) (Proxy :: Proxy (b p)) + , handleRecCon nameB opt path (Proxy :: Proxy (b p)) , ")"] -spaceNonEmpty :: String -> String -spaceNonEmpty "" = "" -spaceNonEmpty s = ' ' : s - -- Record Selectors diff --git a/coalpit.cabal b/coalpit.cabal index 38e8518..51507e0 100644 --- a/coalpit.cabal +++ b/coalpit.cabal @@ -1,11 +1,12 @@ name: coalpit version: 0.1.0.2 synopsis: Command-line options parsing and printing -description: This library generates parsers and printers for given - data types, in the form of command-line arguments -- so - that they can be used to quickly get program interfaces via - a shared library, while being suitable for scripting and as - user interfaces. +description: This library generates parsers and printers for + given data types, in the form of command-line + arguments – so that they can be used to quickly + get program interfaces via a shared library, + while being suitable for scripting and as user + interfaces. license: BSD3 license-file: LICENSE author: defanor |