mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 21:53:42 -06:00
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:
parent
853288b65f
commit
35803db2e6
@ -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;
|
||||
|
||||
@ -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'.
|
||||
5
tests/baselines/reference/parser596700.js
Normal file
5
tests/baselines/reference/parser596700.js
Normal file
@ -0,0 +1,5 @@
|
||||
//// [parser596700.ts]
|
||||
var regex2 = /[a-z/]$/i;
|
||||
|
||||
//// [parser596700.js]
|
||||
var regex2 = /[a-z/]$/i;
|
||||
@ -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.
|
||||
|
||||
8
tests/baselines/reference/parser630933.js
Normal file
8
tests/baselines/reference/parser630933.js
Normal 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=([^/]+)/);
|
||||
@ -1,8 +0,0 @@
|
||||
==== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser645086_3.ts (3 errors) ====
|
||||
var v = /[\]/]/
|
||||
~
|
||||
!!! ',' expected.
|
||||
~
|
||||
!!! Variable declaration expected.
|
||||
|
||||
!!! Expression expected.
|
||||
5
tests/baselines/reference/parser645086_3.js
Normal file
5
tests/baselines/reference/parser645086_3.js
Normal file
@ -0,0 +1,5 @@
|
||||
//// [parser645086_3.ts]
|
||||
var v = /[\]/]/
|
||||
|
||||
//// [parser645086_3.js]
|
||||
var v = /[\]/]/;
|
||||
@ -1,8 +0,0 @@
|
||||
==== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser645086_4.ts (3 errors) ====
|
||||
var v = /[^\]/]/
|
||||
~
|
||||
!!! ',' expected.
|
||||
~
|
||||
!!! Variable declaration expected.
|
||||
|
||||
!!! Expression expected.
|
||||
5
tests/baselines/reference/parser645086_4.js
Normal file
5
tests/baselines/reference/parser645086_4.js
Normal file
@ -0,0 +1,5 @@
|
||||
//// [parser645086_4.ts]
|
||||
var v = /[^\]/]/
|
||||
|
||||
//// [parser645086_4.js]
|
||||
var v = /[^\]/]/;
|
||||
@ -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'.
|
||||
5
tests/baselines/reference/parserRegularExpression2.js
Normal file
5
tests/baselines/reference/parserRegularExpression2.js
Normal file
@ -0,0 +1,5 @@
|
||||
//// [parserRegularExpression2.ts]
|
||||
href.match(/:\/\/(.[^/]+)/)[1];
|
||||
|
||||
//// [parserRegularExpression2.js]
|
||||
href.match(/:\/\/(.[^/]+)/)[1];
|
||||
10
tests/baselines/reference/regExpWithSlashInCharClass.js
Normal file
10
tests/baselines/reference/regExpWithSlashInCharClass.js
Normal 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");
|
||||
@ -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;
|
||||
3
tests/cases/compiler/regExpWithSlashInCharClass.ts
Normal file
3
tests/cases/compiler/regExpWithSlashInCharClass.ts
Normal file
@ -0,0 +1,3 @@
|
||||
var foo1 = "a/".replace(/.[/]/, "");
|
||||
var foo2 = "a//".replace(/.[//]/g, "");
|
||||
var foo3 = "a/".replace(/.[/no sleep /till/]/, "bugfix");
|
||||
Loading…
x
Reference in New Issue
Block a user