UNPKG

cm-tarnation

Version:

An alternative parser for CodeMirror 6

104 lines (103 loc) 4.21 kB
import { Input, NodeProp } from "@lezer/common"; import type { Regex } from "./grammar/definition"; export interface SearchOpts { /** Starting minimum index for the search. */ min?: number; /** Starting maximum index for the search. */ max?: number; /** * If true, the search will return the closest index to the desired value * on failure. */ precise?: boolean; } /** * Performs a binary search through an array. * * The comparator function should return -1 if undershooting the desired * value, +1 if overshooting, and 0 if the value was found. * * The comparator can also short-circuit the search by returning true or * false. Returning true is like returning a 0 (target found), but * returning false induces a null return. */ export declare function search<T, TR>(haystack: T[], target: TR, comparator: (element: T, target: TR) => number | boolean, { min, max, precise }?: SearchOpts): { element: T; index: number; } | { element: null; index: number; } | null; /** Class that implements the Lezer `Input` interface using a normal string. */ export declare class StringInput implements Input { readonly string: string; constructor(string: string); get length(): number; chunk(from: number): string; readonly lineChunks = false; read(from: number, to: number): string; } /** * Safely compiles a regular expression. * * @example * * ```ts * // returns null if features aren't supported (e.g. Safari) * const regex = re`/(?<=\d)\w+/d` * ``` */ export declare function re(str: TemplateStringsArray | string, forceFlags?: string): RegExp | null; /** * Tests if the given string is a "RegExp string", as in it's in the format * of a native `RegExp` statement. */ export declare function isRegExpString(str: string): str is Regex; /** Returns if the given `RegExp` has any remembered capturing groups. */ export declare function hasCapturingGroups(regexp: RegExp): boolean; /** * Creates a lookbehind function from a `RegExp`. This function can only * test for a pattern's (non) existence, so no matches or capturing groups * are returned. * * @param pattern - A `RegExp` to be used as a pattern. * @param negative - Negates the pattern. */ export declare function createLookbehind(pattern: RegExp, negative?: boolean): (str: string, pos: number) => boolean; /** * A special per-node `NodeProp` used for describing nodes where a nested * parser will be embedded. */ export declare const EmbeddedParserProp: NodeProp<string>; /** * Returns a completely concatenated `Int32Array` from a list of arrays. * * @param arrays - Arrays to concatenate. * @param length - If you know the length of the final array, you can pass * it here to avoid having the function calculate it. */ export declare function concatInt32Arrays(arrays: Int32Array[], length?: number): Int32Array; /** * Deduplicates an array. Does not mutate the original array. * * @param arr - The array to deduplicate. * @param insert - Additional values to insert into the array, if desired. */ export declare function dedupe<T extends any[]>(arr: T, ...insert: T): T; /** Performance measuring utility. */ export declare function perfy(): () => number; /** Removes all properties assigned to `undefined` in an object. */ export declare function removeUndefined<T>(obj: T): { [K in keyof T]: Exclude<T[K], undefined>; }; /** Takes a string and escapes any `RegExp` sensitive characters. */ export declare function escapeRegExp(str: string): string; /** Creates a simple pseudo-random ID, with an optional prefix attached. */ export declare function createID(prefix?: string): string; /** Converts a string into an array of codepoints. */ export declare function toPoints(str: string): number[]; /** * Checks an array of codepoints against a codepoint array or a string, * starting from a given position. */ export declare function pointsMatch(points: number[], str: string | number[], pos: number): boolean; /** Very quickly generates a (non-secure) hash from the given string. */ export declare function hash(s: string): number;