on master help sign in
Prettify and comment Indent.
commit22894b4182bc5ec4278c941f78b40893c8c0b69d authorPaweł Nowak <pawel834@gmail.com> dateSat 29 Nov 2014, 17:29 (12 years ago) parentac95cdf
1 file changed+26−7
Data/Syntax/Indent.hs+26−7
Data/Syntax/Indent.hs+26−7
@@ -1,17 +1,31 @@
11{-# LANGUAGE FlexibleInstances #-}
22{-# LANGUAGE UndecidableInstances #-}
33{-# LANGUAGE MultiParamTypeClasses #-}
4−module Data.Syntax.Indent where
4+{- |
5+Module : Data.Syntax.Indent
6+Description : Indentation.
7+Copyright : (c) Paweł Nowak
8+License : MIT
9+
10+Maintainer : Paweł Nowak <pawel834@gmail.com>
11+Stability : experimental
12+
13+Provides a very simple indentation as a \"monad\" transformer.
14+-}
15+module Data.Syntax.Indent (
16+ Indent,
17+ runIndent,
18+ breakLine,
19+ indented
20+ ) where
521
6−import Control.Lens.SemiIso
7−import Data.Char (isSpace)
822import Data.SemiIsoFunctor
9−import Data.Sequences (fromList)
1023import Data.Syntax
1124import Data.Syntax.Char
1225import Data.Syntax.Combinator
1326import Prelude hiding (takeWhile, take)
1427
28+-- | Adds indentation to a syntax description.
1529newtype Indent m a = Indent { unIndent :: (Int, m ()) -> m a }
1630
1731instance SemiIsoFunctor m => SemiIsoFunctor (Indent m) where
@@ -48,11 +62,16 @@ instance SyntaxChar syn seq => SyntaxChar (Indent syn) seq where
4862 decimal = Indent $ const decimal
4963 scientific = Indent $ const scientific
5064
65+-- | @runIndent m tab@ runs the 'Indent' transformer using @tab@ once for each
66+-- level of indentation.
67+runIndent :: Indent m a -> m () -> m a
68+runIndent = ($ 0) . curry . unIndent
69+
70+-- | Inserts a new line and correct indentation, but does not
71+-- require any formatting when parsing (it just skips all white space).
5172breakLine :: SyntaxChar syn seq => Indent syn ()
5273breakLine = Indent $ \(i, tab) -> opt (char '\n') /* opt (sireplicate_ i tab) /* spaces_
5374
75+-- | Increases the indentation level of its argument by one.
5476indented :: Indent m a -> Indent m a
5577indented (Indent f) = Indent $ \(i, tab) -> f (i + 1, tab)
56−
57−runIndent :: Indent m a -> m () -> m a
58−runIndent = ($ 0) . curry . unIndent