FParsec.CSharp 1.0.0

A thin C# wrapper for FParsec.

There is a newer version of this package available.
See the version list below for details.
Install-Package FParsec.CSharp -Version 1.0.0
dotnet add package FParsec.CSharp --version 1.0.0
<PackageReference Include="FParsec.CSharp" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FParsec.CSharp --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

FParsec.CSharp

Build status NuGet

FParsec.CSharp is a C# wrapper for the F# package FParsec. FParsec is a parser combinator library with which you can implement parsers declaratively.

Why FParsec.CSharp?

While using FParsec from C# is entirely possible in theory, it is very awkward in practice. Most of FParsec's elegance is lost in translation due to C#'s inferior type inference and its lack of custom operators.

FParsec.CSharp tries to alleviate that by wrapping FParsec's operators as extension functions.

FParsec.CSharp does not try to hide any types from FParsec or FSharp.Core--the wrapper is thin and also avoids name collisions. That way you can always fallback to FParsec anytime you need some functionality not (yet) implemented by FParsec.CSharp.

Based on the current implementation it should be easy to extend the wrapper yourself if needed. Pull requests are always welcome!

Getting started

Import the combinators, pre-defined parsers, and helper functions:

using FParsec.CSharp; // extension functions (combinators & helpers)
using static FParsec.CSharp.PrimitivesCS; // combinator functions
using static FParsec.CSharp.CharParsersCS; // pre-defined parsers

Now you can write some parsers:

var p = AnyChar.And(Digit);
var r = p.ParseString("a1");
System.Diagnostics.Debug.Assert(r.Result == ('a', '1'));

Using FParsec.CSharp and FParsec together

In case you need one of FParsec's more specialized parsers you can easily import their namespace:

using static FParsec.CharParsers;

In the example below we are using FParsec.CharParsers.many1Chars2(). As you can see it integrates seemlessly with FParsec.CSharp:

var first = Letter.Or(CharP('_'));
var rest = Letter.Or(CharP('_')).Or(Digit);
var identifier = many1Chars2(first, rest);
var p = identifier.And(Skip('=')).And(Int);

var r = p.ParseString("my_1st_var=13");

System.Diagnostics.Debug.Assert(r.Result == ("my_1st_var", 13));

FParsec supports parsing inputs with custom user state, wich is reflected by most of its functions taking a user state type variable. FParsec.CSharp however does not support user state, so you will have to specify Microsoft.FSharp.Core.Unit as the user state type when it can not be inferred:

var p = restOfLine<Unit>(true);

Otherwise you won't be able to use the combinators from FParsec.CSharp on it.

Some of FParsec's parsers take anonymous functions. But since they expect curried FSharpFuncs they won't accept C# lambdas. FParsec.CSharp comes with a little helper to create FSharpFuncs from Func objects:

// convert lambda with factory method
var fsfunc1 = FSharpFunc.From<char, bool>(c => c == 'x' || c == 'y');

// convert Func object with extension method
Func<char, bool> func = c => c == '1' || c == '2';
var fsfunc2 = func.ToFSharpFunc();

var p = manySatisfy<Unit>(fsfunc1).And(manySatisfy<Unit>(fsfunc2));

var r = p.ParseString("xyxyyy212221212");

Examples

Simple JSON

FSharpFunc<CharStream<Unit>, Reply<object>> jvalue = null;

var jnull = StringCI("null").Return((object)null);
var jnum = Int.Map(i => (object)i);
var jbool = StringCI("true").Or(StringCI("false"))
    .Map(b => (object)bool.Parse(b));
var quotedString = Between('"', Many(NoneOf("\"")), '"')
    .Map(string.Concat);
var jstring = quotedString.Map(s => (object)s);

var arrItems = Many(Rec(() => jvalue), sep: CharP(',').And(WS));
var jarray = Between(CharP('[').And(WS), arrItems, CharP(']'))
    .Map(elems => (object)new JArray(elems));

var jidentifier = quotedString;
var jprop = jidentifier.And(WS).And(Skip(':')).And(WS).And(Rec(() => jvalue))
    .Map((name, value) => new JProperty(name, value));
