{- -----------------------------------------------------------------------------
Copyright 2020-2021 Kevin P. Barry

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
----------------------------------------------------------------------------- -}

-- Author: Kevin P. Barry [ta0kira@gmail.com]

module Parser.SourceFile (
  CodeVisibility(..),
  PragmaSource(..),
  WithVisibility(..),
  hasCodeVisibility,
  isModuleOnly,
  isTestsOnly,
  mapCodeVisibility,
  parseInternalSource,
  parsePublicSource,
  parseTestSource,
  pragmaModuleOnly,
  pragmaTestsOnly,
  updateCodeVisibility,
) where

import qualified Data.Set as Set

import Base.CompilerError
import Parser.Common
import Parser.DefinedCategory ()
import Parser.IntegrationTest ()
import Parser.Pragma
import Parser.TextParser
import Parser.TypeCategory ()
import Types.DefinedCategory
import Types.IntegrationTest
import Types.TypeCategory


parseInternalSource :: ErrorContextM m =>
  (FilePath,String) -> m ([PragmaSource SourceContext],[AnyCategory SourceContext],[DefinedCategory SourceContext])
parseInternalSource :: (FilePath, FilePath)
-> m ([PragmaSource SourceContext], [AnyCategory SourceContext],
      [DefinedCategory SourceContext])
parseInternalSource (FilePath
f,FilePath
s) = TextParser
  ([PragmaSource SourceContext], [AnyCategory SourceContext],
   [DefinedCategory SourceContext])
-> FilePath
-> FilePath
-> m ([PragmaSource SourceContext], [AnyCategory SourceContext],
      [DefinedCategory SourceContext])
forall (m :: * -> *) a.
ErrorContextM m =>
TextParser a -> FilePath -> FilePath -> m a
runTextParser (ParsecT CompilerMessage FilePath Identity ()
-> ParsecT CompilerMessage FilePath Identity ()
-> TextParser
     ([PragmaSource SourceContext], [AnyCategory SourceContext],
      [DefinedCategory SourceContext])
-> TextParser
     ([PragmaSource SourceContext], [AnyCategory SourceContext],
      [DefinedCategory SourceContext])
forall (m :: * -> *) open close a.
Applicative m =>
m open -> m close -> m a -> m a
between ParsecT CompilerMessage FilePath Identity ()
optionalSpace ParsecT CompilerMessage FilePath Identity ()
endOfDoc TextParser
  ([PragmaSource SourceContext], [AnyCategory SourceContext],
   [DefinedCategory SourceContext])
withPragmas) FilePath
f FilePath
s where
  withPragmas :: TextParser
  ([PragmaSource SourceContext], [AnyCategory SourceContext],
   [DefinedCategory SourceContext])
withPragmas = do
    [PragmaSource SourceContext]
pragmas <- [TextParser (PragmaSource SourceContext)]
-> TextParser [PragmaSource SourceContext]
forall a. [TextParser a] -> TextParser [a]
parsePragmas [TextParser (PragmaSource SourceContext)]
internalSourcePragmas
    ParsecT CompilerMessage FilePath Identity ()
optionalSpace
    ([AnyCategory SourceContext]
cs,[DefinedCategory SourceContext]
ds) <- TextParser (AnyCategory SourceContext)
-> TextParser (DefinedCategory SourceContext)
-> TextParser
     ([AnyCategory SourceContext], [DefinedCategory SourceContext])
forall a b. TextParser a -> TextParser b -> TextParser ([a], [b])
parseAny2 TextParser (AnyCategory SourceContext)
forall a. ParseFromSource a => TextParser a
sourceParser TextParser (DefinedCategory SourceContext)
forall a. ParseFromSource a => TextParser a
sourceParser
    ([PragmaSource SourceContext], [AnyCategory SourceContext],
 [DefinedCategory SourceContext])
-> TextParser
     ([PragmaSource SourceContext], [AnyCategory SourceContext],
      [DefinedCategory SourceContext])
forall (m :: * -> *) a. Monad m => a -> m a
return ([PragmaSource SourceContext]
pragmas,[AnyCategory SourceContext]
cs,[DefinedCategory SourceContext]
ds)

