module Monomer.Widgets.Util.Parser where
import Control.Applicative ((<|>))
import Data.Text (Text)
import qualified Data.Attoparsec.Text as A
import qualified Data.Text as T
join :: [A.Parser Text] -> A.Parser Text
join :: [Parser Text] -> Parser Text
join [] = Text -> Parser Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
T.empty
join (Parser Text
x:[Parser Text]
xs) = Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
(<>) (Text -> Text -> Text) -> Parser Text -> Parser Text (Text -> Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Text
x Parser Text (Text -> Text) -> Parser Text -> Parser Text
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Parser Text] -> Parser Text
join [Parser Text]
xs
upto :: Int -> A.Parser Text -> A.Parser Text
upto :: Int -> Parser Text -> Parser Text
upto Int
n Parser Text
p
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 = Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
(<>) (Text -> Text -> Text) -> Parser Text -> Parser Text (Text -> Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Text -> Parser Text
forall i a. Parser i a -> Parser i a
A.try Parser Text
p Parser Text (Text -> Text) -> Parser Text -> Parser Text
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Int -> Parser Text -> Parser Text
upto (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Parser Text
p Parser Text -> Parser Text -> Parser Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Parser Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
T.empty
| Bool
otherwise = Text -> Parser Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
T.empty
single :: Char -> A.Parser Text
single :: Char -> Parser Text
single Char
c = Char -> Text
T.singleton (Char -> Text) -> Parser Text Char -> Parser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Char -> Parser Text Char
A.char Char
c