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.List.NonEmpty (NonEmpty(..))
14 import Data.Ord (Ord(..))
15 import Data.String (IsString(..))
16 import Data.Typeable ()
17 import Text.Show (Show(..))
18 import qualified Control.Applicative as Alt
19 import qualified Control.Monad.Classes as MC
20 import qualified Data.Char as Char
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
40 -- NonEmpty P.SourcePos
41 instance ParsecC e s => Sym.Gram_Reader (NonEmpty P.SourcePos) (P.ParsecT e s m) where
43 s <- P.statePos <$> P.getParserState
47 f . P.statePos <$> P.getParserState
48 type instance MC.CanDo (P.ParsecT e s m) (MC.EffReader (NonEmpty P.SourcePos)) = 'True
49 instance ParsecC e s => MC.MonadReaderN 'MC.Zero (NonEmpty P.SourcePos) (P.ParsecT e s m) where
50 askN _n = P.statePos <$> P.getParserState
52 instance ParsecC e s => Sym.Gram_Reader P.SourcePos (P.ParsecT e s m) where
59 type instance MC.CanDo (P.ParsecT e s m) (MC.EffReader P.SourcePos) = 'True
60 instance ParsecC e s => MC.MonadReaderN 'MC.Zero P.SourcePos (P.ParsecT e s m) where
61 askN _n = P.getPosition
63 instance ParsecC e s => Sym.Gram_Reader () (P.ParsecT e s m) where
64 askBefore = fmap ($ ())
65 askAfter = fmap ($ ())
72 type instance MC.CanDo (P.ParsecT e s m) (MC.EffState st) = 'False
73 instance (Monad m, MC.MonadState st m) => Sym.Gram_State st m where
102 instance (ParsecC e s, Show err) => Sym.Gram_Error err (P.ParsecT e s m) where
103 catch me {- if you can :-} = do
106 Left err -> fail $ show err
108 instance ParsecC e s => Sym.Gram_Rule (P.ParsecT e s m) where
109 rule = P.label . Text.unpack
110 instance ParsecC e s => Sym.Gram_Char (P.ParsecT e s m) where
114 unicat cat = P.satisfy $ (`elem` cats) . Char.generalCategory
115 where cats = unicode_categories cat
116 range (l, h) = P.satisfy $ \c -> l <= c && c <= h
117 Terminal f `but` Terminal p = Terminal $ P.notFollowedBy (P.try p) *> f
118 instance ParsecC e String => Sym.Gram_String (P.ParsecT e String m) where
120 instance ParsecC e Text.Text => Sym.Gram_String (P.ParsecT e Text.Text m) where
121 string t = Text.unpack <$> P.string (Text.pack t)
123 textLazy t = TL.fromStrict <$> P.string (TL.toStrict t)
124 instance ParsecC e s => Sym.Gram_Alt (P.ParsecT e s m) where
128 instance ParsecC e s => Sym.Gram_Try (P.ParsecT e s m) where
130 instance ParsecC e s => Sym.Gram_RegR (P.ParsecT e s m) where
131 Terminal f .*> Reg x = Reg $ f <*> x
132 instance ParsecC e s => Sym.Gram_RegL (P.ParsecT e s m) where
133 Reg f <*. Terminal x = Reg $ f <*> x
134 instance ParsecC e s => Sym.Gram_App (P.ParsecT e s m) where
136 instance ParsecC e s => Sym.Gram_AltApp (P.ParsecT e s m) where
138 optional = P.optional
141 manySkip = P.skipMany
142 instance ParsecC e s => Sym.Gram_CF (P.ParsecT e s m) where
143 CF f <& Reg p = CF $ P.lookAhead f <*> p
144 Reg f &> CF p = CF $ P.lookAhead f <*> p
145 minus (CF f) (Reg p) = CF $ P.notFollowedBy (P.try p) *> f
146 instance (ParsecC e s, Sym.Gram_String (P.ParsecT e s m)) => Sym.Gram_Comment (P.ParsecT e s m)
147 instance (ParsecC e s, Sym.Gram_String (P.ParsecT e s m)) => Sym.Gram_Op (P.ParsecT e s m)
148 instance (ParsecC e s, Sym.Gram_String (P.ParsecT e s m)) => Sym.Gram_Mod (P.ParsecT e s m)
149 instance (ParsecC e s, Sym.Gram_String (P.ParsecT e s m)) => Sym.Gram_Type_Name (P.ParsecT e s m)
150 instance (ParsecC e s, Sym.Gram_String (P.ParsecT e s m)) => Sym.Gram_Term_Name (P.ParsecT e s m)
151 instance -- Sym.Gram_Type
153 , Sym.Gram_String (P.ParsecT e s m)
154 , Gram_Source src (P.ParsecT e s m)
156 , MC.MonadState ( Sym.Imports Sym.NameTy
157 , Sym.ModulesTy src )
159 ) => Sym.Gram_Type src (P.ParsecT e s m)
160 instance -- Sym.Gram_Term_Type
162 , Sym.Gram_String (P.ParsecT e s m)
164 , MC.MonadState ( Sym.Imports Sym.NameTy
165 , Sym.ModulesTy src )
167 , Gram_Source src (P.ParsecT e s m)
168 ) => Sym.Gram_Term_Type src (P.ParsecT e s m)
169 instance -- Sym.Gram_Term
171 , Sym.Gram_String (P.ParsecT e s m)
173 , MC.MonadState ( Sym.Imports Sym.NameTy
174 , Sym.ModulesTy src )
176 , MC.MonadState ( Sym.Imports Sym.NameTe
177 , Sym.Modules src ss )
179 , Sym.Gram_Source src (P.ParsecT e s m)
180 , Sym.Gram_Term_Atoms src ss (P.ParsecT e s m)
181 ) => Sym.Gram_Term src ss (P.ParsecT e s m)