UNPKG

cm-tarnation

Version:

An alternative parser for CodeMirror 6

73 lines (72 loc) 2.79 kB
import type { GrammarState } from "../grammar/state"; import { Token } from "../token"; import type { GrammarToken } from "../types"; import type { ParseStack } from "./parsing"; /** * A `Chunk` stores tokens emitted by the tokenization into discrete, well, * chunks. Chunks store their token positions (as in, position in the * document) relative to the chunk's own "position". This allows the chunk * to be moved anywhere in the document, and have the tokens follow. What * this means is that a chunk can be adjusted slightly forward or back as * the document changes, allowing for them to be reused when tokenizing. */ export declare class Chunk { /** The chunk's starting position. */ private _pos; /** The chunk's relative extent, as determined from the positions of its tokens. */ private _max; /** The tokens stored in this chunk. */ tokens: Token[]; /** State at the start of this chunk. */ state: GrammarState; /** * If this chunk has been parsed, this property will have the result of * that parse cached. */ parsed: null | { /** Parsed tokens from this chunk. */ tokens: ArrayBuffer; /** Stack state at the end of this chunk. */ stack: ParseStack; }; /** * @param pos - Position of this chunk. * @param state - The state for the start of this chunk. * @param tokens - The grammar tokens to store in this chunk. */ constructor(pos: number, state: GrammarState, tokens?: GrammarToken[]); /** The chunk's starting position. */ get pos(): number; /** The chunk's starting position. */ set pos(pos: number); /** The chunk's maximum extent, as determined from the positions of its tokens. */ get max(): number; /** * Adds a token to the chunk. * * @param token - The token to add. */ add(token: GrammarToken): void; /** * Sets the chunk's tokens. * * @param tokens - The tokens to add. */ setTokens(tokens: GrammarToken[]): void; /** * Returns a deep clone of the chunk. * * @param withTokens - If false, the tokens will be omitted. Defaults to true. */ clone(withTokens?: boolean): Chunk; /** * Determines if a grammar's state (and parse position) is compatible * with reusing this node. This is only a safe determination if it is * made _after_ the changed range of the document. * * @param state - The state to compare against. * @param pos - The position to compare against. * @param offset - The edit offset, to correct for chunk position differences. */ isReusable(state: GrammarState, pos: number, offset?: number): boolean; }