module Language.LBNF.Utils where
import Control.Monad (unless)
infixr 5 +++
infixr 5 ++++
infixr 5 +++++
infixr 2 |||
infixr 5 ...
infixr 3 ***
a +++ b = a ++ " " ++ b
a ++++ b = a ++ "\n" ++ b
a +++++ b = a ++ "\n\n" ++ b
prParenth s = if s == "" then "" else "(" ++ s ++ ")"
type Parser a b = [a] -> [(b,[a])]
(...) :: Parser a b -> Parser a c -> Parser a (b,c)
(p ... q) s = [((x,y),r) | (x,t) <- p s, (y,r) <- q t]
(|||) :: Parser a b -> Parser a b -> Parser a b
(p ||| q) s = p s ++ q s
lit :: (Eq a) => a -> Parser a a
lit x (c:cs) = [(x,cs) | x == c]
lit _ _ = []
(***) :: Parser a b -> (b -> c) -> Parser a c
(p *** f) s = [(f x,r) | (x,r) <- p s]
succeed :: b -> Parser a b
succeed v s = [(v,s)]
fails :: Parser a b
fails s = []
parseResults :: Parser a b -> [a] -> [b]
parseResults p s = [x | (x,r) <- p s, null r]
replace :: Eq a =>
a
-> a
-> [a] -> [a]
replace x y xs = [ if z == x then y else z | z <- xs]
split :: Eq a => a -> [a] -> ([a],[a])
split x xs = let (ys, zs) = break (==x) xs
in (ys, drop 1 zs)
splitAll :: Eq a => a -> [a] -> [[a]]
splitAll _ [] = [[]]
splitAll x xs = let (ys, zs) = break (==x) xs
in ys : case zs of
[] -> []
_:zs' -> splitAll x zs'
pathSep :: Char
pathSep = '/'
pathInits :: String -> [String]
pathInits "" = []
pathInits xs = let (ys,zs) = split pathSep xs
in ys : map ((ys ++ [pathSep]) ++) (pathInits zs)
basename :: String -> String
basename = last . splitAll pathSep