index.d.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. declare module 'regexp-tree/ast' {
  2. export interface AstClassMap {
  3. 'RegExp': AstRegExp;
  4. 'Disjunction': Disjunction;
  5. 'Alternative': Alternative;
  6. 'Assertion': Assertion;
  7. 'Char': Char;
  8. 'CharacterClass': CharacterClass;
  9. 'ClassRange': ClassRange;
  10. 'Backreference': Backreference;
  11. 'Group': Group;
  12. 'Repetition': Repetition;
  13. 'Quantifier': Quantifier;
  14. }
  15. export type AstClass = keyof AstClassMap;
  16. export type AstNode = AstClassMap[AstClass];
  17. export type AstNodeLocation = {
  18. line: number;
  19. column: number;
  20. offset: number;
  21. };
  22. export interface Base<T extends AstClass> {
  23. type: T;
  24. loc?: {
  25. source: string;
  26. start: AstNodeLocation;
  27. end: AstNodeLocation;
  28. };
  29. }
  30. export interface SimpleChar extends Base<'Char'> {
  31. value: string;
  32. kind: 'simple';
  33. escaped?: true;
  34. codePoint: number;
  35. }
  36. export interface SpecialChar extends Base<'Char'> {
  37. value: string;
  38. kind: 'meta' | 'control' | 'hex' | 'decimal' | 'oct' | 'unicode';
  39. codePoint: number;
  40. }
  41. export type Char =
  42. | SimpleChar
  43. | SpecialChar;
  44. export interface ClassRange extends Base<'ClassRange'> {
  45. from: Char;
  46. to: Char;
  47. }
  48. export interface CharacterClass extends Base<'CharacterClass'> {
  49. negative?: true;
  50. expressions: (Char | ClassRange)[];
  51. }
  52. export interface Alternative extends Base<'Alternative'> {
  53. expressions: Expression[];
  54. }
  55. export interface Disjunction extends Base<'Disjunction'> {
  56. left: Expression | null;
  57. right: Expression | null;
  58. }
  59. export interface CapturingGroup extends Base<'Group'> {
  60. capturing: true;
  61. number: number;
  62. name?: string;
  63. nameRaw?: string;
  64. expression: Expression | null;
  65. }
  66. export interface NoncapturingGroup extends Base<'Group'> {
  67. capturing: false;
  68. expression: Expression | null;
  69. }
  70. export type Group =
  71. | CapturingGroup
  72. | NoncapturingGroup;
  73. export interface NumericBackreference extends Base<'Backreference'> {
  74. kind: 'number';
  75. number: number;
  76. reference: number;
  77. }
  78. export interface NamedBackreference extends Base<'Backreference'> {
  79. kind: 'name';
  80. number: number;
  81. reference: string;
  82. referenceRaw: string;
  83. }
  84. export type Backreference =
  85. | NumericBackreference
  86. | NamedBackreference;
  87. export interface Repetition extends Base<'Repetition'> {
  88. expression: Expression;
  89. quantifier: Quantifier;
  90. }
  91. export interface SimpleQuantifier extends Base<'Quantifier'> {
  92. kind: '+' | '*' | '?';
  93. greedy: boolean;
  94. }
  95. export interface RangeQuantifier extends Base<'Quantifier'> {
  96. kind: 'Range';
  97. from: number;
  98. to?: number;
  99. greedy: boolean;
  100. }
  101. export type Quantifier =
  102. | SimpleQuantifier
  103. | RangeQuantifier;
  104. export interface SimpleAssertion extends Base<'Assertion'> {
  105. kind: '^' | '$' | '\\b' | '\\B';
  106. }
  107. export interface LookaroundAssertion extends Base<'Assertion'> {
  108. kind: 'Lookahead' | 'Lookbehind';
  109. negative?: true;
  110. assertion: Expression | null;
  111. }
  112. export type Assertion =
  113. | SimpleAssertion
  114. | LookaroundAssertion;
  115. export type Expression =
  116. | Char
  117. | CharacterClass
  118. | Alternative
  119. | Disjunction
  120. | Group
  121. | Backreference
  122. | Repetition
  123. | Assertion;
  124. export interface AstRegExp extends Base<'RegExp'> {
  125. body: Expression | null;
  126. flags: string;
  127. }
  128. }
  129. declare module 'regexp-tree' {
  130. import {
  131. AstRegExp,
  132. AstNode,
  133. AstClass,
  134. AstClassMap
  135. } from 'regexp-tree/ast'
  136. export interface ParserOptions {
  137. captureLocations?: boolean;
  138. allowGroupNameDuplicates?: boolean;
  139. }
  140. /**
  141. * Parses a regexp string, producing an AST.
  142. *
  143. * @param regexp a regular expression in different formats: string, AST, RegExp.
  144. * @param options parsing options for this parse call.
  145. */
  146. export function parse(regexp: string | RegExp, options?: ParserOptions): AstRegExp;
  147. /**
  148. * Generates a RegExp string from an AST.
  149. */
  150. export function generate(ast: AstNode | null | undefined): string;
  151. /**
  152. * Creates a RegExp object from a regexp string.
  153. */
  154. export function toRegExp(regexp: string): RegExp;
  155. export interface NodePath<T extends AstNode = AstNode> {
  156. node: T;
  157. parent: AstNode | null;
  158. parentPath: NodePath | null;
  159. property: string | null;
  160. index: number | null;
  161. getParent(): NodePath | null;
  162. getChild(n?: number): NodePath | null;
  163. getPreviousSibling(): NodePath | null;
  164. getNextSibling(): NodePath | null;
  165. setChild<T extends AstNode>(node: T | null, index?: number | null, property?: string | null): NodePath<T> | null;
  166. appendChild<T extends AstNode>(node: T | null, property?: string | null): NodePath<T> | null;
  167. insertChildAt<T extends AstNode>(node: T | null, index: number, property?: string | null): void;
  168. replace<T extends AstNode>(node: T): NodePath<T> | null;
  169. update(nodeProps: Partial<T>): void;
  170. remove(): void;
  171. isRemoved(): boolean;
  172. hasEqualSource(path: NodePath<T>): boolean;
  173. jsonEncode(options?: { format?: string | number, useLoc?: boolean }): string;
  174. }
  175. export type NodeTraversalCallback<T extends AstNode = AstNode> = (node: T, parent: NodePath | null, property?: string, index?: number) => void | boolean;
  176. export interface NodeTraversalCallbacks<T extends AstNode = AstNode> {
  177. pre?: NodeTraversalCallback<T>;
  178. post?: NodeTraversalCallback<T>;
  179. }
  180. export type SpecificNodeTraversalHandlers = {
  181. [P in AstClass]?: NodeTraversalCallback<AstClassMap[P]> | NodeTraversalCallbacks<AstClassMap[P]>;
  182. };
  183. export interface NodeTraversalHandlers<T extends AstNode = AstNode> extends SpecificNodeTraversalHandlers {
  184. '*'?: NodeTraversalCallback;
  185. shouldRun?(ast: T): boolean;
  186. init?(ast: T): void;
  187. }
  188. export type TraversalCallback<T extends AstNode = AstNode> = (path: NodePath<T>) => void | boolean;
  189. export interface TraversalCallbacks<T extends AstNode = AstNode> {
  190. pre?: TraversalCallback<T>;
  191. post?: TraversalCallback<T>;
  192. }
  193. export type SpecificTraversalHandlers = {
  194. [P in AstClass]?: TraversalCallback<AstClassMap[P]> | TraversalCallbacks<AstClassMap[P]>;
  195. };
  196. export interface TraversalHandlers<T extends AstNode = AstNode> extends SpecificTraversalHandlers {
  197. '*'?: TraversalCallback;
  198. shouldRun?(ast: T): boolean;
  199. init?(ast: T): void;
  200. }
  201. /**
  202. * Traverses a RegExp AST.
  203. *
  204. * @param handlers Each `handler` is an object containing handler function for needed
  205. * node types. The value for a node type may also be an object with functions pre and post.
  206. * This enables more context-aware analyses, e.g. measuring star height.
  207. *
  208. * @example
  209. * regexpTree.traverse(ast, {
  210. * onChar(node) {
  211. * ...
  212. * },
  213. * });
  214. */
  215. export function traverse<T extends AstNode>(ast: T, handlers: NodeTraversalHandlers<T> | ReadonlyArray<NodeTraversalHandlers<T>>, options: { asNodes: true }): void;
  216. export function traverse<T extends AstNode>(ast: T, handlers: TraversalHandlers<T> | ReadonlyArray<TraversalHandlers<T>>, options?: { asNodes?: false }): void;
  217. export type TransformHandlers<T extends AstNode = AstNode> = TraversalHandlers<T>;
  218. export class TransformResult<T extends AstNode, E = unknown> {
  219. private _ast;
  220. private _source;
  221. private _string;
  222. private _regexp;
  223. private _extra;
  224. constructor(ast: T, extra?: E);
  225. getAST(): T;
  226. setExtra(extra: E): void;
  227. getExtra(): E;
  228. toRegExp(): RegExp;
  229. getSource(): string;
  230. getFlags(): string;
  231. toString(): string;
  232. }
  233. /**
  234. * Transforms a regular expression.
  235. *
  236. * A regexp can be passed in different formats (string, regexp or AST),
  237. * applying a set of transformations. It is a convenient wrapper
  238. * on top of "parse-traverse-generate" tool chain.
  239. */
  240. export function transform<T extends AstNode>(ast: T, handlers: TraversalHandlers<T> | ReadonlyArray<TraversalHandlers<T>>): TransformResult<T>;
  241. export function transform(regexp: string | RegExp, handlers: TransformHandlers<AstRegExp> | ReadonlyArray<TransformHandlers<AstRegExp>>): TransformResult<AstRegExp>;
  242. /**
  243. * Optimizes a regular expression by replacing some
  244. * sub-expressions with their idiomatic patterns.
  245. */
  246. export function optimize<T extends AstNode>(ast: T, whitelist?: string[]): TransformResult<T>;
  247. export function optimize(regexp: string | RegExp, whitelist?: string[]): TransformResult<AstRegExp>;
  248. /**
  249. * Translates a regular expression in new syntax or in new format
  250. * into equivalent expressions in old syntax.
  251. */
  252. export function compatTranspile<T extends AstNode>(ast: T, whitelist?: string[]): TransformResult<T>;
  253. export function compatTranspile(regexp: string | RegExp, whitelist?: string[]): TransformResult<AstRegExp>;
  254. /**
  255. * Executes a regular expression on a string.
  256. */
  257. export function exec(re: string | RegExp, string: string): RegExpExecArray;
  258. }