on master help sign in
Add instances for Syntax and SyntaxChar.
commitc083497d13832d0cca2c1b2e05d52c79c8a7694d authorPaweł Nowak <pawel834@gmail.com> dateThu 11 Dec 2014, 02:05 (12 years ago) parent98259cc
3 files changed+40−16
Data/Syntax.hs+16−1 Data/Syntax/Char.hs+7 Data/Syntax/Indent.hs+17−15
Data/Syntax.hs+16−1
@@ -21,6 +21,7 @@ module Data.Syntax (
2121
2222import Prelude hiding (take, takeWhile)
2323
24+import Control.Category.Reader
2425import Control.Lens.Iso
2526import Control.Lens.SemiIso
2627import Control.SIArrow
@@ -70,7 +71,7 @@ class ( SIArrow syn
7071 satisfyWith ai p = bifiltered p . ai /$/ anyChar
7172
7273 -- | A specific string.
73− string :: (Seq syn) -> syn () ()
74+ string :: Seq syn -> syn () ()
7475 string s = rev (exact s) /$/ take (olength s)
7576
7677 -- | A string of length @n@.
@@ -94,3 +95,17 @@ class ( SIArrow syn
9495 takeTill1 p = takeWhile1 (not . p)
9596
9697 {-# MINIMAL anyChar #-}
98+
99+instance Syntax syn => Syntax (ReaderCT env syn) where
100+ type Seq (ReaderCT env syn) = Seq syn
101+ anyChar = clift anyChar
102+ char = clift . char
103+ notChar = clift . notChar
104+ satisfy = clift . satisfy
105+ satisfyWith ai = clift . satisfyWith ai
106+ string = clift . string
107+ take = clift . take
108+ takeWhile = clift . takeWhile
109+ takeWhile1 = clift . takeWhile1
110+ takeTill = clift . takeTill
111+ takeTill1 = clift . takeTill1
Data/Syntax/Char.hs+7
@@ -25,6 +25,7 @@ module Data.Syntax.Char (
2525 digitHex
2626 ) where
2727
28+import Control.Category.Reader
2829import Control.Lens.SemiIso
2930import Control.SIArrow
3031import Data.Bits
@@ -54,6 +55,12 @@ class (Syntax syn, Element (Seq syn) ~ Char) => SyntaxChar syn where
5455
5556 {-# MINIMAL decimal, hexadecimal, realFloat, scientific #-}
5657
58+instance SyntaxChar syn => SyntaxChar (ReaderCT env syn) where
59+ decimal = clift decimal
60+ hexadecimal = clift hexadecimal
61+ scientific = clift scientific
62+ realFloat = clift realFloat
63+
5764-- | An useful synonym for SyntaxChars with Text sequences.
5865type SyntaxText syn = (SyntaxChar syn, Seq syn ~ Text)
5966
Data/Syntax/Indent.hs+17−15
@@ -34,25 +34,27 @@ newtype Indent cat a b = Indent { unIndent :: ReaderCT (Int, cat () ()) cat a b
3434instance CategoryTrans Indent where
3535 clift = Indent . clift
3636
37+-- Generalized newtype deriving cannot derive this :(
38+
3739instance Syntax syn => Syntax (Indent syn) where
3840 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
41+ anyChar = Indent anyChar
42+ char = Indent . char
43+ notChar = Indent . notChar
44+ satisfy = Indent . satisfy
45+ satisfyWith ai = Indent . satisfyWith ai
46+ string = Indent . string
47+ take = Indent . take
48+ takeWhile = Indent . takeWhile
49+ takeWhile1 = Indent . takeWhile1
50+ takeTill = Indent . takeTill
51+ takeTill1 = Indent . takeTill1
5052
5153instance SyntaxChar syn => SyntaxChar (Indent syn) where
52− decimal = clift decimal
53− hexadecimal = clift hexadecimal
54− scientific = clift scientific
55− realFloat = clift realFloat
54+ decimal = Indent decimal
55+ hexadecimal = Indent hexadecimal
56+ scientific = Indent scientific
57+ realFloat = Indent realFloat
5658
5759-- | @runIndent m tab@ runs the 'Indent' transformer using @tab@ once for each
5860-- level of indentation.