Skip to content

Commit

Permalink
Switch to text based combinators
Browse files Browse the repository at this point in the history
  • Loading branch information
albertprz committed Nov 19, 2023
1 parent 6f000ff commit 6fa87d0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/Bookhound/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ satisfy cond ma = do
else
empty

withError :: String -> Parser a -> Parser a
withError :: Text -> Parser a -> Parser a
withError = withErrorN 0

withErrorN :: Int -> String -> Parser a -> Parser a
withErrorN :: Int -> Text -> Parser a -> Parser a
withErrorN n str = applyError . Set.singleton $ (n, ErrorAt str)


Expand Down Expand Up @@ -200,13 +200,13 @@ instance Show a => Show (ParseResult a) where
data ParseError
= UnexpectedEof
| ExpectedEof Input
| ErrorAt String
| ErrorAt Text
deriving (Eq, Ord)

instance Show ParseError where
show UnexpectedEof = "Unexpected end of stream"
show (ExpectedEof i) = "Expected end of stream, but got "
<> ">" <> unpack i <> "<"
show (ErrorAt s) = "Error at " <> s
show (ErrorAt s) = "Error at " <> unpack s

type Input = Text
10 changes: 5 additions & 5 deletions src/Bookhound/ParserCombinators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Bookhound.Parser (Parser, allOf, anyChar, anyOf, except, satisfy,
withError)

import Bookhound.Utils.List (hasMultiple, hasSome)
import Bookhound.Utils.Text (ToString (..))
import Bookhound.Utils.Text (ToText (..))
import Control.Applicative (liftA2, optional, (<|>))

import qualified Data.Foldable as Foldable
Expand Down Expand Up @@ -135,13 +135,13 @@ infixl 6 <#>
(<#>) = flip times

infixl 6 <?>
(<?>) :: Parser a -> String -> Parser a
(<?>) :: Parser a -> Text -> Parser a
(<?>) = flip withError

infixl 6 ->>-
(->>-) :: (ToString a, ToString b) => Parser a -> Parser b -> Parser Text
(->>-) p1 p2 = fmap pack $ (<>) <$> fmap toString p1
<*> fmap toString p2
(->>-) :: (ToText a, ToText b) => Parser a -> Parser b -> Parser Text
(->>-) p1 p2 = (<>) <$> fmap toText p1
<*> fmap toText p2

-- Apply Binary Operators
infixl 6 </\>
Expand Down
40 changes: 22 additions & 18 deletions src/Bookhound/Utils/Text.hs
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
module Bookhound.Utils.Text where

import Data.List (intercalate)
import Data.Text (Text, unpack)
import qualified Data.Foldable as Foldable
import Data.List (intercalate)
import Data.Text (Text, pack)
import qualified Data.Text as Text


class ToString a where
toString :: a -> String
class ToText a where
toText :: a -> Text

instance ToString Char where
toString = pure
instance ToText Char where
toText = Text.singleton

instance ToString Int where
toString = show
instance ToText Int where
toText = pack . show

instance ToString Text where
toString = unpack
instance ToText Text where
toText = id

instance ToString Integer where
toString = show
instance ToText Integer where
toText = pack . show

instance ToString Float where
toString = show
instance ToText Float where
toText = pack .show

instance ToString Double where
toString = show
instance ToText Double where
toText = pack . show

instance (ToString a, Foldable m) => ToString (m a) where
toString = concatMap toString
instance ToText a => ToText [a] where
toText = Text.concat . fmap toText

instance (ToText a, Foldable f, Functor f) => ToText (f a) where
toText = Text.concat . Foldable.toList . fmap toText


indent :: Int -> String -> String
Expand Down

0 comments on commit 6fa87d0

Please sign in to comment.