refactor: move parser functions into Utils

This commit is contained in:
Kiana Sheibani 2024-12-04 07:16:34 -05:00
parent 41de325442
commit 939f596c34
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
2 changed files with 21 additions and 4 deletions

View file

@ -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

View file

@ -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