Skip to content

Commit

Permalink
Increasing compiler warning level and resolving potential warnings. (#66
Browse files Browse the repository at this point in the history
)

* Resolve ghc warnings and applying stylish-haskell.
  • Loading branch information
mmzx committed Nov 9, 2019
1 parent bda859c commit e122428
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 110 deletions.
2 changes: 1 addition & 1 deletion Setup.hs
@@ -1,2 +1,2 @@
import Distribution.Simple
import Distribution.Simple
main = defaultMain
8 changes: 4 additions & 4 deletions app/Main.hs
Expand Up @@ -34,10 +34,10 @@ main = do
Left err -> do
hPutStrLn stderr err
exitFailure
Right er -> let dotted = dotER conf er
Right er -> let erDot = dotER conf er
toFile h = SB.hGetContents h >>= SB.hPut (snd $ cout conf)
fmt = fromMaybe Pdf (outfmt conf)
in graphvizWithHandle Dot dotted fmt toFile
in graphvizWithHandle Dot erDot fmt toFile
hClose (snd $ cin conf)
hClose (snd $ cout conf)

Expand Down Expand Up @@ -71,9 +71,9 @@ htmlEntity e = H.Table H.HTable
}
where rows = headerRow : map htmlAttr (attribs e)
headerRow = H.Cells [H.LabelCell [] $ H.Text text]
text = withLabelFmt " [%s]" (hoptions e) $ bold hname
text = withLabelFmt " [%s]" (hoptions e) $ boldFont hname
hname = htmlFont (hoptions e) (name e)
bold s = [H.Format H.Bold s]
boldFont s = [H.Format H.Bold s]

-- | Extracts and formats a graph title from the options given.
-- The options should be title options from an ER value.
Expand Down
6 changes: 2 additions & 4 deletions erd.cabal
Expand Up @@ -75,7 +75,7 @@ executable erd
Erd.Render
Text.Parsec.Erd.Parser
main-is: Main.hs
ghc-options: -Wall -fno-warn-name-shadowing -fno-warn-unused-do-bind
ghc-options: -Wall -W
build-depends: base >= 4.5 && <5
, directory >= 1.3.3.0
, filepath >= 1.4.2.1
Expand All @@ -87,13 +87,11 @@ executable erd
, yaml >= 0.11.0.0
, raw-strings-qq >= 1.1



test-suite spec
default-language: Haskell2010
type: exitcode-stdio-1.0
main-is: Spec.hs
ghc-options: -Wall -fno-warn-name-shadowing -fno-warn-unused-do-bind
ghc-options: -Wall -W
hs-source-dirs:
test, src
other-modules:
Expand Down
10 changes: 4 additions & 6 deletions src/Erd/Config.hs
Expand Up @@ -120,7 +120,7 @@ opts =
(O.ReqArg (\fpath cIO -> do
c <- cIO
i <- openFile fpath ReadMode
return $ c { cin = (fpath, i) }
return $ c {cin = (fpath, i)}
)
"FILE")
("When set, input will be read from the given file.\n"
Expand All @@ -129,7 +129,7 @@ opts =
(O.ReqArg (\fpath cIO -> do
c <- cIO
o <- openFile fpath WriteMode
return $ c { cout = (fpath, o) }
return $ c {cout = (fpath, o)}
)
"FILE")
("When set, output will be written to the given file.\n"
Expand All @@ -147,8 +147,7 @@ opts =
Nothing -> do
ef "'%s' is not a valid output format." fmt
exitFailure
Just gfmt ->
return $ c { outfmt = Just gfmt }
Just gfmt -> return c {outfmt = Just gfmt}
)
"FMT")
(printf "Force the output format to one of:\n%s"
Expand All @@ -161,8 +160,7 @@ opts =
Nothing -> do
ef "'%s' is not a valid type of edge." edge
exitFailure
Just edgeType -> do
return $ c { edgeType = Just edgeType }
Just x -> return c {edgeType = Just x}
)
"EDGE")
(printf "Select one type of edge:\n%s"
Expand Down
57 changes: 27 additions & 30 deletions src/Erd/ER.hs
Expand Up @@ -10,41 +10,38 @@ module Erd.ER
)
where

import qualified Data.Map as M
import Data.Maybe (mapMaybe)
import Data.Text.Lazy
import Data.Word (Word8)
import Text.Printf (printf)
import qualified Data.Map as M
import Data.Maybe (mapMaybe)
import Data.Text.Lazy
import Data.Word (Word8)
import Text.Printf (printf)

import Data.GraphViz.Parsing (ParseDot, parse, runParser)
import qualified Data.GraphViz.Attributes.HTML as H
import Data.GraphViz.Attributes.Colors (Color)
import Data.GraphViz.Attributes.Colors (Color)
import qualified Data.GraphViz.Attributes.HTML as H
import Data.GraphViz.Parsing (ParseDot, parse, runParser)

