VArnas.ParserCombinators 1.0.0

dotnet add package VArnas.ParserCombinators --version 1.0.0
                    
NuGet\Install-Package VArnas.ParserCombinators -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="VArnas.ParserCombinators" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="VArnas.ParserCombinators" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="VArnas.ParserCombinators" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add VArnas.ParserCombinators --version 1.0.0
                    
#r "nuget: VArnas.ParserCombinators, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package VArnas.ParserCombinators@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=VArnas.ParserCombinators&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=VArnas.ParserCombinators&version=1.0.0
                    
Install as a Cake Tool

ParserCombinator

example workflow

The goal of this project is to recreate Haskell like parser combinators in .NET. While developing this library I am also trying to keep performance, ease of use and expressiveness in mind.

Simple example

Few of the available ways to define a parser that parses vowels:

// These static classes contain all the necesarry tools for building parsers.
using static ParserCombinator.Core.CommonParsers;
using static ParserCombinator.Core.Parser;

// Using Or parser function
var vowelParser = 
    Symbol('a').Or(
    Symbol('e')).Or(
    Symbol('i')).Or(
    Symbol('o')).Or(
    Symbol('u'));

// Using OneOf parser
var vowelParser = OneOf(
    Symbol('a'),
    Symbol('e'),
    Symbol('i'),
    Symbol('o'),
    Symbol('u'));

// QOL method equivalent to the above example.
var vowelParser = OneOf("aeiou");

vowelParser
    .ParseFromString("interesting")
    .Map(Console.Write);

More complicated example

This is an example of a parser that parses number palindromes of length 5 (12321, 98789, etc...) and converts the result to a number.

var weirdPalindromeParser =
    digit       .Bind(fst => // We parse any digit and bring it scope as `fst`
    digit       .Bind(snd => // We parse any digit and bring it scope as `snd`
    digit       .Bind(mid => // We parse any digit and bring it scope as `mid`
    Symbol(snd) .Bind(_ =>   // We parse the same digit as `snd`
    Symbol(fst) .Bind(_ =>   // We parse the same digit as `fst`
    {
        var result = int.Parse($"{fst}{snd}{mid}{snd}{fst}");
        return Pure<char, int>(result);
    })))));

weirdPalindromeParser
    .ParseFromString("12321")
    .Map(Console.Write);

Available parsers

  • Any - parses any single symbol.
  • Symbol - parses only if it matches provided symbol.
  • Seq - applies provided parsers that have the same resulting type in sequence.
  • Pure - parser that consumes nothing and always succeeds with provided result.
  • Zero - parser that consumes nothing and always fails with provided error message.
  • Some - applies provided parser 0 or more times.
  • Many - applies provided parser 1 or more times.
  • Repeat - applies provided parser n times.
  • Satisfy - parses a single symbol only if it satisfies provided predicate.
  • OneOf - given a list of parsers tries each one until succeeds or fails (multi-argument OR combinator).

Combining parsers

Common parser operations are also available:

  • Bind - Monadic bind operation used to combine parsers.
  • Map - Map over a given parser (equivalent to functor fmap in Haskell)
  • LeftMap - Equivalent to Haskell functor operator <$.
  • Or - Equivalent to Haskell alternative operator <|>

Contributing

Never done this before, would be interesting.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 281 10/14/2023