Skip to content

Commit

Permalink
change ast
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Mar 17, 2024
1 parent 7b10d7c commit fb0dade
Show file tree
Hide file tree
Showing 10 changed files with 1,454 additions and 190 deletions.
13 changes: 11 additions & 2 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export interface Alternative extends NodeBase {
export interface Group extends NodeBase {
type: "Group"
parent: Alternative | Quantifier
modifiers: Modifiers | null
modifiers: Modifiers
alternatives: Alternative[]
}

Expand Down Expand Up @@ -444,7 +444,16 @@ export interface Backreference extends NodeBase {
export interface Modifiers extends NodeBase {
type: "Modifiers"
parent: Group
add: ModifierFlags | null
/**
* The add modifier flags.
*/
add: ModifierFlags
/**
* The remove modifier flags.
*
* `null` means no remove modifier flags. e.g. `(?ims:x)`
* The reason for `null` is that there is no position where the remove modifier flags appears. Must be behind the minus mark.
*/
remove: ModifierFlags | null
}

Expand Down
4 changes: 2 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class RegExpParserState {
start,
end: start,
raw: "",
modifiers: null,
modifiers: null as never, // Set in onModifiersEnter.
alternatives: [],
}
parent.elements.push(this._node)
Expand Down Expand Up @@ -233,7 +233,7 @@ class RegExpParserState {
start,
end: start,
raw: "",
add: null,
add: null as never, // Set in onAddModifiers.
remove: null,
}
parent.modifiers = this._node
Expand Down
48 changes: 15 additions & 33 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1746,44 +1746,26 @@ export class RegExpValidator {
*/
private consumeModifiers(): boolean {
const start = this.index
this.onModifiersEnter(start)
const hasAddModifiers = this.eatModifiers()
const addModifiers = this.parseModifiers(start, this.index)
this.onAddModifiers(start, this.index, addModifiers)

if (this.eatModifiers()) {
this.onModifiersEnter(start)
const addModifiers = this.parseModifiers(start, this.index)
this.onAddModifiers(start, this.index, addModifiers)
if (this.eat(HYPHEN_MINUS)) {
const modifiersStart = this.index
if (this.eatModifiers()) {
const modifiers = this.parseModifiers(
modifiersStart,
this.index,
addModifiers,
)
this.onRemoveModifiers(
modifiersStart,
this.index,
modifiers,
)
}
}
this.onModifiersLeave(start, this.index)
return true
} else if (this.eat(HYPHEN_MINUS)) {
this.onModifiersEnter(start)
if (this.eat(HYPHEN_MINUS)) {
const modifiersStart = this.index
if (this.eatModifiers()) {
const modifiers = this.parseModifiers(
modifiersStart,
this.index,
)
this.onRemoveModifiers(modifiersStart, this.index, modifiers)
} else {
if (!this.eatModifiers() && !hasAddModifiers) {
this.raise("Invalid empty flags")
}
this.onModifiersLeave(start, this.index)
return true
const modifiers = this.parseModifiers(
modifiersStart,
this.index,
addModifiers,
)
this.onRemoveModifiers(modifiersStart, this.index, modifiers)
}
return false

this.onModifiersLeave(start, this.index)
return true
}

/**
Expand Down
30 changes: 28 additions & 2 deletions test/fixtures/parser/literal/modifiers-valid-2024.json
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,16 @@
"start": 3,
"end": 7,
"raw": "-ims",
"add": null,
"add": {
"type": "ModifierFlags",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"ignoreCase": false,
"multiline": false,
"dotAll": false
},
"remove": {
"type": "ModifierFlags",
"parent": "♻️..",
Expand Down Expand Up @@ -317,7 +326,24 @@
"start": 1,
"end": 17,
"raw": "(?:no-modifiers)",
"modifiers": null,
"modifiers": {
"type": "Modifiers",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"add": {
"type": "ModifierFlags",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"ignoreCase": false,
"multiline": false,
"dotAll": false
},
"remove": null
},
"alternatives": [
{
"type": "Alternative",
Expand Down
38 changes: 36 additions & 2 deletions test/fixtures/parser/literal/test262/not-categorized.json
Original file line number Diff line number Diff line change
Expand Up @@ -3510,7 +3510,24 @@
"start": 1,
"end": 10,
"raw": "(?:ab|cd)",
"modifiers": null,
"modifiers": {
"type": "Modifiers",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"add": {
"type": "ModifierFlags",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"ignoreCase": false,
"multiline": false,
"dotAll": false
},
"remove": null
},
"alternatives": [
{
"type": "Alternative",
Expand Down Expand Up @@ -3638,7 +3655,24 @@
"start": 1,
"end": 10,
"raw": "(?:ab|cd)",
"modifiers": null,
"modifiers": {
"type": "Modifiers",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"add": {
"type": "ModifierFlags",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"ignoreCase": false,
"multiline": false,
"dotAll": false
},
"remove": null
},
"alternatives": [
{
"type": "Alternative",
Expand Down
19 changes: 18 additions & 1 deletion test/fixtures/parser/literal/test262/regexp-dotall.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,24 @@
"start": 1,
"end": 5,
"raw": "(?:)",
"modifiers": null,
"modifiers": {
"type": "Modifiers",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"add": {
"type": "ModifierFlags",
"parent": "♻️..",
"start": 3,
"end": 3,
"raw": "",
"ignoreCase": false,
"multiline": false,
"dotAll": false
},
"remove": null
},
"alternatives": [
{
"type": "Alternative",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,24 @@
"start": 9,
"end": 20,
"raw": "(?:zy\\k<a>)",
"modifiers": null,
"modifiers": {
"type": "Modifiers",
"parent": "♻️..",
"start": 11,
"end": 11,
"raw": "",
"add": {
"type": "ModifierFlags",
"parent": "♻️..",
"start": 11,
"end": 11,
"raw": "",
"ignoreCase": false,
"multiline": false,
"dotAll": false
},
"remove": null
},
"alternatives": [
{
"type": "Alternative",
Expand Down
57 changes: 54 additions & 3 deletions test/fixtures/parser/literal/test262/regexp-lookbehind.json
Original file line number Diff line number Diff line change
Expand Up @@ -2831,7 +2831,24 @@
"start": 6,
"end": 16,
"raw": "(?:b\\d{2})",
"modifiers": null,
"modifiers": {
"type": "Modifiers",
"parent": "♻️..",
"start": 8,
"end": 8,
"raw": "",
"add": {
"type": "ModifierFlags",
"parent": "♻️..",
"start": 8,
"end": 8,
"raw": "",
"ignoreCase": false,
"multiline": false,
"dotAll": false
},
"remove": null
},
"alternatives": [
{
"type": "Alternative",
Expand Down Expand Up @@ -3113,7 +3130,24 @@
"start": 5,
"end": 12,
"raw": "(?:\\1b)",
"modifiers": null,
"modifiers": {
"type": "Modifiers",
"parent": "♻️..",
"start": 7,
"end": 7,
"raw": "",
"add": {
"type": "ModifierFlags",
"parent": "♻️..",
"start": 7,
"end": 7,
"raw": "",
"ignoreCase": false,
"multiline": false,
"dotAll": false
},
"remove": null
},
"alternatives": [
{
"type": "Alternative",
Expand Down Expand Up @@ -3257,7 +3291,24 @@
"start": 5,
"end": 13,
"raw": "(?:\\1|b)",
"modifiers": null,
"modifiers": {
"type": "Modifiers",
"parent": "♻️..",
"start": 7,
"end": 7,
"raw": "",
"add": {
"type": "ModifierFlags",
"parent": "♻️..",
"start": 7,
"end": 7,
"raw": "",
"ignoreCase": false,
"multiline": false,
"dotAll": false
},
"remove": null
},
"alternatives": [
{
"type": "Alternative",
Expand Down
Loading

0 comments on commit fb0dade

Please sign in to comment.