{- -----------------------------------------------------------------------------
Copyright 2020 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 Test.Parser (tests) where

import Control.Monad (when)

import Base.CompilerError
import Base.TrackedErrors
import Parser.Common
import Parser.TextParser
import Test.Common


tests :: [IO (TrackedErrors ())]
tests :: [IO (TrackedErrors ())]
tests = [
    TextParser Char -> [Char] -> Char -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs TextParser Char
stringChar [Char]
"\\'" Char
'\'',
    TextParser Char -> [Char] -> Char -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs TextParser Char
stringChar [Char]
"\\\"" Char
'"',
    TextParser Char -> [Char] -> Char -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs TextParser Char
stringChar [Char]
"\\?" Char
'?',
    TextParser Char -> [Char] -> Char -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs TextParser Char
stringChar [Char]
"\\\\" Char
'\\',
    TextParser Char -> [Char] -> Char -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs TextParser Char
stringChar [Char]
"\\a" Char
'\a',
    TextParser Char -> [Char] -> Char -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs TextParser Char
stringChar [Char]
"\\b" Char
'\b',
    TextParser Char -> [Char] -> Char -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs TextParser Char
stringChar [Char]
"\\f" Char
'\f',
    TextParser Char -> [Char] -> Char -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs TextParser Char
stringChar [Char]
"\\n" Char
'\n',
    TextParser Char -> [Char] -> Char -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs TextParser Char
stringChar [Char]
"\\r" Char
'\r',
    TextParser Char -> [Char] -> Char -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs TextParser Char
stringChar [Char]
"\\t" Char
'\t',
    TextParser Char -> [Char] -> Char -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs TextParser Char
stringChar [Char]
"\\v" Char
'\v',
    TextParser Char -> [Char] -> Char -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs TextParser Char
stringChar [Char]
"\n" Char
'\n',
    TextParser Char -> [Char] -> Char -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs TextParser Char
stringChar [Char]
"\\x0A" Char
'\n',
    TextParser Char -> [Char] -> Char -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs TextParser Char
stringChar [Char]
"\\012" Char
'\n',
    TextParser Char -> [Char] -> IO (TrackedErrors ())
forall a. Show a => TextParser a -> [Char] -> IO (TrackedErrors ())
checkParseFail TextParser Char
stringChar [Char]
"\"",
    TextParser Char -> [Char] -> IO (TrackedErrors ())
forall a. Show a => TextParser a -> [Char] -> IO (TrackedErrors ())
checkParseFail TextParser Char
stringChar [Char]
"\\q",
    TextParser Char -> [Char] -> IO (TrackedErrors ())
forall a. Show a => TextParser a -> [Char] -> IO (TrackedErrors ())
checkParseFail TextParser Char
stringChar [Char]
"\\00",
    TextParser Char -> [Char] -> IO (TrackedErrors ())
forall a. Show a => TextParser a -> [Char] -> IO (TrackedErrors ())
checkParseFail TextParser Char
stringChar [Char]
"\\x0",

    TextParser [Char] -> [Char] -> [Char] -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs TextParser [Char]
regexChar [Char]
"\\\\" [Char]
"\\\\",
    TextParser [Char] -> [Char] -> [Char] -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs TextParser [Char]
regexChar [Char]
"\\n" [Char]
"\\n",
    TextParser [Char] -> [Char] -> [Char] -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs TextParser [Char]
regexChar [Char]
"\n" [Char]
"\n",
    TextParser [Char] -> [Char] -> [Char] -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs TextParser [Char]
regexChar [Char]
"\\\"" [Char]
"\"",
    TextParser [Char] -> [Char] -> IO (TrackedErrors ())
forall a. Show a => TextParser a -> [Char] -> IO (TrackedErrors ())
checkParseFail TextParser [Char]
regexChar [Char]
"\"",

    TextParser [Char] -> [Char] -> [Char] -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs ([Char] -> TextParser ()
keyword [Char]
"keyword" TextParser () -> TextParser [Char] -> TextParser [Char]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> TextParser Char -> TextParser [Char]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
some TextParser Char
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
m (Token s)
asciiChar) [Char]
"keyword   string" [Char]
"string",
    TextParser [Char] -> [Char] -> IO (TrackedErrors ())
forall a. Show a => TextParser a -> [Char] -> IO (TrackedErrors ())
checkParseFail ([Char] -> TextParser ()
keyword [Char]
"keyword" TextParser () -> TextParser [Char] -> TextParser [Char]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> TextParser Char -> TextParser [Char]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
some TextParser Char
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
m (Token s)
asciiChar) [Char]
"keywordstring",
    TextParser [Char] -> [Char] -> IO (TrackedErrors ())
forall a. Show a => TextParser a -> [Char] -> IO (TrackedErrors ())
checkParseFail ([Char] -> TextParser ()
keyword [Char]
"keyword" TextParser () -> TextParser [Char] -> TextParser [Char]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> TextParser Char -> TextParser [Char]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
some TextParser Char
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
m (Token s)
asciiChar) [Char]
"keyword_string",
    TextParser (Either [Char] [Char])
-> [Char] -> Either [Char] [Char] -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs
      ((([Char] -> Either [Char] [Char])
-> TextParser [Char] -> TextParser (Either [Char] [Char])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Char] -> Either [Char] [Char]
forall a b. a -> Either a b
Left (TextParser [Char] -> TextParser (Either [Char] [Char]))
-> TextParser [Char] -> TextParser (Either [Char] [Char])
forall a b. (a -> b) -> a -> b
$ [Char] -> TextParser ()
keyword [Char]
"keyword" TextParser () -> TextParser [Char] -> TextParser [Char]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> TextParser Char -> TextParser [Char]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
some TextParser Char
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
m (Token s)
asciiChar) TextParser (Either [Char] [Char])
-> TextParser (Either [Char] [Char])
-> TextParser (Either [Char] [Char])
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (([Char] -> Either [Char] [Char])
-> TextParser [Char] -> TextParser (Either [Char] [Char])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Char] -> Either [Char] [Char]
forall a b. b -> Either a b
Right (TextParser [Char] -> TextParser (Either [Char] [Char]))
-> TextParser [Char] -> TextParser (Either [Char] [Char])
forall a b. (a -> b) -> a -> b
$ TextParser Char -> TextParser [Char]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
some TextParser Char
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
m (Token s)
asciiChar))
      [Char]