parsePublicSource :: ErrorContextM m => (FilePath,String) -> m ([PragmaSource SourceContext],[AnyCategory SourceContext])
parsePublicSource :: (FilePath, FilePath)
-> m ([PragmaSource SourceContext], [AnyCategory SourceContext])
parsePublicSource (FilePath
f,FilePath
s) = TextParser
  ([PragmaSource SourceContext], [AnyCategory SourceContext])
-> FilePath
-> FilePath
-> m ([PragmaSource SourceContext], [AnyCategory SourceContext])
forall (m :: * -> *) a.
ErrorContextM m =>
TextParser a -> FilePath -> FilePath -> m a
runTextParser (ParsecT CompilerMessage FilePath Identity ()
-> ParsecT CompilerMessage FilePath Identity ()
-> TextParser
     ([PragmaSource SourceContext], [AnyCategory SourceContext])
-> TextParser
     ([PragmaSource SourceContext], [AnyCategory SourceContext])
forall (m :: * -> *) open close a.
Applicative m =>
m open -> m close -> m a -> m a
between ParsecT CompilerMessage FilePath Identity ()
optionalSpace ParsecT CompilerMessage FilePath Identity ()
endOfDoc TextParser
  ([PragmaSource SourceContext], [AnyCategory SourceContext])
withPragmas) FilePath
f FilePath
s where
  withPragmas :: TextParser
  ([PragmaSource SourceContext], [AnyCategory SourceContext])
withPragmas = do
    [PragmaSource SourceContext]
pragmas <- [TextParser (PragmaSource SourceContext)]
-> TextParser [PragmaSource SourceContext]
forall a. [TextParser a] -> TextParser [a]
parsePragmas [TextParser (PragmaSource SourceContext)]
publicSourcePragmas
    ParsecT CompilerMessage FilePath Identity ()
optionalSpace
    [AnyCategory SourceContext]
cs <- TextParser (AnyCategory SourceContext)
-> ParsecT CompilerMessage FilePath Identity ()
-> ParsecT
     CompilerMessage FilePath Identity [AnyCategory SourceContext]
forall (m :: * -> *) a sep. MonadPlus m => m a -> m sep -> m [a]
sepBy TextParser (AnyCategory SourceContext)
forall a. ParseFromSource a => TextParser a
sourceParser ParsecT CompilerMessage FilePath Identity ()
optionalSpace
    ([PragmaSource SourceContext], [AnyCategory SourceContext])
-> TextParser
     ([PragmaSource SourceContext], [AnyCategory SourceContext])
forall (m :: * -> *) a. Monad m => a -> m a
return ([PragmaSource SourceContext]
pragmas,[AnyCategory SourceContext]
cs)

parseTestSource :: ErrorContextM m => (FilePath,String) -> m ([PragmaSource SourceContext],[IntegrationTest SourceContext])
parseTestSource :: (FilePath, FilePath)
-> m ([PragmaSource SourceContext],
      [IntegrationTest SourceContext])
parseTestSource (FilePath
f,FilePath
s) = TextParser
  ([PragmaSource SourceContext], [IntegrationTest SourceContext])
-> FilePath
-> FilePath
-> m ([PragmaSource SourceContext],
      [IntegrationTest SourceContext])
forall (m :: * -> *) a.
ErrorContextM m =>
TextParser a -> FilePath -> FilePath -> m a
runTextParser (ParsecT CompilerMessage FilePath Identity ()
-> ParsecT CompilerMessage FilePath Identity ()
-> TextParser
     ([PragmaSource SourceContext], [IntegrationTest SourceContext])
-> TextParser
     ([PragmaSource SourceContext], [IntegrationTest SourceContext])
forall (m :: * -> *) open close a.
Applicative m =>
m open -> m close -> m a -> m a
between ParsecT CompilerMessage FilePath Identity ()
optionalSpace ParsecT CompilerMessage FilePath Identity ()
endOfDoc TextParser
  ([PragmaSource SourceContext], [IntegrationTest SourceContext])
withPragmas) FilePath
f FilePath
s where
  withPragmas :: TextParser
  ([PragmaSource SourceContext], [IntegrationTest SourceContext])
withPragmas = do
    [PragmaSource SourceContext]
