From faab2338de0ddf2f7a2e31a5380066c29bdd717f Mon Sep 17 00:00:00 2001 From: Matt Diephouse Date: Wed, 13 Jan 2016 15:50:52 -0500 Subject: [PATCH] Document the declarations in Source.hs --- src/Source.hs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Source.hs b/src/Source.hs index 9c5630179..e4aa94e7a 100644 --- a/src/Source.hs +++ b/src/Source.hs @@ -5,42 +5,55 @@ import Range import qualified Data.Vector as Vector import qualified Data.Text as T +-- | The contents of a source file, backed by a vector for efficient slicing. newtype Source a = Source { getVector :: Vector.Vector a } deriving (Eq, Show, Functor, Foldable, Traversable) +-- | Return a Source from a list of items. fromList :: [a] -> Source a fromList = Source . Vector.fromList +-- | Return a Source of Chars from a Text. fromText :: T.Text -> Source Char fromText = Source . Vector.fromList . T.unpack +-- | Return a list of items with the contents of the Source. toList :: Source a -> [a] toList = Vector.toList . getVector +-- | Return a Source that contains a slice of the given Source. slice :: Range -> Source a -> Source a slice range = Source . Vector.slice (start range) (rangeLength range) . getVector +-- | Return a String with the contents of the Source. toString :: Source Char -> String toString = toList +-- | Return the item at the given index. at :: Source a -> Int -> a at = (Vector.!) . getVector +-- | Test whether the source is empty. null :: Source a -> Bool null = Vector.null . getVector +-- | Prepend an item. cons :: a -> Source a -> Source a cons a = Source . Vector.cons a . getVector +-- | Remove the first item and return it with the rest of the source. uncons :: Source a -> Maybe (a, Source a) uncons (Source vector) = if Vector.null vector then Nothing else Just (Vector.head vector, Source $ Vector.tail vector) +-- | Split the source into the longest prefix of elements that do not satisfy the predicate and the rest without copying. break :: (a -> Bool) -> Source a -> (Source a, Source a) break predicate (Source vector) = let (start, remainder) = Vector.break predicate vector in (Source start, Source remainder) +-- | Concatenate two sources. (++) :: Source a -> Source a -> Source a (++) (Source a) = Source . (a Vector.++) . getVector +-- | Split the contents of the source by newlines. actualLines :: Source Char -> [Source Char] actualLines source | Source.null source = [ source ] actualLines source = case Source.break (== '\n') source of