parser.d.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. type _If<Test, Then, Else> = Test extends true ? Then : Else;
  2. export type Features = {
  3. lookbehind?: boolean;
  4. namedGroups?: boolean;
  5. unicodePropertyEscape?: boolean;
  6. unicodeSet?: boolean;
  7. modifiers?: boolean;
  8. };
  9. export type AstNodeType =
  10. | "alternative"
  11. | "anchor"
  12. | "characterClass"
  13. | "characterClassEscape"
  14. | "characterClassRange"
  15. | "disjunction"
  16. | "dot"
  17. | "group"
  18. | "quantifier"
  19. | "reference"
  20. | "unicodePropertyEscape"
  21. | "value";
  22. export type Base<T extends AstNodeType> = {
  23. range: [number, number];
  24. raw: string;
  25. type: T;
  26. };
  27. export type AstNode<F extends Features = {}> =
  28. | Alternative<F>
  29. | Anchor
  30. | CharacterClass<F>
  31. | CharacterClassEscape
  32. | CharacterClassRange
  33. | Disjunction<F>
  34. | Dot
  35. | Group<F>
  36. | Quantifier<F>
  37. | Reference<F>
  38. | _If<F["unicodePropertyEscape"], UnicodePropertyEscape, never>
  39. | Value;
  40. export type RootNode<F extends Features = {}> = Exclude<
  41. AstNode<F>,
  42. CharacterClassRange
  43. >;
  44. export type Anchor = Base<"anchor"> & {
  45. kind: "boundary" | "end" | "not-boundary" | "start";
  46. };
  47. export type CharacterClassEscape = Base<"characterClassEscape"> & {
  48. value: 'd' | 'D' | 'w' | 'W' | 's' | 'S';
  49. };
  50. export type Value = Base<"value"> & {
  51. codePoint: number;
  52. kind:
  53. | "controlLetter"
  54. | "hexadecimalEscape"
  55. | "identifier"
  56. | "null"
  57. | "octal"
  58. | "singleEscape"
  59. | "symbol"
  60. | "unicodeCodePointEscape"
  61. | "unicodeEscape";
  62. };
  63. export type Identifier = Base<"value"> & {
  64. value: string;
  65. };
  66. export type Alternative<F extends Features = {}> = Base<"alternative"> & {
  67. body: RootNode<F>[];
  68. };
  69. export type CharacterClassRange = Base<"characterClassRange"> & {
  70. max: Value;
  71. min: Value;
  72. };
  73. export type UnicodePropertyEscape = Base<"unicodePropertyEscape"> & {
  74. negative: boolean;
  75. value: string;
  76. };
  77. export type CharacterClassBody =
  78. | CharacterClassEscape
  79. | CharacterClassRange
  80. | UnicodePropertyEscape
  81. | Value;
  82. export type CharacterClass<F extends Features = {}> = Base<"characterClass"> & {
  83. body: CharacterClassBody[];
  84. negative: boolean;
  85. kind: "union" | _If<F["unicodeSet"], "intersection" | "subtraction", never>;
  86. };
  87. export type ModifierFlags = {
  88. enabling: string;
  89. disabling: string;
  90. };
  91. export type NonCapturingGroup<F extends Features = {}> = Base<"group"> &
  92. (
  93. | {
  94. behavior:
  95. | "lookahead"
  96. | "lookbehind"
  97. | "negativeLookahead"
  98. | "negativeLookbehind";
  99. body: RootNode<F>[];
  100. }
  101. | ({
  102. behavior: "ignore";
  103. body: RootNode<F>[];
  104. } & _If<
  105. F["modifiers"],
  106. {
  107. modifierFlags?: ModifierFlags;
  108. },
  109. {
  110. modifierFlags: undefined;
  111. }
  112. >)
  113. );
  114. export type CapturingGroup<F extends Features = {}> = Base<"group"> & {
  115. behavior: "normal";
  116. body: RootNode<F>[];
  117. } & _If<
  118. F["namedGroups"],
  119. {
  120. name?: Identifier;
  121. },
  122. {
  123. name: undefined;
  124. }
  125. >;
  126. export type Group<F extends Features = {}> =
  127. | CapturingGroup<F>
  128. | NonCapturingGroup<F>;
  129. export type Quantifier<F extends Features = {}> = Base<"quantifier"> & {
  130. body: [RootNode<F>];
  131. greedy: boolean;
  132. max?: number;
  133. min: number;
  134. symbol?: "?" | "*" | "+";
  135. };
  136. export type Disjunction<F extends Features = {}> = Base<"disjunction"> & {
  137. body: [RootNode<F>, RootNode<F>, ...RootNode<F>[]];
  138. };
  139. export type Dot = Base<"dot">;
  140. export type NamedReference = Base<"reference"> & {
  141. matchIndex: undefined;
  142. name: Identifier;
  143. };
  144. export type IndexReference = Base<"reference"> & {
  145. matchIndex: number;
  146. name: undefined;
  147. };
  148. export type Reference<F extends Features = {}> = _If<
  149. F["namedGroups"],
  150. IndexReference | NamedReference,
  151. IndexReference
  152. >;
  153. export function parse<F extends Features = {}>(
  154. str: string,
  155. flags: string,
  156. features?: F
  157. ): RootNode<F>;