packages feed

trifecta 0.3 → 0.4

raw patch · 6 files changed

+68/−89 lines, 6 filesdep −textdep −text-icudep −wl-pprint-extras

Dependencies removed: text, text-icu, wl-pprint-extras

Files

Text/Trifecta/Hunk.hs view
@@ -5,30 +5,24 @@   ) where  import Data.ByteString+import qualified Data.ByteString.UTF8 as UTF8 import Data.FingerTree as FingerTree import Data.Function (on) import Data.Hashable import Data.Interned-import Data.Text as Text-import Data.Text.ICU.Convert-import GHC.IO+import Data.String import Text.Trifecta.Delta-import Text.PrettyPrint.Leijen.Extras  data Hunk = Hunk {-# UNPACK #-} !Id !Delta {-# UNPACK #-} !ByteString+  deriving Show  hunk :: ByteString -> Hunk hunk = intern  --- assuming utf8 encoding-prettyByteString :: ByteString -> Doc e-prettyByteString bs = string (Text.unpack (toUnicode (unsafeDupablePerformIO (open "UTF8" Nothing)) bs))--instance Pretty Hunk where-  pretty (Hunk _ _ bs) = prettyByteString bs+-- instance Show Hunk where showsPrec d (Hunk _ _ b) = showsPrec d b -instance Show Hunk where-  showsPrec _ h = displayS (renderPretty 0.9 80 (pretty h))+instance IsString Hunk where+  fromString = hunk . UTF8.fromString  instance Eq Hunk where   (==) = (==) `on` identity
Text/Trifecta/It.hs view
@@ -16,6 +16,7 @@ import Data.Functor.Plus import Text.Trifecta.Rope as Rope import Text.Trifecta.Delta+import Text.Trifecta.Bytes import Text.Parsec.Prim hiding ((<|>))  data It a @@ -74,7 +75,7 @@ input = Cont $ \r e -> Done r e r  instance Stream Delta It Char where-  uncons d = k <$> peekIt d <|> return Nothing where +  uncons d = (k <$> peekIt d) <|> return Nothing where      k (d', bs) = case LazyUTF8.uncons bs of       Just (c, _) -> Just (c, d' <> delta c)       Nothing     -> Nothing@@ -82,7 +83,7 @@ peekIt :: Delta -> It (Delta, Lazy.ByteString) peekIt n = Cont go where   go h eof -    | n < lastNewline h eof = grab n h (\c lbs -> Done h eof (c, lbs)) -                                       (Fail h eof "peek: failed to grab rope")+    | bytes n < bytes (lastNewline h eof) = grab n h (\c lbs -> Done h eof (c, lbs)) +                                            (Fail h eof "peek: failed to grab rope")     | eof                   = Fail h True "Unexpected EOF"     | otherwise             = Cont $ \h' -> go (h <> h') -- h' <> h
Text/Trifecta/Path.hs view
@@ -2,11 +2,10 @@ module Text.Trifecta.Path   ( FileName   , Path(..), History(..)-  , startPath+  , file   , snocPath   , path   , appendPath-  , prettyPathWith   ) where  import Data.Hashable@@ -14,30 +13,12 @@ import Data.Interned.String import Data.Function (on) import Data.Semigroup-import Text.PrettyPrint.Leijen.Extras  type FileName = InternedString  data Path = Path {-# UNPACK #-} !Id !History !MaybeFileName {-# UNPACK #-} !Int [Int]   deriving Show --- Todo: Make a prettier path-prettyPathWith :: (Doc e -> Doc e) -> Path -> Int -> Doc e -prettyPathWith wrapDir = go where-  go (Path _ h mf l flags) delta -     = addHistory -     $ wrapDir $ hsep $ text "#" : pretty (l + delta) : addFile (map pretty flags) where-    addHistory = case h of-      Continue p d -> above (prettyPathWith wrapDir p d)-      Complete -> id-    addFile = case mf of-      JustFileName f -> (:) (dquotes (pretty (unintern f)))-      NothingFileName -> id--instance Pretty Path where-  pretty p = prettyPathWith id p 0-- instance Eq Path where   (==) = (==) `on` identity @@ -51,8 +32,8 @@  data MaybeFileName = JustFileName !FileName | NothingFileName deriving (Eq, Show) -startPath :: FileName -> Path -startPath !n = path Complete (JustFileName n) 0 []+file :: String -> Path +file !n = path Complete (JustFileName (intern n)) 0 []  snocPath :: Path -> Int -> MaybeFileName -> Int -> [Int] -> Path snocPath d l jf l' flags = path (Continue d l) jf l' flags@@ -101,3 +82,25 @@ pathCache :: Cache Path pathCache = mkCache {-# NOINLINE pathCache #-}++{-++instance Pretty Path where+  pretty p = prettyPathWith id p 0++prettyPathWith :: (Doc e -> Doc e) -> Path -> Int -> Doc e +prettyPathWith wrapDir = go where+  go (Path _ h mf l flags) delta +     = addHistory +     $ wrapDir $ hsep $ text "#" : pretty (l + delta) : addFile (map pretty flags) where+    addHistory = case h of+      Continue p d -> above (prettyPathWith wrapDir p d)+      Complete -> id+    addFile = case mf of+      JustFileName f -> (:) (dquotes (pretty (unintern f)))+      NothingFileName -> id++instance Show Path where+  showsPrec d (Path _ _ (JustFileName f) l _) = showString (unintern f) . showChar ':' . showsPrec 10 l+  showsPrec d (Path _ _ NothingFileName l _) = showString "-:" . showsPrec 10 l+-}
Text/Trifecta/Rope.hs view
@@ -20,7 +20,7 @@ import Text.Trifecta.Bytes import Text.Trifecta.Strand -data Rope = Rope !Delta !(FingerTree Delta Strand)+data Rope = Rope !Delta !(FingerTree Delta Strand) deriving Show  rope :: FingerTree Delta Strand -> Rope rope r = Rope (measure r) r@@ -37,6 +37,30 @@ instance Measured Delta Rope where   measure (Rope s _) = s +-- | obtain the byte location of the last newline in a rope, or the end of the rope if at EOF+lastNewline :: Rope -> Bool -> Delta+lastNewline t True  = delta t+lastNewline t False = rewind (delta t)++-- | grab a lazy bytestring starting from some point. This bytestring does not cross path nodes+--   if the index is to the start of a bytestring fragment, we update it to deal with any +--   intervening path fragments+grab :: Delta -> Rope -> (Delta ->  Lazy.ByteString -> r) -> r -> r+grab i t ks kf = trim (toList r) (delta l) (bytes i - bytes l) where+  trim (PathStrand p : xs)            j k = trim xs (j <> delta p) k+  trim (HunkStrand (Hunk _ _ h) : xs) j 0 = go j h xs+  trim (HunkStrand (Hunk _ _ h) : xs) _ k = go i (Strict.drop k h) xs+  trim [] _ _                             = kf+  go j h s = ks j $ Lazy.fromChunks $ h : [ a | HunkStrand (Hunk _ _ a) <- s ]+  (l, r) = FingerTree.split (\b -> bytes b > bytes i) (strands t)++instance Monoid Rope where+  mempty = Rope mempty mempty+  mappend = (<>)++instance Semigroup Rope where+  Rope mx x <> Rope my y = Rope (mx <> my) (x `mappend` y)+ instance Reducer Rope Rope where   unit = id @@ -59,42 +83,3 @@   unit = unit . hunk   cons = cons . hunk    snoc r = snoc r . hunk--instance Show Rope where-  showsPrec d (Rope _ r) = showsPrec d (toList r)---- | obtain the byte location of the last newline in a rope, or the end of the rope if at EOF-lastNewline :: Rope -> Bool -> Delta-lastNewline t True  = delta t-lastNewline t False = rewind (delta t)---- | grab a lazy bytestring starting from some point. This bytestring does not cross path nodes---   if the index is to the start of a bytestring fragment, we update it to deal with any ---   intervening path fragments-grab :: Delta -> Rope -> (Delta -> Lazy.ByteString -> r) -> r -> r-grab i t ks kf = case FingerTree.viewl r of-  HunkStrand (Hunk _ _ a) :< r' -> case bi - bl of -    0  -> ks (measure l) (Lazy.fromChunks $ a : chunks r')-    db -> ks i           (Lazy.fromChunks $ (Strict.drop db a) : chunks r')-  _ -> kf-  where -    bi = bytes i-    bl = bytes l-    (l, r) = FingerTree.split (\b -> bytes b > bi) (strands t)-    chunks s = case viewl s of -      HunkStrand (Hunk _ _ a) :< s' -> a : chunks s'-      _ -> []--{--indexByte :: Int -> Rope -> Word8-indexByte i (Rope _ t) = Strict.index a $ i - bytes l where-   (l, r) = FingerTree.split (\b -> bytes b > i) t-   HunkStrand (Hunk _ _ a) :< _ = FingerTree.viewl r--}--instance Monoid Rope where-  mempty = Rope mempty mempty-  mappend = (<>)--instance Semigroup Rope where-  Rope mx x <> Rope my y = Rope (mx <> my) (x `mappend` y)
Text/Trifecta/Strand.hs view
@@ -11,19 +11,15 @@ import Text.Trifecta.Path import Text.Trifecta.Bytes import Text.Trifecta.Delta-import Text.PrettyPrint.Leijen.Extras  data Strand   = HunkStrand !Hunk   | PathStrand !Path--instance Show Strand where-  showsPrec d (HunkStrand h) = showsPrec d h-  showsPrec d (PathStrand p) = showsPrec d p+  deriving Show -instance Pretty Strand where-  pretty (HunkStrand h) = pretty h-  pretty (PathStrand p) = pretty p+--instance Show Strand where+--  showsPrec d (HunkStrand h) = showsPrec d h+--  showsPrec d (PathStrand p) = showsPrec d p  instance Measured Delta Strand where   measure (HunkStrand s) = delta s
trifecta.cabal view
@@ -1,6 +1,6 @@ name:          trifecta category:      Text, Parsing-version:       0.3+version:       0.4 license:       BSD3 cabal-version: >= 1.6 license-file:  LICENSE@@ -24,9 +24,6 @@     intern           >= 0.5.1.1  && < 0.6,     hashable         >= 1.1.2.1  && < 1.2,     bytestring       >= 0.9.1    && < 0.10,-    text             >= 0.11.1.5 && < 0.12,-    text-icu         >= 0.6.3.4  && < 0.7,-    wl-pprint-extras >= 1.2      && < 1.3,     semigroups       >= 0.7.1    && < 0.8,      fingertree       >= 0.0.1    && < 0.1,     reducers         >= 0.1.2    && < 0.2,@@ -36,6 +33,9 @@     parallel         >= 3.1.0.1  && < 3.2,     transformers     >= 0.2.2    && < 0.3 +--  text             >= 0.11.1.5 && < 0.12,+--  text-icu         >= 0.6.3.4  && < 0.7,+--  wl-pprint-extras >= 1.2      && < 1.3,   ghc-options: -Wall    exposed-modules: