ParserObjects is a library for object-based parsers, combinators, parsing utilities and abstractions. See the Documentation for more details on usage.
Install-Package ParserObjects
See the Test Suite Examples for some examples of usage.
This project has several goals:
- To enable composing of parsers from smaller building blocks
- To enable crafting of parsers using a variety of techniques and algorithms, whichever is the best fit for your project
- To use streaming input from a variety of sources, instead of reading data into fixed memory buffers
- To be generic as much as possible, supporting common string-parsing algorithms but also being extensible to any structured input type
- Using good interface design to encourage proper use and good code style
- To provide an easy on-ramp for people who need to parse data, but are not experts in parsing theory
An ISequence is similar to an IEnumerable or IEnumerator in concept, though with a focus on supporting parsing operations. Sequences allow you to GetNext() items one at a time, to Peek() at items without consuming them, and rewind the sequence to a previous location. There are many ways to create a sequence:
using static ParserObjects.Sequences;
// Turn an existing IReadOnlyList<T> into an ISequence<T>
var sequence = FromList(myList);
// Get a character sequence from a string
var sequence = FromString("...");
// Get a character sequence from a file or stream
var sequence = FromCharacterFile("fileName.txt");
var sequence = FromCharacterStream(myStream);An IParser is an object that attempts to match a pattern in an input sequence. If the pattern is matched, the items will be consumed by the parser and a result will be returned. If the parser cannot match at the beginning of the input sequence, it returns a failure and no items are consumed. There are several dozen types of parsers in the library, which can be found by including these lines in your file:
using static ParserObjects.Parsers;
using static ParserObjects.Parsers<char>;Once you have a parser, you can pass it an input sequence to obtain a parse result:
var result = parser.Parse(sequence);The ParserObjects library provides many small parsers and parser combinators that can be built up into parsers for large and complex grammars. In addition, ParserObjects provides several pre-made parsers for specialty parsing needs. Please see the Documentation for more information, and the Test Suite Examples for real-world usage patterns.
We welcome contributions including code, feedback, bug reports and suggestions.
ParserObjects library is currently in active development. Version 6 is available on Nuget and is preferred over all previous versions.