2018-03-10 03:24:23 +03:00
|
|
|
{-# LANGUAGE DataKinds, GADTs, ScopedTypeVariables, TypeFamilies, TypeOperators, TypeApplications #-}
|
2017-04-19 23:25:46 +03:00
|
|
|
module SpecHelpers
|
2018-03-10 03:24:23 +03:00
|
|
|
( file
|
|
|
|
, evaluateFiles
|
|
|
|
, diffFilePaths
|
2017-04-20 02:33:27 +03:00
|
|
|
, parseFilePath
|
2017-12-10 19:46:17 +03:00
|
|
|
, readFilePair
|
2018-02-07 23:20:27 +03:00
|
|
|
, Verbatim(..)
|
|
|
|
, verbatim
|
|
|
|
, readFileVerbatim
|
2017-04-19 23:25:46 +03:00
|
|
|
) where
|
2017-04-19 19:12:19 +03:00
|
|
|
|
2017-12-10 19:46:17 +03:00
|
|
|
import Control.Monad ((<=<))
|
2018-03-10 03:24:23 +03:00
|
|
|
import Control.Monad.IO.Class
|
2017-07-28 21:37:02 +03:00
|
|
|
import Control.Exception
|
2017-06-24 17:09:50 +03:00
|
|
|
import Data.Blob
|
2017-04-19 19:12:19 +03:00
|
|
|
import qualified Data.ByteString as B
|
2017-04-19 23:25:46 +03:00
|
|
|
import Data.Functor.Both
|
2017-11-27 22:17:48 +03:00
|
|
|
import Data.Language
|
2017-12-11 21:51:52 +03:00
|
|
|
import Data.Maybe (fromMaybe, fromJust)
|
2017-06-24 16:59:41 +03:00
|
|
|
import Data.Source
|
2017-11-27 21:30:38 +03:00
|
|
|
import Rendering.Renderer
|
2017-04-20 02:33:27 +03:00
|
|
|
import Semantic
|
2017-05-30 16:14:50 +03:00
|
|
|
import Semantic.Task
|
2017-12-10 19:46:17 +03:00
|
|
|
import qualified Semantic.IO as IO
|
2017-04-19 19:45:08 +03:00
|
|
|
import System.FilePath
|
2018-02-07 23:20:27 +03:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import Data.Text.Encoding (decodeUtf8)
|
2017-04-19 19:12:19 +03:00
|
|
|
|
2018-03-10 03:24:23 +03:00
|
|
|
import Analysis.Abstract.Evaluating
|
|
|
|
import Data.Map as Map
|
|
|
|
import Data.Union
|
|
|
|
import Data.Semigroup
|
|
|
|
import Data.Functor.Foldable
|
|
|
|
import Data.Abstract.Evaluatable
|
|
|
|
import Data.Abstract.Address
|
|
|
|
import Data.Abstract.Environment
|
|
|
|
import Data.Abstract.FreeVariables
|
|
|
|
import Data.Abstract.ModuleTable
|
|
|
|
import Data.Abstract.Store
|
|
|
|
import Data.Abstract.Value as Value
|
|
|
|
import Parsing.Parser
|
|
|
|
|
|
|
|
file :: MonadIO m => FilePath -> m Blob
|
|
|
|
file path = fromJust <$> IO.readFile path (IO.languageForFilePath path)
|
|
|
|
|
|
|
|
evaluateFiles :: forall v term.
|
|
|
|
( Data.Abstract.Evaluatable.Evaluatable (Data.Functor.Foldable.Base term)
|
|
|
|
, FreeVariables term
|
|
|
|
, MonadAddressable (LocationFor v) v (Evaluation term v)
|
|
|
|
, MonadValue term v (Evaluation term v)
|
|
|
|
, Ord (LocationFor v)
|
|
|
|
, Recursive term
|
|
|
|
, Semigroup (Cell (LocationFor v) v)
|
|
|
|
)
|
|
|
|
=> Parser term
|
|
|
|
-> [FilePath]
|
|
|
|
-> IO (
|
|
|
|
(
|
|
|
|
(
|
|
|
|
( Either Prelude.String v
|
|
|
|
, Store (LocationFor v) v
|
|
|
|
)
|
|
|
|
, Map.Map Data.Abstract.FreeVariables.Name (Data.Abstract.FreeVariables.Name, Maybe (Address (LocationFor v) v))
|
|
|
|
)
|
|
|
|
, Environment (LocationFor v) v
|
|
|
|
)
|
|
|
|
, ModuleTable (Environment (LocationFor v) v)
|
|
|
|
)
|
|
|
|
evaluateFiles parser paths = do
|
|
|
|
blobs@(b:bs) <- traverse file paths
|
|
|
|
(t:ts) <- runTask $ traverse (parse parser) blobs
|
|
|
|
pure $ evaluates @v (zip bs ts) (b, t)
|
|
|
|
|
2017-04-19 23:25:46 +03:00
|
|
|
-- | Returns an s-expression formatted diff for the specified FilePath pair.
|
2017-07-28 21:37:02 +03:00
|
|
|
diffFilePaths :: Both FilePath -> IO B.ByteString
|
2017-12-10 19:46:17 +03:00
|
|
|
diffFilePaths paths = readFilePair paths >>= runTask . diffBlobPair SExpressionDiffRenderer
|
2017-04-19 23:25:46 +03:00
|
|
|
|
|
|
|
-- | Returns an s-expression parse tree for the specified FilePath.
|
2017-07-28 21:37:02 +03:00
|
|
|
parseFilePath :: FilePath -> IO B.ByteString
|
2018-03-09 20:07:34 +03:00
|
|
|
parseFilePath path = IO.readFile path (IO.languageForFilePath path) >>= pure . fromJust >>= runTask . parseBlob SExpressionTermRenderer
|
2017-04-21 23:56:19 +03:00
|
|
|
|
2017-12-11 19:27:13 +03:00
|
|
|
-- | Read two files to a BlobPair.
|
|
|
|
readFilePair :: Both FilePath -> IO BlobPair
|
2018-03-09 20:07:34 +03:00
|
|
|
readFilePair paths = let paths' = fmap (\p -> (p, IO.languageForFilePath p)) paths in
|
2017-12-11 19:27:13 +03:00
|
|
|
runBothWith IO.readFilePair paths'
|
2017-05-11 16:28:52 +03:00
|
|
|
|
2018-02-07 23:20:27 +03:00
|
|
|
readFileVerbatim :: FilePath -> IO Verbatim
|
2018-02-07 23:26:14 +03:00
|
|
|
readFileVerbatim = fmap verbatim . B.readFile
|
2018-02-07 23:20:27 +03:00
|
|
|
|
|
|
|
newtype Verbatim = Verbatim B.ByteString
|
|
|
|
deriving (Eq)
|
|
|
|
|
|
|
|
instance Show Verbatim where
|
|
|
|
show (Verbatim x) = show x
|
|
|
|
|
|
|
|
verbatim :: B.ByteString -> Verbatim
|
|
|
|
verbatim = Verbatim . stripWhitespace
|
|
|
|
where
|
|
|
|
stripWhitespace :: B.ByteString -> B.ByteString
|
|
|
|
stripWhitespace = B.foldl' go B.empty
|
|
|
|
where go acc x | x `B.elem` " \t\n" = acc
|
|
|
|
| otherwise = B.snoc acc x
|