var objProps = Many(jprop, sep: CharP(',').And(WS));
var jobject = Between(CharP('{').And(WS), objProps, CharP('}'))
    .Map(props => (object)new JObject(props));

jvalue = OneOf(jnum, jbool, jnull, jstring, jarray, jobject).And(WS);

var simpleJsonParser = WS.And(jobject).And(WS).And(EOF).Map(o => (JObject)o);

Simple XML

var nameStart = Letter.Or(CharP('_'));
var nameChar = Letter.Or(Digit).Or(AnyOf("-_."));
var name = nameStart.And(Many(nameChar))
    .Map((first, rest) => string.Concat(rest.Prepend(first)));

var quotedString = Between('"', Many(NoneOf("\"")), '"')
    .Map(string.Concat);
var attribute = WS1.And(name).And(WS).And(Skip('=')).And(WS).And(quotedString)
    .Map((attrName, attrVal) => new XAttribute(attrName, attrVal));
var attributes = Many(Try(attribute));

FSharpFunc<CharStream<Unit>, Reply<XElement>> element = null;

var emptyElement = Between("<", name.And(attributes).And(WS), "/>")
    .Map((elName, attrs) => new XElement(elName, attrs));

var openingTag = Between('<', name.And(attributes).And(WS), '>');
FSharpFunc<CharStream<Unit>, Reply<string>> closingTag(string tagName) => Between("</", StringP(tagName).And(WS), ">");
var childElements = Many1(Try(WS.And(Rec(() => element)).And(WS)))
    .Map(attrs => (object)attrs);
var text = Many(NoneOf("<"))
    .Map(t => (object)string.Concat(t));
var content = childElements.Or(text);
var parentElement = openingTag.And(content).Map(Flat).And(x => closingTag(x.Item1).Return(x))
    .Map((elName, elAttrs, elContent) => new XElement(elName, elAttrs, elContent));

element = Try(emptyElement).Or(parentElement);

var simpleXmlParser = element.And(WS).And(EOF);

Glob patterns

