module Text.Parsec.Indent.Explicit
(
Indentation
, indentation
, indented
, sameOrIndented
, same
, block
, checkIndent
, topLevel
, notTopLevel
) where
import Control.Monad (unless, when)
import Text.Parsec
import Text.Parsec.Indent.Internal
indentation :: Monad m => ParsecT s u m Indentation
indentation :: forall (m :: * -> *) s u. Monad m => ParsecT s u m Indentation
indentation = do
pos <- ParsecT s u m SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
return $! Indentation {iLine = sourceLine pos, iColumn = sourceColumn pos}
indented
:: (Monad m, Stream s m z)
=> Indentation
-> ParsecT s u m ()
indented :: forall (m :: * -> *) s z u.
(Monad m, Stream s m z) =>
Indentation -> ParsecT s u m ()
indented Indentation
ref = do
pos <- ParsecT s u m Indentation
forall (m :: * -> *) s u. Monad m => ParsecT s u m Indentation
indentation
when (iColumn pos <= iColumn ref) $ unexpected (prettyIndentation pos)
sameOrIndented
:: (Monad m, Stream s m z)
=> Indentation
-> ParsecT s u m ()
sameOrIndented :: forall (m :: * -> *) s z u.
(Monad m, Stream s m z) =>
Indentation -> ParsecT s u m ()
sameOrIndented Indentation
ref = do
pos <- ParsecT s u m Indentation
forall (m :: * -> *) s u. Monad m => ParsecT s u m Indentation
indentation
when (iColumn pos <= iColumn ref && iLine pos /= iLine ref) $
unexpected (prettyIndentation pos)
same
:: (Monad m, Stream s m z)
=> Indentation
-> ParsecT s u m ()
same :: forall (m :: * -> *) s z u.
(Monad m, Stream s m z) =>
Indentation -> ParsecT s u m ()
same Indentation
ref = do
pos <- ParsecT s u m Indentation
forall (m :: * -> *) s u. Monad m => ParsecT s u m Indentation
indentation
when (iLine pos /= iLine ref) $ unexpected "line break"
block
:: (Monad m, Stream s m z)
=> ParsecT s u m a
-> ParsecT s u m [a]
block :: forall (m :: * -> *) s z u a.
(Monad m, Stream s m z) =>
ParsecT s u m a -> ParsecT s u m [a]
block ParsecT s u m a
p = do
ref <- ParsecT s u m Indentation
forall (m :: * -> *) s u. Monad m => ParsecT s u m Indentation
indentation
many1 (checkIndent ref >> p)
checkIndent
:: (Monad m, Stream s m z)
=> Indentation
-> ParsecT s u m ()
checkIndent :: forall (m :: * -> *) s z u.
(Monad m, Stream s m z) =>
Indentation -> ParsecT s u m ()
checkIndent Indentation
ref = do
pos <- ParsecT s u m Indentation
forall (m :: * -> *) s u. Monad m => ParsecT s u m Indentation
indentation
when (iColumn pos /= iColumn ref) $
(<?> prettyIndentation ref ++ " (started at line " ++ prettyLine ref ++ ")")
(unexpected $ prettyIndentation pos)
topLevel
:: (Monad m, Stream s m z)
=> ParsecT s u m ()
topLevel :: forall (m :: * -> *) s z u.
(Monad m, Stream s m z) =>
ParsecT s u m ()
topLevel = do
pos <- ParsecT s u m Indentation
forall (m :: * -> *) s u. Monad m => ParsecT s u m Indentation
indentation
unless (iColumn pos == 1) $ unexpected "indentation"
notTopLevel
:: (Monad m, Stream s m z)
=> ParsecT s u m ()
notTopLevel :: forall (m :: * -> *) s z u.
(Monad m, Stream s m z) =>
ParsecT s u m ()
notTopLevel = do
pos <- ParsecT s u m Indentation
forall (m :: * -> *) s u. Monad m => ParsecT s u m Indentation
indentation
when (iColumn pos == 1) $ unexpected "top-level"