pragmas <- [TextParser (PragmaSource SourceContext)]
-> TextParser [PragmaSource SourceContext]
forall a. [TextParser a] -> TextParser [a]
parsePragmas [TextParser (PragmaSource SourceContext)]
testSourcePragmas
    ParsecT CompilerMessage FilePath Identity ()
optionalSpace
    [IntegrationTest SourceContext]
ts <- ParsecT
  CompilerMessage FilePath Identity (IntegrationTest SourceContext)
-> ParsecT CompilerMessage FilePath Identity ()
-> ParsecT
     CompilerMessage FilePath Identity [IntegrationTest SourceContext]
forall (m :: * -> *) a sep. MonadPlus m => m a -> m sep -> m [a]
sepBy ParsecT
  CompilerMessage FilePath Identity (IntegrationTest SourceContext)
forall a. ParseFromSource a => TextParser a
sourceParser ParsecT CompilerMessage FilePath Identity ()
optionalSpace
    ([PragmaSource SourceContext], [IntegrationTest SourceContext])
-> TextParser
     ([PragmaSource SourceContext], [IntegrationTest SourceContext])
forall (m :: * -> *) a. Monad m => a -> m a
return ([PragmaSource SourceContext]
pragmas,[IntegrationTest SourceContext]
ts)

publicSourcePragmas :: [TextParser (PragmaSource SourceContext)]
publicSourcePragmas :: [TextParser (PragmaSource SourceContext)]
publicSourcePragmas = [TextParser (PragmaSource SourceContext)
pragmaModuleOnly,TextParser (PragmaSource SourceContext)
pragmaTestsOnly]

internalSourcePragmas :: [TextParser (PragmaSource SourceContext)]
internalSourcePragmas :: [TextParser (PragmaSource SourceContext)]
internalSourcePragmas = [TextParser (PragmaSource SourceContext)
pragmaTestsOnly]

testSourcePragmas :: [TextParser (PragmaSource SourceContext)]
testSourcePragmas :: [TextParser (PragmaSource SourceContext)]
testSourcePragmas = []

pragmaModuleOnly :: TextParser (PragmaSource SourceContext)
pragmaModuleOnly :: TextParser (PragmaSource SourceContext)
pragmaModuleOnly = FilePath
-> Either
     (SourceContext -> PragmaSource SourceContext)
     (SourceContext -> TextParser (PragmaSource SourceContext))
-> TextParser (PragmaSource SourceContext)
forall a.
FilePath
-> Either (SourceContext -> a) (SourceContext -> TextParser a)
-> TextParser a
autoPragma FilePath
"ModuleOnly" (Either
   (SourceContext -> PragmaSource SourceContext)
   (SourceContext -> TextParser (PragmaSource SourceContext))
 -> TextParser (PragmaSource SourceContext))
-> Either
     (SourceContext -> PragmaSource SourceContext)
     (SourceContext -> TextParser (PragmaSource SourceContext))
-> TextParser (PragmaSource SourceContext)
forall a b. (a -> b) -> a -> b
$ (SourceContext -> PragmaSource SourceContext)
-> Either
     (SourceContext -> PragmaSource SourceContext)
     (SourceContext -> TextParser (PragmaSource SourceContext))
forall a b. a -> Either a b
Left SourceContext -> PragmaSource SourceContext
forall c. c -> PragmaSource c
parseAt where
  parseAt :: c -> PragmaSource c
parseAt c
c = [c] -> CodeVisibility -> PragmaSource c
forall c. [c] -> CodeVisibility -> PragmaSource c
PragmaVisibility [c
c] CodeVisibility
ModuleOnly

pragmaTestsOnly :: TextParser (PragmaSource SourceContext)
pragmaTestsOnly :: TextParser (PragmaSource SourceContext)
pragmaTestsOnly = FilePath
-> Either
     (SourceContext -> PragmaSource SourceContext)
     (SourceContext -> TextParser (PragmaSource SourceContext))
-> TextParser (PragmaSource SourceContext)
forall a.
FilePath
-> Either (SourceContext -> a) (SourceContext -> TextParser a)
-> TextParser a
autoPragma FilePath
"TestsOnly" (Either
   (SourceContext -> PragmaSource SourceContext)
   (SourceContext -> TextParser (PragmaSource SourceContext))
 -> TextParser (PragmaSource SourceContext))
