Fixed error when parsing slashes in RegExp literals.

Basically we weren't recognizing that a slash can occur in a character class, so we were bailing out too early on code like `/[/]/`.

Fixes issue #318.
This commit is contained in:
Daniel Rosenwasser 2014-07-31 00:32:41 -07:00 committed by Daniel Rosenwasser
parent 853288b65f
commit 35803db2e6
14 changed files with 62 additions and 67 deletions

View File

@ -831,34 +831,46 @@ module ts {
if (token === SyntaxKind.SlashToken || token === SyntaxKind.SlashEqualsToken) {
var p = tokenPos + 1;
var inEscape = false;
var inClass = false;
var inCharacterClass = false;
while (true) {
// If we've hit EOF without closing off the regex,
// simply return the token we originally parsed.
if (p >= len) {
return token;
}
var ch = text.charCodeAt(p);
// Line breaks are not permissible in the middle of a RegExp.
if (isLineBreak(ch)) {
return token;
}
if (inEscape) {
// Parsing an escape character;
// reset the flag and just advance to the next char.
inEscape = false;
}
else if (ch === CharacterCodes.slash) {
else if (ch === CharacterCodes.slash && !inCharacterClass) {
// A slash within a character class is permissible,
// but in general it signals the end of the regexp literal.
break;
}
else if (ch === CharacterCodes.openBracket) {
inClass = true;
inCharacterClass = true;
}
else if (ch === CharacterCodes.backslash) {
inEscape = true;
}
else if (ch === CharacterCodes.closeBracket) {
inClass = false;
inCharacterClass = false;
}
p++;
}
p++;
while (isIdentifierPart(text.charCodeAt(p))) p++;
while (isIdentifierPart(text.charCodeAt(p))) {
p++;
}
pos = p;
tokenValue = text.substring(tokenPos, pos);
token = SyntaxKind.RegularExpressionLiteral;

View File

@ -1,8 +0,0 @@
==== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser596700.ts (3 errors) ====
var regex2 = /[a-z/]$/i;
~
!!! ',' expected.
~
!!! '=' expected.
~
!!! Cannot find name 'i'.

View File

@ -0,0 +1,5 @@
//// [parser596700.ts]
var regex2 = /[a-z/]$/i;
//// [parser596700.js]
var regex2 = /[a-z/]$/i;

View File

@ -1,12 +0,0 @@
==== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser630933.ts (4 errors) ====
var a = "Hello";
var b = a.match(/\/ver=([^/]+)/);
~
!!! ',' expected.
~
!!! Expression expected.
~
!!! Expression expected.
~~~~~~~~~~~~~~~~~~~~~~
!!! Supplied parameters do not match any signature of call target.

View File

@ -0,0 +1,8 @@
//// [parser630933.ts]
var a = "Hello";
var b = a.match(/\/ver=([^/]+)/);
//// [parser630933.js]
var a = "Hello";
var b = a.match(/\/ver=([^/]+)/);

View File

@ -1,8 +0,0 @@
==== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser645086_3.ts (3 errors) ====
var v = /[\]/]/
~
!!! ',' expected.
~
!!! Variable declaration expected.
!!! Expression expected.

View File

@ -0,0 +1,5 @@
//// [parser645086_3.ts]
var v = /[\]/]/
//// [parser645086_3.js]
var v = /[\]/]/;

View File

@ -1,8 +0,0 @@
==== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser645086_4.ts (3 errors) ====
var v = /[^\]/]/
~
!!! ',' expected.
~
!!! Variable declaration expected.
!!! Expression expected.

View File

@ -0,0 +1,5 @@
//// [parser645086_4.ts]
var v = /[^\]/]/
//// [parser645086_4.js]
var v = /[^\]/]/;

View File

@ -1,10 +1,4 @@
==== tests/cases/conformance/parser/ecmascript5/RegularExpressions/parserRegularExpression2.ts (4 errors) ====
==== tests/cases/conformance/parser/ecmascript5/RegularExpressions/parserRegularExpression2.ts (1 errors) ====
href.match(/:\/\/(.[^/]+)/)[1];
~
!!! ',' expected.
~
!!! Expression expected.
~
!!! Expression expected.
~~~~
!!! Cannot find name 'href'.

View File

@ -0,0 +1,5 @@
//// [parserRegularExpression2.ts]
href.match(/:\/\/(.[^/]+)/)[1];
//// [parserRegularExpression2.js]
href.match(/:\/\/(.[^/]+)/)[1];

View File

@ -0,0 +1,10 @@
//// [regExpWithSlashInCharClass.ts]
var foo1 = "a/".replace(/.[/]/, "");
var foo2 = "a//".replace(/.[//]/g, "");
var foo3 = "a/".replace(/.[/no sleep /till/]/, "bugfix");
//// [regExpWithSlashInCharClass.js]
var foo1 = "a/".replace(/.[/]/, "");
var foo2 = "a//".replace(/.[//]/g, "");
var foo3 = "a/".replace(/.[/no sleep /till/]/, "bugfix");

View File

@ -1,22 +1,6 @@
==== tests/cases/compiler/validRegexp.ts (9 errors) ====
==== tests/cases/compiler/validRegexp.ts (1 errors) ====
var x = / [a - z /]$ / i;
~
!!! ',' expected.
~
!!! '=' expected.
~
!!! Cannot find name 'i'.
!!! ',' expected.
var x1 = /[a-z/]$/i;
~
!!! ',' expected.
~
!!! '=' expected.
~
!!! Cannot find name 'i'.
var x2 = /[a-z/]$ /i;
~
!!! ',' expected.
~
!!! '=' expected.
~
!!! Cannot find name 'i'.
var x2 = /[a-z/]$ /i;

View File

@ -0,0 +1,3 @@
var foo1 = "a/".replace(/.[/]/, "");
var foo2 = "a//".replace(/.[//]/g, "");
var foo3 = "a/".replace(/.[/no sleep /till/]/, "bugfix");