A tool for lexing and parsing text for the .NET framework, intended for those medium sized parsing applications where you used to cobble together a parser by hand. When a parsing task is too complex for regular expressions but too simple for dragging in a huge tool, Piglet can help! With a fluent and readable syntax and a low footprint, everyone can use proper parsing techniques.
Piglet boasts a fluent configuration interface, where you type what you mean to parse and not how you indend do to so. With modern code completion guiding you along the way, writing a parser is easy.
Dependency free, no pre-build step needed. A single assembly is all that you need. Download it here or on NuGet. Open sourced under the MIT license.
Open sourced and open for contributors at any skill level. Fork away here at github.
Piglet is designed to make parsing as quick and easy as possible. The key to this is to avoid technical terms as much as possible and use a fluent configuration that describes what you actually want the parser to do. But enough talk, here is a JSON parser implemented in Piglet:
var config = ParserFactory.Fluent(); var jsonObject = config.Rule(); var jsonElement = config.Rule(); var jsonValue = config.Rule(); var jsonArray = config.Rule(); jsonObject.IsMadeUp.By("{") .Followed.ByListOf(jsonElement).As("ElementList").ThatIs.SeparatedBy(",").Optional .Followed.By("}") .WhenFound( o => new JsonObject { Elements = o.ElementList } ); jsonElement.IsMadeUp.By(config.QuotedString).As("Name") .Followed.By(":") .Followed.By(jsonValue).As("Value") .WhenFound( o => new JsonElement { Name = o.Name, Value = o.Value } ); jsonValue.IsMadeUp.By(config.QuotedString) .Or.By () .Or.By () .Or.By(jsonObject) .Or.By(jsonArray) .Or.By () .Or.By("null").WhenFound(o => null); jsonArray.IsMadeUp.By("[") .Followed.ByListOf(jsonValue).As("Values").ThatIs.SeparatedBy(",").Optional .Followed.By("]") .WhenFound(o => o.Values); var parser = config.CreateParser();
That's all you need to do. The parser object now contains a working parser ready to do your bidding, pass it a string and receive a JSONobject back. Most of the common things a user might want to parse are already implemented, such as integers, doubles and quoted strings. Less work for you.