cm-tarnation
Version:
An alternative parser for CodeMirror 6
58 lines (57 loc) • 2.44 kB
TypeScript
import type { MatchOutput } from "../../types";
import type * as DF from "../definition";
import { Matched } from "../matched";
import { RegExpMatcher } from "../matchers/regexp";
import { Node } from "../node";
import type { Repository } from "../repository";
import type { GrammarState } from "../state";
/**
* A {@link Node} with some sort of associated pattern. Patterns exist as
* subclasses of this class.
*/
export declare abstract class Rule {
/** The name of this rule. May be different to what is emitted to the AST. */
name: string;
/** The {@link Node} associated with this rule. */
node: Node;
/** If given, this function will be checked prior to matching. */
lookbehind?: (str: string, pos: number) => boolean;
/** If given, this {@link RegExpMatcher} will be checked after matching. */
lookahead?: RegExpMatcher;
/**
* A list of "context setters", which are functions that update the
* {@link GrammarState} context table.
*/
contextSetters?: ((state: GrammarState) => void)[];
/** If true, all of the context setters will be fired before this rule is checked. */
contextImmediate?: boolean;
/**
* If the underlying pattern emits captures, this list of {@link Node} or
* `CaptureFunction` objects will be associated 1-1 with each capture.
*/
captures: (Node | CaptureFunction)[];
/**
* If true, this rule won't actually emit tokens based off of matches,
* and will only cause side-effects like state switching.
*/
rematch?: boolean;
/**
* @param repo - The {@link Repository} to add this rule to.
* @param rule - The rule definition.
*/
constructor(repo: Repository, rule: DF.Rule);
/**
* Function that subclasses must implement. The `state` argument is given
* last, unlike usual, so that it can be ignored if the subclass doesn't
* need access to the grammar's state.
*/
abstract exec(str: string, pos: number, state: GrammarState): Matched | MatchOutput;
/**
* @param state - The current {@link GrammarState}.
* @param str - The string to match.
* @param pos - The position to start matching at.
*/
match(state: GrammarState, str: string, pos: number): Matched | null;
}
declare type CaptureFunction = (state: GrammarState, capture: string) => Node | boolean;
export {};