index.d.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. declare module 'regexp-tree/ast' {
  2. export type AstClass =
  3. | 'Char'
  4. | 'ClassRange'
  5. | 'CharacterClass'
  6. | 'Alternative'
  7. | 'Disjunction'
  8. | 'Group'
  9. | 'Backreference'
  10. | 'Repetition'
  11. | 'Quantifier'
  12. | 'Assertion'
  13. | 'RegExp';
  14. interface Base<T> {
  15. type: T;
  16. loc?: {
  17. source: string;
  18. start: number;
  19. end: number;
  20. };
  21. }
  22. export interface SimpleChar extends Base<'Char'> {
  23. value: string;
  24. kind: 'simple';
  25. escaped?: true;
  26. }
  27. export interface SpecialChar extends Base<'Char'> {
  28. value: string;
  29. kind: 'meta' | 'control' | 'hex' | 'decimal' | 'oct' | 'unicode';
  30. }
  31. export type Char = SimpleChar | SpecialChar;
  32. export interface ClassRange extends Base<'ClassRange'> {
  33. from: Char;
  34. to: Char;
  35. }
  36. export interface CharacterClass extends Base<'CharacterClass'> {
  37. negative?: true;
  38. expressions: (Char | ClassRange)[];
  39. }
  40. export interface Alternative extends Base<'Alternative'> {
  41. expressions: Expression[];
  42. }
  43. export interface Disjunction extends Base<'Disjunction'> {
  44. expressions: (Expression | null)[];
  45. }
  46. export interface CapturingGroup extends Base<'Group'> {
  47. capturing: true;
  48. number: number;
  49. name?: string;
  50. expression: Expression | null;
  51. }
  52. export interface NoncapturingGroup extends Base<'Group'> {
  53. capturing: false;
  54. expression: Expression | null;
  55. }
  56. export type Group = CapturingGroup | NoncapturingGroup;
  57. export interface NumericBackreference extends Base<'Backreference'> {
  58. kind: 'number';
  59. number: number;
  60. reference: number;
  61. }
  62. export interface NamedBackreference extends Base<'Backreference'> {
  63. kind: 'name';
  64. number: number;
  65. reference: string;
  66. }
  67. export type Backreference = NumericBackreference | NamedBackreference;
  68. export interface Repetition extends Base<'Repetition'> {
  69. expression: Expression;
  70. quantifier: Quantifier;
  71. }
  72. export interface SimpleQuantifier extends Base<'Quantifier'> {
  73. kind: '+' | '*' | '?';
  74. greedy: boolean;
  75. }
  76. export interface RangeQuantifier extends Base<'Quantifier'> {
  77. kind: 'Range';
  78. from: number;
  79. to?: number;
  80. greedy: boolean;
  81. }
  82. export type Quantifier = SimpleQuantifier | RangeQuantifier;
  83. export interface SimpleAssertion extends Base<'Assertion'> {
  84. kind: '^' | '$' | '\\b' | '\\B';
  85. }
  86. export interface LookaroundAssertion extends Base<'Assertion'> {
  87. kind: 'Lookahead' | 'Lookbehind';
  88. negative?: true;
  89. assertion: Expression | null;
  90. }
  91. export type Assertion = SimpleAssertion | LookaroundAssertion;
  92. export type Expression =
  93. | Char
  94. | CharacterClass
  95. | Alternative
  96. | Disjunction
  97. | Group
  98. | Backreference
  99. | Repetition
  100. | Assertion;
  101. export interface AstRegExp extends Base<'RegExp'> {
  102. body: Expression | null;
  103. flags: string;
  104. }
  105. }
  106. declare module 'regexp-tree' {
  107. import { AstRegExp } from 'regexp-tree/ast'
  108. interface ParserOptions {
  109. captureLocations?: boolean;
  110. }
  111. export function parse(s: string | RegExp, options?: ParserOptions): AstRegExp;
  112. export function generate(ast: AstRegExp): string;
  113. export function toRegExp(regexp: string): RegExp;
  114. }