1 {-# LANGUAGE ConstraintKinds #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
6 import Test.Tasty.HUnit
8 import Control.Applicative (Applicative(..))
10 import Data.Semigroup ((<>))
11 import Data.String (IsString(..))
12 import Prelude hiding (any, (^), exp)
13 import qualified Control.Applicative as Gram_AltApp
14 import qualified Data.Char as Char
15 import qualified Data.Text as Text
16 import qualified Text.Megaparsec as P
18 import Language.Symantic.Grammar
21 type ParsecC e s = (P.Token s ~ Char, P.Stream s, P.ErrorComponent e)
22 instance ParsecC e s => IsString (P.ParsecT e s m [Char]) where
24 instance ParsecC e s => Gram_Rule (P.ParsecT e s m) where
25 rule = P.label . Text.unpack
26 instance ParsecC e s => Gram_Terminal (P.ParsecT e s m) where
31 unicat cat = P.satisfy $ (`elem` cats) . Char.generalCategory
32 where cats = unicode_categories cat
33 range (l, h) = P.satisfy $ \c -> l <= c && c <= h
34 but (Terminal f) (Terminal p) = Terminal $ P.notFollowedBy (P.try p) *> f
35 instance ParsecC e s => Gram_Alt (P.ParsecT e s m) where
36 empty = Gram_AltApp.empty
37 (<+>) = (Gram_AltApp.<|>)
39 instance ParsecC e s => Gram_Try (P.ParsecT e s m) where
41 instance ParsecC e s => Gram_RegR (P.ParsecT e s m) where
42 Terminal f .*> Reg x = Reg $ f <*> x
43 instance ParsecC e s => Gram_RegL (P.ParsecT e s m) where
44 Reg f <*. Terminal x = Reg $ f <*> x
45 instance ParsecC e s => Gram_App (P.ParsecT e s m)
46 instance ParsecC e s => Gram_AltApp (P.ParsecT e s m)
47 instance ParsecC e s => Gram_CF (P.ParsecT e s m) where
48 CF f <& Reg p = CF $ P.lookAhead f <*> p
49 Reg f &> CF p = CF $ P.lookAhead f <*> p
50 CF f `minus` Reg p = CF $ P.notFollowedBy (P.try p) *> f
51 instance ParsecC e s => Gram_Comment (P.ParsecT e s m)
53 elide :: Text.Text -> String
54 elide s | Text.length s > 42 = take 42 (Text.unpack s) <> ['…']
55 elide s = Text.unpack s
58 tests = testGroup "Grammar"
59 [ testGroup "Terminal" $
61 testCase (elide exp) $
62 runEBNF (unTerminal (void inp)) @?= exp
64 [ string "" ==> "\"\""
65 , string "abé\"to" ==> "\"abé\", U+34, \"to\""
66 , string "\"" ==> "U+34"
67 , range ('a', 'z') ==> "\"a\"…\"z\""
68 , unicat Unicat_Letter ==> "Unicat_Letter"
69 , unicat (Unicat Char.LowercaseLetter) ==> "Unicat LowercaseLetter"
73 testCase (elide exp) $
74 runEBNF (unReg (void inp)) @?= exp
76 [ (<>) <$> string "0" .*> someR (char '1') ==> "\"0\", {\"1\"}-"
77 , (<>) <$> someL (char '1') <*. string "0" ==> "{\"1\"}-, \"0\""
81 testCase (elide exp) $
82 runEBNF (unCF (void inp)) @?= exp
84 [ (<>) <$> string "0" <*> string "1" ==> "\"0\", \"1\""
85 , (<>) <$> string "0" <* string "X" <*> string "1" ==> "\"0\", \"X\", \"1\""
86 , (<>) <$> (string "0" <+> string "1") <*> string "2" ==> "(\"0\" | \"1\"), \"2\""
87 , (<>) <$> string "0" <*> (string "1" <+> string "2") ==> "\"0\", (\"1\" | \"2\")"
88 , string "0" <+> string "1" <+> string "2" ==> "\"0\" | \"1\" | \"2\""
89 , choice [string "0", string "1", string "2"] ==> "\"0\" | \"1\" | \"2\""
91 [ (<>) <$> string "0" <*> string "1"
92 , string "2" <+> string "3"
94 ] <*> string "5" ==> "(\"0\", \"1\" | \"2\" | \"3\" | \"4\"), \"5\""
95 , concat <$> many (string "0") ==> "{\"0\"}"
96 , () <$ char 'a' <* char 'b' <* char 'c' ==> "\"a\", \"b\", \"c\""
97 ,let g0 = (<>) <$> string "0" .*> someR (char '1') in
98 (<>) <$> string "0" <& g0 ==> "\"0\" & \"0\", {\"1\"}-"
99 ,let g0 = (<>) <$> string "0" .*> someR (char '1') in
100 let g1 = (<>) <$> someL (char '1') <*. string "0" in
101 string "0" `minus` g0 `minus` g1 ==>
102 "\"0\" - \"0\", {\"1\"}- - {\"1\"}-, \"0\""
104 <$> many (string "0" <+> string "1")
105 <*> some (string "2") ==> "{\"0\" | \"1\"}, {\"2\"}-"
112 testGroup "Language.Symantic"