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
SourcePos
pos <- ParsecT s u m SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
Indentation -> ParsecT s u m Indentation
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Indentation -> ParsecT s u m Indentation)
-> Indentation -> ParsecT s u m Indentation
forall a b. (a -> b) -> a -> b
$! Indentation {iLine :: Int
iLine = SourcePos -> Int
sourceLine SourcePos
pos, iColumn :: Int
iColumn = SourcePos -> Int
sourceColumn SourcePos
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
Indentation
pos <- ParsecT s u m Indentation
forall (m :: * -> *) s u. Monad m => ParsecT s u m Indentation
indentation
Bool -> ParsecT s u m () -> ParsecT s u m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Indentation -> Int
iColumn Indentation
pos Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Indentation -> Int
iColumn Indentation
ref) (ParsecT s u m () -> ParsecT s u m ())
-> ParsecT s u m () -> ParsecT s u m ()
forall a b. (a -> b) -> a -> b
$ String -> ParsecT s u m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
String -> ParsecT s u m a
unexpected (Indentation -> String
prettyIndentation Indentation
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
Indentation
pos <- ParsecT s u m Indentation
forall (m :: * -> *) s u. Monad m => ParsecT s u m Indentation
indentation
Bool -> ParsecT s u m () -> ParsecT s u m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Indentation -> Int
iColumn Indentation
pos Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Indentation -> Int
iColumn Indentation
ref Bool -> Bool -> Bool
&& Indentation -> Int
iLine Indentation
pos Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Indentation -> Int
iLine Indentation
ref) (ParsecT s u m () -> ParsecT s u m ())
-> ParsecT s u m () -> ParsecT s u m ()
forall a b. (a -> b) -> a -> b
$
String -> ParsecT s u m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
String -> ParsecT s u m a
unexpected (Indentation -> String
prettyIndentation Indentation
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
Indentation
pos <- ParsecT s u m Indentation
forall (m :: * -> *) s u. Monad m => ParsecT s u m Indentation
indentation
Bool -> ParsecT s u m () -> ParsecT s u m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Indentation -> Int
iLine Indentation
pos Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Indentation -> Int
iLine Indentation
ref) (ParsecT s u m () -> ParsecT s u m ())
-> ParsecT s u m () -> ParsecT s u m ()
forall a b. (a -> b) -> a -> b
$ String -> ParsecT s u m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
String -> ParsecT s u m a
unexpected String
"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
Indentation
ref <- ParsecT s u m Indentation
forall (m :: * -> *) s u. Monad m => ParsecT s u m Indentation
indentation
ParsecT s u m a -> ParsecT s u m [a]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (Indentation -> ParsecT s u m ()
forall (m :: * -> *) s z u.
(Monad m, Stream s m z) =>
Indentation -> ParsecT s u m ()
checkIndent Indentation
ref ParsecT s u m () -> ParsecT s u m a -> ParsecT s u m a
forall a b. ParsecT s u m a -> ParsecT s u m b -> ParsecT s u m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT s u m a
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
Indentation
pos <- ParsecT s u m Indentation
forall (m :: * -> *) s u. Monad m => ParsecT s u m Indentation
indentation
Bool -> ParsecT s u m () -> ParsecT s u m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Indentation -> Int
iColumn Indentation
pos Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Indentation -> Int
iColumn Indentation
ref) (ParsecT s u m () -> ParsecT s u m ())
-> ParsecT s u m () -> ParsecT s u m ()
forall a b. (a -> b) -> a -> b
$
(ParsecT s u m () -> String -> ParsecT s u m ()
forall s u (m :: * -> *) a.
ParsecT s u m a -> String -> ParsecT s u m a
<?> Indentation -> String
prettyIndentation Indentation
ref String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" (started at line " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Indentation -> String
prettyLine Indentation
ref String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")")
(String -> ParsecT s u m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
String -> ParsecT s u m a
unexpected (String -> ParsecT s u m ()) -> String -> ParsecT s u m ()
forall a b. (a -> b) -> a -> b
$ Indentation -> String
prettyIndentation Indentation
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
Indentation
pos <- ParsecT s u m Indentation
forall (m :: * -> *) s u. Monad m => ParsecT s u m Indentation
indentation
Bool -> ParsecT s u m () -> ParsecT s u m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Indentation -> Int
iColumn Indentation
pos Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1) (ParsecT s u m () -> ParsecT s u m ())
-> ParsecT s u m () -> ParsecT s u m ()
forall a b. (a -> b) -> a -> b
$ String -> ParsecT s u m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
String -> ParsecT s u m a
unexpected String
"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
Indentation
pos <- ParsecT s u m Indentation
forall (m :: * -> *) s u. Monad m => ParsecT s u m Indentation
indentation
Bool -> ParsecT s u m () -> ParsecT s u m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Indentation -> Int
iColumn Indentation
pos Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1) (ParsecT s u m () -> ParsecT s u m ())
-> ParsecT s u m () -> ParsecT s u m ()
forall a b. (a -> b) -> a -> b
$ String -> ParsecT s u m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
String -> ParsecT s u m a
unexpected String
"top-level"