D 2016-11-20T05:36:39.109 L PComb P 264ef1e73b1ad918a83f013089eff62308353642 U User W 6340

PComb: a Parser Combinator library in TypeScript

A collection of general and specific-purpose parsers that can be composed with combinator functions to very powerful effect.

Parsers

A parser is a function that takes a ParserInput, a ParserOutput and returns a ParseResult.

ParseResult

A ParseResult is a three-element tuple with these values:
    1. a boolean that indicates if the parser successfully matched the input
    2. a copy of the ParserInput object that was passed in with any matched test removed from the text property
    3. a copy of the ParserOutput object that was passed in with any matched text appended to the matched array 

ParserInput

The code that uses PComb must provide input to the library with a class that implements the ParserInput interface:
export interface ParserInput {

    /** What is the text that is being parsed? */

    text: string;

    copy(): ParserInput;

}

ParserOutput

Similarly to ParserInput, a class that implements ParserOutput is provided by the calling code to record the units of text that have been matched by individual parser as well to record any state that is specific to the particular use:
export interface ParserOutput {

    /** The text that was matched by the parser. */

    matched: Array<string>;

    copy(): ParserOutput;

}

Combinators

A combinator is a higher-order function that takes one or more Parser functions as input and produces a new function that wraps and executes them in some particular way.

ParserAction

A ParserAction is a function that takes a string of matched characters and a ParserOutput and returns a ParserOutput:
export type ParserAction = (matchedText: string, output: ParserOutput) => ParserOutput;

If such a function is provided to a Parser, it will be executed on a successful match. The returned ParserOutput object then becomes the return value for the Parser. This mechanism allows calling code to have use-specific state that is recorded and mutated on parser matching.


Interested? Get Started or check out the [PComb API|API docs].

Z 6733707d0e0b244b48906d25c144df14