Ecma262: Early errors for invalid regular expressions

Created on 20 Dec 2017  路  7Comments  路  Source: tc39/ecma262

As it currently stands, the spec categorizes most invalid regular expression literals that don't interfere with parsing (e.g. /(/) as runtime errors. (See grammar productions for RegularExpressionLiteral; regular expression parsing errors only occur during Evaluation of such literals, through RegExpCreate and RegExpInitialize steps 7 and 8.) However, it seems that many implementations treat them as early errors, as discovered in https://github.com/ternjs/acorn/issues/640.

Specifically, the following script parses in V8, but not in SpiderMonkey or JavaScriptCore. I am not sure how it performs on ChakraCore.

if (false) (/(/);

Alternatively, this could be used as a test:

new Function('return /(/');

Most helpful comment

As it currently stands, the spec categorizes most invalid regular expression literals that don't interfere with parsing (e.g. /(/) as runtime errors ...
regular expression parsing errors only occur during Evaluation of such literals

What about the following early error?

It is a Syntax Error if BodyText of RegularExpressionLiteral
cannot be recognized using the goal symbol Pattern
of the ECMAScript RegExp grammar specified in 21.2.1. 

All 7 comments

Alternatively, this could be used as a test:

Or () => /(/

Tested with Chakra on MacOS, it also treats this as an early parsing error. So it's only V8 that delays it to the runtime.

@TimothyGu

Specifically, the following script parses in V8, but not in SpiderMonkey or JavaScriptCore. I am not sure how it performs on ChakraCore.

$ eshost -e '(() => { if (false) /(/ })()'
#### Chakra
SyntaxError: Expected ')' in regular expression

#### JavaScriptCore
SyntaxError: Invalid regular expression: missing )

#### SpiderMonkey
SyntaxError: unterminated parenthetical:

#### V8 --harmony
undefined

#### V8
undefined

As a side comment, eshost-cli + jsvu make it really easy to test across all engines.

As it currently stands, the spec categorizes most invalid regular expression literals that don't interfere with parsing (e.g. /(/) as runtime errors ...
regular expression parsing errors only occur during Evaluation of such literals

What about the following early error?

It is a Syntax Error if BodyText of RegularExpressionLiteral
cannot be recognized using the goal symbol Pattern
of the ECMAScript RegExp grammar specified in 21.2.1. 

Agreed with @jmdyck , this is already specified an early error. V8 has a bug open for this spec violation.

@jmdyck Oops, I overlooked that one.

@littledan Thanks for confirming.

Was this page helpful?
0 / 5 - 0 ratings