@@ -1,6 +1,5 @@
1{-# LANGUAGE TypeFamilies #-}
1{-# LANGUAGE TypeFamilies #-}
2{-# LANGUAGE ConstraintKinds #-}
2{-# LANGUAGE ConstraintKinds #-}
3−{-# LANGUAGE MultiParamTypeClasses #-}
5Module : Data.Syntax.Char
4Module : Data.Syntax.Char
6Description : Char specific combinators.
5Description : Char specific combinators.
@@ -27,11 +26,11 @@ module Data.Syntax.Char (
29import Control.Lens.SemiIso
28import Control.Lens.SemiIso
29+import Control.SIArrow
30import Data.Bits
30import Data.Bits
31import Data.Char
31import Data.Char
32import Data.MonoTraversable
32import Data.MonoTraversable
33import Data.Scientific (Scientific)
33import Data.Scientific (Scientific)
34−import Data.SemiIsoFunctor
35import Data.Syntax
34import Data.Syntax
36import Data.Syntax.Combinator
35import Data.Syntax.Combinator
37import Data.Text (Text)
36import Data.Text (Text)
@@ -40,47 +39,47 @@ import Data.Text (Text)
41-- Note: methods of this class do not have default implementations (for now),
40-- Note: methods of this class do not have default implementations (for now),
42-- because their code is quite ugly and already written in most parser libraries.
41-- because their code is quite ugly and already written in most parser libraries.
43−class (Syntax syn seq, Element seq ~ Char) => SyntaxChar syn seq where
42+class (Syntax syn, Element (Seq syn) ~ Char) => SyntaxChar syn where
44 -- | An unsigned decimal number.
43 -- | An unsigned decimal number.
45− decimal :: Integral a => syn a
44+ decimal :: Integral a => syn () a
47 -- | An unsigned hexadecimal number.
46 -- | An unsigned hexadecimal number.
48− hexadecimal :: (Integral a, Bits a) => syn a
47+ hexadecimal :: (Integral a, Bits a) => syn () a
50 -- | A signed real number.
49 -- | A signed real number.
51− realFloat :: RealFloat a => syn a
50+ realFloat :: RealFloat a => syn () a
53 -- | A scientific number.
52 -- | A scientific number.
54− scientific :: syn Scientific
53+ scientific :: syn () Scientific
56 {-# MINIMAL decimal, hexadecimal, realFloat, scientific #-}
55 {-# MINIMAL decimal, hexadecimal, realFloat, scientific #-}
58-- | An useful synonym for SyntaxChars with Text sequences.
57-- | An useful synonym for SyntaxChars with Text sequences.
59−type SyntaxText syn = SyntaxChar syn Text
58+type SyntaxText syn = (SyntaxChar syn, Seq syn ~ Text)
61-- | A number with an optional leading '+' or '-' sign character.
60-- | A number with an optional leading '+' or '-' sign character.
62−signed :: (Real a, SyntaxChar syn seq) => syn a -> syn a
61+signed :: (Real a, SyntaxChar syn) => syn () a -> syn () a
63signed n = _Negative /$/ char '-' */ n
62signed n = _Negative /$/ char '-' */ n
64− /|/ opt_ (char '+') */ n
63+ /+/ opt_ (char '+') */ n
66-- | Accepts zero or more spaces. Generates a single space.
65-- | Accepts zero or more spaces. Generates a single space.
67−spaces :: SyntaxChar syn seq => syn ()
66+spaces :: SyntaxChar syn => syn () ()
68spaces = opt spaces1
67spaces = opt spaces1
70-- | Accepts zero or more spaces. Generates no output.
69-- | Accepts zero or more spaces. Generates no output.
71−spaces_ :: SyntaxChar syn seq => syn ()
70+spaces_ :: SyntaxChar syn => syn () ()
72spaces_ = opt_ spaces1
71spaces_ = opt_ spaces1
74-- | Accepts one or more spaces. Generates a single space.
73-- | Accepts one or more spaces. Generates a single space.
75−spaces1 :: SyntaxChar syn seq => syn ()
74+spaces1 :: SyntaxChar syn => syn () ()
76spaces1 = constant (opoint ' ') /$/ takeWhile1 isSpace
75spaces1 = constant (opoint ' ') /$/ takeWhile1 isSpace
78-- | Accepts a single newline. Generates a newline.
77-- | Accepts a single newline. Generates a newline.
79−endOfLine :: SyntaxChar syn seq => syn ()
78+endOfLine :: SyntaxChar syn => syn () ()
80endOfLine = char '\n'
79endOfLine = char '\n'
82-- | A decimal digit.
81-- | A decimal digit.
83−digitDec :: SyntaxChar syn seq => syn Int
82+digitDec :: SyntaxChar syn => syn () Int
84digitDec = semiIso toChar toInt /$/ anyChar
83digitDec = semiIso toChar toInt /$/ anyChar
85 where toInt c | isDigit c = Right (digitToInt c)
84 where toInt c | isDigit c = Right (digitToInt c)
86 | otherwise = Left ("Expected a decimal digit, got " ++ [c])
85 | otherwise = Left ("Expected a decimal digit, got " ++ [c])
@@ -88,7 +87,7 @@ digitDec = semiIso toChar toInt /$/ anyChar
88 | otherwise = Left ("Expected a decimal digit, got number " ++ show i)
87 | otherwise = Left ("Expected a decimal digit, got number " ++ show i)
90-- | An octal digit.
89-- | An octal digit.
91−digitOct :: SyntaxChar syn seq => syn Int
90+digitOct :: SyntaxChar syn => syn () Int
92digitOct = semiIso toChar toInt /$/ anyChar
91digitOct = semiIso toChar toInt /$/ anyChar
93 where toInt c | isOctDigit c = Right (digitToInt c)
92 where toInt c | isOctDigit c = Right (digitToInt c)
94 | otherwise = Left ("Expected an octal digit, got " ++ [c])
93 | otherwise = Left ("Expected an octal digit, got " ++ [c])
@@ -96,7 +95,7 @@ digitOct = semiIso toChar toInt /$/ anyChar
96 | otherwise = Left ("Expected an octal digit, got number " ++ show i)
95 | otherwise = Left ("Expected an octal digit, got number " ++ show i)
98-- | A hex digit.
97-- | A hex digit.
99−digitHex :: SyntaxChar syn seq => syn Int
98+digitHex :: SyntaxChar syn => syn () Int
100digitHex = semiIso toChar toInt /$/ anyChar
99digitHex = semiIso toChar toInt /$/ anyChar
101 where toInt c | isHexDigit c = Right (digitToInt c)
100 where toInt c | isHexDigit c = Right (digitToInt c)
102 | otherwise = Left ("Expected a hex digit, got " ++ [c])
101 | otherwise = Left ("Expected a hex digit, got " ++ [c])