Add instances for Syntax and SyntaxChar.

This commit is contained in:
Paweł Nowak 2014-12-11 02:05:55 +01:00
parent 98259cc26f
commit c083497d13
3 changed files with 40 additions and 16 deletions

View File

@ -21,6 +21,7 @@ module Data.Syntax (
import Prelude hiding (take, takeWhile)
import Control.Category.Reader
import Control.Lens.Iso
import Control.Lens.SemiIso
import Control.SIArrow
@ -70,7 +71,7 @@ class ( SIArrow syn
satisfyWith ai p = bifiltered p . ai /$/ anyChar
-- | A specific string.
string :: (Seq syn) -> syn () ()
string :: Seq syn -> syn () ()
string s = rev (exact s) /$/ take (olength s)
-- | A string of length @n@.
@ -94,3 +95,17 @@ class ( SIArrow syn
takeTill1 p = takeWhile1 (not . p)
{-# MINIMAL anyChar #-}
instance Syntax syn => Syntax (ReaderCT env syn) where
type Seq (ReaderCT env syn) = Seq syn
anyChar = clift anyChar
char = clift . char
notChar = clift . notChar
satisfy = clift . satisfy
satisfyWith ai = clift . satisfyWith ai
string = clift . string
take = clift . take
takeWhile = clift . takeWhile
takeWhile1 = clift . takeWhile1
takeTill = clift . takeTill
takeTill1 = clift . takeTill1

View File

@ -25,6 +25,7 @@ module Data.Syntax.Char (
digitHex
) where
import Control.Category.Reader
import Control.Lens.SemiIso
import Control.SIArrow
import Data.Bits
@ -54,6 +55,12 @@ class (Syntax syn, Element (Seq syn) ~ Char) => SyntaxChar syn where
{-# MINIMAL decimal, hexadecimal, realFloat, scientific #-}
instance SyntaxChar syn => SyntaxChar (ReaderCT env syn) where
decimal = clift decimal
hexadecimal = clift hexadecimal
scientific = clift scientific
realFloat = clift realFloat
-- | An useful synonym for SyntaxChars with Text sequences.
type SyntaxText syn = (SyntaxChar syn, Seq syn ~ Text)

View File

@ -34,25 +34,27 @@ newtype Indent cat a b = Indent { unIndent :: ReaderCT (Int, cat () ()) cat a b
instance CategoryTrans Indent where
clift = Indent . clift
-- Generalized newtype deriving cannot derive this :(
instance Syntax syn => Syntax (Indent syn) where
type Seq (Indent syn) = Seq syn
anyChar = clift anyChar
char = clift . char
notChar = clift . notChar
satisfy = clift . satisfy
satisfyWith ai = clift . satisfyWith ai
string = clift . string
take = clift . take
takeWhile = clift . takeWhile
takeWhile1 = clift . takeWhile1
takeTill = clift . takeTill
takeTill1 = clift . takeTill1
anyChar = Indent anyChar
char = Indent . char
notChar = Indent . notChar
satisfy = Indent . satisfy
satisfyWith ai = Indent . satisfyWith ai
string = Indent . string
take = Indent . take
takeWhile = Indent . takeWhile
takeWhile1 = Indent . takeWhile1
takeTill = Indent . takeTill
takeTill1 = Indent . takeTill1
instance SyntaxChar syn => SyntaxChar (Indent syn) where
decimal = clift decimal
hexadecimal = clift hexadecimal
scientific = clift scientific
realFloat = clift realFloat
decimal = Indent decimal
hexadecimal = Indent hexadecimal
scientific = Indent scientific
realFloat = Indent realFloat
-- | @runIndent m tab@ runs the 'Indent' transformer using @tab@ once for each
-- level of indentation.