hasktorch-gradually-typed-0.2.0.0: experimental project for hasktorch
Safe HaskellSafe-Inferred
LanguageHaskell2010

Torch.Data.Parser

Synopsis

Documentation

type Parser (b :: Type -> Type) (i :: Type) (a :: Type) = FreeT ((->) i) b a Source #

Parser b i a is a parser that consumes a stream of i tokens and as a result yields a value of type a, while operating under the b non-determinism monad.

For most purposes, the non-determinism monad b should be a MonadPlus. Useful examples include [] or Logic if you want backtracking, and Maybe if you want no backtracking.

Use StateT s [] if you want to maintain a state s that is automatically reverted when backtracking via [].

hoistFreeT can be used to change the backtracking monad.

FreeT provides instances for Functor, Applicative, Monad, Alternative and MonadPlus.

recurse :: forall t b i a. (Parser b i a -> (Parser b i a -> t b a) -> t b a) -> Parser b i a -> t b a Source #

Recurse over a parser.

Tears down the free monad transformer over the '(->) i' functor using iteration: recurse next parser = next parser (parser' -> next parser' (parser'' -> next parser'' (parser''' -> next parser''' (...))))

parseStream :: forall s b i a. Monad b => (s -> b (i, s)) -> Parser (StateT s b) i a -> s -> b (a, s) Source #

parseString :: forall b i a. MonadPlus b => Parser (StateT [i] b) i a -> [i] -> b (a, [i]) Source #

token :: forall b i. Monad b => Parser b i i Source #

token is trivial parser that consumes a single token i and yields it.

Other parsers can be derived from this one using methods of the Functor, Applicative, Monad, Alternative, and MonadPlus typeclasses and the parser combinators in this module.

eof :: Alternative b => Parser (StateT [i] b) i () Source #

notFollowedBy :: forall b i a. (Alternative b, Foldable b, MonadPlus b) => Parser (StateT [i] b) i a -> Parser (StateT [i] b) i () Source #

satisfy :: forall b i. MonadPlus b => (i -> Bool) -> Parser b i i Source #

satisfy p is a simple parser that consumes a single token i and yields it if and only if p i evaluates to True. Otherwise, the parser fails.

isToken :: forall b i. (MonadPlus b, Eq i) => i -> Parser b i i Source #

is i is a simple parser that consumes a single token and yields it if and only if it is equal to i. Otherwise, the parser fails.

isNotToken :: forall b i. (MonadPlus b, Eq i) => i -> Parser b i i Source #

isNot i is a simple parser that consumes a single token and yields it if and only if it is not equal to i. If the token is equal to i, the parser fails.

scan :: (Alternative m, Monad m) => (s -> a -> Maybe s) -> s -> m a -> m [a] Source #

Stateful scanner.

>>> :{
  f s a | "ell" `isInfixOf` (s ++ [a]) = Nothing
        | otherwise                    = Just (s ++ [a])
:}
>>> head $ parseString @[] (scan f "" token) "hello 123"
("hel","lo 123")

atMost :: (Alternative m, Monad m) => Int -> m a -> m [a] Source #

atMost n p applies the parser p at most n times and returns every parsing result. If parsing of p succeeds less the n times, repeatP n p succeeds as well.

>>> head $ parseString @[] (atMost 2 (isToken 'a')) "aaaaaab"
("aa","aaaab")

eitherP :: Alternative f => f a -> f b -> f (Either a b) Source #

eitherP p p' combines the two alternatives p and p'.

void :: Functor f => f a -> f () Source #

void p applies the parser p and discards its result.

combine :: (Applicative f, Semigroup a) => f a -> f a -> f a Source #

combine p p' merges the results of p and p' using the Semigroup instance.

combines :: (Applicative f, Monoid a) => [f a] -> f a Source #

combines ps merges the results of the parsers ps using the Monoid instance.

isString :: (Traversable t, MonadPlus b, Eq i) => t i -> Parser b i (t i) Source #

