29
29
-- | a sequence of lines need to be /folded/ to a single line. An example
30
30
-- | is MIME headers. Line folding based binding separation is used in
31
31
-- | Haskell as well.
32
- module Text. Parsing.Indent
32
+ module Parsing.Indent
33
33
( IndentParser
34
34
, runIndent
35
35
, withBlock
@@ -66,20 +66,15 @@ import Control.Monad.State.Trans (get, put)
66
66
import Control.Monad.Trans.Class (lift )
67
67
import Data.List (List (..), many )
68
68
import Data.Maybe (Maybe (..))
69
- import Text. Parsing.Parser (ParseState (ParseState), ParserT , fail )
70
- import Text. Parsing.Parser .Combinators (option , optionMaybe )
71
- import Text. Parsing.Parser .Pos (Position (..), initialPos )
72
- import Text. Parsing.Parser .String (oneOf , string )
69
+ import Parsing (ParseState (ParseState), ParserT , fail , position )
70
+ import Parsing.Combinators (option , optionMaybe )
71
+ import Parsing.Pos (Position (..), initialPos )
72
+ import Parsing.String (oneOf , string )
73
73
74
74
-- | Indentation sensitive parser type. Usually @ m @ will
75
75
-- | be @ Identity @ as with any @ ParserT @
76
76
type IndentParser s a = ParserT s (State Position ) a
77
77
78
- -- | @ getPosition @ returns current position
79
- -- | should probably be added to Text.Parsing.Parser.Pos
80
- getPosition :: forall m s . ParserT s m Position
81
- getPosition = gets \(ParseState _ pos _) -> pos
82
-
83
78
-- | simple helper function to avoid typ-problems with MonadState instance
84
79
get' :: forall s . IndentParser s Position
85
80
get' = do
@@ -102,7 +97,6 @@ setSourceLine (Position { line: _, column: c }) l = Position { line: l, column:
102
97
biAp :: forall a b c . (a -> b ) -> (b -> b -> c ) -> a -> a -> c
103
98
biAp f c v1 v2 = c (f v1) (f v2)
104
99
105
- -- | @ many1 @ should prabably be inside Text.Parsing.Parser.Combinators
106
100
many1 :: forall s m a . ParserT s m a -> ParserT s m (List a )
107
101
many1 p = lift2 Cons p (many p)
108
102
@@ -127,7 +121,7 @@ withBlock' = withBlock (flip const)
127
121
-- | Parses only when indented past the level of the reference
128
122
indented :: forall s . IndentParser s Unit
129
123
indented = do
130
- pos <- getPosition
124
+ pos <- position
131
125
s <- get'
132
126
if biAp sourceColumn (<=) pos s then fail " not indented"
133
127
else do
@@ -137,7 +131,7 @@ indented = do
137
131
-- | Same as `indented`, but does not change internal state
138
132
indented' :: forall s . IndentParser s Unit
139
133
indented' = do
140
- pos <- getPosition
134
+ pos <- position
141
135
s <- get'
142
136
if biAp sourceColumn (<=) pos s then fail " not indented" else pure unit
143
137
@@ -148,7 +142,7 @@ sameOrIndented = sameLine <|> indented
148
142
-- | Parses only on the same line as the reference
149
143
sameLine :: forall s . IndentParser s Unit
150
144
sameLine = do
151
- pos <- getPosition
145
+ pos <- position
152
146
s <- get'
153
147
if biAp sourceLine (==) pos s then pure unit else fail " over one line"
154
148
@@ -168,15 +162,15 @@ block p = withPos $ do
168
162
withPos :: forall s a . IndentParser s a -> IndentParser s a
169
163
withPos x = do
170
164
a <- get'
171
- p <- getPosition
165
+ p <- position
172
166
r <- put' p *> x
173
167
put' a *> pure r
174
168
175
169
-- | Ensures the current indentation level matches that of the reference
176
170
checkIndent :: forall s . IndentParser s Unit
177
171
checkIndent = do
178
172
s <- get'
179
- p <- getPosition
173
+ p <- position
180
174
if biAp sourceColumn (==) p s then pure unit else fail " indentation doesn't match"
181
175
182
176
-- | Run the result of an indentation sensitive parse
0 commit comments