FParsec.CSharp
0.1.9
A thin C# wrapper for FParsec.
See the version list below for details.
Install-Package FParsec.CSharp -Version 0.1.9
dotnet add package FParsec.CSharp --version 0.1.9
<PackageReference Include="FParsec.CSharp" Version="0.1.9" />
paket add FParsec.CSharp --version 0.1.9
FParsec.CSharp
This is a C# wrapper for the parser combinator library FParsec which is written in optimized F#.
Why Parser Combinators?
Easy: because they are the most elegant way to define parsers.
Check out the tests and see for yourself.
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. Pull requests are always welcome!
Example
This is what a basic JSON parser looks like with FParsec.CSharp
:
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 = Skip('"').And(Many(CharP(c => c != '"'))).And(Skip('"'))
.Map(string.Concat);
var jstring = quotedString.Map(s => (object)s);
var jarray = Skip('[').And(WS).And(Many(Rec(() => jvalue), sep: CharP(',').And(WS))).And(Skip(']'))
.Map(elems => (object)new JArray(elems));
var jidentifier = quotedString;
var jprop = jidentifier.And(WS).And(Skip(':')).And(WS).And(Rec(() => jvalue))
.Map(x => new JProperty(x.Item1, x.Item2));
var jobject = Skip('{').And(WS).And(Many(jprop, sep: CharP(',').And(WS))).And(Skip('}'))
.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);
Run it like so:
[Fact] public void CanParseJson() => simpleJsonParser
.ParseString(@"{
""prop1"" : ""val"",
""prop2"" : [false, 13, null],
""prop3"" : { }
}")
.ShouldBe(new JObject(
new JProperty("prop1", "val"),
new JProperty("prop2", new JArray(false, 13, null)),
new JProperty("prop3", new JObject())));
You can find more examples in the test project.
Hints
In order to simplify the types shown in IntelliSense you can use type aliases:
using Chars = FParsec.CharStream<Microsoft.FSharp.Core.Unit>;
Unfortunately C# does not support aliasing types with unbound generics, hence if you want to simplify the type of parser you will have to do it for each of the possible Reply<T>
s you are using:
using StringParser = FSharpFunc<CharStream<Unit>, Reply<string>>;
using JsonParser = FSharpFunc<CharStream<Unit>, Reply<JObject>>;
// ...
Alternatives
FParsec.CSharp
does not wrap all of FParsec
's features yet. If you need an all-in-one solution then have a look at the following alternatives:
FParsec.CSharp
This is a C# wrapper for the parser combinator library FParsec which is written in optimized F#.
Why Parser Combinators?
Easy: because they are the most elegant way to define parsers.
Check out the tests and see for yourself.
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. Pull requests are always welcome!
Example
This is what a basic JSON parser looks like with FParsec.CSharp
:
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 = Skip('"').And(Many(CharP(c => c != '"'))).And(Skip('"'))
.Map(string.Concat);
var jstring = quotedString.Map(s => (object)s);
var jarray = Skip('[').And(WS).And(Many(Rec(() => jvalue), sep: CharP(',').And(WS))).And(Skip(']'))
.Map(elems => (object)new JArray(elems));
var jidentifier = quotedString;
var jprop = jidentifier.And(WS).And(Skip(':')).And(WS).And(Rec(() => jvalue))
.Map(x => new JProperty(x.Item1, x.Item2));
var jobject = Skip('{').And(WS).And(Many(jprop, sep: CharP(',').And(WS))).And(Skip('}'))
.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);
Run it like so:
[Fact] public void CanParseJson() => simpleJsonParser
.ParseString(@"{
""prop1"" : ""val"",
""prop2"" : [false, 13, null],
""prop3"" : { }
}")
.ShouldBe(new JObject(
new JProperty("prop1", "val"),
new JProperty("prop2", new JArray(false, 13, null)),
new JProperty("prop3", new JObject())));
You can find more examples in the test project.
Hints
In order to simplify the types shown in IntelliSense you can use type aliases:
using Chars = FParsec.CharStream<Microsoft.FSharp.Core.Unit>;
Unfortunately C# does not support aliasing types with unbound generics, hence if you want to simplify the type of parser you will have to do it for each of the possible Reply<T>
s you are using:
using StringParser = FSharpFunc<CharStream<Unit>, Reply<string>>;
using JsonParser = FSharpFunc<CharStream<Unit>, Reply<JObject>>;
// ...
Alternatives
FParsec.CSharp
does not wrap all of FParsec
's features yet. If you need an all-in-one solution then have a look at the following alternatives:
Release Notes
Switch to MIT license.
Dependencies
-
.NETStandard 2.0
- FParsec (>= 1.0.3)
- FSharp.Core (>= 4.6.2)
Used By
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 | 220 | 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 | 210 | 6/12/2019 |
8.1.2 | 198 | 6/11/2019 |
8.1.1 | 190 | 6/11/2019 |
8.1.0 | 194 | 6/11/2019 |
8.0.0 | 189 | 6/11/2019 |
7.1.1 | 195 | 6/10/2019 |
7.1.0 | 199 | 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 | 215 | 6/4/2019 |
6.1.0 | 212 | 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 | 205 | 5/24/2019 |
4.4.0 | 208 | 5/24/2019 |
4.3.0 | 197 | 5/24/2019 |
4.2.0 | 214 | 5/24/2019 |
4.1.0 | 193 | 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 | 199 | 5/19/2019 |
2.15.0 | 211 | 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 | 209 | 5/17/2019 |
2.5.0 | 214 | 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 | 207 | 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 | 216 | 5/5/2019 |
0.1.29 | 225 | 5/3/2019 |
0.1.28 | 216 | 5/3/2019 |
0.1.26 | 213 | 5/3/2019 |
0.1.25 | 310 | 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 | 204 | 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 |