module Language.Bash.Parse.Builder
(
Builder
, fromChar
, fromString
, toString
, (<+>)
, many
, many1
, oneOf
, noneOf
, char
, anyChar
, satisfy
, string
, span
, matchedPair
) where
import Prelude hiding (span)
import Control.Applicative hiding (many)
import qualified Control.Applicative as Applicative
import Data.Monoid
import qualified Text.Parsec.Char as P
import Text.Parsec.Prim hiding ((<|>), many)
infixr 4 <+>
type Builder = Endo String
fromChar :: Char -> Builder
fromChar = Endo . showChar
fromString :: String -> Builder
fromString = Endo . showString
toString :: Builder -> String
toString = flip appEndo ""
(<+>) :: (Applicative f, Monoid a) => f a -> f a -> f a
(<+>) = liftA2 mappend
many :: (Alternative f, Monoid a) => f a -> f a
many = fmap mconcat . Applicative.many
many1 :: (Alternative f, Monoid a) => f a -> f a
many1 = fmap mconcat . Applicative.some
oneOf :: Stream s m Char => [Char] -> ParsecT s u m Builder
oneOf cs = fromChar <$> P.oneOf cs
noneOf :: Stream s m Char => [Char] -> ParsecT s u m Builder
noneOf cs = fromChar <$> P.noneOf cs
char :: Stream s m Char => Char -> ParsecT s u m Builder
char c = fromChar c <$ P.char c
anyChar :: Stream s m Char => ParsecT s u m Builder
anyChar = fromChar <$> P.anyChar
satisfy :: Stream s m Char => (Char -> Bool) -> ParsecT s u m Builder
satisfy p = fromChar <$> P.satisfy p
string :: Stream s m Char => String -> ParsecT s u m Builder
string s = fromString s <$ P.string s
span
:: Stream s m Char
=> Char
-> Char
-> ParsecT s u m Builder
-> ParsecT s u m Builder
span start end esc = char start *> many inner <* char end
where
inner = esc <|> satisfy (/= end)
matchedPair
:: Stream s m Char
=> Char
-> Char
-> ParsecT s u m Builder
-> ParsecT s u m Builder
matchedPair start end esc = char start <+> many inner <+> char end
where
inner = esc <|> satisfy (/= end)