on 519227b help sign in
5 files changed+134−369
since splitsnapshots
unifiedsplit
Data/Syntax.hs+30−122 Data/Syntax/Char.hs+17−24 Data/Syntax/Combinator.hs+23−161 Data/Syntax/Indent.hs+43−39 syntax.cabal+21−23
Data/Syntax.hs+30−122
@@ -1,6 +1,7 @@
1−{-# LANGUAGE TypeFamilies #-}
21{-# LANGUAGE FlexibleContexts #-}
32{-# LANGUAGE DefaultSignatures #-}
3+{-# LANGUAGE MultiParamTypeClasses #-}
4+{-# LANGUAGE FunctionalDependencies #-}
45{- |
56Module : Data.Syntax
67Description : Abstract syntax description.
@@ -10,28 +11,22 @@ License : MIT
1011Maintainer : Paweł Nowak <pawel834@gmail.com>
1112Stability : experimental
1213
13−Reversible parsing and pretty-printing.
14+Abstract syntax descriptions based on semi-isomorphisms.
1415-}
1516module Data.Syntax (
1617 -- * Syntax.
1718 Syntax(..),
18− Isolable(..),
1919 -- * Common isomorphisms.
2020 packed
2121 ) where
2222
23−import Prelude hiding (take, takeWhile, id, (.))
23+import Prelude hiding (take, takeWhile)
2424
25−import Control.Category
26−import Control.Category.Reader
27−import Control.Category.Structures
28−import Control.Lens.Iso
29−import Control.Lens.SemiIso
30−import Control.SIArrow
31−import Data.MonoTraversable
32−import Data.Sequences hiding (take, takeWhile, replicate)
33−import qualified Data.Vector as V
34−import qualified Data.Vector.Unboxed as VU
25+import Control.Lens.Iso
26+import Control.Lens.SemiIso
27+import Data.MonoTraversable
28+import Data.SemiIsoFunctor
29+import Data.Sequences hiding (take, takeWhile)
3530
3631-- | An isomorphism between a sequence and a list of its elements.
3732packed :: IsSequence seq => Iso' seq [Element seq]
@@ -39,150 +34,63 @@ packed = iso otoList fromList
3934
4035-- | An abstract syntax description based on semi-isomorphisms.
4136--
42−-- This class can be implemented by both parsers and printers.
43−--
37+-- This class can be implemented by both parsers and printers (and maybe more?).
38+--
4439-- The usual use is to write a polymorphic syntax description and instantiate it
45−-- both as a parser and a printer. Examples are available in 'syntax-example' and
46−-- 'syntax-example-json' packages.
40+-- both as a parser and a printer. An example is available in the 'syntax-example'
41+-- package.
4742--
4843-- Methods of this class try to mimic "Data.Attoparsec.Text" interface.
49−class ( SIArrow syn
50− , IsSequence (Seq syn)
51− , Eq (Seq syn)
52− , Eq (Element (Seq syn)))
53− => Syntax syn
44+class ( SemiIsoAlternative syn
45+ , SemiIsoMonad syn
46+ , IsSequence seq
47+ , Eq seq
48+ , Eq (Element seq))
49+ => Syntax syn seq | syn -> seq
5450 where
55− -- | The sequence type used by this syntax.
56− type Seq syn :: *
5751
5852 -- | Any character.
59− anyChar :: syn () (Element (Seq syn))
53+ anyChar :: syn (Element seq)
6054
6155 -- | A specific character.
62− char :: Element (Seq syn) -> syn () ()
56+ char :: Element seq -> syn ()
6357 char c = rev (exact c) /$/ anyChar
6458
6559 -- | Any character except the given one.
66− notChar :: Element (Seq syn) -> syn () (Element (Seq syn))
60+ notChar :: Element seq -> syn (Element seq)
6761 notChar c = bifiltered (/= c) /$/ anyChar
6862
6963 -- | Any character satisfying a predicate.
70− satisfy :: (Element (Seq syn) -> Bool) -> syn () (Element (Seq syn))
64+ satisfy :: (Element seq -> Bool) -> syn (Element seq)
7165 satisfy p = bifiltered p /$/ anyChar
7266
7367 -- | Transforms a character using a SemiIso and filters out values
7468 -- not satisfying the predicate.
75− satisfyWith :: ASemiIso' a (Element (Seq syn)) -> (a -> Bool) -> syn () a
69+ satisfyWith :: ASemiIso' a (Element seq) -> (a -> Bool) -> syn a
7670 satisfyWith ai p = bifiltered p . ai /$/ anyChar
7771
7872 -- | A specific string.
79− string :: Seq syn -> syn () ()
73+ string :: seq -> syn ()
8074 string s = rev (exact s) /$/ take (olength s)
8175
8276 -- | A string of length @n@.
83− take :: Int -> syn () (Seq syn)
77+ take :: Int -> syn seq
8478 take n = packed /$/ sireplicate n anyChar
8579
8680 -- | Maximal string which elements satisfy a predicate.
87− takeWhile :: (Element (Seq syn) -> Bool) -> syn () (Seq syn)
81+ takeWhile :: (Element seq -> Bool) -> syn seq
8882 takeWhile p = packed /$/ simany (satisfy p)
8983
9084 -- | Maximal non-empty string which elements satisfy a predicate.
91− takeWhile1 :: (Element (Seq syn) -> Bool) -> syn () (Seq syn)
85+ takeWhile1 :: (Element seq -> Bool) -> syn seq
9286 takeWhile1 p = packed /$/ sisome (satisfy p)
9387
9488 -- | Maximal string which elements do not satisfy a predicate.
95− takeTill :: (Element (Seq syn) -> Bool) -> syn () (Seq syn)
89+ takeTill :: (Element seq -> Bool) -> syn seq
9690 takeTill p = takeWhile (not . p)
9791
9892 -- | Maximal non-empty string which elements do not satisfy a predicate.
99− takeTill1 :: (Element (Seq syn) -> Bool) -> syn () (Seq syn)
93+ takeTill1 :: (Element seq -> Bool) -> syn seq
10094 takeTill1 p = takeWhile1 (not . p)
10195
102− -- | Constant size vector. The default implementation uses lists, but
103− -- "syntax-attoparsec" and "syntax-printer" override it with an efficient
104− -- implementation that works on a vector directly.
105− --
106− -- @vecN n e@ describes a size @n@ vector with elements @e@.
107− --
108− -- Also see 'Data.Syntax.Combinator.vec'.
109− vecN :: Int -> syn () a -> syn () (V.Vector a)
110− vecN n e = packed /$/ sireplicate n e
111−
112− -- | Constant size vector with index-aware element. The default implementation
113− -- uses lists, but "syntax-attoparsec" and "syntax-printer" override it with an
114− -- efficient implementation that works on a vector directly.
115− --
116− -- @ivecN n e@ describes a size @n@ vector with elements @e@. Each element
117− -- gets its index and should output a value and the index unchanged.
118− --
119− -- Also see 'Data.Syntax.Combinator.ivec'.
120− ivecN :: Int -> syn Int (Int, a) -> syn () (V.Vector a)
121− ivecN n e = (packed /$/)
122− $ sisequence
123− $ map (\(i, e') -> constant i ^>> e'
124− >>> first (sipure (constant i))
125− >># unit . swapped)
126− $ zip [0 .. n-1] (replicate n e)
127−
128− -- | Constant size unboxed vector. The default implementation uses lists, but
129− -- "syntax-attoparsec" and "syntax-printer" override it with an efficient
130− -- implementation that works on a vector directly.
131− --
132− -- @vecN n e@ describes a size @n@ vector with elements @e@.
133− --
134− -- Also see 'Data.Syntax.Combinator.vec'.
135− uvecN :: VU.Unbox a => Int -> syn () a -> syn () (VU.Vector a)
136− uvecN n e = packed /$/ sireplicate n e
137−
138− -- | Constant size unboxed vector with index-aware element. The default implementation
139− -- uses lists, but "syntax-attoparsec" and "syntax-printer" override it with an
140− -- efficient implementation that works on a vector directly.
141− --
142− -- @ivecN n e@ describes a size @n@ vector with elements @e@. Each element
143− -- gets its index and should output a value and the index unchanged.
144− --
145− -- Also see 'Data.Syntax.Combinator.ivec'.
146− uivecN :: VU.Unbox a => Int -> syn Int (Int, a) -> syn () (VU.Vector a)
147− uivecN n e = (packed /$/)
148− $ sisequence
149− $ map (\(i, e') -> constant i ^>> e'
150− >>> first (sipure (constant i))
151− >># unit . swapped)
152− $ zip [0 .. n-1] (replicate n e)
153−
15496 {-# MINIMAL anyChar #-}
155−
156−instance Syntax syn => Syntax (ReaderCT env syn) where
157− type Seq (ReaderCT env syn) = Seq syn
158− anyChar = clift anyChar
159− char = clift . char
160− notChar = clift . notChar
161− satisfy = clift . satisfy
162− satisfyWith ai = clift . satisfyWith ai
163− string = clift . string
164− take = clift . take
165− takeWhile = clift . takeWhile
166− takeWhile1 = clift . takeWhile1
167− takeTill = clift . takeTill
168− takeTill1 = clift . takeTill1
169− vecN n e = ReaderCT $ vecN n . runReaderCT e
170− ivecN n e = ReaderCT $ ivecN n . runReaderCT e
171− uvecN n e = ReaderCT $ uvecN n . runReaderCT e
172− uivecN n e = ReaderCT $ uivecN n . runReaderCT e
173−
174−-- | Execute a computation in an isolated context.
175−--
176−-- The motivating example: you want to write a function
177−--
178−-- > serializeList :: Syntax syn => syn () a -> syn () [a]
179−--
180−-- Notice that we cannot just use simany, because the first @syn () a@
181−-- could eat the entire sequence (even though we printed more than 1 value!), so
182−-- we have to insert some kind of separators between the element. But how can we
183−-- be sure that @syn () a@ will not eat our separators? We can't! Thats why we
184−-- have to do the parsing in two stages: first extract the sequence between separators,
185−-- then run the @syn () a@ on this sequence.
186−class Syntax syn => Isolable syn where
187− -- | Turns a computation into one executed in an isolated context.
188− isolate :: syn () b -> syn (Seq syn) b
Data/Syntax/Char.hs+17−24
@@ -1,5 +1,6 @@
11{-# LANGUAGE TypeFamilies #-}
22{-# LANGUAGE ConstraintKinds #-}
3+{-# LANGUAGE MultiParamTypeClasses #-}
34{- |
45Module : Data.Syntax.Char
56Description : Char specific combinators.
@@ -25,14 +26,12 @@ module Data.Syntax.Char (
2526 digitHex
2627 ) where
2728
28−import Control.Category.Reader
29−import Control.Category.Structures
3029import Control.Lens.SemiIso
31−import Control.SIArrow
3230import Data.Bits
3331import Data.Char
3432import Data.MonoTraversable
3533import Data.Scientific (Scientific)
34+import Data.SemiIsoFunctor
3635import Data.Syntax
3736import Data.Syntax.Combinator
3837import Data.Text (Text)
@@ -41,53 +40,47 @@ import Data.Text (Text)
4140--
4241-- Note: methods of this class do not have default implementations (for now),
4342-- because their code is quite ugly and already written in most parser libraries.
44−class (Syntax syn, Element (Seq syn) ~ Char) => SyntaxChar syn where
43+class (Syntax syn seq, Element seq ~ Char) => SyntaxChar syn seq where
4544 -- | An unsigned decimal number.
46− decimal :: Integral a => syn () a
45+ decimal :: Integral a => syn a
4746
4847 -- | An unsigned hexadecimal number.
49− hexadecimal :: (Integral a, Bits a) => syn () a
48+ hexadecimal :: (Integral a, Bits a) => syn a
5049
5150 -- | A signed real number.
52− realFloat :: RealFloat a => syn () a
51+ realFloat :: RealFloat a => syn a
5352
5453 -- | A scientific number.
55− scientific :: syn () Scientific
54+ scientific :: syn Scientific
5655
5756 {-# MINIMAL decimal, hexadecimal, realFloat, scientific #-}
5857
59−instance SyntaxChar syn => SyntaxChar (ReaderCT env syn) where
60− decimal = clift decimal
61− hexadecimal = clift hexadecimal
62− scientific = clift scientific
63− realFloat = clift realFloat
64−
6558-- | An useful synonym for SyntaxChars with Text sequences.
66−type SyntaxText syn = (SyntaxChar syn, Seq syn ~ Text)
59+type SyntaxText syn = SyntaxChar syn Text
6760
6861-- | A number with an optional leading '+' or '-' sign character.
69−signed :: (Real a, SyntaxChar syn) => syn () a -> syn () a
62+signed :: (Real a, SyntaxChar syn seq) => syn a -> syn a
7063signed n = _Negative /$/ char '-' */ n
71− /+/ opt_ (char '+') */ n
64+ /|/ opt_ (char '+') */ n
7265
7366-- | Accepts zero or more spaces. Generates a single space.
74−spaces :: SyntaxChar syn => syn () ()
67+spaces :: SyntaxChar syn seq => syn ()
7568spaces = opt spaces1
7669
7770-- | Accepts zero or more spaces. Generates no output.
78−spaces_ :: SyntaxChar syn => syn () ()
71+spaces_ :: SyntaxChar syn seq => syn ()
7972spaces_ = opt_ spaces1
8073
8174-- | Accepts one or more spaces. Generates a single space.
82−spaces1 :: SyntaxChar syn => syn () ()
75+spaces1 :: SyntaxChar syn seq => syn ()
8376spaces1 = constant (opoint ' ') /$/ takeWhile1 isSpace
8477
8578-- | Accepts a single newline. Generates a newline.
86−endOfLine :: SyntaxChar syn => syn () ()
79+endOfLine :: SyntaxChar syn seq => syn ()
8780endOfLine = char '\n'
8881
8982-- | A decimal digit.
90−digitDec :: SyntaxChar syn => syn () Int
83+digitDec :: SyntaxChar syn seq => syn Int
9184digitDec = semiIso toChar toInt /$/ anyChar
9285 where toInt c | isDigit c = Right (digitToInt c)
9386 | otherwise = Left ("Expected a decimal digit, got " ++ [c])
@@ -95,7 +88,7 @@ digitDec = semiIso toChar toInt /$/ anyChar
9588 | otherwise = Left ("Expected a decimal digit, got number " ++ show i)
9689
9790-- | An octal digit.
98−digitOct :: SyntaxChar syn => syn () Int
91+digitOct :: SyntaxChar syn seq => syn Int
9992digitOct = semiIso toChar toInt /$/ anyChar
10093 where toInt c | isOctDigit c = Right (digitToInt c)
10194 | otherwise = Left ("Expected an octal digit, got " ++ [c])
@@ -103,7 +96,7 @@ digitOct = semiIso toChar toInt /$/ anyChar
10396 | otherwise = Left ("Expected an octal digit, got number " ++ show i)
10497
10598-- | A hex digit.
106−digitHex :: SyntaxChar syn => syn () Int
99+digitHex :: SyntaxChar syn seq => syn Int
107100digitHex = semiIso toChar toInt /$/ anyChar
108101 where toInt c | isHexDigit c = Right (digitToInt c)
109102 | otherwise = Left ("Expected a hex digit, got " ++ [c])
Data/Syntax/Combinator.hs+23−161
@@ -1,6 +1,3 @@
1−{-# LANGUAGE RankNTypes #-}
2−{-# LANGUAGE ScopedTypeVariables #-}
3−{-# LANGUAGE FlexibleContexts #-}
41{- |
52Module : Data.Syntax.Combinator
63Description : Combinators that work with any sequence type.
@@ -12,179 +9,44 @@ Stability : experimental
129
1310Combinators that work with any sequence type.
1411-}
15−module Data.Syntax.Combinator
16− (
17− -- * Combinators.
18− optional,
19− opt,
20− opt_,
21− choice,
22− eitherOf,
12+module Data.Syntax.Combinator where
2313
24− -- * Lists.
25− manyTill,
26− sepBy,
27− sepBy1,
28−
29− -- * Arrowized combinators.
30− takeArr,
31−
32− -- * Vectors.
33− vecNSepBy,
34− ivecNSepBy,
35− vec,
36− vecSepBy,
37− ivec,
38− ivecSepBy,
39−
40− -- * Unboxed vectors.
41− uvecNSepBy,
42− uivecNSepBy,
43− uvec,
44− uvecSepBy,
45− uivec,
46− uivecSepBy
47− ) where
48−
49−import Control.Category
50−import Control.Category.Structures
51−import Control.Lens
52−import Control.Lens.SemiIso
53−import Control.SIArrow
54−import qualified Data.MonoTraversable as Seq
55−import Data.Syntax
56−import qualified Data.Vector as V
57−import qualified Data.Vector.Unboxed as VU
58−import Prelude hiding (id, (.), take)
14+import Control.Lens
15+import Control.Lens.SemiIso
16+import Data.SemiIsoFunctor
5917
6018-- | One or zero occurences of @f@.
61−optional :: SIArrow cat => cat () a -> cat () (Maybe a)
62−optional f = _Just /$/ f /+/ sipure _Nothing
19+optional :: SemiIsoAlternative f => f a -> f (Maybe a)
20+optional f = _Just /$/ f /|/ sipure _Nothing
6321
6422-- | Like 'optional', but specialized for @()@.
65−opt :: SIArrow cat => cat () () -> cat () ()
66−opt f = f /+/ sipure id
23+opt :: SemiIsoAlternative f => f () -> f ()
24+opt f = f /|/ sipure id
6725
6826-- | Parser one or zero occurences of @f@, but prints nothing.
69−opt_ :: SIArrow cat => cat () () -> cat () ()
27+opt_ :: SemiIsoAlternative f => f () -> f ()
7028opt_ f = semiIso (const (Left "opt_")) Right /$/ f
71− /+/ sipure id
72−
73−-- | Tries to apply the actions in the list in order, until one of
74−-- them succeeds. Returns the value of the succeeding action.
75−choice :: SIArrow cat => [cat () a] -> cat () a
76−choice = foldr (/+/) (sifail "choice: all alternatives failed")
77−
78−-- | Combine two alternatives.
79−eitherOf :: SIArrow cat => cat () a -> cat () b -> cat () (Either a b)
80−eitherOf a b = _Left /$/ a /+/ _Right /$/ b
29+ /|/ sipure id
8130
8231-- | @manyTill p end@ applies action p zero or more times until action
8332-- end succeeds, and returns the list of values returned by p.
84−manyTill :: SIArrow cat => cat () a -> cat () () -> cat () [a]
33+manyTill :: SemiIsoAlternative f => f a -> f () -> f [a]
8534manyTill p end = _Empty /$/ end
86− /+/ _Cons /$/ p /*/ manyTill p end
35+ /|/ _Cons /$/ p /*/ manyTill p end
8736
8837-- | Zero or more occurences of @v@ separated by @s@.
89−sepBy :: SIArrow cat => cat () a -> cat () () -> cat () [a]
90−sepBy v s = sepBy1 v s /+/ sipure _Empty
38+sepBy :: SemiIsoAlternative f => f a -> f () -> f [a]
39+sepBy v s = sepBy1 v s /|/ sipure _Empty
9140
9241-- | One or more occurences of @v@ separated by @s@.
93−sepBy1 :: SIArrow cat => cat () a -> cat () () -> cat () [a]
94−sepBy1 v s = _Cons /$/ v /*/ (s */ sepBy1 v s /+/ sipure _Empty)
95−
96−-- | A string of given length.
97−takeArr :: Syntax syn => syn Int (Seq syn)
98−takeArr = sibind $ iso (\n -> constant n #>> take n)
99− (\s -> constant (Seq.olength s) #>> take (Seq.olength s))
100−
101−-- | Constant size vector with separators.
102−--
103−-- @vecNSepBy n e sep@ describes a size @n@ vector with elements @e@ separated by @sep@.
104−vecNSepBy :: Syntax syn => Int -> syn () a -> syn () () -> syn () (V.Vector a)
105−vecNSepBy n e sep = ivecNSepBy n (unit ^>> second e) sep
106−
107−-- | Constant size vector with separators and index-aware elements.
108−--
109−-- @ivecNSepBy n e sep@ describes a size @n@ vector with elements @e@ separated by @sep@.
110−-- Each element gets its index and should output a value and the index unchanged.
111−ivecNSepBy :: Syntax syn => Int -> syn Int (Int, a) -> syn () () -> syn () (V.Vector a)
112−ivecNSepBy n e sep = ivecN n $ sibind $ iso el (el . fst)
113− where
114− el k | k < n - 1 = unit ^>> e *** sep >># unit
115− | otherwise = e
42+sepBy1 :: SemiIsoAlternative f => f a -> f () -> f [a]
43+sepBy1 v s = _Cons /$/ v /*/ (s */ sepBy1 v s /|/ sipure _Empty)
11644
117−-- | Runtime sized vector. The size can depend on the result of some computation.
118−--
119−-- @vec e@ describes a vector with elements @e@.
120−vec :: Syntax syn => syn () a -> syn Int (V.Vector a)
121−vec e = vecSepBy e (siarr id)
122−
123−-- | Runtime sized vector with separators. The size can depend on the result of some
124−-- computation.
125−--
126−-- @vecSepBy e sep@ describes a vector with elements @e@ separated by @sep@.
127−vecSepBy :: Syntax syn => syn () a -> syn () () -> syn Int (V.Vector a)
128−vecSepBy e sep = ivecSepBy (unit ^>> second e) sep
129−
130−-- | Runtime sized vector with index-aware elements. The size can depend on the result
131−-- of some computation.
132−--
133−-- @ivec e@ describes a vector with elements @e@.
134−ivec :: Syntax syn => syn Int (Int, a) -> syn Int (V.Vector a)
135−ivec e = ivecSepBy e (siarr id)
136−
137−-- | Runtime sized vector with index-aware elements and separators. The size can depend
138−-- on the result of some computation.
139−--
140−-- @ivecSepBy e sep@ describes a vector with elements @e@ separated by @sep@.
141−ivecSepBy :: Syntax syn => syn Int (Int, a) -> syn () () -> syn Int (V.Vector a)
142−ivecSepBy e sep = sibind $ iso (\n -> constant n #>> ivecNSepBy n e sep)
143− (\v -> constant (V.length v) #>> ivecNSepBy (V.length v) e sep)
144−
145−-- | Constant size unboxed vector with separators.
146−--
147−-- @uvecNSepBy n e sep@ describes a size @n@ vector with elements @e@ separated by @sep@.
148−uvecNSepBy :: (Syntax syn, VU.Unbox a) => Int -> syn () a -> syn () () -> syn () (VU.Vector a)
149−uvecNSepBy n e sep = uivecNSepBy n (unit ^>> second e) sep
150−
151−-- | Constant size unboxed vector with separators and index-aware elements.
152−--
153−-- @uivecNSepBy n e sep@ describes a size @n@ vector with elements @e@ separated by @sep@.
154−-- Each element gets its index and should output a value and the index unchanged.
155−uivecNSepBy :: (Syntax syn, VU.Unbox a) => Int -> syn Int (Int, a)
156− -> syn () () -> syn () (VU.Vector a)
157−uivecNSepBy n e sep = uivecN n $ sibind $ iso el (el . fst)
158− where
159− el k | k < n - 1 = unit ^>> e *** sep >># unit
160− | otherwise = e
161−
162−-- | Runtime sized unboxed vector. The size can depend on the result of some computation.
163−--
164−-- @uvec e@ describes a vector with elements @e@.
165−uvec :: (Syntax syn, VU.Unbox a) => syn () a -> syn Int (VU.Vector a)
166−uvec e = uvecSepBy e (siarr id)
167−
168−-- | Runtime sized unboxed vector with separators. The size can depend on the result of some
169−-- computation.
170−--
171−-- @uvecSepBy e sep@ describes a vector with elements @e@ separated by @sep@.
172−uvecSepBy :: (Syntax syn, VU.Unbox a) => syn () a -> syn () () -> syn Int (VU.Vector a)
173−uvecSepBy e sep = uivecSepBy (unit ^>> second e) sep
174−
175−-- | Runtime sized unboxed vector with index-aware elements. The size can depend on the result
176−-- of some computation.
177−--
178−-- @uivec e@ describes a vector with elements @e@.
179−uivec :: (Syntax syn, VU.Unbox a) => syn Int (Int, a) -> syn Int (VU.Vector a)
180−uivec e = uivecSepBy e (siarr id)
45+-- | Tries to apply the actions in the list in order, until one of
46+-- 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")
18149
182−-- | Runtime sized unboxed vector with index-aware elements and separators. The size can depend
183−-- on the result of some computation.
184−--
185−-- @uivecSepBy e sep@ describes a vector with elements @e@ separated by @sep@.
186−uivecSepBy :: (Syntax syn, VU.Unbox a) => syn Int (Int, a)
187− -> syn () () -> syn Int (VU.Vector a)
188−uivecSepBy e sep = sibind $ iso (\n -> constant n #>> uivecNSepBy n e sep)
189− (\v -> constant (VU.length v)
190− #>> uivecNSepBy (VU.length v) e sep)
50+-- | Combine two alternatives.
51+eitherOf :: SemiIsoAlternative f => f a -> f b -> f (Either a b)
52+eitherOf a b = _Left /$/ a /|/ _Right /$/ b
Data/Syntax/Indent.hs+43−39
@@ -1,16 +1,16 @@
1−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
21{-# LANGUAGE FlexibleInstances #-}
3−{-# LANGUAGE TypeFamilies #-}
2+{-# LANGUAGE UndecidableInstances #-}
3+{-# LANGUAGE MultiParamTypeClasses #-}
44{- |
55Module : Data.Syntax.Indent
6−Description : Simple indentation.
6+Description : 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 category transformer.
13+Provides a very simple indentation as a \"monad\" transformer.
1414-}
1515module Data.Syntax.Indent (
1616 Indent,
@@ -19,55 +19,59 @@ module Data.Syntax.Indent (
1919 indented
2020 ) where
2121
22−import Control.Category
23−import Control.Category.Reader
24−import Control.Category.Structures
25−import Control.SIArrow
22+import Data.SemiIsoFunctor
2623import Data.Syntax
2724import Data.Syntax.Char
2825import Data.Syntax.Combinator
29−import Prelude hiding (takeWhile, take, id, (.))
26+import Prelude hiding (takeWhile, take)
3027
3128-- | Adds indentation to a syntax description.
32−newtype Indent cat a b = Indent { getIndent :: ReaderCT (Int, cat () ()) cat a b }
33− deriving (Category, Products, Coproducts, CatPlus, SIArrow)
29+newtype Indent m a = Indent { unIndent :: (Int, m ()) -> m a }
3430
35−instance CatTrans Indent where
36− clift = Indent . clift
31+instance SemiIsoFunctor m => SemiIsoFunctor (Indent m) where
32+ simap f (Indent g) = Indent $ \i -> simap f (g i)
3733
38−-- Generalized newtype deriving cannot derive this :(
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
3937
40−instance Syntax syn => Syntax (Indent syn) where
41− type Seq (Indent syn) = Seq syn
42− anyChar = Indent anyChar
43− char = Indent . char
44− notChar = Indent . notChar
45− satisfy = Indent . satisfy
46− satisfyWith ai = Indent . satisfyWith ai
47− string = Indent . string
48− take = Indent . take
49− takeWhile = Indent . takeWhile
50− takeWhile1 = Indent . takeWhile1
51− takeTill = Indent . takeTill
52− takeTill1 = Indent . takeTill1
38+instance SemiIsoAlternative m => SemiIsoAlternative (Indent m) where
39+ siempty = Indent $ \_ -> siempty
40+ Indent f /|/ Indent g = Indent $ \i -> f i /|/ g i
5341
54−instance SyntaxChar syn => SyntaxChar (Indent syn) where
55− decimal = Indent decimal
56− hexadecimal = Indent hexadecimal
57− scientific = Indent scientific
58− realFloat = Indent realFloat
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
5964
6065-- | @runIndent m tab@ runs the 'Indent' transformer using @tab@ once for each
6166-- level of indentation.
62−runIndent :: Indent cat a b -> cat () () -> cat a b
63−runIndent (Indent m) tab = runReaderCT m (0, tab)
67+runIndent :: Indent m a -> m () -> m a
68+runIndent = ($ 0) . curry . unIndent
6469
6570-- | Inserts a new line and correct indentation, but does not
6671-- require any formatting when parsing (it just skips all white space).
67−breakLine :: SyntaxChar syn => Indent syn () ()
68−breakLine = Indent . ReaderCT $ \(i, tab) ->
69− opt (char '\n') /* opt (sireplicate_ i tab) /* spaces_
72+breakLine :: SyntaxChar syn seq => Indent syn ()
73+breakLine = Indent $ \(i, tab) -> opt (char '\n') /* opt (sireplicate_ i tab) /* spaces_
7074
7175-- | Increases the indentation level of its argument by one.
72−indented :: Indent cat a b -> Indent cat a b
73−indented (Indent f) = Indent . ReaderCT $ \(i, tab) -> runReaderCT f (i + 1, tab)
76+indented :: Indent m a -> Indent m a
77+indented (Indent f) = Indent $ \(i, tab) -> f (i + 1, tab)
syntax.cabal+21−23
@@ -1,35 +1,33 @@
11name: syntax
2−version: 1.0.0.0
3−synopsis: Reversible parsing and pretty-printing.
2+version: 0.3.0.0
3+synopsis: Syntax descriptions for unified parsing and pretty-printing.
44description:
5− "syntax" allows you to write a single syntax description and instantiate is both as a parser and a pretty printer.
5+ 'syntax' allows you to write a single syntax description and instantiate is both as a parser and a pretty printer.
66 .
7− Syntax descriptions are written in applicative or arrow style. The library uses a custom typeclass hierarchy, provided
8− by the "semi-iso" package. Most of the time you will be using operators like '/$/', '/*/' and '/+/' (= '<|>'), just
9− like parser combinators. When more power is needed - e.g. when the syntax depends on the parsed or printed value -
10− you turn to arrows.
7+ The interface is based on a custom Functor\/Applicative\/Monad hierarchy, provided by the 'semi-iso' package. You fmap using
8+ a semi-isomorphism instead of function. A semi-isomorphism is a isomorphism that can fail in both directions, with slightly
9+ weakened laws. It is worth to note that @Iso@s and @Prism@s from 'lens' are valid semi-isomorphisms :)
1110 .
12− Semi-isomorphisms from "semi-iso" are the basic building block of syntax descriptions. I recommend reading
13− the hackage page of "semi-iso" first, as it contains much more information.
11+ Once you write a description you can, for example turn it into an Attoparsec parser, or into a 'Data.Syntax.Printer.Text.Printer'.
1412 .
15− Once you write a syntax description (polymorphic in the syntax category) you can instantiate it both as a parser or as
16− a pretty-printer. The library "syntax-attoparsec" gives you the ability to extract an Attoparsec parser. Pretty-printing
17− is implemented by the "syntax-printer" library, which uses Text and ByteString builders. (Note that formatting is handled
18− by "syntax" itself, not by the printer library)
13+ See @syntax-example@ and @syntax-example-json@ for examples, @syntax-attoparsec@ and @syntax-printer@ for a parser/printer implementation.
1914 .
20− Advanced formatting and parsing (for example indentation, haskell layout rule) is implemented as category transformers
21− (similar to monad transformers). Currently only simple indentation is implemented (in "Data.Syntax.Indent") - basically
22− a reader category transformer that tracks current indentation level. I plan on implementing Haskell layout rule in the
23− future.
15+ The library was inspired by:
2416 .
25− The library can work with both text and binary data. Alas, there are no binary combinators implemented yet.
26− I will implement them when i have the time (but these category transformers look so much more interesting for now ;).
17+ * Rendel, Tillmann, and Klaus Ostermann. "Invertible syntax descriptions: unifying parsing and pretty printing." ACM Sigplan Notices. Vol. 45. No. 11. ACM, 2010.
2718 .
28− EXAMPLES! See @syntax-example@ and @syntax-example-json@ for examples.
19+ TODO:
2920 .
30− * "syntax-example" implements a simple lambda calculus.
21+ * Research relative monads and relative monad transformers. Indent is basically a Reader monad over a syntax. How would a State monad look?
3122 .
32− * "syntax-example-json" implements a json parser and pretty printer.
23+ * Try to implement Haskell layout rule.
24+ .
25+ * Combinators for binary data formats, vectors.
26+ .
27+ * Better error messages.
28+ .
29+ * Maybe an implementation of do notation for SemiIsoMonad with QuasiQuoters, like the codo notation for comonads.
30+
3331license: MIT
3432license-file: LICENSE
3533author: Paweł Nowak
@@ -48,5 +46,5 @@ library
4846 Data.Syntax.Char
4947 Data.Syntax.Combinator
5048 Data.Syntax.Indent
51− build-depends: base >= 4 && < 5, mono-traversable, lens >= 4, semi-iso >= 1, scientific >= 0.3, text, vector
49+ build-depends: base >= 4 && < 5, mono-traversable, lens >= 4, semi-iso >= 0.5.0, scientific >= 0.3, text
5250 default-language: Haskell2010