-- | Represents a single schema.
data ER = ER { entities :: [Entity]
, rels :: [Relation]
, title :: Options
}
deriving (Show, Eq)
, rels :: [Relation]
, title :: Options
} deriving (Show, Eq)

-- | Represents a single entity in a schema.
data Entity = Entity { name :: Text
, attribs :: [Attribute]
data Entity = Entity { name :: Text
, attribs :: [Attribute]
, hoptions :: Options
, eoptions :: Options
}
deriving (Show, Eq)
} deriving (Show, Eq)

instance Ord Entity where
e1 `compare` e2 = name e1 `compare` name e2

-- | Represents a single attribute in a particular entity.
data Attribute = Attribute { field :: Text
, pk :: Bool
, fk :: Bool
data Attribute = Attribute { field :: Text
, pk :: Bool
, fk :: Bool
, aoptions :: Options
}
deriving (Show, Eq)
} deriving (Show, Eq)

instance Ord Attribute where
a1 `compare` a2 = field a1 `compare` field a2
Expand Down Expand Up @@ -105,15 +102,15 @@ optionParse :: ParseDot a => (a -> Option) -> String -> Either String Option
optionParse con s =
case fst $ runParser parse quoted of
Left err -> Left (printf "%s (bad value '%s')" err s)
Right a -> Right (con a)
Right a -> Right (con a)
where quoted = "\"" `append` pack s `append` "\""

-- | Selects an option if and only if it corresponds to a font attribute.
optToFont :: Option -> Maybe H.Attribute
optToFont (Color c) = Just $ H.Color c
optToFont (Color c) = Just $ H.Color c
optToFont (FontFace s) = Just $ H.Face s
optToFont (FontSize d) = Just $ H.PointSize d
optToFont _ = Nothing
optToFont _ = Nothing

-- | Selects an option if and only if it corresponds to an HTML attribute.
-- In particular, for tables or table cells.
Expand All @@ -131,7 +128,7 @@ optToHtml _ = Nothing
-- | Selects an option if and only if it corresponds to a label.
optToLabel :: Option -> Maybe Text
optToLabel (Label s) = Just $ pack s
optToLabel _ = Nothing
optToLabel _ = Nothing

-- | Represents a relationship between exactly two entities. After parsing,
-- each `rel` is guaranteed to correspond to an entity defined in the same
Expand All @@ -141,7 +138,7 @@ optToLabel _ = Nothing
-- Those cardinalities are: 0 or 1, exactly 1, 0 or more and 1 or more.
data Relation = Relation { entity1, entity2 :: Text
, card1, card2 :: Cardinality
, roptions :: Options
, roptions :: Options
}
deriving (Show, Eq)

Expand All @@ -151,18 +148,18 @@ data Cardinality = ZeroOne
| OnePlus deriving (Eq)

instance Show Cardinality where
show ZeroOne = "{0,1}"
show One = "1"
show ZeroOne = "{0,1}"
show One = "1"
show ZeroPlus = "0..N"
show OnePlus ="1..N"
show OnePlus = "1..N"

-- | Maps a string representation to a particular relationship cardinality.
cardByName :: Char -> Maybe Cardinality
cardByName '?' = Just ZeroOne
cardByName '1' = Just One
cardByName '*' = Just ZeroPlus
cardByName '+' = Just OnePlus
cardByName _ = Nothing
cardByName _ = Nothing

-- | Hard-coded default options for all graph titles.
defaultTitleOpts :: Options
Expand Down
32 changes: 16 additions & 16 deletions src/Erd/Parse.hs
Expand Up @@ -4,25 +4,25 @@ module Erd.Parse
)
where

import Control.Monad (when)
import Data.List (find)
import Data.Maybe
import Data.Text.Lazy hiding (find, map, reverse)
import Data.Text.Lazy.IO
import System.IO (Handle)
import Text.Parsec
import Text.Printf (printf)
import Text.Parsec.Erd.Parser (AST(..), GlobalOptions(..), document)
import Erd.ER

import Erd.ER
import Control.Monad (when)
import Data.List (find)
import Data.Maybe
import Data.Text.Lazy hiding (find, map, reverse)
import Data.Text.Lazy.IO
import System.IO (Handle)
import Text.Parsec
import Text.Parsec.Erd.Parser (AST (..), GlobalOptions (..), document)
import Text.Printf (printf)

loadER :: String -> Handle -> IO (Either String ER)
loadER fpath f = do
s <- hGetContents f
case parse (do { (opts, ast) <- document; return $ toER opts ast}) fpath s of
Left err -> return $ Left $ show err
Left err -> return $ Left $ show err
Right err@(Left _) -> return err
Right (Right er) -> return $ Right er
Right (Right er) -> return $ Right er

