The faster monadic parser combinator library for C#
Important
ParsecSharp v4 has some major breaking-changes from v3. Please check the following notes.
The main data types like Parser, Result, Success and Failure have been changed to interfaces to enable covariant result types. To follow this change, you can simply rename it, and it should work.
- private static readonly Parser<char, int> integer = Many1(DecDigit()).ToInt();
+ private static readonly IParser<char, int> integer = Many1(DecDigit()).ToInt();
Unfortunately, netstandard2.0 doesn't support binary operator overloading for interfaces. Please use netstandard2.1 compatibles, or rewrite it with other combinators that have the same semantics.
var textChar = unescapedChar | escapedChar; // no longer available in netstandard2.0
var textChar = unescapedChar.Or(escapedChar); // use Or combinator
var textChar = Choice(unescapedChar, escapedChar); // use Choice combinator
This library provides the most useful Text Parsers, Stream Parsers, and Parser Combinators. All APIs are pure, immutable, and can combine with any others. Designed to utilize JIT Compiler optimizations, it realizes complete immutability and can parse infinitely recursive data structures.
This project is inspired by parsec, a monadic parser library for Haskell.
- Easy construction APIs with monads in C#
- Pure/Immutable/Functional framework in C#
- Replace regular expressions in your code (applicable from smallest to largest)
- Most readable API source code
- Strictly typed parsers/combinators that support natural type inference
- A lot of reasonable built-in parsers/combinators
- Supports parsing infinitely recursive data structures
- Supports full backtracking: Parsing Expression Grammar (PEG) style parsing strategy
- Supports parsing streams with any token type (e.g., string, char stream, byte array, binary stream)
- Supports tokenization
- Supports partial parsing
- Supports custom deriviation for core types
- Supports nullable reference types (with C# 8.0 or later)
- Supports Source Link (that allows to refer every parser implementation source codes)
- No additional dependencies
- Faster running
- Just enough error messages
- No left-recursion support
- No packrat parsing support (because it increases parsing time in most cases)
dotnet-cli:
$ dotnet add package ParsecSharp
NuGet Package Manager Console:
> Install-Package ParsecSharp
PackageReference:
<ItemGroup>
<PackageReference Include="ParsecSharp" Version="*" />
</ItemGroup>
Download manually:
- netstandard2.1 (compatible with net5.0 or later, netcoreapp, with some performance improvements and additional implementations that depend on new runtime features)
- netstandard2.0 (compatible with net461 or later, uap, xamarin, and more)
Requires C# 7.3 or later for generic overloading resolution. Recommends C# 13.0 or later for better overloading resolution via OverloadResolutionPriority.
- Add the package reference to your project.
- Add the using directives:
using static ParsecSharp.Parser;
andusing static ParsecSharp.Text;
to your code. - Parse your all.
- JsonParser implementation
- CsvParser implementation
- Arithmetic expression parser implementation
- PEG parser generator implementation
Documentation is included in the UnitTest code.
If you want more information, read the API source code, all is there.
Feel free to create an Issue!
This software is released under the MIT License, see LICENSE.