"keywordstring"
      ([Char] -> Either [Char] [Char]
forall a b. b -> Either a b
Right [Char]
"keywordstring"),

    TextParser [Char] -> [Char] -> [Char] -> IO (TrackedErrors ())
forall a.
(Eq a, Show a) =>
TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs ([Char] -> TextParser [Char]
operator [Char]
">>??!" TextParser [Char] -> TextParser [Char] -> TextParser [Char]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> TextParser Char -> TextParser [Char]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
many TextParser Char
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
m (Token s)
asciiChar) [Char]
">>??!   !!" [Char]
"!!",
    TextParser [Char] -> [Char] -> IO (TrackedErrors ())
forall a. Show a => TextParser a -> [Char] -> IO (TrackedErrors ())
checkParseFail ([Char] -> TextParser [Char]
operator [Char]
">>??!" TextParser [Char] -> TextParser [Char] -> TextParser [Char]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> TextParser Char -> TextParser [Char]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
many TextParser Char
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
m (Token s)
asciiChar) [Char]
">>??!!!"
  ]

checkParsesAs :: (Eq a, Show a) => TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs :: TextParser a -> [Char] -> a -> IO (TrackedErrors ())
checkParsesAs TextParser a
p [Char]
s a
m = TrackedErrors () -> IO (TrackedErrors ())
forall (m :: * -> *) a. Monad m => a -> m a
return (TrackedErrors () -> IO (TrackedErrors ()))
-> TrackedErrors () -> IO (TrackedErrors ())
forall a b. (a -> b) -> a -> b
$ do
  let parsed :: TrackedErrors a