-- | Converts a list of syntactic categories in an entity-relationship
-- description to an ER representation. If there was a problem with the
Expand All @@ -32,14 +32,14 @@ loadER fpath f = do
-- This preserves the ordering of the syntactic elements in the original
-- description.
toER :: GlobalOptions -> [AST] -> Either String ER
toER gopts = toER' (ER [] [] title)
where title = gtoptions gopts `mergeOpts` defaultTitleOpts
toER gopts = toER' (ER [] [] erTitle)
where erTitle = gtoptions gopts `mergeOpts` defaultTitleOpts

toER' :: ER -> [AST] -> Either String ER
toER' er [] = Right (reversed er) >>= validRels
toER' ER { entities = [] } (A a:_) =
let name = show (field a)
in Left $ printf "Attribute '%s' comes before first entity." name
let fieldName = show (field a)
in Left $ printf "Attribute '%s' comes before first entity." fieldName
toER' er@ER { entities = e':es } (A a:xs) = do
let e = e' { attribs = a:attribs e' }
toER' (er { entities = e:es }) xs
Expand Down
53 changes: 25 additions & 28 deletions src/Text/Parsec/Erd/Parser.hs
@@ -1,28 +1,26 @@
{-# LANGUAGE OverloadedStrings #-}

module Text.Parsec.Erd.Parser
(
AST(..),
( AST(..),
GlobalOptions(..),
document,
globalOptions,
entity,
rel,
attr,
comment,
)
where

import Control.Monad (liftM2, when, void)
import Data.Char (isAlphaNum, isSpace, isControl)
import qualified Data.GraphViz.Attributes.HTML as H
import qualified Data.Map as M
import Data.Maybe
import Data.Text.Lazy
import Text.Parsec
import Text.Parsec.Text.Lazy
import Text.Printf (printf)

import Erd.ER
comment
) where

import Erd.ER

import Control.Monad (liftM2, void, when)
import Data.Char (isAlphaNum, isControl, isSpace)
import qualified Data.Map as M
import Data.Maybe
import Data.Text.Lazy
import Text.Parsec
import Text.Parsec.Text.Lazy
import Text.Printf (printf)

data AST = E Entity
| A Attribute
Expand Down Expand Up @@ -68,31 +66,30 @@ attr = do
eolComment
return
$ Just
$ A Attribute { field = n, pk = ispk, fk = isfk, aoptions = opts <> defaultAttrOpts}
$ A Attribute {field = n, pk = ispk, fk = isfk, aoptions = opts <> defaultAttrOpts}

rel :: Parser (Maybe AST)
rel = do
let ops = "?1*+"
e1 <- ident
op1 <- oneOf ops
string "--"
_ <- string "--"
op2 <- oneOf ops
e2 <- ident
opts <- options

let getCard op =
case cardByName op of
Just t -> return t
Nothing -> unexpected (printf "Cardinality '%s' does not exist." op)
t1 <- getCard op1
t2 <- getCard op2
return $ Just $ R Relation { entity1 = e1, entity2 = e2
, card1 = t1, card2 = t2, roptions = opts }
, card1 = t1, card2 = t2, roptions = opts }

globalOptions :: GlobalOptions -> Parser GlobalOptions
globalOptions gopts =
option gopts $ try $ do
n <- ident
n <- ident
opts <- options
case n of
"title" -> emptiness >> globalOptions (gopts { gtoptions = opts})
Expand All @@ -111,19 +108,19 @@ options =

opt :: Parser (String, Option)
opt = do
name <- liftM2 (:) letter (manyTill (letter <|> char '-') (char ':'))
optName <- liftM2 (:) letter (manyTill (letter <|> char '-') (char ':'))
<?> "option name"
emptiness
value <- between (char '"') (char '"') (many $ noneOf "\"")
<?> "option value"
case optionByName name value of
case optionByName optName value of
Left err -> fail err
Right o' -> emptiness >> return (name, o')
Right o' -> emptiness >> return (optName, o')

comment :: Parser (Maybe AST)
comment = do
char '#'
manyTill anyChar $ try eol
_ <- char '#'
_ <- manyTill anyChar $ try eol
return Nothing

ident :: Parser Text
Expand All @@ -139,7 +136,7 @@ identQuoted = do
let p = satisfy (\c -> c /= quote && not (isControl c) )
<?> "any character except " ++ [quote] ++ " or control characters"
n <- fmap pack (many1 p)
char quote
_ <- char quote
return n

identNoSpace :: Parser Text
Expand Down
1 change: 0 additions & 1 deletion test/Test/Erd/Render.hs
Expand Up @@ -8,7 +8,6 @@ import qualified Erd.ER as ER
import Erd.Render (htmlAttr)

import qualified Data.GraphViz.Attributes.HTML as H
import qualified Data.Map as M
import Test.Tasty
import Test.Tasty.HUnit

Expand Down

0 comments on commit e122428

Please sign in to comment.