///
export interface Source {
/**
Read `length` bytes from the underlying source starting from `position`,
into the given `buffer` starting at `offset`, returning the number of bytes
read.
*/
read(buffer: Buffer, offset: number, length: number, position: number): number;
/**
Read `length` bytes of the underlying file as a Buffer. May return a
Buffer shorter than `length` iff EOF has been reached.
*/
readBuffer(length: number, position: number): Buffer;
/**
Return the total number of bytes in the underlying source.
*/
size: number;
}
/**
Any sort of sequence can implement Iterable. It's a lot like Array, but
read-only, and without random access.
*/
export interface Iterable {
next(): T;
}
export interface StatefulIterable extends Iterable {
position: number;
size: number;
peek(): T;
skip(): boolean;
}
/**
In some cases, it makes more sense to iterate in batches, or chunks, through an
iterable of a particular type. The T in ChunkedIterable should itself be a
sequence type, like string[] or Buffer (which effectively extends number[]).
*/
export interface ChunkedIterable {
/** Read the next {length} items from the underlying resource, and advance
the cursor accordingly. The returned chunk may be shorter than {length} if
EOF is reached. */
next(length: number): T;
}
export interface StatefulChunkedIterable extends ChunkedIterable {
/** The cursor, i.e., the current position within the underlying resource. */
position: number;
/** The total size of the underlying resource. */
size: number;
/** Return the next {length} items from the underlying resource but do not
advance the cursor. */
peek(length: number): T;
/** Advance the cursor over the next {length} items in the underlying
resource, returning the number of items actually passed over (which may be
less than {length} if the end of the resource has been reached). */
skip(length: number): number;
}
/**
A stateful representation of some resource that produces Buffer chunks.
*/
export declare type BufferIterable = StatefulChunkedIterable;
/**
Wraps a Buffer as a stateful iterable.
*/
export declare class BufferIterator implements BufferIterable {
private _buffer;
position: number;
constructor(_buffer: Buffer, position?: number);
static fromString(str: string, encoding?: BufferEncoding): BufferIterator;
/**
Return the total length of the underlying Buffer.
*/
get size(): number;
/**
Read the next `length` bytes from the underlying Buffer, or fewer iff we reach
EOF, without advancing our position within the Buffer. Returns a Buffer slice.
*/
peek(length: number): Buffer;
/**
Read the next `length` bytes from the underlying Buffer, or fewer iff we reach
EOF, and advance our position within the Buffer. Returns a Buffer slice.
Buffer#slice never returns entries beyond the end of the buffer:
`Buffer.from([1, 2, 3, 4]).slice(2, 10)` produces ``
*/
next(length: number): Buffer;
/**
Skip over the next `length` characters, returning the number of skipped
characters (which may be < `length` iff EOF has been reached).
We do not allow skipping beyond the end of the buffer.
*/
skip(length: number): number;
}
export interface StringIterable extends StatefulChunkedIterable {
}
/**
Wraps a string as a stateful iterable.
*/
export declare class StringIterator implements StringIterable {
private _string;
position: number;
constructor(_string: string, position?: number);
static fromBuffer(buffer: Buffer, encoding?: BufferEncoding, start?: number, end?: number): StringIterator;
/**
Return the total length of the underlying Buffer.
*/
get size(): number;
/**
Read the next `length` characters from the underlying string, or fewer iff
we reach EOF, without advancing our position within the string.
*/
peek(length: number): string;
/**
Read the next `length` characters from the underlying string, or fewer iff
we reach EOF, and advance our position within the string.
*/
next(length: number): string;
/**
Skip over the next `length` characters, returning the number of skipped
characters (which may be < `length` iff EOF has been reached).
We do not allow skipping beyond the end of the string.
*/
skip(length: number): number;
}
/**
Wrap an Array as an iterable.
*/
export declare class ArrayIterator implements StatefulIterable {
private _array;
position: number;
constructor(_array: Array, position?: number);
get size(): number;
next(): T;
peek(): T;
skip(): boolean;
}
/**
Provide buffered access to a stream of bytes, e.g., a file.
It is buffered, which means you can call `peek(same_number)` repeatedly without
triggering a `read(2)` system call on the underlying file each time. Likewise,
calling `read(small_number)` repeatedly will issue a `read(2)` system call only
when the buffer doesn't have enough data.
When calling `read()` on the underlying file, it will read in batches of
`_block_size` (default: 1024) bytes.
*/
export declare class BufferedSourceReader {
private _source;
private _position;
private _block_size;
protected _buffer: Buffer;
constructor(_source: Source, _position?: number, _block_size?: number);
/**
@returns {number} The position (byte offset) in the file that would be read from if we called read(...). This is different from the internally-held position, which points to the end of the currently held buffer.
*/
get position(): number;
/**
@returns {number} The total size (in bytes) of the underlying file.
*/
get size(): number;
/**
Read data from the underlying file and append it to the buffer.
@param {number} length The number of bytes to consume and append to the buffer.
@returns {boolean} If the read operation reads fewer than the requested bytes, returns false, usually signifying that EOF has been reached. Returns true if it seems that there is more available data.
*/
private _fillBuffer;
/**
Read from the underlying file, appending to the currently held buffer,
until the given predicate function returns false. That function will be called
repeatedly with no arguments. If it returns false the first time it is called,
nothing will be read.
@param {Function} predicate A function that takes a Buffer and returns true if it's long enough.
@returns {void} This may return without the condition being met, if the end of the underlying file has been reached.
*/
protected _readWhile(predicate: (buffer: Buffer) => boolean): void;
/**
Read from the underlying source until we have at least `length` bytes in the buffer.
@param {number} length The number of bytes to return without consuming. This is an upper bound, since the underlying source may contain fewer than the desired bytes.
*/
protected _peekBytes(length: number): Buffer;
/**
Like _peekBytes(length), but consumes the bytes, so that subsequent calls return subsequent chunks.
@param {number} length The number of bytes to return (upper bound).
*/
protected _nextBytes(length: number): Buffer;
/**
Like _nextBytes(length), but doesn't ever slice off a buffer to hold the skipped bytes.
@param {number} length The number of bytes that were skipped (consumed without returning), which may be fewer than `length` iff EOF has been reached.
*/
protected _skipBytes(length: number): number;
}
export declare class SourceBufferIterator extends BufferedSourceReader implements BufferIterable {
constructor(source: Source, position?: number, block_size?: number);
next: (length: number) => Buffer;
peek: (length: number) => Buffer;
skip: (length: number) => number;
}
export declare class SourceStringIterator extends BufferedSourceReader implements StringIterable {
private _encoding;
constructor(source: Source, _encoding: BufferEncoding, position?: number, block_size?: number);
peek(length: number): string;
next(length: number): string;
/**
Skip over the next `length` characters, returning the number of skipped
characters (which may be < `length` iff EOF has been reached).
*/
skip(length: number): number;
nextBytes: (length: number) => Buffer;
peekBytes: (length: number) => Buffer;
skipBytes: (length: number) => number;
}
/**
Tokenizer#map() and Combiner#map() both return Token iterators.
Tokens with a null name and null Tokens should be treated the same way (as
insignificant / ignorable objects that should be skipped).
Tokens with an undefined name and undefined Tokens are always errors.
*/
export interface Token {
name: string;
value: T;
}
/**
Another generic but frequently used alias.
*/
export interface TokenIterable extends Iterable> {
}
export declare function Token(name: string, value?: T): Token;
export interface RegexAction {
(match: RegExpMatchArray): Token;
}
export interface RegexRule extends Array> {
0: RegExp;
1: RegexAction;
}
/**
The type T is the type of each token value, usually `any` (the token name is
always a string).
*/
export declare class Tokenizer {
private default_rules;
private state_rules;
peek_length: number;
constructor(default_rules: RegexRule[], state_rules?: {
[index: string]: RegexRule[];
}, peek_length?: number);
getRules(state_name: string): RegexRule[];
/**
Create a closure around the iterable.
Unfortunately, it seems that TypeScript doesn't like inline functions, so we
use a helper class (TokenizerIterator).
*/
map(iterable: StringIterable, states?: string[]): TokenIterable;
}
export interface CombinerAction {
(tokens: Token[]): Token;
}
export interface CombinerRule extends Array> {
0: string;
1: CombinerAction;
}
/**
Recombine a stream of tokens using a stack of lists, e.g.,
WORD:BT START:STRING CHAR:A CHAR:b CHAR:c END:STRING WORD:ET
becomes:
WORD:BT STRING:Abc WORD:ET
*/
export declare class Combiner {
private rules;
constructor(rules: CombinerRule[]);
findRule(name: string): CombinerRule;
map(iterable: TokenIterable, stack?: Array>>): TokenIterable;
}
export interface MachineCallback {
(match?: RegExpMatchArray): T;
}
export declare type MachineRule = [RegExp, MachineCallback];
export declare function MachineRule(regexp: RegExp, callback: MachineCallback): MachineRule;
export interface MachineStateConstructor {
new (iterable: StringIterable, peek_length?: number): MachineState;
}
/**
Every MachineState should declare a list of rules, at least one of which should
call this.pop() or return a value.
`T` is the result Type
`I` is the internal Type
*/
export declare class MachineState {
protected iterable: StringIterable;
protected peek_length: number;
/** An internal value, which is incrementally built based on the input. */
protected value: I;
/**
Each MachineRule maps a string pattern to an instance method, which returns
a value of type T (or null). If a rule matches the input and the corresponding
instance method returns a non-null value, we should exit (pop) this state by
returning from read().
*/
protected rules: MachineRule[];
constructor(iterable: StringIterable, peek_length?: number);
private get name();
/**
pop() returns the value of this state. When used as a rule's callback, this
consumes nothing from the input iterable, but triggers the end of this state
by returning a value.
*/
pop(): T;
/**
ignore() returns undefined, which instructs the state to keep parsing.
*/
ignore(): T;
attachState(SubState: MachineStateConstructor): MachineState;
/**
This derives a value of type T from the input, terminating with the first rule
that returns a value.
*/
read(): T;
}