faster-megaparsec-0.1.2.0: Speed up Megaparsec parsing when parsing succeeds
Copyright(c) Lackmann Phymetric
LicenseGPL-3
Maintainerolaf.klinke@phymetric.de
Stabilityexperimental
Safe HaskellSafe-Inferred
LanguageHaskell2010

Text.Megaparsec.Simple

Description

This module defines a MonadParsec instance for (a type isomorphic to) StateT s Maybe where s is a Megaparsec Stream type such as String, Text or ByteString. This parser can be faster than Cassava for csv parsing but at the cost of no error information whatsoever.

If, however, you construct your parser in a generic MonadParsec fashion, then with the help of tryFast you can first attempt to specialize and run the fast parser supplied by this module and only on parse error specialize the parser to ParsecT and parse again, yielding an informative error message. This buys you speed in the smooth case of successful parsing at the cost of double parse when something goes wrong.

Beware that the behaviour of a SimpleParser can differ from its Parsec sibling because

  • SimpleParser is always backtracking since it does not know whether it has consumed tokens,
  • any fancy parsing that relies on inspecting parser state components such as offset will not work as intended.
Synopsis

Documentation

data SimpleParser s a Source #

This parser type is isomorphic to StateT s Maybe but has roughly the same instances as Parsec. Since it maintains no state besides the unconsumed input, it is considerably faster than Parsec but can be built using the same combinators.

Instances

Instances details
Stream s => MonadParsec Void s (SimpleParser s) Source # 
Instance details

Defined in Text.Megaparsec.Simple

MonadFail (SimpleParser s) Source # 
Instance details

Defined in Text.Megaparsec.Simple

Methods

fail :: String -> SimpleParser s a #

Alternative (SimpleParser s) Source # 
Instance details

Defined in Text.Megaparsec.Simple

Methods

empty :: SimpleParser s a #

(<|>) :: SimpleParser s a -> SimpleParser s a -> SimpleParser s a #

some :: SimpleParser s a -> SimpleParser s [a] #

many :: SimpleParser s a -> SimpleParser s [a] #

Applicative (SimpleParser s) Source # 
Instance details

Defined in Text.Megaparsec.Simple

Methods

pure :: a -> SimpleParser s a #

(<*>) :: SimpleParser s (a -> b) -> SimpleParser s a -> SimpleParser s b #

liftA2 :: (a -> b -> c) -> SimpleParser s a -> SimpleParser s b -> SimpleParser s c #

(*>) :: SimpleParser s a -> SimpleParser s b -> SimpleParser s b #

(<*) :: SimpleParser s a -> SimpleParser s b -> SimpleParser s a #

Functor (SimpleParser s) Source # 
Instance details

Defined in Text.Megaparsec.Simple

Methods

fmap :: (a -> b) -> SimpleParser s a -> SimpleParser s b #

(<$) :: a -> SimpleParser s b -> SimpleParser s a #

Monad (SimpleParser s) Source # 
Instance details

Defined in Text.Megaparsec.Simple

Methods

(>>=) :: SimpleParser s a -> (a -> SimpleParser s b) -> SimpleParser s b #

(>>) :: SimpleParser s a -> SimpleParser s b -> SimpleParser s b #

return :: a -> SimpleParser s a #

MonadPlus (SimpleParser s) Source # 
Instance details

Defined in Text.Megaparsec.Simple

Methods

mzero :: SimpleParser s a #

mplus :: SimpleParser s a -> SimpleParser s a -> SimpleParser s a #

(a ~ Tokens s, IsString a, Eq a, Stream s) => IsString (SimpleParser s a) Source # 
Instance details

Defined in Text.Megaparsec.Simple

Methods

fromString :: String -> SimpleParser s a #

Monoid a => Monoid (SimpleParser s a) Source # 
Instance details

Defined in Text.Megaparsec.Simple

Semigroup a => Semigroup (SimpleParser s a) Source # 
Instance details

Defined in Text.Megaparsec.Simple

Methods

(<>) :: SimpleParser s a -> SimpleParser s a -> SimpleParser s a #

sconcat :: NonEmpty (SimpleParser s a) -> SimpleParser s a #

stimes :: Integral b => b -> SimpleParser s a -> SimpleParser s a #

tryFast Source #

Arguments

:: forall s a result. (Applicative result, Stream s) 
=> (Parsec Void s a -> String -> s -> result a)

function to run if fast parsing fails

-> (forall p. MonadParsec Void s p => p a)

a generic parser

-> String

input stream name

-> s

input stream

-> result a 

The result type of Megaparsec has changed through the library versions (commonly result = Either err for some err) whence we abstract over it. Instead of

runParser p

you should use

tryFast runParser p

which tries the fast parser and falls back to Parsec in case of a parse failure.

Conversion from/to StateT

toSimpleParser :: StateT s Maybe a -> SimpleParser s a Source #

Use this to implement more parser combinators.

runSimpleParser :: SimpleParser s a -> s -> Maybe (a, s) Source #

Run the SimpleParser on the given input. Consider using tryFast instead if possible.