cm-tarnation
Version:
An alternative parser for CodeMirror 6
103 lines (102 loc) • 3.71 kB
TypeScript
import type { Tag as cmTag, tags as cmTags } from "@lezer/highlight";
/** Filters a record for any properties which are equivalent to a given type. */
declare type FilterFor<O extends Record<string, any>, T> = {
[Property in keyof O as O[Property] extends T ? Property : never]: O[Property];
};
/** Filters out of a record any properties which are equivalent to a given type. */
declare type FilterOut<O extends Record<string, any>, T> = {
[Property in keyof O as O[Property] extends T ? never : Property]: O[Property];
};
/** A type which may be an array of itself, or just itself. */
declare type Arrayable<T> = T | T[];
export interface Grammar {
comments?: {
block?: {
open: string;
close: string;
};
line?: string;
};
closeBrackets?: {
brackets?: string[];
before?: string;
};
indentOnInput?: Regex;
wordChars?: string;
ignoreCase?: boolean;
default?: Node;
repository?: Record<string, RepositoryItem>;
includes?: Record<string, string[]>;
global?: Inside;
root: Inside;
}
export declare type RepositoryItem = Regex | Node | ReuseNode | Rule | State;
export declare type Rule = Lookup | Pattern | Chain;
export interface Node {
type?: string;
open?: string;
close?: string;
emit?: string | boolean;
nest?: string;
tag?: Tag;
openedBy?: Arrayable<string>;
closedBy?: Arrayable<string>;
group?: Arrayable<string>;
autocomplete?: true | string;
fold?: boolean | "inside" | "past_first_line" | `offset(${number}, ${number})`;
indent?: "flat" | `delimited(${string})` | "continued" | `continued(${Regex})` | `add(${number})` | `set(${number})`;
}
export interface State extends Node {
begin: string | Rule;
end: string | Rule;
inside?: Inside | Node | ReuseNode | "inherit" | "loose";
}
export interface RuleOptions extends Node {
captures?: Record<string, Node | ReuseNode | CaptureCondition>;
context?: Arrayable<ContextSetter>;
contextImmediate?: boolean;
lookbehind?: LookbehindSource;
lookahead?: Regex;
rematch?: boolean;
}
export interface Lookup extends RuleOptions {
lookup: string[] | VarIndex;
}
export interface Pattern extends RuleOptions {
match: Arrayable<string | Regex | VarIndex>;
}
export interface Chain extends RuleOptions {
chain: string[];
skip?: Regex;
}
export interface ContextSetter {
if?: MatchIndex;
matches?: string | Regex | VarIndex;
set: string;
to: string | MatchIndex | null;
}
export interface CaptureCondition {
if?: MatchIndex;
matches: string | Regex | VarIndex | MatchIndex;
then?: Node | ReuseNode;
else?: Node | ReuseNode;
}
export declare type ReuseNode = {
is: string;
};
export declare type Inside = (string | Rule | Include | State)[];
export declare type Include = {
include: string;
};
export declare type StyleTag = keyof FilterOut<typeof cmTags, (tag: cmTag) => cmTag>;
export declare type FunctionTag = keyof FilterFor<typeof cmTags, (tag: cmTag) => cmTag>;
export declare type TagModifier = `(${`${string}/` | "!" | "..."}) ` | "";
export declare type TagFunction = `${FunctionTag}(${StyleTag})`;
export declare type Tag = `${TagModifier}${string}`;
export declare type ChainItem = `${string}${"?" | "*" | "+" | ""}`;
export declare type Regex = `/${string}/${string}`;
export declare type LookbehindSource = `${"!" | ""}${Regex}`;
export declare type MatchIndex = `$${number}`;
export declare type VarIndex = `$var:${string}`;
export declare type ContextIndex = `$ctx:${string}`;
export {};