Skip to content

Commit 932beb9

Browse files
committed
Breaking: replace Disjunction node by Alternative node
1 parent 876a59a commit 932beb9

17 files changed

+28697
-19778
lines changed

scripts/update-fixtures.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ for (const filename of Object.keys(Visitor.Fixtures)) {
2727

2828
for (const pattern of Object.keys(fixture.patterns)) {
2929
const ast = parseRegExpLiteral(pattern, options)
30-
const history = []
30+
const history = [] as string[]
3131
const enter = (node: AST.Node): void => {
3232
history.push(`enter:${node.type}:${node.raw}`)
3333
}
@@ -36,27 +36,27 @@ for (const filename of Object.keys(Visitor.Fixtures)) {
3636
}
3737

3838
visitRegExpAST(ast, {
39+
onAlternativeEnter: enter,
3940
onAssertionEnter: enter,
4041
onBackreferenceEnter: enter,
4142
onCapturingGroupEnter: enter,
4243
onCharacterEnter: enter,
4344
onCharacterClassEnter: enter,
4445
onCharacterClassRangeEnter: enter,
4546
onCharacterSetEnter: enter,
46-
onDisjunctionEnter: enter,
4747
onFlagsEnter: enter,
4848
onGroupEnter: enter,
4949
onPatternEnter: enter,
5050
onQuantifierEnter: enter,
5151
onRegExpLiteralEnter: enter,
52+
onAlternativeLeave: leave,
5253
onAssertionLeave: leave,
5354
onBackreferenceLeave: leave,
5455
onCapturingGroupLeave: leave,
5556
onCharacterLeave: leave,
5657
onCharacterClassLeave: leave,
5758
onCharacterClassRangeLeave: leave,
5859
onCharacterSetLeave: leave,
59-
onDisjunctionLeave: leave,
6060
onFlagsLeave: leave,
6161
onGroupLeave: leave,
6262
onPatternLeave: leave,

src/ast.ts

Lines changed: 31 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export type Node = BranchNode | LeafNode
99
export type BranchNode =
1010
| RegExpLiteral
1111
| Pattern
12-
| Disjunction
12+
| Alternative
1313
| Group
1414
| CapturingGroup
1515
| Quantifier
@@ -30,16 +30,20 @@ export type LeafNode =
3030
/**
3131
* The type which includes all atom nodes.
3232
*/
33-
export type Element =
34-
| Disjunction
33+
export type Element = Assertion | Quantifier | QuantifiableElement
34+
35+
/**
36+
* The type which includes all atom nodes that Quantifier node can have as children.
37+
*/
38+
export type QuantifiableElement =
3539
| Group
3640
| CapturingGroup
37-
| Quantifier
3841
| CharacterClass
39-
| Assertion
4042
| CharacterSet
4143
| Character
4244
| Backreference
45+
// Lookahead assertions is quantifiable in Annex-B.
46+
| LookaheadAssertion
4347

4448
/**
4549
* The type which includes all character class atom nodes.
@@ -50,31 +54,6 @@ export type CharacterClassElement =
5054
| Character
5155
| CharacterClassRange
5256

53-
/**
54-
* The type which includes all atom nodes that Alternative node can have as children.
55-
*/
56-
export type AlternativeElement =
57-
| Group
58-
| CapturingGroup
59-
| Quantifier
60-
| CharacterClass
61-
| Assertion
62-
| CharacterSet
63-
| Character
64-
| Backreference
65-
66-
/**
67-
* The type which includes all atom nodes that Quantifier node can have as children.
68-
*/
69-
export type QuantifiableElement =
70-
| Group
71-
| CapturingGroup
72-
| CharacterClass
73-
| LookaheadAssertion
74-
| CharacterSet
75-
| Character
76-
| Backreference
77-
7857
/**
7958
* The type which defines common properties for all node types.
8059
*/
@@ -107,17 +86,17 @@ export interface RegExpLiteral extends NodeBase {
10786
export interface Pattern extends NodeBase {
10887
type: "Pattern"
10988
parent: RegExpLiteral | null
110-
elements: Element[]
89+
alternatives: Alternative[]
11190
}
11291

11392
/**
114-
* The disjunction.
93+
* The alternative.
11594
* E.g. `a|b`
11695
*/
117-
export interface Disjunction extends NodeBase {
118-
type: "Disjunction"
96+
export interface Alternative extends NodeBase {
97+
type: "Alternative"
11998
parent: Pattern | Group | CapturingGroup | LookaroundAssertion
120-
alternatives: AlternativeElement[][]
99+
elements: Element[]
121100
}
122101

123102
/**
@@ -126,14 +105,8 @@ export interface Disjunction extends NodeBase {
126105
*/
127106
export interface Group extends NodeBase {
128107
type: "Group"
129-
parent:
130-
| Pattern
131-
| Disjunction
132-
| Group
133-
| CapturingGroup
134-
| Quantifier
135-
| LookaroundAssertion
136-
elements: Element[]
108+
parent: Alternative | Quantifier
109+
alternatives: Alternative[]
137110
}
138111

139112
/**
@@ -142,15 +115,9 @@ export interface Group extends NodeBase {
142115
*/
143116
export interface CapturingGroup extends NodeBase {
144117
type: "CapturingGroup"
145-
parent:
146-
| Pattern
147-
| Disjunction
148-
| Group
149-
| CapturingGroup
150-
| Quantifier
151-
| LookaroundAssertion
118+
parent: Alternative | Quantifier
152119
name: string | null
153-
elements: Element[]
120+
alternatives: Alternative[]
154121
references: Backreference[]
155122
}
156123

@@ -165,16 +132,10 @@ export type LookaroundAssertion = LookaheadAssertion | LookbehindAssertion
165132
*/
166133
export interface LookaheadAssertion extends NodeBase {
167134
type: "Assertion"
168-
parent:
169-
| Pattern
170-
| Disjunction
171-
| Group
172-
| CapturingGroup
173-
| Quantifier
174-
| LookaroundAssertion
135+
parent: Alternative | Quantifier
175136
kind: "lookahead"
176137
negate: boolean
177-
elements: Element[]
138+
alternatives: Alternative[]
178139
}
179140

180141
/**
@@ -183,10 +144,10 @@ export interface LookaheadAssertion extends NodeBase {
183144
*/
184145
export interface LookbehindAssertion extends NodeBase {
185146
type: "Assertion"
186-
parent: Pattern | Disjunction | Group | CapturingGroup | LookaroundAssertion
147+
parent: Alternative
187148
kind: "lookbehind"
188149
negate: boolean
189-
elements: Element[]
150+
alternatives: Alternative[]
190151
}
191152

192153
/**
@@ -195,7 +156,7 @@ export interface LookbehindAssertion extends NodeBase {
195156
*/
196157
export interface Quantifier extends NodeBase {
197158
type: "Quantifier"
198-
parent: Pattern | Disjunction | Group | CapturingGroup | LookaroundAssertion
159+
parent: Alternative
199160
min: number
200161
max: number // can be Number.POSITIVE_INFINITY
201162
greedy: boolean
@@ -208,13 +169,7 @@ export interface Quantifier extends NodeBase {
208169
*/
209170
export interface CharacterClass extends NodeBase {
210171
type: "CharacterClass"
211-
parent:
212-
| Pattern
213-
| Disjunction
214-
| Group
215-
| CapturingGroup
216-
| Quantifier
217-
| LookaroundAssertion
172+
parent: Alternative | Quantifier
218173
negate: boolean
219174
elements: CharacterClassElement[]
220175
}
@@ -246,13 +201,7 @@ export type BoundaryAssertion = EdgeAssertion | WordBoundaryAssertion
246201
*/
247202
export interface EdgeAssertion extends NodeBase {
248203
type: "Assertion"
249-
parent:
250-
| Pattern
251-
| Disjunction
252-
| Group
253-
| CapturingGroup
254-
| Quantifier
255-
| LookaroundAssertion
204+
parent: Alternative | Quantifier
256205
kind: "start" | "end"
257206
}
258207

@@ -262,13 +211,7 @@ export interface EdgeAssertion extends NodeBase {
262211
*/
263212
export interface WordBoundaryAssertion extends NodeBase {
264213
type: "Assertion"
265-
parent:
266-
| Pattern
267-
| Disjunction
268-
| Group
269-
| CapturingGroup
270-
| Quantifier
271-
| LookaroundAssertion
214+
parent: Alternative | Quantifier
272215
kind: "word"
273216
negate: boolean
274217
}
@@ -287,13 +230,7 @@ export type CharacterSet =
287230
*/
288231
export interface AnyCharacterSet extends NodeBase {
289232
type: "CharacterSet"
290-
parent:
291-
| Pattern
292-
| Disjunction
293-
| Group
294-
| CapturingGroup
295-
| Quantifier
296-
| LookaroundAssertion
233+
parent: Alternative | Quantifier
297234
kind: "any"
298235
}
299236

@@ -303,14 +240,7 @@ export interface AnyCharacterSet extends NodeBase {
303240
*/
304241
export interface EscapeCharacterSet extends NodeBase {
305242
type: "CharacterSet"
306-
parent:
307-
| Pattern
308-
| Disjunction
309-
| Group
310-
| CapturingGroup
311-
| Quantifier
312-
| CharacterClass
313-
| LookaroundAssertion
243+
parent: Alternative | Quantifier | CharacterClass
314244
kind: "digit" | "space" | "word"
315245
negate: boolean
316246
}
@@ -321,14 +251,7 @@ export interface EscapeCharacterSet extends NodeBase {
321251
*/
322252
export interface UnicodePropertyCharacterSet extends NodeBase {
323253
type: "CharacterSet"
324-
parent:
325-
| Pattern
326-
| Disjunction
327-
| Group
328-
| CapturingGroup
329-
| Quantifier
330-
| CharacterClass
331-
| LookaroundAssertion
254+
parent: Alternative | Quantifier | CharacterClass
332255
kind: "property"
333256
key: string
334257
value: string | null
@@ -342,15 +265,7 @@ export interface UnicodePropertyCharacterSet extends NodeBase {
342265
*/
343266
export interface Character extends NodeBase {
344267
type: "Character"
345-
parent:
346-
| Pattern
347-
| Disjunction
348-
| Group
349-
| CapturingGroup
350-
| Quantifier
351-
| CharacterClass
352-
| LookaroundAssertion
353-
| CharacterClassRange
268+
parent: Alternative | Quantifier | CharacterClass | CharacterClassRange
354269
value: number // a code point.
355270
}
356271

@@ -360,13 +275,7 @@ export interface Character extends NodeBase {
360275
*/
361276
export interface Backreference extends NodeBase {
362277
type: "Backreference"
363-
parent:
364-
| Pattern
365-
| Disjunction
366-
| Group
367-
| CapturingGroup
368-
| Quantifier
369-
| LookaroundAssertion
278+
parent: Alternative | Quantifier
370279
ref: number | string
371280
resolved: CapturingGroup
372281
}

0 commit comments

Comments
 (0)