on master help sign in
3 files changed+18−4
since splitsnapshots
unifiedsplit
Data/Syntax.hs+4 Data/Syntax/Combinator.hs+10−1 syntax.cabal+4−3
Data/Syntax.hs+4
@@ -166,6 +166,10 @@ instance Syntax syn => Syntax (ReaderCT env syn) where
166166 takeWhile1 = clift . takeWhile1
167167 takeTill = clift . takeTill
168168 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
169173
170174-- | Execute a computation in an isolated context.
171175--
Data/Syntax/Combinator.hs+10−1
@@ -26,6 +26,9 @@ module Data.Syntax.Combinator
2626 sepBy,
2727 sepBy1,
2828
29+ -- * Arrowized combinators.
30+ takeArr,
31+
2932 -- * Vectors.
3033 vecNSepBy,
3134 ivecNSepBy,
@@ -48,10 +51,11 @@ import Control.Category.Structures
4851import Control.Lens
4952import Control.Lens.SemiIso
5053import Control.SIArrow
54+import qualified Data.MonoTraversable as Seq
5155import Data.Syntax
5256import qualified Data.Vector as V
5357import qualified Data.Vector.Unboxed as VU
54−import Prelude hiding (id, (.))
58+import Prelude hiding (id, (.), take)
5559
5660-- | One or zero occurences of @f@.
5761optional :: SIArrow cat => cat () a -> cat () (Maybe a)
@@ -89,6 +93,11 @@ sepBy v s = sepBy1 v s /+/ sipure _Empty
8993sepBy1 :: SIArrow cat => cat () a -> cat () () -> cat () [a]
9094sepBy1 v s = _Cons /$/ v /*/ (s */ sepBy1 v s /+/ sipure _Empty)
9195
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+
92101-- | Constant size vector with separators.
93102--
94103-- @vecNSepBy n e sep@ describes a size @n@ vector with elements @e@ separated by @sep@.
syntax.cabal+4−3
@@ -5,8 +5,9 @@ description:
55 "syntax" allows you to write a single syntax description and instantiate is both as a parser and a pretty printer.
66 .
77 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 like parser combinators.
9− When more power is needed - e.g. when the syntax depends on the parsed or printed value - you turn to arrows.
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.
1011 .
1112 Semi-isomorphisms from "semi-iso" are the basic building block of syntax descriptions. I recommend reading
1213 the hackage page of "semi-iso" first, as it contains much more information.
@@ -21,7 +22,7 @@ description:
2122 a reader category transformer that tracks current indentation level. I plan on implementing Haskell layout rule in the
2223 future.
2324 .
24− Note that the library can work with both text and binary data. Alas, there are no binary combinators implemented yet.
25+ The library can work with both text and binary data. Alas, there are no binary combinators implemented yet.
2526 I will implement them when i have the time (but these category transformers look so much more interesting for now ;).
2627 .
2728 EXAMPLES! See @syntax-example@ and @syntax-example-json@ for examples.