X-Git-Url: http://git.cielonegro.org/gitweb.cgi?p=Lucu.git;a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FParser.hs;h=660f550a9353cc687a56ae0e6e422ecc41a7e3f1;hp=ac1bf02734f0a09485eddf924002f8d4fc27b88e;hb=19043d7882f936be9b073cae34b52905016c3ad7;hpb=48bc90d66a45c0b9b6f52272b46cf2949ed802e3 diff --git a/Network/HTTP/Lucu/Parser.hs b/Network/HTTP/Lucu/Parser.hs index ac1bf02..660f550 100644 --- a/Network/HTTP/Lucu/Parser.hs +++ b/Network/HTTP/Lucu/Parser.hs @@ -6,18 +6,22 @@ module Network.HTTP.Lucu.Parser ( atMost , finishOff + , skipManyTill + , skipWhile1 + , skipSpace1 + , isAlphaNum ) where import Control.Applicative import Control.Applicative.Unicode import Control.Monad.Unicode -import Data.Attoparsec +import Data.Attoparsec.Char8 import Prelude.Unicode -- |@'atMost' n v@ is like @'P.many' v@ but accumulates @v@ at most -- @n@ times. atMost ∷ Alternative f ⇒ Int → f α → f [α] -{-# INLINE atMost #-} +{-# INLINEABLE atMost #-} atMost 0 _ = pure [] atMost n v = ( (:) <$> v ⊛ atMost (n-1) v ) <|> @@ -28,3 +32,26 @@ atMost n v = ( (:) <$> v ⊛ atMost (n-1) v ) finishOff ∷ Parser α → Parser α {-# INLINE finishOff #-} finishOff = ((endOfInput *>) ∘ return =≪) + +-- |Similar to 'manyTill' but discards the result. +skipManyTill ∷ Alternative f ⇒ f α → f β → f () +{-# INLINEABLE skipManyTill #-} +skipManyTill p end = go + where + go = (end *> pure ()) <|> (p *> go) + +-- |Similar to 'skipWhile' but consumes at least one character. +skipWhile1 ∷ (Char → Bool) → Parser () +{-# INLINE skipWhile1 #-} +skipWhile1 p = takeWhile1 p *> pure () + +-- |Similar to 'skipSpace' but consumes at least one whitespace. +skipSpace1 ∷ Parser () +{-# INLINE skipSpace1 #-} +skipSpace1 = skipMany1 space + +-- |@'isAlphaNum' c@ returns 'True' iff @'isDigit' c || +-- 'isAlpha_ascii' c@. +isAlphaNum ∷ Char → Bool +{-# INLINE isAlphaNum #-} +isAlphaNum c = isDigit c ∨ isAlpha_ascii c