Use "[]" & "[^]" for empty character classes, not "(?!)" & "[\\S\\s]"

See #441.
redux
David Majda 8 years ago
parent 9c04c94c85
commit bd62ddf846

@ -569,10 +569,7 @@ function generateBytecode(ast) {
}, },
"class": function(node) { "class": function(node) {
var regexp, parts, regexpIndex, expectedIndex; var regexp = '/^['
if (node.parts.length > 0) {
regexp = '/^['
+ (node.inverted ? '^' : '') + (node.inverted ? '^' : '')
+ node.parts.map(function(part) { + node.parts.map(function(part) {
return Array.isArray(part) return Array.isArray(part)
@ -581,24 +578,15 @@ function generateBytecode(ast) {
+ js.regexpClassEscape(part[1]) + js.regexpClassEscape(part[1])
: js.regexpClassEscape(part); : js.regexpClassEscape(part);
}).join('') }).join('')
+ ']/' + (node.ignoreCase ? 'i' : ''); + ']/' + (node.ignoreCase ? 'i' : ''),
} else {
/*
* IE considers regexps /[]/ and /[^]/ as syntactically invalid, so we
* translate them into equivalents it can handle.
*/
regexp = node.inverted ? '/^[\\S\\s]/' : '/^(?!)/';
}
parts = '[' parts = '['
+ node.parts.map(function(part) { + node.parts.map(function(part) {
return Array.isArray(part) return Array.isArray(part)
? '["' + js.stringEscape(part[0]) + '", "' + js.stringEscape(part[1]) + '"]' ? '["' + js.stringEscape(part[0]) + '", "' + js.stringEscape(part[1]) + '"]'
: '"' + js.stringEscape(part) + '"'; : '"' + js.stringEscape(part) + '"';
}).join(', ') }).join(', ')
+ ']'; + ']',
regexpIndex = addConst(regexp),
regexpIndex = addConst(regexp);
expectedIndex = addConst( expectedIndex = addConst(
'peg$classExpectation(' 'peg$classExpectation('
+ parts + ', ' + parts + ', '

@ -594,7 +594,7 @@ describe("compiler pass |generateBytecode|", function() {
])); ]));
}); });
describe("non-empty non-inverted case-sensitive", function() { describe("non-inverted case-sensitive", function() {
it("defines correct constants", function() { it("defines correct constants", function() {
expect(pass).toChangeAST('start = [a]', constsDetails([ expect(pass).toChangeAST('start = [a]', constsDetails([
'/^[a]/', '/^[a]/',
@ -603,7 +603,7 @@ describe("compiler pass |generateBytecode|", function() {
}); });
}); });
describe("non-empty inverted case-sensitive", function() { describe("inverted case-sensitive", function() {
it("defines correct constants", function() { it("defines correct constants", function() {
expect(pass).toChangeAST('start = [^a]', constsDetails([ expect(pass).toChangeAST('start = [^a]', constsDetails([
'/^[^a]/', '/^[^a]/',
@ -612,7 +612,7 @@ describe("compiler pass |generateBytecode|", function() {
}); });
}); });
describe("non-empty non-inverted case-insensitive", function() { describe("non-inverted case-insensitive", function() {
it("defines correct constants", function() { it("defines correct constants", function() {
expect(pass).toChangeAST('start = [a]i', constsDetails([ expect(pass).toChangeAST('start = [a]i', constsDetails([
'/^[a]/i', '/^[a]/i',
@ -621,7 +621,7 @@ describe("compiler pass |generateBytecode|", function() {
}); });
}); });
describe("non-empty complex", function() { describe("complex", function() {
it("defines correct constants", function() { it("defines correct constants", function() {
expect(pass).toChangeAST('start = [ab-def-hij-l]', constsDetails([ expect(pass).toChangeAST('start = [ab-def-hij-l]', constsDetails([
'/^[ab-def-hij-l]/', '/^[ab-def-hij-l]/',
@ -629,24 +629,6 @@ describe("compiler pass |generateBytecode|", function() {
])); ]));
}); });
}); });
describe("empty non-inverted", function() {
it("defines correct constants", function() {
expect(pass).toChangeAST('start = []', constsDetails([
'/^(?!)/',
'peg$classExpectation([], false, false)'
]));
});
});
describe("empty inverted", function() {
it("defines correct constants", function() {
expect(pass).toChangeAST('start = [^]', constsDetails([
'/^[\\S\\s]/',
'peg$classExpectation([], true, false)'
]));
});
});
}); });
describe("for any", function() { describe("for any", function() {

Loading…
Cancel
Save