Skip to content

Commit

Permalink
feat: support for modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed May 1, 2023
1 parent 811bcaa commit d50a204
Show file tree
Hide file tree
Showing 20 changed files with 1,065 additions and 83 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"clean": "rimraf .temp index.*",
"lint": "eslint . --ext .ts",
"test": "nyc _mocha \"test/*.ts\" --reporter dot --timeout 10000",
"debug": "mocha --require ts-node/register \"test/*.ts\" --reporter dot --timeout 10000",
"update:test": "ts-node scripts/update-fixtures.ts",
"update:unicode": "run-s update:unicode:*",
"update:unicode:ids": "ts-node scripts/update-unicode-ids.ts",
Expand Down
4 changes: 4 additions & 0 deletions scripts/update-fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ for (const filename of Object.keys(Visitor.fixturesData)) {
onCharacterSetEnter: enter,
onFlagsEnter: enter,
onGroupEnter: enter,
onModifierFlagsEnter: enter,
onModifiersEnter: enter,
onPatternEnter: enter,
onQuantifierEnter: enter,
onRegExpLiteralEnter: enter,
Expand All @@ -62,6 +64,8 @@ for (const filename of Object.keys(Visitor.fixturesData)) {
onCharacterSetLeave: leave,
onFlagsLeave: leave,
onGroupLeave: leave,
onModifierFlagsLeave: leave,
onModifiersLeave: leave,
onPatternLeave: leave,
onQuantifierLeave: leave,
onRegExpLiteralLeave: leave,
Expand Down
24 changes: 24 additions & 0 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type BranchNode =
| CharacterClassRange
| Group
| LookaroundAssertion
| Modifiers
| Pattern
| Quantifier
| RegExpLiteral
Expand All @@ -26,6 +27,7 @@ export type LeafNode =
| Character
| CharacterSet
| Flags
| ModifierFlags

/**
* The type which includes all atom nodes.
Expand Down Expand Up @@ -105,6 +107,7 @@ export interface Alternative extends NodeBase {
export interface Group extends NodeBase {
type: "Group"
parent: Alternative | Quantifier
modifiers: Modifiers | null
alternatives: Alternative[]
}

Expand Down Expand Up @@ -279,6 +282,27 @@ export interface Backreference extends NodeBase {
resolved: CapturingGroup
}

/**
* The modifiers.
*/
export interface Modifiers extends NodeBase {
type: "Modifiers"
parent: Group
add: ModifierFlags | null
remove: ModifierFlags | null
}

/**
* The modifier flags.
*/
export interface ModifierFlags extends NodeBase {
type: "ModifierFlags"
parent: Modifiers
dotAll: boolean
ignoreCase: boolean
multiline: boolean
}

/**
* The flags.
*/
Expand Down
1 change: 1 addition & 0 deletions src/ecma-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export type EcmaVersion =
| 2021
| 2022
| 2023
| 2024
85 changes: 84 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
LookaroundAssertion,
Pattern,
Quantifier,
Modifiers,
} from "./ast"
import type { EcmaVersion } from "./ecma-versions"
import { HYPHEN_MINUS } from "./unicode"
Expand All @@ -22,6 +23,7 @@ type AppendableNode =
| CharacterClass
| Group
| LookaroundAssertion
| Modifiers
| Pattern

const DUMMY_PATTERN: Pattern = {} as Pattern
Expand All @@ -45,7 +47,7 @@ class RegExpParserState {

public constructor(options?: RegExpParser.Options) {
this.strict = Boolean(options?.strict)
this.ecmaVersion = options?.ecmaVersion ?? 2023
this.ecmaVersion = options?.ecmaVersion ?? 2024
}

public get pattern(): Pattern {
Expand Down Expand Up @@ -172,6 +174,7 @@ class RegExpParserState {
start,
end: start,
raw: "",
modifiers: null,
alternatives: [],
}
parent.elements.push(this._node)
Expand All @@ -188,6 +191,85 @@ class RegExpParserState {
this._node = node.parent
}

public onModifiersEnter(start: number): void {
const parent = this._node
if (parent.type !== "Group") {
throw new Error("UnknownError")
}

this._node = {
type: "Modifiers",
parent,
start,
end: start,
raw: "",
add: null,
remove: null,
}
parent.modifiers = this._node
}

public onModifiersLeave(start: number, end: number): void {
const node = this._node
if (node.type !== "Modifiers" || node.parent.type !== "Group") {
throw new Error("UnknownError")
}

node.end = end
node.raw = this.source.slice(start, end)
this._node = node.parent
}

public onAddModifiers(
start: number,
end: number,
{
ignoreCase,
multiline,
dotAll,
}: { ignoreCase: boolean; multiline: boolean; dotAll: boolean },
): void {
const parent = this._node
if (parent.type !== "Modifiers") {
throw new Error("UnknownError")
}
parent.add = {
type: "ModifierFlags",
parent,
start,
end,
raw: this.source.slice(start, end),
ignoreCase,
multiline,
dotAll,
}
}

public onRemoveModifiers(
start: number,
end: number,
{
ignoreCase,
multiline,
dotAll,
}: { ignoreCase: boolean; multiline: boolean; dotAll: boolean },
): void {
const parent = this._node
if (parent.type !== "Modifiers") {
throw new Error("UnknownError")
}
parent.remove = {
type: "ModifierFlags",
parent,
start,
end,
raw: this.source.slice(start, end),
ignoreCase,
multiline,
dotAll,
}
}

public onCapturingGroupEnter(start: number, name: string | null): void {
const parent = this._node
if (parent.type !== "Alternative") {
Expand Down Expand Up @@ -526,6 +608,7 @@ export namespace RegExpParser {
* - `2019`, `2020`, and `2021` added more valid Unicode Property Escapes.
* - `2022` added `d` flag.
* - `2023` added more valid Unicode Property Escapes.
* - `2024` added modifier.
*/
ecmaVersion?: EcmaVersion
}
Expand Down
10 changes: 0 additions & 10 deletions src/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,4 @@ export class Reader {
}
return false
}

public eat3(cp1: number, cp2: number, cp3: number): boolean {
if (this._cp1 === cp1 && this._cp2 === cp2 && this._cp3 === cp3) {
this.advance()
this.advance()
this.advance()
return true
}
return false
}
}
Loading

0 comments on commit d50a204

Please sign in to comment.