cm-tarnation
Version:
An alternative parser for CodeMirror 6
81 lines (80 loc) • 3.26 kB
TypeScript
import type { GrammarState } from "../grammar/state";
import type { GrammarToken } from "../types";
import { Chunk } from "./chunk";
/**
* A `ChunkBuffer` stores `Chunk` objects that then store the actual tokens
* emitted by tokenizing. The creation of `Chunk` objects is fully
* automatic, all the parser needs to do is push the tokens to the buffer.
*
* Storing chunks instead of tokens allows the buffer to more easily manage
* a large number of tokens, and more importantly, allows the parser to use
* chunks as checkpoints, enabling it to only tokenize what it needs to and
* reuse everything else.
*/
export declare class ChunkBuffer {
/** The actual array of chunks that the buffer manages. */
chunks: Chunk[];
/** @param chunks - The chunks to populate the buffer with. */
constructor(chunks?: Chunk[]);
/** The last chunk in the buffer. */
get last(): Chunk | null;
/** The last chunk in the buffer. */
set last(chunk: Chunk | null);
/** Ensures there is at least one chunk in the buffer. */
ensureLast(pos: number, state: GrammarState): void;
/** Retrieves a `Chunk` from the buffer. */
get(index: number): Chunk | null;
/**
* Adds tokens to the buffer, splitting them automatically into chunks.
* Returns true if a new chunk was created.
*
* @param state - The state to be cached.
* @param token - The token to add to the buffer.
*/
add(state: GrammarState, token: GrammarToken): boolean;
/**
* Splits the buffer into a left and right section. The left section
* takes the indexed chunk, which will have its tokens cleared.
*
* @param index - The chunk index to split on.
*/
split(index: number): {
left: ChunkBuffer;
right: ChunkBuffer;
};
/**
* Offsets every chunk's position whose index is past or at the given index.
*
* @param index - The index the slide will start at.
* @param offset - The positional offset that is applied to the chunks.
* @param cutLeft - If true, every chunk previous to `index` will be
* removed from the buffer.
*/
slide(index: number, offset: number, cutLeft?: boolean): this;
/**
* Links another `ChunkBuffer` to the end of this buffer.
*
* @param right - The buffer to link.
* @param max - If given, the maximum size of the buffer (by document
* position) will be clamped to below this number.
*/
link(right: ChunkBuffer, max?: number): this;
/** Binary search comparator function. */
private searchCmp;
/**
* Searches for the closest chunk to the given position.
*
* @param pos - The position to find.
* @param side - The side to search on. -1 is left (before), 1 is right
* (after). 0 is the default, and it means either side.
* @param precise - If true, the search will require an exact hit. If the
* search misses, it will return `null` for both the token and index.
*/
search(pos: number, side?: 1 | 0 | -1, precise?: boolean): {
chunk: null;
index: null;
} | {
chunk: Chunk;
index: number;
};
}