module Text.Parsec.Indent (
IndentParserT, runIndent,
withBlock, withBlock', block,
indented, same, sameOrIndented, checkIndent, withPos,
indentBrackets, indentAngles, indentBraces, indentParens,
(<+/>), (<-/>), (<*/>)
) where
import Text.Parsec
import Text.Parsec.Pos
import Text.Parsec.Token
import Control.Monad.State
import Control.Concatenative
import Debug.Trace
type IndentParserT s u m a = ParsecT s u (StateT SourcePos m) a
withBlock :: (Stream s (StateT SourcePos m) Char, Monad m) => (a -> [b] -> c) ->
IndentParserT s u m a -> IndentParserT s u m x -> IndentParserT s u m b -> IndentParserT s u m c
withBlock f a b p = withPos $ do
r1 <- a
r2 <- option [] (b >> indented >> block p)
return (f r1 r2)
withBlock' :: (Stream s (StateT SourcePos m) Char, Monad m) =>
IndentParserT s u m a -> IndentParserT s u m x -> IndentParserT s u m b -> IndentParserT s u m [b]
withBlock' = withBlock (flip const)
indented :: (Stream s (StateT SourcePos m) Char, Monad m) => IndentParserT s u m ()
indented = do
pos <- getPosition
s <- get
if biAp sourceColumn (<=) pos s then parserFail "not indented" else do
put $ setSourceLine s (sourceLine pos)
return ()
sameOrIndented :: (Stream s (StateT SourcePos m) Char, Monad m) => IndentParserT s u m ()
sameOrIndented = same <|> indented
same :: (Stream s (StateT SourcePos m) Char, Monad m) => IndentParserT s u m ()
same = do
pos <- getPosition
s <- get
if biAp sourceLine (==) pos s then return () else parserFail "over one line"
block :: (Stream s (StateT SourcePos m) Char, Monad m) => IndentParserT s u m a -> IndentParserT s u m [a]
block p = withPos $ do
r <- many1 (checkIndent >> p)
return r
withPos :: (Stream s (StateT SourcePos m) Char, Monad m) => IndentParserT s u m a -> IndentParserT s u m a
withPos x = do
a <- get
p <- getPosition
r <- put p >> x
put a >> return r
checkIndent :: (Stream s (StateT SourcePos m) Char, Monad m) => IndentParserT s u m ()
checkIndent = do
s <- get
p <- getPosition
if biAp sourceColumn (==) p s then return () else parserFail "indentation doesn't match"
runIndent :: Monad m => SourceName -> StateT SourcePos m a -> m a
runIndent s = flip evalStateT (initialPos s)
(<+/>) :: (Stream s (StateT SourcePos m) Char, Monad m) =>
IndentParserT s u m (a -> b) -> IndentParserT s u m a -> IndentParserT s u m b
a <+/> b = ap a (sameOrIndented >> b)
(<-/>) :: (Stream s (StateT SourcePos m) Char, Monad m) =>
IndentParserT s u m a -> IndentParserT s u m b -> IndentParserT s u m a
a <-/> b = liftM2 const a (sameOrIndented >> b)
(<*/>) :: (Stream s (StateT SourcePos m) Char, Monad m) =>
IndentParserT s u m ([a] -> b) -> IndentParserT s u m a -> IndentParserT s u m b
a <*/> b = ap a (many (sameOrIndented >> b))
indentBrackets :: (Stream s (StateT SourcePos m) Char, Monad m) => GenTokenParser s u (StateT SourcePos m) -> IndentParserT s u m a -> IndentParserT s u m a
indentBrackets lexer p = withPos $ return id <-/> symbol lexer "[" <+/> p <-/> symbol lexer "]"
indentAngles :: (Stream s (StateT SourcePos m) Char, Monad m) => GenTokenParser s u (StateT SourcePos m) -> IndentParserT s u m a -> IndentParserT s u m a
indentAngles lexer p = withPos $ return id <-/> symbol lexer "<" <+/> p <-/> symbol lexer ">"
indentBraces :: (Stream s (StateT SourcePos m) Char, Monad m) => GenTokenParser s u (StateT SourcePos m) -> IndentParserT s u m a -> IndentParserT s u m a
indentBraces lexer p = withPos $ return id <-/> symbol lexer "{" <+/> p <-/> symbol lexer "}"
indentParens :: (Stream s (StateT SourcePos m) Char, Monad m) => GenTokenParser s u (StateT SourcePos m) -> IndentParserT s u m a -> IndentParserT s u m a
indentParens lexer p = withPos $ return id <-/> symbol lexer "(" <+/> p <-/> symbol lexer ")"