regexp-tree
Advanced tools
Comparing version 0.1.11 to 0.1.12
@@ -13,3 +13,2 @@ /** | ||
module.exports = { | ||
// To track the names of the groups, and return them | ||
@@ -47,2 +46,3 @@ // in the transform result state. | ||
delete node.name; | ||
delete node.nameRaw; | ||
}, | ||
@@ -59,3 +59,4 @@ Backreference: function Backreference(path) { | ||
node.reference = node.number; | ||
delete node.referenceRaw; | ||
} | ||
}; |
@@ -35,3 +35,3 @@ /** | ||
if (node.name) { | ||
return '(?<' + node.name + '>' + expression + ')'; | ||
return '(?<' + (node.nameRaw || node.name) + '>' + expression + ')'; | ||
} | ||
@@ -49,3 +49,3 @@ | ||
case 'name': | ||
return '\\k<' + node.reference + '>'; | ||
return '\\k<' + (node.referenceRaw || node.reference) + '>'; | ||
default: | ||
@@ -52,0 +52,0 @@ throw new TypeError('Unknown Backreference kind: ' + node.kind); |
199
index.d.ts
declare module 'regexp-tree/ast' { | ||
export type AstClass = | ||
| 'Char' | ||
| 'ClassRange' | ||
| 'CharacterClass' | ||
| 'Alternative' | ||
| 'Disjunction' | ||
| 'Group' | ||
| 'Backreference' | ||
| 'Repetition' | ||
| 'Quantifier' | ||
| 'Assertion' | ||
| 'RegExp'; | ||
export interface AstClassMap { | ||
'RegExp': AstRegExp; | ||
'Disjunction': Disjunction; | ||
'Alternative': Alternative; | ||
'Assertion': Assertion; | ||
'Char': Char; | ||
'CharacterClass': CharacterClass; | ||
'ClassRange': ClassRange; | ||
'Backreference': Backreference; | ||
'Group': Group; | ||
'Repetition': Repetition; | ||
'Quantifier': Quantifier; | ||
} | ||
interface Base<T> { | ||
export type AstClass = keyof AstClassMap; | ||
export type AstNode = AstClassMap[AstClass]; | ||
export interface Base<T extends AstClass> { | ||
type: T; | ||
@@ -35,3 +39,5 @@ loc?: { | ||
export type Char = SimpleChar | SpecialChar; | ||
export type Char = | ||
| SimpleChar | ||
| SpecialChar; | ||
@@ -60,2 +66,3 @@ export interface ClassRange extends Base<'ClassRange'> { | ||
name?: string; | ||
nameRaw?: string; | ||
expression: Expression | null; | ||
@@ -69,3 +76,5 @@ } | ||
export type Group = CapturingGroup | NoncapturingGroup; | ||
export type Group = | ||
| CapturingGroup | ||
| NoncapturingGroup; | ||
@@ -82,5 +91,8 @@ export interface NumericBackreference extends Base<'Backreference'> { | ||
reference: string; | ||
referenceRaw: string; | ||
} | ||
export type Backreference = NumericBackreference | NamedBackreference; | ||
export type Backreference = | ||
| NumericBackreference | ||
| NamedBackreference; | ||
@@ -104,3 +116,5 @@ export interface Repetition extends Base<'Repetition'> { | ||
export type Quantifier = SimpleQuantifier | RangeQuantifier; | ||
export type Quantifier = | ||
| SimpleQuantifier | ||
| RangeQuantifier; | ||
@@ -117,3 +131,5 @@ export interface SimpleAssertion extends Base<'Assertion'> { | ||
export type Assertion = SimpleAssertion | LookaroundAssertion; | ||
export type Assertion = | ||
| SimpleAssertion | ||
| LookaroundAssertion; | ||
@@ -137,12 +153,149 @@ export type Expression = | ||
declare module 'regexp-tree' { | ||
import { AstRegExp } from 'regexp-tree/ast' | ||
interface ParserOptions { | ||
import { | ||
AstRegExp, | ||
AstNode, | ||
AstClass, | ||
AstClassMap | ||
} from 'regexp-tree/ast' | ||
export interface ParserOptions { | ||
captureLocations?: boolean; | ||
} | ||
export function parse(s: string | RegExp, options?: ParserOptions): AstRegExp; | ||
/** | ||
* Parses a regexp string, producing an AST. | ||
* | ||
* @param regexp a regular expression in different formats: string, AST, RegExp. | ||
* @param options parsing options for this parse call. | ||
*/ | ||
export function parse(regexp: string | RegExp, options?: ParserOptions): AstRegExp; | ||
export function generate(ast: AstRegExp): string; | ||
/** | ||
* Generates a RegExp string from an AST. | ||
*/ | ||
export function generate(ast: AstNode | null | undefined): string; | ||
/** | ||
* Creates a RegExp object from a regexp string. | ||
*/ | ||
export function toRegExp(regexp: string): RegExp; | ||
} | ||
export interface NodePath<T extends AstNode = AstNode> { | ||
node: T; | ||
parent: AstNode | null; | ||
parentPath: NodePath | null; | ||
property: string | null; | ||
index: number | null; | ||
getParent(): NodePath | null; | ||
getChild(n?: number): NodePath | null; | ||
getPreviousSibling(): NodePath | null; | ||
getNextSibling(): NodePath | null; | ||
setChild<T extends AstNode>(node: T | null, index?: number | null, property?: string | null): NodePath<T> | null; | ||
appendChild<T extends AstNode>(node: T | null, property?: string | null): NodePath<T> | null; | ||
insertChildAt<T extends AstNode>(node: T | null, index: number, property?: string | null): void; | ||
replace<T extends AstNode>(node: T): NodePath<T> | null; | ||
update(nodeProps: Partial<T>): void; | ||
remove(): void; | ||
isRemoved(): boolean; | ||
hasEqualSource(path: NodePath<T>): boolean; | ||
jsonEncode(options?: { format?: string | number, useLoc?: boolean }): string; | ||
} | ||
export type NodeTraversalCallback<T extends AstNode = AstNode> = (node: T, parent: NodePath | null, property?: string, index?: number) => void | boolean; | ||
export interface NodeTraversalCallbacks<T extends AstNode = AstNode> { | ||
pre?: NodeTraversalCallback<T>; | ||
post?: NodeTraversalCallback<T>; | ||
} | ||
export type SpecificNodeTraversalHandlers = { | ||
[P in AstClass]?: NodeTraversalCallback<AstClassMap[P]> | NodeTraversalCallbacks<AstClassMap[P]>; | ||
}; | ||
export interface NodeTraversalHandlers<T extends AstNode = AstNode> extends SpecificNodeTraversalHandlers { | ||
'*'?: NodeTraversalCallback; | ||
shouldRun?(ast: T): boolean; | ||
init?(ast: T): void; | ||
} | ||
export type TraversalCallback<T extends AstNode = AstNode> = (path: NodePath<T>) => void | boolean; | ||
export interface TraversalCallbacks<T extends AstNode = AstNode> { | ||
pre?: TraversalCallback<T>; | ||
post?: TraversalCallback<T>; | ||
} | ||
export type SpecificTraversalHandlers = { | ||
[P in AstClass]?: TraversalCallback<AstClassMap[P]> | TraversalCallbacks<AstClassMap[P]>; | ||
}; | ||
export interface TraversalHandlers<T extends AstNode = AstNode> extends SpecificTraversalHandlers { | ||
'*'?: TraversalCallback; | ||
shouldRun?(ast: T): boolean; | ||
init?(ast: T): void; | ||
} | ||
/** | ||
* Traverses a RegExp AST. | ||
* | ||
* @param handlers Each `handler` is an object containing handler function for needed | ||
* node types. The value for a node type may also be an object with functions pre and post. | ||
* This enables more context-aware analyses, e.g. measuring star height. | ||
* | ||
* @example | ||
* regexpTree.traverse(ast, { | ||
* onChar(node) { | ||
* ... | ||
* }, | ||
* }); | ||
*/ | ||
export function traverse<T extends AstNode>(ast: T, handlers: NodeTraversalHandlers<T> | ReadonlyArray<NodeTraversalHandlers<T>>, options: { asNodes: true }): void; | ||
export function traverse<T extends AstNode>(ast: T, handlers: TraversalHandlers<T> | ReadonlyArray<TraversalHandlers<T>>, options?: { asNodes?: false }): void; | ||
export type TransformHandlers<T extends AstNode = AstNode> = TraversalHandlers<T>; | ||
export class TransformResult<T extends AstNode, E = unknown> { | ||
private _ast; | ||
private _source; | ||
private _string; | ||
private _regexp; | ||
private _extra; | ||
constructor(ast: T, extra?: E); | ||
getAST(): T; | ||
setExtra(extra: E): void; | ||
getExtra(): E; | ||
toRegExp(): RegExp; | ||
getSource(): string; | ||
getFlags(): string; | ||
toString(): string; | ||
} | ||
/** | ||
* Transforms a regular expression. | ||
* | ||
* A regexp can be passed in different formats (string, regexp or AST), | ||
* applying a set of transformations. It is a convenient wrapper | ||
* on top of "parse-traverse-generate" tool chain. | ||
*/ | ||
export function transform<T extends AstNode>(ast: T, handlers: TraversalHandlers<T> | ReadonlyArray<TraversalHandlers<T>>): TransformResult<T>; | ||
export function transform(regexp: string | RegExp, handlers: TransformHandlers<AstRegExp> | ReadonlyArray<TransformHandlers<AstRegExp>>): TransformResult<AstRegExp>; | ||
/** | ||
* Optimizes a regular expression by replacing some | ||
* sub-expressions with their idiomatic patterns. | ||
*/ | ||
export function optimize<T extends AstNode>(ast: T, whitelist?: string[]): TransformResult<T>; | ||
export function optimize(regexp: string | RegExp, whitelist?: string[]): TransformResult<AstRegExp>; | ||
/** | ||
* Translates a regular expression in new syntax or in new format | ||
* into equivalent expressions in old syntax. | ||
*/ | ||
export function compatTranspile<T extends AstNode>(ast: T, whitelist?: string[]): TransformResult<T>; | ||
export function compatTranspile(regexp: string | RegExp, whitelist?: string[]): TransformResult<AstRegExp>; | ||
/** | ||
* Executes a regular expression on a string. | ||
*/ | ||
export function exec(re: string | RegExp, string: string): RegExpExecArray; | ||
} |
{ | ||
"name": "regexp-tree", | ||
"version": "0.1.11", | ||
"version": "0.1.12", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Regular Expressions parser in JavaScript", |
@@ -1339,2 +1339,3 @@ # regexp-tree | ||
name: 'foo', | ||
nameRaw: 'foo', | ||
number: 1, | ||
@@ -1351,2 +1352,29 @@ expression: { | ||
Note: The `nameRaw` property represents the name *as parsed from the original source*, including escape sequences. The `name` property represents the canonical decoded form of the name. | ||
For example, given the `/u` flag and the following group: | ||
```regexp | ||
(?<\u{03C0}>x) | ||
``` | ||
We would have the following node: | ||
```js | ||
{ | ||
type: 'Group', | ||
capturing: true, | ||
name: 'π', | ||
nameRaw: '\\u{03C0}', | ||
number: 1, | ||
expression: { | ||
type: 'Char', | ||
value: 'x', | ||
symbol: 'x', | ||
kind: 'simple', | ||
codePoint: 120 | ||
} | ||
} | ||
``` | ||
##### Non-capturing group | ||
@@ -1470,2 +1498,3 @@ | ||
name: 'foo', | ||
nameRaw: 'foo', | ||
number: 1, | ||
@@ -1484,3 +1513,4 @@ expression: { | ||
number: 1, | ||
reference: 'foo' | ||
reference: 'foo', | ||
referenceRaw: 'foo' | ||
}, | ||
@@ -1497,2 +1527,48 @@ { | ||
Note: The `referenceRaw` property represents the reference *as parsed from the original source*, including escape sequences. The `reference` property represents the canonical decoded form of the reference. | ||
For example, given the `/u` flag and the following pattern (matches `www`): | ||
```regexp | ||
(?<π>w)\k<\u{03C0}>\1 | ||
``` | ||
We would have the following node: | ||
```js | ||
{ | ||
type: 'Alternative', | ||
expressions: [ | ||
{ | ||
type: 'Group', | ||
capturing: true, | ||
name: 'π', | ||
nameRaw: 'π', | ||
number: 1, | ||
expression: { | ||
type: 'Char', | ||
value: 'w', | ||
symbol: 'w', | ||
kind: 'simple', | ||
codePoint: 119 | ||
} | ||
}, | ||
{ | ||
type: 'Backreference', | ||
kind: 'name', | ||
number: 1, | ||
reference: 'π', | ||
referenceRaw: '\\u{03C0}' | ||
}, | ||
{ | ||
type: 'Backreference', | ||
kind: 'number', | ||
number: 1, | ||
reference: 1 | ||
} | ||
] | ||
} | ||
``` | ||
#### Quantifiers | ||
@@ -1499,0 +1575,0 @@ |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
311820
6424
2086