/** * # Parser Monad and Basic Combinators * * This module defines the basic types and combinators for the parser monad. * To learn about monadic parsers refer to the list of literature in the * [Wikipedia page](https://en.wikipedia.org/wiki/Parser_combinator). */ import * as inp from "./input"; import * as pr from "./result"; import * as lex from "./lexer"; import * as ref from "./ref"; /** * * ## Parsing Function * * `Parse` type represents a parsing function whics takes a * `ParserInput` stream as an argument and returns a `ParseResult` object. * The type of value to be parsed and the type of terminals in the input stream * are given as type parameters `T` and `S`. */ export type Parse = (input: inp.ParserInput) => pr.ParseResult; /** * ## Parser Class * * The central type in the Parzec library is the `Parser` class. It wraps * a parsing function and provides the core combinators to combine parsers in * various ways. */ export declare class Parser { readonly parse: Parse; /** * Constructor wraps the parsing function. */ constructor(parse: Parse); /** * The monadic bind that corresponds to Haskell's `>>=` operator. Runs * `this` parser, and if it succeeds, feeds its result to the `binder` * function that returns a new Parser. This is the basic operation that is * used in other combinators to glue parsers together. */ bind(binder: (value: T) => Parser): Parser; /** * The sequence operator. Runs `this` parser, and if it succeeds, runs the * `other` parser ignoring the result of `this` one. */ seq(other: Parser): Parser; /** * Map result of the parser to another value. This function implements a * [_functor_](https://en.wikipedia.org/wiki/Functor) which is a superclass * of monad. */ map(mapper: (value: T) => U): Parser; /** * ## Conditional Parsing * * The ordered choice operation. Creates a parser that first runs `this` * parser, and if that fails, runs the `other` one. Corresponds to the `/` * operation in [PEG grammars](https://en.wikipedia.org/wiki/Parsing_expression_grammar). */ or(other: Parser): Parser; /** * Parse an optional value, if the parser fails then the default value is * returned. */ optional(defaultValue: T): Parser; /** * Parse an optional reference value, if the parser fails then null is * returned. */ optionalRef(): Parser; /** * Runs parser and checks that it succeeds and that the result it returns * satisfies a given predicate. */ where(predicate: (value: T) => boolean): Parser; /** * ## Parsing Multiple Items * * Creates a parser that will run `this` parser zero or more times. The * results of the input parser are added to an array. */ zeroOrMore(): Parser; /** * Creates a parser that runs `this` parser one or more times. */ oneOrMore(): Parser; /** * Parsing succeeds if `this` parser succeeds from `min` to `max` times. */ occurrences(min: number, max: number): Parser; /** * ## Lookahead & Backtracking * * Check that `this` parser succeeds without consuming any input. * Corresponds to the `&` operator in PEG grammars. */ and(): Parser; /** * Check that `this` parser fails without consuming any input. Corresponds * to the `!` operator in PEG grammars. */ not(): Parser; /** * Bactrack to the current input position, even if the given parser fails * and has advanced the input position. Normally we do not bactrack when a * parser has advanced in the input. Doing so would loose the position where * the parsing failed and make error messages more vague. Sometimes, * however, we need more input lookahead. In these cases, you can use the * backtrack operation to retry the next rule. */ backtrack(): Parser; /** * ## Error Reporting and Debugging * * Give a human-readable name to the "thing" that the given parser matches. * This name is reported as expected value, if the parsing fails. */ expect(expected: string): Parser; /** * Attach debugging information to a parser. To trace which rules are * triggered during parsing, you can add debugging info to any parser. This * combinator produces a hierarchical tree of parser invocations which * includes information about input symbol and its position. If debugging * is disabled, this function does nothing. */ trace(ruleName: string): Parser; } /** * ## Debugging Options * * The following object contains the global settings that control the parser * reporting. */ export declare const parserDebug: { /** * When `debugging` flag is on parsers count the number of rules evaluated * during their operation. The `rulesEvaluated` field contains this * information. */ debugging: boolean; rulesEvaluated: number; /** * If errorMessages flag is turned off, the expected information will not be * available in parse errors. This speeds up the parsing nominally. */ errorMessages: boolean; /** * The current indentation level in the debugging output is stored in this * field. */ indentation: number; /** * Indent the debug output by one level. */ indent(): void; /** * Unndent the debug output by one level. */ unindent(): void; /** * Write a string to the debug output. */ write(text: string): void; }; /** * ## Main Functions * * Attempt to parse an input with a given parser. Takes a Parser and a * ParserInput as arguments and return a ParseResult. */ export declare function tryParse(parser: Parser, input: inp.ParserInput): pr.ParseResult; /** * Parse an input using a given parser, or throw an exception, if parsing fails. */ export declare function parse(parser: Parser, input: inp.ParserInput): T; /** * ## Monadic Returns * * Create a parser that always succeeds and returns the given value without * consuming any input. This function implements the monadic return, that is, * it lifts a value to the parser monad. */ export declare function mret(value: T): Parser; /** * Create a parser that always fails. The terminals reported as * found or expected are given as an argument. */ export declare function fail(found: string, ...expected: string[]): Parser; /** * ## Parsing Terminals * * Creates a parser that reads one terminal from the input and returns it, if it * satisfies the given predicate; otherwise the parser fails. */ export declare function satisfy(predicate: (value: T) => boolean): Parser; /** * Creates a parser that reads one terminal from the input and returns it, if it * does **not** satisfy a given predicate. */ export declare function notSatisfy(predicate: (value: T) => boolean): Parser; /** * Any of the given parsers must succeed. The operation is the same * as the `or` combinator generalized to arbitrary number of choices. */ export declare function any(...parsers: Parser[]): Parser; /** * Peek next symbol in the input stream without changing the position. */ export declare function peek(): Parser; /** * Select a parser to be used based on the next symbol in the input. This * function is an alternative to the the "any" combinator. It reduces * backtracking when the parser to be applied can be deduced from the next * symbol. */ export declare function choose(selector: (input: S) => Parser): Parser; /** * ## Getting Current Position * * A parser that returns the current position of the input. This is useful * when binding parsers together and you want to know the position where you * currently are. The position can be also used for backtracking. */ export declare function position(): Parser; /** * ## User-Managed State * * Get the current satellite state stored in the input. */ export declare function getState(): Parser; /** * Set the current satellite state stored in the input. The new state * is not given explicitly. Rather, a funcion which returns the new * state is specified. */ export declare function setState(newValue: () => T): Parser; /** * Mutate the satellite state stored in the input. The mutation is done * with a function given as an argument. */ export declare function mutateState(mutate: (state: T) => void): Parser; /** * Check that the current state matches a predicate. If not, the result parser * fails. */ export declare function checkState(predicate: (state: T) => boolean): Parser; /** * Clean up the current state after a parser has been executed. The clean-up * function is run regardless of whether the parser succeeds or fails. */ export declare function cleanupState(parser: Parser, cleanup: (state: U) => void): Parser; /** * ## Defining Mutually Recursive Parsers * * Often grammar rules are mutually recursive, which means that there is no way * to write them in an order where all the dependent rules are defined. In these * occasions, you can just create a _reference_ to a parser and set its * implementation later. To refer to the parser that is not yet defined, you can * use this function. */ export declare function forwardRef(parser: ref.Ref>): Parser; /** * ## General Parsers * * The catch-all parser that will match any symbol read from the input. */ export declare function anything(): Parser; /** * Parser that succeeds if the symbol read from the input is equal (===) to * given parameter; otherwise parsing fails. */ export declare function is(value: T): Parser; /** * Parse a specific token from the lexer input stream. */ export declare function token(token: T): Parser, lex.Token>; /** * Helper function to create a terminal parser. */ export declare function terminal(tok: T, name: string): Parser, lex.Token>;