Refactor how automatic annotations are read

This commit is contained in:
Alexander Kobjolke 2022-03-08 23:07:42 +01:00
parent 4514d0f120
commit 9da29e683a
3 changed files with 12 additions and 6 deletions

View file

@ -30,7 +30,7 @@ import Annotator.Rule
import Annotator.Annotation
version :: Vsn.Version
version = Vsn.makeVersion [0,0,4,0]
version = Vsn.makeVersion [0,0,4,1]
type App a = ReaderT Options IO a

View file

@ -2,7 +2,9 @@
module Annotator.Annotation (Annotation(..), fromFile) where
import Data.Char (isSpace)
import Annotator.Rule
import Annotator.Util
data Annotation = Intentional !Rule !String
| FalsePositive !Rule !String
@ -10,14 +12,14 @@ data Annotation = Intentional !Rule !String
deriving (Show, Read, Eq)
fromFile :: FilePath -> IO [Either String Annotation]
fromFile fn = fmap safeRead . removeBoring . lines <$> readFile fn
fromFile fn = fmap safeRead . removeBoring . map (dropWhile isSpace) . lines <$> readFile fn
where
removeBoring :: [String] -> [String]
removeBoring = filter (not . isBoring)
removeBoring = filter (not . boring)
isBoring x = isComment x || isEmpty x
isComment x = take 1 x == "#" || take 2 x == "--"
isEmpty = null . dropWhile isSpace
boring = anyp [comment, null]
comment = anyp [startsWith "#", startsWith "--"]
startsWith prefix = (== prefix) . take (length prefix)
safeRead :: String -> Either String Annotation
safeRead s = case reads s of

4
src/Annotator/Util.hs Normal file
View file

@ -0,0 +1,4 @@
module Annotator.Util where
anyp :: [a -> Bool] -> a -> Bool
anyp preds x = or (map ($ x) preds)