@@ -1,5 +1,6 @@
1module Parse.Helpers where
1module Parse.Helpers where
3+import Prelude hiding (until)
3import Control.Applicative ((<$>),(<*>))
4import Control.Applicative ((<$>),(<*>))
4import Control.Monad
5import Control.Monad
5import Control.Monad.State
6import Control.Monad.State
@@ -237,17 +238,36 @@ closeComment =
237 , concat <$> sequence [ try (string "{-"), closeComment, closeComment ]
238 , concat <$> sequence [ try (string "{-"), closeComment, closeComment ]
241+until :: IParser a -> IParser b -> IParser b
244+ go = end <|> (p >> go)
240anyUntil :: IParser String -> IParser String
246anyUntil :: IParser String -> IParser String
241anyUntil end = go
247anyUntil end = go
243 go = end <|> (:) <$> anyChar <*> go
249 go = end <|> (:) <$> anyChar <*> go
245−anyThen :: IParser a -> IParser a
251+ignoreUntil :: IParser a -> IParser (Maybe a)
246−anyThen end = go
252+ignoreUntil end = go
248 ignore p = const () <$> p
254 ignore p = const () <$> p
249− go = end <|> do ignore multiComment <|> ignore anyChar
255+ filler = choice [ ignore multiComment
250− go
256+ , ignore (markdown (\_ _ -> mzero))
259+ go = choice [ Just <$> end
260+ , filler `until` choice [ const Nothing <$> eof, newline >> go ]
263+onFreshLines :: (a -> b -> b) -> b -> IParser a -> IParser b
264+onFreshLines insert init thing = go init
267+ optionValue <- ignoreUntil thing
269+ Nothing -> return values
270+ Just v -> go (insert v values)
252withSource :: IParser a -> IParser (String, a)
272withSource :: IParser a -> IParser (String, a)
253withSource p = do
273withSource p = do
@@ -266,3 +286,13 @@ anyUntilPos pos = go
266 True -> return []
286 True -> return []
267 False -> (:) <$> anyChar <*> go
287 False -> (:) <$> anyChar <*> go
289+markdown :: (String -> [a] -> IParser (String, [a])) -> IParser (String, [a])
290+markdown interpolation = try (string "[markdown|") >> closeMarkdown "" []
292+ closeMarkdown md stuff =
293+ choice [ do try (string "|]")
295+ , uncurry closeMarkdown =<< interpolation md stuff
297+ closeMarkdown (md ++ [c]) stuff