1 {-# LANGUAGE ConstraintKinds #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
6 import Test.Tasty.HUnit
8 import Control.Applicative (Applicative(..))
9 import qualified Control.Applicative as Alt
11 import qualified Data.Char as Char
12 import Data.Functor.Identity
13 import qualified Data.List as List
14 import Data.Monoid ((<>))
15 import Data.String (IsString(..))
16 import qualified Data.Text as Text
17 import Prelude hiding (any, (^), exp)
18 import qualified Text.Megaparsec as P
20 import Language.Symantic.Grammar
23 type ParsecC e s = (P.Token s ~ Char, P.Stream s, P.ErrorComponent e)
24 instance ParsecC e s => IsString (P.ParsecT e s m [Char]) where
26 instance ParsecC e s => Gram_Rule (P.ParsecT e s m) where
27 rule = P.label . Text.unpack
28 instance ParsecC e s => Gram_Terminal (P.ParsecT e s m) where
33 unicat cat = P.satisfy $ (`List.elem` cats) . Char.generalCategory
34 where cats = unicode_categories cat
35 range (l, h) = P.satisfy $ \c -> l <= c && c <= h
36 but (Terminal f) (Terminal p) = Terminal $ P.notFollowedBy (P.try p) *> f
37 instance ParsecC e s => Alter (P.ParsecT e s m) where
39 x <+> y = P.try x Alt.<|> y
40 instance ParsecC e s => Gram_RegR (P.ParsecT e s m) where
41 Terminal f .*> Reg x = Reg $ f <*> x
42 instance ParsecC e s => Gram_RegL (P.ParsecT e s m) where
43 Reg f <*. Terminal x = Reg $ f <*> x
44 instance ParsecC e s => App (P.ParsecT e s m)
45 instance ParsecC e s => Alt (P.ParsecT e s m)
46 instance ParsecC e s => Gram_CF (P.ParsecT e s m) where
47 CF f <& Reg p = CF $ P.lookAhead f <*> p
48 Reg f &> CF p = CF $ P.lookAhead f <*> p
49 minus (CF f) (Reg p) = CF $ P.notFollowedBy (P.try p) *> f
50 instance ParsecC e s => Gram_Meta P.SourcePos (P.ParsecT e s m) where
54 instance ParsecC e s => Gram_Lexer (P.ParsecT e s m)
57 => P.ParsecT P.Dec s m a -> s
58 -> m (Either (P.ParseError (P.Token s) P.Dec) a)
59 runParserT p = P.runParserT p ""
62 :: P.ParsecT P.Dec s Identity a -> s
63 -> Either (P.ParseError (P.Token s) P.Dec) a
64 runParser p = P.runParser p ""
66 elide :: String -> String
67 elide s | length s > 42 = take 42 s ++ ['…']
71 tests = testGroup "Grammar"
72 [ testGroup "Terminal" $
74 testCase (elide $ Text.unpack exp) $
75 runEBNF (unTerminal (void inp)) @?= exp
77 [ string "" ==> "\"\""
78 , string "abé\"to" ==> "\"abé\", U+34, \"to\""
79 , string "\"" ==> "U+34"
80 , range ('a', 'z') ==> "\"a\"…\"z\""
81 , unicat Unicat_Letter ==> "Unicat_Letter"
82 , unicat (Unicat Char.LowercaseLetter) ==> "Unicat LowercaseLetter"
86 testCase (elide $ Text.unpack exp) $
87 runEBNF (unReg (void inp)) @?= exp
89 [ (<>) <$> string "0" .*> someR (char '1') ==> "\"0\", {\"1\"}-"
90 , (<>) <$> someL (char '1') <*. string "0" ==> "{\"1\"}-, \"0\""
94 testCase (elide $ Text.unpack exp) $
95 runEBNF (unCF (void inp)) @?= exp
97 [ (<>) <$> string "0" <*> string "1" ==> "\"0\", \"1\""
98 , (<>) <$> string "0" <* string "X" <*> string "1" ==> "\"0\", \"X\", \"1\""
99 , (<>) <$> (string "0" <+> string "1") <*> string "2" ==> "(\"0\" | \"1\"), \"2\""
100 , (<>) <$> string "0" <*> (string "1" <+> string "2") ==> "\"0\", (\"1\" | \"2\")"
101 , string "0" <+> string "1" <+> string "2" ==> "\"0\" | \"1\" | \"2\""
102 , choice [string "0", string "1", string "2"] ==> "\"0\" | \"1\" | \"2\""
104 [ (<>) <$> string "0" <*> string "1"
105 , string "2" <+> string "3"
107 ] <*> string "5" ==> "(\"0\", \"1\" | \"2\" | \"3\" | \"4\"), \"5\""
108 , concat <$> many (string "0") ==> "{\"0\"}"
109 , () <$ char 'a' <* char 'b' <* char 'c' ==> "\"a\", \"b\", \"c\""
110 ,let g0 = (<>) <$> string "0" .*> someR (char '1') in
111 (<>) <$> string "0" <& g0 ==> "\"0\" & \"0\", {\"1\"}-"
112 ,let g0 = (<>) <$> string "0" .*> someR (char '1') in
113 let g1 = (<>) <$> someL (char '1') <*. string "0" in
114 string "0" `minus` g0 `minus` g1 ==>
115 "\"0\" - \"0\", {\"1\"}- - {\"1\"}-, \"0\""
117 <$> many (string "0" <+> string "1")
118 <*> some (string "2") ==> "{\"0\" | \"1\"}, {\"2\"}-"
125 testGroup "Language.Symantic"