parsed = TextParser a -> [Char] -> [Char] -> TrackedErrors a
forall a. TextParser a -> [Char] -> [Char] -> TrackedErrors a
readSingleWith TextParser a
p [Char]
"(string)" [Char]
s
  TrackedErrors a -> TrackedErrors ()
forall (m :: * -> *) a.
ErrorContextM m =>
TrackedErrorsT Identity a -> m ()
check TrackedErrors a
parsed
  a
e <- TrackedErrors a
parsed
  Bool -> TrackedErrors () -> TrackedErrors ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (a
e a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
m) (TrackedErrors () -> TrackedErrors ())
-> TrackedErrors () -> TrackedErrors ()
forall a b. (a -> b) -> a -> b
$
    [Char] -> TrackedErrors ()
forall (m :: * -> *) a. ErrorContextM m => [Char] -> m a
compilerErrorM ([Char] -> TrackedErrors ()) -> [Char] -> TrackedErrors ()
forall a b. (a -> b) -> a -> b
$ [Char] -> [Char]
forall a. Show a => a -> [Char]
show [Char]
s [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" does not parse as " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
m [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
":\n" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
e
  where
    check :: TrackedErrorsT Identity a -> m ()
check TrackedErrorsT Identity a
c
      | TrackedErrorsT Identity a -> Bool
forall (t :: (* -> *) -> * -> *) a.
(ErrorContextT t, ErrorContextM (t Identity)) =>
t Identity a -> Bool
isCompilerError TrackedErrorsT Identity a
c = [Char] -> m ()
forall (m :: * -> *) a. ErrorContextM m => [Char] -> m a
compilerErrorM ([Char] -> m ()) -> [Char] -> m ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Parse '" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
s [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"':\n" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ CompilerMessage -> [Char]
forall a. Show a => a -> [Char]
show (TrackedErrorsT Identity a -> CompilerMessage
forall a. TrackedErrors a -> CompilerMessage
getCompilerError TrackedErrorsT Identity a
c)
      | Bool
otherwise = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

checkParseFail :: Show a => TextParser a -> [Char] -> IO (TrackedErrors ())
checkParseFail :: TextParser a -> [Char] -> IO (TrackedErrors ())
checkParseFail TextParser a
p [Char]
s = do
  let parsed :: TrackedErrors a
parsed = TextParser a -> [Char] -> [Char] -> TrackedErrors a
forall a. TextParser a -> [Char] -> [Char] -> TrackedErrors a
readSingleWith TextParser a
p [Char]
"(string)" [Char]
s
  TrackedErrors () -> IO (TrackedErrors ())
forall (m :: * -> *) a. Monad m => a -> m a
return (TrackedErrors () -> IO (TrackedErrors ()))
-> TrackedErrors () -> IO (TrackedErrors ())
forall a b. (a -> b) -> a -> b
$ TrackedErrors a -> TrackedErrors ()
forall (m :: * -> *) a.
(ErrorContextM m, Show a) =>
TrackedErrorsT Identity a -> m ()
check TrackedErrors a
parsed
  where
    check :: TrackedErrorsT Identity a -> m ()
check TrackedErrorsT Identity a
c
      | TrackedErrorsT Identity a -> Bool
forall (t :: (* -> *) -> * -> *) a.
(ErrorContextT t, ErrorContextM (t Identity)) =>
t Identity a -> Bool
isCompilerError TrackedErrorsT Identity a
c = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      | Bool
otherwise = [Char] -> m ()
forall (m :: * -> *) a. ErrorContextM m => [Char] -> m a
compilerErrorM ([Char] -> m ()) -> [Char] -> m ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Parse '" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
s [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"': Expected failure but got\n" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
                                     a -> [Char]
forall a. Show a => a -> [Char]
show (TrackedErrorsT Identity a -> a
forall a. TrackedErrors a -> a
getCompilerSuccess TrackedErrorsT Identity a
c) [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"\n"