-> Either
     (SourceContext -> PragmaSource SourceContext)
     (SourceContext -> TextParser (PragmaSource SourceContext))
-> TextParser (PragmaSource SourceContext)
forall a b. (a -> b) -> a -> b
$ (SourceContext -> PragmaSource SourceContext)
-> Either
     (SourceContext -> PragmaSource SourceContext)
     (SourceContext -> TextParser (PragmaSource SourceContext))
forall a b. a -> Either a b
Left SourceContext -> PragmaSource SourceContext
forall c. c -> PragmaSource c
parseAt where
  parseAt :: c -> PragmaSource c
parseAt c
c = [c] -> CodeVisibility -> PragmaSource c
forall c. [c] -> CodeVisibility -> PragmaSource c
PragmaVisibility [c
c] CodeVisibility
TestsOnly

data CodeVisibility = ModuleOnly | TestsOnly | FromDependency deriving (CodeVisibility -> CodeVisibility -> Bool
(CodeVisibility -> CodeVisibility -> Bool)
-> (CodeVisibility -> CodeVisibility -> Bool) -> Eq CodeVisibility
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CodeVisibility -> CodeVisibility -> Bool
$c/= :: CodeVisibility -> CodeVisibility -> Bool
== :: CodeVisibility -> CodeVisibility -> Bool
$c== :: CodeVisibility -> CodeVisibility -> Bool
Eq,Eq CodeVisibility
Eq CodeVisibility
-> (CodeVisibility -> CodeVisibility -> Ordering)
-> (CodeVisibility -> CodeVisibility -> Bool)
-> (CodeVisibility -> CodeVisibility -> Bool)
-> (CodeVisibility -> CodeVisibility -> Bool)
-> (CodeVisibility -> CodeVisibility -> Bool)
-> (CodeVisibility -> CodeVisibility -> CodeVisibility)
-> (CodeVisibility -> CodeVisibility -> CodeVisibility)
-> Ord CodeVisibility
CodeVisibility -> CodeVisibility -> Bool
CodeVisibility -> CodeVisibility -> Ordering
CodeVisibility -> CodeVisibility -> CodeVisibility
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: CodeVisibility -> CodeVisibility -> CodeVisibility
$cmin :: CodeVisibility -> CodeVisibility -> CodeVisibility
max :: CodeVisibility -> CodeVisibility -> CodeVisibility
$cmax :: CodeVisibility -> CodeVisibility -> CodeVisibility
>= :: CodeVisibility -> CodeVisibility -> Bool
$c>= :: CodeVisibility -> CodeVisibility -> Bool
> :: CodeVisibility -> CodeVisibility -> Bool
$c> :: CodeVisibility -> CodeVisibility -> Bool
<= :: CodeVisibility -> CodeVisibility -> Bool
$c<= :: CodeVisibility -> CodeVisibility -> Bool
< :: CodeVisibility -> CodeVisibility -> Bool
$c< :: CodeVisibility -> CodeVisibility -> Bool
compare :: CodeVisibility -> CodeVisibility -> Ordering
$ccompare :: CodeVisibility -> CodeVisibility -> Ordering
$cp1Ord :: Eq CodeVisibility
Ord,Int -> CodeVisibility -> ShowS
[CodeVisibility] -> ShowS
CodeVisibility -> FilePath
(Int -> CodeVisibility -> ShowS)
-> (CodeVisibility -> FilePath)
-> ([CodeVisibility] -> ShowS)
-> Show CodeVisibility
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [CodeVisibility] -> ShowS
$cshowList :: [CodeVisibility] -> ShowS
show :: CodeVisibility -> FilePath
$cshow :: CodeVisibility -> FilePath
showsPrec :: Int -> CodeVisibility -> ShowS
$cshowsPrec :: Int -> CodeVisibility -> ShowS
Show)

