-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
85 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using Parlot.Fluent; | ||
using Pidgin.Examples.Json; | ||
|
||
using static Parlot.Fluent.Parsers; | ||
|
||
namespace Pidgin.Bench.ParlotParsers | ||
{ | ||
public class ParlotJsonParser | ||
{ | ||
private static readonly Parlot.Fluent.Parser<IJson> Json; | ||
|
||
static ParlotJsonParser() | ||
{ | ||
var LBrace = Terms.Char('{'); | ||
var RBrace = Terms.Char('}'); | ||
var LBracket = Terms.Char('['); | ||
var RBracket = Terms.Char(']'); | ||
var Colon = Terms.Char(':'); | ||
var Comma = Terms.Char(','); | ||
|
||
var String = Terms.String(StringLiteralQuotes.Double); | ||
|
||
var jsonString = | ||
String | ||
.Then<IJson>(static s => new JsonString(s.ToString())); | ||
|
||
var json = Deferred<IJson>(); | ||
|
||
var jsonArray = | ||
Between(LBracket, Separated(Comma, json), RBracket) | ||
.Then<IJson>(static els => new JsonArray(els.ToImmutableArray())); | ||
|
||
var jsonMember = | ||
String.And(Colon).And(json) | ||
.Then(static member => new KeyValuePair<string, IJson>(member.Item1.ToString(), member.Item3)); | ||
|
||
var jsonObject = | ||
Between(LBrace, Separated(Comma, jsonMember), RBrace) | ||
.Then<IJson>(static kvps => new JsonObject(kvps.ToImmutableDictionary())); | ||
|
||
Json = json.Parser = jsonString.Or(jsonArray).Or(jsonObject); | ||
} | ||
|
||
public static IJson Parse(string input) | ||
{ | ||
if (Json.TryParse(input, out var result)) | ||
{ | ||
return result; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters