Skip to content

Commit c923637

Browse files
Merge pull request DmitrySoshnikov#73 from silentroach/feature-check-range-boundaries
check for correct boundaries in char class range
2 parents 336cd82 + f2090d4 commit c923637

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/parser/__tests__/parser-test262-test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ describe('test262', () => {
3333

3434
it('invalid char range', () => {
3535
// 15.10.2.15-6-1
36-
// TODO: /^[z-a]$/ - should throw, 'a' is less than 'z'
36+
invalid('/^[z-a]$/', 'out of order');
37+
invalid('/^[-z-a]$/', 'out of order');
3738
});
3839

3940
it('invalid quantifier range', () => {

src/parser/generated/regexp-tree.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ const productions = [[-1,1,(_1,_1loc) => { __loc = yyloc(_1loc, _1loc);__ = _1 }
274274
[19,1,(_1,_1loc) => { __loc = yyloc(_1loc, _1loc); __ = [_1] }],
275275
[19,2,(_1,_2,_1loc,_2loc) => { __loc = yyloc(_1loc, _2loc); __ = [_1].concat(_2) }],
276276
[19,4,(_1,_2,_3,_4,_1loc,_2loc,_3loc,_4loc) => { __loc = yyloc(_1loc, _4loc);
277+
checkClassRange(_1.value, _3.value);
278+
277279
__ = [
278280
Node({
279281
type: 'ClassRange',
@@ -289,6 +291,8 @@ const productions = [[-1,1,(_1,_1loc) => { __loc = yyloc(_1loc, _1loc);__ = _1 }
289291
[20,1,(_1,_1loc) => { __loc = yyloc(_1loc, _1loc);__ = _1 }],
290292
[20,2,(_1,_2,_1loc,_2loc) => { __loc = yyloc(_1loc, _2loc); __ = [_1].concat(_2) }],
291293
[20,4,(_1,_2,_3,_4,_1loc,_2loc,_3loc,_4loc) => { __loc = yyloc(_1loc, _4loc);
294+
checkClassRange(_1.value, _3.value);
295+
292296
__ = [
293297
Node({
294298
type: 'ClassRange',
@@ -845,6 +849,15 @@ function getRange(text) {
845849
return range;
846850
}
847851

852+
/**
853+
* Checks class range
854+
*/
855+
function checkClassRange(from, to) {
856+
if (from > to) {
857+
throw new SyntaxError(`Range ${from}-${to} out of order in character class`);
858+
}
859+
}
860+
848861
/**
849862
* Creates a character node.
850863
*/

src/parser/regexp.bnf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ function getRange(text) {
174174
return range;
175175
}
176176

177+
/**
178+
* Checks class range
179+
*/
180+
function checkClassRange(from, to) {
181+
if (from > to) {
182+
throw new SyntaxError(`Range ${from}-${to} out of order in character class`);
183+
}
184+
}
185+
177186
/**
178187
* Creates a character node.
179188
*/
@@ -694,6 +703,8 @@ NonemptyClassRanges
694703

695704
| ClassAtom DASH ClassAtom ClassRanges
696705
{
706+
checkClassRange($1.value, $3.value);
707+
697708
$$ = [
698709
Node({
699710
type: 'ClassRange',
@@ -715,6 +726,8 @@ NonemptyClassRangesNoDash
715726

716727
| ClassAtomNoDash DASH ClassAtom ClassRanges
717728
{
729+
checkClassRange($1.value, $3.value);
730+
718731
$$ = [
719732
Node({
720733
type: 'ClassRange',

0 commit comments

Comments
 (0)