/** * The type which includes all nodes. */ export type Node = BranchNode | LeafNode /** * The type which includes all branch nodes. */ export type BranchNode = | Alternative | CapturingGroup | CharacterClass | CharacterClassRange | ClassIntersection | ClassStringDisjunction | ClassSubtraction | ExpressionCharacterClass | Group | LookaroundAssertion | Modifiers | Pattern | Quantifier | RegExpLiteral | StringAlternative /** * The type which includes all leaf nodes. */ export type LeafNode = | Backreference | BoundaryAssertion | Character | CharacterSet | Flags | ModifierFlags /** * The type which includes all atom nodes. */ export type Element = Assertion | QuantifiableElement | Quantifier /** * The type which includes all atom nodes that Quantifier node can have as children. */ export type QuantifiableElement = | Backreference | CapturingGroup | Character | CharacterClass | CharacterSet | ExpressionCharacterClass | Group | LookaheadAssertion /** * The type which includes all character class atom nodes. */ export type CharacterClassElement = | ClassRangesCharacterClassElement | UnicodeSetsCharacterClassElement export type ClassRangesCharacterClassElement = | Character | CharacterClassRange | CharacterUnicodePropertyCharacterSet | EscapeCharacterSet export type UnicodeSetsCharacterClassElement = | Character | CharacterClassRange | ClassStringDisjunction | EscapeCharacterSet | ExpressionCharacterClass | UnicodePropertyCharacterSet | UnicodeSetsCharacterClass /** * 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: CapturingGroup | Group | LookaroundAssertion | Pattern elements: Element[] } /** * The uncapturing group. * E.g. `(?:ab)` */ export interface Group extends NodeBase { type: "Group" parent: Alternative | Quantifier modifiers: Modifiers | null 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 type Backreference = AmbiguousBackreference | UnambiguousBackreference interface BaseBackreference extends NodeBase { type: "Backreference" parent: Alternative | Quantifier ref: number | string ambiguous: boolean resolved: CapturingGroup | CapturingGroup[] } export interface AmbiguousBackreference extends BaseBackreference { ref: string ambiguous: true resolved: CapturingGroup[] } export interface UnambiguousBackreference extends BaseBackreference { ambiguous: false resolved: CapturingGroup } /** * The modifiers. */ export interface Modifiers extends NodeBase { type: "Modifiers" parent: Group /** * The add modifier flags. */ add: ModifierFlags /** * The remove modifier flags. * * `null` means no remove modifier flags. e.g. `(?ims:x)` * The reason for `null` is that there is no position where the remove modifier flags appears. Must be behind the minus mark. */ remove: ModifierFlags | null } /** * The modifier flags. */ export interface ModifierFlags extends NodeBase { type: "ModifierFlags" parent: Modifiers dotAll: boolean ignoreCase: boolean multiline: boolean } /** * 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 unicodeSets: boolean }