refactor: move parser functions into Utils
This commit is contained in:
parent
41de325442
commit
939f596c34
|
@ -4,13 +4,10 @@ import Data.Either
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
import Data.String
|
import Data.String
|
||||||
import Data.String.Parser
|
import Data.String.Parser
|
||||||
|
import Utils
|
||||||
|
|
||||||
--- PARSING
|
--- PARSING
|
||||||
|
|
||||||
export
|
|
||||||
scan : Monad m => ParseT m a -> ParseT m a
|
|
||||||
scan p = many (requireFailure p *> skip (satisfy (const True))) *> p
|
|
||||||
|
|
||||||
export
|
export
|
||||||
mul : Parser Integer
|
mul : Parser Integer
|
||||||
mul = do
|
mul = do
|
||||||
|
|
|
@ -2,9 +2,29 @@ module Utils
|
||||||
|
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
import Data.String
|
import Data.String
|
||||||
|
import Data.String.Parser
|
||||||
|
|
||||||
%default total
|
%default total
|
||||||
|
|
||||||
export
|
export
|
||||||
parseNat : String -> Maybe Nat
|
parseNat : String -> Maybe Nat
|
||||||
parseNat = map cast . filter (> 0) . parseInteger
|
parseNat = map cast . filter (> 0) . parseInteger
|
||||||
|
|
||||||
|
|
||||||
|
export
|
||||||
|
lookahead : Functor m => ParseT m a -> ParseT m a
|
||||||
|
lookahead p = P $ \s => map (\case
|
||||||
|
OK r _ => OK r s
|
||||||
|
Fail i err => Fail i err)
|
||||||
|
(p.runParser s)
|
||||||
|
export covering
|
||||||
|
until : Monad m => ParseT m a -> ParseT m ()
|
||||||
|
until p = skip $ many (requireFailure p *> skip (satisfy (const True)))
|
||||||
|
|
||||||
|
export covering
|
||||||
|
scan : Monad m => ParseT m a -> ParseT m a
|
||||||
|
scan p = until p *> p
|
||||||
|
|
||||||
|
export covering
|
||||||
|
scanWhile : Monad m => ParseT m a -> ParseT m b -> ParseT m b
|
||||||
|
scanWhile w p = many (lookahead w *> requireFailure p *> skip (satisfy (const True))) *> p
|
||||||
|
|
Loading…
Reference in a new issue