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

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

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

@ -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