2019-10-30 22:43:32 +03:00
|
|
|
{-# LANGUAGE TypeApplications, TypeOperators #-}
|
2019-10-30 20:29:18 +03:00
|
|
|
|
|
|
|
-- | FileCheck-style directives for testing Core compilers.
|
2019-08-16 19:34:56 +03:00
|
|
|
module Directive ( Directive (..)
|
2019-10-30 20:29:18 +03:00
|
|
|
, readDirectivesFromFile
|
2019-08-29 16:20:13 +03:00
|
|
|
, describe
|
2019-08-16 19:34:56 +03:00
|
|
|
) where
|
|
|
|
|
2019-10-30 22:43:32 +03:00
|
|
|
import Analysis.Concrete (Concrete (..))
|
2019-11-09 06:37:17 +03:00
|
|
|
import Control.Algebra
|
2019-09-19 01:07:43 +03:00
|
|
|
import Control.Monad
|
2019-10-30 22:43:32 +03:00
|
|
|
import Control.Monad.Trans.Resource (ResourceT, runResourceT)
|
2019-10-11 01:00:53 +03:00
|
|
|
import Core.Core (Core)
|
2019-10-30 20:49:54 +03:00
|
|
|
import qualified Core.Core as Core
|
2019-10-30 20:29:18 +03:00
|
|
|
import Core.Name (Name)
|
2019-10-21 21:09:20 +03:00
|
|
|
import qualified Core.Parser
|
|
|
|
import qualified Core.Pretty
|
2019-08-16 19:34:56 +03:00
|
|
|
import Data.ByteString.Char8 (ByteString)
|
|
|
|
import qualified Data.ByteString.Char8 as ByteString
|
2019-10-30 20:29:18 +03:00
|
|
|
import qualified Data.ByteString.Streaming.Char8 as ByteStream
|
2019-10-30 20:13:53 +03:00
|
|
|
import Data.Text (Text)
|
|
|
|
import qualified Data.Text as T
|
2019-10-30 20:49:54 +03:00
|
|
|
import qualified Source.Span as Source
|
2019-10-30 22:43:32 +03:00
|
|
|
import qualified Streaming.Prelude as Stream
|
2019-10-30 20:29:18 +03:00
|
|
|
import Syntax.Term (Term)
|
2019-10-30 22:43:32 +03:00
|
|
|
import qualified System.Path as Path
|
|
|
|
import qualified System.Path.PartClass as Path.Class
|
2019-10-30 20:29:18 +03:00
|
|
|
import qualified Text.Parser.Token.Style as Style
|
2019-10-30 20:13:53 +03:00
|
|
|
import Text.Trifecta (CharParsing, TokenParsing (..))
|
2019-08-16 19:34:56 +03:00
|
|
|
import qualified Text.Trifecta as Trifecta
|
|
|
|
|
2019-08-27 17:25:21 +03:00
|
|
|
{- |
|
|
|
|
|
|
|
|
Directives are parsed from magic comments in test files and
|
|
|
|
describe to the test suite how to query the results of a given test
|
|
|
|
case. A directive that looks like this:
|
2019-08-16 19:34:56 +03:00
|
|
|
|
2019-08-16 22:54:17 +03:00
|
|
|
@
|
2019-12-20 19:43:15 +03:00
|
|
|
# CHECK-RESULT: key: value
|
2019-08-16 22:54:17 +03:00
|
|
|
@
|
|
|
|
|
2019-12-20 19:43:15 +03:00
|
|
|
would test that the value for @key@ in the result evaluates to the given
|
|
|
|
concrete value.
|
2019-08-27 17:25:21 +03:00
|
|
|
|
|
|
|
This syntax was inspired by LLVM's
|
|
|
|
[FileCheck](https://llvm.org/docs/CommandGuide/FileCheck.html). This
|
|
|
|
approach is less direct than tests that pattern-match over an AST, but
|
|
|
|
enable us to keep the text of test cases in close proximity to the
|
|
|
|
assertions we want to make, which improves maintainability
|
|
|
|
significantly and has been a successful strategy for the LLVM and Rust
|
|
|
|
projects.
|
2019-08-16 22:54:17 +03:00
|
|
|
|
|
|
|
-}
|
2019-12-20 19:43:15 +03:00
|
|
|
data Directive = Tree (Term Core Name) -- | @# CHECK-TREE: core@
|
2019-12-20 18:47:17 +03:00
|
|
|
| Result Text (Concrete (Term (Core.Ann Source.Span :+: Core))) -- | @# CHECK-RESULT key: expected
|
2019-08-16 22:54:17 +03:00
|
|
|
| Fails -- | @# CHECK-FAILS@ fails unless translation fails.
|
|
|
|
deriving (Eq, Show)
|
2019-08-16 19:34:56 +03:00
|
|
|
|
2019-10-30 20:29:18 +03:00
|
|
|
-- | Extract all directives from a file.
|
|
|
|
readDirectivesFromFile :: Path.Class.AbsRel ar => Path.File ar -> IO [Directive]
|
|
|
|
readDirectivesFromFile
|
|
|
|
= runResourceT
|
|
|
|
. Stream.toList_
|
|
|
|
. Stream.mapM (either perish pure . parseDirective)
|
|
|
|
. Stream.takeWhile isComment
|
|
|
|
. Stream.mapped ByteStream.toStrict
|
|
|
|
. ByteStream.lines
|
|
|
|
. ByteStream.readFile @(ResourceT IO)
|
|
|
|
. Path.toString
|
|
|
|
where
|
|
|
|
perish s = fail ("Directive parsing error: " <> s)
|
|
|
|
isComment = (== Just '#') . fmap fst . ByteString.uncons
|
|
|
|
|
2019-10-30 20:13:53 +03:00
|
|
|
|
2019-08-29 16:20:13 +03:00
|
|
|
describe :: Directive -> String
|
2019-10-30 20:29:18 +03:00
|
|
|
describe Fails = "<expect failure>"
|
|
|
|
describe (Tree t) = Core.Pretty.showCore t
|
2019-10-30 20:13:53 +03:00
|
|
|
describe (Result t e) = T.unpack t <> ": " <> show e
|
2019-08-29 16:20:13 +03:00
|
|
|
|
2019-10-30 20:13:53 +03:00
|
|
|
fails :: CharParsing m => m Directive
|
2019-08-16 20:20:08 +03:00
|
|
|
fails = Fails <$ Trifecta.string "# CHECK-FAILS"
|
|
|
|
|
2019-10-30 20:13:53 +03:00
|
|
|
tree :: (Monad m, TokenParsing m) => m Directive
|
2019-09-19 01:07:43 +03:00
|
|
|
tree = do
|
|
|
|
void $ Trifecta.string "# CHECK-TREE: "
|
2019-09-24 05:00:44 +03:00
|
|
|
Tree <$> Core.Parser.core
|
2019-09-19 01:07:43 +03:00
|
|
|
|
2019-10-30 20:13:53 +03:00
|
|
|
result :: (Monad m, TokenParsing m) => m Directive
|
|
|
|
result = do
|
|
|
|
void $ Trifecta.string "# CHECK-RESULT "
|
|
|
|
key <- Trifecta.ident Style.haskellIdents
|
|
|
|
void $ Trifecta.symbolic ':'
|
2019-10-30 20:49:54 +03:00
|
|
|
Result key <$> concrete
|
|
|
|
|
2019-12-20 18:47:17 +03:00
|
|
|
concrete :: TokenParsing m => m (Concrete term)
|
2019-10-30 20:49:54 +03:00
|
|
|
concrete = Trifecta.choice
|
|
|
|
[ String <$> Trifecta.stringLiteral
|
|
|
|
, Bool True <$ Trifecta.symbol "#true"
|
|
|
|
, Bool False <$ Trifecta.symbol "#false"
|
|
|
|
, Unit <$ Trifecta.symbol "#unit"
|
2019-10-30 20:13:53 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
directive :: (Monad m, TokenParsing m) => m Directive
|
2019-12-20 19:43:15 +03:00
|
|
|
directive = Trifecta.choice [ fails, result, tree ]
|
2019-08-16 19:34:56 +03:00
|
|
|
|
2019-09-24 05:00:44 +03:00
|
|
|
parseDirective :: ByteString -> Either String Directive
|
|
|
|
parseDirective = Trifecta.foldResult (Left . show) Right
|
2019-09-24 05:16:25 +03:00
|
|
|
. Trifecta.parseByteString (directive <* Trifecta.eof) mempty
|