var globParser =
    Many(OneOf(
        Skip('?').Map(NFA.MakeAnyChar),
        Skip('*').Map(NFA.MakeAnyChar).Map(NFA.MakeZeroOrMore),
        Between('[', AnyChar.And(Skip('-')).And(AnyChar), ']').Map(NFA.MakeCharRange),
        Skip('\\').And(AnyOf(@"?*[]\")).Map(NFA.MakeChar),
        AnyChar.Map(NFA.MakeChar)))
    .And(EOF)
    .Map(NFA.Concat)
    .Map(proto => proto(new Final()));

Arithmetic expressions

var exprParser =
    WS.And(new OPPBuilder<int, Unit>()
        .WithOperators(ops => ops
            .AddInfix("+", 10, Associativity.Left, WS, (x, y) => x + y)
            .AddInfix("-", 10, Associativity.Left, WS, (x, y) => x - y)
            .AddInfix("*", 20, Associativity.Left, WS, (x, y) => x * y)
            .AddInfix("/", 20, Associativity.Left, WS, (x, y) => x / y)
            .AddPrefix("-", 20, x => -x)
            .AddInfix("^", 30, Associativity.Right, WS, (x, y) => (int)Math.Pow(x, y))
            .AddPostfix("!", 40, Factorial))
        .WithImplicitOperator(20, (x, y) => x * y)
        .WithTerms(term => OneOf(
            Integer.And(WS),
            Between(CharP('(').And(WS), term, CharP(')').And(WS))))
        .Build()
        .ExpressionParser);

Simple regex

var simpleRegexParser =
    Many(new OPPBuilder<NFA.ProtoState, Unit>()
        .WithImplicitOperator(2, NFA.Connect)
        .WithOperators(ops => ops
            .AddPostfix("*", 3, NFA.MakeZeroOrMore)
            .AddPostfix("+", 3, NFA.MakeOneOrMore)
            .AddPostfix("?", 3, NFA.MakeZeroOrOne)
            .AddInfix("|", 1, NFA.MakeAlternation))
        .WithTerms(matchExpr => {
            var group = Between('(', Many(matchExpr), ')');
            var wildcard = Skip('.');
            var charMatch = NoneOf("*+?|()");
            return OneOf(
                group.Map(NFA.Concat),
                wildcard.Map(NFA.MakeAnyChar),
                charMatch.Map(NFA.MakeChar));
        })
        .Build()
        .ExpressionParser)
    .And(EOF)
    .Map(NFA.Concat)
    .Map(proto => proto(new Final()));

FParsec.CSharp

Build status NuGet

FParsec.CSharp is a C# wrapper for the F# package FParsec. FParsec is a parser combinator library with which you can implement parsers declaratively.

Why FParsec.CSharp?

While using FParsec from C# is entirely possible in theory, it is very awkward in practice. Most of FParsec's elegance is lost in translation due to C#'s inferior type inference and its lack of custom operators.

FParsec.CSharp tries to alleviate that by wrapping FParsec's operators as extension functions.

FParsec.CSharp does not try to hide any types from FParsec or FSharp.Core--the wrapper is thin and also avoids name collisions. That way you can always fallback to FParsec anytime you need some functionality not (yet) implemented by FParsec.CSharp.

Based on the current implementation it should be easy to extend the wrapper yourself if needed. Pull requests are always welcome!

Getting started

Import the combinators, pre-defined parsers, and helper functions:

using FParsec.CSharp; // extension functions (combinators & helpers)
using static FParsec.CSharp.PrimitivesCS; // combinator functions
using static FParsec.CSharp.CharParsersCS; // pre-defined parsers

Now you can write some parsers:

var p = AnyChar.And(Digit);
var r = p.ParseString("a1");
System.Diagnostics.Debug.Assert(r.Result == ('a', '1'));

Using FParsec.CSharp and FParsec together

In case you need one of FParsec's more specialized parsers you can easily import their namespace:

using static FParsec.CharParsers;

In the example below we are using FParsec.CharParsers.many1Chars2(). As you can see it integrates seemlessly with FParsec.CSharp:

var first = Letter.Or(CharP('_'));
var rest = Letter.Or(CharP('_')).Or(Digit);
var identifier = many1Chars2(first, rest);
var p = identifier.And(Skip('=')).And(Int);

var r = p.ParseString("my_1st_var=13");

System.Diagnostics.Debug.Assert(r.Result == ("my_1st_var", 13));

FParsec supports parsing inputs with custom user state, wich is reflected by most of its functions taking a user state type variable. FParsec.CSharp however does not support user state, so you will have to specify Microsoft.FSharp.Core.Unit as the user state type when it can not be inferred:

var p = restOfLine<Unit>(true);

Otherwise you won't be able to use the combinators from FParsec.CSharp on it.

Some of FParsec's parsers take anonymous functions. But since they expect curried FSharpFuncs they won't accept C# lambdas. FParsec.CSharp comes with a little helper to create FSharpFuncs from Func objects:

// convert lambda with factory method
var fsfunc1 = FSharpFunc.From<char, bool>(c => c == 'x' || c == 'y');

// convert Func object with extension method
Func<char, bool> func = c => c == '1' || c == '2';
var fsfunc2 = func.ToFSharpFunc();

var p = manySatisfy<Unit>(fsfunc1).And(manySatisfy<Unit>(fsfunc2));

var r = p.ParseString("xyxyyy212221212");

Examples

Simple JSON

FSharpFunc<CharStream<Unit>, Reply<object>> jvalue = null;

var jnull = StringCI("null").Return((object)null);
var jnum = Int.Map(i => (object)i);
var jbool = StringCI("true").Or(StringCI("false"))
    .Map(b => (object)bool.Parse(b));
var quotedString = Between('"', Many(NoneOf("\"")), '"')
    .Map(string.Concat);
var jstring = quotedString.Map(s => (object)s);

var arrItems = Many(Rec(() => jvalue), sep: CharP(',').And(WS));
var jarray = Between(CharP('[').And(WS), arrItems, CharP(']'))
    .Map(elems => (object)new JArray(elems));

var jidentifier = quotedString;
var jprop = jidentifier.And(WS).And(Skip(':')).And(WS).And(Rec(() => jvalue))
    .Map((name, value) => new JProperty(name, value));
var objProps = Many(jprop, sep: CharP(',').And(WS));
var jobject = Between(CharP('{').And(WS), objProps, CharP('}'))
    .Map(props => (object)new JObject(props));

jvalue = OneOf(jnum, jbool, jnull, jstring, jarray, jobject).And(WS);

var simpleJsonParser = WS.And(jobject).And(WS).And(EOF).Map(o => (JObject)o);

Simple XML

var nameStart = Letter.Or(CharP('_'));
var nameChar = Letter.Or(Digit).Or(AnyOf("-_."));
var name = nameStart.And(Many(nameChar))
    .Map((first, rest) => string.Concat(rest.Prepend(first)));

var quotedString = Between('"', Many(NoneOf("\"")), '"')
    .Map(string.Concat);
var attribute = WS1.And(name).And(WS).And(Skip('=')).And(WS).And(quotedString)
    .Map((attrName, attrVal) => new XAttribute(attrName, attrVal));
var attributes = Many(Try(attribute));

FSharpFunc<CharStream<Unit>, Reply<XElement>> element = null;

var emptyElement = Between("<", name.And(attributes).And(WS), "/>")
    .Map((elName, attrs) => new XElement(elName, attrs));

var openingTag = Between('<', name.And(attributes).And(WS), '>');
FSharpFunc<CharStream<Unit>, Reply<string>> closingTag(string tagName) => Between("</", StringP(tagName).And(WS), ">");
var childElements = Many1(Try(WS.And(Rec(() => element)).And(WS)))
    .Map(attrs => (object)attrs);
var text = Many(NoneOf("<"))
    .Map(t => (object)string.Concat(t));
var content = childElements.Or(text);
var parentElement = openingTag.And(content).Map(Flat).And(x => closingTag(x.Item1).Return(x))
    .Map((elName, elAttrs, elContent) => new XElement(elName, elAttrs, elContent));

element = Try(emptyElement).Or(parentElement);

var simpleXmlParser = element.And(WS).And(EOF);

Glob patterns

var globParser =
    Many(OneOf(
        Skip('?').Map(NFA.MakeAnyChar),
        Skip('*').Map(NFA.MakeAnyChar).Map(NFA.MakeZeroOrMore),
        Between('[', AnyChar.And(Skip('-')).And(AnyChar), ']').Map(NFA.MakeCharRange),
        Skip('\\').And(AnyOf(@"?*[]\")).Map(NFA.MakeChar),
        AnyChar.Map(NFA.MakeChar)))
    .And(EOF)
    .Map(NFA.Concat)
    .Map(proto => proto(new Final()));

Arithmetic expressions

var exprParser =
    WS.And(new OPPBuilder<int, Unit>()
        .WithOperators(ops => ops
            .AddInfix("+", 10, Associativity.Left, WS, (x, y) => x + y)
            .AddInfix("-", 10, Associativity.Left, WS, (x, y) => x - y)
            .AddInfix("*", 20, Associativity.Left, WS, (x, y) => x * y)
            .AddInfix("/", 20, Associativity.Left, WS, (x, y) => x / y)
            .AddPrefix("-", 20, x => -x)
            .AddInfix("^", 30, Associativity.Right, WS, (x, y) => (int)Math.Pow(x, y))
            .AddPostfix("!", 40, Factorial))
        .WithImplicitOperator(20, (x, y) => x * y)
        .WithTerms(term => OneOf(
            Integer.And(WS),
            Between(CharP('(').And(WS), term, CharP(')').And(WS))))
        .Build()
        .ExpressionParser);

Simple regex

var simpleRegexParser =
    Many(new OPPBuilder<NFA.ProtoState, Unit>()
        .WithImplicitOperator(2, NFA.Connect)
        .WithOperators(ops => ops
            .AddPostfix("*", 3, NFA.MakeZeroOrMore)
            .AddPostfix("+", 3, NFA.MakeOneOrMore)
            .AddPostfix("?", 3, NFA.MakeZeroOrOne)
            .AddInfix("|", 1, NFA.MakeAlternation))
        .WithTerms(matchExpr => {
            var group = Between('(', Many(matchExpr), ')');
            var wildcard = Skip('.');
            var charMatch = NoneOf("*+?|()");
            return OneOf(
                group.Map(NFA.Concat),
                wildcard.Map(NFA.MakeAnyChar),
                charMatch.Map(NFA.MakeChar));
        })
        .Build()
        .ExpressionParser)
    .And(EOF)
    .Map(NFA.Concat)
    .Map(proto => proto(new Final()));

Release Notes

Bump version to 1.0.0 +semver:major.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
12.1.0 39 1/1/2021
12.0.5 39 12/30/2020
12.0.4 160 11/14/2020
12.0.3 162 7/10/2020
12.0.2 224 3/6/2020
12.0.1 109 3/6/2020
12.0.0 184 11/12/2019
11.0.0 174 10/3/2019
8.3.2 185 9/1/2019
8.3.1 195 6/23/2019
8.3.0 180 6/23/2019
8.2.0 173 6/18/2019
8.1.3 215 6/12/2019
8.1.2 203 6/11/2019
8.1.1 195 6/11/2019
8.1.0 194 6/11/2019
8.0.0 194 6/11/2019
7.1.1 200 6/10/2019
7.1.0 204 6/10/2019
7.0.0 194 6/10/2019
6.4.0 218 6/6/2019
6.3.0 228 6/6/2019
6.2.0 220 6/4/2019
6.1.0 217 6/4/2019
6.0.0 212 6/4/2019
5.12.0 220 6/3/2019
5.11.0 207 6/2/2019
5.10.2 207 5/31/2019
5.10.1 214 5/31/2019
5.9.0 214 5/28/2019
5.8.0 191 5/28/2019
5.7.0 191 5/28/2019
5.6.0 208 5/27/2019
5.5.0 198 5/27/2019
5.4.0 211 5/26/2019
5.3.0 201 5/25/2019
5.2.0 212 5/25/2019
5.1.0 207 5/25/2019
5.0.0 211 5/25/2019
4.5.0 210 5/24/2019
4.4.0 208 5/24/2019
4.3.0 202 5/24/2019
4.2.0 214 5/24/2019
4.1.0 198 5/24/2019
4.0.1 203 5/23/2019
4.0.0 193 5/23/2019
3.6.0 204 5/23/2019
3.5.0 202 5/23/2019
3.4.0 211 5/22/2019
3.3.0 216 5/22/2019
3.2.0 216 5/21/2019
3.1.0 231 5/21/2019
3.0.0 214 5/21/2019
2.17.0 206 5/21/2019
2.16.0 204 5/19/2019
2.15.0 216 5/19/2019
2.11.0 217 5/19/2019
2.10.1 212 5/19/2019
2.10.0 209 5/18/2019
2.9.0 209 5/18/2019
2.8.0 206 5/18/2019
2.7.0 200 5/18/2019
2.6.0 214 5/17/2019
2.5.0 219 5/17/2019
2.4.0 205 5/17/2019
2.3.0 210 5/17/2019
2.2.0 217 5/17/2019
2.1.0 210 5/17/2019
2.0.0 212 5/16/2019
1.1.0 216 5/16/2019
1.0.0 217 5/15/2019
0.1.36 222 5/10/2019
0.1.32 221 5/5/2019
0.1.29 225 5/3/2019
0.1.28 221 5/3/2019
0.1.26 218 5/3/2019
0.1.25 315 5/2/2019
0.1.20 227 4/29/2019
0.1.19 228 4/29/2019
0.1.18 215 4/29/2019
0.1.17 218 4/29/2019
0.1.16 209 4/28/2019
0.1.15 211 4/28/2019
0.1.14 222 4/28/2019
0.1.12 238 4/28/2019
0.1.9 242 4/28/2019
0.1.6 241 4/27/2019
Show less