symantic-grammar 0.0.0.20170623 → 0.1.0.20170703
raw patch · 7 files changed
+108/−96 lines, 7 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Language.Symantic.Grammar.ContextFree: cf_of_Reg :: Reg lr g a -> CF g a
- Language.Symantic.Grammar.ContextFree: cf_of_Terminal :: Terminal g a -> CF g a
- Language.Symantic.Grammar.Meta: At :: !src -> !a -> At src a
- Language.Symantic.Grammar.Meta: Span :: !src -> !src -> Span src
- Language.Symantic.Grammar.Meta: [at] :: At src a -> !src
- Language.Symantic.Grammar.Meta: [spanBegin] :: Span src -> !src
- Language.Symantic.Grammar.Meta: [spanEnd] :: Span src -> !src
- Language.Symantic.Grammar.Meta: [unAt] :: At src a -> !a
- Language.Symantic.Grammar.Meta: class Inj_Error a b
- Language.Symantic.Grammar.Meta: class Source src => Inj_Source a src
- Language.Symantic.Grammar.Meta: class Source src
- Language.Symantic.Grammar.Meta: class Source (SourceOf a) => Sourced a
- Language.Symantic.Grammar.Meta: data At src a
- Language.Symantic.Grammar.Meta: data Span src
- Language.Symantic.Grammar.Meta: g_ask_after :: Gram_Reader st g => g (st -> a) -> g a
- Language.Symantic.Grammar.Meta: g_ask_before :: Gram_Reader st g => g (st -> a) -> g a
- Language.Symantic.Grammar.Meta: g_catch :: Gram_Error err g => g (Either err a) -> g a
- Language.Symantic.Grammar.Meta: g_get_after :: (Gram_State st g, Functor g) => g (st -> a) -> g a
- Language.Symantic.Grammar.Meta: g_get_before :: (Gram_State st g, Functor g) => g (st -> a) -> g a
- Language.Symantic.Grammar.Meta: g_put :: (Gram_State st g, Functor g) => g (st, a) -> g a
- Language.Symantic.Grammar.Meta: g_source :: (Gram_Source src g, Functor g) => g (src -> a) -> g a
- Language.Symantic.Grammar.Meta: g_state_after :: Gram_State st g => g (st -> (st, a)) -> g a
- Language.Symantic.Grammar.Meta: g_state_before :: Gram_State st g => g (st -> (st, a)) -> g a
- Language.Symantic.Grammar.Meta: inj_Error :: Inj_Error a b => a -> b
- Language.Symantic.Grammar.Meta: inj_Source :: Inj_Source a src => a -> src
- Language.Symantic.Grammar.Meta: instance (GHC.Classes.Eq a, GHC.Classes.Eq src) => GHC.Classes.Eq (Language.Symantic.Grammar.Meta.At src a)
- Language.Symantic.Grammar.Meta: instance (GHC.Classes.Ord a, GHC.Classes.Ord src) => GHC.Classes.Ord (Language.Symantic.Grammar.Meta.At src a)
- Language.Symantic.Grammar.Meta: instance (GHC.Show.Show a, GHC.Show.Show src) => GHC.Show.Show (Language.Symantic.Grammar.Meta.At src a)
- Language.Symantic.Grammar.Meta: instance (Language.Symantic.Grammar.Meta.Gram_Reader (Language.Symantic.Grammar.Meta.Source_Input src) g, Language.Symantic.Grammar.Meta.Inj_Source (Language.Symantic.Grammar.Meta.Span (Language.Symantic.Grammar.Meta.Source_Input src)) src) => Language.Symantic.Grammar.Meta.Gram_Source src g
- Language.Symantic.Grammar.Meta: instance GHC.Base.Functor (Language.Symantic.Grammar.Meta.At src)
- Language.Symantic.Grammar.Meta: instance GHC.Classes.Eq src => GHC.Classes.Eq (Language.Symantic.Grammar.Meta.Span src)
- Language.Symantic.Grammar.Meta: instance GHC.Classes.Ord src => GHC.Classes.Ord (Language.Symantic.Grammar.Meta.Span src)
- Language.Symantic.Grammar.Meta: instance GHC.Show.Show src => GHC.Show.Show (Language.Symantic.Grammar.Meta.Span src)
- Language.Symantic.Grammar.Meta: instance Language.Symantic.Grammar.Meta.Inj_Error err e => Language.Symantic.Grammar.Meta.Inj_Error err (Data.Either.Either e a)
- Language.Symantic.Grammar.Meta: instance Language.Symantic.Grammar.Meta.Inj_Source a ()
- Language.Symantic.Grammar.Meta: instance Language.Symantic.Grammar.Meta.Source ()
- Language.Symantic.Grammar.Meta: lift_Error :: forall e0 err e1 a. Inj_Error e0 e1 => Inj_Error e1 err => Proxy e1 -> Either e0 a -> Either err a
- Language.Symantic.Grammar.Meta: noSource :: Source src => src
- Language.Symantic.Grammar.Meta: setSource :: Sourced a => a -> SourceOf a -> a
- Language.Symantic.Grammar.Meta: sourceOf :: Sourced a => a -> SourceOf a
+ Language.Symantic.Grammar.ContextFree: cfOf :: ContextFreeOf gram => gram g a -> CF g a
+ Language.Symantic.Grammar.ContextFree: class ContextFreeOf gram
+ Language.Symantic.Grammar.ContextFree: instance Language.Symantic.Grammar.ContextFree.ContextFreeOf (Language.Symantic.Grammar.Regular.Reg lr)
+ Language.Symantic.Grammar.ContextFree: instance Language.Symantic.Grammar.ContextFree.ContextFreeOf Language.Symantic.Grammar.Terminal.Terminal
+ Language.Symantic.Grammar.Error: class ErrorInj a b
+ Language.Symantic.Grammar.Error: errorInj :: ErrorInj a b => a -> b
+ Language.Symantic.Grammar.Error: instance Language.Symantic.Grammar.Error.ErrorInj err e => Language.Symantic.Grammar.Error.ErrorInj err (Data.Either.Either e a)
+ Language.Symantic.Grammar.Error: liftError :: forall e0 err e1 a. ErrorInj e0 e1 => ErrorInj e1 err => Proxy e1 -> Either e0 a -> Either err a
+ Language.Symantic.Grammar.Meta: askAfter :: Gram_Reader st g => g (st -> a) -> g a
+ Language.Symantic.Grammar.Meta: askBefore :: Gram_Reader st g => g (st -> a) -> g a
+ Language.Symantic.Grammar.Meta: catch :: Gram_Error err g => g (Either err a) -> g a
+ Language.Symantic.Grammar.Meta: getAfter :: (Gram_State st g, Functor g) => g (st -> a) -> g a
+ Language.Symantic.Grammar.Meta: getBefore :: (Gram_State st g, Functor g) => g (st -> a) -> g a
+ Language.Symantic.Grammar.Meta: instance (Language.Symantic.Grammar.Meta.Gram_Reader (Language.Symantic.Grammar.Source.Source_Input src) g, Language.Symantic.Grammar.Source.SourceInj (Language.Symantic.Grammar.Source.Span (Language.Symantic.Grammar.Source.Source_Input src)) src) => Language.Symantic.Grammar.Meta.Gram_Source src g
+ Language.Symantic.Grammar.Meta: put :: (Gram_State st g, Functor g) => g (st, a) -> g a
+ Language.Symantic.Grammar.Meta: stateAfter :: Gram_State st g => g (st -> (st, a)) -> g a
+ Language.Symantic.Grammar.Meta: stateBefore :: Gram_State st g => g (st -> (st, a)) -> g a
+ Language.Symantic.Grammar.Source: At :: !src -> !a -> At src a
+ Language.Symantic.Grammar.Source: Span :: !src -> !src -> Span src
+ Language.Symantic.Grammar.Source: [at] :: At src a -> !src
+ Language.Symantic.Grammar.Source: [spanBegin] :: Span src -> !src
+ Language.Symantic.Grammar.Source: [spanEnd] :: Span src -> !src
+ Language.Symantic.Grammar.Source: [unAt] :: At src a -> !a
+ Language.Symantic.Grammar.Source: class Source src
+ Language.Symantic.Grammar.Source: class Source src => SourceInj a src
+ Language.Symantic.Grammar.Source: class Source (SourceOf a) => Sourced a
+ Language.Symantic.Grammar.Source: data At src a
+ Language.Symantic.Grammar.Source: data Span src
+ Language.Symantic.Grammar.Source: instance (GHC.Classes.Eq a, GHC.Classes.Eq src) => GHC.Classes.Eq (Language.Symantic.Grammar.Source.At src a)
+ Language.Symantic.Grammar.Source: instance (GHC.Classes.Ord a, GHC.Classes.Ord src) => GHC.Classes.Ord (Language.Symantic.Grammar.Source.At src a)
+ Language.Symantic.Grammar.Source: instance (GHC.Show.Show a, GHC.Show.Show src) => GHC.Show.Show (Language.Symantic.Grammar.Source.At src a)
+ Language.Symantic.Grammar.Source: instance GHC.Base.Functor (Language.Symantic.Grammar.Source.At src)
+ Language.Symantic.Grammar.Source: instance GHC.Classes.Eq src => GHC.Classes.Eq (Language.Symantic.Grammar.Source.Span src)
+ Language.Symantic.Grammar.Source: instance GHC.Classes.Ord src => GHC.Classes.Ord (Language.Symantic.Grammar.Source.Span src)
+ Language.Symantic.Grammar.Source: instance GHC.Show.Show src => GHC.Show.Show (Language.Symantic.Grammar.Source.Span src)
+ Language.Symantic.Grammar.Source: instance Language.Symantic.Grammar.Source.Source ()
+ Language.Symantic.Grammar.Source: instance Language.Symantic.Grammar.Source.SourceInj a ()
+ Language.Symantic.Grammar.Source: noSource :: Source src => src
+ Language.Symantic.Grammar.Source: setSource :: Sourced a => a -> SourceOf a -> a
+ Language.Symantic.Grammar.Source: sourceInj :: SourceInj a src => a -> src
+ Language.Symantic.Grammar.Source: sourceOf :: Sourced a => a -> SourceOf a
+ Language.Symantic.Grammar.Source: withSource :: SourceInj src (SourceOf a) => Sourced a => a -> src -> a
- Language.Symantic.Grammar.ContextFree: class (Gram_Terminal g, Gram_Rule g, Gram_Alt g, Gram_App g, Gram_AltApp g, Gram_CF g) => Gram_Comment g where commentable = rule3 "commentable" $ \ space line block -> skipMany $ choice [space, line, block] comment_line prefix = rule "comment_line" $ prefix *> many (any `minus` (void (char '\n') <+> eoi)) comment_block begin end = rule "comment_block" $ begin *> many (any `minus` end) <* cf_of_Reg end lexeme = rule1 "lexeme" $ \ g -> g <* commentable (void $ string " " <+> string "\n\ \ ") (void $ comment_line (string "--")) (void $ comment_block (string "{-") (string "-}")) parens = rule1 "parens" $ between (lexeme $ char '(') (lexeme $ char ')') symbol = lexeme . string
+ Language.Symantic.Grammar.ContextFree: class (Gram_Terminal g, Gram_Rule g, Gram_Alt g, Gram_App g, Gram_AltApp g, Gram_CF g) => Gram_Comment g where commentable = rule3 "commentable" $ \ space line block -> skipMany $ choice [space, line, block] comment_line prefix = rule "comment_line" $ prefix *> many (any `minus` (void (char '\n') <+> eoi)) comment_block begin end = rule "comment_block" $ begin *> many (any `minus` end) <* cfOf end lexeme = rule1 "lexeme" $ \ g -> g <* commentable (void $ string " " <+> string "\n\ \ ") (void $ comment_line (string "--")) (void $ comment_block (string "{-") (string "-}")) parens = rule1 "parens" $ between (lexeme $ char '(') (lexeme $ char ')') symbol = lexeme . string
- Language.Symantic.Grammar.Meta: class (Gram_Reader (Source_Input src) g, Inj_Source (Span (Source_Input src)) src) => Gram_Source src g where g_source g = g_ask_after $ g_ask_before $ (\ f (beg :: Source_Input src) (end :: Source_Input src) -> f (inj_Source $ Span beg end :: src)) <$> g
+ Language.Symantic.Grammar.Meta: class (Gram_Reader (Source_Input src) g, SourceInj (Span (Source_Input src)) src) => Gram_Source src g where source g = askAfter $ askBefore $ (\ f (beg :: Source_Input src) (end :: Source_Input src) -> f (sourceInj $ Span beg end :: src)) <$> g
- Language.Symantic.Grammar.Meta: class Gram_State st g where g_get_before g = g_state_before ((\ f st -> (st, f st)) <$> g) g_get_after g = g_state_after ((\ f st -> (st, f st)) <$> g) g_put g = g_state_after ((\ (st, a) -> const (st, a)) <$> g)
+ Language.Symantic.Grammar.Meta: class Gram_State st g where getBefore g = stateBefore ((\ f st -> (st, f st)) <$> g) getAfter g = stateAfter ((\ f st -> (st, f st)) <$> g) put g = stateAfter ((\ (st, a) -> const (st, a)) <$> g)
- Language.Symantic.Grammar.Meta: source :: Inj_Source src (SourceOf a) => Sourced a => a -> src -> a
+ Language.Symantic.Grammar.Meta: source :: (Gram_Source src g, Functor g) => g (src -> a) -> g a
Files
- Language/Symantic/Grammar.hs +4/−0
- Language/Symantic/Grammar/ContextFree.hs +7/−6
- Language/Symantic/Grammar/EBNF.hs +5/−5
- Language/Symantic/Grammar/Error.hs +18/−0
- Language/Symantic/Grammar/Meta.hs +22/−84
- Language/Symantic/Grammar/Source.hs +49/−0
- symantic-grammar.cabal +3/−1
Language/Symantic/Grammar.hs view
@@ -6,6 +6,8 @@ , module Language.Symantic.Grammar.ContextFree , module Language.Symantic.Grammar.Operators , module Language.Symantic.Grammar.Meta+ , module Language.Symantic.Grammar.Error+ , module Language.Symantic.Grammar.Source , module Language.Symantic.Grammar.BinTree ) where @@ -16,4 +18,6 @@ import Language.Symantic.Grammar.ContextFree import Language.Symantic.Grammar.Operators import Language.Symantic.Grammar.Meta+import Language.Symantic.Grammar.Error+import Language.Symantic.Grammar.Source import Language.Symantic.Grammar.BinTree
Language/Symantic/Grammar/ContextFree.hs view
@@ -43,11 +43,12 @@ f bo (op, SideL) <> " - " <> g bo (op, SideR) where op = infixL 6 -cf_of_Terminal :: Terminal g a -> CF g a-cf_of_Terminal (Terminal g) = CF g--cf_of_Reg :: Reg lr g a -> CF g a-cf_of_Reg (Reg g) = CF g+class ContextFreeOf gram where+ cfOf :: gram g a -> CF g a+instance ContextFreeOf Terminal where+ cfOf (Terminal g) = CF g+instance ContextFreeOf (Reg lr) where+ cfOf (Reg g) = CF g -- ** Class 'Gram_CF' -- | Symantics for context-free grammars.@@ -118,7 +119,7 @@ prefix *> many (any `minus` (void (char '\n') <+> eoi)) comment_block :: CF g String -> Reg lr g String -> CF g String comment_block begin end = rule "comment_block" $- begin *> many (any `minus` end) <* cf_of_Reg end+ begin *> many (any `minus` end) <* cfOf end lexeme :: CF g a -> CF g a lexeme = rule1 "lexeme" $ \g -> g <* commentable
Language/Symantic/Grammar/EBNF.hs view
@@ -32,13 +32,13 @@ -- * 'Text' of the 'EBNF' rendition. newtype EBNF a = EBNF { unEBNF :: RuleMode -> (Infix, Side) -> Text } instance Gram_Reader st EBNF where- g_ask_before (EBNF e) = EBNF e- g_ask_after (EBNF e) = EBNF e+ askBefore (EBNF e) = EBNF e+ askAfter (EBNF e) = EBNF e instance Gram_State st EBNF where- g_state_before (EBNF e) = EBNF e- g_state_after (EBNF e) = EBNF e+ stateBefore (EBNF e) = EBNF e+ stateAfter (EBNF e) = EBNF e instance Gram_Error err EBNF where- g_catch (EBNF e) = EBNF e+ catch (EBNF e) = EBNF e -- | Get textual rendition of given 'EBNF'. runEBNF :: EBNF a -> Text
+ Language/Symantic/Grammar/Error.hs view
@@ -0,0 +1,18 @@+{-# LANGUAGE TypeApplications #-}+module Language.Symantic.Grammar.Error where++import Data.Proxy (Proxy)++-- * Class 'ErrorInj'+class ErrorInj a b where+ errorInj :: a -> b+instance ErrorInj err e => ErrorInj err (Either e a) where+ errorInj = Left . errorInj++liftError ::+ forall e0 err e1 a.+ ErrorInj e0 e1 =>+ ErrorInj e1 err =>+ Proxy e1 -> Either e0 a -> Either err a+liftError _e1 (Right a) = Right a+liftError _e1 (Left e) = Left $ errorInj @e1 @err $ errorInj @e0 @e1 e
Language/Symantic/Grammar/Meta.hs view
@@ -1,107 +1,45 @@ {-# LANGUAGE DefaultSignatures #-}-{-# LANGUAGE DeriveFunctor #-}-{-# LANGUAGE TypeApplications #-}-{-# LANGUAGE TypeFamilies #-} {-# LANGUAGE UndecidableInstances #-} module Language.Symantic.Grammar.Meta where -import Data.Function (const)-import Data.Proxy (Proxy(..))-import Data.Typeable (Typeable)+import Language.Symantic.Grammar.Source -- * Type 'Gram_Reader' class Gram_Reader st g where- g_ask_before :: g (st -> a) -> g a- g_ask_after :: g (st -> a) -> g a+ askBefore :: g (st -> a) -> g a+ askAfter :: g (st -> a) -> g a -- * Type 'Gram_State' class Gram_State st g where- g_state_before :: g (st -> (st, a)) -> g a- g_state_after :: g (st -> (st, a)) -> g a- g_get_before :: g (st -> a) -> g a- g_get_after :: g (st -> a) -> g a- g_put :: g (st, a) -> g a- default g_get_before :: Functor g => g (st -> a) -> g a- default g_get_after :: Functor g => g (st -> a) -> g a- default g_put :: Functor g => g (st, a) -> g a- g_get_before g = g_state_before ((\f st -> (st, f st)) <$> g)- g_get_after g = g_state_after ((\f st -> (st, f st)) <$> g)- g_put g = g_state_after ((\(st, a) -> const (st, a)) <$> g)+ stateBefore :: g (st -> (st, a)) -> g a+ stateAfter :: g (st -> (st, a)) -> g a+ getBefore :: g (st -> a) -> g a+ getAfter :: g (st -> a) -> g a+ put :: g (st, a) -> g a+ default getBefore :: Functor g => g (st -> a) -> g a+ default getAfter :: Functor g => g (st -> a) -> g a+ default put :: Functor g => g (st, a) -> g a+ getBefore g = stateBefore ((\f st -> (st, f st)) <$> g)+ getAfter g = stateAfter ((\f st -> (st, f st)) <$> g)+ put g = stateAfter ((\(st, a) -> const (st, a)) <$> g) -- * Class 'Gram_Error' -- | Symantics for handling errors at the semantic level (not the syntaxic one). class Gram_Error err g where- g_catch :: g (Either err a) -> g a---- * Class 'Inj_Error'-class Inj_Error a b where- inj_Error :: a -> b-instance Inj_Error err e => Inj_Error err (Either e a) where- inj_Error = Left . inj_Error--lift_Error ::- forall e0 err e1 a.- Inj_Error e0 e1 =>- Inj_Error e1 err =>- Proxy e1 -> Either e0 a -> Either err a-lift_Error _e1 (Right a) = Right a-lift_Error _e1 (Left e) = Left $ inj_Error @e1 @err $ inj_Error @e0 @e1 e---- * Class 'Source'-class Source src where- noSource :: src-instance Source () where- noSource = ()---- ** Class 'Inj_Source'-class Source src => Inj_Source a src where- inj_Source :: a -> src-instance Inj_Source a () where- inj_Source _ = ()---- ** Type family 'SourceOf'-type family SourceOf a---- ** Type 'Sourced'-class Source (SourceOf a) => Sourced a where- sourceOf :: a -> SourceOf a- setSource :: a -> SourceOf a -> a-infixl 5 `setSource`--source :: Inj_Source src (SourceOf a) => Sourced a => a -> src -> a-source a src = a `setSource` inj_Source src---- ** Type 'Source_Input'-type family Source_Input (src :: *) :: *-type instance Source_Input () = ()---- ** Type 'Span'-data Span src- = Span- { spanBegin :: !src- , spanEnd :: !src- } deriving (Eq, Ord, Show, Typeable)+ catch :: g (Either err a) -> g a --- ** Class 'Gram_Source'+-- * Class 'Gram_Source' class ( Gram_Reader (Source_Input src) g- , Inj_Source (Span (Source_Input src)) src+ , SourceInj (Span (Source_Input src)) src ) => Gram_Source src g where- g_source :: Functor g => g (src -> a) -> g a- g_source g =- g_ask_after $ g_ask_before $+ source :: Functor g => g (src -> a) -> g a+ source g =+ askAfter $ askBefore $ (\f (beg::Source_Input src) (end::Source_Input src) ->- f (inj_Source $ Span beg end::src))+ f (sourceInj $ Span beg end::src)) <$> g instance ( Gram_Reader (Source_Input src) g- , Inj_Source (Span (Source_Input src)) src+ , SourceInj (Span (Source_Input src)) src ) => Gram_Source src g---- ** Type 'At'--- | Attach a 'Source' to something.-data At src a- = At- { at :: !src- , unAt :: !a- } deriving (Eq, Functor, Ord, Show, Typeable)
+ Language/Symantic/Grammar/Source.hs view
@@ -0,0 +1,49 @@+{-# LANGUAGE DeriveFunctor #-}+{-# LANGUAGE TypeFamilies #-}+module Language.Symantic.Grammar.Source where++import Data.Functor (Functor)+import Data.Typeable (Typeable)++-- * Class 'Source'+class Source src where+ noSource :: src+instance Source () where+ noSource = ()++-- ** Class 'SourceInj'+class Source src => SourceInj a src where+ sourceInj :: a -> src+instance SourceInj a () where+ sourceInj _ = ()++-- ** Type family 'SourceOf'+type family SourceOf a++-- ** Type 'Sourced'+class Source (SourceOf a) => Sourced a where+ sourceOf :: a -> SourceOf a+ setSource :: a -> SourceOf a -> a+infixl 5 `setSource`++withSource :: SourceInj src (SourceOf a) => Sourced a => a -> src -> a+withSource a src = a `setSource` sourceInj src++-- ** Type 'Source_Input'+type family Source_Input (src :: *) :: *+type instance Source_Input () = ()++-- ** Type 'Span'+data Span src+ = Span+ { spanBegin :: !src+ , spanEnd :: !src+ } deriving (Eq, Ord, Show, Typeable)++-- ** Type 'At'+-- | Attach a 'Source' to something.+data At src a+ = At+ { at :: !src+ , unAt :: !a+ } deriving (Eq, Functor, Ord, Show, Typeable)
symantic-grammar.cabal view
@@ -22,7 +22,7 @@ -- PVP: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 0.0.0.20170623+version: 0.1.0.20170703 Source-Repository head location: git://git.autogeree.net/symantic@@ -55,6 +55,8 @@ Language.Symantic.Grammar.Operators Language.Symantic.Grammar.Regular Language.Symantic.Grammar.Terminal+ Language.Symantic.Grammar.Source+ Language.Symantic.Grammar.Error build-depends: base >= 4.6 && < 5 , text