Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import * as AST from "./ast"
import { RegExpParser } from "./parser"
import { RegExpValidator } from "./validator"
import { RegExpVisitor } from "./visitor"
import { RegExpSyntaxError } from "./regexp-syntax-error"

export { AST, RegExpParser, RegExpValidator }
export { AST, RegExpParser, RegExpValidator, RegExpSyntaxError }

/**
* Parse a given regular expression literal then make AST object.
Expand Down
49 changes: 28 additions & 21 deletions src/regexp-syntax-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,34 @@ import type { RegExpValidatorSourceContext } from "./validator"
export class RegExpSyntaxError extends SyntaxError {
public index: number

public constructor(
srcCtx: RegExpValidatorSourceContext,
flags: { unicode: boolean; unicodeSets: boolean },
index: number,
message: string,
) {
let source = ""
if (srcCtx.kind === "literal") {
const literal = srcCtx.source.slice(srcCtx.start, srcCtx.end)
if (literal) {
source = `: ${literal}`
}
} else if (srcCtx.kind === "pattern") {
const pattern = srcCtx.source.slice(srcCtx.start, srcCtx.end)
const flagsText = `${flags.unicode ? "u" : ""}${
flags.unicodeSets ? "v" : ""
}`
source = `: /${pattern}/${flagsText}`
}

super(`Invalid regular expression${source}: ${message}`)
public constructor(message: string, index: number) {
super(message)
this.index = index
}
}

export function newRegExpSyntaxError(
srcCtx: RegExpValidatorSourceContext,
flags: { unicode: boolean; unicodeSets: boolean },
index: number,
message: string,
): RegExpSyntaxError {
let source = ""
if (srcCtx.kind === "literal") {
const literal = srcCtx.source.slice(srcCtx.start, srcCtx.end)
if (literal) {
source = `: ${literal}`
}
} else if (srcCtx.kind === "pattern") {
const pattern = srcCtx.source.slice(srcCtx.start, srcCtx.end)
const flagsText = `${flags.unicode ? "u" : ""}${
flags.unicodeSets ? "v" : ""
}`
source = `: /${pattern}/${flagsText}`
}

return new RegExpSyntaxError(
`Invalid regular expression${source}: ${message}`,
index,
)
}
4 changes: 2 additions & 2 deletions src/validator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { EcmaVersion } from "./ecma-versions"
import { latestEcmaVersion } from "./ecma-versions"
import { Reader } from "./reader"
import { RegExpSyntaxError } from "./regexp-syntax-error"
import { newRegExpSyntaxError } from "./regexp-syntax-error"
import {
ASTERISK,
BACKSPACE,
Expand Down Expand Up @@ -1246,7 +1246,7 @@ export class RegExpValidator {
message: string,
context?: { index?: number; unicode?: boolean; unicodeSets?: boolean },
): never {
throw new RegExpSyntaxError(
throw newRegExpSyntaxError(
this._srcCtx!,
{
unicode:
Expand Down