1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE UndecidableInstances #-}
3 {-# OPTIONS_GHC -fno-warn-orphans #-}
4 -- | Symantic instances for Megaparsec
5 module Grammar.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 Text.Megaparsec as P
24 import Language.Symantic.Grammar as Sym
25 import qualified Language.Symantic as Sym
31 -- NonEmpty P.SourcePos
32 instance ParsecC e s => Sym.Gram_Reader (NonEmpty P.SourcePos) (P.ParsecT e s m) where
34 s <- P.statePos <$> P.getParserState
38 f . P.statePos <$> P.getParserState
39 type instance MC.CanDo (P.ParsecT e s m) (MC.EffReader (NonEmpty P.SourcePos)) = 'True
40 instance ParsecC e s => MC.MonadReaderN 'MC.Zero (NonEmpty P.SourcePos) (P.ParsecT e s m) where
41 askN _n = P.statePos <$> P.getParserState
43 instance ParsecC e s => Sym.Gram_Reader P.SourcePos (P.ParsecT e s m) where
50 type instance MC.CanDo (P.ParsecT e s m) (MC.EffReader P.SourcePos) = 'True
51 instance ParsecC e s => MC.MonadReaderN 'MC.Zero P.SourcePos (P.ParsecT e s m) where
52 askN _n = P.getPosition
54 instance ParsecC e s => Sym.Gram_Reader () (P.ParsecT e s m) where
55 g_ask_before = fmap ($ ())
56 g_ask_after = 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
91 -- | Convenient alias for defining instances involving 'P.ParsecT'.
92 type ParsecC e s = (P.Token s ~ Char, P.Stream s, P.ErrorComponent e)
93 instance ParsecC e s => IsString (P.ParsecT e s m [Char]) where
99 instance (ParsecC e s, Show err) => Sym.Gram_Error err (P.ParsecT e s m) where
103 Left err -> fail $ show err
105 instance ParsecC e s => Sym.Gram_Rule (P.ParsecT e s m) where
106 rule = P.label . Text.unpack
107 instance ParsecC e s => Sym.Gram_Terminal (P.ParsecT e s m) where
112 unicat cat = P.satisfy $ (`elem` cats) . Char.generalCategory
113 where cats = unicode_categories cat
114 range (l, h) = P.satisfy $ \c -> l <= c && c <= h
115 Terminal f `but` Terminal p = Terminal $ P.notFollowedBy (P.try p) *> f
116 instance ParsecC e s => Sym.Gram_Alt (P.ParsecT e s m) where
120 instance ParsecC e s => Sym.Gram_Try (P.ParsecT e s m) where
122 instance ParsecC e s => Sym.Gram_RegR (P.ParsecT e s m) where
123 Terminal f .*> Reg x = Reg $ f <*> x
124 instance ParsecC e s => Sym.Gram_RegL (P.ParsecT e s m) where
125 Reg f <*. Terminal x = Reg $ f <*> x
126 instance ParsecC e s => Sym.Gram_App (P.ParsecT e s m) where
128 instance ParsecC e s => Sym.Gram_AltApp (P.ParsecT e s m) where
130 optional = P.optional
133 skipMany = P.skipMany
134 instance ParsecC e s => Sym.Gram_CF (P.ParsecT e s m) where
135 CF f <& Reg p = CF $ P.lookAhead f <*> p
136 Reg f &> CF p = CF $ P.lookAhead f <*> p
137 minus (CF f) (Reg p) = CF $ P.notFollowedBy (P.try p) *> f
138 instance ParsecC e s => Sym.Gram_Comment (P.ParsecT e s m)
139 instance ParsecC e s => Sym.Gram_Op (P.ParsecT e s m)
140 instance ParsecC e s => Sym.Gram_Name (P.ParsecT e s m)
141 instance -- Sym.Gram_Type
143 , Gram_Source src (P.ParsecT e s m)
144 ) => Sym.Gram_Type src (P.ParsecT e s m)
145 instance -- Sym.Gram_Term_Type
147 , Gram_Source src (P.ParsecT e s m)
148 ) => Sym.Gram_Term_Type src (P.ParsecT e s m)
149 instance -- Sym.Gram_Term
152 , MC.MonadState (Sym.Imports, Sym.Modules src ss) (P.ParsecT e s m)
153 , Sym.Gram_Source src (P.ParsecT e s m)
154 , Sym.Gram_Term_Atoms src ss (P.ParsecT e s m)
155 ) => Sym.Gram_Term src ss (P.ParsecT e s m)