public static ParserFactory <JsonArray> Array() { return(P.Concat <JsonArray>( P.Literal("["), JP.Whitespace(), P.Repeat <JsonArray, JsonEntity>( P.Concat <JsonEntity>( JP.Entity(), JP.Whitespace(), P.Optional <P.Void>( P.Literal <P.Void>(",") ), JP.Whitespace() ).Process(p => ((Parser <JsonEntity>)p.Parts[0]).Value), Quantification.Star ).Process(p => { var a = new JsonArray(); a.Items.AddRange(p.Matches.Select(m => m.Value)); return a; }), P.Literal("]") ) .Process(p => ((Parser <JsonArray>)p.Parts[2]).Value) .Name(nameof(JP.Array))); }
private static ParserFactory <KeyValuePair <string, JsonEntity> > ObjectItem() { // prevent infinite recursion by passing builders instead of factories return(P.ConcatB <KeyValuePair <string, JsonEntity> >( JP.Whitespace, JP.String, JP.Whitespace, () => P.Literal(":"), JP.Whitespace, JP.Entity, JP.Whitespace, () => P.Optional <P.Void>( P.Concat <P.Void>( P.Literal(","), JP.Whitespace() ) ), JP.Whitespace ).Process(p => { return new KeyValuePair <string, JsonEntity>( ((Parser <JsonString>)p.Parts[1]).Value.Value, ((Parser <JsonEntity>)p.Parts[5]).Value ); }) .Name(nameof(JP.ObjectItem))); }
public static ParserFactory <JsonEntity> Entity() { // use parser builders (not factories) to help prevent infinite recursion return(P.AnyB <JsonEntity>( () => P.Cast <JsonEntity, JsonObject>(JP.Object()), () => P.Cast <JsonEntity, JsonArray>(JP.Array()), () => P.Cast <JsonEntity, JsonString>(JP.String()), () => P.Cast <JsonEntity, JsonNumber>(JP.Number()), () => P.Cast <JsonEntity, JsonNull>(JP.Null()), () => P.Cast <JsonEntity, JsonBoolean>(JP.Boolean()) ).Name(nameof(JP.Entity))); }
public static ParserFactory <JsonString> String() { return(P.Concat <JsonString>( P.Literal("\""), P.Repeat <string, string>( P.Any <string>( P.StringRegex(@"[^\""\\]+"), JP.EscapedStringCharacter() ), Quantification.Star ).Process(p => System.String.Concat(p.Matches.Select(m => m.Value))), P.Literal("\"") ) .Process(p => { return new JsonString( ((Parser <string>)p.Parts[1]).Value ); }) .Name(nameof(JP.String))); }
public static ParserFactory <JsonObject> Object() { return(P.Concat <JsonObject>( P.Literal("{"), JP.Whitespace(), P.Repeat <JsonObject, KeyValuePair <string, JsonEntity> >( JP.ObjectItem(), Quantification.Star ).Process(p => { var o = new JsonObject(); foreach (Parser <KeyValuePair <string, JsonEntity> > i in p.Matches) { o.Items.Add(i.Value.Key, i.Value.Value); } return o; }), JP.Whitespace(), P.Literal("}") ) .Process(p => ((Parser <JsonObject>)p.Parts[2]).Value) .Name(nameof(JP.Object))); }