At the moment there are two widely used major versions of Parsec: Parsec 2 and
Parsec 3.
My advice is simply to use the latest release of Parsec 3. But if you want to
make a conscientious choice, read on.
New in Parsec 3
Monad Transformer
Parsec 3 introduces a monad transformer, ParsecT
, which can be used to combine
parsing with other monadic effects.
Streams
Although Parsec 2 lets you to choose the token type (which is useful when you
want to separate lexical analysis from the parsing), the tokens are always
arranged into lists. List may be not the most efficient data structure in which to store
large texts.
Parsec 3 can work with arbitrary streams -- data structures with a list-like
interface. You can define your own streams, but Parsec 3 also includes a popular
and efficient Stream implementation based on ByteString (for Char
-based
parsing), exposed through the modules Text.Parsec.ByteString
and
Text.Parsec.ByteString.Lazy
.
Reasons to prefer Parsec 2
Fewer extensions required
Advanced features provided by Parsec 3 do not come for free; to implement them
several language extensions are required.
Neither of the two versions is Haskell-2010 (i.e. both use extensions), but
Parsec 2 uses fewer extensions than Parsec 3, so chances that any given compiler
can compile Parsec 2 are higher than those for Parsec 3.
By this time both versions work with GHC, while Parsec 2 is also reported to
build with JHC and is included as one of the JHC's standard libraries.
Performance
Originally (i.e. as of 3.0 version) Parsec 3 was considerably slower than
Parsec 2. However, work on improving Parsec 3 performance has been done,
and as of version 3.1 Parsec 3 is only slightly slower than Parsec 2
(benchmarks: 1, 2).
Compatibility layer
It has been possible to "reimplement" all of the Parsec 2 API in Parsec 3. This
compatibility layer is provided by the Parsec 3 package under the module hierarchy
Text.ParserCombinators.Parsec
(the same hierarchy which is used by Parsec 2),
while the new Parsec 3 API is available under the Text.Parsec
hierarchy.
This means that you can use Parsec 3 as a drop-in replacement for Parsec 2.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…