Skip to content

Commit

Permalink
Give Notice a parameter for the doc highlights.
Browse files Browse the repository at this point in the history
  • Loading branch information
robrix committed Oct 18, 2020
1 parent b0231be commit 7263883
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
1 change: 1 addition & 0 deletions fused-effects-parser.cabal
Expand Up @@ -69,6 +69,7 @@ test-suite test
, fused-effects-parser
, hedgehog ^>= 1
, parsers
, prettyprinter-ansi-terminal
, tasty ^>= 1.2
, tasty-hedgehog ^>= 1
, tasty-hunit ^>= 0.10
8 changes: 4 additions & 4 deletions src/Control/Carrier/Parser/Church.hs
Expand Up @@ -58,17 +58,17 @@ import Text.Parser.Char (CharParsing(..))
import Text.Parser.Combinators
import Text.Parser.Token (TokenParsing)

runParserWithString :: Has (Throw Notice.Notice) sig m => Pos -> String -> ParserC m a -> m a
runParserWithString :: Has (Throw (Notice.Notice AnsiStyle)) sig m => Pos -> String -> ParserC m a -> m a
runParserWithString pos str = runParserWith Nothing (Input pos str)
{-# INLINE runParserWithString #-}

runParserWithFile :: (Has (Throw Notice.Notice) sig m, MonadIO m) => FilePath -> ParserC m a -> m a
runParserWithFile :: (Has (Throw (Notice.Notice AnsiStyle)) sig m, MonadIO m) => FilePath -> ParserC m a -> m a
runParserWithFile path p = do
input <- liftIO (readFile path)
runParserWith (Just path) (Input (Pos 0 0) input) p
{-# INLINE runParserWithFile #-}

runParserWith :: Has (Throw Notice.Notice) sig m => Maybe FilePath -> Input -> ParserC m a -> m a
runParserWith :: Has (Throw (Notice.Notice AnsiStyle)) sig m => Maybe FilePath -> Input -> ParserC m a -> m a
runParserWith path input = runParser (const pure) failure failure input
where
src = sourceFromString path (str input)
Expand Down Expand Up @@ -305,7 +305,7 @@ expected_ :: Lens' Err (Set String)
expected_ = lens expected $ \ i expected -> i{ expected }
{-# INLINE expected_ #-}

errToNotice :: Source -> Err -> Notice.Notice
errToNotice :: Source -> Err -> Notice.Notice AnsiStyle
errToNotice source Err{ input = Input pos _, reason, expected } = Notice.Notice
{ level = Just Notice.Error
, excerpt = Excerpt (Source.path source) (source ! pos) (Span pos pos)
Expand Down
2 changes: 1 addition & 1 deletion src/Control/Effect/Parser/GHCI.hs
Expand Up @@ -14,7 +14,7 @@ import Text.Parser.Char
import Text.Parser.Combinators
import Text.Parser.Token

parse :: Show a => ParserC (Either Notice) a -> String -> IO ()
parse :: Show a => ParserC (Either (Notice AnsiStyle)) a -> String -> IO ()
parse p s = do
let v = runParserWithString (Pos 0 0) s p
either (putDoc . (<> line) . prettyNotice) print v
Expand Down
16 changes: 8 additions & 8 deletions src/Control/Effect/Parser/Notice.hs
Expand Up @@ -31,27 +31,27 @@ prettyLevel = \case
Error -> red (pretty "error")


data Notice = Notice
data Notice a = Notice
{ level :: !(Maybe Level)
, excerpt :: {-# UNPACK #-} !Excerpt
, reason :: !(Doc AnsiStyle)
, context :: ![Doc AnsiStyle]
, reason :: !(Doc a)
, context :: ![Doc a]
}
deriving (Show)

level_ :: Lens' Notice (Maybe Level)
level_ :: Lens' (Notice AnsiStyle) (Maybe Level)
level_ = lens level $ \ n level -> n{ level }

excerpt_ :: Lens' Notice Excerpt
excerpt_ :: Lens' (Notice AnsiStyle) Excerpt
excerpt_ = lens excerpt $ \ n excerpt -> n{ excerpt }

reason_ :: Lens' Notice (Doc AnsiStyle)
reason_ :: Lens' (Notice AnsiStyle) (Doc AnsiStyle)
reason_ = lens reason $ \ n reason -> n{ reason }

context_ :: Lens' Notice [Doc AnsiStyle]
context_ :: Lens' (Notice AnsiStyle) [Doc AnsiStyle]
context_ = lens context $ \ n context -> n{ context }

prettyNotice :: Notice -> Doc AnsiStyle
prettyNotice :: Notice AnsiStyle -> Doc AnsiStyle
prettyNotice (Notice level (Excerpt path line span) reason context) = vsep
( nest 2 (group (fillSep
[ bold (pretty (fromMaybe "(interactive)" path)) <> colon <> pos (Span.start span) <> colon <> foldMap ((space <>) . (<> colon) . prettyLevel) level
Expand Down
3 changes: 2 additions & 1 deletion test/Test.hs
Expand Up @@ -16,6 +16,7 @@ import Hedgehog
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
import Prelude hiding (lines)
import Prettyprinter.Render.Terminal (AnsiStyle)
import Test.Tasty
import Test.Tasty.Hedgehog
import Test.Tasty.HUnit
Expand Down Expand Up @@ -73,7 +74,7 @@ parserTests = testGroup "ParserC (Church)"
]


parsesInto :: (Eq a, Show a) => ParserC (Either Notice) a -> String -> a -> Assertion
parsesInto :: (Eq a, Show a) => ParserC (Either (Notice AnsiStyle)) a -> String -> a -> Assertion
parsesInto p s expected = case runParserWithString (Pos 0 0) s p of
Left err -> assertFailure (show err)
Right actual -> actual @?= expected
Expand Down

0 comments on commit 7263883

Please sign in to comment.