ParserSpec.hs 3.49 KB
Newer Older
1
2
3
4
5
6
module MA.FunctorExpression.ParserSpec (spec) where

import Test.Hspec
import Test.Hspec.Megaparsec

import Data.Functor.Classes
7
import Type.Reflection
8
9
10
11
12

import Data.Text (Text)

import MA.FunctorExpression.Type
import MA.FunctorExpression.Parser
13
14
import MA.Parser.Lexer
import MA.Parser.Types
15
16
17
18
19

spec :: Spec
spec = do
  parseFunctorExpressionSpec

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

parseFunctorExpressionSpec :: Spec
parseFunctorExpressionSpec = describe "parseFunctorExpression" $ do
  it "refuses an empty file" $
    parsing `shouldFailOn` ""

  it "refuses a functor expression without functor symbol" $
    parsing `shouldFailOn` "X"

  it "parses a single prefix operator" $
    parsing "Pre X" `shouldParse` (Functor 2 (SomeFunctor (PrefixFunctor (Variable))))

  it "parses a single postfix operator" $
    parsing "X Post" `shouldParse`
    (Functor 1 (SomeFunctor (PostfixFunctor Variable)))

  it "parses nested operators" $ do
    parsing "Pre (Pre X)" `shouldParse`
      (Functor
         2
         (SomeFunctor
            (PrefixFunctor (Functor 2 (SomeFunctor (PrefixFunctor Variable))))))
    parsing "(X Post) Post" `shouldParse`
      (Functor
         1
         (SomeFunctor
            (PostfixFunctor (Functor 1 (SomeFunctor (PostfixFunctor Variable))))))
    parsing "Pre (X Post)" `shouldParse`
      (Functor
         2
         (SomeFunctor
            (PrefixFunctor (Functor 1 (SomeFunctor (PostfixFunctor Variable))))))

  it "fails to parse an expression without X" $
    parsing `shouldFailOn` "Pre"

  it "gets precedence right" $
    parsing "Pre X Post" `shouldParse`
    (Functor
       1
       (SomeFunctor
          (PostfixFunctor (Functor 2 (SomeFunctor (PrefixFunctor Variable))))))

  it "handles whitespace at the begining" $
    parsing `shouldSucceedOn` "  Pre X"

66

67
68
-- A simple prefix parser

69
70
71
72
73
74
data PrefixFunctor a = PrefixFunctor a
  deriving (Functor, Foldable, Traversable, Show)

instance Eq1 PrefixFunctor where
  liftEq f (PrefixFunctor x) (PrefixFunctor y) = f x y

75
76
instance Show1 PrefixFunctor where
  liftShowsPrec show' _ prec (PrefixFunctor x) rest
77
78
    | prec > 10 = "(PrefixFunctor " ++ show' 11 x (rest ++ ")")
    | otherwise = "PrefixFunctor " ++ show' 11 x rest
79

80
prefixParser :: FunctorParser PrefixFunctor
81
prefixParser = prefix (symbol "Pre" >> return PrefixFunctor)
82

83
-- A simple postfix parser
84
85
86
87
88
89
90

data PostfixFunctor a = PostfixFunctor a
  deriving (Functor, Foldable, Traversable, Show)

instance Eq1 PostfixFunctor where
  liftEq f (PostfixFunctor x) (PostfixFunctor y) = f x y

91
92
93
94
95
instance Show1 PostfixFunctor where
  liftShowsPrec show' _ prec (PostfixFunctor x) rest
    | prec > 10 = "(PostfixFunctor " ++ show' 11 x (rest ++ ")")
    | otherwise = "PostfixFunctor " ++ show' 11 x rest

96
postfixParser :: FunctorParser PostfixFunctor
97
postfixParser = postfix (symbol "Post" >> return PostfixFunctor)
98

99
-- List of functors
100

101
data SomeFunctor a where
102
  SomeFunctor :: (Eq1 f, Typeable f, Show1 f) => f a -> SomeFunctor a
103

104
105
106
107
108
instance Eq1 SomeFunctor where
  liftEq eq (SomeFunctor (f1 :: tf1 a)) (SomeFunctor (f2 :: tf2 b)) =
    case eqTypeRep (typeRep @tf1) (typeRep @tf2) of
      Nothing -> False -- different types
      Just HRefl -> liftEq eq f1 f2
109

110
111
112
instance Show1 SomeFunctor where
  liftShowsPrec show' showList' prec (SomeFunctor f) = liftShowsPrec show' showList' prec f

113
114
functors :: [[FunctorParser SomeFunctor]]
functors = [[transParser SomeFunctor prefixParser], [transParser SomeFunctor postfixParser]]
115

116
-- Helpers
117

118
119
parsing :: Text -> Either ParseErr (FunctorExpression SomeFunctor Precedence)
parsing = parseFunctorExpression functors ""