-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathequal.ts
174 lines (157 loc) · 4.58 KB
/
equal.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import {
Node,
Group,
CapturingGroup,
Alternative,
LookaroundAssertion,
Quantifier,
Pattern,
CharacterClass,
CharacterClassRange,
Character,
RegExpLiteral,
Flags,
Backreference,
CharacterSet,
Assertion,
ClassStringDisjunction,
StringAlternative,
ExpressionCharacterClass,
ClassSubtraction,
ClassIntersection,
} from "@eslint-community/regexpp/ast";
import { isStrictBackreference } from "./basic";
import { assertNever } from "./util";
/**
* Returns whether two nodes are structurally equivalent.
*
* If two elements are structurally equivalent, they must also semantically equivalent. However, two semantically
* equivalent elements might not be structurally equivalent (e.g. `/[ab]/` !=<sub>struct</sub> `/[ba]/`).
*/
export function structurallyEqual(x: Node | null, y: Node | null): boolean {
if (x == y) {
return true;
}
if (!x || !y || x.type != y.type) {
return false;
}
switch (x.type) {
case "Alternative":
case "StringAlternative": {
const other = y as Alternative | StringAlternative;
return manyAreStructurallyEqual(x.elements, other.elements);
}
case "Assertion": {
const other = y as Assertion;
if (x.kind === other.kind) {
if (x.kind === "lookahead" || x.kind === "lookbehind") {
const otherLookaround = y as LookaroundAssertion;
return (
x.negate === otherLookaround.negate &&
manyAreStructurallyEqual(x.alternatives, otherLookaround.alternatives)
);
} else {
return x.raw === other.raw;
}
}
return false;
}
case "Backreference": {
const other = y as Backreference;
const groupsX = x.ambiguous ? x.resolved : [x.resolved];
const groupsY = other.ambiguous ? other.resolved : [other.resolved];
/**
* Keep any groups of `y` that did not match anything.
* If there are any groups remaining after searching the groups of `x`, they do not match.
*/
const unusedGroupsY = new Set(groupsY);
for (const groupX of groupsX) {
const matches = groupsY.filter(groupY => structurallyEqual(groupX, groupY));
if (matches.length === 0) return false;
for (const groupY of matches) {
unusedGroupsY.delete(groupY);
}
}
if (unusedGroupsY.size > 0) return false;
return isStrictBackreference(x) == isStrictBackreference(other);
}
case "Character": {
const other = y as Character;
return x.value === other.value;
}
case "CharacterClass": {
const other = y as CharacterClass;
return (
x.negate === other.negate &&
x.unicodeSets === other.unicodeSets &&
manyAreStructurallyEqual(x.elements, other.elements)
);
}
case "CharacterClassRange": {
const other = y as CharacterClassRange;
return structurallyEqual(x.min, other.min) && structurallyEqual(x.max, other.max);
}
case "CharacterSet": {
const other = y as CharacterSet;
if (x.kind === "property" && other.kind === "property") {
return x.negate === other.negate && x.key === other.key && x.value === other.value;
} else {
return x.raw === other.raw;
}
}
case "ExpressionCharacterClass": {
const other = y as ExpressionCharacterClass;
return x.negate === other.negate && structurallyEqual(x.expression, other.expression);
}
case "ClassIntersection":
case "ClassSubtraction": {
const other = y as ClassIntersection | ClassSubtraction;
return structurallyEqual(x.left, other.left) && structurallyEqual(x.right, other.right);
}
case "Flags": {
const other = y as Flags;
return (
x.dotAll === other.dotAll &&
x.global === other.global &&
x.ignoreCase === other.ignoreCase &&
x.multiline === other.multiline &&
x.sticky === other.sticky &&
x.unicode === other.unicode &&
x.unicodeSets === other.unicodeSets
);
}
case "ClassStringDisjunction":
case "CapturingGroup":
case "Group":
case "Pattern": {
const other = y as CapturingGroup | Group | Pattern | ClassStringDisjunction;
return manyAreStructurallyEqual(x.alternatives, other.alternatives);
}
case "Quantifier": {
const other = y as Quantifier;
return (
x.min === other.min &&
x.max === other.max &&
x.greedy === other.greedy &&
structurallyEqual(x.element, other.element)
);
}
case "RegExpLiteral": {
const other = y as RegExpLiteral;
return structurallyEqual(x.flags, other.flags) && structurallyEqual(x.pattern, other.pattern);
}
default:
throw assertNever(x);
}
}
function manyAreStructurallyEqual(a: readonly Node[], b: readonly Node[]): boolean {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (!structurallyEqual(a[i], b[i])) {
return false;
}
}
return true;
}