on master help sign in
Generalize to arrows.
commit98259cc26f4df59a729127180ed862a2aa7a82ea authorPaweł Nowak <pawel834@gmail.com> dateThu 11 Dec 2014, 01:47 (12 years ago) parent519227b
5 files changed+92−100
Data/Syntax.hs+20−20 Data/Syntax/Char.hs+16−17 Data/Syntax/Combinator.hs+17−17 Data/Syntax/Indent.hs+37−44 syntax.cabal+2−2
Data/Syntax.hs+20−20
@@ -1,7 +1,6 @@
1+{-# LANGUAGE TypeFamilies #-}
12{-# LANGUAGE FlexibleContexts #-}
23{-# LANGUAGE DefaultSignatures #-}
3−{-# LANGUAGE MultiParamTypeClasses #-}
4−{-# LANGUAGE FunctionalDependencies #-}
54{- |
65Module : Data.Syntax
76Description : Abstract syntax description.
@@ -24,8 +23,8 @@ import Prelude hiding (take, takeWhile)
2423
2524import Control.Lens.Iso
2625import Control.Lens.SemiIso
26+import Control.SIArrow
2727import Data.MonoTraversable
28−import Data.SemiIsoFunctor
2928import Data.Sequences hiding (take, takeWhile)
3029
3130-- | An isomorphism between a sequence and a list of its elements.
@@ -41,56 +40,57 @@ packed = iso otoList fromList
4140-- package.
4241--
4342-- Methods of this class try to mimic "Data.Attoparsec.Text" interface.
44−class ( SemiIsoAlternative syn
45− , SemiIsoMonad syn
46− , IsSequence seq
47− , Eq seq
48− , Eq (Element seq))
49− => Syntax syn seq | syn -> seq
43+class ( SIArrow syn
44+ , IsSequence (Seq syn)
45+ , Eq (Seq syn)
46+ , Eq (Element (Seq syn)))
47+ => Syntax syn
5048 where
49+ -- | The sequence type used by this syntax.
50+ type Seq syn :: *
5151
5252 -- | Any character.
53− anyChar :: syn (Element seq)
53+ anyChar :: syn () (Element (Seq syn))
5454
5555 -- | A specific character.
56− char :: Element seq -> syn ()
56+ char :: Element (Seq syn) -> syn () ()
5757 char c = rev (exact c) /$/ anyChar
5858
5959 -- | Any character except the given one.
60− notChar :: Element seq -> syn (Element seq)
60+ notChar :: Element (Seq syn) -> syn () (Element (Seq syn))
6161 notChar c = bifiltered (/= c) /$/ anyChar
6262
6363 -- | Any character satisfying a predicate.
64− satisfy :: (Element seq -> Bool) -> syn (Element seq)
64+ satisfy :: (Element (Seq syn) -> Bool) -> syn () (Element (Seq syn))
6565 satisfy p = bifiltered p /$/ anyChar
6666
6767 -- | Transforms a character using a SemiIso and filters out values
6868 -- not satisfying the predicate.
69− satisfyWith :: ASemiIso' a (Element seq) -> (a -> Bool) -> syn a
69+ satisfyWith :: ASemiIso' a (Element (Seq syn)) -> (a -> Bool) -> syn () a
7070 satisfyWith ai p = bifiltered p . ai /$/ anyChar
7171
7272 -- | A specific string.
73− string :: seq -> syn ()
73+ string :: (Seq syn) -> syn () ()
7474 string s = rev (exact s) /$/ take (olength s)
7575
7676 -- | A string of length @n@.
77− take :: Int -> syn seq
77+ take :: Int -> syn () (Seq syn)
7878 take n = packed /$/ sireplicate n anyChar
7979
8080 -- | Maximal string which elements satisfy a predicate.
81− takeWhile :: (Element seq -> Bool) -> syn seq
81+ takeWhile :: (Element (Seq syn) -> Bool) -> syn () (Seq syn)
8282 takeWhile p = packed /$/ simany (satisfy p)
8383
8484 -- | Maximal non-empty string which elements satisfy a predicate.
85− takeWhile1 :: (Element seq -> Bool) -> syn seq
85+ takeWhile1 :: (Element (Seq syn) -> Bool) -> syn () (Seq syn)
8686 takeWhile1 p = packed /$/ sisome (satisfy p)
8787
8888 -- | Maximal string which elements do not satisfy a predicate.
89− takeTill :: (Element seq -> Bool) -> syn seq
89+ takeTill :: (Element (Seq syn) -> Bool) -> syn () (Seq syn)
9090 takeTill p = takeWhile (not . p)
9191
9292 -- | Maximal non-empty string which elements do not satisfy a predicate.
93− takeTill1 :: (Element seq -> Bool) -> syn seq
93+ takeTill1 :: (Element (Seq syn) -> Bool) -> syn () (Seq syn)
9494 takeTill1 p = takeWhile1 (not . p)
9595
9696 {-# MINIMAL anyChar #-}
Data/Syntax/Char.hs+16−17
@@ -1,6 +1,5 @@
11{-# LANGUAGE TypeFamilies #-}
22{-# LANGUAGE ConstraintKinds #-}
3−{-# LANGUAGE MultiParamTypeClasses #-}
43{- |
54Module : Data.Syntax.Char
65Description : Char specific combinators.
@@ -27,11 +26,11 @@ module Data.Syntax.Char (
2726 ) where
2827
2928import Control.Lens.SemiIso
29+import Control.SIArrow
3030import Data.Bits
3131import Data.Char
3232import Data.MonoTraversable
3333import Data.Scientific (Scientific)
34−import Data.SemiIsoFunctor
3534import Data.Syntax
3635import Data.Syntax.Combinator
3736import Data.Text (Text)
@@ -40,47 +39,47 @@ import Data.Text (Text)
4039--
4140-- Note: methods of this class do not have default implementations (for now),
4241-- because their code is quite ugly and already written in most parser libraries.
43−class (Syntax syn seq, Element seq ~ Char) => SyntaxChar syn seq where
42+class (Syntax syn, Element (Seq syn) ~ Char) => SyntaxChar syn where
4443 -- | An unsigned decimal number.
45− decimal :: Integral a => syn a
44+ decimal :: Integral a => syn () a
4645
4746 -- | An unsigned hexadecimal number.
48− hexadecimal :: (Integral a, Bits a) => syn a
47+ hexadecimal :: (Integral a, Bits a) => syn () a
4948
5049 -- | A signed real number.
51− realFloat :: RealFloat a => syn a
50+ realFloat :: RealFloat a => syn () a
5251
5352 -- | A scientific number.
54− scientific :: syn Scientific
53+ scientific :: syn () Scientific
5554
5655 {-# MINIMAL decimal, hexadecimal, realFloat, scientific #-}
5756
5857-- | An useful synonym for SyntaxChars with Text sequences.
59−type SyntaxText syn = SyntaxChar syn Text
58+type SyntaxText syn = (SyntaxChar syn, Seq syn ~ Text)
6059
6160-- | A number with an optional leading '+' or '-' sign character.
62−signed :: (Real a, SyntaxChar syn seq) => syn a -> syn a
61+signed :: (Real a, SyntaxChar syn) => syn () a -> syn () a
6362signed n = _Negative /$/ char '-' */ n
64− /|/ opt_ (char '+') */ n
63+ /+/ opt_ (char '+') */ n
6564
6665-- | Accepts zero or more spaces. Generates a single space.
67−spaces :: SyntaxChar syn seq => syn ()
66+spaces :: SyntaxChar syn => syn () ()
6867spaces = opt spaces1
6968
7069-- | Accepts zero or more spaces. Generates no output.
71−spaces_ :: SyntaxChar syn seq => syn ()
70+spaces_ :: SyntaxChar syn => syn () ()
7271spaces_ = opt_ spaces1
7372
7473-- | Accepts one or more spaces. Generates a single space.
75−spaces1 :: SyntaxChar syn seq => syn ()
74+spaces1 :: SyntaxChar syn => syn () ()
7675spaces1 = constant (opoint ' ') /$/ takeWhile1 isSpace
7776
7877-- | Accepts a single newline. Generates a newline.
79−endOfLine :: SyntaxChar syn seq => syn ()
78+endOfLine :: SyntaxChar syn => syn () ()
8079endOfLine = char '\n'
8180
8281-- | A decimal digit.
83−digitDec :: SyntaxChar syn seq => syn Int
82+digitDec :: SyntaxChar syn => syn () Int
8483digitDec = semiIso toChar toInt /$/ anyChar
8584 where toInt c | isDigit c = Right (digitToInt c)
8685 | otherwise = Left ("Expected a decimal digit, got " ++ [c])
@@ -88,7 +87,7 @@ digitDec = semiIso toChar toInt /$/ anyChar
8887 | otherwise = Left ("Expected a decimal digit, got number " ++ show i)
8988
9089-- | An octal digit.
91−digitOct :: SyntaxChar syn seq => syn Int
90+digitOct :: SyntaxChar syn => syn () Int
9291digitOct = semiIso toChar toInt /$/ anyChar
9392 where toInt c | isOctDigit c = Right (digitToInt c)
9493 | otherwise = Left ("Expected an octal digit, got " ++ [c])
@@ -96,7 +95,7 @@ digitOct = semiIso toChar toInt /$/ anyChar
9695 | otherwise = Left ("Expected an octal digit, got number " ++ show i)
9796
9897-- | A hex digit.
99−digitHex :: SyntaxChar syn seq => syn Int
98+digitHex :: SyntaxChar syn => syn () Int
10099digitHex = semiIso toChar toInt /$/ anyChar
101100 where toInt c | isHexDigit c = Right (digitToInt c)
102101 | otherwise = Left ("Expected a hex digit, got " ++ [c])
Data/Syntax/Combinator.hs+17−17
@@ -13,40 +13,40 @@ module Data.Syntax.Combinator where
1313
1414import Control.Lens
1515import Control.Lens.SemiIso
16−import Data.SemiIsoFunctor
16+import Control.SIArrow
1717
1818-- | One or zero occurences of @f@.
19−optional :: SemiIsoAlternative f => f a -> f (Maybe a)
20−optional f = _Just /$/ f /|/ sipure _Nothing
19+optional :: SIArrow cat => cat () a -> cat () (Maybe a)
20+optional f = _Just /$/ f /+/ sipure _Nothing
2121
2222-- | Like 'optional', but specialized for @()@.
23−opt :: SemiIsoAlternative f => f () -> f ()
24−opt f = f /|/ sipure id
23+opt :: SIArrow cat => cat () () -> cat () ()
24+opt f = f /+/ sipure id
2525
2626-- | Parser one or zero occurences of @f@, but prints nothing.
27−opt_ :: SemiIsoAlternative f => f () -> f ()
27+opt_ :: SIArrow cat => cat () () -> cat () ()
2828opt_ f = semiIso (const (Left "opt_")) Right /$/ f
29− /|/ sipure id
29+ /+/ sipure id
3030
3131-- | @manyTill p end@ applies action p zero or more times until action
3232-- end succeeds, and returns the list of values returned by p.
33−manyTill :: SemiIsoAlternative f => f a -> f () -> f [a]
33+manyTill :: SIArrow cat => cat () a -> cat () () -> cat () [a]
3434manyTill p end = _Empty /$/ end
35− /|/ _Cons /$/ p /*/ manyTill p end
35+ /+/ _Cons /$/ p /*/ manyTill p end
3636
3737-- | Zero or more occurences of @v@ separated by @s@.
38−sepBy :: SemiIsoAlternative f => f a -> f () -> f [a]
39−sepBy v s = sepBy1 v s /|/ sipure _Empty
38+sepBy :: SIArrow cat => cat () a -> cat () () -> cat () [a]
39+sepBy v s = sepBy1 v s /+/ sipure _Empty
4040
4141-- | One or more occurences of @v@ separated by @s@.
42−sepBy1 :: SemiIsoAlternative f => f a -> f () -> f [a]
43−sepBy1 v s = _Cons /$/ v /*/ (s */ sepBy1 v s /|/ sipure _Empty)
42+sepBy1 :: SIArrow cat => cat () a -> cat () () -> cat () [a]
43+sepBy1 v s = _Cons /$/ v /*/ (s */ sepBy1 v s /+/ sipure _Empty)
4444
4545-- | Tries to apply the actions in the list in order, until one of
4646-- them succeeds. Returns the value of the succeeding action.
47−choice :: SemiIsoAlternative f => [f a] -> f a
48−choice = foldr (/|/) (sifail "choice: all alternatives failed")
47+choice :: SIArrow cat => [cat () a] -> cat () a
48+choice = foldr (/+/) (sifail "choice: all alternatives failed")
4949
5050-- | Combine two alternatives.
51−eitherOf :: SemiIsoAlternative f => f a -> f b -> f (Either a b)
52−eitherOf a b = _Left /$/ a /|/ _Right /$/ b
51+eitherOf :: SIArrow cat => cat () a -> cat () b -> cat () (Either a b)
52+eitherOf a b = _Left /$/ a /+/ _Right /$/ b
Data/Syntax/Indent.hs+37−44
@@ -1,16 +1,16 @@
1+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
12{-# LANGUAGE FlexibleInstances #-}
2−{-# LANGUAGE UndecidableInstances #-}
3−{-# LANGUAGE MultiParamTypeClasses #-}
3+{-# LANGUAGE TypeFamilies #-}
44{- |
55Module : Data.Syntax.Indent
6−Description : Indentation.
6+Description : Simple indentation.
77Copyright : (c) Paweł Nowak
88License : MIT
99
1010Maintainer : Paweł Nowak <pawel834@gmail.com>
1111Stability : experimental
1212
13−Provides a very simple indentation as a \"monad\" transformer.
13+Provides a very simple indentation as a category transformer (a functor from Cat to Cat).
1414-}
1515module Data.Syntax.Indent (
1616 Indent,
@@ -19,59 +19,52 @@ module Data.Syntax.Indent (
1919 indented
2020 ) where
2121
22−import Data.SemiIsoFunctor
22+import Control.Category
23+import Control.Category.Reader
24+import Control.SIArrow
2325import Data.Syntax
2426import Data.Syntax.Char
2527import Data.Syntax.Combinator
26−import Prelude hiding (takeWhile, take)
28+import Prelude hiding (takeWhile, take, id, (.))
2729
2830-- | Adds indentation to a syntax description.
29−newtype Indent m a = Indent { unIndent :: (Int, m ()) -> m a }
31+newtype Indent cat a b = Indent { unIndent :: ReaderCT (Int, cat () ()) cat a b }
32+ deriving (Category, Products, Coproducts, CategoryPlus, SIArrow)
3033
31−instance SemiIsoFunctor m => SemiIsoFunctor (Indent m) where
32− simap f (Indent g) = Indent $ \i -> simap f (g i)
34+instance CategoryTrans Indent where
35+ clift = Indent . clift
3336
34−instance SemiIsoApply m => SemiIsoApply (Indent m) where
35− sipure ai = Indent $ \_ -> sipure ai
36− Indent f /*/ Indent g = Indent $ \i -> f i /*/ g i
37+instance Syntax syn => Syntax (Indent syn) where
38+ type Seq (Indent syn) = Seq syn
39+ anyChar = clift anyChar
40+ char = clift . char
41+ notChar = clift . notChar
42+ satisfy = clift . satisfy
43+ satisfyWith ai = clift . satisfyWith ai
44+ string = clift . string
45+ take = clift . take
46+ takeWhile = clift . takeWhile
47+ takeWhile1 = clift . takeWhile1
48+ takeTill = clift . takeTill
49+ takeTill1 = clift . takeTill1
3750
38−instance SemiIsoAlternative m => SemiIsoAlternative (Indent m) where
39− siempty = Indent $ \_ -> siempty
40− Indent f /|/ Indent g = Indent $ \i -> f i /|/ g i
41−
42−instance SemiIsoMonad m => SemiIsoMonad (Indent m) where
43− (Indent m) //= f = Indent $ \i -> m i //= (\x -> unIndent (f x) i)
44−
45−instance SemiIsoFix m => SemiIsoFix (Indent m) where
46− sifix f = Indent $ \i -> sifix $ \y -> unIndent (f y) i
47−
48−instance Syntax syn seq => Syntax (Indent syn) seq where
49− anyChar = Indent $ const anyChar
50− char = Indent . const . char
51− notChar = Indent . const . notChar
52− satisfy = Indent . const . satisfy
53− satisfyWith ai = Indent . const . satisfyWith ai
54− string = Indent . const . string
55− take = Indent . const . take
56− takeWhile = Indent . const . takeWhile
57− takeWhile1 = Indent . const . takeWhile1
58− takeTill = Indent . const . takeTill
59− takeTill1 = Indent . const . takeTill1
60−
61−instance SyntaxChar syn seq => SyntaxChar (Indent syn) seq where
62− decimal = Indent $ const decimal
63− scientific = Indent $ const scientific
51+instance SyntaxChar syn => SyntaxChar (Indent syn) where
52+ decimal = clift decimal
53+ hexadecimal = clift hexadecimal
54+ scientific = clift scientific
55+ realFloat = clift realFloat
6456
6557-- | @runIndent m tab@ runs the 'Indent' transformer using @tab@ once for each
6658-- level of indentation.
67−runIndent :: Indent m a -> m () -> m a
68−runIndent = ($ 0) . curry . unIndent
59+runIndent :: Indent cat a b -> cat () () -> cat a b
60+runIndent (Indent m) tab = runReaderCT m (0, tab)
6961
7062-- | Inserts a new line and correct indentation, but does not
7163-- require any formatting when parsing (it just skips all white space).
72−breakLine :: SyntaxChar syn seq => Indent syn ()
73−breakLine = Indent $ \(i, tab) -> opt (char '\n') /* opt (sireplicate_ i tab) /* spaces_
64+breakLine :: SyntaxChar syn => Indent syn () ()
65+breakLine = Indent . ReaderCT $ \(i, tab) ->
66+ opt (char '\n') /* opt (sireplicate_ i tab) /* spaces_
7467
7568-- | Increases the indentation level of its argument by one.
76−indented :: Indent m a -> Indent m a
77−indented (Indent f) = Indent $ \(i, tab) -> f (i + 1, tab)
69+indented :: Indent cat a b -> Indent cat a b
70+indented (Indent f) = Indent . ReaderCT $ \(i, tab) -> runReaderCT f (i + 1, tab)
syntax.cabal+2−2
@@ -1,5 +1,5 @@
11name: syntax
2−version: 0.3.0.0
2+version: 1.0.0.0
33synopsis: Syntax descriptions for unified parsing and pretty-printing.
44description:
55 'syntax' allows you to write a single syntax description and instantiate is both as a parser and a pretty printer.
@@ -46,5 +46,5 @@ library
4646 Data.Syntax.Char
4747 Data.Syntax.Combinator
4848 Data.Syntax.Indent
49− build-depends: base >= 4 && < 5, mono-traversable, lens >= 4, semi-iso >= 0.5.0, scientific >= 0.3, text
49+ build-depends: base >= 4 && < 5, mono-traversable, lens >= 4, semi-iso >= 1.0.0.0, scientific >= 0.3, text
5050 default-language: Haskell2010