]> Git — Sourcephile - haskell/symantic.git/blob - symantic-grammar/Language/Symantic/Grammar/Test.hs
Split into symantic{,-grammar,-lib}.
[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 qualified Control.Applicative as Alt
10 import Control.Monad
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
19
20 import Language.Symantic.Grammar
21
22 -- * Type 'ParsecT'
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
25 fromString = P.string
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
29 any = P.anyChar
30 eoi = P.eof
31 char = P.char
32 string = P.string
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
38 empty = Alt.empty
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
51 metaG p = do
52 pos <- P.getPosition
53 ($ pos) <$> p
54 instance ParsecC e s => Gram_Lexer (P.ParsecT e s m)
55
56 runParserT :: Monad 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 ""
60
61 runParser
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 ""
65
66 elide :: String -> String
67 elide s | length s > 42 = take 42 s ++ ['…']
68 elide s = s
69
70 tests :: TestTree
71 tests = testGroup "Grammar"
72 [ testGroup "Terminal" $
73 let (==>) inp exp =
74 testCase (elide $ Text.unpack exp) $
75 runEBNF (unTerminal (void inp)) @?= exp
76 ; infix 1 ==> in
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"
83 ]
84 , testGroup "Reg" $
85 let (==>) inp exp =
86 testCase (elide $ Text.unpack exp) $
87 runEBNF (unReg (void inp)) @?= exp
88 ; infix 1 ==> in
89 [ (<>) <$> string "0" .*> someR (char '1') ==> "\"0\", {\"1\"}-"
90 , (<>) <$> someL (char '1') <*. string "0" ==> "{\"1\"}-, \"0\""
91 ]
92 , testGroup "CF" $
93 let (==>) inp exp =
94 testCase (elide $ Text.unpack exp) $
95 runEBNF (unCF (void inp)) @?= exp
96 ; infix 1 ==> in
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\""
103 , (<>) <$> choice
104 [ (<>) <$> string "0" <*> string "1"
105 , string "2" <+> string "3"
106 , string "4"
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\""
116 , (<>)
117 <$> many (string "0" <+> string "1")
118 <*> some (string "2") ==> "{\"0\" | \"1\"}, {\"2\"}-"
119 ]
120 ]
121
122 main :: IO ()
123 main =
124 defaultMain $
125 testGroup "Language.Symantic"
126 [tests]