parser.d.ts 3.3 KB

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