data WithVisibility a =
  WithVisibility {
    WithVisibility a -> Set CodeVisibility
wvVisibility :: Set.Set CodeVisibility,
    WithVisibility a -> a
wvData :: a
  }
  deriving (Int -> WithVisibility a -> ShowS
[WithVisibility a] -> ShowS
WithVisibility a -> FilePath
(Int -> WithVisibility a -> ShowS)
-> (WithVisibility a -> FilePath)
-> ([WithVisibility a] -> ShowS)
-> Show (WithVisibility a)
forall a. Show a => Int -> WithVisibility a -> ShowS
forall a. Show a => [WithVisibility a] -> ShowS
forall a. Show a => WithVisibility a -> FilePath
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [WithVisibility a] -> ShowS
$cshowList :: forall a. Show a => [WithVisibility a] -> ShowS
show :: WithVisibility a -> FilePath
$cshow :: forall a. Show a => WithVisibility a -> FilePath
showsPrec :: Int -> WithVisibility a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> WithVisibility a -> ShowS
Show)

hasCodeVisibility :: CodeVisibility -> WithVisibility a -> Bool
hasCodeVisibility :: CodeVisibility -> WithVisibility a -> Bool
hasCodeVisibility CodeVisibility
v = CodeVisibility -> Set CodeVisibility -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.member CodeVisibility
v (Set CodeVisibility -> Bool)
-> (WithVisibility a -> Set CodeVisibility)
-> WithVisibility a
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WithVisibility a -> Set CodeVisibility
forall a. WithVisibility a -> Set CodeVisibility
wvVisibility

mapCodeVisibility :: (a -> b) -> WithVisibility a -> WithVisibility b
mapCodeVisibility :: (a -> b) -> WithVisibility a -> WithVisibility b
mapCodeVisibility a -> b
f (WithVisibility Set CodeVisibility
v a
x) = Set CodeVisibility -> b -> WithVisibility b
forall a. Set CodeVisibility -> a -> WithVisibility a
WithVisibility Set CodeVisibility
v (a -> b
f a
x)

updateCodeVisibility :: (Set.Set CodeVisibility -> Set.Set CodeVisibility) ->
  WithVisibility a -> WithVisibility a
updateCodeVisibility :: (Set CodeVisibility -> Set CodeVisibility)
-> WithVisibility a -> WithVisibility a
updateCodeVisibility Set CodeVisibility -> Set CodeVisibility
f (WithVisibility Set CodeVisibility
v a
x) = Set CodeVisibility -> a -> WithVisibility a
forall a. Set CodeVisibility -> a -> WithVisibility a
WithVisibility (Set CodeVisibility -> Set CodeVisibility
f Set CodeVisibility
v) a
x

data PragmaSource c =
  PragmaVisibility {
    PragmaSource c -> [c]
pvContext :: [c],
    PragmaSource c -> CodeVisibility
pvScopes :: CodeVisibility
  }
  deriving (Int -> PragmaSource c -> ShowS
[PragmaSource c] -> ShowS
PragmaSource c -> FilePath
(Int -> PragmaSource c -> ShowS)
-> (PragmaSource c -> FilePath)
-> ([PragmaSource c] -> ShowS)
-> Show (PragmaSource c)
forall c. Show c => Int -> PragmaSource c -> ShowS
forall c. Show c => [PragmaSource c] -> ShowS
forall c. Show c => PragmaSource c -> FilePath
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [PragmaSource c] -> ShowS
$cshowList :: forall c. Show c => [PragmaSource c] -> ShowS
show :: PragmaSource c -> FilePath
$cshow :: forall c. Show c => PragmaSource c -> FilePath
showsPrec :: Int -> PragmaSource c -> ShowS
$cshowsPrec :: forall c. Show c => Int -> PragmaSource c -> ShowS
Show)

isModuleOnly :: PragmaSource c -> Bool
isModuleOnly :: PragmaSource c -> Bool
isModuleOnly (PragmaVisibility [c]
_ CodeVisibility
ModuleOnly) = Bool
True
isModuleOnly PragmaSource c
_                               = Bool
False

isTestsOnly :: PragmaSource c -> Bool
isTestsOnly :: PragmaSource c -> Bool
isTestsOnly (PragmaVisibility [c]
_ CodeVisibility
TestsOnly) = Bool
True
isTestsOnly PragmaSource c
_                              = Bool
False