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 import Annotator.Annotation
version :: Vsn.Version version :: Vsn.Version
version = Vsn.makeVersion [0,0,4,0] version = Vsn.makeVersion [0,0,4,1]
type App a = ReaderT Options IO a type App a = ReaderT Options IO a

View file

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