]> Git — Sourcephile - haskell/symantic.git/blob - symantic-grammar/Language/Symantic/Grammar/Test.hs
Fix symantic-grammar test.
[haskell/symantic.git] / symantic-grammar / Language / Symantic / Grammar / Test.hs
1 {-# LANGUAGE ConstraintKinds #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 module Test where
4
5 import Test.Tasty
6 import Test.Tasty.HUnit
7
8 import Control.Applicative (Applicative(..))
9 import Control.Monad
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
17
18 import Language.Symantic.Grammar
19
20 -- * Type 'ParsecT'
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
23 fromString = P.string
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
27 any = P.anyChar
28 eoi = P.eof
29 char = P.char
30 string = P.string
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.<|>)
38 choice = P.choice
39 instance ParsecC e s => Gram_Try (P.ParsecT e s m) where
40 try = P.try
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)
52
53 elide :: Text.Text -> String
54 elide s | Text.length s > 42 = take 42 (Text.unpack s) <> ['…']
55 elide s = Text.unpack s
56
57 tests :: TestTree
58 tests = testGroup "Grammar"
59 [ testGroup "Terminal" $
60 let (==>) inp exp =
61 testCase (elide exp) $
62 runEBNF (unTerminal (void inp)) @?= exp
63 ; infix 1 ==> in
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"
70 ]
71 , testGroup "Reg" $
72 let (==>) inp exp =
73 testCase (elide exp) $
74 runEBNF (unReg (void inp)) @?= exp
75 ; infix 1 ==> in
76 [ (<>) <$> string "0" .*> someR (char '1') ==> "\"0\", {\"1\"}-"
77 , (<>) <$> someL (char '1') <*. string "0" ==> "{\"1\"}-, \"0\""
78 ]
79 , testGroup "CF" $
80 let (==>) inp exp =
81 testCase (elide exp) $
82 runEBNF (unCF (void inp)) @?= exp
83 ; infix 1 ==> in
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\""
90 , (<>) <$> choice
91 [ (<>) <$> string "0" <*> string "1"
92 , string "2" <+> string "3"
93 , string "4"
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\""
103 , (<>)
104 <$> many (string "0" <+> string "1")
105 <*> some (string "2") ==> "{\"0\" | \"1\"}, {\"2\"}-"
106 ]
107 ]
108
109 main :: IO ()
110 main =
111 defaultMain $
112 testGroup "Language.Symantic"
113 [tests]