UNPKG

cm-tarnation

Version:

An alternative parser for CodeMirror 6

111 lines (110 loc) 4.23 kB
import { Input, Parser as CodeMirrorParser, PartialParse, Tree, TreeFragment } from "@lezer/common"; import type { TarnationLanguage } from "./language"; /** * Factory for correctly instantiating {@link Parser} instances. To * CodeMirror, this class is the `parser`, and a {@link Parser} is the * running process of said parser. */ export declare class ParserFactory extends CodeMirrorParser { private language; /** A wrapper function that enables mixed parsing. */ private wrapper; /** * @param language - The {@link TarnationLanguage} that this factory * passes to the {@link Parser} instances it constructs. */ constructor(language: TarnationLanguage); createParse(input: Input, fragments: TreeFragment[], ranges: { from: number; to: number; }[]): PartialParse; /** * Special "nest" function provided to the `parseMixed` function. * Determines which nodes indicate a nested parsing region, and if so, * returns a `NestedParser` for said region. */ private nest; } /** * The `Parser` is the main interface for tokenizing and parsing, and what * CodeMirror directly interacts with. * * Additionally, the `Parser` handles the recovery of grammar state from * the stale trees provided by CodeMirror, and then uses this data to * restart tokenization with reused tokens. * * Note that the `Parser` is not persistent a objects It is discarded as * soon as the parse is done. That means that its startup time is very significant. */ export declare class Parser implements PartialParse { /** The host language. */ private language; /** * An object storing details about the region of the document to be * parsed, where it was edited, the length, etc. */ private region; /** The current state of the grammar, such as the stack. */ private state; /** {@link Chunk} buffer, where matched tokens are cached. */ private buffer; private compiler; /** * A buffer containing the stale _ahead_ state of the tokenized output. * As in, when a user makes a change, this is all of the tokenization * data for the previous document after the location of that new change. */ private previousRight?; /** A function used to measure how long the parse is taking. */ private measurePerformance; /** The current position of the parser. */ parsedPos: number; /** * The position the parser will be stopping at early, if given a location * to stop at. */ stoppedAt: number | null; /** The current performance value, in milliseconds. */ performance?: number; /** * @param language - The language containing the grammar to use. * @param input - The input document to parse. * @param fragments - The fragments to be used for determining reuse of * previous parses. * @param ranges - The ranges of the document to parse. */ constructor(language: TarnationLanguage, input: Input, fragments: TreeFragment[], ranges: { from: number; to: number; }[]); /** True if the parser is done. */ get done(): boolean; /** * Notifies the parser to not progress past the given position. * * @param pos - The position to stop at. */ stopAt(pos: number): void; /** Advances tokenization one step. */ advance(): Tree | null; private finish; /** Advances the parser to the next chunk. */ private nextChunk; /** * Tries to reuse a buffer _ahead_ of the current position. Returns true * if this was successful, otherwise false. * * @param right - The buffer to try and reuse. */ private tryToReuse; /** * Returns the first chunk buffer found within a tree, if any. * * @param tree - The tree to search through, recursively. * @param from - The start of the search area. * @param to - The end of the search area. * @param offset - An offset added to the tree's positions, so that they * may match some other source's positions. */ private find; }