isString s is a simple parser that consumes Char tokens and yields them if and only if they assemble the String s. Otherwise, the parser fails.

string :: MonadPlus b => Parser b i [i] Source #

string matches any string

>>> parseString @[] string "a string"
[("a string",""),("a strin","g"),("a stri","ng"),("a str","ing"),("a st","ring"),("a s","tring"),("a ","string"),("a"," string"),("","a string")]
  • - >>> p = string @[] >>= s -> (guard ("dog" isInfixOf s) >> pure s)
  • - >>> head $ parseString p "this is a string with a dog"
  • - ("this is a string with a dog","")
  • - >>> p = string @[] >>= s -> (guard (not $ "dog" isInfixOf s) >> pure s)
  • - >>> head $ parseString p "this is also string with a dog"
  • - ("this is also string with a do","g")

intP :: (CharParsing m, Monad m) => m Int Source #

Orphan instances

(Applicative f, MonadLogic b, MonadPlus b) => MonadLogic (FreeT f b) Source # 
Instance details

Methods

msplit :: FreeT f b a -> FreeT f b (Maybe (a, FreeT f b a)) Source #

interleave :: FreeT f b a -> FreeT f b a -> FreeT f b a Source #

(>>-) :: FreeT f b a -> (a -> FreeT f b b0) -> FreeT f b b0 Source #

once :: FreeT f b a -> FreeT f b a Source #

lnot :: FreeT f b a -> FreeT f b () Source #

ifte :: FreeT f b a -> (a -> FreeT f b b0) -> FreeT f b b0 -> FreeT f b b0 Source #

(Alternative b, Foldable b, MonadPlus b) => CharParsing (FreeT ((->) Char) (StateT [Char] b)) Source # 
Instance details

Methods

satisfy :: (Char -> Bool) -> FreeT ((->) Char) (StateT [Char] b) Char Source #

char :: Char -> FreeT ((->) Char) (StateT [Char] b) Char Source #

notChar :: Char -> FreeT ((->) Char) (StateT [Char] b) Char Source #

anyChar :: FreeT ((->) Char) (StateT [Char] b) Char Source #

string :: String -> FreeT ((->) Char) (StateT [Char] b) String Source #

text :: Text -> FreeT ((->) Char) (StateT [Char] b) Text Source #

(Alternative b, Foldable b, MonadPlus b) => Parsing (FreeT ((->) i) (StateT [i] b)) Source # 
Instance details

Methods

try :: FreeT ((->) i) (StateT [i] b) a -> FreeT ((->) i) (StateT [i] b) a Source #

(<?>) :: FreeT ((->) i) (StateT [i] b) a -> String -> FreeT ((->) i) (StateT [i] b) a Source #

skipMany :: FreeT ((->) i) (StateT [i] b) a -> FreeT ((->) i) (StateT [i] b) () Source #

skipSome :: FreeT ((->) i) (StateT [i] b) a -> FreeT ((->) i) (StateT [i] b) () Source #

unexpected :: String -> FreeT ((->) i) (StateT [i] b) a Source #

eof :: FreeT ((->) i) (StateT [i] b) () Source #

notFollowedBy :: Show a => FreeT ((->) i) (StateT [i] b) a -> FreeT ((->) i) (StateT [i] b) () Source #

(Alternative b, Foldable b, MonadPlus b) => TokenParsing (FreeT ((->) Char) (StateT [Char] b)) Source # 
Instance details

Methods

someSpace :: FreeT ((->) Char) (StateT [Char] b) () Source #

nesting :: FreeT ((->) Char) (StateT [Char] b) a -> FreeT ((->) Char) (StateT [Char] b) a Source #

semi :: FreeT ((->) Char) (StateT [Char] b) Char Source #

highlight :: Highlight -> FreeT ((->) Char) (StateT [Char] b) a -> FreeT ((->) Char) (StateT [Char] b) a Source #

token :: FreeT ((->) Char) (StateT [Char] b) a -> FreeT ((->) Char) (StateT [Char] b) a Source #