/** * The type which includes all nodes. */ export type Node = BranchNode | LeafNode /** * The type which includes all branch nodes. */ export type BranchNode = | RegExpLiteral | Pattern | Alternative | Group | CapturingGroup | Quantifier | CharacterClass | LookaroundAssertion | CharacterClassRange /** * The type which includes all leaf nodes. */ export type LeafNode = | BoundaryAssertion | CharacterSet | Character | Backreference | Flags /** * The type which includes all atom nodes. */ export type Element = Assertion | Quantifier | QuantifiableElement /** * The type which includes all atom nodes that Quantifier node can have as children. */ export type QuantifiableElement = | Group | CapturingGroup | CharacterClass | CharacterSet | Character | Backreference // Lookahead assertions is quantifiable in Annex-B. | LookaheadAssertion /** * The type which includes all character class atom nodes. */ export type CharacterClassElement = | EscapeCharacterSet | UnicodePropertyCharacterSet | Character | CharacterClassRange /** * The type which defines common properties for all node types. */ export interface NodeBase { /** The node type. */ type: Node["type"] /** The parent node. */ parent: Node["parent"] /** The 0-based index that this node starts. */ start: number /** The 0-based index that this node ends. */ end: number /** The raw text of this node. */ raw: string } /** * The root node. */ export interface RegExpLiteral extends NodeBase { type: "RegExpLiteral" parent: null pattern: Pattern flags: Flags } /** * The pattern. */ export interface Pattern extends NodeBase { type: "Pattern" parent: RegExpLiteral | null alternatives: Alternative[] } /** * The alternative. * E.g. `a|b` */ export interface Alternative extends NodeBase { type: "Alternative" parent: Pattern | Group | CapturingGroup | LookaroundAssertion elements: Element[] } /** * The uncapturing group. * E.g. `(?:ab)` */ export interface Group extends NodeBase { type: "Group" parent: Alternative | Quantifier alternatives: Alternative[] } /** * The capturing group. * E.g. `(ab)`, `(?ab)` */ export interface CapturingGroup extends NodeBase { type: "CapturingGroup" parent: Alternative | Quantifier name: string | null alternatives: Alternative[] references: Backreference[] } /** * The lookaround assertion. */ export type LookaroundAssertion = LookaheadAssertion | LookbehindAssertion /** * The lookahead assertion. * E.g. `(?=ab)`, `(?!ab)` */ export interface LookaheadAssertion extends NodeBase { type: "Assertion" parent: Alternative | Quantifier kind: "lookahead" negate: boolean alternatives: Alternative[] } /** * The lookbehind assertion. * E.g. `(?<=ab)`, `(?` */ export interface Backreference extends NodeBase { type: "Backreference" parent: Alternative | Quantifier ref: number | string resolved: CapturingGroup } /** * The flags. */ export interface Flags extends NodeBase { type: "Flags" parent: RegExpLiteral | null dotAll: boolean global: boolean hasIndices: boolean ignoreCase: boolean multiline: boolean sticky: boolean unicode: boolean }