@@ -1,7 +1,6 @@
1+{-# LANGUAGE TypeFamilies #-}
1{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE DefaultSignatures #-}
3{-# LANGUAGE DefaultSignatures #-}
3−{-# LANGUAGE MultiParamTypeClasses #-}
4−{-# LANGUAGE FunctionalDependencies #-}
6Module : Data.Syntax
5Module : Data.Syntax
7Description : Abstract syntax description.
6Description : Abstract syntax description.
@@ -11,22 +10,28 @@ License : MIT
11Maintainer : Paweł Nowak <pawel834@gmail.com>
10Maintainer : Paweł Nowak <pawel834@gmail.com>
12Stability : experimental
11Stability : experimental
14−Abstract syntax descriptions based on semi-isomorphisms.
13+Reversible parsing and pretty-printing.
16module Data.Syntax (
15module Data.Syntax (
17 -- * Syntax.
16 -- * Syntax.
18 Syntax(..),
17 Syntax(..),
19 -- * Common isomorphisms.
19 -- * Common isomorphisms.
23−import Prelude hiding (take, takeWhile)
23+import Prelude hiding (take, takeWhile, id, (.))
25+import Control.Category
26+import Control.Category.Reader
27+import Control.Category.Structures
25−import Control.Lens.Iso
28+import Control.Lens.Iso
26−import Control.Lens.SemiIso
29+import Control.Lens.SemiIso
30+import Control.SIArrow
27−import Data.MonoTraversable
31+import Data.MonoTraversable
28−import Data.SemiIsoFunctor
29−import Data.Sequences hiding (take, takeWhile)
32+import Data.Sequences hiding (take, takeWhile, replicate)
33+import qualified Data.Vector as V
34+import qualified Data.Vector.Unboxed as VU
31-- | An isomorphism between a sequence and a list of its elements.
36-- | An isomorphism between a sequence and a list of its elements.
32packed :: IsSequence seq => Iso' seq [Element seq]
37packed :: IsSequence seq => Iso' seq [Element seq]
@@ -34,63 +39,150 @@ packed = iso otoList fromList
35-- | An abstract syntax description based on semi-isomorphisms.
40-- | An abstract syntax description based on semi-isomorphisms.
37−-- This class can be implemented by both parsers and printers (and maybe more?).
42+-- This class can be implemented by both parsers and printers.
39-- The usual use is to write a polymorphic syntax description and instantiate it
44-- The usual use is to write a polymorphic syntax description and instantiate it
40−-- both as a parser and a printer. An example is available in the 'syntax-example'
45+-- both as a parser and a printer. Examples are available in 'syntax-example' and
41−-- package.
46+-- 'syntax-example-json' packages.
43-- Methods of this class try to mimic "Data.Attoparsec.Text" interface.
48-- Methods of this class try to mimic "Data.Attoparsec.Text" interface.
44−class ( SemiIsoAlternative syn
49+class ( SIArrow syn
46− , IsSequence seq
50+ , IsSequence (Seq syn)
47− , Eq seq
51+ , Eq (Seq syn)
48− , Eq (Element seq))
52+ , Eq (Element (Seq syn)))
49− => Syntax syn seq | syn -> seq
53+ => Syntax syn
55+ -- | The sequence type used by this syntax.
52 -- | Any character.
58 -- | Any character.
53− anyChar :: syn (Element seq)
59+ anyChar :: syn () (Element (Seq syn))
55 -- | A specific character.
61 -- | A specific character.
56− char :: Element seq -> syn ()
62+ char :: Element (Seq syn) -> syn () ()
57 char c = rev (exact c) /$/ anyChar
63 char c = rev (exact c) /$/ anyChar
59 -- | Any character except the given one.
65 -- | Any character except the given one.
60− notChar :: Element seq -> syn (Element seq)
66+ notChar :: Element (Seq syn) -> syn () (Element (Seq syn))
61 notChar c = bifiltered (/= c) /$/ anyChar
67 notChar c = bifiltered (/= c) /$/ anyChar
63 -- | Any character satisfying a predicate.
69 -- | Any character satisfying a predicate.
64− satisfy :: (Element seq -> Bool) -> syn (Element seq)
70+ satisfy :: (Element (Seq syn) -> Bool) -> syn () (Element (Seq syn))
65 satisfy p = bifiltered p /$/ anyChar
71 satisfy p = bifiltered p /$/ anyChar
67 -- | Transforms a character using a SemiIso and filters out values
73 -- | Transforms a character using a SemiIso and filters out values
68 -- not satisfying the predicate.
74 -- not satisfying the predicate.
69− satisfyWith :: ASemiIso' a (Element seq) -> (a -> Bool) -> syn a
75+ satisfyWith :: ASemiIso' a (Element (Seq syn)) -> (a -> Bool) -> syn () a
70 satisfyWith ai p = bifiltered p . ai /$/ anyChar
76 satisfyWith ai p = bifiltered p . ai /$/ anyChar
72 -- | A specific string.
78 -- | A specific string.
73− string :: seq -> syn ()
79+ string :: Seq syn -> syn () ()
74 string s = rev (exact s) /$/ take (olength s)
80 string s = rev (exact s) /$/ take (olength s)
76 -- | A string of length @n@.
82 -- | A string of length @n@.
77− take :: Int -> syn seq
83+ take :: Int -> syn () (Seq syn)
78 take n = packed /$/ sireplicate n anyChar
84 take n = packed /$/ sireplicate n anyChar
80 -- | Maximal string which elements satisfy a predicate.
86 -- | Maximal string which elements satisfy a predicate.
81− takeWhile :: (Element seq -> Bool) -> syn seq
87+ takeWhile :: (Element (Seq syn) -> Bool) -> syn () (Seq syn)
82 takeWhile p = packed /$/ simany (satisfy p)
88 takeWhile p = packed /$/ simany (satisfy p)
84 -- | Maximal non-empty string which elements satisfy a predicate.
90 -- | Maximal non-empty string which elements satisfy a predicate.
85− takeWhile1 :: (Element seq -> Bool) -> syn seq
91+ takeWhile1 :: (Element (Seq syn) -> Bool) -> syn () (Seq syn)
86 takeWhile1 p = packed /$/ sisome (satisfy p)
92 takeWhile1 p = packed /$/ sisome (satisfy p)
88 -- | Maximal string which elements do not satisfy a predicate.
94 -- | Maximal string which elements do not satisfy a predicate.
89− takeTill :: (Element seq -> Bool) -> syn seq
95+ takeTill :: (Element (Seq syn) -> Bool) -> syn () (Seq syn)
90 takeTill p = takeWhile (not . p)
96 takeTill p = takeWhile (not . p)
92 -- | Maximal non-empty string which elements do not satisfy a predicate.
98 -- | Maximal non-empty string which elements do not satisfy a predicate.
93− takeTill1 :: (Element seq -> Bool) -> syn seq
99+ takeTill1 :: (Element (Seq syn) -> Bool) -> syn () (Seq syn)
94 takeTill1 p = takeWhile1 (not . p)
100 takeTill1 p = takeWhile1 (not . p)
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.
106+ -- @vecN n e@ describes a size @n@ vector with elements @e@.
108+ -- Also see 'Data.Syntax.Combinator.vec'.
109+ vecN :: Int -> syn () a -> syn () (V.Vector a)
110+ vecN n e = packed /$/ sireplicate n e
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.
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.
119+ -- Also see 'Data.Syntax.Combinator.ivec'.
120+ ivecN :: Int -> syn Int (Int, a) -> syn () (V.Vector a)
121+ ivecN n e = (packed /$/)
123+ $ map (\(i, e') -> constant i ^>> e'
124+ >>> first (sipure (constant i))
126+ $ zip [0 .. n-1] (replicate n e)
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.
132+ -- @vecN n e@ describes a size @n@ vector with elements @e@.
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
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.
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.
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 /$/)
149+ $ map (\(i, e') -> constant i ^>> e'
150+ >>> first (sipure (constant i))
152+ $ zip [0 .. n-1] (replicate n e)
96 {-# MINIMAL anyChar #-}
154 {-# MINIMAL anyChar #-}
156+instance Syntax syn => Syntax (ReaderCT env syn) where
157+ type Seq (ReaderCT env syn) = Seq syn
158+ anyChar = clift anyChar
160+ notChar = clift . notChar
161+ satisfy = clift . satisfy
162+ satisfyWith ai = clift . satisfyWith ai
163+ string = clift . string
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
174+-- | Execute a computation in an isolated context.
176+-- The motivating example: you want to write a function
178+-- > serializeList :: Syntax syn => syn () a -> syn () [a]
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