1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE UndecidableInstances #-}
3 {-# OPTIONS_GHC -fno-warn-orphans #-}
4 -- | Symantic instances for Megaparsec
5 module Testing.Megaparsec where
7 import Control.Applicative (Applicative(..))
8 import Control.Monad (Monad(..))
9 import Data.Char (Char)
10 import Data.Either (Either(..))
11 import Data.Function (($), (.))
12 import Data.Functor (Functor(..), (<$>))
13 import Data.Ord (Ord(..))
14 import Data.String (String, IsString(..))
15 import Data.Typeable ()
16 import Text.Show (Show(..))
17 import qualified Control.Applicative as Alt
18 import qualified Control.Monad.Classes as MC
19 import qualified Data.Char as Char
20 import qualified Data.List as List
21 import qualified Data.Text as Text
22 import qualified Data.Text.Lazy as TL
23 import qualified Text.Megaparsec as P
24 import qualified Text.Megaparsec.Char as P
26 import Language.Symantic.Grammar as Sym
27 import qualified Language.Symantic as Sym
28 import Language.Symantic.Lib ()
31 -- | Convenient alias for defining instances involving 'P.ParsecT'.
32 type ParsecC e s = (P.Token s ~ Char, P.Stream s, Ord e)
33 instance ParsecC e [Char] => IsString (P.ParsecT e [Char] m [Char]) where
42 instance ParsecC e s => Sym.Gram_Reader P.SourcePos (P.ParsecT e s m) where
49 type instance MC.CanDo (P.ParsecT e s m) (MC.EffReader P.SourcePos) = 'True
50 instance ParsecC e s => MC.MonadReaderN 'MC.Zero P.SourcePos (P.ParsecT e s m) where
51 askN _n = P.getSourcePos
54 instance ParsecC e s => Sym.Gram_Reader () (P.ParsecT e s m) where
55 askBefore = fmap ($ ())
56 askAfter = fmap ($ ())
63 type instance MC.CanDo (P.ParsecT e s m) (MC.EffState st) = 'False
64 instance (Monad m, MC.MonadState st m) => Sym.Gram_State st m where
93 instance (ParsecC e s, Show err) => Sym.Gram_Error err (P.ParsecT e s m) where
94 catch me {- if you can :-} = do
97 Left err -> fail $ show err
99 instance ParsecC e s => Sym.Gram_Rule (P.ParsecT e s m) where
100 rule = P.label . Text.unpack
101 instance ParsecC e s => Sym.Gram_Char (P.ParsecT e s m) where
105 unicat cat = P.satisfy $ (`List.elem` cats) . Char.generalCategory
106 where cats = unicode_categories cat
107 range (l, h) = P.satisfy $ \c -> l <= c && c <= h
108 Terminal f `but` Terminal p = Terminal $ P.notFollowedBy (P.try p) *> f
109 instance ParsecC e String => Sym.Gram_String (P.ParsecT e String m) where
111 instance ParsecC e Text.Text => Sym.Gram_String (P.ParsecT e Text.Text m) where
112 string t = Text.unpack <$> P.string (Text.pack t)
114 textLazy t = TL.fromStrict <$> P.string (TL.toStrict t)
115 instance ParsecC e s => Sym.Gram_Alt (P.ParsecT e s m) where
119 instance ParsecC e s => Sym.Gram_Try (P.ParsecT e s m) where
121 instance ParsecC e s => Sym.Gram_RegR (P.ParsecT e s m) where
122 Terminal f .*> Reg x = Reg $ f <*> x
123 instance ParsecC e s => Sym.Gram_RegL (P.ParsecT e s m) where
124 Reg f <*. Terminal x = Reg $ f <*> x
125 instance ParsecC e s => Sym.Gram_App (P.ParsecT e s m) where
127 instance ParsecC e s => Sym.Gram_AltApp (P.ParsecT e s m) where
129 optional = P.optional
132 manySkip = P.skipMany
133 instance ParsecC e s => Sym.Gram_CF (P.ParsecT e s m) where
134 CF f <& Reg p = CF $ P.lookAhead f <*> p
135 Reg f &> CF p = CF $ P.lookAhead f <*> p
136 minus (CF f) (Reg p) = CF $ P.notFollowedBy (P.try p) *> f
137 instance (ParsecC e s, Sym.Gram_String (P.ParsecT e s m)) => Sym.Gram_Comment (P.ParsecT e s m)
138 instance (ParsecC e s, Sym.Gram_String (P.ParsecT e s m)) => Sym.Gram_Op (P.ParsecT e s m)
139 instance (ParsecC e s, Sym.Gram_String (P.ParsecT e s m)) => Sym.Gram_Mod (P.ParsecT e s m)
140 instance (ParsecC e s, Sym.Gram_String (P.ParsecT e s m)) => Sym.Gram_Type_Name (P.ParsecT e s m)
141 instance (ParsecC e s, Sym.Gram_String (P.ParsecT e s m)) => Sym.Gram_Term_Name (P.ParsecT e s m)
142 instance -- Sym.Gram_Type
144 , Sym.Gram_String (P.ParsecT e s m)
145 , Gram_Source src (P.ParsecT e s m)
147 , MC.MonadState ( Sym.Imports Sym.NameTy
148 , Sym.ModulesTy src )
150 ) => Sym.Gram_Type src (P.ParsecT e s m)
151 instance -- Sym.Gram_Term_Type
153 , Sym.Gram_String (P.ParsecT e s m)
155 , MC.MonadState ( Sym.Imports Sym.NameTy
156 , Sym.ModulesTy src )
158 , Gram_Source src (P.ParsecT e s m)
159 ) => Sym.Gram_Term_Type src (P.ParsecT e s m)
160 instance -- Sym.Gram_Term
162 , Sym.Gram_String (P.ParsecT e s m)
164 , MC.MonadState ( Sym.Imports Sym.NameTy
165 , Sym.ModulesTy src )
167 , MC.MonadState ( Sym.Imports Sym.NameTe
168 , Sym.Modules src ss )
170 , Sym.Gram_Source src (P.ParsecT e s m)
171 , Sym.Gram_Term_Atoms src ss (P.ParsecT e s m)
172 ) => Sym.Gram_Term src ss (P.ParsecT e s m)