Skip to content

Commit fb0dade

Browse files
committed
change ast
1 parent 7b10d7c commit fb0dade

10 files changed

+1454
-190
lines changed

src/ast.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export interface Alternative extends NodeBase {
124124
export interface Group extends NodeBase {
125125
type: "Group"
126126
parent: Alternative | Quantifier
127-
modifiers: Modifiers | null
127+
modifiers: Modifiers
128128
alternatives: Alternative[]
129129
}
130130

@@ -444,7 +444,16 @@ export interface Backreference extends NodeBase {
444444
export interface Modifiers extends NodeBase {
445445
type: "Modifiers"
446446
parent: Group
447-
add: ModifierFlags | null
447+
/**
448+
* The add modifier flags.
449+
*/
450+
add: ModifierFlags
451+
/**
452+
* The remove modifier flags.
453+
*
454+
* `null` means no remove modifier flags. e.g. `(?ims:x)`
455+
* The reason for `null` is that there is no position where the remove modifier flags appears. Must be behind the minus mark.
456+
*/
448457
remove: ModifierFlags | null
449458
}
450459

src/parser.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class RegExpParserState {
204204
start,
205205
end: start,
206206
raw: "",
207-
modifiers: null,
207+
modifiers: null as never, // Set in onModifiersEnter.
208208
alternatives: [],
209209
}
210210
parent.elements.push(this._node)
@@ -233,7 +233,7 @@ class RegExpParserState {
233233
start,
234234
end: start,
235235
raw: "",
236-
add: null,
236+
add: null as never, // Set in onAddModifiers.
237237
remove: null,
238238
}
239239
parent.modifiers = this._node

src/validator.ts

+15-33
Original file line numberDiff line numberDiff line change
@@ -1746,44 +1746,26 @@ export class RegExpValidator {
17461746
*/
17471747
private consumeModifiers(): boolean {
17481748
const start = this.index
1749+
this.onModifiersEnter(start)
1750+
const hasAddModifiers = this.eatModifiers()
1751+
const addModifiers = this.parseModifiers(start, this.index)
1752+
this.onAddModifiers(start, this.index, addModifiers)
17491753

1750-
if (this.eatModifiers()) {
1751-
this.onModifiersEnter(start)
1752-
const addModifiers = this.parseModifiers(start, this.index)
1753-
this.onAddModifiers(start, this.index, addModifiers)
1754-
if (this.eat(HYPHEN_MINUS)) {
1755-
const modifiersStart = this.index
1756-
if (this.eatModifiers()) {
1757-
const modifiers = this.parseModifiers(
1758-
modifiersStart,
1759-
this.index,
1760-
addModifiers,
1761-
)
1762-
this.onRemoveModifiers(
1763-
modifiersStart,
1764-
this.index,
1765-
modifiers,
1766-
)
1767-
}
1768-
}
1769-
this.onModifiersLeave(start, this.index)
1770-
return true
1771-
} else if (this.eat(HYPHEN_MINUS)) {
1772-
this.onModifiersEnter(start)
1754+
if (this.eat(HYPHEN_MINUS)) {
17731755
const modifiersStart = this.index
1774-
if (this.eatModifiers()) {
1775-
const modifiers = this.parseModifiers(
1776-
modifiersStart,
1777-
this.index,
1778-
)
1779-
this.onRemoveModifiers(modifiersStart, this.index, modifiers)
1780-
} else {
1756+
if (!this.eatModifiers() && !hasAddModifiers) {
17811757
this.raise("Invalid empty flags")
17821758
}
1783-
this.onModifiersLeave(start, this.index)
1784-
return true
1759+
const modifiers = this.parseModifiers(
1760+
modifiersStart,
1761+
this.index,
1762+
addModifiers,
1763+
)
1764+
this.onRemoveModifiers(modifiersStart, this.index, modifiers)
17851765
}
1786-
return false
1766+
1767+
this.onModifiersLeave(start, this.index)
1768+
return true
17871769
}
17881770

17891771
/**

test/fixtures/parser/literal/modifiers-valid-2024.json

+28-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,16 @@
227227
"start": 3,
228228
"end": 7,
229229
"raw": "-ims",
230-
"add": null,
230+
"add": {
231+
"type": "ModifierFlags",
232+
"parent": "♻️..",
233+
"start": 3,
234+
"end": 3,
235+
"raw": "",
236+
"ignoreCase": false,
237+
"multiline": false,
238+
"dotAll": false
239+
},
231240
"remove": {
232241
"type": "ModifierFlags",
233242
"parent": "♻️..",
@@ -317,7 +326,24 @@
317326
"start": 1,
318327
"end": 17,
319328
"raw": "(?:no-modifiers)",
320-
"modifiers": null,
329+
"modifiers": {
330+
"type": "Modifiers",
331+
"parent": "♻️..",
332+
"start": 3,
333+
"end": 3,
334+
"raw": "",
335+
"add": {
336+
"type": "ModifierFlags",
337+
"parent": "♻️..",
338+
"start": 3,
339+
"end": 3,
340+
"raw": "",
341+
"ignoreCase": false,
342+
"multiline": false,
343+
"dotAll": false
344+
},
345+
"remove": null
346+
},
321347
"alternatives": [
322348
{
323349
"type": "Alternative",

test/fixtures/parser/literal/test262/not-categorized.json

+36-2
Original file line numberDiff line numberDiff line change
@@ -3510,7 +3510,24 @@
35103510
"start": 1,
35113511
"end": 10,
35123512
"raw": "(?:ab|cd)",
3513-
"modifiers": null,
3513+
"modifiers": {
3514+
"type": "Modifiers",
3515+
"parent": "♻️..",
3516+
"start": 3,
3517+
"end": 3,
3518+
"raw": "",
3519+
"add": {
3520+
"type": "ModifierFlags",
3521+
"parent": "♻️..",
3522+
"start": 3,
3523+
"end": 3,
3524+
"raw": "",
3525+
"ignoreCase": false,
3526+
"multiline": false,
3527+
"dotAll": false
3528+
},
3529+
"remove": null
3530+
},
35143531
"alternatives": [
35153532
{
35163533
"type": "Alternative",
@@ -3638,7 +3655,24 @@
36383655
"start": 1,
36393656
"end": 10,
36403657
"raw": "(?:ab|cd)",
3641-
"modifiers": null,
3658+
"modifiers": {
3659+
"type": "Modifiers",
3660+
"parent": "♻️..",
3661+
"start": 3,
3662+
"end": 3,
3663+
"raw": "",
3664+
"add": {
3665+
"type": "ModifierFlags",
3666+
"parent": "♻️..",
3667+
"start": 3,
3668+
"end": 3,
3669+
"raw": "",
3670+
"ignoreCase": false,
3671+
"multiline": false,
3672+
"dotAll": false
3673+
},
3674+
"remove": null
3675+
},
36423676
"alternatives": [
36433677
{
36443678
"type": "Alternative",

test/fixtures/parser/literal/test262/regexp-dotall.json

+18-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,24 @@
3232
"start": 1,
3333
"end": 5,
3434
"raw": "(?:)",
35-
"modifiers": null,
35+
"modifiers": {
36+
"type": "Modifiers",
37+
"parent": "♻️..",
38+
"start": 3,
39+
"end": 3,
40+
"raw": "",
41+
"add": {
42+
"type": "ModifierFlags",
43+
"parent": "♻️..",
44+
"start": 3,
45+
"end": 3,
46+
"raw": "",
47+
"ignoreCase": false,
48+
"multiline": false,
49+
"dotAll": false
50+
},
51+
"remove": null
52+
},
3653
"alternatives": [
3754
{
3855
"type": "Alternative",

test/fixtures/parser/literal/test262/regexp-duplicate-named-groups.json

+18-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,24 @@
9898
"start": 9,
9999
"end": 20,
100100
"raw": "(?:zy\\k<a>)",
101-
"modifiers": null,
101+
"modifiers": {
102+
"type": "Modifiers",
103+
"parent": "♻️..",
104+
"start": 11,
105+
"end": 11,
106+
"raw": "",
107+
"add": {
108+
"type": "ModifierFlags",
109+
"parent": "♻️..",
110+
"start": 11,
111+
"end": 11,
112+
"raw": "",
113+
"ignoreCase": false,
114+
"multiline": false,
115+
"dotAll": false
116+
},
117+
"remove": null
118+
},
102119
"alternatives": [
103120
{
104121
"type": "Alternative",

test/fixtures/parser/literal/test262/regexp-lookbehind.json

+54-3
Original file line numberDiff line numberDiff line change
@@ -2831,7 +2831,24 @@
28312831
"start": 6,
28322832
"end": 16,
28332833
"raw": "(?:b\\d{2})",
2834-
"modifiers": null,
2834+
"modifiers": {
2835+
"type": "Modifiers",
2836+
"parent": "♻️..",
2837+
"start": 8,
2838+
"end": 8,
2839+
"raw": "",
2840+
"add": {
2841+
"type": "ModifierFlags",
2842+
"parent": "♻️..",
2843+
"start": 8,
2844+
"end": 8,
2845+
"raw": "",
2846+
"ignoreCase": false,
2847+
"multiline": false,
2848+
"dotAll": false
2849+
},
2850+
"remove": null
2851+
},
28352852
"alternatives": [
28362853
{
28372854
"type": "Alternative",
@@ -3113,7 +3130,24 @@
31133130
"start": 5,
31143131
"end": 12,
31153132
"raw": "(?:\\1b)",
3116-
"modifiers": null,
3133+
"modifiers": {
3134+
"type": "Modifiers",
3135+
"parent": "♻️..",
3136+
"start": 7,
3137+
"end": 7,
3138+
"raw": "",
3139+
"add": {
3140+
"type": "ModifierFlags",
3141+
"parent": "♻️..",
3142+
"start": 7,
3143+
"end": 7,
3144+
"raw": "",
3145+
"ignoreCase": false,
3146+
"multiline": false,
3147+
"dotAll": false
3148+
},
3149+
"remove": null
3150+
},
31173151
"alternatives": [
31183152
{
31193153
"type": "Alternative",
@@ -3257,7 +3291,24 @@
32573291
"start": 5,
32583292
"end": 13,
32593293
"raw": "(?:\\1|b)",
3260-
"modifiers": null,
3294+
"modifiers": {
3295+
"type": "Modifiers",
3296+
"parent": "♻️..",
3297+
"start": 7,
3298+
"end": 7,
3299+
"raw": "",
3300+
"add": {
3301+
"type": "ModifierFlags",
3302+
"parent": "♻️..",
3303+
"start": 7,
3304+
"end": 7,
3305+
"raw": "",
3306+
"ignoreCase": false,
3307+
"multiline": false,
3308+
"dotAll": false
3309+
},
3310+
"remove": null
3311+
},
32613312
"alternatives": [
32623313
{
32633314
"type": "Alternative",

0 commit comments

Comments
 (0)