: never;
/**
* Create a parser which tries to match the given parser
* as many times as possible
* @param parser the parser to test
* @returns a parser which tries to match the given parser
* as many times as possible
*/
export declare function many(parser: P): ManyOf
;
/**
* Create a parser which match at least one time,
* then as many times as possible, the given parser
* @param parser the parser to test
* @returns a parser which match at least one time,
* then as many times as possible, the given parser
*/
export declare function oneOrMore
(parser: P): ManyOf
;
/**
* Create a parser which match the given parser
* the given number of times
* @param parser the parser to test
* @param times how many exact times the parser must match
* @returns a parser which match the given parser
* the given number of times
*/
export declare function times
(parser: P, times: number): ManyOf
;
/**
* Create a parser which check the following syntax:
* `wrapper content wrapper` from the given parsers.
* The resultant parser will consider only the output of the
* content parser.\
* For example, to parse a string you can use `between(match('"'), contentParser)`
* @param wrapper the wrapper parser to match before and after the content
* @param content the parser responsible to match the content
* @return a parser which consider only the wrapped content
*/
export declare function between(wrapper: Parser, content: Parser): Parser;
/**
* Create a parser which check the following syntax:
* `left content rigth` from the given parsers.
* The resultant parser will consider only the output of the
* content parser.\
* For example, to parse an array you can use `between(match('['), valueParser, match(']'))`
* @param left the parser to match before the content
* @param content the parser responsible to match the content
* @param right the parser to match after the content
* @return a parser which consider only the wrapped content
*/
export declare function between(left: Parser, content: Parser, right: Parser): Parser;
/**
* Generate an high-order function which creates a
* between expression for a parser. This function
* returns another function which takes the content
* parser as argument and generate a parser which will
* match the following syntax: `wrapper content wrapper`.
* The resultant parser will consider only the output of the
* content parser.\
* For example, to parse a string you can use
* ```js
* const contentParser = letters; // or whatever
* const betweenQuotes = parsebetween(match('"'));
* const stringParser = betweenQuotes(contentParser);
* ```
* @param wrapper the wrapper parser to match before and after the content
* @return an high-order function which generate a between parser expression
*/
export declare function parseBetween(wrapper: Parser): (content: Parser) => Parser;
/**
* Generate an high-order function which creates a
* between expression for a parser. This function
* returns another function which takes the content
* parser as argument and generate a parser which will
* match the following syntax: `left content right`.
* The resultant parser will consider only the output of the
* content parser.\
* For example, to parse a simple numeric array you can use:
* ```js
* const contentParser = sequence(digits, match(',')); // or whatever
* const betweenBrackets = parsebetween(match('['));
* const arrayParser = betweenBrackets(contentParser);
* ```
* @param left the parser to match before the content
* @param right the parser to match after the content
* @return an high-order function which generate a between parser expression
*/
export declare function parseBetween(left: Parser, right: Parser): (content: Parser) => Parser;
/**
* Generate a parser which tries to match one or more
* times the following syntax: `(content separator)* content`.
* The resultant parser will consider only the output of
* the content parser.\
* For example, to parse a comma-separated list you can use:
* `separated(match(','), contentPasrer)`.
* @param separator the parser that matches separator
* @param content the parser that match the content
* @returns a parser which generate a separated expression
*/
export declare function separated(separator: Parser, content: Parser): Parser;
/**
* Generate an high-order function which creates a
* separated expression for a parser. This function
* returns another function which takes the content
* parser as argument and generate a parser which will
* match the following syntax: `(content separator)* content`.
* The resultant parser will consider only the output of the
* content parser.\
* For example, to parse a comma-separated list you can use:
* ```js
* const contentParser = letters; // or whatever
* const commaSeparated = parseSeparated(match(','));
* const commaSeparatedParser = commaSeparated(contentParser);
* ```
* @param separator the parser that matches separator
* @return an high-order function which generate a between parser expression
*/
export declare function parseSeparated(separator: Parser): (content: Parser) => Parser;
/**
* Generate a parser which will use the given thunk lazily.
* This is helpful if you need to use parsers which depends one
* on the other:
* ```js
* const parserA = lazy(() => sequence([ parserB, digits, parserB ]));
* const parserB = lazy(() => oneOf([ parserA, digits, letters ]));
* ```
* @param thunk the lazy evaluated function which produce a parser
* @param notCached wether to cache or not the output of the thunk (default: `false --> cached`)
* @returns a parser which evaluates lazily the given thunk
*/
export declare function lazy(thunk: () => P, notCached?: boolean): P;
/**
* Create a parser which always produces an error state
* @param message the message of the error state
* @returns a failure parser
*/
export declare function fail(message: E): Parser;
/**
* Create a parser which always produces a success parser state
* @param value the result value of the success state
* @returns a success parser
*/
export declare function success(value: T): Parser;
type ContextualOneOf = P extends Parser ? R : P extends Parser | infer Rest ? Rest extends Parser ? ContextualOneOf extends never ? never : R | ContextualOneOf : never : never;
type ContextualCB = () => Generator
| R, ContextualOneOf
| R>;
/**
* Create a parser which automatically chain the parsers produced
* by a generator function, yielding their parserd results states.\
* For example:
* ```js
* const declTypeParser = combo.oneOf(
* combo.match('VAR '),
* combo.match('GLOBAL_VAR ')
* );
* const typeParser = combo.oneOf(
* combo.match(' INT '),
* combo.match(' STRING '),
* combo.match(' BOOL ')
* ).map(v => v.trim().toLowerCase());
* const stringParser = combo.between(combo.match('"'), combo.letters);
* const numParser = combo.digits.map(Number);
* const boolParser = combo.oneOf(combo.match('true'), combo.match('false')).map(v => v === 'true');
* const parser = combo.contextual(function*() { // Note that we use a generator function here
* const declarationType = yield declTypeParser; // yielding parser => returning 'var' | 'global_var'
* const varName = yield combo.letters; // returning string
* const type = yield typeParser; // returning 'int' | 'string' | 'bool'
* let data;
* switch (type) {
* case 'int': data = yield numParser; break;
* case 'string': data = yield stringParser; break;
* case 'bool': data = yield boolParser; break;
* }
* return { varName: varName, data, type: type.trim().toLowerCase(), declarationType: declarationType.trim().toLowerCase() };
* });
* parser('VAR theAnswer INT 42'); // { varName: 'theAnswer', data: 42, type: 'int', declarationType: 'var' }
* parser('GLOBAL_VAR greeting STRING "Hello"'); // { varName: 'greetubg', data: 'Hello', type: 'string', declarationType: 'global_var' }
* parser('VAR skyIsBlue BOOL true'); // { varName: 'skyIsBlue', data: true, type: 'bool', declarationType: 'var' }
* ```
* @param generator the function that generates the parsers to chain
* @returns a automatically chained parser
*/
export declare function contextual
(generator: ContextualCB
): Parser;
export declare namespace binary {
/**
* Create a binary parser which check if the current state
* starts with the given binary expression
* @param expression the string expression to match
* @param expressionOffset the offset where the considered expression starts
* @param expressionLength the length of considered expression
* @return a binary parser which check if the current state starts with the given bianry expression
*/
export function match(expression: Binariable, expressionOffset?: number, expressionLength?: number): BinaryParser;
/** A binary parser which consumes a buffer bit by bit */
export const bit: BinaryParser<0 | 1, string>;
/** A binary parser which consume a buffer bit by bit, expecting an unsetted bit (0) */
export const zero: BinaryParser<0, string>;
/** A binary parser which consume a buffer bit by bit, expecting a setted bit (1) */
export const one: BinaryParser<1, string>;
/**
* Create a binary parser which read an unsigned integer from
* a fixed number of bits into a {@link Number}. The number of bits must not exceed
* 32. For integers longer than 32 bits use {@link biguint}
* @param bits the number of bits to read (1 <= bits <= 32)
* @param bigEndian wether to read the number in Big Endian or Little Endian mode
* @returns a binary parser which read an unsigned integer with the given specs
*/
export function uint(bits: number, bigEndian?: boolean): BinaryParser;
/** A binary parser which reads an unsigned 8-bit (1 byte) integer as {@link Number} */
export const uint8: BinaryParser;
/** A binary parser which reads an unsigned 8-bit (1 byte) integer as {@link Number}, Big Endian mode */
export const uint8BE: BinaryParser;
/** A binary parser which reads an unsigned 16-bit (2 byte) integer as {@link Number} */
export const uint16: BinaryParser;
/** A binary parser which reads an unsigned 16-bit (2 byte) integer as {@link Number}, Big Endian mode */
export const uint16BE: BinaryParser;
/** A binary parser which reads an unsigned 24-bit (3 byte) integer as {@link Number} */
export const uint24: BinaryParser;
/** A binary parser which reads an unsigned 24-bit (3 byte) integer as {@link Number}, Big Endian mode */
export const uint24BE: BinaryParser;
/** A binary parser which reads an unsigned 32-bit (4 byte) integer as {@link Number} */
export const uint32: BinaryParser;
/** A binary parser which reads an unsigned 32-bit (4 byte) integer as {@link Number}, BigEndian mode */
export const uint32BE: BinaryParser;
/**
* Create a binary parser which read an unsigned integer from
* a fixed number of bits into a {@link BigInt}. The number of
* readable bits is limited by JS environment implementation.
* @param bits the number of bits to read (bits >= 1)
* @param bigEndian wether to read the number in Big Endian or Little Endian mode
* @returns a binary parser which read an unsigned integer with the given specs
*/
export function biguint(bits: number, bigEndian?: boolean): BinaryParser;
/** A binary parser which reads an unsigned 64-bit (8 byte) integer as {@link BigInt} */
export const uint64: BinaryParser;
/** A binary parser which reads an unsigned 64-bit (8 byte) integer as {@link BigInt}, Big Endian mode */
export const uint64BE: BinaryParser;
/** A binary parser which reads an unsigned 128-bit (16 byte) integer as {@link BigInt} */
export const uint128: BinaryParser;
/** A binary parser which reads an unsigned 128-bit (16 byte) integer as {@link BigInt}, Big Endian mode */
export const uint128BE: BinaryParser;
/** A binary parser which reads an unsigned 256-bit (32 byte) integer as {@link BigInt} */
export const uint256: BinaryParser;
/** A binary parser which reads an unsigned 256-bit (32 byte) integer as {@link BigInt}, Big Endian mode */
export const uint256BE: BinaryParser;
/**
* Create a binary parser which read a signed integer from
* a fixed number of bits into a {@link Number}. The number of bits must not exceed
* 32. For integers longer than 32 bits use {@link bigint}
* @param bits the number of bits to read (1 <= bits <= 32)
* @param bigEndian wether to read the number in Big Endian or Little Endian mode
* @returns a binary parser which read a signed integer with the given specs
*/
export function int(bits: number, bigEndian?: boolean): BinaryParser;
/** A binary parser which reads a signed 8-bit (1 byte) integer as {@link Number} */
export const int8: BinaryParser;
/** A binary parser which reads a signed 8-bit (1 byte) integer as {@link Number}, Big Endian mode */
export const int8BE: BinaryParser;
/** A binary parser which reads a signed 16-bit (2 byte) integer as {@link Number} */
export const int16: BinaryParser;
/** A binary parser which reads a signed 16-bit (2 byte) integer as {@link Number}, Big Endian mode */
export const int16BE: BinaryParser;
/** A binary parser which reads a signed 24-bit (3 byte) integer as {@link Number} */
export const int24: BinaryParser;
/** A binary parser which reads a signed 24-bit (3 byte) integer as {@link Number}, Big Endian mode */
export const int24BE: BinaryParser;
/** A binary parser which reads a signed 32-bit (4 byte) integer as {@link Number} */
export const int32: BinaryParser;
/** A binary parser which reads a signed 32-bit (4 byte) integer as {@link Number}, Big Endian mode */
export const int32BE: BinaryParser;
/**
* Create a binary parser which read a signed integer from
* a fixed number of bits into a {@link BigInt}. The number of
* readable bits is limited by JS environment implementation.
* @param bits the number of bits to read (bits >= 1)
* @param bigEndian wether to read the number in Big Endian or Little Endian mode
* @returns a binary parser which read a signed integer with the given specs
*/
export function bigint(bits: number, bigEndian?: boolean): BinaryParser;
/** A binary parser which reads an unsigned 64-bit (8 byte) integer as {@link BigInt} */
export const int64: BinaryParser;
/** A binary parser which reads an unsigned 64-bit (8 byte) integer as {@link BigInt}, Big Endian mode */
export const int64BE: BinaryParser;
/** A binary parser which reads an unsigned 128-bit (16 byte) integer as {@link BigInt} */
export const int128: BinaryParser;
/** A binary parser which reads an unsigned 128-bit (16 byte) integer as {@link BigInt}, Big Endian mode */
export const int128BE: BinaryParser;
/** A binary parser which reads an unsigned 256-bit (32 byte) integer as {@link BigInt} */
export const int256: BinaryParser;
/** A binary parser which reads an unsigned 256-bit (32 byte) integer as {@link BigInt}, Big Endian mode */
export const int256BE: BinaryParser;
type SequenceOfParsersResult = Parsers extends [] ? [] : Parsers extends [BinaryParser] ? [OR] : Parsers extends [BinaryParser, ...infer Rest1] ? Rest1 extends [BinaryParser, ...infer Rest2] ? Rest2 extends BinaryParser[] ? [OR1, OR2, ...SequenceOfParsersResult] : never : never : Parsers extends BinaryParser[] ? OR[] : never;
type SequenceOfParsersError = Parsers extends [] ? never : Parsers extends [BinaryParser] ? OE : Parsers extends [BinaryParser, ...infer Rest1] ? Rest1 extends [BinaryParser, ...infer Rest2] ? Rest2 extends BinaryParser[] ? OE1 | OE2 | SequenceOfParsersError : never : never : Parsers extends BinaryParser[] ? OE : never;
type SequenceOfParsers = SequenceOfParsersResult extends never ? never : SequenceOfParsersError extends never ? never : BinaryParser, SequenceOfParsersError, any, any>;
/**
* Create a binary parser which check the concatenation in
* sequence of the given binary parsers
* @param parsers an array of binary parsers to concatenate
* @return a binary parser that is the concatenation in a sequence
* of the given binary parsers
*/
export function sequence(parsers: Parsers): SequenceOfParsers;
/**
* Create a binary parser which check the concatenation in
* sequence of the given binary parsers
* @param parsers binary parsers to concatenate
* @return a binary parser that is the concatenation in a sequence
* of the given binary parsers
*/
export function sequence(...parsers: Parsers): SequenceOfParsers;
type ParserOneOf = SequenceOfParsersResult extends never ? never : SequenceOfParsersError extends never ? never : Parser>, SequenceOfParsersError, any, any>;
/**
* Create a binary parser which returns the first success state
* from any given binary parsers.
* @param parsers an array of binary parsers to test
* @return a binary parser which test the given binary parsers
*/
export function oneOf(parsers: Parsers): ParserOneOf;
/**
* Create a binary parser which returns the first success state
* from any given binary parsers.
* @param parsers binary parsers to test
* @return a binary parser which test the given binary parsers
*/
export function oneOf(...parsers: Parsers): ParserOneOf;
type ManyOf = P extends BinaryParser ? BinaryParser : never;
/**
* Create a binary parser which tries to match the given binary parser
* as many times as possible
* @param parser the binary parser to test
* @returns a binary parser which tries to match the given binary parser
* as many times as possible
*/
export function many(parser: P): ManyOf
;
/**
* Create a binary parser which match at least one time,
* then as many times as possible, the given binary parser
* @param parser the binary parser to test
* @returns a binary parser which match at least one time,
* then as many times as possible, the given binary parser
*/
export function oneOrMore
(parser: P): ManyOf
;
/**
* Create a binary parser which match the given binary parser
* the given number of times
* @param parser the binary parser to test
* @param times how many exact times the binary parser must match
* @returns a binary parser which match the given binary parser
* the given number of times
*/
export function times
(parser: P, times: number): ManyOf
;
/**
* Create a binary parser which check the following syntax:
* `wrapper content wrapper` from the given binary parsers.
* The resultant binary parser will consider only the output of the
* content binary parser.\
* For example, to parse a binary string enclosed by the sequence `0110` you can use
* `between(sequence(zero, one, one, zero), contentParser)`
* @param wrapper the wrapper binary parser to match before and after the content
* @param content the binary parser responsible to match the content
* @return a binary parser which consider only the wrapped content
*/
export function between(wrapper: BinaryParser, content: BinaryParser): BinaryParser;
/**
* Create a binary parser which check the following syntax:
* `left content rigth` from the given binary parsers.
* The resultant binary parser will consider only the output of the
* content binary parser.\
* For example, to parse a binary string enclosed by the sequence `0110` and `1001` you can use
* `between(sequence(zero, one, one, zero), contentParser, sequence(one, zero, zero, one))`
* @param left the binary parser to match before the content
* @param content the binary parser responsible to match the content
* @param right the binary parser to match after the content
* @return a binary parser which consider only the wrapped content
*/
export function between(left: BinaryParser, content: BinaryParser, right: BinaryParser): BinaryParser;
/**
* Generate an high-order function which creates a
* between expression for a binary parser. This function
* returns another function which takes the content
* binary parser as argument and generate a binary parser which will
* match the following syntax: `wrapper content wrapper`.
* The resultant binary parser will consider only the output of the
* content binary parser.\
* For example, to parse content between the sequence `0110` you can use
* ```js
* const contentParser = uint8; // or whatever
* const betweenSequence = parsebetween(sequence(zero, one, one, zero));
* const myParser = betweenSequence(contentParser);
* ```
* @param wrapper the wrapper binary parser to match before and after the content
* @return an high-order function which generate a binary between parser expression
*/
export function parseBetween(wrapper: BinaryParser): (content: BinaryParser) => BinaryParser;
/**
* Generate an high-order function which creates a
* between expression for a binary parser. This function
* returns another function which takes the content
* binary parser as argument and generate a binary parser which will
* match the following syntax: `left content right`.
* The resultant binary parser will consider only the output of the
* content binary parser.\
* For example, to parse content between the sequence `0110` and `1001` you can use
* ```js
* const contentParser = uint8; // or whatever
* const betweenSequence = parsebetween(sequence(zero, one, one, zero), sequence(one, zero, zero, one));
* const myParser = betweenSequence(contentParser);
* ```
* @param left the parser to match before the content
* @param right the parser to match after the content
* @return an high-order function which generate a between parser expression
*/
export function parseBetween(left: BinaryParser, right: BinaryParser): (content: BinaryParser) => BinaryParser;
/**
* Generate a binary parser which tries to match one or more
* times the following syntax: `(content separator)* content`.
* The resultant binary parser will consider only the output of
* the content binary parser.\
* For example, to a list separated by the sequence `000` you can use:
* `separated(sequence(zero, zero, zero), contentPasrer)`.
* @param separator the binary parser that matches separator
* @param content the binary parser that match the content
* @returns a binary parser which generate a separated expression
*/
export function separated(separator: BinaryParser, content: BinaryParser): BinaryParser;
/**
* Generate an high-order function which creates a
* separated expression for a binary parser. This function
* returns another function which takes the content
* binary parser as argument and generate a binary parser which will
* match the following syntax: `(content separator)* content`.
* The resultant binary parser will consider only the output of the
* content binary parser.\
* For example, to parse a list which elements are separated by `0` you can use:
* ```js
* const contentParser = uint8; // or whatever
* const zeroSeparated = parseSeparated(zero);
* const zeroSeparatedParser = zeroSeparated(contentParser);
* ```
* @param separator the binary parser that matches separator
* @return an high-order function which generate a binary between parser expression
*/
export function parseSeparated(separator: BinaryParser): (content: BinaryParser) => BinaryParser;
/**
* Generate a binary parser which will use the given thunk lazily.
* This is helpful if you need to use binary parsers which depends one
* on the other:
* ```js
* const parserA = lazy(() => sequence(parserB, zero, zero, parserB));
* const parserB = lazy(() => oneOf(parserA, sequence(zero, zero, one), sequence(one, one, zero)));
* ```
* @param thunk the lazy evaluated function which produce a binary parser
* @param notCached wether to cache or not the output of the thunk (default: `false --> cached`)
* @returns a binary parser which evaluates lazily the given thunk
*/
export function lazy(thunk: () => P, notCached?: boolean): P;
/**
* Create a binary parser which always produces an error state
* @param message the message of the error state
* @returns a failure binary parser
*/
export function fail(message: E): BinaryParser;
/**
* Create a binary parser which always produces a success binary parser state
* @param value the result value of the success state
* @returns a success binary parser
*/
export function success(value: T): BinaryParser;
type ContextualOneOf = P extends BinaryParser ? R : P extends BinaryParser