trifecta 0.12 → 0.13
raw patch · 6 files changed
+125/−118 lines, 6 files
Files
- Text/Trifecta.hs +2/−2
- Text/Trifecta/It.hs +0/−97
- Text/Trifecta/Parser/Class.hs +3/−3
- Text/Trifecta/Parser/It.hs +97/−0
- Text/Trifecta/Parser/Prim.hs +21/−14
- trifecta.cabal +2/−2
Text/Trifecta.hs view
@@ -4,7 +4,7 @@ , module Text.Trifecta.Diagnostic , module Text.Trifecta.Diagnostic.Level , module Text.Trifecta.Hunk- , module Text.Trifecta.It+ , module Text.Trifecta.Parser.It , module Text.Trifecta.Parser.Char , module Text.Trifecta.Parser.Class , module Text.Trifecta.Parser.Err@@ -26,7 +26,7 @@ import Text.Trifecta.Diagnostic import Text.Trifecta.Diagnostic.Level import Text.Trifecta.Hunk-import Text.Trifecta.It+import Text.Trifecta.Parser.It import Text.Trifecta.Parser.Char import Text.Trifecta.Parser.Class import Text.Trifecta.Parser.Err
− Text/Trifecta/It.hs
@@ -1,97 +0,0 @@-{-# LANGUAGE MultiParamTypeClasses, BangPatterns, MagicHash, UnboxedTuples #-}-module Text.Trifecta.It - ( It(Pure, It, result)- , needIt- , wantIt- , fillIt- , lineIt- , sliceIt- , stepIt- ) where--import Control.Applicative-import Control.Comonad-import Control.Monad-import Data.Monoid-import Data.ByteString as Strict-import Data.ByteString.Lazy as Lazy-import Data.Functor.Bind-import Data.Functor.Plus-import Text.Trifecta.Rope as Rope-import Text.Trifecta.Delta-import Text.Trifecta.Bytes-import Text.Trifecta.Util.MaybePair-import Text.Trifecta.Parser.Step--data It a- = Pure { result :: a } - | It { result :: a, _it :: Rope -> It a }--instance Functor It where- fmap f (Pure a) = Pure (f a)- fmap f (It a k) = It (f a) (fmap f . k)--instance Applicative It where- pure = Pure- Pure f <*> Pure a = Pure (f a)- Pure f <*> It a ka = It (f a) (fmap f . ka)- It f kf <*> Pure a = It (f a) (fmap ($a) . kf)- It f kf <*> It a ka = It (f a) (\r -> kf r <*> ka r)--instance Monad It where- return = Pure- Pure a >>= f = f a- It a k >>= f = It (result (f a)) (k >=> f)--instance Apply It where (<.>) = (<*>) -instance Bind It where (>>-) = (>>=) --instance Extend It where- duplicate p@Pure{} = Pure p- duplicate p@(It _ k) = It p (duplicate . k)-- extend f p@Pure{} = Pure (f p)- extend f p@(It _ k) = It (f p) (extend f . k)--instance Comonad It where- extract = result--needIt :: a -> (Rope -> Maybe a) -> It a-needIt z f = k where - k = It z $ \r -> case f r of - Just a -> Pure a- Nothing -> k--wantIt :: a -> (Rope -> (# Bool, a #)) -> It a-wantIt z f = It z k where - k r = case f r of- (# False, a #) -> It a k- (# True, a #) -> Pure a---- given a position, go there, and grab the text forward from that point-fillIt :: Delta -> It (MaybePair Delta Strict.ByteString)-fillIt n = wantIt NothingPair $ \r -> - (# bytes n < bytes (rewind (delta r))- , grabLine n r NothingPair JustPair #) - --- return the text of the line that contains a given position-lineIt :: Delta -> It (Maybe Strict.ByteString)-lineIt n = wantIt Nothing $ \r -> - (# bytes n < bytes (rewind (delta r))- , grabLine n r Nothing (const Just) #)--sliceIt :: Delta -> Delta -> It Strict.ByteString-sliceIt !i !j = wantIt mempty $ \r -> - (# bytes j < bytes (rewind (delta r))- , grabRest i r mempty $ const $ - Strict.concat . - Lazy.toChunks . - Lazy.take (fromIntegral (bj - bi)) #)- where- bi = bytes i- bj = bytes j--stepIt :: It a -> Step e a-stepIt = go mempty where- go r (Pure a) = StepDone r mempty a- go r (It a k) = StepCont r (pure a) (\s -> go s (k s))
Text/Trifecta/Parser/Class.hs view
@@ -10,11 +10,11 @@ import Control.Monad (MonadPlus(..)) import Control.Monad.Trans.Class import Control.Monad.Trans.State.Lazy-import Text.Trifecta.It-import Text.Trifecta.Delta-import Data.Semigroup import Data.ByteString as Strict+import Data.Semigroup import Data.Set as Set+import Text.Trifecta.Delta+import Text.Trifecta.Parser.It class ( Alternative m, MonadPlus m) => MonadParser m where satisfy :: (Char -> Bool) -> m Char
+ Text/Trifecta/Parser/It.hs view
@@ -0,0 +1,97 @@+{-# LANGUAGE MultiParamTypeClasses, BangPatterns, MagicHash, UnboxedTuples #-}+module Text.Trifecta.Parser.It + ( It(Pure, It, result)+ , needIt+ , wantIt+ , fillIt+ , lineIt+ , sliceIt+ , stepIt+ ) where++import Control.Applicative+import Control.Comonad+import Control.Monad+import Data.Monoid+import Data.ByteString as Strict+import Data.ByteString.Lazy as Lazy+import Data.Functor.Bind+import Data.Functor.Plus+import Text.Trifecta.Rope as Rope+import Text.Trifecta.Delta+import Text.Trifecta.Bytes+import Text.Trifecta.Util.MaybePair+import Text.Trifecta.Parser.Step++data It a+ = Pure { result :: a } + | It { result :: a, _it :: Rope -> It a }++instance Functor It where+ fmap f (Pure a) = Pure (f a)+ fmap f (It a k) = It (f a) (fmap f . k)++instance Applicative It where+ pure = Pure+ Pure f <*> Pure a = Pure (f a)+ Pure f <*> It a ka = It (f a) (fmap f . ka)+ It f kf <*> Pure a = It (f a) (fmap ($a) . kf)+ It f kf <*> It a ka = It (f a) (\r -> kf r <*> ka r)++instance Monad It where+ return = Pure+ Pure a >>= f = f a+ It a k >>= f = It (result (f a)) (k >=> f)++instance Apply It where (<.>) = (<*>) +instance Bind It where (>>-) = (>>=) ++instance Extend It where+ duplicate p@Pure{} = Pure p+ duplicate p@(It _ k) = It p (duplicate . k)++ extend f p@Pure{} = Pure (f p)+ extend f p@(It _ k) = It (f p) (extend f . k)++instance Comonad It where+ extract = result++needIt :: a -> (Rope -> Maybe a) -> It a+needIt z f = k where + k = It z $ \r -> case f r of + Just a -> Pure a+ Nothing -> k++wantIt :: a -> (Rope -> (# Bool, a #)) -> It a+wantIt z f = It z k where + k r = case f r of+ (# False, a #) -> It a k+ (# True, a #) -> Pure a++-- given a position, go there, and grab the text forward from that point+fillIt :: Delta -> It (MaybePair Delta Strict.ByteString)+fillIt n = wantIt NothingPair $ \r -> + (# bytes n < bytes (rewind (delta r))+ , grabLine n r NothingPair JustPair #) + +-- return the text of the line that contains a given position+lineIt :: Delta -> It (Maybe Strict.ByteString)+lineIt n = wantIt Nothing $ \r -> + (# bytes n < bytes (rewind (delta r))+ , grabLine n r Nothing (const Just) #)++sliceIt :: Delta -> Delta -> It Strict.ByteString+sliceIt !i !j = wantIt mempty $ \r -> + (# bytes j < bytes (rewind (delta r))+ , grabRest i r mempty $ const $ + Strict.concat . + Lazy.toChunks . + Lazy.take (fromIntegral (bj - bi)) #)+ where+ bi = bytes i+ bj = bytes j++stepIt :: It a -> Step e a+stepIt = go mempty where+ go r (Pure a) = StepDone r mempty a+ go r (It a k) = StepCont r (pure a) (\s -> go s (k s))
Text/Trifecta/Parser/Prim.hs view
@@ -2,7 +2,8 @@ module Text.Trifecta.Parser.Prim ( Parser(..) , why--- , stepParser+ , stepParser+ , parseTest ) where import Control.Applicative@@ -11,25 +12,25 @@ import Control.Monad import Data.Functor.Plus import Data.Semigroup--- import Data.Monoid+import Data.Foldable+import Data.Monoid import Data.Functor.Bind-import Data.Set as Set hiding (empty)+import Data.Set as Set hiding (empty, toList) import Data.ByteString as Strict hiding (empty) import Data.Sequence as Seq hiding (empty) import Data.ByteString.UTF8 as UTF8 import Data.Bifunctor import Text.PrettyPrint.Free-import Text.Trifecta.It import Text.Trifecta.Delta import Text.Trifecta.Diagnostic import Text.Trifecta.Render.Prim import Text.Trifecta.Parser.Class+import Text.Trifecta.Parser.It import Text.Trifecta.Parser.Err import Text.Trifecta.Parser.Err.State---import Text.Trifecta.Parser.Step---import Text.Trifecta.Parser.Result+import Text.Trifecta.Parser.Step+import Text.Trifecta.Parser.Result import Text.Trifecta.Util.MaybePair---import Text.PrettyPrint.Free import System.Console.Terminfo.PrettyPrint data Parser e a = Parser @@ -144,10 +145,9 @@ bimap f g (JuSt a e d bs) = JuSt (g a) (fmap f e) d bs bimap f _ (NoSt e d bs) = NoSt (fmap f e) d bs -{- stepParser :: (Diagnostic e -> Diagnostic t) -> (ErrState e -> Delta -> ByteString -> Diagnostic t) ->- Parser e a -> ErrState e -> Delta -> ByteString -> Step e a+ Parser e a -> ErrState e -> Delta -> ByteString -> Step t a stepParser yl y (Parser p) e0 d0 bs0 = go mempty $ p ju no ju no e0 d0 bs0 where@@ -159,11 +159,18 @@ JuSt a e _ _ -> Success (yl <$> errLog e) a NoSt e d bs -> Failure (yl <$> errLog e) (y e d bs)) (go <*> k)--} -why :: PrettyTerm e => ErrState e -> Delta -> ByteString -> Diagnostic TermDoc-why (ErrState ss m _) d bs - | Set.null ss = diagnose prettyTerm (surface d bs) m- | otherwise = expected <$> diagnose prettyTerm (surface d bs) m +why :: (e -> Doc t) -> ErrState e -> Delta -> ByteString -> Diagnostic (Doc t)+why pp (ErrState ss m _) d bs + | Set.null ss = diagnose pp (surface d bs) m+ | otherwise = expected <$> diagnose pp (surface d bs) m where expected doc = doc <> text ", expected" <+> fillSep (punctuate (char ',') $ text <$> toList ss)++parseTest :: Show a => Parser TermDoc a -> ByteString -> IO ()+parseTest p bs = case eof (feed st bs) of+ Failure xs e -> displayLn $ prettyTerm $ toList (xs |> e)+ Success xs a -> do + displayLn $ prettyTerm $ toList xs+ print a+ where st = stepParser id (why id) (release mempty *> p) mempty mempty bs
trifecta.cabal view
@@ -1,6 +1,6 @@ name: trifecta category: Text, Parsing-version: 0.12+version: 0.13 license: BSD3 cabal-version: >= 1.6 license-file: LICENSE@@ -49,7 +49,7 @@ Text.Trifecta.Diagnostic Text.Trifecta.Diagnostic.Level Text.Trifecta.Hunk- Text.Trifecta.It+ Text.Trifecta.Parser.It Text.Trifecta.Parser.Char Text.Trifecta.Parser.Class Text.Trifecta.Parser.Err